|
| 1 | +# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +""" |
| 4 | +You might have used master key providers to protect your data keys |
| 5 | +in an earlier version of the AWS Encryption SDK. |
| 6 | +This example shows how to configure a keyring that behaves like an AWS KMS master key provider. |
| 7 | +
|
| 8 | +The AWS Encryption SDK provided an AWS KMS master key provider for |
| 9 | +interacting with AWS Key Management Service (AWS KMS). |
| 10 | +On encrypt, the AWS KMS master key provider behaves like the AWS KMS keyring |
| 11 | +and encrypts with all CMKs that you identify. |
| 12 | +However, on decrypt, |
| 13 | +the AWS KMS master key provider reviews each encrypted data key (EDK). |
| 14 | +If the EDK was encrypted under an AWS KMS CMK, |
| 15 | +the AWS KMS master key provider attempts to decrypt it. |
| 16 | +Whether decryption succeeds depends on permissions on the CMK. |
| 17 | +This continues until the AWS KMS master key provider either runs out of EDKs |
| 18 | +or succeeds in decrypting an EDK. |
| 19 | +We have found that separating these two behaviors |
| 20 | +makes the expected behavior clearer, |
| 21 | +so that is what we did with the AWS KMS keyring and the AWS KMS discovery keyring. |
| 22 | +However, as you migrate from master key providers to keyrings, |
| 23 | +you might want a keyring that behaves like the AWS KMS master key provider. |
| 24 | +
|
| 25 | +For more examples of how to use the AWS KMS keyring, |
| 26 | +see the ``keyring/aws_kms`` directory. |
| 27 | +""" |
| 28 | +import aws_encryption_sdk |
| 29 | +from aws_encryption_sdk.key_providers.kms import KMSMasterKeyProvider |
| 30 | +from aws_encryption_sdk.keyrings.aws_kms import AwsKmsKeyring |
| 31 | +from aws_encryption_sdk.keyrings.multi import MultiKeyring |
| 32 | + |
| 33 | +try: # Python 3.5.0 and 3.5.1 have incompatible typing modules |
| 34 | + from typing import Sequence # noqa pylint: disable=unused-import |
| 35 | +except ImportError: # pragma: no cover |
| 36 | + # We only actually need these imports when running the mypy checks |
| 37 | + pass |
| 38 | + |
| 39 | + |
| 40 | +def run(aws_kms_cmk, aws_kms_additional_cmks, source_plaintext): |
| 41 | + # type: (str, Sequence[str], bytes) -> None |
| 42 | + """Demonstrate how to create a keyring that behaves like an AWS KMS master key provider. |
| 43 | +
|
| 44 | + :param str aws_kms_cmk: The ARN of an AWS KMS CMK that protects data keys |
| 45 | + :param List[str] aws_kms_additional_cmks: Additional ARNs of secondary AWS KMS CMKs |
| 46 | + :param bytes source_plaintext: Plaintext to encrypt |
| 47 | + """ |
| 48 | + # Prepare your encryption context. |
| 49 | + # https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context |
| 50 | + encryption_context = { |
| 51 | + "encryption": "context", |
| 52 | + "is not": "secret", |
| 53 | + "but adds": "useful metadata", |
| 54 | + "that can help you": "be confident that", |
| 55 | + "the data you are handling": "is what you think it is", |
| 56 | + } |
| 57 | + |
| 58 | + # This is the master key provider whose behavior we want to reproduce. |
| 59 | + # |
| 60 | + # When encrypting, this master key provider generates the data key using the first CMK in the list |
| 61 | + # and encrypts the data key using all specified CMKs. |
| 62 | + # However, when decrypting, this master key provider attempts to decrypt |
| 63 | + # any data keys that were encrypted under an AWS KMS CMK. |
| 64 | + master_key_provider_cmks = [aws_kms_cmk] + aws_kms_additional_cmks |
| 65 | + _master_key_provider_to_replicate = KMSMasterKeyProvider( # noqa: intentionally never used |
| 66 | + key_ids=master_key_provider_cmks, |
| 67 | + ) |
| 68 | + |
| 69 | + # Create a CMK keyring that encrypts and decrypts using the specified AWS KMS CMKs. |
| 70 | + # |
| 71 | + # This keyring reproduces the encryption behavior of the AWS KMS master key provider. |
| 72 | + # |
| 73 | + # The AWS KMS keyring requires that you explicitly identify the CMK |
| 74 | + # that you want the keyring to use to generate the data key. |
| 75 | + cmk_keyring = AwsKmsKeyring(generator_key_id=aws_kms_cmk, key_ids=aws_kms_additional_cmks) |
| 76 | + |
| 77 | + # Create an AWS KMS discovery keyring that will attempt to decrypt |
| 78 | + # any data keys that were encrypted under an AWS KMS CMK. |
| 79 | + discovery_keyring = AwsKmsKeyring(is_discovery=True) |
| 80 | + |
| 81 | + # Combine the single-CMK and discovery keyrings |
| 82 | + # to create a keyring that behaves like an AWS KMS master key provider. |
| 83 | + # |
| 84 | + # The CMK keyring reproduces the encryption behavior |
| 85 | + # and the discovery keyring reproduces the decryption behavior. |
| 86 | + # This also means that it does not matter if the CMK keyring fails to decrypt. |
| 87 | + # For example, if you configured the CMK keyring with aliases, |
| 88 | + # it works on encrypt but fails to match any encrypted data keys on decrypt |
| 89 | + # because the serialized key name is the resulting CMK ARN rather than the alias name. |
| 90 | + # However, because the discovery keyring attempts to decrypt any AWS KMS-encrypted |
| 91 | + # data keys that it finds, the message still decrypts successfully. |
| 92 | + keyring = MultiKeyring(generator=cmk_keyring, children=[discovery_keyring]) |
| 93 | + |
| 94 | + # Encrypt your plaintext data. |
| 95 | + ciphertext, _encrypt_header = aws_encryption_sdk.encrypt( |
| 96 | + source=source_plaintext, encryption_context=encryption_context, keyring=keyring |
| 97 | + ) |
| 98 | + |
| 99 | + # Demonstrate that the ciphertext and plaintext are different. |
| 100 | + assert ciphertext != source_plaintext |
| 101 | + |
| 102 | + # Decrypt your encrypted data using the same keyring you used on encrypt. |
| 103 | + # |
| 104 | + # You do not need to specify the encryption context on decrypt |
| 105 | + # because the header of the encrypted message includes the encryption context. |
| 106 | + decrypted, decrypt_header = aws_encryption_sdk.decrypt(source=ciphertext, keyring=keyring) |
| 107 | + |
| 108 | + # Demonstrate that the decrypted plaintext is identical to the original plaintext. |
| 109 | + assert decrypted == source_plaintext |
| 110 | + |
| 111 | + # Verify that the encryption context used in the decrypt operation includes |
| 112 | + # the encryption context that you specified when encrypting. |
| 113 | + # The AWS Encryption SDK can add pairs, so don't require an exact match. |
| 114 | + # |
| 115 | + # In production, always use a meaningful encryption context. |
| 116 | + assert set(encryption_context.items()) <= set(decrypt_header.encryption_context.items()) |
0 commit comments