Debian Codegraph @

integration/awskms/aws_kms_client_test.go

raw
1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package awskms
16
17 import (
18 "bytes"
19 "encoding/hex"
20 "os"
21 "path/filepath"
22 "strings"
23 "testing"
24
25 "github.com/aws/aws-sdk-go/aws"
26 "github.com/aws/aws-sdk-go/service/kms"
27 "github.com/tink-crypto/tink-go/v2/core/registry"
28 "github.com/tink-crypto/tink-go-awskms/v2/integration/awskms/internal/fakeawskms"
29 )
30
31 func testFilePath(t *testing.T, filename string) string {
32 t.Helper()
33 srcDir, ok := os.LookupEnv("TEST_SRCDIR")
34 if ok {
35 workspaceDir, ok := os.LookupEnv("TEST_WORKSPACE")
36 if !ok {
37 t.Fatal("TEST_WORKSPACE not found")
38 }
39 return filepath.Join(srcDir, workspaceDir, filename)
40 }
41 return filepath.Join("../..", filename)
42 }
43
44 func TestNewClientWithOptions_URIPrefix(t *testing.T) {
45 // Necessary for testing deprecated factory functions.
46 credFile := testFilePath(t, "testdata/aws/credentials.csv")
47 keyARN := "arn:aws:kms:us-east-2:235739564943:key/3ee50705-5a82-4f5b-9753-05c4f473922f"
48 fakekms, err := fakeawskms.New([]string{keyARN})
49 if err != nil {
50 t.Fatalf("fakeawskms.New() failed: %v", err)
51 }
52
53 tests := []struct {
54 name string
55 uriPrefix string
56 valid bool
57 }{
58 {
59 name: "AWS partition",
60 uriPrefix: "aws-kms://arn:aws:kms:us-east-2:235739564943:key/3ee50705-5a82-4f5b-9753-05c4f473922f",
61 valid: true,
62 },
63 {
64 name: "AWS US government partition",
65 uriPrefix: "aws-kms://arn:aws-us-gov:kms:us-gov-east-1:235739564943:key/3ee50705-5a82-4f5b-9753-05c4f473922f",
66 valid: true,
67 },
68 {
69 name: "AWS CN partition",
70 uriPrefix: "aws-kms://arn:aws-cn:kms:cn-north-1:235739564943:key/3ee50705-5a82-4f5b-9753-05c4f473922f",
71 valid: true,
72 },
73 {
74 name: "invalid",
75 uriPrefix: "bad-prefix://arn:aws-cn:kms:cn-north-1:235739564943:key/3ee50705-5a82-4f5b-9753-05c4f473922f",
76 valid: false,
77 },
78 }
79
80 for _, test := range tests {
81 t.Run(test.name, func(t *testing.T) {
82 _, err := NewClientWithOptions(test.uriPrefix)
83 if test.valid && err != nil {
84 t.Errorf("NewClientWithOptions(%q) err = %v, want nil", test.uriPrefix, err)
85 }
86 if !test.valid && err == nil {
87 t.Errorf("NewClientWithOptions(%q) err = nil, want error", test.uriPrefix)
88 }
89
90 // Test deprecated factory functions.
91 _, err = NewClient(test.uriPrefix)
92 if test.valid && err != nil {
93 t.Errorf("NewClient(%q) err = %v, want nil", test.uriPrefix, err)
94 }
95 if !test.valid && err == nil {
96 t.Errorf("NewClient(%q) err = nil, want error", test.uriPrefix)
97 }
98
99 _, err = NewClientWithCredentials(test.uriPrefix, credFile)
100 if test.valid && err != nil {
101 t.Errorf("NewClientWithCredentialPath(%q, _) err = %v, want nil", test.uriPrefix, err)
102 }
103 if !test.valid && err == nil {
104 t.Errorf("NewClientWithCredentialPath(%q, _) err = nil, want error", test.uriPrefix)
105 }
106
107 _, err = NewClientWithKMS(test.uriPrefix, fakekms)
108 if test.valid && err != nil {
109 t.Errorf("NewClientWithKMS(%q, _) err = %v, want nil", test.uriPrefix, err)
110 }
111 if !test.valid && err == nil {
112 t.Errorf("NewClientWithKMS(%q, _) err = nil, want error", test.uriPrefix)
113 }
114 })
115 }
116 }
117
118 func TestNewClientWithOptions_WithCredentialPath(t *testing.T) {
119 srcDir, ok := os.LookupEnv("TEST_SRCDIR")
120 if !ok {
121 t.Skip("TEST_SRCDIR not set")
122 }
123 workspaceDir, ok := os.LookupEnv("TEST_WORKSPACE")
124 if !ok {
125 t.Skip("TEST_WORKSPACE not set")
126 }
127
128 uriPrefix := "aws-kms://arn:aws-us-gov:kms:us-gov-east-1:235739564943:key/"
129
130 tests := []struct {
131 name string
132 credFile string
133 valid bool
134 }{
135 {
136 name: "valid CSV credentials file",
137 credFile: filepath.Join(srcDir, workspaceDir, "testdata/aws/credentials.csv"),
138 valid: true,
139 },
140 {
141 name: "valid INI credentials file",
142 credFile: filepath.Join(srcDir, workspaceDir, "testdata/aws/credentials.cred"),
143 valid: true,
144 },
145 {
146 name: "invalid credentials file",
147 credFile: filepath.Join(srcDir, workspaceDir, "testdata/aws/access_keys_bad.csv"),
148 valid: false,
149 },
150 }
151
152 for _, test := range tests {
153 t.Run(test.name, func(t *testing.T) {
154 _, err := NewClientWithOptions(uriPrefix, WithCredentialPath(test.credFile))
155 if test.valid && err != nil {
156 t.Errorf("NewClientWithOptions(uriPrefix, WithCredentialPath(%q)) err = %v, want nil", test.credFile, err)
157 }
158 if !test.valid && err == nil {
159 t.Errorf("NewClientWithOptions(uriPrefix, WithCredentialPath(%q)) err = nil, want error", test.credFile)
160 }
161
162 // Test deprecated factory function.
163 _, err = NewClientWithCredentials(uriPrefix, test.credFile)
164 if test.valid && err != nil {
165 t.Errorf("NewClientWithCredentials(uriPrefix, %q) err = %v, want nil", test.credFile, err)
166 }
167 if !test.valid && err == nil {
168 t.Errorf("NewClientWithCredentials(uriPrefix, %q) err = nil, want error", test.credFile)
169 }
170
171 })
172 }
173 }
174
175 func TestNewClientWithOptions_RepeatedWithKMSFails(t *testing.T) {
176 keyARN := "arn:aws:kms:us-east-2:235739564943:key/3ee50705-5a82-4f5b-9753-05c4f473922f"
177 fakekms, err := fakeawskms.New([]string{keyARN})
178 if err != nil {
179 t.Fatalf("fakekms.New() failed: %v", err)
180 }
181
182 _, err = NewClientWithOptions("aws-kms://", WithKMS(fakekms), WithKMS(fakekms))
183 if err == nil {
184 t.Fatalf("NewClientWithOptions(_, WithKMS(_), WithKMS(_)) err = nil, want error")
185 }
186 }
187
188 func TestNewClientWithOptions_RepeatedWithEncryptionContextNameFails(t *testing.T) {
189 _, err := NewClientWithOptions("aws-kms://", WithEncryptionContextName(LegacyAdditionalData), WithEncryptionContextName(AssociatedData))
190 if err == nil {
191 t.Fatalf("NewClientWithOptions(_, WithEncryptionContextName(_), WithEncryptionContextName(_)) err = nil, want error")
192 }
193 }
194
195 func TestSupported(t *testing.T) {
196 uriPrefix := "aws-kms://arn:aws-us-gov:kms:us-gov-east-1:235739564943:key/"
197 supportedKeyURI := "aws-kms://arn:aws-us-gov:kms:us-gov-east-1:235739564943:key/3ee50705-5a82-4f5b-9753-05c4f473922f"
198 nonSupportedKeyURI := "aws-kms://arn:aws-us-gov:kms:us-gov-east-DOES-NOT-EXIST:key/"
199
200 client, err := NewClientWithOptions(uriPrefix)
201 if err != nil {
202 t.Fatalf("NewClientWithOptions() failed: %v", err)
203 }
204
205 if !client.Supported(supportedKeyURI) {
206 t.Errorf("client with URI prefix %q should support key URI %q", uriPrefix, supportedKeyURI)
207 }
208
209 if client.Supported(nonSupportedKeyURI) {
210 t.Errorf("client with URI prefix %q should NOT support key URI %q", uriPrefix, nonSupportedKeyURI)
211 }
212 }
213
214 func TestGetAEADSupportedURI(t *testing.T) {
215 uriPrefix := "aws-kms://arn:aws-us-gov:kms:us-gov-east-1:235739564943:key/"
216 supportedKeyURI := "aws-kms://arn:aws-us-gov:kms:us-gov-east-1:235739564943:key/3ee50705-5a82-4f5b-9753-05c4f473922f"
217
218 client, err := NewClientWithOptions(uriPrefix)
219 if err != nil {
220 t.Fatalf("NewClientWithOptions() failed: %v", err)
221 }
222
223 _, err = client.GetAEAD(supportedKeyURI)
224 if err != nil {
225 t.Errorf("client with URI prefix %q should support key URI %q", uriPrefix, supportedKeyURI)
226 }
227 }
228
229 func TestGetAEADEncryptDecrypt(t *testing.T) {
230 keyARN := "arn:aws:kms:us-east-2:235739564943:key/3ee50705-5a82-4f5b-9753-05c4f473922f"
231 keyURI := "aws-kms://arn:aws:kms:us-east-2:235739564943:key/3ee50705-5a82-4f5b-9753-05c4f473922f"
232 fakekms, err := fakeawskms.New([]string{keyARN})
233 if err != nil {
234 t.Fatalf("fakekms.New() failed: %v", err)
235 }
236
237 client, err := NewClientWithOptions("aws-kms://", WithKMS(fakekms))
238 if err != nil {
239 t.Fatalf("NewClientWithOptions() failed: %v", err)
240 }
241
242 a, err := client.GetAEAD(keyURI)
243 if err != nil {
244 t.Fatalf("client.GetAEAD(keyURI) err = %v, want nil", err)
245 }
246
247 plaintext := []byte("plaintext")
248 associatedData := []byte("associatedData")
249 ciphertext, err := a.Encrypt(plaintext, associatedData)
250 if err != nil {
251 t.Fatalf("a.Encrypt(plaintext, associatedData) err = %v, want nil", err)
252 }
253 decrypted, err := a.Decrypt(ciphertext, associatedData)
254 if err != nil {
255 t.Fatalf("a.Decrypt(ciphertext, associatedData) err = %v, want nil", err)
256 }
257 if !bytes.Equal(decrypted, plaintext) {
258 t.Errorf("decrypted = %q, want %q", decrypted, plaintext)
259 }
260
261 _, err = a.Decrypt(ciphertext, []byte("invalidAssociatedData"))
262 if err == nil {
263 t.Error("a.Decrypt(ciphertext, []byte(\"invalidAssociatedData\")) err = nil, want error")
264 }
265
266 _, err = a.Decrypt([]byte("invalidCiphertext"), associatedData)
267 if err == nil {
268 t.Error("a.Decrypt([]byte(\"invalidCiphertext\"), associatedData) err = nil, want error")
269 }
270 }
271
272 func TestUsesAdditionalDataAsContextName(t *testing.T) {
273 keyARN := "arn:aws:kms:us-east-2:235739564943:key/3ee50705-5a82-4f5b-9753-05c4f473922f"
274 keyURI := "aws-kms://arn:aws:kms:us-east-2:235739564943:key/3ee50705-5a82-4f5b-9753-05c4f473922f"
275 fakekms, err := fakeawskms.New([]string{keyARN})
276 if err != nil {
277 t.Fatalf("fakeawskms.New() failed: %v", err)
278 }
279
280 client, err := NewClientWithKMS("aws-kms://", fakekms)
281 if err != nil {
282 t.Fatalf("NewClientWithKMS() failed: %v", err)
283 }
284
285 a, err := client.GetAEAD(keyURI)
286 if err != nil {
287 t.Fatalf("client.GetAEAD(keyURI) failed: %s", err)
288 }
289
290 plaintext := []byte("plaintext")
291 associatedData := []byte("associatedData")
292 ciphertext, err := a.Encrypt(plaintext, associatedData)
293 if err != nil {
294 t.Fatalf("a.Encrypt(plaintext, associatedData) err = %v, want nil", err)
295 }
296
297 hexAD := hex.EncodeToString(associatedData)
298 context := map[string]*string{"additionalData": &hexAD}
299 decRequest := &kms.DecryptInput{
300 KeyId: aws.String(keyARN),
301 CiphertextBlob: ciphertext,
302 EncryptionContext: context,
303 }
304 decResponse, err := fakekms.Decrypt(decRequest)
305 if err != nil {
306 t.Fatalf("fakeKMS.Decrypt(decRequest) err = %s, want nil", err)
307 }
308 if !bytes.Equal(decResponse.Plaintext, plaintext) {
309 t.Errorf("decResponse.Plaintext = %q, want %q", decResponse.Plaintext, plaintext)
310 }
311 if strings.Compare(*decResponse.KeyId, keyARN) != 0 {
312 t.Errorf("decResponse.KeyId = %q, want %q", *decResponse.KeyId, keyARN)
313 }
314 }
315
316 func TestEncryptionContextName(t *testing.T) {
317 keyARN := "arn:aws:kms:us-east-2:235739564943:key/3ee50705-5a82-4f5b-9753-05c4f473922f"
318 keyURI := "aws-kms://arn:aws:kms:us-east-2:235739564943:key/3ee50705-5a82-4f5b-9753-05c4f473922f"
319 fakekms, err := fakeawskms.New([]string{keyARN})
320 if err != nil {
321 t.Fatalf("fakeawskms.New() failed: %v", err)
322 }
323
324 tests := []struct {
325 contextName EncryptionContextName
326 wantContextName string
327 }{
328 {
329 contextName: LegacyAdditionalData,
330 wantContextName: "additionalData",
331 },
332 {
333 contextName: AssociatedData,
334 wantContextName: "associatedData",
335 },
336 }
337
338 for _, test := range tests {
339 t.Run(test.wantContextName, func(t *testing.T) {
340 client, err := NewClientWithOptions("aws-kms://", WithKMS(fakekms), WithEncryptionContextName(test.contextName))
341 if err != nil {
342 t.Fatalf("NewClientWithOptions() failed: %v", err)
343 }
344
345 a, err := client.GetAEAD(keyURI)
346 if err != nil {
347 t.Fatalf("client.GetAEAD(keyURI) failed: %s", err)
348 }
349
350 plaintext := []byte("plaintext")
351 associatedData := []byte("associatedData")
352 ciphertext, err := a.Encrypt(plaintext, associatedData)
353 if err != nil {
354 t.Fatalf("a.Encrypt(plaintext, associatedData) err = %v, want nil", err)
355 }
356
357 hexAD := hex.EncodeToString(associatedData)
358 context := map[string]*string{test.wantContextName: &hexAD}
359 decRequest := &kms.DecryptInput{
360 KeyId: aws.String(keyARN),
361 CiphertextBlob: ciphertext,
362 EncryptionContext: context,
363 }
364 decResponse, err := fakekms.Decrypt(decRequest)
365 if err != nil {
366 t.Fatalf("fakeKMS.Decrypt(decRequest) err = %s, want nil", err)
367 }
368 if !bytes.Equal(decResponse.Plaintext, plaintext) {
369 t.Errorf("decResponse.Plaintext = %q, want %q", decResponse.Plaintext, plaintext)
370 }
371 if strings.Compare(*decResponse.KeyId, keyARN) != 0 {
372 t.Errorf("decResponse.KeyId = %q, want %q", *decResponse.KeyId, keyARN)
373 }
374 })
375 }
376 }
377
378 func TestEncryptionContextName_defaultEncryptionContextName(t *testing.T) {
379 keyARN := "arn:aws:kms:us-east-2:235739564943:key/3ee50705-5a82-4f5b-9753-05c4f473922f"
380 keyURI := "aws-kms://arn:aws:kms:us-east-2:235739564943:key/3ee50705-5a82-4f5b-9753-05c4f473922f"
381 fakekms, err := fakeawskms.New([]string{keyARN})
382 if err != nil {
383 t.Fatalf("fakeawskms.New() failed: %v", err)
384 }
385
386 tests := []struct {
387 name string
388 client func(t *testing.T) registry.KMSClient
389 wantContextName string
390 }{
391 {
392 name: "NewClientWithOptions",
393 client: func(t *testing.T) registry.KMSClient {
394 t.Helper()
395 c, err := NewClientWithOptions(keyURI, WithKMS(fakekms))
396 if err != nil {
397 t.Fatalf("NewClientWithOptions() failed: %v", err)
398 }
399 return c
400
401 },
402 wantContextName: "associatedData",
403 },
404 // Test deprecated factory function.
405 {
406 name: "NewClientWithKMS",
407 client: func(t *testing.T) registry.KMSClient {
408 t.Helper()
409 c, err := NewClientWithKMS(keyURI, fakekms)
410 if err != nil {
411 t.Fatalf("NewClientWithKMS() failed: %v", err)
412 }
413 return c
414 },
415 wantContextName: "additionalData",
416 },
417 }
418
419 for _, test := range tests {
420 t.Run(test.name, func(t *testing.T) {
421 client := test.client(t)
422 a, err := client.GetAEAD(keyURI)
423 if err != nil {
424 t.Fatalf("client.GetAEAD(keyURI) failed: %s", err)
425 }
426
427 plaintext := []byte("plaintext")
428 associatedData := []byte("associatedData")
429 ciphertext, err := a.Encrypt(plaintext, associatedData)
430 if err != nil {
431 t.Fatalf("a.Encrypt(plaintext, associatedData) err = %v, want nil", err)
432 }
433
434 hexAD := hex.EncodeToString(associatedData)
435 context := map[string]*string{test.wantContextName: &hexAD}
436 decRequest := &kms.DecryptInput{
437 KeyId: aws.String(keyARN),
438 CiphertextBlob: ciphertext,
439 EncryptionContext: context,
440 }
441 decResponse, err := fakekms.Decrypt(decRequest)
442 if err != nil {
443 t.Fatalf("fakeKMS.Decrypt(decRequest) err = %s, want nil", err)
444 }
445 if !bytes.Equal(decResponse.Plaintext, plaintext) {
446 t.Errorf("decResponse.Plaintext = %q, want %q", decResponse.Plaintext, plaintext)
447 }
448 if strings.Compare(*decResponse.KeyId, keyARN) != 0 {
449 t.Errorf("decResponse.KeyId = %q, want %q", *decResponse.KeyId, keyARN)
450 }
451 })
452 }
453 }