forked from Qarik-Group/safe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vault.go
276 lines (245 loc) · 5.44 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
package vault
import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/jhunt/ansi"
"github.com/jhunt/tree"
)
// A Vault represents a means for interacting with a remote Vault
// instance (unsealed and pre-authenticated) to read and write secrets.
type Vault struct {
URL string
Token string
Client *http.Client
}
// NewVault creates a new Vault object. If an empty token is specified,
// the current user's token is read from ~/.vault-token.
func NewVault(url, token string) (*Vault, error) {
if token == "" {
b, err := ioutil.ReadFile(fmt.Sprintf("%s/.vault-token", os.Getenv("HOME")))
if err != nil {
return nil, err
}
token = string(b)
}
if token == "" {
return nil, fmt.Errorf("no vault token specified; are you authenticated?")
}
return &Vault{
URL: url,
Token: token,
Client: &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: os.Getenv("VAULT_SKIP_VERIFY") != "",
},
},
CheckRedirect: func(req *http.Request, via []*http.Request) error {
if len(via) > 10 {
return fmt.Errorf("stopped after 10 redirects")
}
req.Header.Add("X-Vault-Token", token)
return nil
},
},
}, nil
}
func (v *Vault) url(f string, args ...interface{}) string {
return v.URL + fmt.Sprintf(f, args...)
}
func (v *Vault) request(req *http.Request) (*http.Response, error) {
req.Header.Add("X-Vault-Token", v.Token)
return v.Client.Do(req)
}
// Read checks the Vault for a Secret at the specified path, and returns it.
// If there is nothing at that path, a nil *Secret will be returned, with no
// error.
func (v *Vault) Read(path string) (secret *Secret, err error) {
secret = NewSecret()
req, err := http.NewRequest("GET", v.url("/v1/%s", path), nil)
if err != nil {
return
}
res, err := v.request(req)
if err != nil {
return
}
switch res.StatusCode {
case 200:
break
case 404:
err = NotFound
return
default:
err = fmt.Errorf("API %s", res.Status)
return
}
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return
}
var raw map[string]interface{}
if err = json.Unmarshal(b, &raw); err != nil {
return
}
if rawdata, ok := raw["data"]; ok {
if data, ok := rawdata.(map[string]interface{}); ok {
for k, v := range data {
if s, ok := v.(string); ok {
secret.data[k] = s
} else {
b, err = json.Marshal(v)
if err != nil {
return
}
secret.data[k] = string(b)
}
}
return
}
}
err = fmt.Errorf("malformed response from vault")
return
}
// List returns the set of (relative) paths that are directly underneath
// the given path. Intermediate path nodes are suffixed with a single "/",
// whereas leaf nodes (the secrets themselves) are not.
func (v *Vault) List(path string) (paths []string, err error) {
req, err := http.NewRequest("GET", v.url("/v1/%s?list=1", path), nil)
if err != nil {
return
}
res, err := v.request(req)
if err != nil {
return
}
switch res.StatusCode {
case 200:
break
case 404:
err = NotFound
return
default:
err = fmt.Errorf("API %s", res.Status)
return
}
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return
}
var r struct{ Data struct{ Keys []string } }
if err = json.Unmarshal(b, &r); err != nil {
return
}
return r.Data.Keys, nil
}
type Node struct {
Path string
Children []Node
}
// Tree returns a tree that represents the hierarhcy of paths contained
// below the given path, inside of the Vault.
func (v *Vault) Tree(path string, ansify bool) (tree.Node, error) {
name := path
if ansify {
name = ansi.Sprintf("@C{%s}", path)
}
t := tree.New(name)
l, err := v.List(path)
if err != nil {
return t, err
}
var kid tree.Node
for _, p := range l {
if p[len(p)-1:len(p)] == "/" {
kid, err = v.Tree(path+"/"+p[0:len(p)-1], ansify)
if ansify {
name = ansi.Sprintf("@B{%s}", p)
} else {
name = p[0 : len(p)-1]
}
} else {
kid, err = v.Tree(path+"/"+p, ansify)
if ansify {
name = ansi.Sprintf("@G{%s}", p)
} else {
name = p
}
}
if err != nil {
return t, err
}
kid.Name = name
t.Append(kid)
}
return t, nil
}
// Write takes a Secret and writes it to the Vault at the specified path.
func (v *Vault) Write(path string, s *Secret) error {
raw := s.JSON()
if raw == "" {
return fmt.Errorf("nothing to write")
}
req, err := http.NewRequest("POST", v.url("/v1/%s", path), strings.NewReader(raw))
if err != nil {
return err
}
res, err := v.request(req)
if err != nil {
return err
}
switch res.StatusCode {
case 200:
break
case 204:
break
default:
return fmt.Errorf("API %s", res.Status)
}
return nil
}
// Delete removes the secret stored at the specified path.
func (v *Vault) Delete(path string) error {
req, err := http.NewRequest("DELETE", v.url("/v1/%s", path), nil)
if err != nil {
return err
}
res, err := v.request(req)
if err != nil {
return err
}
switch res.StatusCode {
case 200:
break
case 204:
break
default:
return fmt.Errorf("API %s", res.Status)
}
return nil
}
// Copy copies secrets from one path to another.
func (v *Vault) Copy(oldpath, newpath string) error {
secret, err := v.Read(oldpath)
if err != nil {
return err
}
return v.Write(newpath, secret)
}
// Move moves secrets from one path to another.
func (v *Vault) Move(oldpath, newpath string) error {
err := v.Copy(oldpath, newpath)
if err != nil {
return err
}
err = v.Delete(oldpath)
if err != nil {
return err
}
return nil
}