-
Notifications
You must be signed in to change notification settings - Fork 86
chore: Add example for custom KMS client config #440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
de0e562
doc: Add example for custom KMS client config
seebees face173
Merge branch 'master' into custom-kms-client
seebees 859efc2
Merge branch 'master' into custom-kms-client
seebees 10b67d4
Merge branch 'master' into custom-kms-client
ajewellamz 98c1da2
Merge branch 'master' into custom-kms-client
texastony 06917df
Merge branch 'master' into custom-kms-client
imabhichow 997a330
Merge branch 'master' into custom-kms-client
texastony 7207a27
Merge branch 'master' into custom-kms-client
kessplas 45f7e63
fix static analysis issues
217d71f
fix config parameter
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). You | ||
# may not use this file except in compliance with the License. A copy of | ||
# the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0/ | ||
# | ||
# or in the "license" file accompanying this file. This file is | ||
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF | ||
# ANY KIND, either express or implied. See the License for the specific | ||
# language governing permissions and limitations under the License. | ||
"""Example showing how to customize the AWS KMS Client.""" | ||
import boto3 | ||
from botocore.config import Config | ||
|
||
import aws_encryption_sdk | ||
from aws_encryption_sdk import CommitmentPolicy | ||
|
||
|
||
# Create a new class that extends the AWS KMS Provider you need to use | ||
class CustomKMSClientMasterKeyProvider(aws_encryption_sdk.StrictAwsKmsMasterKeyProvider): | ||
"""Custom region-specific client which extends the StrictAwsKmsMasterKeyProvider""" | ||
|
||
# Override `add_regional_client` to use whatever configuration you need | ||
def add_regional_client(self, region_name): | ||
"""Adds a regional client for the specified region if it does not already exist. | ||
:param str region_name: AWS Region ID (ex: us-east-1) | ||
""" | ||
if region_name not in self._regional_clients: | ||
session = boto3.session.Session(botocore_session=self.config.botocore_session) | ||
client = session.client( | ||
'kms', | ||
region_name=region_name, | ||
# Add additional custom client configuration here | ||
config=Config(connection_timeout=10).merge(self._user_agent_adding_config) | ||
) | ||
self._register_client(client, region_name) | ||
self._regional_clients[region_name] = client | ||
|
||
|
||
# This is just an example of using the above master key provider | ||
def encrypt_decrypt(key_arn, source_plaintext, botocore_session=None): | ||
"""Encrypts and then decrypts a string under one KMS customer master key (CMK). | ||
|
||
:param str key_arn: Amazon Resource Name (ARN) of the KMS CMK | ||
:param bytes source_plaintext: Data to encrypt | ||
:param botocore_session: existing botocore session instance | ||
:type botocore_session: botocore.session.Session | ||
""" | ||
kwargs = dict(key_ids=[key_arn]) | ||
|
||
if botocore_session is not None: | ||
kwargs["botocore_session"] = botocore_session | ||
|
||
# Set up an encryption client with an explicit commitment policy. Note that if you do not explicitly choose a | ||
# commitment policy, REQUIRE_ENCRYPT_REQUIRE_DECRYPT is used by default. | ||
client = aws_encryption_sdk.EncryptionSDKClient(commitment_policy=CommitmentPolicy.REQUIRE_ENCRYPT_REQUIRE_DECRYPT) | ||
|
||
# Create the custom master key provider using the ARN of the key and the session (botocore_session) | ||
kms_key_provider = CustomKMSClientMasterKeyProvider(**kwargs) | ||
|
||
# Encrypt the plaintext using the AWS Encryption SDK. It returns the encrypted message and the header. Note: in | ||
# order for decrypt to succeed, the key_ids value must be the key ARN of the CMK. | ||
ciphertext, encrypted_message_header = client.encrypt(source=source_plaintext, key_provider=kms_key_provider) | ||
|
||
# Decrypt the encrypted message using the AWS Encryption SDK. It returns the decrypted message and the header | ||
plaintext, decrypted_message_header = client.decrypt(source=ciphertext, key_provider=kms_key_provider) | ||
|
||
# Check if the original message and the decrypted message are the same | ||
assert source_plaintext == plaintext | ||
|
||
# Check if the headers of the encrypted message and decrypted message match | ||
assert all( | ||
pair in encrypted_message_header.encryption_context.items() | ||
for pair in decrypted_message_header.encryption_context.items() | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a note: IMO by including this in the official examples, this implicitly blesses these private methods as part of the public API for this class. Not sure if that's an issue or not, depending on the plans for the future of the codebase. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it may be better to refactor this example as:
However this does not address discovery.