Skip to content

Commit 18328ad

Browse files
committed
chore: KMS keyring example
1 parent 0dfeb5d commit 18328ad

File tree

3 files changed

+63
-56
lines changed

3 files changed

+63
-56
lines changed

examples/src/keyrings/aws_kms_keyring_example.py

+56-52
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,73 @@
22
# SPDX-License-Identifier: Apache-2.0
33
"""
44
This example sets up the KMS Keyring
5+
6+
The AWS KMS keyring uses symmetric encryption KMS keys to generate, encrypt and
7+
decrypt data keys. This example creates a KMS Keyring and then encrypts a custom input EXAMPLE_DATA
8+
with an encryption context. This example also demonstrates some sanity checks for demonstration.
9+
1. Ciphertext and plaintext data are not the same
10+
2. Encryption context is correct in the decrypted message header
11+
3. Decrypted plaintext value matches EXAMPLE_DATA
12+
13+
AWS KMS keyrings can be used independently or in a multi-keyring with other keyrings
14+
of the same or a different type.
15+
16+
For more info on how to use KMS keyring, see
17+
https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-kms-keyring.html
518
"""
619
import sys
7-
20+
from typing import Dict
821
import boto3
922

1023
from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders
1124
from aws_cryptographic_materialproviders.mpl.config import MaterialProvidersConfig
1225
from aws_cryptographic_materialproviders.mpl.models import CreateAwsKmsKeyringInput
1326
from aws_cryptographic_materialproviders.mpl.references import IKeyring
14-
from typing import Dict
1527

1628
import aws_encryption_sdk
1729
from aws_encryption_sdk import CommitmentPolicy
1830

1931

2032
# TODO-MPL: Remove this as part of removing PYTHONPATH hacks.
21-
module_root_dir = '/'.join(__file__.split("/")[:-1])
33+
MODULE_ROOT_DIR = '/'.join(__file__.split("/")[:-1])
2234

23-
sys.path.append(module_root_dir)
35+
sys.path.append(MODULE_ROOT_DIR)
2436

2537
EXAMPLE_DATA: bytes = b"Hello World"
2638

