-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathvault.go
562 lines (489 loc) · 13.5 KB
/
vault.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
package main
import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"github.com/cloudfoundry-community/vaultkv"
)
type Vault struct {
URL string
Token string
Insecure bool
HTTP *http.Client
}
type VaultCreds struct {
SealKey string `json:"seal_key"`
RootToken string `json:"root_token"`
}
func (vault *Vault) Init(store string) error {
l := Logger.Wrap("vault init")
l.Debug("checking initialization state of the vault")
res, err := vault.Do("GET", "/v1/sys/init", nil)
if err != nil {
l.Error("failed to check initialization state of the vault: %s", err)
return err
}
defer func() {
ioutil.ReadAll(res.Body)
res.Body.Close()
}()
b, err := ioutil.ReadAll(res.Body)
if err != nil {
l.Error("failed to read response from the vault, concerning its initialization state: %s", err)
return err
}
var init struct {
Initialized bool `json:"initialized"`
}
if err = json.Unmarshal(b, &init); err != nil {
l.Error("failed to parse response from the vault, concerning its initialization state: %s", err)
return err
}
if init.Initialized {
l.Info("vault is already initialized")
l.Debug("reading credentials files from %s", store)
b, err := ioutil.ReadFile(store)
if err != nil {
l.Error("failed to read vault credentials from %s: %s", store, err)
return err
}
creds := VaultCreds{}
err = json.Unmarshal(b, &creds)
if err != nil {
l.Error("failed to parse vault credentials from %s: %s", store, err)
return err
}
vault.Token = creds.RootToken
os.Setenv("VAULT_TOKEN", vault.Token)
vault.updateHomeDirs()
return vault.Unseal(creds.SealKey)
}
//////////////////////////////////////////
l.Info("initializing the vault with 1/1 keys")
res, err = vault.Do("PUT", "/v1/sys/init", map[string]int{
"secret_shares": 1,
"secret_threshold": 1,
})
if err != nil {
l.Error("failed to initialize the vault: %s", err)
return err
}
defer func() {
ioutil.ReadAll(res.Body)
res.Body.Close()
}()
b, err = ioutil.ReadAll(res.Body)
if err != nil {
l.Error("failed to read response from the vault, concerning our initialization attempt: %s", err)
return err
}
var keys struct {
RootToken string `json:"root_token"`
Keys []string `json:"keys"`
}
if err = json.Unmarshal(b, &keys); err != nil {
l.Error("failed to parse response from the vault, concerning our initialization attempt: %s", err)
return err
}
if keys.RootToken == "" || len(keys.Keys) != 1 {
if keys.RootToken == "" {
l.Error("failed to initialize vault: root token was blank")
}
if len(keys.Keys) != 1 {
l.Error("failed to initialize vault: incorrect number of seal keys (%d) returned", len(keys.Keys))
}
err = fmt.Errorf("invalid response from vault: token '%s' and %d keys", keys.RootToken, len(keys.Keys))
return err
}
creds := VaultCreds{
SealKey: keys.Keys[0],
RootToken: keys.RootToken,
}
l.Debug("marshaling credentials for longterm storage")
b, err = json.Marshal(creds)
if err != nil {
l.Error("failed to marshal vault root token / seal key for longterm storage: %s", err)
return err
}
l.Debug("storing credentials at %s (mode 0600)", store)
err = ioutil.WriteFile(store, b, 0600)
if err != nil {
l.Error("failed to write credentials to longterm storage file %s: %s", store, err)
return err
}
vault.Token = creds.RootToken
os.Setenv("VAULT_TOKEN", vault.Token)
vault.updateHomeDirs()
return vault.Unseal(creds.SealKey)
}
func (vault *Vault) Unseal(key string) error {
l := Logger.Wrap("vault unseal")
l.Debug("checking current seal status of the vault")
res, err := vault.Do("GET", "/v1/sys/seal-status", nil)
if err != nil {
l.Error("failed to check current seal status of the vault: %s", err)
return err
}
defer func() {
ioutil.ReadAll(res.Body)
res.Body.Close()
}()
b, err := ioutil.ReadAll(res.Body)
if err != nil {
l.Error("failed to read response from the vault, concerning current seal status: %s", err)
return err
}
var status struct {
Sealed bool `json:"sealed"`
}
err = json.Unmarshal(b, &status)
if err != nil {
l.Error("failed to parse response from the vault, concerning current seal status: %s", err)
return err
}
if !status.Sealed {
l.Info("vault is already unsealed")
return nil
}
//////////////////////////////////////////
l.Info("vault is sealed; unsealing it")
res, err = vault.Do("POST", "/v1/sys/unseal", map[string]string{
"key": key,
})
if err != nil {
l.Error("failed to unseal vault: %s", err)
return err
}
defer func() {
ioutil.ReadAll(res.Body)
res.Body.Close()
}()
b, err = ioutil.ReadAll(res.Body)
if err != nil {
l.Error("failed to read response from the vault, concerning our unseal attempt: %s", err)
return err
}
err = json.Unmarshal(b, &status)
if err != nil {
l.Error("failed to parse response from the vault, concerning our unseal attempt: %s", err)
return err
}
if status.Sealed {
err = fmt.Errorf("vault is still sealed after unseal attempt")
l.Error("%s", err)
return err
}
l.Info("unsealed the vault")
return nil
}
func (vault *Vault) VerifyMount(store string, createIfMissing bool) error {
l := Logger.Wrap("Verify Mount")
vault_url, err := url.Parse(vault.URL)
if err != nil {
l.Error("vault URL is invalid: %s", err)
return err
}
kvvault := &vaultkv.Client{
AuthToken: vault.Token,
VaultURL: vault_url,
Client: &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: vault.Insecure,
},
},
},
Trace: os.Stdout,
}
l.Debug("checking vault has the secret mount created")
var mounts []string
var mountMap map[string]vaultkv.Mount
mountMap, err = kvvault.ListMounts()
if err != nil {
return err
}
for k := range mountMap {
mounts = append(mounts, k)
}
for _, mount := range mounts {
if strings.Trim(store, "/") == strings.Trim(mount, "/") {
l.Debug("Found secret mount %s", store)
return nil // Path is found
}
}
if createIfMissing {
return kvvault.EnableSecretsMount(store, vaultkv.Mount{
Type: "kv",
Description: fmt.Sprintf("A KV v%d Mount created by safe", 1),
Options: vaultkv.KVMountOptions{}.WithVersion(1),
})
}
return errors.New(fmt.Sprintf("Secret mount %s is missing", store))
}
func (vault *Vault) NewRequest(method, url string, data interface{}) (*http.Request, error) {
if data == nil {
return http.NewRequest(method, url, nil)
}
cooked, err := json.Marshal(data)
if err != nil {
return nil, err
}
return http.NewRequest(method, url, strings.NewReader(string(cooked)))
}
func (vault *Vault) Do(method, url string, data interface{}) (*http.Response, error) {
req, err := vault.NewRequest(method, fmt.Sprintf("%s%s", vault.URL, url), data)
if err != nil {
return nil, err
}
req.Header.Add("X-Vault-Token", vault.Token)
return vault.HTTP.Do(req)
}
func (vault *Vault) Get(path string, out interface{}) (bool, error) {
exists := false
res, err := vault.Do("GET", fmt.Sprintf("/v1/secret/%s", path), nil)
if err != nil {
return exists, err
}
defer func() {
ioutil.ReadAll(res.Body)
res.Body.Close()
}()
if res.StatusCode == 404 {
return exists, nil
}
if res.StatusCode != 200 && res.StatusCode != 204 {
return exists, fmt.Errorf("API %s", res.Status)
}
exists = true
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return exists, err
}
if out == nil {
return exists, nil
}
var raw map[string]interface{}
if err = json.Unmarshal(b, &raw); err != nil {
return exists, err
}
var data interface{}
var ok bool
if data, ok = raw["data"]; !ok {
return exists, fmt.Errorf("Malformed response from Vault")
}
dataBytes, err := json.Marshal(&data)
if err != nil {
return exists, fmt.Errorf("could not remarshal vault data")
}
err = json.Unmarshal(dataBytes, &out)
return exists, err
}
func (vault *Vault) Put(path string, data interface{}) error {
res, err := vault.Do("POST", fmt.Sprintf("/v1/secret/%s", path), data)
if err != nil {
return err
}
defer func() {
ioutil.ReadAll(res.Body)
res.Body.Close()
}()
if res.StatusCode != 200 && res.StatusCode != 204 {
return fmt.Errorf("API %s", res.Status)
}
return nil
}
func (vault *Vault) Delete(path string) error {
res, err := vault.Do("DELETE", fmt.Sprintf("/v1/secret/%s", path), nil)
if err != nil {
return err
}
defer func() {
ioutil.ReadAll(res.Body)
res.Body.Close()
}()
if res.StatusCode != 200 && res.StatusCode != 204 && res.StatusCode != 404 {
return fmt.Errorf("API %s", res.Status)
}
return nil
}
func (vault *Vault) Clear(instanceID string) {
l := Logger.Wrap("vault clear %s", instanceID)
var rm func(string)
rm = func(path string) {
l.Debug("removing Vault secrets at/below %s", path)
if err := vault.Delete(path); err != nil {
l.Error("failed to delete %s: %s", path, err)
}
res, err := vault.Do("GET", fmt.Sprintf("%s?list=1", path), nil)
if err != nil {
l.Error("failed to list secrets at %s: %s", path, err)
return
}
defer func() {
ioutil.ReadAll(res.Body)
res.Body.Close()
}()
b, err := ioutil.ReadAll(res.Body)
if err != nil {
l.Error("failed to read response from the vault: %s", err)
return
}
var r struct{ Data struct{ Keys []string } }
if err = json.Unmarshal(b, &r); err != nil {
l.Error("failed to parse response from the vault: %s", err)
return
}
for _, sub := range r.Data.Keys {
rm(fmt.Sprintf("%s/%s", path, strings.TrimSuffix(sub, "/")))
}
l.Debug("cleared out vault secrets")
}
l.Info("removing secrets under /v1/secrets/%s", instanceID)
rm(fmt.Sprintf("/v1/secret/%s", instanceID))
l.Info("completed")
}
func (vault *Vault) Track(instanceID, action string, taskID int, params interface{}) error {
l := Logger.Wrap("vault track %s", instanceID)
l.Debug("tracking action '%s', task %d", action, taskID)
task := struct {
Action string `json:"action"`
Task int `json:"task"`
Params interface{} `json:"params"`
}{action, taskID, deinterface(params)}
return vault.Put(fmt.Sprintf("%s/task", instanceID), task)
}
func (vault *Vault) Index(instanceID string, data interface{}) error {
idx, err := vault.GetIndex("db")
if err != nil {
return err
}
if data != nil {
idx.Data[instanceID] = data
return idx.Save()
}
delete(idx.Data, instanceID)
err = idx.Save()
vault.Clear(instanceID)
return err
}
type Instance struct {
ID string
ServiceID string
PlanID string
}
func (vault *Vault) FindInstance(id string) (*Instance, bool, error) {
idx, err := vault.GetIndex("db")
if err != nil {
return nil, false, err
}
raw, err := idx.Lookup(id)
if err != nil {
return nil, false, nil /* not found */
}
inst, ok := raw.(map[string]interface{})
if !ok {
return nil, true, fmt.Errorf("indexed value [%s] is malformed (not a real map)", id)
}
instance := &Instance{}
if v, ok := inst["service_id"]; ok {
if s, ok := v.(string); ok {
instance.ServiceID = s
}
}
if v, ok := inst["plan_id"]; ok {
if s, ok := v.(string); ok {
instance.PlanID = s
}
}
return instance, true, nil
}
func (vault *Vault) State(instanceID string) (string, int, map[string]interface{}, error) {
type TaskState struct {
Action string `json:"action"`
Task int `json:"task"`
Params map[string]interface{} `json:"params"`
}
state := TaskState{}
exists, err := vault.Get(fmt.Sprintf("%s/task", instanceID), &state)
if err == nil && !exists {
err = fmt.Errorf("Instance %s not found in Vault", instanceID)
}
return state.Action, state.Task, state.Params, err
}
// getVaultDB returns the vault index (some useful vault constructs) that we're using to keep track of service/plan usage data
func (vault *Vault) getVaultDB() (*VaultIndex, error) {
l := Logger.Wrap("task.log")
l.Debug("retrieving vault 'db' index (for tracking service usage)")
db, err := vault.GetIndex("db")
if err != nil {
l.Error("failed to get 'db' index out of the vault: %s", err)
return nil, err
}
return db, nil
}
func (vault *Vault) updateHomeDirs() {
home := os.Getenv("BLACKSMITH_OPER_HOME")
if home == "" {
return
}
l := Logger.Wrap("update-home")
/* ~/.saferc */
path := fmt.Sprintf("%s/.saferc", home)
l.Debug("writing ~/.saferc file to %s", path)
var saferc struct {
Version int `json:"version"`
Current string `json:"current"`
Vaults struct {
Local struct {
URL string `json:"url"`
Token string `json:"token"`
NoStrongbox bool `json:"no-strongbox"`
} `json:"blacksmith"`
} `json:"vaults"`
}
saferc.Version = 1
saferc.Current = "blacksmith"
saferc.Vaults.Local.URL = vault.URL
saferc.Vaults.Local.Token = vault.Token
saferc.Vaults.Local.NoStrongbox = true
b, err := json.Marshal(saferc)
if err != nil {
l.Error("failed to marshal new ~/.saferc: %s", err)
} else {
err = ioutil.WriteFile(path, b, 0666)
if err != nil {
l.Error("failed to write new ~/.saferc: %s", err)
}
}
/* ~/.svtoken */
path = fmt.Sprintf("%s/.svtoken", home)
l.Debug("writing ~/.svtoken file to %s", path)
var svtoken struct {
Vault string `json:"vault"`
Token string `json:"token"`
}
svtoken.Vault = vault.URL
svtoken.Token = vault.Token
b, err = json.Marshal(svtoken)
if err != nil {
l.Error("failed to marshal new ~/.svtoken: %s", err)
} else {
err = ioutil.WriteFile(path, b, 0666)
if err != nil {
l.Error("failed to write new ~/.svtoken: %s", err)
}
}
/* ~/.vault-token */
path = fmt.Sprintf("%s/.vault-token", home)
l.Debug("writing ~/.vault-token file to %s", path)
err = ioutil.WriteFile(path, []byte(vault.Token), 0666)
if err != nil {
l.Error("failed to write new ~/.vault-token: %s", err)
}
}