|
| 1 | +# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +"""Example showing how to customize the AWS KMS Client.""" |
| 14 | +import boto3 |
| 15 | +from botocore.config import Config |
| 16 | + |
| 17 | +import aws_encryption_sdk |
| 18 | +from aws_encryption_sdk import CommitmentPolicy |
| 19 | + |
| 20 | + |
| 21 | +# Create a new class that extends the AWS KMS Provider you need to use |
| 22 | +class CustomKMSClientMasterKeyProvider(aws_encryption_sdk.StrictAwsKmsMasterKeyProvider): |
| 23 | + """Custom region-specific client which extends the StrictAwsKmsMasterKeyProvider""" |
| 24 | + |
| 25 | + # Override `add_regional_client` to use whatever configuration you need |
| 26 | + def add_regional_client(self, region_name): |
| 27 | + """Adds a regional client for the specified region if it does not already exist. |
| 28 | + :param str region_name: AWS Region ID (ex: us-east-1) |
| 29 | + """ |
| 30 | + if region_name not in self._regional_clients: |
| 31 | + session = boto3.session.Session(botocore_session=self.config.botocore_session) |
| 32 | + client = session.client( |
| 33 | + 'kms', |
| 34 | + region_name=region_name, |
| 35 | + # Add additional custom client configuration here |
| 36 | + config=Config(connection_timeout=10).merge(self._user_agent_adding_config) |
| 37 | + ) |
| 38 | + self._register_client(client, region_name) |
| 39 | + self._regional_clients[region_name] = client |
| 40 | + |
| 41 | + |
| 42 | +# This is just an example of using the above master key provider |
| 43 | +def encrypt_decrypt(key_arn, source_plaintext, botocore_session=None): |
| 44 | + """Encrypts and then decrypts a string under one KMS customer master key (CMK). |
| 45 | +
|
| 46 | + :param str key_arn: Amazon Resource Name (ARN) of the KMS CMK |
| 47 | + :param bytes source_plaintext: Data to encrypt |
| 48 | + :param botocore_session: existing botocore session instance |
| 49 | + :type botocore_session: botocore.session.Session |
| 50 | + """ |
| 51 | + kwargs = dict(key_ids=[key_arn]) |
| 52 | + |
| 53 | + if botocore_session is not None: |
| 54 | + kwargs["botocore_session"] = botocore_session |
| 55 | + |
| 56 | + # Set up an encryption client with an explicit commitment policy. Note that if you do not explicitly choose a |
| 57 | + # commitment policy, REQUIRE_ENCRYPT_REQUIRE_DECRYPT is used by default. |
| 58 | + client = aws_encryption_sdk.EncryptionSDKClient(commitment_policy=CommitmentPolicy.REQUIRE_ENCRYPT_REQUIRE_DECRYPT) |
| 59 | + |
| 60 | + # Create the custom master key provider using the ARN of the key and the session (botocore_session) |
| 61 | + kms_key_provider = CustomKMSClientMasterKeyProvider(**kwargs) |
| 62 | + |
| 63 | + # Encrypt the plaintext using the AWS Encryption SDK. It returns the encrypted message and the header. Note: in |
| 64 | + # order for decrypt to succeed, the key_ids value must be the key ARN of the CMK. |
| 65 | + ciphertext, encrypted_message_header = client.encrypt(source=source_plaintext, key_provider=kms_key_provider) |
| 66 | + |
| 67 | + # Decrypt the encrypted message using the AWS Encryption SDK. It returns the decrypted message and the header |
| 68 | + plaintext, decrypted_message_header = client.decrypt(source=ciphertext, key_provider=kms_key_provider) |
| 69 | + |
| 70 | + # Check if the original message and the decrypted message are the same |
| 71 | + assert source_plaintext == plaintext |
| 72 | + |
| 73 | + # Check if the headers of the encrypted message and decrypted message match |
| 74 | + assert all( |
| 75 | + pair in encrypted_message_header.encryption_context.items() |
| 76 | + for pair in decrypted_message_header.encryption_context.items() |
| 77 | + ) |
0 commit comments