-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathtest_i_aws_encrytion_sdk_client.py
452 lines (405 loc) · 20.7 KB
/
test_i_aws_encrytion_sdk_client.py
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
# Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
"""Integration test suite for `aws_encryption_sdk`."""
import io
import logging
import pytest
from botocore.exceptions import BotoCoreError
import aws_encryption_sdk
from aws_encryption_sdk.identifiers import USER_AGENT_SUFFIX, Algorithm
from aws_encryption_sdk.key_providers.kms import KMSMasterKey, KMSMasterKeyProvider
from .integration_test_utils import (
get_cmk_arn,
setup_kms_master_key_provider,
setup_kms_master_key_provider_with_botocore_session,
)
pytestmark = [pytest.mark.integ]
VALUES = {
"plaintext_128": (
b"\xa3\xf6\xbc\x89\x95\x15(\xc8}\\\x8d=zu^{JA\xc1\xe9\xf0&m\xe6TD\x03"
b"\x165F\x85\xae\x96\xd9~ \xa6\x13\x88\xf8\xdb\xc9\x0c\xd8\xd8\xd4\xe0"
b"\x02\xe9\xdb+\xd4l\xeaq\xf6\xba.cg\xda\xe4V\xd9\x9a\x96\xe8\xf4:\xf5"
b"\xfd\xd7\xa6\xfa\xd1\x85\xa7o\xf5\x94\xbcE\x14L\xa1\x87\xd9T\xa6\x95"
b"eZVv\xfe[\xeeJ$a<9\x1f\x97\xe1\xd6\x9dQc\x8b7n\x0f\x1e\xbd\xf5\xba"
b"\x0e\xae|%\xd8L]\xa2\xa2\x08\x1f"
),
"encryption_context": {"key_a": "value_a", "key_b": "value_b", "key_c": "value_c"},
}
def test_encrypt_verify_user_agent_kms_master_key_provider(caplog):
caplog.set_level(level=logging.DEBUG)
mkp = setup_kms_master_key_provider()
mk = mkp.master_key(get_cmk_arn())
mk.generate_data_key(algorithm=Algorithm.AES_256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384, encryption_context={})
assert USER_AGENT_SUFFIX in caplog.text
def test_encrypt_verify_user_agent_kms_master_key(caplog):
caplog.set_level(level=logging.DEBUG)
mk = KMSMasterKey(key_id=get_cmk_arn())
mk.generate_data_key(algorithm=Algorithm.AES_256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384, encryption_context={})
assert USER_AGENT_SUFFIX in caplog.text
def test_remove_bad_client():
test = KMSMasterKeyProvider()
test.add_regional_client("us-fakey-12")
with pytest.raises(BotoCoreError):
test._regional_clients["us-fakey-12"].list_keys()
assert not test._regional_clients
def test_regional_client_does_not_modify_botocore_session(caplog):
mkp = setup_kms_master_key_provider_with_botocore_session()
fake_region = "us-fakey-12"
assert mkp.config.botocore_session.get_config_variable("region") != fake_region
mkp.add_regional_client(fake_region)
assert mkp.config.botocore_session.get_config_variable("region") != fake_region
class TestKMSThickClientIntegration(object):
@pytest.fixture(autouse=True)
def apply_fixtures(self):
self.kms_master_key_provider = setup_kms_master_key_provider()
def test_encryption_cycle_default_algorithm_framed_stream(self):
"""Test that the enrypt/decrypt cycle completes successfully
for a framed message using the default algorithm.
"""
with aws_encryption_sdk.stream(
source=io.BytesIO(VALUES["plaintext_128"]),
key_provider=self.kms_master_key_provider,
mode="e",
encryption_context=VALUES["encryption_context"],
) as encryptor:
ciphertext = encryptor.read()
header_1 = encryptor.header
with aws_encryption_sdk.stream(
source=io.BytesIO(ciphertext), key_provider=self.kms_master_key_provider, mode="d"
) as decryptor:
plaintext = decryptor.read()
header_2 = decryptor.header
assert plaintext == VALUES["plaintext_128"]
assert header_1.encryption_context == header_2.encryption_context
def test_encryption_cycle_default_algorithm_framed_stream_many_lines(self):
"""Test that the enrypt/decrypt cycle completes successfully
for a framed message with many frames using the default algorithm.
"""
ciphertext = b""
with aws_encryption_sdk.stream(
source=io.BytesIO(VALUES["plaintext_128"] * 10),
key_provider=self.kms_master_key_provider,
mode="e",
encryption_context=VALUES["encryption_context"],
frame_length=128,
) as encryptor:
for chunk in encryptor:
ciphertext += chunk
header_1 = encryptor.header
plaintext = b""
with aws_encryption_sdk.stream(
source=io.BytesIO(ciphertext), key_provider=self.kms_master_key_provider, mode="d"
) as decryptor:
for chunk in decryptor:
plaintext += chunk
header_2 = decryptor.header
assert plaintext == VALUES["plaintext_128"] * 10
assert header_1.encryption_context == header_2.encryption_context
def test_encryption_cycle_default_algorithm_non_framed(self):
"""Test that the enrypt/decrypt cycle completes successfully
for a non-framed message using the default algorithm.
"""
ciphertext, _ = aws_encryption_sdk.encrypt(
source=VALUES["plaintext_128"],
key_provider=self.kms_master_key_provider,
encryption_context=VALUES["encryption_context"],
frame_length=0,
)
plaintext, _ = aws_encryption_sdk.decrypt(source=ciphertext, key_provider=self.kms_master_key_provider)
assert plaintext == VALUES["plaintext_128"]
def test_encryption_cycle_default_algorithm_non_framed_no_encryption_context(self):
"""Test that the enrypt/decrypt cycle completes successfully
for a non-framed message using the default algorithm.
"""
ciphertext, _ = aws_encryption_sdk.encrypt(
source=VALUES["plaintext_128"], key_provider=self.kms_master_key_provider, frame_length=0
)
plaintext, _ = aws_encryption_sdk.decrypt(source=ciphertext, key_provider=self.kms_master_key_provider)
assert plaintext == VALUES["plaintext_128"]
def test_encryption_cycle_default_algorithm_single_frame(self):
"""Test that the enrypt/decrypt cycle completes successfully
for a single frame message using the default algorithm.
"""
ciphertext, _ = aws_encryption_sdk.encrypt(
source=VALUES["plaintext_128"],
key_provider=self.kms_master_key_provider,
encryption_context=VALUES["encryption_context"],
frame_length=1024,
)
plaintext, _ = aws_encryption_sdk.decrypt(source=ciphertext, key_provider=self.kms_master_key_provider)
assert plaintext == VALUES["plaintext_128"]
def test_encryption_cycle_default_algorithm_multiple_frames(self):
"""Test that the enrypt/decrypt cycle completes successfully
for a framed message with multiple frames using the
default algorithm.
"""
ciphertext, _ = aws_encryption_sdk.encrypt(
source=VALUES["plaintext_128"] * 100,
key_provider=self.kms_master_key_provider,
encryption_context=VALUES["encryption_context"],
frame_length=1024,
)
plaintext, _ = aws_encryption_sdk.decrypt(source=ciphertext, key_provider=self.kms_master_key_provider)
assert plaintext == VALUES["plaintext_128"] * 100
def test_encryption_cycle_aes_128_gcm_iv12_tag16_single_frame(self):
"""Test that the enrypt/decrypt cycle completes successfully
for a single frame message using the aes_128_gcm_iv12_tag16
algorithm.
"""
ciphertext, _ = aws_encryption_sdk.encrypt(
source=VALUES["plaintext_128"],
key_provider=self.kms_master_key_provider,
encryption_context=VALUES["encryption_context"],
frame_length=1024,
algorithm=Algorithm.AES_128_GCM_IV12_TAG16,
)
plaintext, _ = aws_encryption_sdk.decrypt(source=ciphertext, key_provider=self.kms_master_key_provider)
assert plaintext == VALUES["plaintext_128"]
def test_encryption_cycle_aes_128_gcm_iv12_tag16_non_framed(self):
"""Test that the enrypt/decrypt cycle completes successfully
for a non-framed message using the aes_128_gcm_iv12_tag16
algorithm.
"""
ciphertext, _ = aws_encryption_sdk.encrypt(
source=VALUES["plaintext_128"],
key_provider=self.kms_master_key_provider,
encryption_context=VALUES["encryption_context"],
frame_length=0,
algorithm=Algorithm.AES_128_GCM_IV12_TAG16,
)
plaintext, _ = aws_encryption_sdk.decrypt(source=ciphertext, key_provider=self.kms_master_key_provider)
assert plaintext == VALUES["plaintext_128"]
def test_encryption_cycle_aes_192_gcm_iv12_tag16_single_frame(self):
"""Test that the enrypt/decrypt cycle completes successfully
for a single frame message using the aes_192_gcm_iv12_tag16
algorithm.
"""
ciphertext, _ = aws_encryption_sdk.encrypt(
source=VALUES["plaintext_128"],
key_provider=self.kms_master_key_provider,
encryption_context=VALUES["encryption_context"],
frame_length=1024,
algorithm=Algorithm.AES_192_GCM_IV12_TAG16,
)
plaintext, _ = aws_encryption_sdk.decrypt(source=ciphertext, key_provider=self.kms_master_key_provider)
assert plaintext == VALUES["plaintext_128"]
def test_encryption_cycle_aes_192_gcm_iv12_tag16_non_framed(self):
"""Test that the enrypt/decrypt cycle completes successfully
for a non-framed message using the aes_192_gcm_iv12_tag16
algorithm.
"""
ciphertext, _ = aws_encryption_sdk.encrypt(
source=VALUES["plaintext_128"],
key_provider=self.kms_master_key_provider,
encryption_context=VALUES["encryption_context"],
frame_length=0,
algorithm=Algorithm.AES_192_GCM_IV12_TAG16,
)
plaintext, _ = aws_encryption_sdk.decrypt(source=ciphertext, key_provider=self.kms_master_key_provider)
assert plaintext == VALUES["plaintext_128"]
def test_encryption_cycle_aes_256_gcm_iv12_tag16_single_frame(self):
"""Test that the enrypt/decrypt cycle completes successfully
for a single frame message using the aes_256_gcm_iv12_tag16
algorithm.
"""
ciphertext, _ = aws_encryption_sdk.encrypt(
source=VALUES["plaintext_128"],
key_provider=self.kms_master_key_provider,
encryption_context=VALUES["encryption_context"],
frame_length=1024,
algorithm=Algorithm.AES_256_GCM_IV12_TAG16,
)
plaintext, _ = aws_encryption_sdk.decrypt(source=ciphertext, key_provider=self.kms_master_key_provider)
assert plaintext == VALUES["plaintext_128"]
def test_encryption_cycle_aes_256_gcm_iv12_tag16_non_framed(self):
"""Test that the enrypt/decrypt cycle completes successfully
for a non-framed message using the aes_256_gcm_iv12_tag16
algorithm.
"""
ciphertext, _ = aws_encryption_sdk.encrypt(
source=VALUES["plaintext_128"],
key_provider=self.kms_master_key_provider,
encryption_context=VALUES["encryption_context"],
frame_length=0,
algorithm=Algorithm.AES_256_GCM_IV12_TAG16,
)
plaintext, _ = aws_encryption_sdk.decrypt(source=ciphertext, key_provider=self.kms_master_key_provider)
assert plaintext == VALUES["plaintext_128"]
def test_encryption_cycle_aes_128_gcm_iv12_tag16_hkdf_sha256_single_frame(self):
"""Test that the enrypt/decrypt cycle completes successfully for a
single frame message using the aes_128_gcm_iv12_tag16_hkdf_sha256
algorithm.
"""
ciphertext, _ = aws_encryption_sdk.encrypt(
source=VALUES["plaintext_128"],
key_provider=self.kms_master_key_provider,
encryption_context=VALUES["encryption_context"],
frame_length=1024,
algorithm=Algorithm.AES_128_GCM_IV12_TAG16_HKDF_SHA256,
)
plaintext, _ = aws_encryption_sdk.decrypt(source=ciphertext, key_provider=self.kms_master_key_provider)
assert plaintext == VALUES["plaintext_128"]
def test_encryption_cycle_aes_128_gcm_iv12_tag16_hkdf_sha256_non_framed(self):
"""Test that the enrypt/decrypt cycle completes successfully for a
non-framed message using the aes_128_gcm_iv12_tag16_hkdf_sha256
algorithm.
"""
ciphertext, _ = aws_encryption_sdk.encrypt(
source=VALUES["plaintext_128"],
key_provider=self.kms_master_key_provider,
encryption_context=VALUES["encryption_context"],
frame_length=0,
algorithm=Algorithm.AES_128_GCM_IV12_TAG16_HKDF_SHA256,
)
plaintext, _ = aws_encryption_sdk.decrypt(source=ciphertext, key_provider=self.kms_master_key_provider)
assert plaintext == VALUES["plaintext_128"]
def test_encryption_cycle_aes_192_gcm_iv12_tag16_hkdf_sha256_single_frame(self):
"""Test that the enrypt/decrypt cycle completes successfully for a
single frame message using the aes_192_gcm_iv12_tag16_hkdf_sha256
algorithm.
"""
ciphertext, _ = aws_encryption_sdk.encrypt(
source=VALUES["plaintext_128"],
key_provider=self.kms_master_key_provider,
encryption_context=VALUES["encryption_context"],
frame_length=1024,
algorithm=Algorithm.AES_192_GCM_IV12_TAG16_HKDF_SHA256,
)
plaintext, _ = aws_encryption_sdk.decrypt(source=ciphertext, key_provider=self.kms_master_key_provider)
assert plaintext == VALUES["plaintext_128"]
def test_encryption_cycle_aes_192_gcm_iv12_tag16_hkdf_sha256_non_framed(self):
"""Test that the enrypt/decrypt cycle completes successfully for a
non-framed message using the aes_192_gcm_iv12_tag16_hkdf_sha256
algorithm.
"""
ciphertext, _ = aws_encryption_sdk.encrypt(
source=VALUES["plaintext_128"],
key_provider=self.kms_master_key_provider,
encryption_context=VALUES["encryption_context"],
frame_length=0,
algorithm=Algorithm.AES_192_GCM_IV12_TAG16_HKDF_SHA256,
)
plaintext, _ = aws_encryption_sdk.decrypt(source=ciphertext, key_provider=self.kms_master_key_provider)
assert plaintext == VALUES["plaintext_128"]
def test_encryption_cycle_aes_256_gcm_iv12_tag16_hkdf_sha256_single_frame(self):
"""Test that the enrypt/decrypt cycle completes successfully for a
single frame message using the aes_256_gcm_iv12_tag16_hkdf_sha256
algorithm.
"""
ciphertext, _ = aws_encryption_sdk.encrypt(
source=VALUES["plaintext_128"],
key_provider=self.kms_master_key_provider,
encryption_context=VALUES["encryption_context"],
frame_length=1024,
algorithm=Algorithm.AES_256_GCM_IV12_TAG16_HKDF_SHA256,
)
plaintext, _ = aws_encryption_sdk.decrypt(source=ciphertext, key_provider=self.kms_master_key_provider)
assert plaintext == VALUES["plaintext_128"]
def test_encryption_cycle_aes_256_gcm_iv12_tag16_hkdf_sha256_non_framed(self):
"""Test that the enrypt/decrypt cycle completes successfully for a
non-framed message using the aes_256_gcm_iv12_tag16_hkdf_sha256
algorithm.
"""
ciphertext, _ = aws_encryption_sdk.encrypt(
source=VALUES["plaintext_128"],
key_provider=self.kms_master_key_provider,
encryption_context=VALUES["encryption_context"],
frame_length=0,
algorithm=Algorithm.AES_256_GCM_IV12_TAG16_HKDF_SHA256,
)
plaintext, _ = aws_encryption_sdk.decrypt(source=ciphertext, key_provider=self.kms_master_key_provider)
assert plaintext == VALUES["plaintext_128"]
def test_encryption_cycle_aes_128_gcm_iv12_tag16_hkdf_sha256_ecdsa_p256_single_frame(self):
"""Test that the enrypt/decrypt cycle completes successfully for a single
frame message using the aes_128_gcm_iv12_tag16_hkdf_sha256_ecdsa_p256
algorithm.
"""
ciphertext, _ = aws_encryption_sdk.encrypt(
source=VALUES["plaintext_128"],
key_provider=self.kms_master_key_provider,
encryption_context=VALUES["encryption_context"],
frame_length=1024,
algorithm=Algorithm.AES_128_GCM_IV12_TAG16_HKDF_SHA256_ECDSA_P256,
)
plaintext, _ = aws_encryption_sdk.decrypt(source=ciphertext, key_provider=self.kms_master_key_provider)
assert plaintext == VALUES["plaintext_128"]
def test_encryption_cycle_aes_128_gcm_iv12_tag16_hkdf_sha256_ecdsa_p256_non_framed(self):
"""Test that the enrypt/decrypt cycle completes successfully for a single
block message using the aes_128_gcm_iv12_tag16_hkdf_sha256_ecdsa_p256
algorithm.
"""
ciphertext, _ = aws_encryption_sdk.encrypt(
source=VALUES["plaintext_128"],
key_provider=self.kms_master_key_provider,
encryption_context=VALUES["encryption_context"],
frame_length=0,
algorithm=Algorithm.AES_128_GCM_IV12_TAG16_HKDF_SHA256_ECDSA_P256,
)
plaintext, _ = aws_encryption_sdk.decrypt(source=ciphertext, key_provider=self.kms_master_key_provider)
assert plaintext == VALUES["plaintext_128"]
def test_encryption_cycle_aes_192_gcm_iv12_tag16_hkdf_sha384_ecdsa_p384_single_frame(self):
"""Test that the enrypt/decrypt cycle completes successfully for a single
frame message using the aes_192_gcm_iv12_tag16_hkdf_sha384_ecdsa_p384
algorithm.
"""
ciphertext, _ = aws_encryption_sdk.encrypt(
source=VALUES["plaintext_128"],
key_provider=self.kms_master_key_provider,
encryption_context=VALUES["encryption_context"],
frame_length=1024,
algorithm=Algorithm.AES_192_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384,
)
plaintext, _ = aws_encryption_sdk.decrypt(source=ciphertext, key_provider=self.kms_master_key_provider)
assert plaintext == VALUES["plaintext_128"]
def test_encryption_cycle_aes_192_gcm_iv12_tag16_hkdf_sha384_ecdsa_p384_non_framed(self):
"""Test that the enrypt/decrypt cycle completes successfully for a single
block message using the aes_192_gcm_iv12_tag16_hkdf_sha384_ecdsa_p384
algorithm.
"""
ciphertext, _ = aws_encryption_sdk.encrypt(
source=VALUES["plaintext_128"],
key_provider=self.kms_master_key_provider,
encryption_context=VALUES["encryption_context"],
frame_length=0,
algorithm=Algorithm.AES_192_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384,
)
plaintext, _ = aws_encryption_sdk.decrypt(source=ciphertext, key_provider=self.kms_master_key_provider)
assert plaintext == VALUES["plaintext_128"]
def test_encryption_cycle_aes_256_gcm_iv12_tag16_hkdf_sha384_ecdsa_p384_single_frame(self):
"""Test that the enrypt/decrypt cycle completes successfully for a single
frame message using the aes_256_gcm_iv12_tag16_hkdf_sha384_ecdsa_p384
algorithm.
"""
ciphertext, _ = aws_encryption_sdk.encrypt(
source=VALUES["plaintext_128"],
key_provider=self.kms_master_key_provider,
encryption_context=VALUES["encryption_context"],
frame_length=1024,
algorithm=Algorithm.AES_256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384,
)
plaintext, _ = aws_encryption_sdk.decrypt(source=ciphertext, key_provider=self.kms_master_key_provider)
assert plaintext == VALUES["plaintext_128"]
def test_encryption_cycle_aes_256_gcm_iv12_tag16_hkdf_sha384_ecdsa_p384_non_framed(self):
"""Test that the enrypt/decrypt cycle completes successfully for a single
block message using the aes_256_gcm_iv12_tag16_hkdf_sha384_ecdsa_p384
algorithm.
"""
ciphertext, _ = aws_encryption_sdk.encrypt(
source=VALUES["plaintext_128"],
key_provider=self.kms_master_key_provider,
encryption_context=VALUES["encryption_context"],
frame_length=0,
algorithm=Algorithm.AES_256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384,
)
plaintext, _ = aws_encryption_sdk.decrypt(source=ciphertext, key_provider=self.kms_master_key_provider)
assert plaintext == VALUES["plaintext_128"]