-
Notifications
You must be signed in to change notification settings - Fork 86
chore(examples): Added file streaming, migration and set encryption algorithm examples #676
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
17 commits
Select commit
Hold shift + click to select a range
048a799
first commit
RitvikKapila 385edf2
added file streaming example; updated assertion error message in all …
RitvikKapila 6395d60
Merge branch 'mpl-reviewed' into rkapila/stream-migration-keyrings
RitvikKapila 7f65d22
fix gitignore
RitvikKapila d8e20f1
fix flake8; gitignore
RitvikKapila 121b73e
added migration example
RitvikKapila 7018577
fix flake8
RitvikKapila c82fc2f
fix codebuild tests
RitvikKapila 6617db7
fix
RitvikKapila cf4b3a9
fix
RitvikKapila 863c898
final editing
RitvikKapila 0721a43
updating commitment policy link
RitvikKapila 04c3746
minor edits
RitvikKapila f028aac
refactor set_encryption_algorithm_example to set_encryption_algorithm…
RitvikKapila 6ff24fd
fix isort
RitvikKapila 23246f6
fix
RitvikKapila a9065e0
m
RitvikKapila 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
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
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,152 @@ | ||
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
""" | ||
This example demonstrates file streaming for encryption and decryption. | ||
|
||
File streaming is useful when the plaintext or ciphertext file/data is too large to load into | ||
memory. Therefore, the AWS Encryption SDK allows users to stream the data, instead of loading it | ||
all at once in memory. In this example, we demonstrate file streaming for encryption and decryption | ||
using a Raw AES keyring. However, you can use any keyring with streaming. | ||
|
||
This example creates a Raw AES Keyring and then encrypts an input stream from the file | ||
`plaintext_filename` with an encryption context to an output (encrypted) file `ciphertext_filename`. | ||
It then decrypts the ciphertext from `ciphertext_filename` to a new file `decrypted_filename`. | ||
This example also includes some sanity checks for demonstration: | ||
1. Ciphertext and plaintext data are not the same | ||
2. Encryption context is correct in the decrypted message header | ||
3. Decrypted plaintext value matches EXAMPLE_DATA | ||
These sanity checks are for demonstration in the example only. You do not need these in your code. | ||
|
||
For more information on how to use Raw AES keyrings, see | ||
https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-raw-aes-keyring.html | ||
|
||
See raw_aes_keyring_example.py in the same directory for another raw AES keyring example | ||
in the AWS Encryption SDK for Python. | ||
""" | ||
import filecmp | ||
import secrets | ||
import sys | ||
|
||
from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders | ||
from aws_cryptographic_materialproviders.mpl.config import MaterialProvidersConfig | ||
from aws_cryptographic_materialproviders.mpl.models import AesWrappingAlg, CreateRawAesKeyringInput | ||
from aws_cryptographic_materialproviders.mpl.references import IKeyring | ||
from typing import Dict | ||
|
||
import aws_encryption_sdk | ||
from aws_encryption_sdk import CommitmentPolicy | ||
|
||
# TODO-MPL: Remove this as part of removing PYTHONPATH hacks. | ||
MODULE_ROOT_DIR = '/'.join(__file__.split("/")[:-1]) | ||
|
||
sys.path.append(MODULE_ROOT_DIR) | ||
|
||
|
||
def encrypt_and_decrypt_with_keyring( | ||
plaintext_filename: str, | ||
ciphertext_filename: str, | ||
decrypted_filename: str | ||
): | ||
"""Demonstrate a streaming encrypt/decrypt cycle. | ||
|
||
Usage: encrypt_and_decrypt_with_keyring(plaintext_filename | ||
ciphertext_filename | ||
decrypted_filename) | ||
:param plaintext_filename: filename of the plaintext data | ||
:type plaintext_filename: string | ||
:param ciphertext_filename: filename of the ciphertext data | ||
:type ciphertext_filename: string | ||
:param decrypted_filename: filename of the decrypted data | ||
:type decrypted_filename: string | ||
""" | ||
# 1. Instantiate the encryption SDK client. | ||
# This builds the client with the REQUIRE_ENCRYPT_REQUIRE_DECRYPT commitment policy, | ||
# which enforces that this client only encrypts using committing algorithm suites and enforces | ||
# that this client will only decrypt encrypted messages that were created with a committing | ||
# algorithm suite. | ||
# This is the default commitment policy if you were to build the client as | ||
# `client = aws_encryption_sdk.EncryptionSDKClient()`. | ||
client = aws_encryption_sdk.EncryptionSDKClient( | ||
commitment_policy=CommitmentPolicy.REQUIRE_ENCRYPT_REQUIRE_DECRYPT | ||
) | ||
|
||
# 2. The key namespace and key name are defined by you. | ||
# and are used by the Raw AES keyring to determine | ||
# whether it should attempt to decrypt an encrypted data key. | ||
# For more information, see | ||
# https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-raw-aes-keyring.html | ||
key_name_space = "Some managed raw keys" | ||
key_name = "My 256-bit AES wrapping key" | ||
|
||
# 3. Create encryption context. | ||
# Remember that your encryption context is NOT SECRET. | ||
# For more information, see | ||
# https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context | ||
encryption_context: Dict[str, str] = { | ||
"encryption": "context", | ||
"is not": "secret", | ||
"but adds": "useful metadata", | ||
"that can help you": "be confident that", | ||
"the data you are handling": "is what you think it is", | ||
} | ||
|
||
# 4. Generate a 256-bit AES key to use with your keyring. | ||
# In practice, you should get this key from a secure key management system such as an HSM. | ||
|
||
# Here, the input to secrets.token_bytes() = 32 bytes = 256 bits | ||
static_key = secrets.token_bytes(32) | ||
|
||
# 5. Create a Raw AES keyring | ||
# We choose to use a raw AES keyring, but any keyring can be used with streaming. | ||
mat_prov: AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders( | ||
config=MaterialProvidersConfig() | ||
) | ||
|
||
keyring_input: CreateRawAesKeyringInput = CreateRawAesKeyringInput( | ||
key_namespace=key_name_space, | ||
key_name=key_name, | ||
wrapping_key=static_key, | ||
wrapping_alg=AesWrappingAlg.ALG_AES256_GCM_IV12_TAG16 | ||
) | ||
|
||
raw_aes_keyring: IKeyring = mat_prov.create_raw_aes_keyring( | ||
input=keyring_input | ||
) | ||
|
||
# 6. Encrypt the data stream with the encryptionContext | ||
with open(plaintext_filename, 'rb') as pt_file, open(ciphertext_filename, 'wb') as ct_file: | ||
with client.stream( | ||
mode='e', | ||
source=pt_file, | ||
keyring=raw_aes_keyring, | ||
encryption_context=encryption_context | ||
) as encryptor: | ||
for chunk in encryptor: | ||
ct_file.write(chunk) | ||
|
||
# 7. Demonstrate that the ciphertext and plaintext are different. | ||
# (This is an example for demonstration; you do not need to do this in your own code.) | ||
assert not filecmp.cmp(plaintext_filename, ciphertext_filename), \ | ||
"Ciphertext and plaintext data are the same. Invalid encryption" | ||
|
||
# 8. Decrypt your encrypted data stream using the same keyring you used on encrypt. | ||
with open(ciphertext_filename, 'rb') as ct_file, open(decrypted_filename, 'wb') as pt_file: | ||
with client.stream( | ||
mode='d', | ||
source=ct_file, | ||
keyring=raw_aes_keyring, | ||
encryption_context=encryption_context | ||
) as decryptor: | ||
for chunk in decryptor: | ||
pt_file.write(chunk) | ||
|
||
# 9. Demonstrate that the encryption context is correct in the decrypted message header | ||
# (This is an example for demonstration; you do not need to do this in your own code.) | ||
for k, v in encryption_context.items(): | ||
assert v == decryptor.header.encryption_context[k], \ | ||
"Encryption context does not match expected values" | ||
|
||
# 10. Demonstrate that the decrypted plaintext is identical to the original plaintext. | ||
# (This is an example for demonstration; you do not need to do this in your own code.) | ||
assert filecmp.cmp(plaintext_filename, decrypted_filename), \ | ||
"Decrypted plaintext should be identical to the original plaintext. Invalid decryption" |
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
Oops, something went wrong.
Oops, something went wrong.
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.