26
26
with an encryption context. This example also includes some sanity checks for demonstration:
27
27
1. Ciphertext and plaintext data are not the same
28
28
2. Decryption of ciphertext is possible using the multi_keyring,
29
- and every one of the keyrings from the multi_keyring separately
29
+ and every one of the keyrings from the multi_keyring separately
30
30
3. All decrypted plaintext value match EXAMPLE_DATA
31
31
These sanity checks are for demonstration in the example only. You do not need these in your code.
32
32
56
56
EXAMPLE_DATA : bytes = b"Hello World"
57
57
58
58
59
- def get_aws_region_from_kms_key_id (kms_key_id : str ) -> str :
60
- """
61
- Get the AWS Region from the KMS Key ID.
62
-
63
- Usage: get_aws_region_from_kms_key_id(kms_key_id)
64
- :param kms_key_id: KMS Key identifier for the KMS key you want to use
65
- :type kms_key_id: string
66
- :return: AWS Region
67
- :rtype: string
68
- """
69
- return kms_key_id .split (":" )[3 ]
70
-
71
-
72
59
def encrypt_and_decrypt_with_keyring (
73
60
default_region_kms_key_id : str ,
74
- second_region_kms_key_id : str
61
+ second_region_kms_key_id : str ,
62
+ default_region : str ,
63
+ second_region : str
75
64
):
76
65
"""Demonstrate an encrypt/decrypt cycle using an AWS KMS Multi keyring.
77
66
The multi_keyring is created using a KMS keyring as generator keyring and another KMS keyring
78
67
as a child keyring. For this example, `default_region_kms_key_id` is the generator key id
79
68
for a KMS key located in your default region, and `second_region_kms_key_id` is the KMS key id
80
- for a KMS Key located in some second Region .
69
+ for a KMS Key located in some second region .
81
70
82
- Usage: encrypt_and_decrypt_with_keyring(default_region_kms_key_id, second_region_kms_key_id)
71
+ Usage: encrypt_and_decrypt_with_keyring(default_region_kms_key_id,
72
+ second_region_kms_key_id,
73
+ default_region,
74
+ second_region)
83
75
:param default_region_kms_key_id: KMS Key identifier for the default region KMS key you want to
84
76
use as a generator keyring
85
77
:type default_region_kms_key_id: string
86
78
:param second_region_kms_key_id: KMS Key identifier for the second region KMS key you want to
87
79
use as a child keyring
88
80
:type second_region_kms_key_id: string
81
+ :param default_region: AWS Region for the default region KMS key
82
+ :type default_region: string
83
+ :param second_region: AWS Region for the second region KMS key
84
+ :type second_region: string
89
85
90
86
For more information on KMS Key identifiers, see
91
87
https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#key-id
@@ -155,9 +151,9 @@ def encrypt_and_decrypt_with_keyring(
155
151
156
152
# 7. Demonstrate that you can successfully decrypt data using a KMS keyring with just the
157
153
# `default_region_kms_key_id` directly.
154
+ # (This is an example for demonstration; you do not need to do this in your own code.)
158
155
159
156
# 7a. Create a boto3 client for KMS for the default region.
160
- default_region = get_aws_region_from_kms_key_id (default_region_kms_key_id )
161
157
default_region_kms_client = boto3 .client ('kms' , region_name = default_region )
162
158
163
159
# 7b. Create KMS keyring
@@ -182,9 +178,9 @@ def encrypt_and_decrypt_with_keyring(
182
178
183
179
# 8. Demonstrate that you can also successfully decrypt data using a KMS keyring with just the
184
180
# `second_region_kms_key_id` directly.
181
+ # (This is an example for demonstration; you do not need to do this in your own code.)
185
182
186
183
# 8a. Create a boto3 client for KMS for the second region.
187
- second_region = get_aws_region_from_kms_key_id (second_region_kms_key_id )
188
184
second_region_kms_client = boto3 .client ('kms' , region_name = second_region )
189
185
190
186
# 8b. Create KMS keyring
0 commit comments