-
Notifications
You must be signed in to change notification settings - Fork 86
Example for using multiple keys in multiple regions #177
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
Closed
Closed
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
3b62bc3
Testing something, want AppVeyor to run
caitlin-tibbetts 626d5ba
Quick change
caitlin-tibbetts 83f4ff8
Running AppVeyor
caitlin-tibbetts 534e225
Added example for using multiple keyrings in multiple regions
caitlin-tibbetts 42e86ab
Undid something quickly
caitlin-tibbetts 6b84d3a
Merge branch 'master' into a1b1c1-example
caitlin-tibbetts 2dfe2d0
Merge branch 'master' of github.com:aws/aws-encryption-sdk-python int…
caitlin-tibbetts fabc5e3
Fixed importerror
caitlin-tibbetts 67f0ddc
Merge branch 'a1b1c1-example' of github.com:caitlin-tibbetts/aws-encr…
caitlin-tibbetts 30eab33
Formatting fix
caitlin-tibbetts 453b82d
Update tox.ini
caitlin-tibbetts 2208890
Update tox.ini
caitlin-tibbetts d724335
Made some changes to the multiple_kms_cmk_regions example/test
caitlin-tibbetts 306d1a9
This is my next interation of the code for the example; however, I am…
caitlin-tibbetts bde7a56
Changed the example to test two CMKs in the same region until Issue #…
caitlin-tibbetts b7e9dd1
Found out how to make a new valid test key, so now there are two vali…
caitlin-tibbetts 4d8c7a0
Ran autoformat
caitlin-tibbetts 1fdbb32
Added some docstrings
caitlin-tibbetts d3240eb
Formatting will be the death of me
caitlin-tibbetts 4eb5fde
Used correct keys in test
caitlin-tibbetts bb6c650
Updated some comments
caitlin-tibbetts a833f52
Merge branch 'master' of github.com:aws/aws-encryption-sdk-python int…
caitlin-tibbetts 9e5fcd4
Update the integration tests
caitlin-tibbetts 38e2757
Small changes
caitlin-tibbetts 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 hidden or 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,72 @@ | ||
# 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 basic encryption and decryption of a value already in memory | ||
using multiple KMS CMKs in multiple regions. | ||
""" | ||
import aws_encryption_sdk | ||
from aws_encryption_sdk.key_providers.kms import KMSMasterKey, KMSMasterKeyProvider | ||
from aws_encryption_sdk.internal.crypto.encryption import encrypt, decrypt | ||
|
||
|
||
def multiple_kms_cmk_regions(key_arn_1, key_arn_2, source_plaintext, botocore_session=None): | ||
"""Encrypts and then decrypts a string under multiple KMS customer master keys (CMKs) in multiple regions. | ||
|
||
:param str key_arn_1: Amazon Resource Name (ARN) of the KMS CMK | ||
:param str key_arn_2: Amazon Resource Name (ARN) of another KMS CMK | ||
:param bytes source_plaintext: Data to encrypt | ||
:param botocore_session: existing botocore session instance | ||
:type botocore_session: botocore.session.Session | ||
""" | ||
# Check that these keys are in different regions | ||
assert not key_arn_1.split(":")[3] == key_arn_2.split(":")[3] | ||
|
||
kwargs = dict(key_ids=[key_arn_1, key_arn_2]) | ||
|
||
if botocore_session is not None: | ||
kwargs["botocore_session"] = botocore_session | ||
|
||
# Create master key provider using the ARNs of the keys and the session (botocore_session) | ||
kms_key_provider = KMSMasterKeyProvider(**kwargs) | ||
|
||
# Encrypt the plaintext using the AWS Encryption SDK. It returns the encrypted message and the header | ||
ciphertext, encrypted_message_header = encrypt(kms_key_provider, source_plaintext) | ||
|
||
# Check that both key ARNs are in the message headers | ||
assert len(encrypted_message_header.encrypted_data_keys) == 2 | ||
|
||
caitlin-tibbetts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Decrypt the encrypted message using the AWS Encryption SDK. It returns the decrypted message and the header | ||
# Either of our keys can be used to decrypt the message | ||
plaintext_1, decrypted_message_header_1 = decrypt( | ||
KMSMasterKey(key_id=key_arn_1), ciphertext | ||
) | ||
plaintext_2, decrypted_message_header_2 = decrypt( | ||
KMSMasterKey(key_id=key_arn_2), ciphertext | ||
) | ||
|
||
# Check that the original message and the decrypted message are the same | ||
if not isinstance(source_plaintext, bytes): | ||
plaintext1 = plaintext_1.decode("utf-8") | ||
plaintext2 = plaintext_2.decode("utf-8") | ||
assert source_plaintext == plaintext_1 | ||
assert source_plaintext == plaintext_2 | ||
|
||
# Check that 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_1.encryption_context.items() | ||
) | ||
assert all( | ||
pair in encrypted_message_header.encryption_context.items() | ||
for pair in decrypted_message_header_2.encryption_context.items() | ||
) |
This file contains hidden or 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,33 @@ | ||
# 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. | ||
"""Unit test suite for the encryption and decryption using multiple KMS CMKs in multiple regions example.""" | ||
|
||
import botocore.session | ||
import pytest | ||
|
||
from ..src.multiple_kms_cmk_regions import multiple_kms_cmk_regions | ||
|
||
from .examples_test_utils import get_cmk_arn | ||
from .examples_test_utils import static_plaintext | ||
|
||
|
||
pytestmark = [pytest.mark.examples] | ||
caitlin-tibbetts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def test_multiple_kms_cmk_regions(): | ||
plaintext = static_plaintext | ||
cmk_arn_1 = get_cmk_arn("us-west-2") | ||
cmk_arn_2 = get_cmk_arn("eu-central-1") | ||
multiple_kms_cmk_regions( | ||
cmk_arn_1, cmk_arn_2, source_plaintext=plaintext, botocore_session=botocore.session.Session() | ||
) |
This file contains hidden or 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
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.