Skip to content

Commit 7512802

Browse files
committed
minor fix
1 parent bedf3cd commit 7512802

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

examples/src/aws_cryptographic_materials_manager_example.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
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
10-
with an encryption context. This example also includes some sanity checks for demonstration:
10+
with an encryption context. Creating a CMM involves taking a keyring as input,
11+
and we use an AWS KMS Keyring for this example.
12+
This example also includes some sanity checks for demonstration:
1113
1. Ciphertext and plaintext data are not the same
1214
2. Encryption context is correct in the decrypted message header
1315
3. Decrypted plaintext value matches EXAMPLE_DATA
@@ -42,9 +44,9 @@
4244
def encrypt_and_decrypt_with_cmm(
4345
kms_key_id: str
4446
):
45-
"""Demonstrate an encrypt/decrypt cycle using an AWS KMS keyring.
47+
"""Demonstrate an encrypt/decrypt cycle using an AWS Cryptographic Material Managers.
4648
47-
Usage: encrypt_and_decrypt_with_keyring(kms_key_id)
49+
Usage: encrypt_and_decrypt_with_cmm(kms_key_id)
4850
:param kms_key_id: KMS Key identifier for the KMS key you want to use for encryption and
4951
decryption of your data keys.
5052
:type kms_key_id: string
@@ -63,10 +65,7 @@ def encrypt_and_decrypt_with_cmm(
6365
commitment_policy=CommitmentPolicy.REQUIRE_ENCRYPT_REQUIRE_DECRYPT
6466
)
6567

66-
# 2. Create a boto3 client for KMS.
67-
kms_client = boto3.client('kms', region_name="us-west-2")
68-
69-
# 3. Create encryption context.
68+
# 2. Create encryption context.
7069
# Remember that your encryption context is NOT SECRET.
7170
# For more information, see
7271
# https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
@@ -79,6 +78,8 @@ def encrypt_and_decrypt_with_cmm(
7978
}
8079

8180
# 4. Create a KMS keyring to use with the CryptographicMaterialsManager
81+
kms_client = boto3.client('kms', region_name="us-west-2")
82+
8283
mat_prov: AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders(
8384
config=MaterialProvidersConfig()
8485
)
@@ -102,31 +103,31 @@ def encrypt_and_decrypt_with_cmm(
102103
input=cmm_input
103104
)
104105

105-
# 5. Encrypt the data with the encryptionContext.
106+
# 6. Encrypt the data with the encryptionContext.
106107
ciphertext, _ = client.encrypt(
107108
source=EXAMPLE_DATA,
108109
materials_manager=cmm,
109110
encryption_context=encryption_context
110111
)
111112

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

117-
# 7. Decrypt your encrypted data using the same keyring you used on encrypt.
118+
# 8. Decrypt your encrypted data using the same cmm you used on encrypt.
118119
plaintext_bytes, dec_header = client.decrypt(
119120
source=ciphertext,
120121
materials_manager=cmm
121122
)
122123

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

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

0 commit comments

Comments
 (0)