Skip to content

Commit f862c1a

Browse files
Adding back hasCleartextDataKey methods
1 parent 0d20979 commit f862c1a

File tree

11 files changed

+50
-29
lines changed

11 files changed

+50
-29
lines changed

src/main/java/com/amazonaws/encryptionsdk/keyrings/KmsKeyring.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,15 @@ public void onEncrypt(EncryptionMaterials encryptionMaterials) {
8080

8181
// If the input encryption materials do not contain a plaintext data key and this keyring does not
8282
// have a generator defined, OnEncrypt MUST not modify the encryption materials and MUST fail.
83-
if (encryptionMaterials.getCleartextDataKey() == null && generatorKeyId == null) {
83+
if (!encryptionMaterials.hasCleartextDataKey() && generatorKeyId == null) {
8484
throw new AwsCryptoException("Encryption materials must contain either a plaintext data key or a generator");
8585
}
8686

8787
final List<String> keyIdsToEncrypt = new ArrayList<>(keyIds);
8888

8989
// If the input encryption materials do not contain a plaintext data key and a generator is defined onEncrypt
9090
// MUST attempt to generate a new plaintext data key and encrypt that data key by calling KMS GenerateDataKey.
91-
if (encryptionMaterials.getCleartextDataKey() == null) {
91+
if (!encryptionMaterials.hasCleartextDataKey()) {
9292
generateDataKey(encryptionMaterials);
9393
} else if (generatorKeyId != null) {
9494
// If this keyring's generator is defined and was not used to generate a data key, OnEncrypt
@@ -126,7 +126,7 @@ public void onDecrypt(DecryptionMaterials decryptionMaterials, List<? extends En
126126
requireNonNull(decryptionMaterials, "decryptionMaterials are required");
127127
requireNonNull(encryptedDataKeys, "encryptedDataKeys are required");
128128

129-
if (decryptionMaterials.getCleartextDataKey() != null || encryptedDataKeys.isEmpty()) {
129+
if (decryptionMaterials.hasCleartextDataKey() || encryptedDataKeys.isEmpty()) {
130130
return;
131131
}
132132

src/main/java/com/amazonaws/encryptionsdk/keyrings/MultiKeyring.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void onEncrypt(EncryptionMaterials encryptionMaterials) {
5252
generatorKeyring.onEncrypt(encryptionMaterials);
5353
}
5454

55-
if (encryptionMaterials.getCleartextDataKey() == null) {
55+
if (!encryptionMaterials.hasCleartextDataKey()) {
5656
throw new AwsCryptoException("Either a generator keyring must be supplied that produces a cleartext " +
5757
"data key or a cleartext data key must already be present in the encryption materials.");
5858
}
@@ -67,7 +67,7 @@ public void onDecrypt(DecryptionMaterials decryptionMaterials, List<? extends En
6767
requireNonNull(decryptionMaterials, "decryptionMaterials are required");
6868
requireNonNull(encryptedDataKeys, "encryptedDataKeys are required");
6969

70-
if (decryptionMaterials.getCleartextDataKey() != null) {
70+
if (decryptionMaterials.hasCleartextDataKey()) {
7171
return;
7272
}
7373

@@ -85,7 +85,7 @@ public void onDecrypt(DecryptionMaterials decryptionMaterials, List<? extends En
8585
try {
8686
keyring.onDecrypt(decryptionMaterials, encryptedDataKeys);
8787

88-
if (decryptionMaterials.getCleartextDataKey() != null) {
88+
if (decryptionMaterials.hasCleartextDataKey()) {
8989
// Decryption succeeded, return immediately
9090
return;
9191
}

src/main/java/com/amazonaws/encryptionsdk/keyrings/RawKeyring.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ abstract class RawKeyring implements Keyring {
6868
public void onEncrypt(EncryptionMaterials encryptionMaterials) {
6969
requireNonNull(encryptionMaterials, "encryptionMaterials are required");
7070

71-
if (encryptionMaterials.getCleartextDataKey() == null) {
71+
if (!encryptionMaterials.hasCleartextDataKey()) {
7272
generateDataKey(encryptionMaterials);
7373
}
7474

@@ -84,7 +84,7 @@ public void onDecrypt(DecryptionMaterials decryptionMaterials, List<? extends En
8484
requireNonNull(decryptionMaterials, "decryptionMaterials are required");
8585
requireNonNull(encryptedDataKeys, "encryptedDataKeys are required");
8686

87-
if (decryptionMaterials.getCleartextDataKey() != null || encryptedDataKeys.isEmpty()) {
87+
if (decryptionMaterials.hasCleartextDataKey() || encryptedDataKeys.isEmpty()) {
8888
return;
8989
}
9090

src/main/java/com/amazonaws/encryptionsdk/model/DecryptionMaterials.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public DataKey<?> getDataKey() {
6060
* @param keyringTraceEntry The keyring trace entry recording this action.
6161
*/
6262
public void setCleartextDataKey(SecretKey cleartextDataKey, KeyringTraceEntry keyringTraceEntry) {
63-
if (this.dataKey != null) {
63+
if (hasCleartextDataKey()) {
6464
throw new IllegalStateException("cleartextDataKey was already populated");
6565
}
6666
requireNonNull(cleartextDataKey, "cleartextDataKey is required");
@@ -74,6 +74,15 @@ public SecretKey getCleartextDataKey() {
7474
return dataKey == null ? null : dataKey.getKey();
7575
}
7676

77+
/**
78+
* Returns true if a cleartext data key has been populated.
79+
*
80+
* @return True if cleartext data key is populated, false otherwise.
81+
*/
82+
public boolean hasCleartextDataKey() {
83+
return this.dataKey != null;
84+
}
85+
7786
public PublicKey getTrailingSignatureKey() {
7887
return trailingSignatureKey;
7988
}

src/main/java/com/amazonaws/encryptionsdk/model/EncryptionMaterials.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public SecretKey getCleartextDataKey() {
101101
* @param keyringTraceEntry The keyring trace entry recording this action.
102102
*/
103103
public void setCleartextDataKey(SecretKey cleartextDataKey, KeyringTraceEntry keyringTraceEntry) {
104-
if (this.cleartextDataKey != null) {
104+
if (hasCleartextDataKey()) {
105105
throw new IllegalStateException("cleartextDataKey was already populated");
106106
}
107107
requireNonNull(cleartextDataKey, "cleartextDataKey is required");
@@ -111,6 +111,15 @@ public void setCleartextDataKey(SecretKey cleartextDataKey, KeyringTraceEntry ke
111111
keyringTrace.add(keyringTraceEntry);
112112
}
113113

114+
/**
115+
* Returns true if a cleartext data key has been populated.
116+
*
117+
* @return True is a cleartext data key has been populated, false otherwise.
118+
*/
119+
public boolean hasCleartextDataKey() {
120+
return this.cleartextDataKey != null;
121+
}
122+
114123
/**
115124
* The private key to be used to sign the message trailer. Must be present if any only if required by the
116125
* crypto algorithm, and the key type must likewise match the algorithm in use.

src/test/java/com/amazonaws/encryptionsdk/keyrings/KmsKeyringTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
import static com.amazonaws.encryptionsdk.kms.KmsUtils.KMS_PROVIDER_ID;
4646
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4747
import static org.junit.jupiter.api.Assertions.assertEquals;
48-
import static org.junit.jupiter.api.Assertions.assertNull;
48+
import static org.junit.jupiter.api.Assertions.assertFalse;
4949
import static org.junit.jupiter.api.Assertions.assertThrows;
5050
import static org.junit.jupiter.api.Assertions.assertTrue;
5151
import static org.mockito.Mockito.when;
@@ -231,7 +231,7 @@ void testDiscoveryEncrypt() {
231231
.build();
232232
keyring.onEncrypt(encryptionMaterials);
233233

234-
assertNull(encryptionMaterials.getCleartextDataKey());
234+
assertFalse(encryptionMaterials.hasCleartextDataKey());
235235
assertEquals(0, encryptionMaterials.getKeyringTrace().getEntries().size());
236236
}
237237

@@ -344,7 +344,7 @@ void testDecryptNoDataKey() {
344344

345345
keyring.onDecrypt(decryptionMaterials, Collections.emptyList());
346346

347-
assertNull(decryptionMaterials.getCleartextDataKey());
347+
assertFalse(decryptionMaterials.hasCleartextDataKey());
348348
assertEquals(0, decryptionMaterials.getKeyringTrace().getEntries().size());
349349
}
350350

src/test/java/com/amazonaws/encryptionsdk/keyrings/MultiKeyringTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void testConstructor() {
6767
@Test
6868
void testOnEncryptWithGenerator() {
6969
MultiKeyring keyring = new MultiKeyring(generatorKeyring, childrenKeyrings);
70-
when(encryptionMaterials.getCleartextDataKey()).thenReturn(cleartextDataKey);
70+
when(encryptionMaterials.hasCleartextDataKey()).thenReturn(true);
7171

7272
keyring.onEncrypt(encryptionMaterials);
7373

@@ -79,7 +79,7 @@ void testOnEncryptWithGenerator() {
7979
@Test
8080
void testOnEncryptWithoutGenerator() {
8181
MultiKeyring keyring = new MultiKeyring(null, childrenKeyrings);
82-
when(encryptionMaterials.getCleartextDataKey()).thenReturn(cleartextDataKey);
82+
when(encryptionMaterials.hasCleartextDataKey()).thenReturn(true);
8383

8484
keyring.onEncrypt(encryptionMaterials);
8585

@@ -91,7 +91,7 @@ void testOnEncryptWithoutGenerator() {
9191
@Test
9292
void testOnEncryptNoPlaintextDataKey() {
9393
MultiKeyring keyring = new MultiKeyring(null, childrenKeyrings);
94-
when(encryptionMaterials.getCleartextDataKey()).thenReturn(null);
94+
when(encryptionMaterials.hasCleartextDataKey()).thenReturn(false);
9595

9696
assertThrows(AwsCryptoException.class, () -> keyring.onEncrypt(encryptionMaterials));
9797
}
@@ -100,7 +100,7 @@ void testOnEncryptNoPlaintextDataKey() {
100100
void testOnDecryptWithPlaintextDataKey() {
101101
MultiKeyring keyring = new MultiKeyring(generatorKeyring, childrenKeyrings);
102102

103-
when(decryptionMaterials.getCleartextDataKey()).thenReturn(cleartextDataKey);
103+
when(decryptionMaterials.hasCleartextDataKey()).thenReturn(true);
104104
keyring.onDecrypt(decryptionMaterials, encryptedDataKeys);
105105

106106
verifyNoInteractions(generatorKeyring, keyring1, keyring2);
@@ -110,7 +110,7 @@ void testOnDecryptWithPlaintextDataKey() {
110110
void testOnDecryptWithGenerator() {
111111
MultiKeyring keyring = new MultiKeyring(generatorKeyring, childrenKeyrings);
112112

113-
when(decryptionMaterials.getCleartextDataKey()).thenReturn(null).thenReturn(null).thenReturn(cleartextDataKey);
113+
when(decryptionMaterials.hasCleartextDataKey()).thenReturn(false).thenReturn(false).thenReturn(true);
114114
keyring.onDecrypt(decryptionMaterials, encryptedDataKeys);
115115

116116
InOrder inOrder = inOrder(generatorKeyring, keyring1);
@@ -123,7 +123,7 @@ void testOnDecryptWithGenerator() {
123123
void testOnDecryptWithoutGenerator() {
124124
MultiKeyring keyring = new MultiKeyring(null, childrenKeyrings);
125125

126-
when(decryptionMaterials.getCleartextDataKey()).thenReturn(null).thenReturn(null).thenReturn(cleartextDataKey);
126+
when(decryptionMaterials.hasCleartextDataKey()).thenReturn(false).thenReturn(false).thenReturn(true);
127127
keyring.onDecrypt(decryptionMaterials, encryptedDataKeys);
128128

129129
InOrder inOrder = inOrder(keyring1, keyring2);
@@ -136,7 +136,7 @@ void testOnDecryptWithoutGenerator() {
136136
void testOnDecryptFailureThenSuccess() {
137137
MultiKeyring keyring = new MultiKeyring(generatorKeyring, childrenKeyrings);
138138

139-
when(decryptionMaterials.getCleartextDataKey()).thenReturn(null).thenReturn(cleartextDataKey);
139+
when(decryptionMaterials.hasCleartextDataKey()).thenReturn(false).thenReturn(true);
140140
doThrow(new IllegalStateException()).when(generatorKeyring).onDecrypt(decryptionMaterials, encryptedDataKeys);
141141

142142
keyring.onDecrypt(decryptionMaterials, encryptedDataKeys);
@@ -151,7 +151,7 @@ void testOnDecryptFailureThenSuccess() {
151151
void testOnDecryptFailure() {
152152
MultiKeyring keyring = new MultiKeyring(generatorKeyring, childrenKeyrings);
153153

154-
when(decryptionMaterials.getCleartextDataKey()).thenReturn(null);
154+
when(decryptionMaterials.hasCleartextDataKey()).thenReturn(false);
155155
doThrow(new AwsCryptoException()).when(generatorKeyring).onDecrypt(decryptionMaterials, encryptedDataKeys);
156156
doThrow(new IllegalStateException()).when(keyring1).onDecrypt(decryptionMaterials, encryptedDataKeys);
157157
doThrow(new IllegalArgumentException()).when(keyring2).onDecrypt(decryptionMaterials, encryptedDataKeys);
@@ -176,7 +176,7 @@ void testOnDecryptFailure() {
176176
void testOnDecryptNoFailuresNoPlaintextDataKeys() {
177177
MultiKeyring keyring = new MultiKeyring(generatorKeyring, childrenKeyrings);
178178

179-
when(decryptionMaterials.getCleartextDataKey()).thenReturn(null, null, null, null);
179+
when(decryptionMaterials.hasCleartextDataKey()).thenReturn(false, false, false, false);
180180
keyring.onDecrypt(decryptionMaterials, encryptedDataKeys);
181181

182182
InOrder inOrder = inOrder(generatorKeyring, keyring1, keyring2);

src/test/java/com/amazonaws/encryptionsdk/keyrings/RawKeyringTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
import static com.amazonaws.encryptionsdk.internal.RandomBytesGenerator.generate;
3838
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
3939
import static org.junit.jupiter.api.Assertions.assertEquals;
40-
import static org.junit.jupiter.api.Assertions.assertNotNull;
41-
import static org.junit.jupiter.api.Assertions.assertNull;
40+
import static org.junit.jupiter.api.Assertions.assertFalse;
41+
import static org.junit.jupiter.api.Assertions.assertTrue;
4242
import static org.mockito.ArgumentMatchers.eq;
4343
import static org.mockito.Mockito.when;
4444

@@ -114,7 +114,7 @@ void testEncryptNullDataKey() {
114114
assertEquals(encryptionMaterials.getCleartextDataKey().getAlgorithm(), ALGORITHM.getDataKeyAlgo());
115115
assertArrayEquals(encryptionMaterials.getCleartextDataKey().getEncoded(), dataKeyCaptor.getValue());
116116
assertEquals(1, encryptionMaterials.getEncryptedDataKeys().size());
117-
assertNotNull(encryptionMaterials.getCleartextDataKey());
117+
assertTrue(encryptionMaterials.hasCleartextDataKey());
118118
assertEncryptedDataKeyEquals(ENCRYPTED_DATA_KEY, encryptionMaterials.getEncryptedDataKeys().get(0));
119119
assertEquals(2, encryptionMaterials.getKeyringTrace().getEntries().size());
120120
assertEquals(GENERATED_DATA_KEY_TRACE, encryptionMaterials.getKeyringTrace().getEntries().get(0));
@@ -146,7 +146,7 @@ void testDecryptNoValidDataKey() {
146146

147147
keyring.onDecrypt(decryptionMaterials, Collections.singletonList(INVALID_DATA_KEY));
148148

149-
assertNull(decryptionMaterials.getCleartextDataKey());
149+
assertFalse(decryptionMaterials.hasCleartextDataKey());
150150
assertEquals(0, decryptionMaterials.getKeyringTrace().getEntries().size());
151151
}
152152

@@ -160,7 +160,7 @@ void testDecryptNoDataKey() {
160160

161161
keyring.onDecrypt(decryptionMaterials, Collections.emptyList());
162162

163-
assertNull(decryptionMaterials.getCleartextDataKey());
163+
assertFalse(decryptionMaterials.hasCleartextDataKey());
164164
assertEquals(0, decryptionMaterials.getKeyringTrace().getEntries().size());
165165
}
166166

src/test/java/com/amazonaws/encryptionsdk/keyrings/RawRsaKeyringTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
3434
import static org.junit.jupiter.api.Assertions.assertEquals;
3535
import static org.junit.jupiter.api.Assertions.assertFalse;
36-
import static org.junit.jupiter.api.Assertions.assertNotNull;
3736
import static org.junit.jupiter.api.Assertions.assertTrue;
3837

3938
class RawRsaKeyringTest {
@@ -109,7 +108,7 @@ void testEncryptDecryptGenerateDataKey() {
109108

110109
keyring.onEncrypt(encryptionMaterials);
111110

112-
assertNotNull(encryptionMaterials.getCleartextDataKey());
111+
assertTrue(encryptionMaterials.hasCleartextDataKey());
113112
assertEquals(encryptionMaterials.getCleartextDataKey().getAlgorithm(), ALGORITHM.getDataKeyAlgo());
114113
assertEquals(1, encryptionMaterials.getEncryptedDataKeys().size());
115114

src/test/java/com/amazonaws/encryptionsdk/model/DecryptionMaterialsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import static com.amazonaws.encryptionsdk.internal.RandomBytesGenerator.generate;
3333
import static org.junit.jupiter.api.Assertions.assertEquals;
34+
import static org.junit.jupiter.api.Assertions.assertFalse;
3435
import static org.junit.jupiter.api.Assertions.assertNotSame;
3536
import static org.junit.jupiter.api.Assertions.assertNull;
3637
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -129,6 +130,7 @@ void testGetOptionalProperties() {
129130

130131
assertNull(materials.getAlgorithm());
131132
assertNull(materials.getCleartextDataKey());
133+
assertFalse(materials.hasCleartextDataKey());
132134
assertNull(materials.getTrailingSignatureKey());
133135
assertTrue(materials.getEncryptionContext().isEmpty());
134136
assertTrue(materials.getKeyringTrace().getEntries().isEmpty());

src/test/java/com/amazonaws/encryptionsdk/model/EncryptionMaterialsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import static com.amazonaws.encryptionsdk.internal.RandomBytesGenerator.generate;
3636
import static org.junit.jupiter.api.Assertions.assertEquals;
37+
import static org.junit.jupiter.api.Assertions.assertFalse;
3738
import static org.junit.jupiter.api.Assertions.assertNotSame;
3839
import static org.junit.jupiter.api.Assertions.assertNull;
3940
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -154,6 +155,7 @@ void testGetOptionalProperties() {
154155

155156
assertNull(materials.getAlgorithm());
156157
assertNull(materials.getCleartextDataKey());
158+
assertFalse(materials.hasCleartextDataKey());
157159
assertTrue(materials.getEncryptedDataKeys().isEmpty());
158160
assertNull(materials.getTrailingSignatureKey());
159161
assertTrue(materials.getKeyringTrace().getEntries().isEmpty());

0 commit comments

Comments
 (0)