Skip to content

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
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 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 Jul 23, 2019
626d5ba
Quick change
caitlin-tibbetts Jul 23, 2019
83f4ff8
Running AppVeyor
caitlin-tibbetts Jul 23, 2019
534e225
Added example for using multiple keyrings in multiple regions
caitlin-tibbetts Jul 24, 2019
42e86ab
Undid something quickly
caitlin-tibbetts Jul 24, 2019
6b84d3a
Merge branch 'master' into a1b1c1-example
caitlin-tibbetts Jul 24, 2019
2dfe2d0
Merge branch 'master' of github.com:aws/aws-encryption-sdk-python int…
caitlin-tibbetts Jul 24, 2019
fabc5e3
Fixed importerror
caitlin-tibbetts Jul 24, 2019
67f0ddc
Merge branch 'a1b1c1-example' of github.com:caitlin-tibbetts/aws-encr…
caitlin-tibbetts Jul 24, 2019
30eab33
Formatting fix
caitlin-tibbetts Jul 24, 2019
453b82d
Update tox.ini
caitlin-tibbetts Jul 25, 2019
2208890
Update tox.ini
caitlin-tibbetts Jul 25, 2019
d724335
Made some changes to the multiple_kms_cmk_regions example/test
caitlin-tibbetts Jul 25, 2019
306d1a9
This is my next interation of the code for the example; however, I am…
caitlin-tibbetts Jul 25, 2019
bde7a56
Changed the example to test two CMKs in the same region until Issue #…
caitlin-tibbetts Jul 26, 2019
b7e9dd1
Found out how to make a new valid test key, so now there are two vali…
caitlin-tibbetts Jul 26, 2019
4d8c7a0
Ran autoformat
caitlin-tibbetts Jul 26, 2019
1fdbb32
Added some docstrings
caitlin-tibbetts Jul 26, 2019
d3240eb
Formatting will be the death of me
caitlin-tibbetts Jul 26, 2019
4eb5fde
Used correct keys in test
caitlin-tibbetts Jul 26, 2019
bb6c650
Updated some comments
caitlin-tibbetts Jul 29, 2019
a833f52
Merge branch 'master' of github.com:aws/aws-encryption-sdk-python int…
caitlin-tibbetts Aug 2, 2019
9e5fcd4
Update the integration tests
caitlin-tibbetts Aug 5, 2019
38e2757
Small changes
caitlin-tibbetts Aug 5, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions examples/src/multiple_kms_cmk_regions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# 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


def encrypt_decrypt(key_arn1, key_arn2, region_name1, region_name2, 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_arn1, key_arn2], region_names=[region_name1, region_name2])

if botocore_session is not None:
kwargs["botocore_session"] = botocore_session

# Create master key provider using the ARN of the key and the session (botocore_session)
kms_key_provider = aws_encryption_sdk.KMSMasterKeyProvider(**kwargs)

# Encrypt the plaintext using the AWS Encryption SDK. It returns the encrypted message and the header
ciphertext, encrypted_message_header = aws_encryption_sdk.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 = aws_encryption_sdk.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()
)
31 changes: 31 additions & 0 deletions examples/test/test_i_multiple_kms_cmk_regions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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 encrypt_decrypt
from .examples_test_utils import get_cmk_arn
from .examples_test_utils import static_plaintext


pytestmark = [pytest.mark.examples]


def test_one_kms_cmk():
plaintext = static_plaintext
cmk_arn1 = get_cmk_arn()
cmk_arn2 = get_cmk_arn()
encrypt_decrypt(key_arn1=cmk_arn1, key_arn2=cmk_arn2, region_name1="us-west-1",
region_name2="us-east-1", source_plaintext=plaintext, botocore_session=botocore.session.Session())
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ commands =
examples: {[testenv:base-command]commands} examples/test/ -m examples
all: {[testenv:base-command]commands} test/ examples/test/
manual: {[testenv:base-command]commands}

# Verify that local tests work without environment variables present
[testenv:nocmk]
basepython = python3
Expand Down Expand Up @@ -252,7 +252,7 @@ commands = python setup.py check -r -s

[testenv:bandit]
basepython = python3
deps =
deps =
bandit>=1.5.1
commands = bandit -r src/aws_encryption_sdk/

Expand Down