Skip to content

Commit 1a85fb5

Browse files
Updating based on python example updates
1 parent d00281b commit 1a85fb5

File tree

4 files changed

+68
-23
lines changed

4 files changed

+68
-23
lines changed

src/examples/java/com/amazonaws/crypto/examples/keyring/awskms/ActLikeAwsKmsMasterKeyProvider.java

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,36 @@
1212
import com.amazonaws.encryptionsdk.kms.AwsKmsCmkId;
1313
import com.amazonaws.encryptionsdk.kms.KmsMasterKeyProvider;
1414

15+
import java.util.ArrayList;
1516
import java.util.Arrays;
1617
import java.util.HashMap;
18+
import java.util.List;
1719
import java.util.Map;
1820

21+
import static java.util.stream.Collectors.toList;
22+
1923
/**
20-
* In earlier versions of the AWS Encryption SDK, you used master key providers to determine how your data keys are protected.
24+
* You might have used master key providers to protect your data keys
25+
* in an earlier version of the AWS Encryption SDK.
26+
* This example shows how to configure a keyring that behaves like an AWS KMS master key provider.
2127
* <p>
22-
* The AWS Encryption SDK provided an AWS KMS master key provider for interacting with AWS Key Management Service (AWS KMS).
23-
* Like the AWS KMS keyring,
24-
* the AWS KMS master key provider encrypts with all CMKs that you identify,
25-
* but unlike the AWS KMS keyring,
26-
* the AWS KMS master key provider always attempts to decrypt
27-
* *any* data keys that were encrypted under an AWS KMS CMK.
28+
* The AWS Encryption SDK provided an AWS KMS master key provider for
29+
* interacting with AWS Key Management Service (AWS KMS).
30+
* On encrypt, the AWS KMS master key provider behaves like the AWS KMS keyring
31+
* and encrypts with all CMKs that you identify.
32+
* However, on decrypt,
33+
* the AWS KMS master key provider reviews each encrypted data key (EDK).
34+
* If the EDK was encrypted under an AWS KMS CMK,
35+
* the AWS KMS master key provider attempts to decrypt it.
36+
* Whether decryption succeeds depends on permissions on the CMK.
37+
* This continues until the AWS KMS master key provider either runs out of EDKs
38+
* or succeeds in decrypting an EDK.
2839
* We have found that separating these two behaviors
2940
* makes the expected behavior clearer,
3041
* so that is what we did with the AWS KMS keyring and the AWS KMS discovery keyring.
3142
* However, as you migrate from master key providers to keyrings,
3243
* you might want a keyring that behaves like the AWS KMS master key provider.
3344
* <p>
34-
* This example shows how to configure a keyring that behaves like an AWS KMS master key provider.
35-
* <p>
3645
* For more examples of how to use the AWS KMS keyring,
3746
* see the 'keyring/awskms' directory.
3847
*/
@@ -41,10 +50,11 @@ public class ActLikeAwsKmsMasterKeyProvider {
4150
/**
4251
* Demonstrate how to create a keyring that behaves like an AWS KMS master key provider.
4352
*
44-
* @param awsKmsCmk The ARN of an AWS KMS CMK that protects data keys
45-
* @param sourcePlaintext Plaintext to encrypt
53+
* @param awsKmsCmk The ARN of an AWS KMS CMK that protects data keys
54+
* @param awsKmsAdditionalCmks Additional ARNs of secondary AWS KMS CMKs
55+
* @param sourcePlaintext Plaintext to encrypt
4656
*/
47-
public static void run(final AwsKmsCmkId awsKmsCmk, final byte[] sourcePlaintext) {
57+
public static void run(final AwsKmsCmkId awsKmsCmk, final List<AwsKmsCmkId> awsKmsAdditionalCmks, byte[] sourcePlaintext) {
4858
// Instantiate the AWS Encryption SDK.
4959
final AwsCrypto awsEncryptionSdk = new AwsCrypto();
5060

@@ -59,22 +69,42 @@ public static void run(final AwsKmsCmkId awsKmsCmk, final byte[] sourcePlaintext
5969

6070
// This is the master key provider whose behavior we want to reproduce.
6171
//
62-
// When encrypting, this master key provider uses only the specified `aws_kms_cmk`.
72+
// When encrypting, this master key provider generates the data key using the first CMK in the list
73+
// and encrypts the data key using all specified CMKs.
6374
// However, when decrypting, this master key provider attempts to decrypt
6475
// any data keys that were encrypted under an AWS KMS CMK.
76+
final List<String> masterKeyProviderCmks = new ArrayList<>();
77+
masterKeyProviderCmks.add(awsKmsCmk.toString());
78+
masterKeyProviderCmks.addAll(awsKmsAdditionalCmks.stream().map(AwsKmsCmkId::toString).collect(toList()));
6579
final KmsMasterKeyProvider masterKeyProviderToReplicate = KmsMasterKeyProvider.builder()
66-
.withKeysForEncryption(awsKmsCmk.toString()).build();
80+
.withKeysForEncryption(masterKeyProviderCmks).build();
6781

68-
// Create a single-CMK keyring that encrypts and decrypts using a single AWS KMS CMK.
69-
final Keyring singleCmkKeyring = StandardKeyrings.awsKms(awsKmsCmk);
82+
// Create a CMK keyring that encrypts and decrypts using the specified AWS KMS CMKs.
83+
//
84+
// This keyring reproduces the encryption behavior of the AWS KMS master key provider.
85+
//
86+
// The AWS KMS keyring requires that you explicitly identify the CMK
87+
// that you want the keyring to use to generate the data key.
88+
final Keyring cmkKeyring = StandardKeyrings.awsKmsBuilder()
89+
.generatorKeyId(awsKmsCmk)
90+
.keyIds(awsKmsAdditionalCmks)
91+
.build();
7092

7193
// Create an AWS KMS discovery keyring that will attempt to decrypt
7294
// any data keys that were encrypted under an AWS KMS CMK.
7395
final Keyring discoveryKeyring = StandardKeyrings.awsKmsDiscoveryBuilder().build();
7496

7597
// Combine the single-CMK and discovery keyrings
7698
// to create a keyring that behaves like an AWS KMS master key provider.
77-
final Keyring keyring = StandardKeyrings.multi(singleCmkKeyring, discoveryKeyring);
99+
//
100+
// The CMK keyring reproduces the encryption behavior
101+
// and the discovery keyring reproduces the decryption behavior.
102+
// This also means that it does not matter if the CMK keyring fails on decrypt,
103+
// for example if you configured it with aliases which would work on encrypt
104+
// but fail to match any encrypted data keys on decrypt,
105+
// because the discovery keyring attempts to decrypt any AWS KMS-encrypted
106+
// data keys that it finds.
107+
final Keyring keyring = StandardKeyrings.multi(cmkKeyring, discoveryKeyring);
78108

79109
// Encrypt your plaintext data.
80110
final AwsCryptoResult<byte[]> encryptResult = awsEncryptionSdk.encrypt(

src/examples/java/com/amazonaws/crypto/examples/keyring/awskms/DiscoveryDecrypt.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@
2121
* However, sometimes you need more flexibility on decrypt,
2222
* especially when you don't know which CMKs were used to encrypt a message.
2323
* To address this need, you can use an AWS KMS discovery keyring.
24-
* The AWS KMS discovery keyring does nothing on encrypt
25-
* but attempts to decrypt *any* data keys that were encrypted under an AWS KMS CMK.
24+
* The AWS KMS discovery keyring does nothing on encrypt.
25+
* On decrypt it reviews each encrypted data key (EDK).
26+
* If an EDK was encrypted under an AWS KMS CMK,
27+
* the AWS KMS discovery keyring attempts to decrypt it.
28+
* Whether decryption succeeds depends on permissions on the CMK.
29+
* This continues until the AWS KMS discovery keyring either runs out of EDKs
30+
* or succeeds in decrypting an EDK.
2631
* <p>
2732
* This example shows how to configure and use an AWS KMS discovery keyring.
2833
* <p>

src/examples/java/com/amazonaws/crypto/examples/keyring/awskms/DiscoveryDecryptInRegionOnly.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,13 @@
2525
* However, sometimes you need more flexibility on decrypt,
2626
* especially if you don't know which CMK was used to encrypt a message.
2727
* To address this need, you can use an AWS KMS discovery keyring.
28-
* The AWS KMS discovery keyring does nothing on encrypt
29-
* but attempts to decrypt *any* data keys that were encrypted under an AWS KMS CMK.
28+
* The AWS KMS discovery keyring does nothing on encrypt.
29+
* On decrypt it reviews each encrypted data key (EDK).
30+
* If an EDK was encrypted under an AWS KMS CMK,
31+
* the AWS KMS discovery keyring attempts to decrypt it.
32+
* Whether decryption succeeds depends on permissions on the CMK.
33+
* This continues until the AWS KMS discovery keyring either runs out of EDKs
34+
* or succeeds in decrypting an EDK.
3035
* <p>
3136
* However, sometimes you need to be a *bit* more restrictive than that.
3237
* To address this need, you can use a client supplier that restricts the regions an AWS KMS keyring can talk to.

src/examples/java/com/amazonaws/crypto/examples/keyring/awskms/DiscoveryDecryptWithPreferredRegions.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,13 @@
2525
* However, sometimes you need more flexibility on decrypt,
2626
* especially if you might not know beforehand which CMK was used to encrypt a message.
2727
* To address this need, you can use an AWS KMS discovery keyring.
28-
* The AWS KMS discovery keyring will do nothing on encrypt
29-
* but will attempt to decrypt *any* data keys that were encrypted under an AWS KMS CMK.
28+
* The AWS KMS discovery keyring does nothing on encrypt.
29+
* On decrypt it reviews each encrypted data key (EDK).
30+
* If an EDK was encrypted under an AWS KMS CMK,
31+
* the AWS KMS discovery keyring attempts to decrypt it.
32+
* Whether decryption succeeds depends on permissions on the CMK.
33+
* This continues until the AWS KMS discovery keyring either runs out of EDKs
34+
* or succeeds in decrypting an EDK.
3035
* <p>
3136
* However, sometimes you need to be a *bit* more restrictive than that.
3237
* To address this need, you can use a client supplier to restrict what regions an AWS KMS keyring can talk to.

0 commit comments

Comments
 (0)