Skip to content

Commit cb7e3d1

Browse files
cleanup
1 parent 5ae44f5 commit cb7e3d1

File tree

7 files changed

+27
-52
lines changed

7 files changed

+27
-52
lines changed

codebuild/coverage/coverage_mpl.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ env:
77
phases:
88
install:
99
runtime-versions:
10-
python: latest
10+
python: 3.11
1111
build:
1212
commands:
1313
- pip install "tox < 4.0"

examples/src/keyrings/hierarchical_keyring.py

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
CacheTypeDefault,
1616
CreateAwsKmsHierarchicalKeyringInput,
1717
DefaultCache,
18-
GetBranchKeyIdInput,
19-
GetBranchKeyIdOutput,
2018
)
2119
from aws_cryptographic_materialproviders.mpl.references import IBranchKeyIdSupplier, IKeyring
2220
from typing import Dict
@@ -25,6 +23,8 @@
2523
from aws_encryption_sdk import CommitmentPolicy
2624
from aws_encryption_sdk.exceptions import AWSEncryptionSDKClientError
2725

26+
from .example_branch_key_id_supplier import ExampleBranchKeyIdSupplier
27+
2828
module_root_dir = '/'.join(__file__.split("/")[:-1])
2929

3030
sys.path.append(module_root_dir)
@@ -73,39 +73,6 @@ def encrypt_and_decrypt_with_keyring(
7373
branch_key_id_A: str = keystore.create_key(input=CreateKeyInput()).branch_key_identifier
7474
branch_key_id_B: str = keystore.create_key(input=CreateKeyInput()).branch_key_identifier
7575

76-
class ExampleBranchKeyIdSupplier(IBranchKeyIdSupplier):
77-
"""Example implementation of a branch key ID supplier."""
78-
79-
branch_key_id_for_tenant_A: str
80-
branch_key_id_for_tenant_B: str
81-
82-
def __init__(self, tenant_1_id, tenant_2_id):
83-
self.branch_key_id_for_tenant_A = tenant_1_id
84-
self.branch_key_id_for_tenant_B = tenant_2_id
85-
86-
def get_branch_key_id(
87-
self,
88-
# Change this to `native_input`
89-
input: GetBranchKeyIdInput # noqa pylint: disable=redefined-builtin
90-
) -> GetBranchKeyIdOutput:
91-
"""Returns branch key ID from the tenant ID in input's encryption context."""
92-
encryption_context: Dict[str, str] = input.encryption_context
93-
94-
if b"tenant" not in encryption_context:
95-
raise ValueError("EncryptionContext invalid, does not contain expected tenant key value pair.")
96-
97-
tenant_key_id: str = encryption_context.get(b"tenant")
98-
branch_key_id: str
99-
100-
if tenant_key_id == b"TenantA":
101-
branch_key_id = self.branch_key_id_for_tenant_A
102-
elif tenant_key_id == b"TenantB":
103-
branch_key_id = self.branch_key_id_for_tenant_B
104-
else:
105-
raise ValueError(f"Item does not contain valid tenant ID: {tenant_key_id=}")
106-
107-
return GetBranchKeyIdOutput(branch_key_id=branch_key_id)
108-
10976
# 5. Create a branch key supplier that maps the branch key id to a more readable format
11077
branch_key_id_supplier: IBranchKeyIdSupplier = ExampleBranchKeyIdSupplier(
11178
tenant_1_id=branch_key_id_A,
@@ -132,8 +99,10 @@ def get_branch_key_id(
13299
input=keyring_input
133100
)
134101

135-
# The Branch Key Id supplier uses the encryption context to determine which branch key id will
136-
# be used to encrypt data.
102+
# 7. Create encryption context for both tenants.
103+
# The Branch Key Id supplier uses the encryption context to determine which branch key id will
104+
# be used to encrypt data.
105+
137106
# Create encryption context for TenantA
138107
encryption_context_A: Dict[str, str] = {
139108
"tenant": "TenantA",
@@ -154,7 +123,7 @@ def get_branch_key_id(
154123
"the data you are handling": "is what you think it is",
155124
}
156125

157-
# Encrypt the data for encryptionContextA & encryptionContextB
126+
# 8. Encrypt the data for encryptionContextA & encryptionContextB
158127
ciphertext_A, _ = client.encrypt(
159128
source=EXAMPLE_DATA,
160129
keyring=hierarchical_keyring,
@@ -166,8 +135,8 @@ def get_branch_key_id(
166135
encryption_context=encryption_context_B
167136
)
168137

169-
# To attest that TenantKeyB cannot decrypt a message written by TenantKeyA
170-
# let's construct more restrictive hierarchical keyrings.
138+
# 9. To attest that TenantKeyB cannot decrypt a message written by TenantKeyA,
139+
# let's construct more restrictive hierarchical keyrings.
171140
keyring_input_A: CreateAwsKmsHierarchicalKeyringInput = CreateAwsKmsHierarchicalKeyringInput(
172141
key_store=keystore,
173142
branch_key_id=branch_key_id_A,
@@ -198,6 +167,11 @@ def get_branch_key_id(
198167
input=keyring_input_B
199168
)
200169

170+
# 10. Demonstrate that data encrypted by one tenant's key
171+
# cannot be decrypted with by a keyring specific to another tenant.
172+
173+
# Keyring with tenant B's branch key cannot decrypt data encrypted with tenant A's branch key
174+
# This will fail and raise a AWSEncryptionSDKClientError, which we swallow ONLY for demonstration purposes.
201175
try:
202176
client.decrypt(
203177
source=ciphertext_A,
@@ -206,7 +180,8 @@ def get_branch_key_id(
206180
except AWSEncryptionSDKClientError:
207181
pass
208182

209-
# This should fail
183+
# Keyring with tenant A's branch key cannot decrypt data encrypted with tenant B's branch key.
184+
# This will fail and raise a AWSEncryptionSDKClientError, which we swallow ONLY for demonstration purposes.
210185
try:
211186
client.decrypt(
212187
source=ciphertext_B,
@@ -215,7 +190,8 @@ def get_branch_key_id(
215190
except AWSEncryptionSDKClientError:
216191
pass
217192

218-
# These should succeed
193+
# 10. Demonstrate that data encrypted by one tenant's branch key can be decrypted by that tenant,
194+
# and that the decrypted data matches the input data.
219195
plaintext_bytes_A, _ = client.decrypt(
220196
source=ciphertext_A,
221197
keyring=hierarchical_keyring_A

examples/src/keyrings/module_.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"""Should remove this."""
1+
"""Should remove this once PYTHONPATH issues are resolved by adding doo files."""

examples/src/module_.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"""Should remove this."""
1+
"""Should remove this once PYTHONPATH issues are resolved by adding doo files."""

test/unit/test_crypto_authentication_signer.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ def test_signer_from_key_bytes(patch_default_backend, patch_serialization, patch
8181
mock_algorithm_info = MagicMock(return_value=sentinel.algorithm_info, spec=patch_ec.EllipticCurve)
8282
_algorithm = MagicMock(signing_algorithm_info=mock_algorithm_info)
8383

84-
# signer = Signer.from_key_bytes(algorithm=_algorithm, key_bytes=sentinel.key_bytes)
85-
84+
# Explicitly pass in patched serialization module.
85+
# Patching the module introduces namespace issues
86+
# which causes the method's `isinstance` checks to fail
87+
# by changing the namespace from `serialization.Encoding.DER` to `Encoding.DER`.
8688
signer = Signer.from_key_bytes(
8789
algorithm=_algorithm,
8890
key_bytes=sentinel.key_bytes,

test_vector_handlers/tox.ini

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
envlist =
33
# The test vectors depend on new features now,
44
# so until release we can only effectively test the local version of the ESDK.
5-
py{37,38,39,310}-awses_local{,-mpl},
5+
py{37,38,39,310}-awses_local
66
# 1.2.0 and 1.2.max are being difficult because of attrs
77
bandit, doc8, readme,
88
{flake8,pylint}{,-tests},
@@ -48,8 +48,6 @@ passenv =
4848
sitepackages = False
4949
deps =
5050
-rtest/requirements.txt
51-
# install the MPL if in environment
52-
mpl: -r../requirements_mpl.txt
5351
..
5452
commands =
5553
{[testenv:base-command]commands}

tox.ini

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ envlist =
5959
commands = pytest --basetemp={envtmpdir} -l {posargs}
6060

6161
[testenv]
62-
; passenv = AWS_ENCRYPTION_SDK_PYTHON_INTEGRATION_TEST_AWS_KMS_KEY_ID,AWS_ENCRYPTION_SDK_PYTHON_INTEGRATION_TEST_AWS_KMS_KEY_ID_2,AWS_ENCRYPTION_SDK_PYTHON_INTEGRATION_TEST_AWS_KMS_MRK_KEY_ID_1,AWS_ENCRYPTION_SDK_PYTHON_INTEGRATION_TEST_AWS_KMS_MRK_KEY_ID_2,AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,AWS_SESSION_TOKEN,AWS_CONTAINER_CREDENTIALS_RELATIVE_URI,AWS_PROFILE,PIP_CONFIG_FILE
6362
passenv =
6463
# Identifies AWS KMS key id to use in integration tests
6564
AWS_ENCRYPTION_SDK_PYTHON_INTEGRATION_TEST_AWS_KMS_KEY_ID \
@@ -80,7 +79,7 @@ passenv =
8079
sitepackages = False
8180
deps =
8281
-rdev_requirements/test-requirements.txt
83-
# install the MPL if in environment
82+
# install the MPL requirements if the `-mpl` suffix is present
8483
mpl: -rrequirements_mpl.txt
8584
commands =
8685
local: {[testenv:base-command]commands} test/ -m local --ignore test/unit/mpl/

0 commit comments

Comments
 (0)