Skip to content

Commit 8bd9383

Browse files
committed
fix: updated aws_kms_rsa_keyring
1 parent 731c698 commit 8bd9383

File tree

3 files changed

+24
-28
lines changed

3 files changed

+24
-28
lines changed

examples/src/keyrings/aws_kms_multi_keyring_example.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,12 @@
3636
For more information on how to use Multi keyrings, see
3737
https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-multi-keyring.html
3838
"""
39-
import secrets
4039
import sys
4140

4241
import boto3
4342
from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders
4443
from aws_cryptographic_materialproviders.mpl.config import MaterialProvidersConfig
45-
from aws_cryptographic_materialproviders.mpl.models import (
46-
CreateAwsKmsKeyringInput,
47-
CreateAwsKmsMultiKeyringInput
48-
)
44+
from aws_cryptographic_materialproviders.mpl.models import CreateAwsKmsKeyringInput, CreateAwsKmsMultiKeyringInput
4945
from aws_cryptographic_materialproviders.mpl.references import IKeyring
5046
from typing import Dict
5147

examples/src/keyrings/aws_kms_rsa_keyring_example.py

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,17 @@
33
"""
44
This example sets up the AWS KMS RSA Keyring
55
6-
The AWS KMS RSA keyring uses asymmetric encryption KMS RSA keys to generate, encrypt and
7-
decrypt data keys. This example creates a KMS RSA Keyring and then encrypts a custom input
6+
This example creates a KMS RSA Keyring and then encrypts a custom input
87
EXAMPLE_DATA with an encryption context. This example also includes some sanity checks for
98
demonstration:
109
1. Ciphertext and plaintext data are not the same
1110
2. Encryption context is correct in the decrypted message header
1211
3. Decrypted plaintext value matches EXAMPLE_DATA
1312
These sanity checks are for demonstration in the example only. You do not need these in your code.
14-
"""
15-
#
16-
# AWS KMS RSA keyrings can be used independently or in a multi-keyring with other keyrings
17-
# of the same or a different type.
1813
1914
# For more information on how to use KMS keyrings, see
2015
# https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-kms-keyring.html
16+
"""
2117
import sys
2218

2319
import boto3
@@ -29,6 +25,7 @@
2925

3026
import aws_encryption_sdk
3127
from aws_encryption_sdk import CommitmentPolicy
28+
from aws_encryption_sdk.identifiers import AlgorithmSuite
3229

3330
# TODO-MPL: Remove this as part of removing PYTHONPATH hacks.
3431
MODULE_ROOT_DIR = '/'.join(__file__.split("/")[:-1])
@@ -39,14 +36,16 @@
3936

4037

4138
def encrypt_and_decrypt_with_keyring(
42-
kms_rsa_key_id: str
39+
kms_rsa_key_id: str,
40+
kms_rsa_public_key: str
4341
):
4442
"""Demonstrate an encrypt/decrypt cycle using an AWS KMS RSA keyring.
4543
46-
Usage: encrypt_and_decrypt_with_keyring(kms_rsa_key_id)
47-
:param kms_rsa_key_id: KMS RSA Key identifier for the KMS RSA key you want to use for
48-
encryption and decryption of your data keys.
44+
Usage: encrypt_and_decrypt_with_keyring(kms_rsa_key_id, kms_rsa_public_key)
45+
:param kms_rsa_key_id: KMS RSA Key identifier for the KMS RSA key you want to use
4946
:type kms_rsa_key_id: string
47+
:param kms_rsa_public_key: KMS RSA public key you want to use
48+
:type kms_rsa_public_key: string
5049
5150
For more information on KMS Key identifiers, see
5251
https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#key-id
@@ -78,17 +77,6 @@ def encrypt_and_decrypt_with_keyring(
7877
}
7978

8079
# 4. Create a KMS RSA keyring
81-
kms_rsa_public_key = '''-----BEGIN PUBLIC KEY-----
82-
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA27Uc/fBaMVhxCE/SpCMQ
83-
oSBRSzQJw+o2hBaA+FiPGtiJ/aPy7sn18aCkelaSj4kwoC79b/arNHlkjc7OJFsN
84-
/GoFKgNvaiY4lOeJqEiWQGSSgHtsJLdbO2u4OOSxh8qIRAMKbMgQDVX4FR/PLKeK
85-
fc2aCDvcNSpAM++8NlNmv7+xQBJydr5ce91eISbHkFRkK3/bAM+1iddupoRw4Wo2
86-
r3avzrg5xBHmzR7u1FTab22Op3Hgb2dBLZH43wNKAceVwKqKA8UNAxashFON7xK9
87-
yy4kfOL0Z/nhxRKe4jRZ/5v508qIzgzCksYy7Y3QbMejAtiYnr7s5/d5KWw0swou
88-
twIDAQAB
89-
-----END PUBLIC KEY-----
90-
'''
91-
9280
mat_prov: AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders(
9381
config=MaterialProvidersConfig()
9482
)
@@ -108,7 +96,8 @@ def encrypt_and_decrypt_with_keyring(
10896
ciphertext, _ = client.encrypt(
10997
source=EXAMPLE_DATA,
11098
keyring=kms_rsa_keyring,
111-
encryption_context=encryption_context
99+
encryption_context=encryption_context,
100+
algorithm=AlgorithmSuite.AES_256_GCM_HKDF_SHA512_COMMIT_KEY
112101
)
113102

114103
# 6. Demonstrate that the ciphertext and plaintext are different.

examples/test/keyrings/test_i_aws_kms_rsa_keyring_example.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,15 @@
1111
def test_encrypt_and_decrypt_with_keyring():
1212
"""Test function for encrypt and decrypt using the AWS KMS RSA Keyring example."""
1313
kms_rsa_key_id = "arn:aws:kms:us-west-2:370957321024:key/mrk-63d386cb70614ea59b32ad65c9315297"
14-
encrypt_and_decrypt_with_keyring(kms_rsa_key_id)
14+
15+
# THIS IS A PUBLIC RESOURCE AND SHOULD NOT BE USED IN A PRODUCTION ENVIRONMENT
16+
kms_rsa_public_key = bytes("-----BEGIN PUBLIC KEY-----\n"
17+
+ "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA27Uc/fBaMVhxCE/SpCMQ"
18+
+ "oSBRSzQJw+o2hBaA+FiPGtiJ/aPy7sn18aCkelaSj4kwoC79b/arNHlkjc7OJFsN"
19+
+ "/GoFKgNvaiY4lOeJqEiWQGSSgHtsJLdbO2u4OOSxh8qIRAMKbMgQDVX4FR/PLKeK"
20+
+ "fc2aCDvcNSpAM++8NlNmv7+xQBJydr5ce91eISbHkFRkK3/bAM+1iddupoRw4Wo2"
21+
+ "r3avzrg5xBHmzR7u1FTab22Op3Hgb2dBLZH43wNKAceVwKqKA8UNAxashFON7xK9"
22+
+ "yy4kfOL0Z/nhxRKe4jRZ/5v508qIzgzCksYy7Y3QbMejAtiYnr7s5/d5KWw0swou"
23+
+ "twIDAQAB"
24+
+ "\n-----END PUBLIC KEY-----", 'utf-8')
25+
encrypt_and_decrypt_with_keyring(kms_rsa_key_id, kms_rsa_public_key)

0 commit comments

Comments
 (0)