Skip to content

Commit 92eef9f

Browse files
nbaarsmp911de
authored andcommitted
Add plaintext backup and convergent encryption support and version to Vault transit keys.
Closes gh-661 Original pull request: gh-793
1 parent 13bfce1 commit 92eef9f

File tree

4 files changed

+85
-6
lines changed

4 files changed

+85
-6
lines changed

spring-vault-core/src/main/java/org/springframework/vault/core/VaultTransitTemplate.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
package org.springframework.vault.core;
1717

1818
import com.fasterxml.jackson.annotation.JsonProperty;
19-
import org.jetbrains.annotations.NotNull;
20-
2119
import org.springframework.lang.Nullable;
2220
import org.springframework.util.Assert;
2321
import org.springframework.util.ObjectUtils;
@@ -598,6 +596,15 @@ static class VaultTransitKeyImpl implements VaultTransitKey {
598596
@JsonProperty("supports_signing")
599597
private boolean supportsSigning;
600598

599+
@JsonProperty("allow_plaintext_backup")
600+
private boolean allowPlaintextBackup;
601+
602+
@JsonProperty("convergent_encryption")
603+
private boolean supportsConvergentEncryption;
604+
605+
@JsonProperty("convergent_encryption_version")
606+
private int convergentVersion;
607+
601608
public VaultTransitKeyImpl() {
602609
}
603610

@@ -631,6 +638,21 @@ public boolean supportsSigning() {
631638
return isSupportsSigning();
632639
}
633640

641+
@Override
642+
public boolean allowPlaintextBackup() {
643+
return isAllowPlaintextBackup();
644+
}
645+
646+
@Override
647+
public boolean supportsConvergentEncryption() {
648+
return isSupportsConvergentEncryption();
649+
}
650+
651+
@Override
652+
public int getConvergentVersion() {
653+
return this.convergentVersion;
654+
}
655+
634656
@Nullable
635657
public String getName() {
636658
return this.name;
@@ -668,6 +690,10 @@ public int getMinEncryptionVersion() {
668690
return this.minEncryptionVersion;
669691
}
670692

693+
public boolean isAllowPlaintextBackup() {
694+
return this.allowPlaintextBackup;
695+
}
696+
671697
public boolean isSupportsDecryption() {
672698
return this.supportsDecryption;
673699
}
@@ -684,6 +710,10 @@ public boolean isSupportsSigning() {
684710
return this.supportsSigning;
685711
}
686712

713+
public boolean isSupportsConvergentEncryption() {
714+
return this.supportsConvergentEncryption;
715+
}
716+
687717
public void setName(@Nullable String name) {
688718
this.name = name;
689719
}
@@ -756,15 +786,17 @@ public boolean equals(Object o) {
756786
&& this.supportsDerivation == that.supportsDerivation
757787
&& this.supportsSigning == that.supportsSigning && Objects.equals(this.name, that.name)
758788
&& this.cipherMode.equals(that.cipherMode) && Objects.equals(this.type, that.type)
759-
&& this.keys.equals(that.keys);
789+
&& this.allowPlaintextBackup == that.allowPlaintextBackup
790+
&& this.supportsConvergentEncryption == that.supportsConvergentEncryption;
760791
}
761792

762793
@Override
763794
public int hashCode() {
764795
return Objects.hash(this.name, this.cipherMode, this.type, this.deletionAllowed, this.derived,
765796
this.exportable, this.keys, this.latestVersion, this.minDecryptionVersion,
766797
this.minEncryptionVersion, this.supportsDecryption, this.supportsEncryption,
767-
this.supportsDerivation, this.supportsSigning);
798+
this.supportsDerivation, this.supportsSigning, this.allowPlaintextBackup,
799+
this.supportsConvergentEncryption);
768800
}
769801

770802
}

