-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathkeysource_test.go
406 lines (345 loc) · 11.8 KB
/
keysource_test.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
/*
Copyright (C) 2022 The Flux authors
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package awskms
import (
"context"
"encoding/base64"
"fmt"
logger "log"
"os"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/kms"
awsv1 "github.com/aws/aws-sdk-go/aws"
sessionv1 "github.com/aws/aws-sdk-go/aws/session"
kmsv1 "github.com/aws/aws-sdk-go/service/kms"
. "github.com/onsi/gomega"
"github.com/ory/dockertest"
)
var (
testKMSServerURL string
testKMSARN string
)
const (
dummyARN = "arn:aws:kms:us-west-2:107501996527:key/612d5f0p-p1l3-45e6-aca6-a5b005693a48"
testLocalKMSTag = "3.11.1"
testLocalKMSImage = "nsmithuk/local-kms"
)
// TestMain initializes a AWS KMS server using Docker, writes the HTTP address to
// testAWSEndpoint, tries to generate a key for encryption-decryption using a
// backoff retry approach and then sets testKMSARN to the id of the generated key.
// It then runs all the tests, which can make use of the various `test*` variables.
func TestMain(m *testing.M) {
// Uses a sensible default on Windows (TCP/HTTP) and Linux/MacOS (socket)
pool, err := dockertest.NewPool("")
if err != nil {
logger.Fatalf("could not connect to docker: %s", err)
}
// Pull the image, create a container based on it, and run it
// resource, err := pool.Run("nsmithuk/local-kms", testLocalKMSVersion, []string{})
resource, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: testLocalKMSImage,
Tag: testLocalKMSTag,
ExposedPorts: []string{"8080"},
})
if err != nil {
logger.Fatalf("could not start resource: %s", err)
}
purgeResource := func() {
if err := pool.Purge(resource); err != nil {
logger.Printf("could not purge resource: %s", err)
}
}
testKMSServerURL = fmt.Sprintf("http://127.0.0.1:%v", resource.GetPort("8080/tcp"))
masterKey := createTestMasterKey(dummyARN)
kmsClient, err := createTestKMSClient(masterKey)
if err != nil {
purgeResource()
logger.Fatalf("could not create session: %s", err)
}
var key *kms.CreateKeyOutput
if err := pool.Retry(func() error {
key, err = kmsClient.CreateKey(context.TODO(), &kms.CreateKeyInput{})
if err != nil {
return err
}
return nil
}); err != nil {
purgeResource()
logger.Fatalf("could not create key: %s", err)
}
if key.KeyMetadata.Arn != nil {
testKMSARN = *key.KeyMetadata.Arn
} else {
purgeResource()
logger.Fatalf("could not set arn")
}
// Run the tests, but only if we succeeded in setting up the AWS KMS server.
var code int
if err == nil {
code = m.Run()
}
// This can't be deferred, as os.Exit simpy does not care
if err := pool.Purge(resource); err != nil {
logger.Fatalf("could not purge resource: %s", err)
}
os.Exit(code)
}
func TestMasterKey_Encrypt(t *testing.T) {
g := NewWithT(t)
key := createTestMasterKey(testKMSARN)
dataKey := []byte("thisistheway")
g.Expect(key.Encrypt(dataKey)).To(Succeed())
g.Expect(key.EncryptedKey).ToNot(BeEmpty())
kmsClient, err := createTestKMSClient(key)
g.Expect(err).ToNot(HaveOccurred())
k, err := base64.StdEncoding.DecodeString(key.EncryptedKey)
g.Expect(err).ToNot(HaveOccurred())
input := &kms.DecryptInput{
CiphertextBlob: k,
EncryptionContext: key.EncryptionContext,
}
decrypted, err := kmsClient.Decrypt(context.TODO(), input)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(decrypted.Plaintext).To(Equal(dataKey))
}
func TestMasterKey_Encrypt_SOPS_Compat(t *testing.T) {
g := NewWithT(t)
encryptKey := createTestMasterKey(testKMSARN)
dataKey := []byte("encrypt-compat")
g.Expect(encryptKey.Encrypt(dataKey)).To(Succeed())
// This is the core decryption logic of `sopskms.MasterKey.Decrypt()`.
// We don't call `sops.MasterKey.Decrypt()` directly to avoid issues with
// session and config setup.
config := awsv1.Config{
Region: awsv1.String("us-west-2"),
Endpoint: &testKMSServerURL,
}
t.Setenv("AWS_ACCESS_KEY_ID", "id")
t.Setenv("AWS_SECRET_ACCESS_KEY", "secret")
k, err := base64.StdEncoding.DecodeString(encryptKey.EncryptedKey)
g.Expect(err).ToNot(HaveOccurred())
sess, err := sessionv1.NewSessionWithOptions(sessionv1.Options{
Config: config,
})
kmsSvc := kmsv1.New(sess)
decrypted, err := kmsSvc.Decrypt(&kmsv1.DecryptInput{CiphertextBlob: k})
g.Expect(err).ToNot(HaveOccurred())
g.Expect(decrypted.Plaintext).To(Equal(dataKey))
}
func TestMasterKey_EncryptIfNeeded(t *testing.T) {
g := NewWithT(t)
key := createTestMasterKey(testKMSARN)
g.Expect(key.EncryptIfNeeded([]byte("data"))).To(Succeed())
encryptedKey := key.EncryptedKey
g.Expect(encryptedKey).ToNot(BeEmpty())
g.Expect(key.EncryptIfNeeded([]byte("some other data"))).To(Succeed())
g.Expect(key.EncryptedKey).To(Equal(encryptedKey))
}
func TestMasterKey_EncryptedDataKey(t *testing.T) {
g := NewWithT(t)
key := &MasterKey{EncryptedKey: "some key"}
g.Expect(key.EncryptedDataKey()).To(BeEquivalentTo(key.EncryptedKey))
}
func TestMasterKey_Decrypt(t *testing.T) {
g := NewWithT(t)
key := createTestMasterKey(testKMSARN)
kmsClient, err := createTestKMSClient(key)
g.Expect(err).ToNot(HaveOccurred())
dataKey := []byte("itsalwaysdns")
out, err := kmsClient.Encrypt(context.TODO(), &kms.EncryptInput{
Plaintext: dataKey, KeyId: &key.Arn, EncryptionContext: key.EncryptionContext,
})
g.Expect(err).ToNot(HaveOccurred())
key.EncryptedKey = base64.StdEncoding.EncodeToString(out.CiphertextBlob)
got, err := key.Decrypt()
g.Expect(err).NotTo(HaveOccurred())
g.Expect(got).To(Equal(dataKey))
}
func TestMasterKey_Decrypt_SOPS_Compat(t *testing.T) {
g := NewWithT(t)
// This is the core encryption logic of `sopskms.MasterKey.Encrypt()`.
// We don't call `sops.MasterKey.Encrypt()` directly to avoid issues with
// session and config setup.
dataKey := []byte("decrypt-compat")
config := awsv1.Config{
Region: awsv1.String("us-west-2"),
Endpoint: &testKMSServerURL,
}
t.Setenv("AWS_ACCESS_KEY_ID", "id")
t.Setenv("AWS_SECRET_ACCESS_KEY", "secret")
sess, err := sessionv1.NewSessionWithOptions(sessionv1.Options{
Config: config,
})
kmsSvc := kmsv1.New(sess)
encrypted, err := kmsSvc.Encrypt(&kmsv1.EncryptInput{Plaintext: dataKey, KeyId: &testKMSARN})
g.Expect(err).ToNot(HaveOccurred())
decryptKey := createTestMasterKey(testKMSARN)
decryptKey.EncryptedKey = base64.StdEncoding.EncodeToString(encrypted.CiphertextBlob)
dec, err := decryptKey.Decrypt()
g.Expect(err).ToNot(HaveOccurred())
g.Expect(dec).To(Equal(dataKey))
}
func TestMasterKey_EncryptDecrypt_RoundTrip(t *testing.T) {
g := NewWithT(t)
dataKey := []byte("thisistheway")
encryptKey := createTestMasterKey(testKMSARN)
g.Expect(encryptKey.Encrypt(dataKey)).To(Succeed())
g.Expect(encryptKey.EncryptedKey).ToNot(BeEmpty())
decryptKey := createTestMasterKey(testKMSARN)
decryptKey.EncryptedKey = encryptKey.EncryptedKey
decryptedData, err := decryptKey.Decrypt()
g.Expect(err).ToNot(HaveOccurred())
g.Expect(decryptedData).To(Equal(dataKey))
}
func TestMasterKey_NeedsRotation(t *testing.T) {
g := NewWithT(t)
key := NewMasterKeyFromArn(dummyARN, nil, "")
g.Expect(key.NeedsRotation()).To(BeFalse())
key.CreationDate = key.CreationDate.Add(-(kmsTTL + time.Second))
g.Expect(key.NeedsRotation()).To(BeTrue())
}
func TestMasterKey_ToMap(t *testing.T) {
g := NewWithT(t)
key := MasterKey{
Arn: "test-arn",
Role: "test-role",
EncryptedKey: "enc-key",
EncryptionContext: map[string]string{
"env": "test",
},
}
g.Expect(key.ToMap()).To(Equal(map[string]interface{}{
"arn": "test-arn",
"role": "test-role",
"created_at": "0001-01-01T00:00:00Z",
"enc": "enc-key",
"context": map[string]string{
"env": "test",
},
}))
}
func TestCreds_ApplyToMasterKey(t *testing.T) {
g := NewWithT(t)
creds := CredsProvider{
credsProvider: credentials.NewStaticCredentialsProvider("", "", ""),
}
key := &MasterKey{}
creds.ApplyToMasterKey(key)
g.Expect(key.credentialsProvider).To(Equal(creds.credsProvider))
}
func TestLoadAwsKmsCredsFromYaml(t *testing.T) {
g := NewWithT(t)
credsYaml := []byte(`
aws_access_key_id: test-id
aws_secret_access_key: test-secret
aws_session_token: test-token
`)
credsProvider, err := LoadCredsProviderFromYaml(credsYaml)
g.Expect(err).ToNot(HaveOccurred())
creds, err := credsProvider.credsProvider.Retrieve(context.TODO())
g.Expect(err).ToNot(HaveOccurred())
g.Expect(creds.AccessKeyID).To(Equal("test-id"))
g.Expect(creds.SecretAccessKey).To(Equal("test-secret"))
g.Expect(creds.SessionToken).To(Equal("test-token"))
}
func Test_createKMSConfig(t *testing.T) {
tests := []struct {
name string
key MasterKey
assertFunc func(g *WithT, cfg *aws.Config, err error)
fallback bool
}{
{
name: "master key with invalid arn fails",
key: MasterKey{
Arn: "arn:gcp:kms:antartica-north-2::key/45e6-aca6-a5b005693a48",
},
assertFunc: func(g *WithT, _ *aws.Config, err error) {
g.Expect(err).To(HaveOccurred())
g.Expect(err.Error()).To(ContainSubstring("no valid ARN found"))
},
},
{
name: "master key with with proper configuration passes",
key: MasterKey{
credentialsProvider: credentials.NewStaticCredentialsProvider("test-id", "test-secret", "test-token"),
AwsProfile: "test-profile",
Arn: "arn:aws:kms:us-west-2:107501996527:key/612d5f0p-p1l3-45e6-aca6-a5b005693a48",
},
assertFunc: func(g *WithT, cfg *aws.Config, err error) {
g.Expect(err).ToNot(HaveOccurred())
g.Expect(cfg.Region).To(Equal("us-west-2"))
creds, err := cfg.Credentials.Retrieve(context.TODO())
g.Expect(err).ToNot(HaveOccurred())
g.Expect(creds.AccessKeyID).To(Equal("test-id"))
g.Expect(creds.SecretAccessKey).To(Equal("test-secret"))
g.Expect(creds.SessionToken).To(Equal("test-token"))
// ConfigSources is a slice of config.Config, which in turn is an interface.
// Since we use a LoadOptions object, we assert the type of cfgSrc and then
// check if the expected profile is present.
for _, cfgSrc := range cfg.ConfigSources {
if src, ok := cfgSrc.(config.LoadOptions); ok {
g.Expect(src.SharedConfigProfile).To(Equal("test-profile"))
}
}
},
},
{
name: "master key without creds and profile falls back to the environment variables",
key: MasterKey{
Arn: "arn:aws:kms:us-west-2:107501996527:key/612d5f0p-p1l3-45e6-aca6-a5b005693a48",
},
fallback: true,
assertFunc: func(g *WithT, cfg *aws.Config, err error) {
g.Expect(err).ToNot(HaveOccurred())
creds, err := cfg.Credentials.Retrieve(context.TODO())
g.Expect(creds.AccessKeyID).To(Equal("id"))
g.Expect(creds.SecretAccessKey).To(Equal("secret"))
g.Expect(creds.SessionToken).To(Equal("token"))
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
// Set the environment variables if we want to fallback
if tt.fallback {
t.Setenv("AWS_ACCESS_KEY_ID", "id")
t.Setenv("AWS_SECRET_ACCESS_KEY", "secret")
t.Setenv("AWS_SESSION_TOKEN", "token")
}
cfg, err := tt.key.createKMSConfig()
tt.assertFunc(g, cfg, err)
})
}
}
func createTestMasterKey(arn string) MasterKey {
return MasterKey{
Arn: arn,
credentialsProvider: credentials.NewStaticCredentialsProvider("id", "secret", ""),
epResolver: epResolver{},
}
}
// epResolver is a dummy resolver that points to the local test KMS server
type epResolver struct{}
func (e epResolver) ResolveEndpoint(service, region string, options ...interface{}) (aws.Endpoint, error) {
return aws.Endpoint{
URL: testKMSServerURL,
}, nil
}
func createTestKMSClient(key MasterKey) (*kms.Client, error) {
cfg, err := key.createKMSConfig()
if err != nil {
return nil, err
}
cfg.EndpointResolverWithOptions = epResolver{}
return kms.NewFromConfig(*cfg), nil
}