Allow empty plaintext for transit decryption.

Fixes gh-223.
Original pull request: gh-225.
This commit is contained in:
Mikko Koli
2018-03-21 15:43:19 +02:00
committed by Mark Paluch
parent bef6576c00
commit 8ebd492db6
2 changed files with 45 additions and 1 deletions

View File

@@ -532,10 +532,12 @@ public class VaultTransitTemplate implements VaultTransitOperations {
encrypted = new VaultDecryptionResult(new VaultException(
data.get("error")));
}
else {
else if (StringUtils.hasText(data.get("plaintext"))) {
encrypted = new VaultDecryptionResult(toPlaintext(
Base64Utils.decodeFromString(data.get("plaintext")),
ciphertext.getContext()));
} else {
encrypted = new VaultDecryptionResult(toPlaintext("",ciphertext.getContext()));
}
}
else {
@@ -559,6 +561,11 @@ public class VaultTransitTemplate implements VaultTransitOperations {
.of(plaintext);
}
private static Plaintext toPlaintext(String plaintext, VaultTransitContext context) {
return context != null ? Plaintext.of(plaintext).with(context) : Plaintext
.of(plaintext);
}
@SuppressWarnings("unchecked")
private static List<Map<String, String>> getBatchData(VaultResponse vaultResponse) {
return (List<Map<String, String>>) vaultResponse.getRequiredData().get(

View File

@@ -547,6 +547,43 @@ public class VaultTransitTemplateIntegrationTests extends IntegrationTestSupport
}
@Test
public void shouldBatchDecryptEmptyPlaintext() {
assumeTrue(vaultVersion.isGreaterThanOrEqualTo(BATCH_INTRODUCED_IN_VERSION));
transitOperations.createKey("mykey");
Ciphertext empty = transitOperations.encrypt("mykey", Plaintext.of(""));
List<VaultDecryptionResult> decrypted = transitOperations.decrypt("mykey",
Arrays.asList(empty));
assertThat(decrypted.get(0).getAsString()).isEqualTo("");
}
@Test
public void shouldBatchDecryptEmpltyPlaintextWithContext() {
assumeTrue(vaultVersion.isGreaterThanOrEqualTo(BATCH_INTRODUCED_IN_VERSION));
VaultTransitKeyCreationRequest request = VaultTransitKeyCreationRequest.builder() //
.derived(true) //
.build();
transitOperations.createKey("mykey", request);
Plaintext empty = Plaintext.of("").with(
VaultTransitContext.builder().context("oneContext".getBytes()).build());
List<VaultEncryptionResult> encrypted = transitOperations.encrypt("mykey",
Arrays.asList(empty));
List<VaultDecryptionResult> decrypted = transitOperations.decrypt("mykey",
Arrays.asList(encrypted.get(0).get()));
assertThat(decrypted.get(0).get()).isEqualTo(empty);
}
@Test
public void generateHmacShouldCreateHmac() {
assumeTrue(vaultVersion.isGreaterThanOrEqualTo(SIGN_VERIFY_INTRODUCED_IN_VERSION));