Skip to content

Commit c7f47b2

Browse files
committed
Polishing.
Add author and since tags. Refine nullability and toString rendering. Original pull request: gh-808 See gh-789
1 parent c5a087a commit c7f47b2

File tree

7 files changed

+111
-94
lines changed

7 files changed

+111
-94
lines changed

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
*
3737
* @author Zakaria Amine
3838
* @author Mark Paluch
39+
* @author Jeroen Willemsen
3940
* @since 2.3
4041
*/
4142
class VaultKeyValueMetadataTemplate implements VaultKeyValueMetadataOperations {
@@ -120,12 +121,8 @@ private static Versioned.Metadata buildVersion(String version, Map<String, Objec
120121
.createdAt(createdTime)
121122
.deletedAt(deletionTime)
122123
.destroyed(destroyed)
123-
.version(kvVersion);
124-
125-
if (versionData.get("custom_metadata") != null) {
126-
Map<String, String> customMetadata = (Map<String, String>) versionData.get("custom_metadata");
127-
builder.customMetadata(customMetadata);
128-
}
124+
.version(kvVersion)
125+
.customMetadata((Map<String, String>) versionData.get("custom_metadata"));
129126

130127
return builder.build();
131128
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public interface VaultVersionedKeyValueOperations extends VaultKeyValueOperation
4343
* @param path must not be {@literal null}.
4444
* @return the data. May be {@literal null} if the path does not exist.
4545
*/
46+
@Override
4647
@Nullable
4748
default Versioned<Map<String, Object>> get(String path) {
4849
return get(path, Version.unversioned());

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
*
4747
* @author Mark Paluch
4848
* @author Maciej Drozdzowski
49+
* @author Jeroen Willemsen
4950
* @since 2.1
5051
*/
5152
public class VaultVersionedKeyValueTemplate extends VaultKeyValue2Accessor implements VaultVersionedKeyValueOperations {
@@ -159,6 +160,7 @@ public Metadata put(String path, Object body) {
159160
return getMetadata(response.getRequiredData());
160161
}
161162

163+
@SuppressWarnings("unchecked")
162164
private static Metadata getMetadata(Map<String, Object> responseMetadata) {
163165

164166
MetadataBuilder builder = Metadata.builder();
@@ -176,12 +178,8 @@ private static Metadata getMetadata(Map<String, Object> responseMetadata) {
176178
}
177179

178180
Integer version = (Integer) responseMetadata.get("version");
179-
builder.version(Version.from(version));
180-
181-
if (responseMetadata.get("custom_metadata") != null) {
182-
Map<String, String> customMetadata = (Map<String, String>) responseMetadata.get("custom_metadata");
183-
builder.customMetadata(customMetadata);
184-
}
181+
builder.version(Version.from(version))
182+
.customMetadata((Map<String, String>) responseMetadata.get("custom_metadata"));
185183

186184
return builder.build();
187185
}

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

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* Value object to bind Vault HTTP kv metadata update API requests.
2727
*
2828
* @author Zakaria Amine
29+
* @author Jeroen Willemsen
2930
* @see <a href=
3031
* "https://www.vaultproject.io/api-docs/secret/kv/kv-v2#update-metadata">Update
3132
* Metadata</a>
@@ -43,28 +44,21 @@ public class VaultMetadataRequest {
4344
private final String deleteVersionAfter;
4445

4546
@JsonProperty("custom_metadata")
46-
private final Map<String, String> customMetadata;
47+
private final @Nullable Map<String, String> customMetadata;
4748

48-
private VaultMetadataRequest(int maxVersions, boolean casRequired, @Nullable Duration deleteVersionAfter,
49-
@Nullable Map<String, String> customMetadata) {
50-
this.maxVersions = maxVersions;
49+
private VaultMetadataRequest(boolean casRequired, @Nullable Map<String, String> customMetadata,
50+
@Nullable Duration deleteVersionAfter, int maxVersions) {
5151
this.casRequired = casRequired;
52+
this.customMetadata = customMetadata;
5253
this.deleteVersionAfter = DurationParser
5354
.formatDuration(deleteVersionAfter != null ? deleteVersionAfter : Duration.ZERO);
54-
this.customMetadata = customMetadata;
55+
this.maxVersions = maxVersions;
5556
}
5657

5758
public static VaultMetadataRequestBuilder builder() {
5859
return new VaultMetadataRequestBuilder();
5960
}
6061

61-
/**
62-
* @return The number of versions to keep per key.
63-
*/
64-
public int getMaxVersions() {
65-
return this.maxVersions;
66-
}
67-
6862
/**
6963
* @return If true all keys will require the cas parameter to be set on all write
7064
* requests.
@@ -73,6 +67,11 @@ public boolean isCasRequired() {
7367
return this.casRequired;
7468
}
7569

70+
@Nullable
71+
public Map<String, String> getCustomMetadata() {
72+
return this.customMetadata;
73+
}
74+
7675
/**
7776
* @return the deletion_time for all new versions written to this key. Accepts
7877
* <a href="https://golang.org/pkg/time/#ParseDuration">Go duration format string</a>.
@@ -81,31 +80,33 @@ public String getDeleteVersionAfter() {
8180
return this.deleteVersionAfter;
8281
}
8382

84-
@Nullable
85-
public Map<String, String> getCustomMetadata() {
86-
return this.customMetadata;
83+
/**
84+
* @return The number of versions to keep per key.
85+
*/
86+
public int getMaxVersions() {
87+
return this.maxVersions;
8788
}
8889

8990
public static class VaultMetadataRequestBuilder {
9091

91-
private int maxVersions;
92-
9392
private boolean casRequired;
9493

9594
@Nullable
96-
private Duration deleteVersionAfter;
95+
private Map<String, String> customMetadata;
9796

9897
@Nullable
99-
private Map<String, String> customMetadata;
98+
private Duration deleteVersionAfter;
99+
100+
private int maxVersions;
100101

101102
/**
102-
* Set the number of versions to keep per key.
103-
* @param maxVersions
103+
* Set the cas_required parameter to {@code true} to require the cas parameter to
104+
* be set on all write requests.
104105
* @return {@link VaultMetadataRequest}
106+
* @since 3.1
105107
*/
106-
public VaultMetadataRequestBuilder maxVersions(int maxVersions) {
107-
this.maxVersions = maxVersions;
108-
return this;
108+
public VaultMetadataRequestBuilder casRequired() {
109+
return casRequired(true);
109110
}
110111

111112
/**
@@ -119,6 +120,17 @@ public VaultMetadataRequestBuilder casRequired(boolean casRequired) {
119120
return this;
120121
}
121122

123+
/**
124+
* Sets the custom Metadata for the metadata request.
125+
* @param customMetadata
126+
* @return {@link VaultMetadataRequest}
127+
* @since 3.1
128+
*/
129+
public VaultMetadataRequestBuilder customMetadata(Map<String, String> customMetadata) {
130+
this.customMetadata = customMetadata;
131+
return this;
132+
}
133+
122134
/**
123135
* Sets the deletion time for all new versions written to this key.
124136
* @param deleteVersionAfter
@@ -130,21 +142,21 @@ public VaultMetadataRequestBuilder deleteVersionAfter(Duration deleteVersionAfte
130142
}
131143

132144
/**
133-
* Sets the custom Metadata for the metadatarequest
134-
* @param customMetadata
145+
* Set the number of versions to keep per key.
146+
* @param maxVersions
135147
* @return {@link VaultMetadataRequest}
136148
*/
137-
public VaultMetadataRequestBuilder customMetadata(Map<String, String> customMetadata) {
138-
this.customMetadata = customMetadata;
149+
public VaultMetadataRequestBuilder maxVersions(int maxVersions) {
150+
this.maxVersions = maxVersions;
139151
return this;
140152
}
141153

142154
/**
143155
* @return a new {@link VaultMetadataRequest}
144156
*/
145157
public VaultMetadataRequest build() {
146-
return new VaultMetadataRequest(this.maxVersions, this.casRequired, this.deleteVersionAfter,
147-
this.customMetadata);
158+
return new VaultMetadataRequest(this.casRequired, this.customMetadata, this.deleteVersionAfter,
159+
this.maxVersions);
148160
}
149161

150162
}

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

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@
1818
import java.time.Duration;
1919
import java.time.Instant;
2020
import java.time.Period;
21+
import java.util.Collections;
2122
import java.util.List;
2223
import java.util.Map;
2324

2425
import org.springframework.lang.Nullable;
26+
import org.springframework.vault.support.Versioned.Metadata;
2527

2628
/**
2729
* Value object to bind Vault HTTP kv read metadata API responses.
2830
*
2931
* @author Zakaria Amine
32+
* @author Jeroen Willemsen
3033
* @since 2.3
3134
*/
3235
public class VaultMetadataResponse {
@@ -37,6 +40,8 @@ public class VaultMetadataResponse {
3740

3841
private final int currentVersion;
3942

43+
private final Map<String, String> customMetadata;
44+
4045
private final Duration deleteVersionAfter;
4146

4247
private final int maxVersions;
@@ -47,20 +52,19 @@ public class VaultMetadataResponse {
4752

4853
private final List<Versioned.Metadata> versions;
4954

50-
private final Map<String, String> customMetadata;
51-
5255
private VaultMetadataResponse(boolean casRequired, Instant createdTime, int currentVersion,
53-
Duration deleteVersionAfter, int maxVersions, int oldestVersion, Instant updatedTime,
54-
List<Versioned.Metadata> versions, Map<String, String> customMetadata) {
56+
Map<String, String> customMetadata, Duration deleteVersionAfter, int maxVersions, int oldestVersion,
57+
Instant updatedTime, List<Metadata> versions) {
58+
5559
this.casRequired = casRequired;
5660
this.createdTime = createdTime;
61+
this.customMetadata = customMetadata;
5762
this.currentVersion = currentVersion;
5863
this.deleteVersionAfter = deleteVersionAfter;
5964
this.maxVersions = maxVersions;
6065
this.oldestVersion = oldestVersion;
6166
this.updatedTime = updatedTime;
6267
this.versions = versions;
63-
this.customMetadata = customMetadata;
6468
}
6569

6670
public static VaultMetadataResponseBuilder builder() {
@@ -98,9 +102,9 @@ public Duration getDeleteVersionAfter() {
98102
}
99103

100104
/**
101-
* @return KV of customMetadata. Entries can be any arbitrary key-value pairs
105+
* @return the custom metadata. Entries can be any arbitrary key-value pairs
106+
* @since 3.1
102107
*/
103-
@Nullable
104108
public Map<String, String> getCustomMetadata() {
105109
return this.customMetadata;
106110
}
@@ -127,12 +131,10 @@ public Instant getUpdatedTime() {
127131
}
128132

129133
/**
130-
* Follows the following format.
131-
*
132-
* "versions": { "1": { "created_time": "2020-05-18T12:23:09.895587932Z",
133-
* "deletion_time": "2020-05-18T12:31:00.66257744Z", "destroyed": false }, "2": {
134-
* "created_time": "2020-05-18T12:23:10.122081788Z", "deletion_time": "", "destroyed":
135-
* false } }
134+
* Follows the following format. "versions": { "1": { "created_time":
135+
* "2020-05-18T12:23:09.895587932Z", "deletion_time": "2020-05-18T12:31:00.66257744Z",
136+
* "destroyed": false }, "2": { "created_time": "2020-05-18T12:23:10.122081788Z",
137+
* "deletion_time": "", "destroyed": false } }
136138
* @return the key versions and their details
137139
*/
138140
public List<Versioned.Metadata> getVersions() {
@@ -143,6 +145,8 @@ public static class VaultMetadataResponseBuilder {
143145

144146
private boolean casRequired;
145147

148+
private Map<String, String> customMetadata;
149+
146150
private Instant createdTime;
147151

148152
private int currentVersion;
@@ -157,8 +161,6 @@ public static class VaultMetadataResponseBuilder {
157161

158162
private List<Versioned.Metadata> versions;
159163

160-
private Map<String, String> customMetadata;
161-
162164
public VaultMetadataResponseBuilder casRequired(boolean casRequired) {
163165
this.casRequired = casRequired;
164166
return this;
@@ -174,6 +176,11 @@ public VaultMetadataResponseBuilder currentVersion(int currentVersion) {
174176
return this;
175177
}
176178

179+
public VaultMetadataResponseBuilder customMetadata(@Nullable Map<String, String> customMetadata) {
180+
this.customMetadata = customMetadata != null ? customMetadata : Collections.emptyMap();
181+
return this;
182+
}
183+
177184
public VaultMetadataResponseBuilder deleteVersionAfter(Duration deleteVersionAfter) {
178185
this.deleteVersionAfter = deleteVersionAfter;
179186
return this;
@@ -199,15 +206,10 @@ public VaultMetadataResponseBuilder versions(List<Versioned.Metadata> versions)
199206
return this;
200207
}
201208

202-
public VaultMetadataResponseBuilder customMetadata(Map<String, String> customMetadata) {
203-
this.customMetadata = customMetadata;
204-
return this;
205-
}
206-
207209
public VaultMetadataResponse build() {
208210
return new VaultMetadataResponse(this.casRequired, this.createdTime, this.currentVersion,
209-
this.deleteVersionAfter, this.maxVersions, this.oldestVersion, this.updatedTime, this.versions,
210-
this.customMetadata);
211+
this.customMetadata, this.deleteVersionAfter, this.maxVersions, this.oldestVersion,
212+
this.updatedTime, this.versions);
211213
}
212214

213215
}

0 commit comments

Comments
 (0)