Allow empty plaintext for transit encryption.
We now allow construction and encryption of empty Plaintext objects. Empty plaintext can make sense from a security perspective. Closes gh-223.
This commit is contained in:
@@ -18,7 +18,6 @@ package org.springframework.vault.support;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* Value object representing plaintext with an optional {@link VaultTransitContext}.
|
||||
@@ -44,13 +43,12 @@ public class Plaintext {
|
||||
/**
|
||||
* Factory method to create {@link Plaintext} from a byte sequence.
|
||||
*
|
||||
* @param plaintext the plaintext to encrypt, must not be {@literal null} or empty.
|
||||
* @param plaintext the plaintext to encrypt, must not be {@literal null}.
|
||||
* @return the {@link Plaintext} for {@code plaintext}.
|
||||
*/
|
||||
public static Plaintext of(byte[] plaintext) {
|
||||
|
||||
Assert.isTrue(!ObjectUtils.isEmpty(plaintext),
|
||||
"Plaintext must not be null or empty");
|
||||
Assert.notNull(plaintext, "Plaintext must not be null");
|
||||
|
||||
return new Plaintext(plaintext, VaultTransitContext.empty());
|
||||
}
|
||||
@@ -60,12 +58,12 @@ public class Plaintext {
|
||||
* {@link String} is encoded to {@code byte} using the default
|
||||
* {@link java.nio.charset.Charset}.
|
||||
*
|
||||
* @param plaintext the plaintext to encrypt, must not be {@literal null} or empty.
|
||||
* @param plaintext the plaintext to encrypt, must not be {@literal null}.
|
||||
* @return the {@link Plaintext} for {@code plaintext}.
|
||||
*/
|
||||
public static Plaintext of(String plaintext) {
|
||||
|
||||
Assert.hasText(plaintext, "Plaintext must not be null or empty");
|
||||
Assert.notNull(plaintext, "Plaintext must not be null");
|
||||
|
||||
return of(plaintext.getBytes());
|
||||
}
|
||||
|
||||
@@ -293,6 +293,24 @@ public class VaultTransitTemplateIntegrationTests extends IntegrationTestSupport
|
||||
assertThat(ciphertext).startsWith("vault:v1:");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encryptShouldEncryptEmptyValues() {
|
||||
|
||||
transitOperations.createKey("mykey", VaultTransitKeyCreationRequest.builder()
|
||||
.convergentEncryption(true).derived(true).build());
|
||||
|
||||
VaultTransitContext context = VaultTransitContext.builder()
|
||||
.context("blubb".getBytes()) //
|
||||
.nonce("123456789012".getBytes()) //
|
||||
.build();
|
||||
|
||||
Ciphertext ciphertext = transitOperations.encrypt("mykey",
|
||||
Plaintext.of("").with(context));
|
||||
|
||||
assertThat(ciphertext.getCiphertext()).startsWith("vault:v1:");
|
||||
assertThat(ciphertext.getContext()).isEqualTo(context);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encryptShouldCreateWrappedCiphertextWithNonceAndContext() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user