-
Notifications
You must be signed in to change notification settings - Fork 0
/
vault.go
69 lines (53 loc) · 1.35 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
package main
import (
"context"
"encoding/json"
"github.com/hashicorp/vault-client-go"
"path/filepath"
"time"
)
type VaultConnection struct {
Config VaultConfig
client *vault.Client
}
func (vc *VaultConnection) Connect() error {
vaultOptions := []vault.ClientOption{
vault.WithAddress(vc.Config.Address),
}
if vc.Config.IgnoreTls {
vaultOptions = append(vaultOptions, vault.WithTLS(
vault.TLSConfiguration{
InsecureSkipVerify: true,
},
))
}
vlt, err := vault.New(vaultOptions...)
if err != nil {
return err
}
if err := vlt.SetToken(vc.Config.Token); err != nil {
return err
}
vc.client = vlt
return nil
}
func (vc *VaultConnection) ListSecrets(path string) ([]string, error) {
ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second)
defer cancel()
secrets, err := vc.client.Secrets.KvV2List(ctx, path, vault.WithMountPath(vc.Config.StoreName))
if err != nil {
return nil, err
}
return secrets.Data.Keys, nil
}
func (vc *VaultConnection) GetSecret(path, item string) ([]byte, error) {
ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second)
defer cancel()
fullpath := filepath.Join(path, item)
secret, err := vc.client.Secrets.KvV2Read(ctx, fullpath, vault.WithMountPath(vc.Config.StoreName))
if err != nil {
return nil, err
}
js, err := json.Marshal(secret.Data.Data)
return js, err
}