forked from Qarik-Group/safe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vault.go
174 lines (152 loc) · 3.4 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
package vault
import (
"strings"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
)
// 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
}
// 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,
}, 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 http.DefaultClient.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
}
// 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
}