Skip to content

Commit 41e40f3

Browse files
committed
fix
1 parent cbf72ff commit 41e40f3

8 files changed

+43
-95
lines changed

examples/src/keyrings/aws_kms_discovery_keyring_example.py

+9-36
Original file line numberDiff line numberDiff line change
@@ -57,41 +57,21 @@
5757
EXAMPLE_DATA: bytes = b"Hello World"
5858

5959

60-
def get_account_id_from_kms_key_id(kms_key_id: str) -> str:
61-
"""
62-
Get the AWS Account ID from the KMS Key ID.
63-
64-
Usage: get_account_id_from_kms_key_id(kms_key_id)
65-
:param kms_key_id: KMS Key identifier for the KMS key you want to use
66-
:type kms_key_id: string
67-
:return: AWS Account ID
68-
:rtype: string
69-
"""
70-
return kms_key_id.split(":")[4]
71-
72-
73-
def get_aws_region_from_kms_key_id(kms_key_id: str) -> str:
74-
"""
75-
Get the AWS Region from the KMS Key ID.
76-
77-
Usage: get_aws_region_from_kms_key_id(kms_key_id)
78-
:param kms_key_id: KMS Key identifier for the KMS key you want to use
79-
:type kms_key_id: string
80-
:return: AWS Region
81-
:rtype: string
82-
"""
83-
return kms_key_id.split(":")[3]
84-
85-
8660
def encrypt_and_decrypt_with_keyring(
87-
kms_key_id: str
61+
kms_key_id: str,
62+
aws_account_id: str,
63+
aws_region: str
8864
):
8965
"""Demonstrate an encrypt/decrypt cycle using an AWS KMS Discovery Keyring.
9066
91-
Usage: encrypt_and_decrypt_with_keyring(kms_key_id)
67+
Usage: encrypt_and_decrypt_with_keyring(kms_key_id, aws_account_id)
9268
:param kms_key_id: KMS Key identifier for the KMS key you want to use for creating
9369
the kms_keyring used for encryption
9470
:type kms_key_id: string
71+
:param aws_account_id: AWS Account ID to use in the discovery filter
72+
:type aws_account_id: string
73+
:param aws_region: AWS Region to use for the kms client
74+
:type aws_region: string
9575
9676
For more information on KMS Key identifiers, see
9777
https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#key-id
@@ -108,11 +88,7 @@ def encrypt_and_decrypt_with_keyring(
10888
)
10989

11090
# 2. Create a boto3 client for KMS.
111-
112-
# Get the AWS Region from the KMS Key ID.
113-
region_name = get_aws_region_from_kms_key_id(kms_key_id)
114-
115-
kms_client = boto3.client('kms', region_name=region_name)
91+
kms_client = boto3.client('kms', region_name=aws_region)
11692

11793
# 3. Create encryption context.
11894
# Remember that your encryption context is NOT SECRET.
@@ -158,9 +134,6 @@ def encrypt_and_decrypt_with_keyring(
158134
# so that we limit the set of ciphertexts we are willing to decrypt to only ones
159135
# created by KMS keys in our account and partition.
160136

161-
# Get the AWS Account ID from the KMS Key ID to use in the discovery filter
162-
aws_account_id: str = get_account_id_from_kms_key_id(kms_key_id=kms_key_id)
163-
164137
discovery_keyring_input: CreateAwsKmsDiscoveryKeyringInput = CreateAwsKmsDiscoveryKeyringInput(
165138
kms_client=kms_client,
166139
discovery_filter=DiscoveryFilter(

examples/src/keyrings/aws_kms_discovery_multi_keyring_example.py

+5-36
Original file line numberDiff line numberDiff line change
@@ -53,42 +53,19 @@
5353
EXAMPLE_DATA: bytes = b"Hello World"
5454

5555

56-
def get_account_id_from_kms_key_id(kms_key_id: str) -> str:
57-
"""
58-
Get the AWS Account ID from the KMS Key ID.
59-
60-
Usage: get_account_id_from_kms_key_id(kms_key_id)
61-
:param kms_key_id: KMS Key identifier for the KMS key you want to use
62-
:type kms_key_id: string
63-
:return: AWS Account ID
64-
:rtype: string
65-
"""
66-
return kms_key_id.split(":")[4]
67-
68-
69-
def get_aws_region_from_kms_key_id(kms_key_id: str) -> str:
70-
"""
71-
Get the AWS Region from the KMS Key ID.
72-
73-
Usage: get_aws_region_from_kms_key_id(kms_key_id)
74-
:param kms_key_id: KMS Key identifier for the KMS key you want to use
75-
:type kms_key_id: string
76-
:return: AWS Region
77-
:rtype: string
78-
"""
79-
return kms_key_id.split(":")[3]
80-
81-
8256
def encrypt_and_decrypt_with_keyring(
8357
kms_key_id: str,
58+
aws_account_id: str,
8459
aws_regions: list[str]
8560
):
8661
"""Demonstrate an encrypt/decrypt cycle using an AWS KMS Discovery Multi Keyring.
8762
88-
Usage: encrypt_and_decrypt_with_keyring(kms_key_id, aws_regions)
63+
Usage: encrypt_and_decrypt_with_keyring(kms_key_id, aws_account_id, aws_regions)
8964
:param kms_key_id: KMS Key identifier for the KMS key you want to use for creating
9065
the kms_keyring used for encryption
9166
:type kms_key_id: string
67+
:param aws_account_id: AWS Account ID to use in the discovery filter
68+
:type aws_account_id: string
9269
:param aws_regions: List of AWS Regions to use for creating the discovery multi keyring
9370
:type aws_regions: list[string]
9471
@@ -107,11 +84,7 @@ def encrypt_and_decrypt_with_keyring(
10784
)
10885

