|
| 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.mpl import AwsCryptographicMaterialProviders |
| 8 | +from aws_cryptographic_materialproviders.mpl.config import MaterialProvidersConfig |
| 9 | +from aws_cryptographic_materialproviders.mpl.models import CreateAwsKmsKeyringInput |
| 10 | +from aws_cryptographic_materialproviders.mpl.references import IKeyring |
| 11 | + |
| 12 | +from aws_cryptographic_materialproviders.keystore import KeyStore |
| 13 | +from aws_cryptographic_materialproviders.keystore.config import KeyStoreConfig |
| 14 | +from aws_cryptographic_materialproviders.keystore.models import CreateKeyInput, KMSConfigurationKmsKeyArn |
| 15 | +from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders |
| 16 | +from aws_cryptographic_materialproviders.mpl.config import MaterialProvidersConfig |
| 17 | +from aws_cryptographic_materialproviders.mpl.models import ( |
| 18 | + CacheTypeDefault, |
| 19 | + CreateAwsKmsHierarchicalKeyringInput, |
| 20 | + DefaultCache, |
| 21 | +) |
| 22 | +from aws_cryptographic_materialproviders.mpl.references import IBranchKeyIdSupplier, IKeyring |
| 23 | +from typing import Dict # noqa pylint: disable=wrong-import-order |
| 24 | + |
| 25 | +import aws_encryption_sdk |
| 26 | +from aws_encryption_sdk import CommitmentPolicy |
| 27 | +from aws_encryption_sdk.exceptions import AWSEncryptionSDKClientError |
| 28 | + |
| 29 | +from .branch_key_id_supplier_example import ExampleBranchKeyIdSupplier |
| 30 | + |
| 31 | + |
| 32 | +def create_keyring( |
| 33 | + key_store_table_name: str, |
| 34 | + logical_key_store_name: str, |
| 35 | + kms_key_id: str |
| 36 | +): |
| 37 | + """Demonstrate how to create a hierarchy keyring. |
| 38 | +
|
| 39 | + Usage: create_keyring(key_store_table_name, logical_key_store_name, kms_key_id) |
| 40 | + :param key_store_table_name: Name of the KeyStore DynamoDB table. |
| 41 | + :type key_store_table_name: string |
| 42 | + :param logical_key_store_name: Logical name of the KeyStore. |
| 43 | + :type logical_key_store_name: string |
| 44 | + :param kms_key_id: KMS Key identifier for the KMS key you want to use. |
| 45 | + :type kms_key_id: string |
| 46 | +
|
| 47 | + For more information on KMS Key identifiers, see |
| 48 | + https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#key-id |
| 49 | + """ |
| 50 | + # Create boto3 clients for DynamoDB and KMS. |
| 51 | + ddb_client = boto3.client('dynamodb', region_name="us-west-2") |
| 52 | + kms_client = boto3.client('kms', region_name="us-west-2") |
| 53 | + |
| 54 | + # Configure your KeyStore resource. |
| 55 | + # This SHOULD be the same configuration that you used |
| 56 | + # to initially create and populate your KeyStore. |
| 57 | + keystore: KeyStore = KeyStore( |
| 58 | + config=KeyStoreConfig( |
| 59 | + ddb_client=ddb_client, |
| 60 | + ddb_table_name=key_store_table_name, |
| 61 | + logical_key_store_name=logical_key_store_name, |
| 62 | + kms_client=kms_client, |
| 63 | + kms_configuration=KMSConfigurationKmsKeyArn( |
| 64 | + value=kms_key_id |
| 65 | + ), |
| 66 | + ) |
| 67 | + ) |
| 68 | + |
| 69 | + # Call CreateKey to create two new active branch keys |
| 70 | + branch_key_id_a: str = keystore.create_key(input=CreateKeyInput()).branch_key_identifier |
| 71 | + branch_key_id_b: str = keystore.create_key(input=CreateKeyInput()).branch_key_identifier |
| 72 | + |
| 73 | + # Create a branch key supplier that maps the branch key id to a more readable format |
| 74 | + branch_key_id_supplier: IBranchKeyIdSupplier = ExampleBranchKeyIdSupplier( |
| 75 | + tenant_1_id=branch_key_id_a, |
| 76 | + tenant_2_id=branch_key_id_b, |
| 77 | + ) |
| 78 | + |
| 79 | + # Create the Hierarchical Keyring. |
| 80 | + mat_prov: AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders( |
| 81 | + config=MaterialProvidersConfig() |
| 82 | + ) |
| 83 | + |
| 84 | + keyring_input: CreateAwsKmsHierarchicalKeyringInput = CreateAwsKmsHierarchicalKeyringInput( |
| 85 | + key_store=keystore, |
| 86 | + branch_key_id_supplier=branch_key_id_supplier, |
| 87 | + ttl_seconds=600, |
| 88 | + cache=CacheTypeDefault( |
| 89 | + value=DefaultCache( |
| 90 | + entry_capacity=100 |
| 91 | + ) |
| 92 | + ), |
| 93 | + ) |
| 94 | + |
| 95 | + keyring: IKeyring = mat_prov.create_aws_kms_hierarchical_keyring( |
| 96 | + input=keyring_input |
| 97 | + ) |
| 98 | + |
| 99 | + return keyring |
| 100 | + |
| 101 | + |
| 102 | +def encrypt_using_keyring( |
| 103 | + plaintext_data: bytes, |
| 104 | + keyring: IKeyring |
| 105 | +): |
| 106 | + """Demonstrate how to encrypt plaintext data using an AWS KMS keyring. |
| 107 | +
|
| 108 | + Usage: encrypt_using_keyring(plaintext_data, keyring) |
| 109 | + :param plaintext_data: plaintext data you want to encrypt |
| 110 | + :type: bytes |
| 111 | + :param keyring: Keyring to use for encryption. |
| 112 | + :type keyring: IKeyring |
| 113 | + """ |
| 114 | + client = aws_encryption_sdk.EncryptionSDKClient() |
| 115 | + |
| 116 | + ciphertext_data, _ = client.encrypt( |
| 117 | + source=plaintext_data, |
| 118 | + keyring=keyring |
| 119 | + ) |
| 120 | + |
| 121 | + return ciphertext_data |
| 122 | + |
| 123 | + |
| 124 | +def decrypt_using_keyring( |
| 125 | + ciphertext_data: bytes, |
| 126 | + keyring: IKeyring |
| 127 | +): |
| 128 | + """Demonstrate how to decrypt ciphertext data using an AWS KMS keyring. |
| 129 | +
|
| 130 | + Usage: decrypt_using_keyring(ciphertext_data, keyring) |
| 131 | + :param ciphertext_data: ciphertext data you want to decrypt |
| 132 | + :type: bytes |
| 133 | + :param keyring: Keyring to use for decryption. |
| 134 | + :type keyring: IKeyring |
| 135 | + """ |
| 136 | + client = aws_encryption_sdk.EncryptionSDKClient() |
| 137 | + |
| 138 | + decrypted_plaintext_data, _ = client.decrypt( |
| 139 | + source=ciphertext_data, |
| 140 | + keyring=keyring |
| 141 | + ) |
| 142 | + |
| 143 | + return decrypted_plaintext_data |
0 commit comments