|
| 1 | +# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +"""Performance tests for the hierarchy keyring.""" |
| 4 | + |
| 5 | +import aws_encryption_sdk |
| 6 | +import boto3 |
| 7 | +from aws_cryptographic_materialproviders.keystore import KeyStore |
| 8 | +from aws_cryptographic_materialproviders.keystore.config import KeyStoreConfig |
| 9 | +from aws_cryptographic_materialproviders.keystore.models import KMSConfigurationKmsKeyArn |
| 10 | +from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders |
| 11 | +from aws_cryptographic_materialproviders.mpl.config import MaterialProvidersConfig |
| 12 | +from aws_cryptographic_materialproviders.mpl.models import ( |
| 13 | + CacheTypeDefault, |
| 14 | + CreateAwsKmsHierarchicalKeyringInput, |
| 15 | + DefaultCache, |
| 16 | +) |
| 17 | +from aws_cryptographic_materialproviders.mpl.references import IKeyring |
| 18 | + |
| 19 | +from ..utils.util import PerfTestUtils |
| 20 | + |
| 21 | + |
| 22 | +def create_keyring( |
| 23 | + key_store_table_name: str, |
| 24 | + logical_key_store_name: str, |
| 25 | + kms_key_id: str, |
| 26 | + branch_key_id: str = PerfTestUtils.DEFAULT_BRANCH_KEY_ID |
| 27 | +): |
| 28 | + """Demonstrate how to create a hierarchy keyring. |
| 29 | +
|
| 30 | + Usage: create_keyring(key_store_table_name, logical_key_store_name, kms_key_id, branch_key_id) |
| 31 | + :param key_store_table_name: Name of the KeyStore DynamoDB table. |
| 32 | + :type key_store_table_name: string |
| 33 | + :param logical_key_store_name: Logical name of the KeyStore. |
| 34 | + :type logical_key_store_name: string |
| 35 | + :param kms_key_id: KMS Key identifier for the KMS key you want to use. |
| 36 | + :type kms_key_id: string |
| 37 | + :param branch_key_id: Branch key you want to use for the hierarchy keyring. |
| 38 | + :type branch_key_id: string |
| 39 | +
|
| 40 | + For more information on KMS Key identifiers, see |
| 41 | + https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#key-id |
| 42 | + """ |
| 43 | + # Create boto3 clients for DynamoDB and KMS. |
| 44 | + ddb_client = boto3.client('dynamodb', region_name="us-west-2") |
| 45 | + kms_client = boto3.client('kms', region_name="us-west-2") |
| 46 | + |
| 47 | + # Configure your KeyStore resource. |
| 48 | + # This SHOULD be the same configuration that you used |
| 49 | + # to initially create and populate your KeyStore. |
| 50 | + keystore: KeyStore = KeyStore( |
| 51 | + config=KeyStoreConfig( |
| 52 | + ddb_client=ddb_client, |
| 53 | + ddb_table_name=key_store_table_name, |
| 54 | + logical_key_store_name=logical_key_store_name, |
| 55 | + kms_client=kms_client, |
| 56 | + kms_configuration=KMSConfigurationKmsKeyArn( |
| 57 | + value=kms_key_id |
| 58 | + ), |
| 59 | + ) |
| 60 | + ) |
| 61 | + |
| 62 | + # Create the Hierarchical Keyring. |
| 63 | + mat_prov: AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders( |
| 64 | + config=MaterialProvidersConfig() |
| 65 | + ) |
| 66 | + |
| 67 | + keyring_input: CreateAwsKmsHierarchicalKeyringInput = CreateAwsKmsHierarchicalKeyringInput( |
| 68 | + key_store=keystore, |
| 69 | + branch_key_id=branch_key_id, |
| 70 | + ttl_seconds=600, |
| 71 | + cache=CacheTypeDefault( |
| 72 | + value=DefaultCache( |
| 73 | + entry_capacity=100 |
| 74 | + ) |
| 75 | + ), |
| 76 | + ) |
| 77 | + |
| 78 | + keyring: IKeyring = mat_prov.create_aws_kms_hierarchical_keyring( |
| 79 | + input=keyring_input |
| 80 | + ) |
| 81 | + |
| 82 | + return keyring |
| 83 | + |
| 84 | + |
| 85 | +def encrypt_using_keyring( |
| 86 | + plaintext_data: bytes, |
| 87 | + keyring: IKeyring |
| 88 | +): |
| 89 | + """Demonstrate how to encrypt plaintext data using a hierarchy keyring. |
| 90 | +
|
| 91 | + Usage: encrypt_using_keyring(plaintext_data, keyring) |
| 92 | + :param plaintext_data: plaintext data you want to encrypt |
| 93 | + :type: bytes |
| 94 | + :param keyring: Keyring to use for encryption. |
| 95 | + :type keyring: IKeyring |
| 96 | + """ |
| 97 | + client = aws_encryption_sdk.EncryptionSDKClient() |
| 98 | + |
| 99 | + ciphertext_data, _ = client.encrypt( |
| 100 | + source=plaintext_data, |
| 101 | + keyring=keyring, |
| 102 | + encryption_context=PerfTestUtils.DEFAULT_ENCRYPTION_CONTEXT |
| 103 | + ) |
| 104 | + |
| 105 | + return ciphertext_data |
| 106 | + |
| 107 | + |
| 108 | +def decrypt_using_keyring( |
| 109 | + ciphertext_data: bytes, |
| 110 | + keyring: IKeyring |
| 111 | +): |
| 112 | + """Demonstrate how to decrypt ciphertext data using a hierarchy keyring. |
| 113 | +
|
| 114 | + Usage: decrypt_using_keyring(ciphertext_data, keyring) |
| 115 | + :param ciphertext_data: ciphertext data you want to decrypt |
| 116 | + :type: bytes |
| 117 | + :param keyring: Keyring to use for decryption. |
| 118 | + :type keyring: IKeyring |
| 119 | + """ |
| 120 | + client = aws_encryption_sdk.EncryptionSDKClient() |
| 121 | + |
| 122 | + decrypted_plaintext_data, _ = client.decrypt( |
| 123 | + source=ciphertext_data, |
| 124 | + keyring=keyring, |
| 125 | + encryption_context=PerfTestUtils.DEFAULT_ENCRYPTION_CONTEXT |
| 126 | + ) |
| 127 | + |
| 128 | + return decrypted_plaintext_data |
0 commit comments