/
keystore.go
639 lines (579 loc) · 17.8 KB
/
keystore.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
// Copyright 2021 - MinIO, Inc. All rights reserved.
// Use of this source code is governed by the AGPLv3
// license that can be found in the LICENSE file.
package fortanix
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"sync"
"time"
"github.com/minio/kes"
xhttp "github.com/minio/kes/internal/http"
"github.com/minio/kes/internal/key"
)
// APIKey is a Fortanix API key for authenticating to
// a Fortanix SDKMS instance.
type APIKey string
// String returns a string representation of the API key
// that can be sent to a Fortanix SDKMS as part of the
// request headers.
func (k APIKey) String() string { return "Basic " + string(k) }
// KeyStore is a key store that persists and fetches
// cryptographic keys from a Fortanix SDKMS.
type KeyStore struct {
// Endpoint is the Fortanix SDKMS instance endpoint.
Endpoint string
// GroupID is ID of the Fortanix SDKMS group newly created
// keys will belong to.
//
// Fortanix SDKMS uses groups as collection of (security) objects.
// Typically, applications can access some/all objects within groups
// the application is assigned to.
GroupID string
// APIKey is the application's Fortanix SDKMS API key used to authenticate
// operations. It is sent on each request as part of the request headers.
APIKey APIKey
// ErrorLog specifies an optional logger for operational errors.
//
// If nil, logging is done via the log package's standard logger.
ErrorLog *log.Logger
// CAPath is an optional path to a CA certificate or directory
// containing CA certificates.
//
// If not empty, the KeyStore will use the specified CAs to
// verify the Fortanix SDKMS server certificate.
CAPath string
client xhttp.Retry
}
var _ key.Store = (*KeyStore)(nil) // compiler check
// Authenticate tries to establish a connection to
// the Fortanix SDKMS instance and authenticate
// using the KeyStore's credentials.
func (s *KeyStore) Authenticate(ctx context.Context) error {
if s.CAPath != "" {
rootCAs, err := loadCustomCAs(s.CAPath)
if err != nil {
return err
}
s.client.Client.Transport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: &tls.Config{
RootCAs: rootCAs,
},
}
}
// Check if the Fortanix SDKMS endpoint is reachable
var url = endpoint(s.Endpoint, "/sys/v1/health")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return err
}
req.Header.Set("Authorization", s.APIKey.String())
resp, err := s.client.Do(req)
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent {
if err := parseErrorResponse(resp); err != nil {
return err
}
return fmt.Errorf("fortanix: failed to connect to %q: %s (%d)", s.Endpoint, resp.Status, resp.StatusCode)
}
// Check if the authentication credentials are valid
url = endpoint(s.Endpoint, "/sys/v1/session/auth")
req, err = http.NewRequestWithContext(ctx, http.MethodPost, url, nil)
if err != nil {
return err
}
req.Header.Set("Authorization", s.APIKey.String())
resp, err = s.client.Do(req)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
if err := parseErrorResponse(resp); err != nil {
return err
}
return fmt.Errorf("fortanix: failed to authenticate to %q: %s (%d)", s.Endpoint, resp.Status, resp.StatusCode)
}
type Response struct {
Token string `json:"access_token"` // Raw bearer token - clients have to set 'Authorization: Bearer <token>'
}
var response Response
if err := json.NewDecoder(io.LimitReader(resp.Body, 1<<20)).Decode(&response); err != nil {
return fmt.Errorf("fortanix: failed to authenticate to %q: %v", s.Endpoint, err)
}
// Now we revoke the session we just created to cleanup any
// session credentials we just created. This is not strictly
// necessary but allows Fortanix SDKMS to garbage-collect
// unused credentials early.
url = endpoint(s.Endpoint, "/sys/v1/session/terminate")
req, err = http.NewRequestWithContext(ctx, http.MethodPost, url, nil)
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+response.Token)
resp, err = s.client.Do(req)
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent {
if err := parseErrorResponse(resp); err != nil {
return err
}
return fmt.Errorf("fortanix: failed to authenticate to %q: %s (%d)", s.Endpoint, resp.Status, resp.StatusCode)
}
return nil
}
// Create stors the given key at the Fortanix SDKMS if and only
// if no entry with the given name exists.
//
// If no such entry exists, Create returns kes.ErrKeyExists.
func (s *KeyStore) Create(ctx context.Context, name string, key key.Key) error {
type Request struct {
Type string `json:"obj_type"`
Name string `json:"name"`
GroupID string `json:"group_id,omitempty"`
Operations []string `json:"key_ops"`
Value string `json:"value"`
Enabled bool `json:"enabled"`
}
const (
Type = "OPAQUE"
OpExport = "EXPORT"
OpAppManageable = "APPMANAGEABLE"
)
request, err := json.Marshal(Request{
Type: Type,
Name: name,
GroupID: s.GroupID,
Operations: []string{OpExport, OpAppManageable},
Value: base64.StdEncoding.EncodeToString([]byte(key.String())), // Fortanix expects base64-encoded values and will not accept raw strings
Enabled: true,
})
if err != nil {
return err
}
var url = endpoint(s.Endpoint, "/crypto/v1/keys")
req, err := http.NewRequestWithContext(ctx, http.MethodPut, url, xhttp.RetryReader(bytes.NewReader(request)))
if err != nil {
s.logf("fortanix: failed to create key %q: %v", name, err)
return err
}
req.Header.Set("Authorization", s.APIKey.String())
req.Header.Set("Content-Type", "application/json")
resp, err := s.client.Do(req)
if err != nil {
if !errors.Is(err, context.Canceled) {
s.logf("fortanix: failed to create key %q: %v", name, err)
}
return err
}
if resp.StatusCode != http.StatusCreated {
switch err := parseErrorResponse(resp); {
case err == nil:
s.logf("fortanix: failed to create key %q: %s (%q)", name, resp.Status, resp.StatusCode)
return kes.NewError(resp.StatusCode, resp.Status)
case resp.StatusCode == http.StatusConflict && err.Error() == "sobject already exists":
return kes.ErrKeyExists
default:
s.logf("fortanix: failed to create key %q: %v", name, err)
return err
}
}
return nil
}
// Delete deletes the key associated with the given name
// from the Fortanix SDKMS. It may not return an error if no
// entry for the given name exists.
func (s *KeyStore) Delete(ctx context.Context, name string) error {
// In order to detele a key, we need to fetch its key ID first.
// Fortanix SDKMS API does not provide a way to delete a key
// using just its name.
type Request struct {
Name string `json:"name"`
}
request, err := json.Marshal(Request{
Name: name,
})
if err != nil {
s.logf("fortanix: failed to delete %q: %v", name, err)
return err
}
var url = endpoint(s.Endpoint, "/crypto/v1/keys/export")
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, xhttp.RetryReader(bytes.NewReader(request)))
if err != nil {
s.logf("fortanix: failed to delete %q: %v", name, err)
return err
}
req.Header.Set("Authorization", s.APIKey.String())
req.Header.Set("Content-Type", "application/json")
resp, err := s.client.Do(req)
if err != nil {
if !errors.Is(err, context.Canceled) {
s.logf("fortanix: failed to delete %q: %v", name, err)
}
return err
}
if resp.StatusCode != http.StatusOK {
switch err = parseErrorResponse(resp); {
case err == nil:
s.logf("fortanix: failed to delete %q: failed fetch key metadata: %s (%d)", name, resp.Status, resp.StatusCode)
return kes.NewError(resp.StatusCode, resp.Status)
case resp.StatusCode == http.StatusNotFound && err.Error() == "sobject does not exist":
return kes.ErrKeyNotFound
default:
s.logf("fortanix: failed to delete %q: failed to fetch key metadata: %v", name, err)
return err
}
}
type Response struct {
KeyID string `json:"kid"`
}
var response Response
if err := json.NewDecoder(io.LimitReader(resp.Body, key.MaxSize)).Decode(&response); err != nil {
s.logf("fortanix: failed to delete %q: failed to parse key metadata: %v", name, err)
return err
}
// Now, we can delete the key using its key ID.
url = endpoint(s.Endpoint, "/crypto/v1/keys", response.KeyID)
req, err = http.NewRequestWithContext(ctx, http.MethodDelete, url, nil)
if err != nil {
return err
}
req.Header.Set("Authorization", s.APIKey.String())
resp, err = s.client.Do(req)
if err != nil {
if !errors.Is(err, context.Canceled) {
s.logf("fortanix: failed to delete %q: %v", name, err)
}
return err
}
if resp.StatusCode != http.StatusNoContent {
switch err = parseErrorResponse(resp); {
case err == nil:
s.logf("fortanix: failed to delete %q: %s (%d)", name, resp.Status, resp.StatusCode)
return kes.NewError(resp.StatusCode, resp.Status)
default:
s.logf("fortanix: failed to delete %q: %v", name, err)
return err
}
}
return nil
}
// Get returns the key associated with the given name.
//
// If there is no such entry, Get returns kes.ErrKeyNotFound.
func (s *KeyStore) Get(ctx context.Context, name string) (key.Key, error) {
type Request struct {
Name string `json:"name"`
}
request, err := json.Marshal(Request{
Name: name,
})
if err != nil {
s.logf("fortanix: failed to fetch %q: %v", name, err)
return key.Key{}, err
}
var url = endpoint(s.Endpoint, "/crypto/v1/keys/export")
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, xhttp.RetryReader(bytes.NewReader(request)))
if err != nil {
s.logf("fortanix: failed to fetch %q: %v", name, err)
return key.Key{}, err
}
req.Header.Set("Authorization", s.APIKey.String())
req.Header.Set("Content-Type", "application/json")
resp, err := s.client.Do(req)
if err != nil {
if !errors.Is(err, context.Canceled) {
s.logf("fortanix: failed to fetch %q: %v", name, err)
}
return key.Key{}, err
}
if resp.StatusCode != http.StatusOK {
switch err = parseErrorResponse(resp); {
case err == nil:
s.logf("fortanix: failed to fetch %q: %s (%d)", name, resp.Status, resp.StatusCode)
return key.Key{}, kes.NewError(resp.StatusCode, resp.Status)
case resp.StatusCode == http.StatusNotFound && err.Error() == "sobject does not exist":
return key.Key{}, kes.ErrKeyNotFound
default:
s.logf("fortanix: failed to fetch %q: %v", name, err)
return key.Key{}, err
}
}
type Response struct {
Value string `json:"value"`
Enabled bool `json:"enabled"`
}
var response Response
if err := json.NewDecoder(io.LimitReader(resp.Body, key.MaxSize)).Decode(&response); err != nil {
s.logf("fortanix: failed to fetch %q: failed to parse server response %v", name, err)
return key.Key{}, err
}
if !response.Enabled {
s.logf("fortanix: failed to fetch %q: key has been disabled and cannot be used until enabled again", name)
return key.Key{}, errors.New("fortanix: key has been disabled")
}
value, err := base64.StdEncoding.DecodeString(response.Value)
if err != nil {
s.logf("fortanix: failed to fetch %q: %v", name, err)
return key.Key{}, err
}
k, err := key.Parse(string(value))
if err != nil {
s.logf("fortanix: failed to fetch %q: failed to parse key: %v", name, err)
return key.Key{}, err
}
return k, nil
}
// List returns a new Iterator over the Fortanix SDKMS keys.
//
// The returned iterator may or may not reflect any
// concurrent changes to the Fortanix SDKMS instance - i.e.
// creates or deletes. Further, it does not provide any
// ordering guarantees.
func (s *KeyStore) List(ctx context.Context) (key.Iterator, error) {
var (
names = make(chan string, 10)
iter = &iterator{
values: names,
}
)
go func() {
defer close(names)
var start string
for {
var reqURL = endpoint(s.Endpoint, "/crypto/v1/keys") + "?sort=name:asc&limit=100"
if start != "" {
reqURL += "&start=" + start
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqURL, nil)
if err != nil {
s.logf("fortanix: failed to list keys: %v", err)
iter.SetErr(err)
return
}
req.Header.Set("Authorization", s.APIKey.String())
resp, err := s.client.Do(req)
if err != nil {
if !errors.Is(err, context.Canceled) {
s.logf("fortanix: failed to list keys: %v", err)
}
iter.SetErr(err)
return
}
type Response struct {
Name string `json:"name"`
}
var keys []Response
if err := json.NewDecoder(io.LimitReader(resp.Body, 10*key.MaxSize)).Decode(&keys); err != nil {
s.logf("fortanix: failed to list keys: failed to parse server response: %v", err)
iter.SetErr(err)
return
}
if len(keys) == 0 {
return
}
for _, k := range keys {
select {
case names <- k.Name:
case <-ctx.Done():
iter.SetErr(context.Canceled)
return
}
}
start = url.QueryEscape(keys[len(keys)-1].Name)
}
}()
return iter, nil
}
func (s *KeyStore) logf(format string, v ...interface{}) {
if s.ErrorLog == nil {
s.ErrorLog.Printf(format, v...)
} else {
log.Printf(format, v...)
}
}
type iterator struct {
values <-chan string
lock sync.RWMutex
last string
err error
}
var _ key.Iterator = (*iterator)(nil)
// Next moves the iterator to the next key, if any.
// This key is available until Next is called again.
//
// It returns true if and only if there is a new key
// available. If there are no more keys or an error
// has been encountered, Next returns false.
func (i *iterator) Next() bool {
v, ok := <-i.values
if !ok {
return false
}
i.lock.Lock()
defer i.lock.Unlock()
i.last = v
return i.err == nil
}
// Name returns the name of the current key. Name
// can be called multiple times an returns the
// same value until Next is called again.
func (i *iterator) Name() string {
i.lock.RLock()
defer i.lock.RUnlock()
return i.last
}
// Err returns the first error, if any, encountered
// while iterating over the set of keys.
func (i *iterator) Err() error {
i.lock.RLock()
defer i.lock.RUnlock()
return i.err
}
// SetErr sets the iteration error to indicate
// that the iteration failed. Subsequent calls
// to Next will return false.
func (i *iterator) SetErr(err error) {
i.lock.Lock()
defer i.lock.Unlock()
i.err = err
}
// parseErrorResponse returns an error containing
// the response status code and response body
// as error message if the response is an error
// response - i.e. status code >= 400.
//
// If the response status code is < 400, e.g. 200 OK,
// parseErrorResponse returns nil and does not attempt
// to read or close the response body.
//
// If resp is an error response, parseErrorResponse reads
// and closes the response body.
func parseErrorResponse(resp *http.Response) error {
if resp.StatusCode < 400 {
return nil
}
if resp.Body == nil {
return kes.NewError(resp.StatusCode, resp.Status)
}
defer resp.Body.Close()
const MaxSize = 1 << 20
var size = resp.ContentLength
if size < 0 || size > MaxSize {
size = MaxSize
}
if contentType := strings.TrimSpace(resp.Header.Get("Content-Type")); strings.HasPrefix(contentType, "application/json") {
type Response struct {
Message string `json:"message"`
}
var response Response
if err := json.NewDecoder(io.LimitReader(resp.Body, size)).Decode(&response); err != nil {
return err
}
return kes.NewError(resp.StatusCode, response.Message)
}
var sb strings.Builder
if _, err := io.Copy(&sb, io.LimitReader(resp.Body, size)); err != nil {
return err
}
return kes.NewError(resp.StatusCode, sb.String())
}
// loadCustomCAs returns a new RootCA certificate pool
// that contains one or multiple certificates found at
// the given path.
//
// If path is a file then loadCustomCAs tries to parse
// the file as a PEM-encoded certificate.
//
// If path is a directory then loadCustomCAs tries to
// parse any file inside path as PEM-encoded certificate.
// It returns a non-nil error if one file is not a valid
// PEM-encoded X.509 certificate.
func loadCustomCAs(path string) (*x509.CertPool, error) {
var rootCAs = x509.NewCertPool()
f, err := os.Open(path)
if err != nil {
return rootCAs, err
}
defer f.Close()
stat, err := f.Stat()
if err != nil {
return rootCAs, err
}
if !stat.IsDir() {
bytes, err := ioutil.ReadAll(f)
if err != nil {
return rootCAs, err
}
if !rootCAs.AppendCertsFromPEM(bytes) {
return rootCAs, fmt.Errorf("%q does not contain a valid X.509 PEM-encoded certificate", path)
}
return rootCAs, nil
}
files, err := f.Readdir(0)
if err != nil {
return rootCAs, err
}
for _, file := range files {
if file.IsDir() {
continue
}
name := filepath.Join(path, file.Name())
bytes, err := ioutil.ReadFile(name)
if err != nil {
return rootCAs, err
}
if !rootCAs.AppendCertsFromPEM(bytes) {
return rootCAs, fmt.Errorf("%q does not contain a valid X.509 PEM-encoded certificate", name)
}
}
return rootCAs, nil
}
// endpoint returns an endpoint URL starting with the
// given endpoint followed by the path elements.
//
// For example:
// • endpoint("https://127.0.0.1:7373", "version") => "https://127.0.0.1:7373/version"
// • endpoint("https://127.0.0.1:7373/", "/key/create", "my-key") => "https://127.0.0.1:7373/key/create/my-key"
//
// Any leading or trailing whitespaces are removed from
// the endpoint before it is concatenated with the path
// elements.
//
// The path elements will not be URL-escaped.
func endpoint(endpoint string, elems ...string) string {
endpoint = strings.TrimSpace(endpoint)
endpoint = strings.TrimSuffix(endpoint, "/")
if len(elems) > 0 && !strings.HasPrefix(elems[0], "/") {
endpoint += "/"
}
return endpoint + path.Join(elems...)
}