-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
165 lines (148 loc) · 4.09 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"encoding/base64"
"errors"
"flag"
"io/ioutil"
"net"
"net/http"
"os"
"strings"
"github.com/coreos/go-systemd/activation"
"github.com/hashicorp/go-hclog"
vault_api "github.com/hashicorp/vault/api"
)
var (
flagAddr string
flagVaultAddr string
)
func init() {
flag.StringVar(&flagAddr, "listen-addr", "127.0.0.1:8080", "Address to serve")
flag.StringVar(&flagVaultAddr, "vault-addr", "", "Adress of Vault server")
}
// fileExists checks if a file exists and is not a directory before we
// try using it to prevent further errors.
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func newVaultClient() (*vault_api.Client, error) {
var config *vault_api.Config
if flagVaultAddr != "" {
config = &vault_api.Config{
Address: flagVaultAddr,
}
}
vault_client, err := vault_api.NewClient(config)
if err != nil {
hclog.Default().Error("failed to create Vault API client", "err", err)
return nil, err
}
if _, ok := os.LookupEnv("VAULT_TOKEN"); !ok {
token_filename := os.Getenv("HOME") + "/.vault-token"
if !fileExists(token_filename) {
var err = errors.New("there is no such file: " + token_filename)
hclog.Default().Error("vault token is not specified", "err", err)
return nil, err
}
token, err := ioutil.ReadFile(token_filename)
if err != nil {
hclog.Default().Error("couldn't read token", "err", err)
return nil, err
}
//hclog.Default().Info("couldn't read token", "token", string(token))
vault_client.SetToken(strings.TrimSpace(string(token)))
}
if res, err := vault_client.Sys().Health(); err != nil {
hclog.Default().Error("failed to check vault's health", "err", err)
return nil, err
} else {
hclog.Default().Info("vault's healthy", "version", res.Version)
}
return vault_client, nil
}
func getListener(flagAddr string) (net.Listener, error) {
if listeners, err := activation.Listeners(); err != nil || len(listeners) == 0 {
return net.Listen("tcp", flagAddr)
} else {
return listeners[0], nil
}
}
func main() {
flag.Parse()
hclog.Default().SetLevel(hclog.Info)
secret_path := os.Getenv("TFSTATE_SECRET_PATH")
if secret_path == "" {
secret_path = "secret/data/tfstate"
}
hclog.Default().Info("tfstate stored at", "path", secret_path)
vault_client, err := newVaultClient()
if err != nil {
os.Exit(1)
}
vault := vault_client.Logical()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(500)
hclog.Default().Info("couldn't read request body", "err", err)
return
}
encoded_body := base64.StdEncoding.EncodeToString(body)
_, err = vault.Write(secret_path, map[string]interface{}{"data": map[string]interface{}{"data": encoded_body}})
if err != nil {
w.WriteHeader(500)
hclog.Default().Info("couldn't write tfstate", "err", err)
return
}
hclog.Default().Info("wrote state", "data", string(body))
case "GET":
secret, err := vault.Read(secret_path)
if err != nil {
w.WriteHeader(500)
hclog.Default().Info("couldn't read tfstate", "err", err)
return
}
if secret == nil {
return
}
data := secret.Data["data"]
if data != nil {
data = (data.(map[string]interface{}))["data"]
}
if data != nil {
decoded_data, err := base64.StdEncoding.DecodeString(data.(string))
if err != nil {
hclog.Default().Info("couldn't decode base64", "err", err)
w.WriteHeader(500)
return
}
_, err = w.Write(decoded_data)
if err != nil {
hclog.Default().Info("error writing response", "err", err)
return
}
hclog.Default().Debug("read state", "data", string(decoded_data))
}
case "LOCK":
w.WriteHeader(405)
case "UNLOCK":
w.WriteHeader(405)
default:
w.WriteHeader(405)
}
})
lis, err := getListener(flagAddr)
if err != nil {
hclog.Default().Error("failed to get listener", "err", err)
os.Exit(1)
}
if err := http.Serve(lis, nil); err != nil {
hclog.Default().Error("failed to serve HTTP", "err", err)
}
}