forked from pinterest/knox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
314 lines (278 loc) · 8.32 KB
/
client.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
package knox
import (
"bytes"
"crypto/tls"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"time"
)
const refresh = 10 * time.Second
// Client is an interface for interacting with a specific knox key
type Client interface {
// GetPrimary returns the primary key version for the knox key.
// This should be used for sending relationships like signing, encrypting, or api secrets
GetPrimary() string
// GetActive returns all of the active key versions for the knox key.
// This should be used for receiving relationships like verifying or decrypting.
GetActive() []string
}
type fileClient struct {
keyID string
primary string
active []string
}
// update reads the file from a specific location, decodes json, and updates the key in memory.
func (c *fileClient) update() error {
var key Key
f, err := os.Open("/var/lib/knox/v0/keys/" + c.keyID)
if err != nil {
return fmt.Errorf("Knox key file err: %s", err.Error())
}
err = json.NewDecoder(f).Decode(&key)
if err != nil {
return fmt.Errorf("Knox json decode err: %s", err.Error())
}
c.setValues(&key)
return nil
}
func (c *fileClient) setValues(key *Key) {
c.primary = string(key.VersionList.GetPrimary().Data)
ks := key.VersionList.GetActive()
c.active = make([]string, len(ks))
for _, kv := range ks {
c.active = append(c.active, string(kv.Data))
}
}
func (c *fileClient) GetPrimary() string {
return c.primary
}
func (c *fileClient) GetActive() []string {
return c.active
}
// NewFileClient creates a file watcher knox client for the keyID given (it refreshes every ten seconds).
// This client calls `knox register` to cache the key locally on the file system.
func NewFileClient(keyID string) (Client, error) {
var key Key
c := &fileClient{keyID: keyID}
jsonKey, err := Register(keyID)
if err != nil {
return nil, err
}
err = json.Unmarshal(jsonKey, &key)
if err != nil {
return nil, fmt.Errorf("Knox json decode err: %s", err.Error())
}
c.setValues(&key)
go func() {
for range time.Tick(refresh) {
err := c.update()
if err != nil {
log.Println("Failed to update knox key ", err.Error())
}
}
}()
return c, nil
}
// NewMock is a knox Client to be used for testing.
func NewMock(primary string, active []string) Client {
return &fileClient{primary: primary, active: active}
}
// Register registers the given keyName with knox. If the operation fails, it returns an error.
func Register(keyID string) ([]byte, error) {
cmd := exec.Command("knox", "register", "-g", "-k", keyID)
output, err := cmd.CombinedOutput()
if err != nil {
return nil, fmt.Errorf("error getting knox key: %s %v '%q'", keyID, err, output)
}
return output, nil
}
// APIClient is an interface that talks to the knox server for key management.
type APIClient interface {
GetKey(keyID string) (*Key, error)
CreateKey(keyID string, data []byte, acl ACL) (uint64, error)
GetKeys(keys map[string]string) ([]string, error)
DeleteKey(keyID string) error
GetACL(keyID string) (*ACL, error)
PutAccess(keyID string, a *Access) error
AddVersion(keyID string, data []byte) (uint64, error)
UpdateVersion(keyID, versionID string, status VersionStatus) error
CacheGetKey(keyID string) (*Key, error)
NetworkGetKey(keyID string) (*Key, error)
}
type HTTP interface {
Do(req *http.Request) (*http.Response, error)
}
// HTTPClient is a client that uses HTTP to talk to Knox.
type HTTPClient struct {
// Host is used as the host for http connections
Host string
//AuthHandler returns the authorization string for authenticating to knox. Users should be prefixed by 0u, machines by 0m. On fail, return empty string.
AuthHandler func() string
// KeyFolder is the location of cached keys on the file system. If empty, does not check for cached keys.
KeyFolder string
// Client is the http client for making network calls
Client HTTP
}
// NewClient creates a new client to connect to talk to Knox.
func NewClient(host string, client HTTP, authHandler func() string, keyFolder string) APIClient {
return &HTTPClient{
Host: host,
Client: client,
AuthHandler: authHandler,
KeyFolder: keyFolder,
}
}
// CacheGetKey gets the key from file system cache.
func (c *HTTPClient) CacheGetKey(keyID string) (*Key, error) {
if c.KeyFolder == "" {
return nil, fmt.Errorf("No folder set for cached key.")
}
b, err := ioutil.ReadFile(c.KeyFolder + keyID)
if err != nil {
return nil, err
}
k := Key{}
err = json.Unmarshal(b, &k)
if err != nil {
return nil, err
}
return &k, nil
}
// NetworkGetKey gets a knox key by keyID and only uses network without the caches.
func (c *HTTPClient) NetworkGetKey(keyID string) (*Key, error) {
key := &Key{}
err := c.getHTTPData("GET", "/v0/keys/"+keyID+"/", nil, key)
return key, err
}
// GetKey gets a knox key by keyID.
func (c *HTTPClient) GetKey(keyID string) (*Key, error) {
key, err := c.CacheGetKey(keyID)
if err != nil {
return c.NetworkGetKey(keyID)
}
return key, err
}
// CreateKey creates a knox key with given keyID data and ACL.
func (c *HTTPClient) CreateKey(keyID string, data []byte, acl ACL) (uint64, error) {
var i uint64
d := url.Values{}
d.Set("id", keyID)
d.Set("data", base64.StdEncoding.EncodeToString(data))
s, err := json.Marshal(acl)
if err != nil {
return i, err
}
d.Set("acl", string(s))
err = c.getHTTPData("POST", "/v0/keys/", d, &i)
return i, err
}
// GetKeys gets all Knox (if empty map) or gets all keys in map that do not match key version hash.
func (c *HTTPClient) GetKeys(keys map[string]string) ([]string, error) {
var l []string
d := url.Values{}
for k, v := range keys {
d.Set(k, v)
}
err := c.getHTTPData("GET", "/v0/keys/?"+d.Encode(), nil, &l)
return l, err
}
// DeleteKey deletes a key from Knox.
func (c HTTPClient) DeleteKey(keyID string) error {
err := c.getHTTPData("DELETE", "/v0/keys/"+keyID+"/", nil, nil)
return err
}
// GetACL gets a knox key by keyID.
func (c *HTTPClient) GetACL(keyID string) (*ACL, error) {
acl := &ACL{}
err := c.getHTTPData("GET", "/v0/keys/"+keyID+"/access/", nil, acl)
return acl, err
}
// PutAccess will add an ACL rule to a specific key.
func (c *HTTPClient) PutAccess(keyID string, a *Access) error {
d := url.Values{}
s, err := json.Marshal(a)
if err != nil {
return err
}
d.Set("access", string(s))
err = c.getHTTPData("PUT", "/v0/keys/"+keyID+"/access/", d, nil)
return err
}
// AddVersion adds a key version to a specific key.
func (c *HTTPClient) AddVersion(keyID string, data []byte) (uint64, error) {
var i uint64
d := url.Values{}
d.Set("data", base64.StdEncoding.EncodeToString(data))
err := c.getHTTPData("POST", "/v0/keys/"+keyID+"/versions/", d, &i)
return i, err
}
// UpdateVersion either promotes or demotes a specific key version.
func (c *HTTPClient) UpdateVersion(keyID, versionID string, status VersionStatus) error {
d := url.Values{}
s, err := status.MarshalJSON()
if err != nil {
return err
}
d.Set("status", string(s))
err = c.getHTTPData("PUT", "/v0/keys/"+keyID+"/versions/"+versionID+"/", d, nil)
return err
}
func (c *HTTPClient) getClient() (HTTP, error) {
if c.Client == nil {
c.Client = &http.Client{}
}
return c.Client, nil
}
func (c *HTTPClient) getHTTPData(method string, path string, body url.Values, data interface{}) error {
var err error
r, err := http.NewRequest(method, "https://"+c.Host+path, bytes.NewBufferString(body.Encode()))
if err != nil {
return err
}
auth := c.AuthHandler()
if auth == "" {
return fmt.Errorf("No authentication data given. Use 'knox login' or set KNOX_USER_AUTH or KNOX_MACHINE_AUTH")
}
// Get user from env variable and machine hostname from elsewhere.
r.Header.Set("Authorization", auth)
if body != nil {
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
cli, err := c.getClient()
if err != nil {
return err
}
w, err := cli.Do(r)
if err != nil {
return err
}
resp := &Response{}
resp.Data = data
decoder := json.NewDecoder(w.Body)
err = decoder.Decode(resp)
if err != nil {
return err
}
if resp.Status != "ok" {
return fmt.Errorf(resp.Message)
}
return nil
}
// MockClient builds a client that ignores certs and talks to the given host.
func MockClient(host string) *HTTPClient {
return &HTTPClient{
Host: host,
AuthHandler: func() string {
return "TESTAUTH"
},
KeyFolder: "",
Client: &http.Client{Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}},
}
}