Skip to content

Commit 8c5bb63

Browse files
committed
refactoring and comment fixes
1 parent 349b559 commit 8c5bb63

5 files changed

+60
-57
lines changed

examples/src/custom_mpl_cmm_example.py

+32-29
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0
3-
"""Example to create a custom implementation of the ESDK-MPL ICryptographicMaterialsManager class."""
3+
"""
4+
Example to create a custom implementation of the ESDK-MPL ICryptographicMaterialsManager class.
5+
6+
Cryptographic Materials Managers (CMMs) are composable; if you just want to extend the behavior of
7+
the default CMM, you can do this as demonstrated in this example. This is easy if you just want
8+
to add a small check to the CMM methods.
9+
10+
Custom implementation of CMMs must implement get_encryption_materials and decrypt_materials.
11+
If your use case calls for fundamentally change aspects of the default CMM, you can also write
12+
your own implementation without extending a CMM.
13+
14+
For more information on a default implementation of a CMM,
15+
please look at the default_cryptographic_materials_manager_example.py example.
16+
"""
417

518
from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders
619
from aws_cryptographic_materialproviders.mpl.config import MaterialProvidersConfig
@@ -21,21 +34,25 @@
2134
class MPLCustomSigningSuiteOnlyCMM(ICryptographicMaterialsManager):
2235
"""Example custom crypto materials manager class."""
2336

