forked from fastly/go-fastly
/
secret_store.go
491 lines (424 loc) · 12.4 KB
/
secret_store.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
package fastly
import (
"bytes"
"crypto/ed25519"
"crypto/rand"
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
"golang.org/x/crypto/nacl/box"
)
// Secret Store.
// A secret store is a persistent, globally distributed store for secrets accessible to Compute@Edge services during request processing.
// https://developer.fastly.com/reference/api/secret-store/
// SecretStoreMeta is the metadata returned from Secret Store paginated responses.
type SecretStoreMeta struct {
// Limit is the limit of results returned.
Limit int `json:"limit"`
// NextCursor can be used on a subsequent request to fetch the next page of data.
NextCursor string `json:"next_cursor,omitempty"`
}
// SecretStore represents a Secret Store response from the Fastly API.
type SecretStore struct {
ID string `json:"id"`
Name string `json:"name"`
CreatedAt time.Time `json:"created_at"`
}
// CreateSecretStoreInput is used as input to the CreateSecretStore function.
type CreateSecretStoreInput struct {
// Name of the Secret Store (required).
Name string `json:"name"`
}
// CreateSecretStore creates a new resource.
func (c *Client) CreateSecretStore(i *CreateSecretStoreInput) (*SecretStore, error) {
if i.Name == "" {
return nil, ErrMissingName
}
p := "/resources/stores/secret"
resp, err := c.PostJSON(p, i, &RequestOptions{
Parallel: true,
})
if err != nil {
return nil, err
}
defer resp.Body.Close()
var output SecretStore
if err := json.NewDecoder(resp.Body).Decode(&output); err != nil {
return nil, err
}
return &output, nil
}
// SecretStores represents a list of Secret Stores from the Fastly API.
type SecretStores struct {
// Data is a list of Secret Stores.
Data []SecretStore `json:"data"`
// Meta contains response pagination data.
Meta SecretStoreMeta `json:"meta"`
}
// ListSecretStoresInput is used as input to the ListSecretStores function.
type ListSecretStoresInput struct {
// Cursor is the pagination cursor (optional).
Cursor string
// Limit is the desired number of Secret Stores (optional).
Limit int
// Name is the name of the secret store (optional).
Name string
}
// ListSecretStores retrieves all resources.
//
// The returned next cursor, if non-blank, can be used as input to a subsequent
// request for the next page of results.
func (c *Client) ListSecretStores(i *ListSecretStoresInput) (*SecretStores, error) {
p := "/resources/stores/secret"
params := make(map[string]string, 2)
if i.Limit > 0 {
params["limit"] = strconv.Itoa(i.Limit)
}
if i.Cursor != "" {
params["cursor"] = i.Cursor
}
if i.Name != "" {
params["name"] = i.Name
}
resp, err := c.Get(p, &RequestOptions{
Params: params,
Headers: map[string]string{
"Content-Type": "application/json",
"Accept": "application/json",
},
Parallel: true,
})
if err != nil {
return nil, err
}
defer resp.Body.Close()
var output SecretStores
if err := json.NewDecoder(resp.Body).Decode(&output); err != nil {
return nil, err
}
return &output, nil
}
// GetSecretStoreInput is used as input to the GetSecretStore function.
type GetSecretStoreInput struct {
// ID of the Secret Store (required).
ID string
}
// GetSecretStore retrieves the specified resource.
func (c *Client) GetSecretStore(i *GetSecretStoreInput) (*SecretStore, error) {
if i.ID == "" {
return nil, ErrMissingID
}
p := "/resources/stores/secret/" + i.ID
resp, err := c.Get(p, &RequestOptions{
Headers: map[string]string{
"Content-Type": "application/json",
"Accept": "application/json",
},
Parallel: true,
})
if err != nil {
return nil, err
}
defer resp.Body.Close()
var output SecretStore
if err := json.NewDecoder(resp.Body).Decode(&output); err != nil {
return nil, err
}
return &output, nil
}
// DeleteSecretStoreInput is used as input to the DeleteSecretStore function.
type DeleteSecretStoreInput struct {
// ID of the Secret Store (required).
ID string
}
// DeleteSecretStore deletes the specified resource.
func (c *Client) DeleteSecretStore(i *DeleteSecretStoreInput) error {
if i.ID == "" {
return ErrMissingID
}
p := "/resources/stores/secret/" + i.ID
resp, err := c.Delete(p, &RequestOptions{
Headers: map[string]string{
"Content-Type": "application/json",
"Accept": "application/json",
},
Parallel: true,
})
if err != nil {
return err
}
return resp.Body.Close()
}
// Secret is a Secret Store secret.
type Secret struct {
Name string `json:"name"`
Digest []byte `json:"digest"` // Digest is an opaque hash of the secret.
CreatedAt time.Time `json:"created_at"`
Recreated bool `json:"recreated,omitempty"`
}
// CreateSecretInput is used as input to the CreateSecret function.
type CreateSecretInput struct {
// ID of the Secret Store (required).
ID string
// Name of the Secret (required).
Name string
// Secret is the plaintext secret to be stored (required).
// The value will be base64-encoded when delivered to the API, which is the
// required format.
Secret []byte
// ClientKey is the public key used to encrypt the secret with (optional).
ClientKey []byte
// Method is the HTTP request method used to create the secret.
//
// Secret names must be unique within a store.
// The method effects how duplicate names are handled:
//
// - POST: Default. Create a secret and error if one already exists with the same name.
// - PUT: Create or recreate a secret.
// - PATCH: Recreate a secret and error if one does not already exist with the same name.
//
// More details: https://developer.fastly.com/reference/api/services/resources/secret-store-secret/
Method string
}
// CreateSecret creates a new resource.
func (c *Client) CreateSecret(i *CreateSecretInput) (*Secret, error) {
if i.ID == "" {
return nil, ErrMissingID
}
if i.Name == "" {
return nil, ErrMissingName
}
if len(i.Secret) == 0 {
return nil, ErrMissingSecret
}
p := "/resources/stores/secret/" + i.ID + "/secrets"
var body bytes.Buffer
err := json.NewEncoder(&body).Encode(struct {
Name string `json:"name"`
Secret []byte `json:"secret"`
ClientKey []byte `json:"client_key,omitempty"`
}{
Name: i.Name,
Secret: i.Secret,
ClientKey: i.ClientKey,
})
if err != nil {
return nil, err
}
method := i.Method
if method == "" {
method = http.MethodPost
}
switch method {
case http.MethodPost, http.MethodPut, http.MethodPatch:
// Method is allowed.
default:
return nil, ErrInvalidMethod
}
resp, err := c.Request(method, p, &RequestOptions{
Headers: map[string]string{
"Content-Type": "application/json",
"Accept": "application/json",
},
Body: &body,
Parallel: true,
})
if err != nil {
return nil, err
}
defer resp.Body.Close()
var output Secret
if err := json.NewDecoder(resp.Body).Decode(&output); err != nil {
return nil, err
}
return &output, nil
}
// Secrets represents a list of Secrets from the Fastly API.
type Secrets struct {
// Data is a list of Secrets.
Data []Secret `json:"data"`
// Meta contains pagination data.
Meta SecretStoreMeta `json:"meta"`
}
// ListSecretsInput is used as input to the ListSecrets function.
type ListSecretsInput struct {
// Cursor is the pagination cursor (optional).
Cursor string
// ID of the Secret Store (required).
ID string
// Limit is the desired number of Secrets (optional).
Limit int
}
// ListSecrets retrieves all resources.
//
// The returned next cursor, if non-blank, can be used as input to a subsequent
// request for the next page of results.
func (c *Client) ListSecrets(i *ListSecretsInput) (*Secrets, error) {
if i.ID == "" {
return nil, ErrMissingID
}
p := "/resources/stores/secret/" + i.ID + "/secrets"
params := make(map[string]string, 2)
if i.Limit > 0 {
params["limit"] = strconv.Itoa(i.Limit)
}
if i.Cursor != "" {
params["cursor"] = i.Cursor
}
resp, err := c.Get(p, &RequestOptions{
Params: params,
Headers: map[string]string{
"Content-Type": "application/json",
"Accept": "application/json",
},
Parallel: true,
})
if err != nil {
return nil, err
}
defer resp.Body.Close()
var output Secrets
if err := json.NewDecoder(resp.Body).Decode(&output); err != nil {
return nil, err
}
return &output, nil
}
// GetSecretInput is used as input to the GetSecret function.
type GetSecretInput struct {
// ID of the Secret Store (required).
ID string
// Name of the Secret (required).
Name string
}
// GetSecret retrieves the specified resource.
func (c *Client) GetSecret(i *GetSecretInput) (*Secret, error) {
if i.ID == "" {
return nil, ErrMissingID
}
if i.Name == "" {
return nil, ErrMissingName
}
p := "/resources/stores/secret/" + i.ID + "/secrets/" + i.Name
resp, err := c.Get(p, &RequestOptions{
Headers: map[string]string{
"Content-Type": "application/json",
"Accept": "application/json",
},
Parallel: true,
})
if err != nil {
return nil, err
}
defer resp.Body.Close()
var output Secret
if err := json.NewDecoder(resp.Body).Decode(&output); err != nil {
return nil, err
}
return &output, nil
}
// DeleteSecretInput is used as input to the DeleteSecret function.
type DeleteSecretInput struct {
// ID of the Secret Store (required).
ID string
// Name of the secret (required).
Name string
}
// DeleteSecret deletes the specified resource.
func (c *Client) DeleteSecret(i *DeleteSecretInput) error {
if i.ID == "" {
return ErrMissingID
}
if i.Name == "" {
return ErrMissingName
}
p := "/resources/stores/secret/" + i.ID + "/secrets/" + i.Name
resp, err := c.Delete(p, &RequestOptions{
Headers: map[string]string{
"Content-Type": "application/json",
"Accept": "application/json",
},
Parallel: true,
})
if err != nil {
return err
}
return resp.Body.Close()
}
// ClientKey is an X25519 public key that can be used with
// golang.org/x/crypto/nacl/box to encrypt secrets locally before
// uploading them to the Fastly API. A client key is valid only for a
// short amount of time, and should be used immediately. The key is not
// valid after the ExpiresAt time.
//
// Client keys are signed, and the attached signature must be validated
// using the public signing key before it is used. A ValidateSignature
// method is provided for this purpose.
type ClientKey struct {
PublicKey []byte `json:"public_key"`
Signature []byte `json:"signature"`
ExpiresAt time.Time `json:"expires_at"`
}
// VerifySignature reports if the signingKey was used to generate the
// client key's signature. It must be a valid Ed25519 public key, and
// it will panic if len(signingKey) is not ed25519.PublicKeySize.
// https://pkg.go.dev/crypto/ed25519#PublicKeySize
func (ck *ClientKey) VerifySignature(signingKey ed25519.PublicKey) bool {
return ed25519.Verify(signingKey, ck.PublicKey, ck.Signature)
}
// Encrypt uses the client key to encrypt the provided plaintext
// using a libsodium-compatible sealed box.
// https://pkg.go.dev/golang.org/x/crypto/nacl/box#SealAnonymous
// https://libsodium.gitbook.io/doc/public-key_cryptography/sealed_boxes
func (ck *ClientKey) Encrypt(plaintext []byte) ([]byte, error) {
if len(ck.PublicKey) != 32 {
return nil, fmt.Errorf("invalid public key length %d", len(ck.PublicKey))
}
return box.SealAnonymous(nil, plaintext, (*[32]byte)(ck.PublicKey), rand.Reader)
}
// CreateClientKey creates a new time-limited client key for locally
// encrypting secrets before uploading them to the Fastly API.
func (c *Client) CreateClientKey() (*ClientKey, error) {
p := "/resources/stores/secret/client-key"
resp, err := c.Post(p, &RequestOptions{
Headers: map[string]string{
"Content-Type": "application/json",
"Accept": "application/json",
},
Parallel: true,
})
if err != nil {
return nil, err
}
defer resp.Body.Close()
var output ClientKey
if err := json.NewDecoder(resp.Body).Decode(&output); err != nil {
return nil, err
}
return &output, nil
}
// GetSigningKey returns the public signing key for client keys. In
// general the signing key changes very rarely, and it's recommended to
// ship the signing key out-of-band from the API.
func (c *Client) GetSigningKey() (ed25519.PublicKey, error) {
p := "/resources/stores/secret/signing-key"
resp, err := c.Get(p, &RequestOptions{
Headers: map[string]string{
"Content-Type": "application/json",
"Accept": "application/json",
},
Parallel: true,
})
if err != nil {
return nil, err
}
defer resp.Body.Close()
var output struct {
SigningKey []byte `json:"signing_key"`
}
if err := json.NewDecoder(resp.Body).Decode(&output); err != nil {
return nil, err
}
return ed25519.PublicKey(output.SigningKey), nil
}