Skip to content

Commit

Permalink
chore: deprecate getMasterKeyIds() in CryptoResult (#1976)
Browse files Browse the repository at this point in the history
  • Loading branch information
imabhichow authored Feb 5, 2024
1 parent 800bd01 commit 1890ebb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public List<K> getMasterKeys() {
}

/** Convenience method for retrieving the keyIds in the results from {@link #getMasterKeys()}. */
@Deprecated
public List<String> getMasterKeyIds() {
final List<String> result = new ArrayList<>(masterKeys_.size());
for (final MasterKey<K> mk : masterKeys_) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,9 @@ public CiphertextHeaders getHeaders() {

@Override
public List<K> getMasterKeys() {
return Collections.singletonList(dataKey_.getMasterKey());
return dataKey_.getMasterKey() == null
? Collections.emptyList()
: Collections.singletonList(dataKey_.getMasterKey());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,21 @@ public void AwsKmsEncryptDecryptKeyring() {
crypto.encryptData(kmsKeyring, EXAMPLE_DATA, encryptionContext);

List<?> masterKeys = encryptResult.getMasterKeys();
List<String> masterKeyIds = encryptResult.getMasterKeyIds();
// Assert CryptoResult returns empty list if keyrings are used.
assert masterKeys.size() == 0;
assert masterKeys.isEmpty();
assert masterKeyIds.isEmpty();

final byte[] ciphertext = encryptResult.getResult();

// Decrypt the data
final CryptoResult<byte[], ?> decryptResult = crypto.decryptData(kmsKeyring, ciphertext);
assert masterKeys.size() == 0;

// Verify that the encryption context in the result contains the
// encryption context supplied to the encryptData method.
if (!encryptionContext.entrySet().stream()
.allMatch(e -> e.getValue().equals(decryptResult.getEncryptionContext().get(e.getKey())))) {
throw new IllegalStateException("Wrong Encryption Context!");
}
final CryptoResult<byte[], ?> decryptResult =
crypto.decryptData(kmsKeyring, ciphertext, encryptionContext);
masterKeys = decryptResult.getMasterKeys();
masterKeyIds = decryptResult.getMasterKeyIds();
// Assert CryptoResult returns empty list if keyrings are used.
assert masterKeys.isEmpty();
assert masterKeyIds.isEmpty();

// Verify that the decrypted plaintext matches the original plaintext
assert Arrays.equals(decryptResult.getResult(), EXAMPLE_DATA);
Expand Down

0 comments on commit 1890ebb

Please sign in to comment.