diff --git a/src/examples/README.md b/src/examples/README.md index c8ae480b7..5e99106a9 100644 --- a/src/examples/README.md +++ b/src/examples/README.md @@ -39,6 +39,8 @@ We start with AWS KMS examples, then show how to use other wrapping keys. * [with keyrings](./java/com/amazonaws/crypto/examples/keyring/awskms/DiscoveryDecryptInRegionOnly.java) * How to decrypt with a preferred region but failover to others * [with keyrings](./java/com/amazonaws/crypto/examples/keyring/awskms/DiscoveryDecryptWithPreferredRegions.java) + * How to reproduce the behavior of an AWS KMS master key provider + * [with keyrings](./java/com/amazonaws/crypto/examples/keyring/awskms/ActLikeAwsKmsMasterKeyProvider.java) * Using raw wrapping keys * How to use a raw AES wrapping key * [with keyrings](./java/com/amazonaws/crypto/examples/keyring/rawaes/RawAes.java) diff --git a/src/examples/java/com/amazonaws/crypto/examples/keyring/awskms/ActLikeAwsKmsMasterKeyProvider.java b/src/examples/java/com/amazonaws/crypto/examples/keyring/awskms/ActLikeAwsKmsMasterKeyProvider.java new file mode 100644 index 000000000..c6ac3b86e --- /dev/null +++ b/src/examples/java/com/amazonaws/crypto/examples/keyring/awskms/ActLikeAwsKmsMasterKeyProvider.java @@ -0,0 +1,143 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.amazonaws.crypto.examples.keyring.awskms; + +import com.amazonaws.encryptionsdk.AwsCrypto; +import com.amazonaws.encryptionsdk.AwsCryptoResult; +import com.amazonaws.encryptionsdk.DecryptRequest; +import com.amazonaws.encryptionsdk.EncryptRequest; +import com.amazonaws.encryptionsdk.keyrings.Keyring; +import com.amazonaws.encryptionsdk.keyrings.StandardKeyrings; +import com.amazonaws.encryptionsdk.kms.AwsKmsCmkId; +import com.amazonaws.encryptionsdk.kms.KmsMasterKeyProvider; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static java.util.stream.Collectors.toList; + +/** + * 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/awskms' directory.
+ */
+public class ActLikeAwsKmsMasterKeyProvider {
+
+ /**
+ * Demonstrate how to create a keyring that behaves like an AWS KMS master key provider.
+ *
+ * @param awsKmsCmk The ARN of an AWS KMS CMK that protects data keys
+ * @param awsKmsAdditionalCmks Additional ARNs of secondary AWS KMS CMKs
+ * @param sourcePlaintext Plaintext to encrypt
+ */
+ public static void run(final AwsKmsCmkId awsKmsCmk, final List
* This example shows how to configure and use an AWS KMS discovery keyring.
*
diff --git a/src/examples/java/com/amazonaws/crypto/examples/keyring/awskms/DiscoveryDecryptInRegionOnly.java b/src/examples/java/com/amazonaws/crypto/examples/keyring/awskms/DiscoveryDecryptInRegionOnly.java
index 3e2aa987f..c6e7824fd 100644
--- a/src/examples/java/com/amazonaws/crypto/examples/keyring/awskms/DiscoveryDecryptInRegionOnly.java
+++ b/src/examples/java/com/amazonaws/crypto/examples/keyring/awskms/DiscoveryDecryptInRegionOnly.java
@@ -25,8 +25,13 @@
* However, sometimes you need more flexibility on decrypt,
* especially if you don't know which CMK was used to encrypt a message.
* To address this need, you can use an AWS KMS discovery keyring.
- * The AWS KMS discovery keyring does nothing on encrypt
- * but attempts to decrypt *any* data keys that were encrypted under an AWS KMS CMK.
+ * The AWS KMS discovery keyring does nothing on encrypt.
+ * On decrypt it reviews each encrypted data key (EDK).
+ * If an EDK was encrypted under an AWS KMS CMK,
+ * the AWS KMS discovery keyring attempts to decrypt it.
+ * Whether decryption succeeds depends on permissions on the CMK.
+ * This continues until the AWS KMS discovery keyring either runs out of EDKs
+ * or succeeds in decrypting an EDK.
*
* However, sometimes you need to be a *bit* more restrictive than that.
* To address this need, you can use a client supplier that restricts the regions an AWS KMS keyring can talk to.
diff --git a/src/examples/java/com/amazonaws/crypto/examples/keyring/awskms/DiscoveryDecryptWithPreferredRegions.java b/src/examples/java/com/amazonaws/crypto/examples/keyring/awskms/DiscoveryDecryptWithPreferredRegions.java
index 1826dafe4..2419580d4 100644
--- a/src/examples/java/com/amazonaws/crypto/examples/keyring/awskms/DiscoveryDecryptWithPreferredRegions.java
+++ b/src/examples/java/com/amazonaws/crypto/examples/keyring/awskms/DiscoveryDecryptWithPreferredRegions.java
@@ -25,8 +25,13 @@
* However, sometimes you need more flexibility on decrypt,
* especially if you might not know beforehand which CMK was used to encrypt a message.
* To address this need, you can use an AWS KMS discovery keyring.
- * The AWS KMS discovery keyring will do nothing on encrypt
- * but will attempt to decrypt *any* data keys that were encrypted under an AWS KMS CMK.
+ * The AWS KMS discovery keyring does nothing on encrypt.
+ * On decrypt it reviews each encrypted data key (EDK).
+ * If an EDK was encrypted under an AWS KMS CMK,
+ * the AWS KMS discovery keyring attempts to decrypt it.
+ * Whether decryption succeeds depends on permissions on the CMK.
+ * This continues until the AWS KMS discovery keyring either runs out of EDKs
+ * or succeeds in decrypting an EDK.
*
* However, sometimes you need to be a *bit* more restrictive than that.
* To address this need, you can use a client supplier to restrict what regions an AWS KMS keyring can talk to.