Skip to content

Commit 8723f9b

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 270b2df commit 8723f9b

File tree

4 files changed

+85
-4
lines changed

4 files changed

+85
-4
lines changed

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,15 @@ static class VaultTransitKeyImpl implements VaultTransitKey {
574574
@JsonProperty("supports_signing")
575575
private boolean supportsSigning;
576576

577+
@JsonProperty("allow_plaintext_backup")
578+
private boolean allowPlaintextBackup;
579+
580+
@JsonProperty("convergent_encryption")
581+
private boolean supportsConvergentEncryption;
582+
583+
@JsonProperty("convergent_encryption_version")
584+
private int convergentVersion;
585+
577586
public VaultTransitKeyImpl() {
578587
}
579588

@@ -607,6 +616,21 @@ public boolean supportsSigning() {
607616
return isSupportsSigning();
608617
}
609618

619+
@Override
620+
public boolean allowPlaintextBackup() {
621+
return isAllowPlaintextBackup();
622+
}
623+
624+
@Override
625+
public boolean supportsConvergentEncryption() {
626+
return isSupportsConvergentEncryption();
627+
}
628+
629+
@Override
630+
public int getConvergentVersion() {
631+
return this.convergentVersion;
632+
}
633+
610634
@Nullable
611635
public String getName() {
612636
return this.name;
@@ -644,6 +668,10 @@ public int getMinEncryptionVersion() {
644668
return this.minEncryptionVersion;
645669
}
646670

671+
public boolean isAllowPlaintextBackup() {
672+
return this.allowPlaintextBackup;
673+
}
674+
647675
public boolean isSupportsDecryption() {
648676
return this.supportsDecryption;
649677
}
@@ -660,6 +688,10 @@ public boolean isSupportsSigning() {
660688
return this.supportsSigning;
661689
}
662690

691+
public boolean isSupportsConvergentEncryption() {
692+
return this.supportsConvergentEncryption;
693+
}
694+
663695
public void setName(@Nullable String name) {
664696
this.name = name;
665697
}
@@ -732,15 +764,17 @@ public boolean equals(Object o) {
732764
&& this.supportsDerivation == that.supportsDerivation
733765
&& this.supportsSigning == that.supportsSigning && Objects.equals(this.name, that.name)
734766
&& this.cipherMode.equals(that.cipherMode) && Objects.equals(this.type, that.type)
735-
&& this.keys.equals(that.keys);
767+
&& this.allowPlaintextBackup == that.allowPlaintextBackup
768+
&& this.supportsConvergentEncryption == that.supportsConvergentEncryption;
736769
}
737770

738771
@Override
739772
public int hashCode() {
740773
return Objects.hash(this.name, this.cipherMode, this.type, this.deletionAllowed, this.derived,
741774
this.exportable, this.keys, this.latestVersion, this.minDecryptionVersion,
742775
this.minEncryptionVersion, this.supportsDecryption, this.supportsEncryption,
743-
this.supportsDerivation, this.supportsSigning);
776+
this.supportsDerivation, this.supportsSigning, this.allowPlaintextBackup,
777+
this.supportsConvergentEncryption);
744778
}
745779

746780
}

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)