-
Notifications
You must be signed in to change notification settings - Fork 86
Adding example one_kms_cmk #158
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
15 commits
Select commit
Hold shift + click to select a range
47b70ff
Adding example one_kms_cmk
mmegs-amzn d579e71
Changes in one_kms_cmk and made random plaintext static
mmegs-amzn 1bb655e
Changes in one_kms_cmk and made random plaintext static
mmegs-amzn a71f9f3
Update one_kms_cmk.py
MeghaShetty af6f61d
Changes in one_kms_cmk and made random plaintext static
mmegs-amzn ce9c663
Merge branch 'mmegs-examples' of github.com:MeghaShetty/aws-encryptio…
mmegs-amzn a33d4ae
Formatting changes in some files
mmegs-amzn 4be5bf8
Minor changes in some files
mmegs-amzn 42b6c01
Adding example: Encryption decryption of streaming data using one KMS…
mmegs-amzn c73b930
Added 'b' to the static plaintext
mmegs-amzn 164f7dd
Wrapped static plaintext to <= 120 characters
mmegs-amzn 9ef87ea
Added docstrings
mmegs-amzn e21e00d
Corrected docstrings
mmegs-amzn 80251e6
Changed imports for same modules to be in the same statement and chan…
mmegs-amzn 4ac1a9c
Changed handle back
mmegs-amzn 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
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,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 one KMS CMK.""" | ||
import aws_encryption_sdk | ||
|
||
|
||
def encrypt_decrypt(key_arn, source_plaintext, botocore_session=None): | ||
mattsb42-aws marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""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 | ||
|
||
# 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() | ||
) |
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,59 @@ | ||
# 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 streaming data in memory using one KMS CMK.""" | ||
import filecmp | ||
|
||
import aws_encryption_sdk | ||
|
||
|
||
def encrypt_decrypt_stream(key_arn, source_plaintext_filename, botocore_session=None): | ||
"""Encrypts and then decrypts streaming data under one KMS customer master key (CMK). | ||
|
||
:param str key_arn: Amazon Resource Name (ARN) of the KMS CMK | ||
:param str source_plaintext_filename: Filename of file to encrypt | ||
:param botocore_session: existing botocore session instance | ||
:type botocore_session: botocore.session.Session | ||
""" | ||
kwargs = dict() | ||
|
||
kwargs["key_ids"] = [key_arn] | ||
|
||
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) | ||
|
||
ciphertext_filename = source_plaintext_filename + ".encrypted" | ||
decrypted_text_filename = source_plaintext_filename + ".decrypted" | ||
|
||
# Encrypt the plaintext using the AWS Encryption SDK. | ||
with open(source_plaintext_filename, "rb") as plaintext, open(ciphertext_filename, "wb") as ciphertext: | ||
with aws_encryption_sdk.stream(source=plaintext, mode="e", key_provider=kms_key_provider) as encryptor: | ||
for chunk in encryptor: | ||
ciphertext.write(chunk) | ||
|
||
# Decrypt the encrypted message using the AWS Encryption SDK. | ||
with open(ciphertext_filename, "rb") as ciphertext, open(decrypted_text_filename, "wb") as plaintext: | ||
with aws_encryption_sdk.stream(source=ciphertext, mode="d", key_provider=kms_key_provider) as decryptor: | ||
for chunk in decryptor: | ||
plaintext.write(chunk) | ||
|
||
# Check if the original message and the decrypted message are the same | ||
assert filecmp.cmp(source_plaintext_filename, decrypted_text_filename) | ||
|
||
# Check if the headers of the encrypted message and decrypted message match | ||
assert all( | ||
pair in encryptor.header.encryption_context.items() for pair in decryptor.header.encryption_context.items() | ||
) | ||
return ciphertext_filename, decrypted_text_filename |
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
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
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,29 @@ | ||
# 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 one KMS CMK example.""" | ||
|
||
import botocore.session | ||
import pytest | ||
|
||
from ..src.one_kms_cmk import encrypt_decrypt | ||
from .examples_test_utils import get_cmk_arn | ||
from .examples_test_utils import static_plaintext | ||
|
||
|
||
pytestmark = [pytest.mark.examples] | ||
mattsb42-aws marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def test_one_kms_cmk(): | ||
plaintext = static_plaintext | ||
cmk_arn = get_cmk_arn() | ||
encrypt_decrypt(key_arn=cmk_arn, 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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,40 @@ | ||||||
# Copyright 2017-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 of streaming data using one KMS CMK example.""" | ||||||
import os | ||||||
import tempfile | ||||||
|
||||||
import botocore.session | ||||||
import pytest | ||||||
|
||||||
from ..src.one_kms_cmk_streaming_data import encrypt_decrypt_stream | ||||||
from .examples_test_utils import get_cmk_arn, static_plaintext | ||||||
|
||||||
|
||||||
pytestmark = [pytest.mark.examples] | ||||||
|
||||||
|
||||||
def test_one_kms_cmk_streaming_data(): | ||||||
cmk_arn = get_cmk_arn() | ||||||
handle, filename = tempfile.mkstemp() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't use https://www.python.org/dev/peps/pep-0008/#method-names-and-instance-variables
Suggested change
|
||||||
with open(filename, "wb") as f: | ||||||
f.write(static_plaintext) | ||||||
try: | ||||||
new_files = encrypt_decrypt_stream( | ||||||
key_arn=cmk_arn, source_plaintext_filename=filename, botocore_session=botocore.session.Session() | ||||||
) | ||||||
for f in new_files: | ||||||
os.remove(f) | ||||||
finally: | ||||||
os.close(handle) | ||||||
os.remove(filename) |
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.