39+
2740
def encrypt_and_decrypt_with_keyring(
2841
kms_key_id: str
2942
):
30-
"""Demonstrate an encrypt/decrypt cycle using an AWS KMS keyring."""
43+
"""Demonstrate an encrypt/decrypt cycle using an AWS KMS keyring.
3144
45+
Usage: encrypt_and_decrypt_with_keyring(kms_key_id)
46+
:param kms_key_id: KMS Key identifier for the KMS key you want to use for encryption and
47+
decryption of your data keys.
48+
:type kms_key_id: string
49+
50+
For more info on KMS Key identifiers, see
51+
https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#key-id
3252
"""
33-
1. Instantiate the encryption SDK client.
34-
This builds the client with the REQUIRE_ENCRYPT_REQUIRE_DECRYPT commitment policy,
35-
which enforces that this client only encrypts using committing algorithm suites and enforces
36-
that this client will only decrypt encrypted messages that were created with a committing
37-
algorithm suite.
38-
This is the default commitment policy if you were to build the client as
39-
`client = aws_encryption_sdk.EncryptionSDKClient()`.
40-
"""
53+
54+
# 1. Instantiate the encryption SDK client.
55+
# This builds the client with the REQUIRE_ENCRYPT_REQUIRE_DECRYPT commitment policy,
56+
# which enforces that this client only encrypts using committing algorithm suites and enforces
57+
# that this client will only decrypt encrypted messages that were created with a committing
58+
# algorithm suite.
59+
# This is the default commitment policy if you were to build the client as
60+
# `client = aws_encryption_sdk.EncryptionSDKClient()`.
4161
client = aws_encryption_sdk.EncryptionSDKClient(
4262
commitment_policy=CommitmentPolicy.REQUIRE_ENCRYPT_REQUIRE_DECRYPT
4363
)
4464

45-
"""
46-
2. Create boto3 clients for KMS.
47-
"""
65+
# 2. Create a boto3 client for KMS.
4866
kms_client = boto3.client('kms', region_name="us-west-2")
4967

50-
"""
51-
3. Instantiate the Material Providers
52-
"""
53-
mat_prov: AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders(
54-
config=MaterialProvidersConfig()
55-
)
56-
57-
"""
58-
4. Create encryption context
59-
Remember that your encryption context is NOT SECRET.
60-
https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
61-
"""
68+
# 3. Create encryption context.
69+
# Remember that your encryption context is NOT SECRET.
70+
# For more information, see
71+
# https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
6272
encryption_context: Dict[str, str] = {
6373
"encryption": "context",
6474
"is not": "secret",
@@ -67,9 +77,11 @@ def encrypt_and_decrypt_with_keyring(
6777
"the data you are handling": "is what you think it is",
6878
}
6979

70-
"""
71-
5. Create the KMS keyring
72-
"""
80+
# 4. Create a KMS keyring
81+
mat_prov: AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders(
82+
config=MaterialProvidersConfig()
83+
)
84+
7385
keyring_input: CreateAwsKmsKeyringInput = CreateAwsKmsKeyringInput(
7486
kms_key_id=kms_key_id,
7587
kms_client=kms_client
@@ -79,37 +91,29 @@ def encrypt_and_decrypt_with_keyring(
7991
input=keyring_input
8092
)
8193

82-
"""
83-
6. Encrypt the data for the encryptionContext
84-
"""
94+
# 5. Encrypt the data for the encryptionContext
8595
ciphertext, _ = client.encrypt(
8696
source=EXAMPLE_DATA,
8797
keyring=kms_keyring,
8898
encryption_context=encryption_context
8999
)
90100

91-
"""
92-
7. Demonstrate that the ciphertext and plaintext are different.
93-
"""
94-
assert ciphertext != EXAMPLE_DATA, "Ciphertext and plaintext data are the same. Invalid encryption"
95-
96-
"""
97-
8. Decrypt your encrypted data using the same keyring you used on encrypt.
98-
You do not need to specify the encryption context on decrypt
99-
because the header of the encrypted message includes the encryption context.
100-
"""
101+
# 6. Demonstrate that the ciphertext and plaintext are different.
102+
# (This is an example for demonstration; you do not need to do this in your own code.)
103+
assert ciphertext != EXAMPLE_DATA, \
104+
"Ciphertext and plaintext data are the same. Invalid encryption"
105+
106+
# 7. Decrypt your encrypted data using the same keyring you used on encrypt.
101107
plaintext_bytes, dec_header = client.decrypt(
102108
source=ciphertext,
103109
keyring=kms_keyring
104110
)
105111

106-
"""
107-
9. Demonstrate that the encryption context is correct in the decrypted message header
108-
"""
112+
# 8. Demonstrate that the encryption context is correct in the decrypted message header
113+
# (This is an example for demonstration; you do not need to do this in your own code.)
109114
for k, v in encryption_context.items():
110-
assert v == dec_header.encryption_context[k], "Encryption context does not match expected values"
115+
assert v == dec_header.encryption_context[k], \
116+
"Encryption context does not match expected values"
111117

112-
"""
113-
10. Demonstrate that the decrypted plaintext is identical to the original plaintext.
114-
"""
115-
assert plaintext_bytes == EXAMPLE_DATA
118+
# 9. Demonstrate that the decrypted plaintext is identical to the original plaintext.
119+
assert plaintext_bytes == EXAMPLE_DATA

examples/test/keyrings/test_i_aws_kms_keyring_example.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@
99

1010

1111
def test_encrypt_and_decrypt_with_keyring():
12-
key_arn = "arn:aws:kms:us-west-2:658956600833:key/b3537ef1-d8dc-4780-9f5a-55776cbb2f7f"
13-
encrypt_and_decrypt_with_keyring(key_arn)
12+
"""Test function for encryption and decryption using the AWS KMS Keyring example"""
13+
14+
kms_key_id = "arn:aws:kms:us-west-2:658956600833:key/b3537ef1-d8dc-4780-9f5a-55776cbb2f7f"
15+
encrypt_and_decrypt_with_keyring(kms_key_id)

examples/test/keyrings/test_i_hierarchical_keyring.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010

1111
def test_encrypt_and_decrypt_with_keyring():
12+
"""Test function for encryption and decryption using the AWS KMS Hierarchical Keyring example"""
1213
key_store_table_name = "KeyStoreDdbTable"
13-
key_arn = "arn:aws:kms:us-west-2:370957321024:key/9d989aa2-2f9c-438c-a745-cc57d3ad0126"
14-
encrypt_and_decrypt_with_keyring(key_store_table_name, key_store_table_name, key_arn)
14+
kms_key_id = "arn:aws:kms:us-west-2:370957321024:key/9d989aa2-2f9c-438c-a745-cc57d3ad0126"
15+
encrypt_and_decrypt_with_keyring(key_store_table_name, key_store_table_name, kms_key_id)

0 commit comments

Comments
 (0)