forked from pinterest/knox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
439 lines (389 loc) · 12.1 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package knox
import (
"bytes"
"crypto/tls"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/url"
"os"
"os/exec"
"sync"
"time"
)
const refresh = 10 * time.Second
// For linear random backoff on write requests.
const baseBackoff = 50 * time.Millisecond
const maxBackoff = 3 * time.Second
const maxRetryAttempts = 3
// 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
// GetKeyObject returns the full key object, including versions, ACLs, and other attributes.
GetKeyObject() Key
}
type fileClient struct {
sync.RWMutex
keyID string
primary string
active []string
keyObject Key
}
// 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())
}
defer f.Close()
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.Lock()
defer c.Unlock()
c.keyObject = *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 {
c.RLock()
defer c.RUnlock()
return c.primary
}
func (c *fileClient) GetActive() []string {
c.RLock()
defer c.RUnlock()
return c.active
}
func (c *fileClient) GetKeyObject() Key {
c.RLock()
defer c.RUnlock()
return c.keyObject
}
// 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
}
// NewMockKeyVersion creates a Knox KeyVersion to be used for testing
func NewMockKeyVersion(keydata []byte, status VersionStatus) KeyVersion {
return KeyVersion{Data: keydata, Status: status}
}
// NewMock is a knox Client to be used for testing.
func NewMock(primary string, active []string) Client {
var kvl []KeyVersion
kvl = append(kvl, NewMockKeyVersion([]byte(primary), Primary))
for _, data := range active {
kvl = append(kvl, NewMockKeyVersion([]byte(data), Active))
}
return &fileClient{primary: primary, active: active, keyObject: Key{VersionList: KeyVersionList(kvl)}}
}
// 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
}
// GetBackoffDuration returns a time duration to sleep based on the attempt #.
func GetBackoffDuration(attempt int) time.Duration {
basef := float64(baseBackoff)
// Add some randomness.
duration := rand.Float64()*float64(attempt) + basef
if duration > float64(maxBackoff) {
return maxBackoff
}
return time.Duration(duration)
}
// APIClient is an interface that talks to the knox server for key management.
type APIClient interface {
GetKey(keyID string) (*KeyAccess, 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, acl ...Access) error
AddVersion(keyID string, data []byte) (uint64, error)
UpdateVersion(keyID, versionID string, status VersionStatus) error
CacheGetKey(keyID string) (*KeyAccess, error)
NetworkGetKey(keyID string) (*KeyAccess, error)
GetKeyWithStatus(keyID string, status VersionStatus) (*KeyAccess, error)
CacheGetKeyWithStatus(keyID string, status VersionStatus) (*KeyAccess, error)
NetworkGetKeyWithStatus(keyID string, status VersionStatus) (*KeyAccess, 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
// Version is the current client version, useful for debugging and sent as a header
Version string
}
// NewClient creates a new client to connect to talk to Knox.
func NewClient(host string, client HTTP, authHandler func() string, keyFolder, version string) APIClient {
return &HTTPClient{
Host: host,
Client: client,
AuthHandler: authHandler,
KeyFolder: keyFolder,
Version: version,
}
}
// CacheGetKey gets the key from file system cache.
func (c *HTTPClient) CacheGetKey(keyID string) (*KeyAccess, error) {
if c.KeyFolder == "" {
return nil, fmt.Errorf("No folder set for cached key.")
}
path := c.KeyFolder + keyID
b, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
k := KeyAccess{Key: &Key{Path: path}}
err = json.Unmarshal(b, &k)
if err != nil {
return nil, err
}
if k.Key.ID != keyID {
// if Key ID does not match requested ID incase of JSON format update
return nil, errors.New("failed to properly unmarshal cached key")
}
return &k, nil
}
// NetworkGetKey gets a knox key by keyID and only uses network without the caches.
func (c *HTTPClient) NetworkGetKey(keyID string) (*KeyAccess, error) {
key := &KeyAccess{}
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) (*KeyAccess, error) {
key, err := c.CacheGetKey(keyID)
if err != nil {
return c.NetworkGetKey(keyID)
}
return key, err
}
// CacheGetKeyWithStatus gets the key with status from file system cache.
func (c *HTTPClient) CacheGetKeyWithStatus(keyID string, status VersionStatus) (*KeyAccess, error) {
if c.KeyFolder == "" {
return nil, fmt.Errorf("No folder set for cached key.")
}
st, err := status.MarshalJSON()
if err != nil {
return nil, err
}
path := c.KeyFolder + keyID + "?status=" + string(st)
b, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
k := KeyAccess{Key: &Key{Path: path}}
err = json.Unmarshal(b, &k)
if err != nil {
return nil, err
}
if k.Key.ID != keyID {
// if Key ID does not match requested ID incase of JSON format update
return nil, errors.New("failed to properly unmarshal cached key")
}
return &k, nil
}
// NetworkGetKeyWithStatus gets a knox key by keyID and given version status (always calls network).
func (c *HTTPClient) NetworkGetKeyWithStatus(keyID string, status VersionStatus) (*KeyAccess, error) {
// If clients need to know
s, err := status.MarshalJSON()
if err != nil {
return nil, err
}
key := &KeyAccess{}
err = c.getHTTPData("GET", "/v0/keys/"+keyID+"/?status="+string(s), nil, key)
return key, err
}
// GetKeyWithStatus gets a knox key by keyID and status (leverages cache).
func (c *HTTPClient) GetKeyWithStatus(keyID string, status VersionStatus) (*KeyAccess, error) {
key, err := c.CacheGetKeyWithStatus(keyID, status)
if err != nil {
return c.NetworkGetKeyWithStatus(keyID, status)
}
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("acl", 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 {
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)
r.Header.Set("User-Agent", fmt.Sprintf("Knox_Client/%s", c.Version))
if body != nil {
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
cli, err := c.getClient()
if err != nil {
return err
}
resp := &Response{}
resp.Data = data
// Contains retry logic if we decode a 500 error.
for i := 1; i <= maxRetryAttempts; i++ {
err = getHTTPResp(cli, r, resp)
if err != nil {
return err
}
if resp.Status != "ok" {
if (resp.Code != InternalServerErrorCode) || (i == maxRetryAttempts) {
return fmt.Errorf(resp.Message)
}
time.Sleep(GetBackoffDuration(i))
} else {
break
}
}
return nil
}
func getHTTPResp(cli HTTP, r *http.Request, resp *Response) error {
w, err := cli.Do(r)
if err != nil {
return err
}
defer w.Body.Close()
decoder := json.NewDecoder(w.Body)
return decoder.Decode(resp)
}
// 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}}},
Version: "mock",
}
}