-
Notifications
You must be signed in to change notification settings - Fork 86
docs: add example to replicate AWS KMS MKP behavior with AWS KMS keyring #255
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
18 commits
Select commit
Hold shift + click to select a range
314323e
docs: add example to replicate AWS KMS MKP behavior with AWS KMS keyring
mattsb42-aws 356083d
docs: thanks, grazie; fixing grammar
mattsb42-aws 3c71661
docs: update examples/src/keyring/aws_kms/act_like_aws_kms_master_key…
mattsb42-aws b0c0cc0
docs: update examples/src/keyring/aws_kms/act_like_aws_kms_master_key…
mattsb42-aws 93a1371
docs: reword behavior separation statement
mattsb42-aws 70134b1
docs: linting fix
mattsb42-aws dc8dd2f
Merge branch 'master' into mkp-keyring-example
mattsb42-aws 0181431
docs: apply suggestions from code review
mattsb42-aws 2aed0c8
docs: s/replicate/reproduce/
mattsb42-aws bbb24ef
docs: keep naming consistent
mattsb42-aws b7ced3d
docs: fix line length issue in docstring
mattsb42-aws 6239257
docs: revise description of AWS KMS discovery decrypt behavior
mattsb42-aws 52d6277
docs: remove duplicate typo
mattsb42-aws 8837284
docs: refine wording on discovery descriptions
mattsb42-aws b3598d3
docs: revise AWS KMS keyring-as-mkp example
mattsb42-aws 86f618b
docs: break up long sentence
mattsb42-aws 52cbca6
Merge branch 'master' into mkp-keyring-example
mattsb42-aws a57c379
docs: update examples/src/keyring/aws_kms/act_like_aws_kms_master_key…
mattsb42-aws 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
116 changes: 116 additions & 0 deletions
116
examples/src/keyring/aws_kms/act_like_aws_kms_master_key_provider.py
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,116 @@ | ||
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
""" | ||
You might have used master key providers to protect your data keys | ||
in an earlier version of the AWS Encryption SDK. | ||
This example shows how to configure a keyring that behaves like an AWS KMS master key provider. | ||
|
||
The AWS Encryption SDK provided an AWS KMS master key provider for | ||
interacting with AWS Key Management Service (AWS KMS). | ||
On encrypt, the AWS KMS master key provider behaves like the AWS KMS keyring | ||
and encrypts with all CMKs that you identify. | ||
However, on decrypt, | ||
the AWS KMS master key provider reviews each encrypted data key (EDK). | ||
If the EDK was encrypted under an AWS KMS CMK, | ||
the AWS KMS master key provider attempts to decrypt it. | ||
Whether decryption succeeds depends on permissions on the CMK. | ||
This continues until the AWS KMS master key provider either runs out of EDKs | ||
or succeeds in decrypting an EDK. | ||
We have found that separating these two behaviors | ||
makes the expected behavior clearer, | ||
so that is what we did with the AWS KMS keyring and the AWS KMS discovery keyring. | ||
However, as you migrate from master key providers to keyrings, | ||
you might want a keyring that behaves like the AWS KMS master key provider. | ||
|
||
For more examples of how to use the AWS KMS keyring, | ||
see the ``keyring/aws_kms`` directory. | ||
""" | ||
import aws_encryption_sdk | ||
from aws_encryption_sdk.key_providers.kms import KMSMasterKeyProvider | ||
from aws_encryption_sdk.keyrings.aws_kms import AwsKmsKeyring | ||
from aws_encryption_sdk.keyrings.multi import MultiKeyring | ||
|
||
try: # Python 3.5.0 and 3.5.1 have incompatible typing modules | ||
from typing import Sequence # noqa pylint: disable=unused-import | ||
except ImportError: # pragma: no cover | ||
# We only actually need these imports when running the mypy checks | ||
pass | ||
|
||
|
||
def run(aws_kms_cmk, aws_kms_additional_cmks, source_plaintext): | ||
# type: (str, Sequence[str], bytes) -> None | ||
"""Demonstrate how to create a keyring that behaves like an AWS KMS master key provider. | ||
|
||
:param str aws_kms_cmk: The ARN of an AWS KMS CMK that protects data keys | ||
:param List[str] aws_kms_additional_cmks: Additional ARNs of secondary AWS KMS CMKs | ||
:param bytes source_plaintext: Plaintext to encrypt | ||
""" | ||
# Prepare your encryption context. | ||
# https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context | ||
encryption_context = { | ||
"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", | ||
} | ||
|
||
# This is the master key provider whose behavior we want to reproduce. | ||
# | ||
# When encrypting, this master key provider generates the data key using the first CMK in the list | ||
# and encrypts the data key using all specified CMKs. | ||
# However, when decrypting, this master key provider attempts to decrypt | ||
# any data keys that were encrypted under an AWS KMS CMK. | ||
master_key_provider_cmks = [aws_kms_cmk] + aws_kms_additional_cmks | ||
_master_key_provider_to_replicate = KMSMasterKeyProvider( # noqa: intentionally never used | ||
key_ids=master_key_provider_cmks, | ||
) | ||
|
||
# Create a CMK keyring that encrypts and decrypts using the specified AWS KMS CMKs. | ||
# | ||
# This keyring reproduces the encryption behavior of the AWS KMS master key provider. | ||
# | ||
# The AWS KMS keyring requires that you explicitly identify the CMK | ||
# that you want the keyring to use to generate the data key. | ||
cmk_keyring = AwsKmsKeyring(generator_key_id=aws_kms_cmk, key_ids=aws_kms_additional_cmks) | ||
|
||
# Create an AWS KMS discovery keyring that will attempt to decrypt | ||
# any data keys that were encrypted under an AWS KMS CMK. | ||
discovery_keyring = AwsKmsKeyring(is_discovery=True) | ||
|
||
# Combine the single-CMK and discovery keyrings | ||
mattsb42-aws marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# to create a keyring that behaves like an AWS KMS master key provider. | ||
# | ||
# The CMK keyring reproduces the encryption behavior | ||
# and the discovery keyring reproduces the decryption behavior. | ||
# This also means that it does not matter if the CMK keyring fails to decrypt. | ||
# For example, if you configured the CMK keyring with aliases, | ||
# it works on encrypt but fails to match any encrypted data keys on decrypt | ||
# because the serialized key name is the resulting CMK ARN rather than the alias name. | ||
# However, because the discovery keyring attempts to decrypt any AWS KMS-encrypted | ||
# data keys that it finds, the message still decrypts successfully. | ||
keyring = MultiKeyring(generator=cmk_keyring, children=[discovery_keyring]) | ||
|
||
# Encrypt your plaintext data. | ||
ciphertext, _encrypt_header = aws_encryption_sdk.encrypt( | ||
source=source_plaintext, encryption_context=encryption_context, keyring=keyring | ||
) | ||
|
||
# Demonstrate that the ciphertext and plaintext are different. | ||
assert ciphertext != source_plaintext | ||
|
||
# Decrypt your encrypted data using the same keyring you used on encrypt. | ||
# | ||
# You do not need to specify the encryption context on decrypt | ||
# because the header of the encrypted message includes the encryption context. | ||
decrypted, decrypt_header = aws_encryption_sdk.decrypt(source=ciphertext, keyring=keyring) | ||
|
||
# Demonstrate that the decrypted plaintext is identical to the original plaintext. | ||
assert decrypted == source_plaintext | ||
|
||
# Verify that the encryption context used in the decrypt operation includes | ||
# the encryption context that you specified when encrypting. | ||
# The AWS Encryption SDK can add pairs, so don't require an exact match. | ||
# | ||
# In production, always use a meaningful encryption context. | ||
assert set(encryption_context.items()) <= set(decrypt_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
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.