/
s3storeKMS.go
390 lines (323 loc) · 9.37 KB
/
s3storeKMS.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
package store
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/aws/aws-sdk-go/service/sts"
)
// latest is used to keep a single object in s3 with all of the
// most recent values for the given service's secrets. Keeping this
// in a single s3 object allows us to use a single s3 GetObject
// for ListRaw (and thus chamber exec).
type LatestIndexFile struct {
Latest map[string]LatestValue `json:"latest"`
}
type LatestValue struct {
Version int `json:"version"`
Value string `json:"value"`
KMSAlias string `json:"KMSAlias"`
}
var _ Store = &S3KMSStore{}
type S3KMSStore struct {
S3Store
svc s3iface.S3API
stsSvc *sts.STS
bucket string
kmsKeyAlias string
}
func NewS3KMSStore(numRetries int, bucket string, kmsKeyAlias string) (*S3KMSStore, error) {
session, region, err := getSession(numRetries)
if err != nil {
return nil, err
}
svc := s3.New(session, &aws.Config{
MaxRetries: aws.Int(numRetries),
Region: region,
})
stsSvc := sts.New(session, &aws.Config{
MaxRetries: aws.Int(numRetries),
Region: region,
})
if kmsKeyAlias == "" {
kmsKeyAlias = DefaultKeyID
}
s3store := &S3Store{
svc: svc,
stsSvc: stsSvc,
bucket: bucket,
}
return &S3KMSStore{
S3Store: *s3store,
svc: svc,
stsSvc: stsSvc,
bucket: bucket,
kmsKeyAlias: kmsKeyAlias,
}, nil
}
func (s *S3KMSStore) Write(id SecretId, value string) error {
index, err := s.readLatest(id.Service)
if err != nil {
return err
}
if val, ok := index.Latest[id.Key]; val.KMSAlias != s.kmsKeyAlias && ok {
return fmt.Errorf("Unable to overwrite secret %s using new KMS key %s; mismatch with existing key %s", id.Key, s.kmsKeyAlias, val.KMSAlias)
}
objPath := getObjectPath(id)
existing, ok, err := s.readObjectById(id)
if err != nil {
return err
}
var obj secretObject
if ok {
obj = existing
} else {
obj = secretObject{
Service: id.Service,
Key: fmt.Sprintf("/%s/%s", id.Service, id.Key),
Values: map[int]secretVersion{},
}
}
thisVersion := getLatestVersion(obj.Values) + 1
user, err := s.getCurrentUser()
if err != nil {
return err
}
obj.Values[thisVersion] = secretVersion{
Version: thisVersion,
Value: value,
Created: time.Now().UTC(),
CreatedBy: user,
}
pruneOldVersions(obj.Values)
contents, err := json.Marshal(obj)
if err != nil {
return err
}
putObjectInput := &s3.PutObjectInput{
Bucket: aws.String(s.bucket),
ServerSideEncryption: aws.String(s3.ServerSideEncryptionAwsKms),
SSEKMSKeyId: aws.String(s.kmsKeyAlias),
Key: aws.String(objPath),
Body: bytes.NewReader(contents),
}
_, err = s.svc.PutObject(putObjectInput)
if err != nil {
// TODO: catch specific awserr
return err
}
index.Latest[id.Key] = LatestValue{
Version: thisVersion,
Value: value,
KMSAlias: s.kmsKeyAlias,
}
return s.writeLatest(id.Service, index)
}
func (s *S3KMSStore) ListServices(service string, includeSecretName bool) ([]string, error) {
return nil, fmt.Errorf("S3KMS Backend is experimental and does not implement this command")
}
func (s *S3KMSStore) List(service string, includeValues bool) ([]Secret, error) {
index, err := s.readLatest(service)
if err != nil {
return []Secret{}, err
}
secrets := []Secret{}
for key := range index.Latest {
obj, ok, err := s.readObjectById(SecretId{Service: service, Key: key})
if err != nil {
return []Secret{}, err
}
if !ok {
return []Secret{}, ErrSecretNotFound
}
version := getLatestVersion(obj.Values)
val, ok := obj.Values[version]
if !ok {
return []Secret{}, ErrSecretNotFound
}
s := Secret{
Meta: SecretMetadata{
Created: val.Created,
CreatedBy: val.CreatedBy,
Version: val.Version,
Key: obj.Key,
},
}
if includeValues {
s.Value = &val.Value
}
secrets = append(secrets, s)
}
return secrets, nil
}
// ListRaw returns RawSecrets by extracting them from the index file. It only ever uses the
// index file; it never consults the actual secrets, so if the index file is out of sync, these
// results will reflect that.
func (s *S3KMSStore) ListRaw(service string) ([]RawSecret, error) {
index, err := s.readLatest(service)
if err != nil {
return []RawSecret{}, err
}
// Read raw secrets directly from the index file (which caches the latest values)
secrets := []RawSecret{}
for key := range index.Latest {
s := RawSecret{
Key: fmt.Sprintf("/%s/%s", service, key),
Value: index.Latest[key].Value,
}
secrets = append(secrets, s)
}
return secrets, nil
}
func (s *S3KMSStore) Delete(id SecretId) error {
index, err := s.readLatest(id.Service)
if err != nil {
return err
}
if val, ok := index.Latest[id.Key]; val.KMSAlias != s.kmsKeyAlias && ok {
return fmt.Errorf("Unable to overwrite secret %s using new KMS key %s; mismatch with existing key %s", id.Key, s.kmsKeyAlias, val.KMSAlias)
}
if _, ok := index.Latest[id.Key]; ok {
delete(index.Latest, id.Key)
}
if err := s.deleteObjectById(id); err != nil {
return err
}
return s.writeLatest(id.Service, index)
}
func (s *S3KMSStore) readObject(path string) (secretObject, bool, error) {
getObjectInput := &s3.GetObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(path),
}
resp, err := s.svc.GetObject(getObjectInput)
if err != nil {
// handle aws errors
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case s3.ErrCodeNoSuchBucket:
return secretObject{}, false, err
case s3.ErrCodeNoSuchKey:
return secretObject{}, false, nil
default:
return secretObject{}, false, err
}
}
// generic errors
return secretObject{}, false, err
}
raw, err := ioutil.ReadAll(resp.Body)
if err != nil {
return secretObject{}, false, err
}
var obj secretObject
if err := json.Unmarshal(raw, &obj); err != nil {
return secretObject{}, false, err
}
return obj, true, nil
}
func (s *S3KMSStore) puts3raw(path string, contents []byte) error {
putObjectInput := &s3.PutObjectInput{
Bucket: aws.String(s.bucket),
ServerSideEncryption: aws.String(s3.ServerSideEncryptionAwsKms),
SSEKMSKeyId: aws.String(s.kmsKeyAlias),
Key: aws.String(path),
Body: bytes.NewReader(contents),
}
_, err := s.svc.PutObject(putObjectInput)
return err
}
func (s *S3KMSStore) readLatestFile(path string) (LatestIndexFile, error) {
getObjectInput := &s3.GetObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(path),
}
resp, err := s.svc.GetObject(getObjectInput)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
if aerr.Code() == "AccessDenied" {
// If we're not able to read the latest index for a KMS Key then proceed like it doesn't exist.
// We do this because in a chamber secret folder there might be other secrets written with a KMS Key that you don't have access to.
return LatestIndexFile{Latest: map[string]LatestValue{}}, nil
}
if aerr.Code() == s3.ErrCodeNoSuchKey {
// Index doesn't exist yet, return an empty index
return LatestIndexFile{Latest: map[string]LatestValue{}}, nil
}
}
return LatestIndexFile{}, err
}
raw, err := ioutil.ReadAll(resp.Body)
if err != nil {
return LatestIndexFile{}, err
}
var index LatestIndexFile
if err := json.Unmarshal(raw, &index); err != nil {
return LatestIndexFile{}, err
}
return index, nil
}
func (s *S3KMSStore) readLatest(service string) (LatestIndexFile, error) {
// Create an empty latest, this will be used to merge together the various KMS Latest Files
latestResult := LatestIndexFile{Latest: map[string]LatestValue{}}
// List all the files that are prefixed with kms and use them as latest.json files for that KMS Key.
params := &s3.ListObjectsInput{
Bucket: aws.String(s.bucket),
Prefix: aws.String(fmt.Sprintf("%s/__kms", service)),
}
var paginationError error
err := s.svc.ListObjectsPages(params, func(page *s3.ListObjectsOutput, lastPage bool) bool {
for index := range page.Contents {
key_name := *page.Contents[index].Key
result, err := s.readLatestFile(key_name)
if err != nil {
paginationError = errors.New(fmt.Sprintf("Error reading latest index for KMS Key (%s): %s", key_name, err))
return false
}
// Check if the chamber key already exists in the index.Latest map.
// Prefer the most recent version.
for k, v := range result.Latest {
if val, ok := latestResult.Latest[k]; ok {
if val.Version > v.Version {
latestResult.Latest[k] = val
} else {
latestResult.Latest[k] = v
}
} else {
latestResult.Latest[k] = v
}
}
}
return !lastPage
})
if paginationError != nil {
return latestResult, paginationError
}
if err != nil {
return latestResult, err
}
return latestResult, nil
}
func (s *S3KMSStore) latestFileKeyNameByKMSKey() string {
return fmt.Sprintf("__kms_%s__latest.json", strings.Replace(s.kmsKeyAlias, "/", "_", -1))
}
func (s *S3KMSStore) writeLatest(service string, index LatestIndexFile) error {
path := fmt.Sprintf("%s/%s", service, s.latestFileKeyNameByKMSKey())
for k, v := range index.Latest {
if v.KMSAlias != s.kmsKeyAlias {
delete(index.Latest, k)
}
}
raw, err := json.Marshal(index)
if err != nil {
return err
}
return s.puts3raw(path, raw)
}