-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathintegration_test_utils.py
70 lines (61 loc) · 2.69 KB
/
integration_test_utils.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
# 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.
"""Utility functions to handle configuration and credentials setup for integration tests."""
import os
from aws_encryption_sdk.key_providers.kms import KMSMasterKeyProvider
AWS_KMS_KEY_ID = "AWS_ENCRYPTION_SDK_PYTHON_INTEGRATION_TEST_AWS_KMS_KEY_ID"
AWS_KMS_KEY_ID_2 = "AWS_ENCRYPTION_SDK_PYTHON_INTEGRATION_TEST_AWS_KMS_KEY_ID_2"
_KMS_MKP = None
def get_cmk_arn():
"""Retrieves the target CMK ARN from environment variable."""
arn = os.environ.get(AWS_KMS_KEY_ID, None)
if arn is None:
raise ValueError(
'Environment variable "{}" must be set to a valid KMS CMK ARN for integration tests to run'.format(
AWS_KMS_KEY_ID
)
)
if arn.startswith("arn:") and ":alias/" not in arn:
return arn
raise ValueError("KMS CMK ARN provided for integration tests much be a key not an alias")
def get_cmk_arn(region_name):
"""Retrieves a CMK ARN based on the requested region_name"""
if AWS_KMS_KEY_ID in os.environ and AWS_KMS_KEY_ID_2 in os.environ:
raise ValueError(
'Environment variable "{}" or "{}" must be set to a valid KMS CMK ARN for integration tests to run'.format(
AWS_KMS_KEY_ID, AWS_KMS_KEY_ID_2
)
)
arn_1 = os.environ.get(AWS_KMS_KEY_ID, None)
arn_2 = os.environ.get(AWS_KMS_KEY_ID_2, None)
if arn_1.split(':')[3] == region_name:
return arn_1
elif arn_2.split(':')[3] == region_name:
return arn_2
else:
raise ValueError(
'No CMK in the region {} exist in either of your environment variables "{}" or "{}"'.format(
region_name, AWS_KMS_KEY_ID, AWS_KMS_KEY_ID_2
)
)
def setup_kms_master_key_provider(cache=True):
"""Reads the test_values config file and builds the requested KMS Master Key Provider."""
global _KMS_MKP # pylint: disable=global-statement
if cache and _KMS_MKP is not None:
return _KMS_MKP
cmk_arn = get_cmk_arn()
kms_master_key_provider = KMSMasterKeyProvider()
kms_master_key_provider.add_master_key(cmk_arn)
if cache:
_KMS_MKP = kms_master_key_provider
return kms_master_key_provider