|
| 1 | +# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +"""Common Utilities for Migration Examples.""" |
| 4 | +import boto3 |
| 5 | +from aws_cryptographic_material_providers.mpl import AwsCryptographicMaterialProviders |
| 6 | +from aws_cryptographic_material_providers.mpl.config import MaterialProvidersConfig |
| 7 | +from aws_cryptographic_material_providers.mpl.models import ( |
| 8 | + CreateAwsKmsMrkMultiKeyringInput, |
| 9 | + DBEAlgorithmSuiteId, |
| 10 | +) |
| 11 | +from aws_cryptographic_material_providers.mpl.references import IKeyring |
| 12 | +from aws_dbesdk_dynamodb.encrypted.client import EncryptedClient |
| 13 | +from aws_dbesdk_dynamodb.structures.dynamodb import ( |
| 14 | + DynamoDbTableEncryptionConfig, |
| 15 | + DynamoDbTablesEncryptionConfig, |
| 16 | +) |
| 17 | +from aws_dbesdk_dynamodb.structures.structured_encryption import ( |
| 18 | + CryptoAction, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +def setup_awsdbe_client_without_plaintext_override(kms_key_id: str, ddb_table_name: str): |
| 23 | + """ |
| 24 | + Set up a pure AWS Database Encryption SDK EncryptedClient without plaintext override. |
| 25 | +
|
| 26 | + :param kms_key_id: The ARN of the KMS key to use for encryption |
| 27 | + :param ddb_table_name: The name of the DynamoDB table |
| 28 | + :returns EncryptedClient for DynamoDB |
| 29 | + """ |
| 30 | + # 1. Create a Keyring. This Keyring will be responsible for protecting the data keys that protect your data. |
| 31 | + # For this example, we will create a AWS KMS Keyring with the AWS KMS Key we want to use. |
| 32 | + # We will use the `CreateMrkMultiKeyring` method to create this keyring, |
| 33 | + # as it will correctly handle both single region and Multi-Region KMS Keys. |
| 34 | + mat_prov: AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders(config=MaterialProvidersConfig()) |
| 35 | + kms_mrk_multi_keyring_input: CreateAwsKmsMrkMultiKeyringInput = CreateAwsKmsMrkMultiKeyringInput( |
| 36 | + generator=kms_key_id, |
| 37 | + ) |
| 38 | + kms_mrk_multi_keyring: IKeyring = mat_prov.create_aws_kms_mrk_multi_keyring(input=kms_mrk_multi_keyring_input) |
| 39 | + |
| 40 | + # 2. Configure which attributes are encrypted and/or signed when writing new items. |
| 41 | + # For each attribute that may exist on the items we plan to write to our DynamoDbTable, |
| 42 | + # we must explicitly configure how they should be treated during item encryption: |
| 43 | + # - ENCRYPT_AND_SIGN: The attribute is encrypted and included in the signature |
| 44 | + # - SIGN_ONLY: The attribute not encrypted, but is still included in the signature |
| 45 | + # - DO_NOTHING: The attribute is not encrypted and not included in the signature |
| 46 | + attribute_actions_on_encrypt = { |
| 47 | + "partition_key": CryptoAction.SIGN_ONLY, |
| 48 | + "sort_key": CryptoAction.SIGN_ONLY, |
| 49 | + "attribute1": CryptoAction.ENCRYPT_AND_SIGN, |
| 50 | + "attribute2": CryptoAction.SIGN_ONLY, |
| 51 | + ":attribute3": CryptoAction.DO_NOTHING, |
| 52 | + } |
| 53 | + |
| 54 | + # 3. Configure which attributes we expect to be included in the signature |
| 55 | + # when reading items. There are two options for configuring this: |
| 56 | + # |
| 57 | + # - (Recommended) Configure `allowedUnsignedAttributesPrefix`: |
| 58 | + # When defining your DynamoDb schema and deciding on attribute names, |
| 59 | + # choose a distinguishing prefix (such as ":") for all attributes that |
| 60 | + # you do not want to include in the signature. |
| 61 | + # This has two main benefits: |
| 62 | + # - It is easier to reason about the security and authenticity of data within your item |
| 63 | + # when all unauthenticated data is easily distinguishable by their attribute name. |
| 64 | + # - If you need to add new unauthenticated attributes in the future, |
| 65 | + # you can easily make the corresponding update to your `attributeActionsOnEncrypt` |
| 66 | + # and immediately start writing to that new attribute, without |
| 67 | + # any other configuration update needed. |
| 68 | + # Once you configure this field, it is not safe to update it. |
| 69 | + # |
| 70 | + # - Configure `allowedUnsignedAttributes`: You may also explicitly list |
| 71 | + # a set of attributes that should be considered unauthenticated when encountered |
| 72 | + # on read. Be careful if you use this configuration. Do not remove an attribute |
| 73 | + # name from this configuration, even if you are no longer writing with that attribute, |
| 74 | + # as old items may still include this attribute, and our configuration needs to know |
| 75 | + # to continue to exclude this attribute from the signature scope. |
| 76 | + # If you add new attribute names to this field, you must first deploy the update to this |
| 77 | + # field to all readers in your host fleet before deploying the update to start writing |
| 78 | + # with that new attribute. |
| 79 | + # |
| 80 | + # For this example, we have designed our DynamoDb table such that any attribute name with |
| 81 | + # the ":" prefix should be considered unauthenticated. |
| 82 | + unsignAttrPrefix: str = ":" |
| 83 | + |
| 84 | + # 4. Create the DynamoDb Encryption configuration for the table we will be writing to. |
| 85 | + table_configs = {} |
| 86 | + table_config = DynamoDbTableEncryptionConfig( |
| 87 | + logical_table_name=ddb_table_name, |
| 88 | + partition_key_name="partition_key", |
| 89 | + sort_key_name="sort_key", |
| 90 | + attribute_actions_on_encrypt=attribute_actions_on_encrypt, |
| 91 | + keyring=kms_mrk_multi_keyring, |
| 92 | + allowed_unsigned_attribute_prefix=unsignAttrPrefix, |
| 93 | + # Specifying an algorithm suite is not required, |
| 94 | + # but is done here to demonstrate how to do so. |
| 95 | + # We suggest using the |
| 96 | + # `ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY_ECDSA_P384_SYMSIG_HMAC_SHA384` suite, |
| 97 | + # which includes AES-GCM with key derivation, signing, and key commitment. |
| 98 | + # This is also the default algorithm suite if one is not specified in this config. |
| 99 | + # For more information on supported algorithm suites, see: |
| 100 | + # https://docs.aws.amazon.com/database-encryption-sdk/latest/devguide/supported-algorithms.html |
| 101 | + algorithm_suite_id=DBEAlgorithmSuiteId.ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY_ECDSA_P384_SYMSIG_HMAC_SHA384, |
| 102 | + ) |
| 103 | + table_configs[ddb_table_name] = table_config |
| 104 | + tables_config = DynamoDbTablesEncryptionConfig(table_encryption_configs=table_configs) |
| 105 | + |
| 106 | + # 5. Create the EncryptedClient |
| 107 | + return EncryptedClient( |
| 108 | + client=boto3.client("dynamodb"), |
| 109 | + encryption_config=tables_config, |
| 110 | + ) |
| 111 | + |
| 112 | + |
| 113 | +def setup_awsdbe_client_with_plaintext_override(kms_key_id: str, ddb_table_name: str, policy: str): |
| 114 | + """ |
| 115 | + Set up an AWS Database Encryption SDK EncryptedClient with plaintext override. |
| 116 | +
|
| 117 | + :param kms_key_id: The ARN of the KMS key to use for encryption |
| 118 | + :param ddb_table_name: The name of the DynamoDB table |
| 119 | + :param policy: The policy required for the Plaintext Override configuration |
| 120 | + :returns EncryptedClient for DynamoDB |
| 121 | +
|
| 122 | + """ |
| 123 | + # 1. Create a Keyring. This Keyring will be responsible for protecting the data keys that protect your data. |
| 124 | + # For this example, we will create a AWS KMS Keyring with the AWS KMS Key we want to use. |
| 125 | + # We will use the `CreateMrkMultiKeyring` method to create this keyring, |
| 126 | + # as it will correctly handle both single region and Multi-Region KMS Keys. |
| 127 | + mat_prov: AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders(config=MaterialProvidersConfig()) |
| 128 | + kms_mrk_multi_keyring_input: CreateAwsKmsMrkMultiKeyringInput = CreateAwsKmsMrkMultiKeyringInput( |
| 129 | + generator=kms_key_id, |
| 130 | + ) |
| 131 | + kms_mrk_multi_keyring: IKeyring = mat_prov.create_aws_kms_mrk_multi_keyring(input=kms_mrk_multi_keyring_input) |
| 132 | + |
| 133 | + # 2. Configure which attributes are encrypted and/or signed when writing new items. |
| 134 | + # For each attribute that may exist on the items we plan to write to our DynamoDbTable, |
| 135 | + # we must explicitly configure how they should be treated during item encryption: |
| 136 | + # - ENCRYPT_AND_SIGN: The attribute is encrypted and included in the signature |
| 137 | + # - SIGN_ONLY: The attribute not encrypted, but is still included in the signature |
| 138 | + # - DO_NOTHING: The attribute is not encrypted and not included in the signature |
| 139 | + attribute_actions_on_encrypt = { |
| 140 | + "partition_key": CryptoAction.SIGN_ONLY, |
| 141 | + "sort_key": CryptoAction.SIGN_ONLY, |
| 142 | + "attribute1": CryptoAction.ENCRYPT_AND_SIGN, |
| 143 | + "attribute2": CryptoAction.SIGN_ONLY, |
| 144 | + ":attribute3": CryptoAction.DO_NOTHING, |
| 145 | + } |
| 146 | + |
| 147 | + # 3. Configure which attributes we expect to be included in the signature |
| 148 | + # when reading items. There are two options for configuring this: |
| 149 | + # |
| 150 | + # - (Recommended) Configure `allowedUnsignedAttributesPrefix`: |
| 151 | + # When defining your DynamoDb schema and deciding on attribute names, |
| 152 | + # choose a distinguishing prefix (such as ":") for all attributes that |
| 153 | + # you do not want to include in the signature. |
| 154 | + # This has two main benefits: |
| 155 | + # - It is easier to reason about the security and authenticity of data within your item |
| 156 | + # when all unauthenticated data is easily distinguishable by their attribute name. |
| 157 | + # - If you need to add new unauthenticated attributes in the future, |
| 158 | + # you can easily make the corresponding update to your `attributeActionsOnEncrypt` |
| 159 | + # and immediately start writing to that new attribute, without |
| 160 | + # any other configuration update needed. |
| 161 | + # Once you configure this field, it is not safe to update it. |
| 162 | + # |
| 163 | + # - Configure `allowedUnsignedAttributes`: You may also explicitly list |
| 164 | + # a set of attributes that should be considered unauthenticated when encountered |
| 165 | + # on read. Be careful if you use this configuration. Do not remove an attribute |
| 166 | + # name from this configuration, even if you are no longer writing with that attribute, |
| 167 | + # as old items may still include this attribute, and our configuration needs to know |
| 168 | + # to continue to exclude this attribute from the signature scope. |
| 169 | + # If you add new attribute names to this field, you must first deploy the update to this |
| 170 | + # field to all readers in your host fleet before deploying the update to start writing |
| 171 | + # with that new attribute. |
| 172 | + # |
| 173 | + # For this example, we have designed our DynamoDb table such that any attribute name with |
| 174 | + # the ":" prefix should be considered unauthenticated. |
| 175 | + unsignAttrPrefix: str = ":" |
| 176 | + |
| 177 | + # 4. Create the DynamoDb Encryption configuration for the table we will be writing to. |
| 178 | + # Include the plaintext override with the provided policy |
| 179 | + table_configs = {} |
| 180 | + table_config = DynamoDbTableEncryptionConfig( |
| 181 | + logical_table_name=ddb_table_name, |
| 182 | + partition_key_name="partition_key", |
| 183 | + sort_key_name="sort_key", |
| 184 | + attribute_actions_on_encrypt=attribute_actions_on_encrypt, |
| 185 | + keyring=kms_mrk_multi_keyring, |
| 186 | + # Provide plaintext_override policy to the config here |
| 187 | + plaintext_override=policy, |
| 188 | + allowed_unsigned_attribute_prefix=unsignAttrPrefix, |
| 189 | + # Specifying an algorithm suite is not required, |
| 190 | + # but is done here to demonstrate how to do so. |
| 191 | + # We suggest using the |
| 192 | + # `ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY_ECDSA_P384_SYMSIG_HMAC_SHA384` suite, |
| 193 | + # which includes AES-GCM with key derivation, signing, and key commitment. |
| 194 | + # This is also the default algorithm suite if one is not specified in this config. |
| 195 | + # For more information on supported algorithm suites, see: |
| 196 | + # https://docs.aws.amazon.com/database-encryption-sdk/latest/devguide/supported-algorithms.html |
| 197 | + algorithm_suite_id=DBEAlgorithmSuiteId.ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY_ECDSA_P384_SYMSIG_HMAC_SHA384, |
| 198 | + ) |
| 199 | + table_configs[ddb_table_name] = table_config |
| 200 | + tables_config = DynamoDbTablesEncryptionConfig(table_encryption_configs=table_configs) |
| 201 | + |
| 202 | + # 5. Create the EncryptedClient |
| 203 | + return EncryptedClient( |
| 204 | + client=boto3.client("dynamodb"), |
| 205 | + encryption_config=tables_config, |
| 206 | + ) |
0 commit comments