24-
def __init__(self, keyring: IKeyring) -> None:
37+
def __init__(self, keyring: IKeyring, cmm: ICryptographicMaterialsManager = None) -> None:
2538
"""Constructor for MPLCustomSigningSuiteOnlyCMM class."""
26-
mat_prov: AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders(
27-
config=MaterialProvidersConfig()
28-
)
29-
30-
# Create a CryptographicMaterialsManager for encryption and decryption
31-
cmm_input: CreateDefaultCryptographicMaterialsManagerInput = \
32-
CreateDefaultCryptographicMaterialsManagerInput(
33-
keyring=keyring
39+
if cmm is not None:
40+
self.underlying_cmm = cmm
41+
else:
42+
mat_prov: AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders(
43+
config=MaterialProvidersConfig()
3444
)
3545

36-
self.underlying_cmm: ICryptographicMaterialsManager = mat_prov.create_default_cryptographic_materials_manager(
37-
input=cmm_input
38-
)
46+
# Create a CryptographicMaterialsManager for encryption and decryption
47+
cmm_input: CreateDefaultCryptographicMaterialsManagerInput = \
48+
CreateDefaultCryptographicMaterialsManagerInput(
49+
keyring=keyring
50+
)
51+
52+
self.underlying_cmm: ICryptographicMaterialsManager = \
53+
mat_prov.create_default_cryptographic_materials_manager(
54+
input=cmm_input
55+
)
3956

4057
def get_encryption_materials(self, param):
4158
"""Provides encryption materials appropriate for the request for the custom CMM.
@@ -85,30 +102,16 @@ def encrypt_decrypt_with_cmm(
85102
client = aws_encryption_sdk.EncryptionSDKClient(commitment_policy=CommitmentPolicy.REQUIRE_ENCRYPT_REQUIRE_DECRYPT)
86103

87104
# Encrypt the plaintext source data
88-
ciphertext, encryptor_header = client.encrypt(
105+
ciphertext, _ = client.encrypt(
89106
source=EXAMPLE_DATA,
90107
materials_manager=cmm
91108
)
92109

93110
# Decrypt the ciphertext
94-
cycled_plaintext, decrypted_header = client.decrypt(
111+
cycled_plaintext, _ = client.decrypt(
95112
source=ciphertext,
96113
materials_manager=cmm
97114
)
98115

99116
# Verify that the "cycled" (encrypted, then decrypted) plaintext is identical to the source plaintext
100117
assert cycled_plaintext == EXAMPLE_DATA
101-
102-
# Verify that the encryption context used in the decrypt operation includes all key pairs from
103-
# the encrypt operation. (The SDK can add pairs, so don't require an exact match.)
104-
#
105-
# In production, always use a meaningful encryption context. In this sample, we omit the
106-
# encryption context (no key pairs).
107-
# The encryptor_header.encryption_context has items of the form
108-
# b'key': b'value'
109-
# We convert these to strings for easier comparison with the decrypted header below.
110-
for k, v in encryptor_header.encryption_context.items():
111-
k = str(k.decode("utf-8"))
112-
v = str(v.decode("utf-8"))
113-
assert v == decrypted_header.encryption_context[k], \
114-
"Encryption context does not match expected values"

examples/src/aws_cryptographic_materials_manager_example.py renamed to examples/src/default_cryptographic_materials_manager_example.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0
33
"""
4-
This example sets up the AWS Cryptographic Material Managers (CMM).
4+
This example sets up the default Cryptographic Material Managers (CMM).
55
6-
The AWS cryptographic materials manager (CMM) assembles the cryptographic materials
6+
The default cryptographic materials manager (CMM) assembles the cryptographic materials
77
that are used to encrypt and decrypt data. The cryptographic materials include
88
plaintext and encrypted data keys, and an optional message signing key.
99
This example creates a CMM and then encrypts a custom input EXAMPLE_DATA
@@ -15,7 +15,7 @@
1515
3. Decrypted plaintext value matches EXAMPLE_DATA
1616
These sanity checks are for demonstration in the example only. You do not need these in your code.
1717
18-
For more information on AWS Cryptographic Material Managers, see
18+
For more information on Cryptographic Material Managers, see
1919
https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#crypt-materials-manager
2020
"""
2121
import sys
@@ -41,12 +41,12 @@
4141
EXAMPLE_DATA: bytes = b"Hello World"
4242

4343

44-
def encrypt_and_decrypt_with_cmm(
44+
def encrypt_and_decrypt_with_default_cmm(
4545
kms_key_id: str
4646
):
47-
"""Demonstrate an encrypt/decrypt cycle using an AWS Cryptographic Material Managers.
47+
"""Demonstrate an encrypt/decrypt cycle using default Cryptographic Material Managers.
4848
49-
Usage: encrypt_and_decrypt_with_cmm(kms_key_id)
49+
Usage: encrypt_and_decrypt_with_default_cmm(kms_key_id)
5050
:param kms_key_id: KMS Key identifier for the KMS key you want to use for encryption and
5151
decryption of your data keys.
5252
:type kms_key_id: string
@@ -77,7 +77,7 @@ def encrypt_and_decrypt_with_cmm(
7777
"the data you are handling": "is what you think it is",
7878
}
7979

80-
# 4. Create a KMS keyring to use with the CryptographicMaterialsManager
80+
# 3. Create a KMS keyring to use with the CryptographicMaterialsManager
8181
kms_client = boto3.client('kms', region_name="us-west-2")
8282

8383
mat_prov: AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders(
@@ -93,7 +93,7 @@ def encrypt_and_decrypt_with_cmm(
9393
input=keyring_input
9494
)
9595

96-
# 5. Create a CryptographicMaterialsManager for encryption and decryption
96+
# 4. Create a CryptographicMaterialsManager for encryption and decryption
9797
cmm_input: CreateDefaultCryptographicMaterialsManagerInput = \
9898
CreateDefaultCryptographicMaterialsManagerInput(
9999
keyring=kms_keyring
@@ -103,31 +103,31 @@ def encrypt_and_decrypt_with_cmm(
103103
input=cmm_input
104104
)
105105

106-
# 6. Encrypt the data with the encryptionContext.
106+
# 5. Encrypt the data with the encryptionContext.
107107
ciphertext, _ = client.encrypt(
108108
source=EXAMPLE_DATA,
109109
materials_manager=cmm,
110110
encryption_context=encryption_context
111111
)
112112

113-
# 7. Demonstrate that the ciphertext and plaintext are different.
113+
# 6. Demonstrate that the ciphertext and plaintext are different.
114114
# (This is an example for demonstration; you do not need to do this in your own code.)
115115
assert ciphertext != EXAMPLE_DATA, \
116116
"Ciphertext and plaintext data are the same. Invalid encryption"
117117

118-
# 8. Decrypt your encrypted data using the same cmm you used on encrypt.
118+
# 7. Decrypt your encrypted data using the same cmm you used on encrypt.
119119
plaintext_bytes, dec_header = client.decrypt(
120120
source=ciphertext,
121121
materials_manager=cmm
122122
)
123123

124-
# 9. Demonstrate that the encryption context is correct in the decrypted message header
124+
# 8. Demonstrate that the encryption context is correct in the decrypted message header
125125
# (This is an example for demonstration; you do not need to do this in your own code.)
126126
for k, v in encryption_context.items():
127127
assert v == dec_header.encryption_context[k], \
128128
"Encryption context does not match expected values"
129129

130-
# 10. Demonstrate that the decrypted plaintext is identical to the original plaintext.
130+
# 9. Demonstrate that the decrypted plaintext is identical to the original plaintext.
131131
# (This is an example for demonstration; you do not need to do this in your own code.)
132132
assert plaintext_bytes == EXAMPLE_DATA, \
133133
"Decrypted plaintext should be identical to the original plaintext. Invalid decryption"

examples/test/legacy/v3_default_cmm.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0
3-
"""Copy-paste of the V3 default CMM with small changes to pass linters.."""
3+
"""Copy-paste of the V3 default CMM with small changes to pass linters."""
44
import logging
55

66
import attr

examples/test/test_i_aws_cryptographic_materials_manager_example.py

-14
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""Test suite for the default Cryptographic Materials Manager example."""
4+
import pytest
5+
6+
from ..src.default_cryptographic_materials_manager_example import encrypt_and_decrypt_with_default_cmm
7+
8+
pytestmark = [pytest.mark.examples]
9+
10+
11+
def test_encrypt_and_decrypt_with_default_cmm():
12+
"""Test function for encrypt and decrypt using the default Cryptographic Materials Manager example."""
13+
kms_key_id = "arn:aws:kms:us-west-2:658956600833:key/b3537ef1-d8dc-4780-9f5a-55776cbb2f7f"
14+
encrypt_and_decrypt_with_default_cmm(kms_key_id)

0 commit comments

Comments
 (0)