Skip to content

Commit 730b25d

Browse files
committed
tests that do not need to talk to KMS should not need the CMK environment variable to be set: some of the integ tests were looking for this in setup instead of execution, triggering failures even if the pytest markers for those tests were not requested
1 parent 607fbf1 commit 730b25d

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

test/integration/integration_test_utils.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from aws_encryption_sdk.key_providers.kms import KMSMasterKeyProvider
1717

1818
AWS_KMS_KEY_ID = 'AWS_ENCRYPTION_SDK_PYTHON_INTEGRATION_TEST_AWS_KMS_KEY_ID'
19+
_KMS_MKP = None
1920

2021

2122
def get_cmk_arn():
@@ -32,9 +33,17 @@ def get_cmk_arn():
3233
raise ValueError('KMS CMK ARN provided for integration tests much be a key not an alias')
3334

3435

35-
def setup_kms_master_key_provider():
36+
def setup_kms_master_key_provider(cache=True):
3637
"""Reads the test_values config file and builds the requested KMS Master Key Provider."""
38+
global _KMS_MKP # pylint: disable=global-statement
39+
if cache and _KMS_MKP is not None:
40+
return _KMS_MKP
41+
3742
cmk_arn = get_cmk_arn()
3843
kms_master_key_provider = KMSMasterKeyProvider()
3944
kms_master_key_provider.add_master_key(cmk_arn)
45+
46+
if cache:
47+
_KMS_MKP = kms_master_key_provider
48+
4049
return kms_master_key_provider

test/integration/test_i_thread_safety.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from six.moves import queue # six.moves confuses pylint: disable=import-error
2323

2424
import aws_encryption_sdk
25-
from .integration_test_utils import setup_kms_master_key_provider
25+
from .integration_test_utils import get_cmk_arn, setup_kms_master_key_provider
2626

2727
pytestmark = [pytest.mark.integ]
2828

@@ -49,7 +49,7 @@ def crypto_thread_worker(crypto_function, start_pause, input_value, output_queue
4949
:param cache: Cache to use with master key provider (optional)
5050
"""
5151
time.sleep(start_pause)
52-
kms_master_key_provider = setup_kms_master_key_provider()
52+
kms_master_key_provider = setup_kms_master_key_provider(cache=False)
5353
if cache is None:
5454
# For simplicity, always use a caching CMM; just use a null cache if no cache is specified.
5555
cache = aws_encryption_sdk.NullCryptoMaterialsCache()
@@ -105,6 +105,8 @@ def random_pause_time(max_seconds=3):
105105

106106
def test_threading_loop():
107107
"""Test thread safety of client."""
108+
# Check for the CMK ARN first to fail fast if it is not available
109+
get_cmk_arn()
108110
rounds = 20
109111
plaintext_inputs = [
110112
dict(input_value=copy.copy(PLAINTEXT), start_pause=random_pause_time())
@@ -130,6 +132,8 @@ def test_threading_loop():
130132

131133
def test_threading_loop_with_common_cache():
132134
"""Test thread safety of client while using common cryptographic materials cache across all threads."""
135+
# Check for the CMK ARN first to fail fast if it is not available
136+
get_cmk_arn()
133137
rounds = 20
134138
cache = aws_encryption_sdk.LocalCryptoMaterialsCache(capacity=40)
135139
plaintext_inputs = [

test/integration/test_i_xcompat_kms.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ def _file_root():
3434

3535

3636
def _generate_test_cases():
37-
kms_key_provider = setup_kms_master_key_provider()
3837
try:
3938
root_dir = os.path.abspath(file_root())
4039
except Exception: # pylint: disable=broad-except
@@ -65,22 +64,21 @@ def _generate_test_cases():
6564
if key['provider_id'] == 'aws-kms' and key['decryptable']:
6665
_test_cases.append((
6766
os.path.join(base_dir, test_case['plaintext']['filename']),
68-
os.path.join(base_dir, test_case['ciphertext']['filename']),
69-
kms_key_provider
67+
os.path.join(base_dir, test_case['ciphertext']['filename'])
7068
))
7169
break
7270
return _test_cases
7371

7472

75-
@pytest.mark.parametrize('plaintext_filename,ciphertext_filename,key_provider', _generate_test_cases())
76-
def test_decrypt_from_file(plaintext_filename, ciphertext_filename, key_provider):
73+
@pytest.mark.parametrize('plaintext_filename, ciphertext_filename', _generate_test_cases())
74+
def test_decrypt_from_file(plaintext_filename, ciphertext_filename):
7775
"""Tests decrypt from known good files."""
7876
with open(ciphertext_filename, 'rb') as infile:
7977
ciphertext = infile.read()
8078
with open(plaintext_filename, 'rb') as infile:
8179
plaintext = infile.read()
8280
decrypted_ciphertext, _header = aws_encryption_sdk.decrypt(
8381
source=ciphertext,
84-
key_provider=key_provider
82+
key_provider=setup_kms_master_key_provider()
8583
)
8684
assert decrypted_ciphertext == plaintext

0 commit comments

Comments
 (0)