10986
# 2. Create a boto3 client for KMS.
110-
111-
# Get the AWS Region from the KMS Key ID.
112-
region_name = get_aws_region_from_kms_key_id(kms_key_id)
113-
114-
kms_client = boto3.client('kms', region_name=region_name)
87+
kms_client = boto3.client('kms', region_name="us-west-2")
11588

11689
# 3. Create encryption context.
11790
# Remember that your encryption context is NOT SECRET.
@@ -156,10 +129,6 @@ def encrypt_and_decrypt_with_keyring(
156129
# 7. Now create a Discovery Multi keyring to use for decryption. We'll add a discovery filter
157130
# so that we limit the set of ciphertexts we are willing to decrypt to only ones
158131
# created by KMS keys in our account and partition.
159-
160-
# Get the AWS Account ID from the KMS Key ID to use in the discovery filter
161-
aws_account_id: str = get_account_id_from_kms_key_id(kms_key_id=kms_key_id)
162-
163132
discovery_multi_keyring_input: CreateAwsKmsDiscoveryMultiKeyringInput = \
164133
CreateAwsKmsDiscoveryMultiKeyringInput(
165134
regions=aws_regions,

examples/src/keyrings/aws_kms_multi_keyring_example.py

+15-19
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
with an encryption context. This example also includes some sanity checks for demonstration:
2727
1. Ciphertext and plaintext data are not the same
2828
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
3030
3. All decrypted plaintext value match EXAMPLE_DATA
3131
These sanity checks are for demonstration in the example only. You do not need these in your code.
3232
@@ -56,36 +56,32 @@
5656
EXAMPLE_DATA: bytes = b"Hello World"
5757

5858

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-
7259
def encrypt_and_decrypt_with_keyring(
7360
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
7564
):
7665
"""Demonstrate an encrypt/decrypt cycle using an AWS KMS Multi keyring.
7766
The multi_keyring is created using a KMS keyring as generator keyring and another KMS keyring
7867
as a child keyring. For this example, `default_region_kms_key_id` is the generator key id
7968
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.
8170
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)
8375
:param default_region_kms_key_id: KMS Key identifier for the default region KMS key you want to
8476
use as a generator keyring
8577
:type default_region_kms_key_id: string
8678
:param second_region_kms_key_id: KMS Key identifier for the second region KMS key you want to
8779
use as a child keyring
8880
: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
8985
9086
For more information on KMS Key identifiers, see
9187
https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#key-id
@@ -155,9 +151,9 @@ def encrypt_and_decrypt_with_keyring(
155151

156152
# 7. Demonstrate that you can successfully decrypt data using a KMS keyring with just the
157153
# `default_region_kms_key_id` directly.
154+
# (This is an example for demonstration; you do not need to do this in your own code.)
158155

159156
# 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)
161157
default_region_kms_client = boto3.client('kms', region_name=default_region)
162158

163159
# 7b. Create KMS keyring
@@ -182,9 +178,9 @@ def encrypt_and_decrypt_with_keyring(
182178

183179
# 8. Demonstrate that you can also successfully decrypt data using a KMS keyring with just the
184180
# `second_region_kms_key_id` directly.
181+
# (This is an example for demonstration; you do not need to do this in your own code.)
185182

186183
# 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)
188184
second_region_kms_client = boto3.client('kms', region_name=second_region)
189185

190186
# 8b. Create KMS keyring

examples/src/keyrings/aws_kms_rsa_keyring_example.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def encrypt_and_decrypt_with_keyring(
8181
config=MaterialProvidersConfig()
8282
)
8383

84-
# # Create the AWS KMS RSA keyring input
84+
# Create the AWS KMS RSA keyring input
8585
# For more information on the allowed encryption algorithms, please see
8686
# https://docs.aws.amazon.com/kms/latest/developerguide/asymmetric-key-specs.html#key-spec-rsa
8787
keyring_input: CreateAwsKmsRsaKeyringInput = CreateAwsKmsRsaKeyringInput(

examples/src/keyrings/multi_keyring_example.py

+2
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ def encrypt_and_decrypt_with_keyring(
182182

183183
# 11. Demonstrate that you can successfully decrypt data using just the `kms_keyring`
184184
# directly.
185+
# (This is an example for demonstration; you do not need to do this in your own code.)
185186

186187
# 11a. Decrypt your encrypted data using the kms_keyring.
187188
plaintext_bytes_kms_keyring, _ = client.decrypt(
@@ -195,6 +196,7 @@ def encrypt_and_decrypt_with_keyring(
195196

196197
# 12. Demonstrate that you can also successfully decrypt data using the `raw_aes_keyring`
197198
# directly.
199+
# (This is an example for demonstration; you do not need to do this in your own code.)
198200

199201
# 12a. Decrypt your encrypted data using the raw_aes_keyring.
200202
plaintext_bytes_raw_aes_keyring, _ = client.decrypt(

examples/test/keyrings/test_i_aws_kms_discovery_keyring_example.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@
1111
def test_encrypt_and_decrypt_with_keyring():
1212
"""Test function for encrypt and decrypt using the AWS KMS Discovery Keyring example."""
1313
kms_key_id = "arn:aws:kms:us-west-2:658956600833:key/b3537ef1-d8dc-4780-9f5a-55776cbb2f7f"
14-
encrypt_and_decrypt_with_keyring(kms_key_id)
14+
aws_account_id = "658956600833"
15+
aws_region = "us-west-2"
16+
encrypt_and_decrypt_with_keyring(kms_key_id, aws_account_id, aws_region)

examples/test/keyrings/test_i_aws_kms_discovery_multi_keyring_example.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
def test_encrypt_and_decrypt_with_keyring():
1212
"""Test function for encrypt and decrypt using the AWS KMS Discovery Multi Keyring example."""
1313
kms_key_id = "arn:aws:kms:us-west-2:658956600833:key/b3537ef1-d8dc-4780-9f5a-55776cbb2f7f"
14+
aws_account_id = "658956600833"
1415
aws_regions = ["us-east-1", "us-west-2"]
15-
encrypt_and_decrypt_with_keyring(kms_key_id, aws_regions)
16+
encrypt_and_decrypt_with_keyring(kms_key_id, aws_account_id, aws_regions)

examples/test/keyrings/test_i_aws_kms_multi_keyring_example.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,9 @@ def test_encrypt_and_decrypt_with_keyring():
1414
"arn:aws:kms:us-west-2:658956600833:key/b3537ef1-d8dc-4780-9f5a-55776cbb2f7f"
1515
second_region_kms_key_id = \
1616
"arn:aws:kms:eu-central-1:658956600833:key/75414c93-5285-4b57-99c9-30c1cf0a22c2"
17-
encrypt_and_decrypt_with_keyring(default_region_kms_key_id, second_region_kms_key_id)
17+
default_region = "us-west-2"
18+
second_region = "eu-central-1"
19+
encrypt_and_decrypt_with_keyring(default_region_kms_key_id,
20+
second_region_kms_key_id,
21+
default_region,
22+
second_region)

0 commit comments

Comments
 (0)