-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
379 lines (315 loc) · 8.84 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package main
import (
"bufio"
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"gopkg.in/gcfg.v1"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"sync"
"text/template"
)
type сonsulServiceResp struct {
ID string
Node string
Address string
Datacenter string
TaggedAddresses struct {
Lan string
Wan string
}
NodeMeta struct {
}
ServiceID string
ServiceName string
ServiceTags []string
ServiceAddress string
ServicePort int
ServiceEnableTagOverride bool
CreateIndex int
ModifyIndex int
}
type сonsulKVResp struct {
LockIndex int
Key string
Flags int
Value string
CreateIndex int
ModifyIndex int
}
type vaultHealthResp struct {
ClusterID string
ClusterName string
Version string
ServerTimeUtc int
Standby bool
Sealed bool
Initialized bool
}
type vaultInitResp struct {
Keys []string `json:"keys"`
KeysBase64 []string `json:"keys_base64"`
RootToken string `json:"root_token"`
}
type Config struct {
Vault struct {
Scheme string
Name string
}
Init struct {
Save bool
Shares string
Threshold string
}
Consul struct {
Addr string
Scheme string
}
}
const (
configFile = "vault-cli.conf"
consulTokenEnv = "CONSUL_HTTP_TOKEN"
)
func respHTTP(url, methodReq string, metadataHTTP map[string]string, dataHTTP []byte) []byte {
client := &http.Client{}
httpReq, _ := http.NewRequest(methodReq, url, bytes.NewBuffer(dataHTTP))
for key, value := range metadataHTTP {
httpReq.Header.Add(key, value)
}
httpResp, err := client.Do(httpReq)
if err != nil {
fmt.Println(err)
}
body, err := ioutil.ReadAll(httpResp.Body)
if err != nil {
fmt.Println(err)
}
defer httpResp.Body.Close()
return body
}
func getNodeOfService(consulScheme, consulAddr, consulToken, svcName string) map[string][]string {
var Service []сonsulServiceResp
var metadataHTTP = map[string]string{
"X-Consul-Token": consulToken,
}
apiUrlService := fmt.Sprintf("%s://%s:/v1/catalog/service/%s", consulScheme, consulAddr, svcName)
body := respHTTP(apiUrlService, "GET", metadataHTTP, nil)
err := json.Unmarshal(body, &Service)
if err != nil {
fmt.Println(err)
return nil
}
var valueSvc = make(map[string][]string)
for _, srv := range Service {
valueSvc[string(srv.CreateIndex)] = []string{srv.Address, strconv.Itoa(srv.ServicePort)}
//valueSvc[string(srv.Node)] = []string{ srv.Address, strconv.Itoa(srv.ServicePort) }
}
return valueSvc
}
func getKVValue(consulScheme, consulAddr, consulToken, keyKV string) string {
var Value []сonsulKVResp
var valueKV string
var metadataHTTP = map[string]string{
"X-Consul-Token": consulToken,
}
apiUrlKV := fmt.Sprintf("%s://%s:/v1/kv/%s", consulScheme, consulAddr, keyKV)
body := respHTTP(apiUrlKV, "GET", metadataHTTP, nil)
err := json.Unmarshal(body, &Value)
if err != nil {
fmt.Println(err)
}
for _, srv := range Value {
valueKVByte, _ := base64.StdEncoding.DecodeString(srv.Value)
valueKV = string(valueKVByte)
}
return valueKV
}
func getVaultHealth(healthKey, vaultAddr string) bool {
var healthResponse vaultHealthResp
apiUrlHealth := fmt.Sprintf("%s/v1/sys/health", vaultAddr)
body := respHTTP(apiUrlHealth, "GET", nil, nil)
err := json.Unmarshal(body, &healthResponse)
if err != nil {
fmt.Println(err)
}
switch healthKey {
case "Sealed":
status := healthResponse.Sealed
return status
case "Initialized":
status := healthResponse.Initialized
return status
default:
return false
}
}
func vaultInit(vaultAddr, secretShares, secretThreshold string) vaultInitResp {
var vaultInit vaultInitResp
var metadataHTTP = map[string]string{
"Content-Type": "application/json",
}
apiUrlInit := fmt.Sprintf("%s/v1/sys/init", vaultAddr)
data := []byte(fmt.Sprintf(`{"secret_shares": %s,"secret_threshold": %s}`, secretShares, secretThreshold))
body := respHTTP(apiUrlInit, "POST", metadataHTTP, data)
err := json.Unmarshal(body, &vaultInit)
if err != nil {
fmt.Println(err)
}
return vaultInit
}
func vaultUnsealNode(nodeAddr, unsealKey string, wg *sync.WaitGroup) {
var metadataHTTP = map[string]string{
"Content-Type": "application/json",
}
defer wg.Done()
data := []byte(fmt.Sprintf(`{"key": "%s"}`, unsealKey))
apiUrlUnseal := fmt.Sprintf("%s/v1/sys/unseal", nodeAddr)
respHTTP(apiUrlUnseal, "POST", metadataHTTP, data)
}
func vaultBootstrap() {
var (
keys []string
initStatus bool
tmplToBuf bytes.Buffer
)
const unsealKeyTmpl = `{{ block "list" .}}` +
`{{- range $index, $element := .KeysBase64 }}` +
`Unseal Key {{ inc $index }}: {{ $element -}}{{"\n"}}` +
`{{ end }}` +
`{{"\n"}}Initial Root Token: {{ .RootToken }}{{"\n\n"}}` +
`{{ end }}`
consulToken := os.Getenv(consulTokenEnv)
config := readConfig(configFile)
vaultScheme := getKVValue(config.Consul.Scheme, config.Consul.Addr, consulToken, config.Vault.Scheme)
service := getNodeOfService(config.Consul.Scheme, config.Consul.Addr, consulToken, config.Vault.Name)
for _, urlNode := range service {
nodeAddr := fmt.Sprintf("%s://%s:%s", vaultScheme, urlNode[0], urlNode[1])
initStatus = getVaultHealth("Initialized", nodeAddr)
if initStatus == true {
fmt.Println("* Vault is already initialized")
} else {
resp := vaultInit(nodeAddr, config.Init.Shares, config.Init.Threshold)
keys = resp.KeysBase64
funcMap := template.FuncMap{
"inc": func(i int) int {
return i + 1
},
}
tpl := template.Must(template.New("tmpl").Funcs(funcMap).Parse(unsealKeyTmpl))
tpl.Execute(os.Stdout, resp)
if config.Init.Save == true {
tpl.Execute(&tmplToBuf, resp)
ioutil.WriteFile("vault-keys", []byte(tmplToBuf.String()), 0600)
}
}
break
}
if initStatus == false {
threshold, _ := strconv.Atoi(config.Init.Threshold)
for nodeName, urlNode := range service {
nodeAddr := fmt.Sprintf("%s://%s:%s", vaultScheme, urlNode[0], urlNode[1])
fmt.Printf("* Unseal node %s: %s\n\n", nodeName, nodeAddr)
wg := &sync.WaitGroup{}
for num, key := range keys[:threshold] {
fmt.Printf("Use key %d: %s\n", num+1, key)
wg.Add(1)
go vaultUnsealNode(nodeAddr, key, wg)
}
wg.Wait()
sealStatus := getVaultHealth("Sealed", nodeAddr)
if sealStatus == false {
fmt.Println("\n* Node successful unsealed\n")
} else {
fmt.Println("\n* Node didn't unsealed\n")
}
}
}
}
func vaultUnsealCluster() {
var (
keys []string
sealStatus bool
nodeAddr string
)
consulToken := os.Getenv(consulTokenEnv)
config := readConfig(configFile)
in := bufio.NewReader(os.Stdin)
keyString, err := in.ReadString('\n')
keyString = strings.Replace(keyString, "\n", "", -1)
if err == nil {
keys = strings.Split(string(keyString), " ")
}
if strconv.Itoa(len(keys)) != config.Init.Threshold {
panic(fmt.Sprintf("The number of 'unseal key' should be equal %s, try again.", config.Init.Threshold))
}
vaultScheme := getKVValue(config.Consul.Scheme, config.Consul.Addr, consulToken, config.Vault.Scheme)
service := getNodeOfService(config.Consul.Scheme, config.Consul.Addr, consulToken, config.Vault.Name)
for nodeName, urlNode := range service {
nodeAddr = fmt.Sprintf("%s://%s:%s", vaultScheme, urlNode[0], urlNode[1])
fmt.Printf("* Unseal node %s: %s\n\n", nodeName, nodeAddr)
sealStatus = getVaultHealth("Sealed", nodeAddr)
if sealStatus == false {
fmt.Printf("* Node %s already unsealed\n\n", nodeName)
} else {
wg := &sync.WaitGroup{}
for num, key := range keys {
fmt.Printf("Use key %d: %s\n", num+1, key)
wg.Add(1)
go vaultUnsealNode(nodeAddr, key, wg)
}
wg.Wait()
sealStatus = getVaultHealth("Sealed", nodeAddr)
if sealStatus == false {
fmt.Println("\n* Node successful unsealed\n")
} else {
fmt.Println("\n* Node didn't unsealed\n")
}
}
}
}
func readConfig(configFile string) (cfg Config) {
err := gcfg.ReadFileInto(&cfg, configFile)
if err != nil {
fmt.Println(err)
}
if cfg.Vault.Scheme == "default" {
cfg.Vault.Scheme = "http"
}
if cfg.Consul.Scheme == "default" {
cfg.Consul.Scheme = "http"
}
if cfg.Consul.Addr == "default" {
cfg.Consul.Addr = "127.0.0.1:8500"
}
return cfg
}
func main() {
helpMessage := "Usage: vault-cli <command>\n\nCommon commands:\n" +
"* bootstrap\t Bootstrap Vault cluster\n" +
"* unseal\t Unseal vault cluster"
consulTokenErrMessage := fmt.Sprintf("* Variable '%s' is not set.", consulTokenEnv)
if len(os.Args) == 2 {
if os.Args[1] == "bootstrap" || os.Args[1] == "b" {
if os.Getenv(consulTokenEnv) == "" {
fmt.Println(consulTokenErrMessage)
} else {
vaultBootstrap()
}
} else if os.Args[1] == "unseal" || os.Args[1] == "u" {
if os.Getenv(consulTokenEnv) == "" {
fmt.Println(consulTokenErrMessage)
} else {
vaultUnsealCluster()
}
} else {
fmt.Println(helpMessage)
}
} else {
fmt.Println(helpMessage)
}
}