spring-vault-core/src/main/java/org/springframework/vault/support/VaultTransitKey.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,23 @@ public interface VaultTransitKey {
9696
*/
9797
boolean supportsSigning();
9898

99+
/**
100+
* @return if set, enables taking backup of named key in the plaintext format. Once
101+
* set, this cannot be disabled.
102+
*/
103+
boolean allowPlaintextBackup();
104+
105+
/**
106+
* @return If enabled, the key will support convergent encryption, where the same
107+
* plaintext creates the same ciphertext. This requires 'derived' to be set to true.
108+
*/
109+
boolean supportsConvergentEncryption();
110+
111+
/**
112+
* @return the version of the convergent nonce to use. Note: since version 3 the
113+
* algorithm used in `transit`'s convergent encryption returns -1 since the version is
114+
* stored with the key. For backwards compatability this field might be interesting.
115+
*/
116+
int getConvergentVersion();
117+
99118
}

spring-vault-core/src/main/java/org/springframework/vault/support/VaultTransitKeyCreationRequest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@ public class VaultTransitKeyCreationRequest {
3737

3838
private final boolean exportable;
3939

40+
@JsonProperty("allow_plaintext_backup")
41+
private final boolean allowPlaintextBackup;
42+
4043
private VaultTransitKeyCreationRequest(boolean derived, String type, boolean convergentEncryption,
41-
boolean exportable) {
44+
boolean exportable, boolean allowPlaintextBackup) {
4245
this.derived = derived;
4346
this.type = type;
4447
this.convergentEncryption = convergentEncryption;
4548
this.exportable = exportable;
49+
this.allowPlaintextBackup = allowPlaintextBackup;
4650
}
4751

4852
/**
@@ -106,6 +110,8 @@ public static class VaultTransitKeyCreationRequestBuilder {
106110

107111
private boolean exportable;
108112

113+
private boolean allowPlaintextBackup;
114+
109115
VaultTransitKeyCreationRequestBuilder() {
110116
}
111117

@@ -160,6 +166,12 @@ public VaultTransitKeyCreationRequestBuilder exportable(boolean exportable) {
160166
return this;
161167
}
162168

169+
public VaultTransitKeyCreationRequestBuilder allowPlaintextBackup(boolean allowPlaintextBackup) {
170+
171+
this.allowPlaintextBackup = allowPlaintextBackup;
172+
return this;
173+
}
174+
163175
/**
164176
* Build a new {@link VaultTransitKeyCreationRequest} instance. Requires
165177
* {@link #type(String)} to be configured.
@@ -170,7 +182,7 @@ public VaultTransitKeyCreationRequest build() {
170182
Assert.hasText(this.type, "Type must not be empty");
171183

172184
return new VaultTransitKeyCreationRequest(this.derived, this.type, this.convergentEncryption,
173-
this.exportable);
185+
this.exportable, this.allowPlaintextBackup);
174186
}
175187

176188
}

spring-vault-core/src/test/java/org/springframework/vault/core/VaultTransitTemplateIntegrationTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,22 @@ void createKeyShouldCreateKeyWithOptions() {
232232
assertThat(mykey.isDerived()).isTrue();
233233
assertThat(mykey.getMinDecryptionVersion()).isEqualTo(1);
234234
assertThat(mykey.getLatestVersion()).isEqualTo(1);
235+
assertThat(mykey.supportsConvergentEncryption()).isTrue();
236+
assertThat(mykey.getConvergentVersion()).isEqualTo(-1);
237+
}
238+
239+
@Test
240+
void createKeyWithPlaintextBackupOption() {
241+
VaultTransitKeyCreationRequest request = VaultTransitKeyCreationRequest.builder() //
242+
.allowPlaintextBackup(true) //
243+
.build();
244+
245+
this.transitOperations.createKey("mykey", request);
246+
247+
VaultTransitKey mykey = this.transitOperations.getKey("mykey");
248+
249+
assertThat(mykey.getName()).isEqualTo("mykey");
250+
assertThat(mykey.allowPlaintextBackup()).isTrue();
235251
}
236252

237253
@Test

0 commit comments

Comments
 (0)