-
Notifications
You must be signed in to change notification settings - Fork 2
/
kv.go
167 lines (133 loc) · 3.56 KB
/
kv.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
// Package vaultkv provides version agnostic methods for read, write and list of secrets from @hashicorp Vault's KV secret engines
package vaultkv
import (
"fmt"
"strconv"
"strings"
vault "github.com/hashicorp/vault/api"
)
// Constants
const (
ReadPrefix = "data"
WritePrefix = ReadPrefix
ListPrefix = "metadata"
)
// Client represents a KV client
type Client struct {
client *vault.Client
Version int
Mount string
}
// New creates a new kv.Client with the Vault client c and a path p long enough to determine the mount path of the engine
// p = secret/ -> K/V engine mount path secret/
// p = secret -> error
// p = /secret -> error
func New(c *vault.Client, p string) (*Client, error) {
if strings.HasPrefix(p, "/") {
return nil, fmt.Errorf("path %s must not start with '/'", p)
}
if !strings.ContainsRune(p, '/') {
return nil, fmt.Errorf("path %s must contain at least one '/'", p)
}
version, mount, err := getVersionAndMount(c, p)
if err != nil {
return nil, err
}
return &Client{client: c, Version: version, Mount: mount}, nil
}
// Client returns a Vault *vault.Client
func (c *Client) Client() *vault.Client {
return c.client
}
// Read a secret from a K/V version 1 or 2
func (c *Client) Read(p string) (map[string]interface{}, error) {
if c.Version == 2 {
p = FixPath(p, c.Mount, ReadPrefix)
}
s, err := c.client.Logical().Read(p)
if err != nil {
return nil, err
}
if s == nil || s.Data == nil {
return nil, nil
}
if c.Version == 2 {
if s.Data["data"] == nil {
return nil, nil
}
return s.Data["data"].(map[string]interface{}), nil
}
return s.Data, nil
}
// Write a secret to a K/V version 1 or 2
func (c *Client) Write(p string, data map[string]interface{}) error {
if c.Version == 2 {
p = FixPath(p, c.Mount, WritePrefix)
data = map[string]interface{}{
"data": data,
}
}
_, err := c.client.Logical().Write(p, data)
return err
}
// List secrets from a K/V version 1 or 2
func (c *Client) List(p string) ([]string, error) {
if c.Version == 2 {
p = FixPath(p, c.Mount, ListPrefix)
}
s, err := c.client.Logical().List(p)
if err != nil {
return nil, err
}
if s == nil || s.Data == nil {
return nil, nil
}
keys := []string{}
for _, v := range s.Data["keys"].([]interface{}) {
keys = append(keys, v.(string))
}
return keys, nil
}
// SetToken sets the token directly. This won't perform any auth
// verification, it simply sets the token properly for future requests.
func (c *Client) SetToken(v string) {
c.client.SetToken(v)
}
// FixPath inserts the API prefix for v1 style path
// secret/foo -> secret/data/foo
// secret/data/foo -> secret/data/foo
// presumes a valid path
func FixPath(path, mount, prefix string) string {
if !strings.HasSuffix(mount, "/") {
mount += "/"
}
secretPath := strings.TrimPrefix(path, mount)
pp := strings.Split(secretPath, "/")
if pp[0] == prefix {
return path // already v2 style path
}
return fmt.Sprintf("%s%s/%s", mount, prefix, secretPath)
}
// getVersionAndMount of the KV engine
func getVersionAndMount(c *vault.Client, p string) (version int, mount string, err error) {
mounts, err := c.Sys().ListMounts()
if err != nil {
return 0, "", err
}
for k, m := range mounts {
if !strings.HasPrefix(p, k) {
continue
}
switch m.Type {
case "kv", "generic":
version, err := strconv.Atoi(m.Options["version"])
if err != nil {
return 0, "", err
}
return version, k, nil
default:
return 0, "", fmt.Errorf("matching mount %s for path %s is not of type kv", k, p)
}
}
return 0, "", fmt.Errorf("failed to get mount for path: %s", p)
}