Skip to content

Commit f42bd96

Browse files
committed
Polishing.
Add since and author tags. Reorder fields. Introduce empty VaultTokenRequest and refactor interface methods to default methods for delegation on the interface level. See gh-690 Original pull request: gh-803
1 parent e3261c6 commit f42bd96

File tree

4 files changed

+66
-64
lines changed

4 files changed

+66
-64
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* Interface that specifies token-related operations.
2525
*
2626
* @author Mark Paluch
27+
* @author Nanne Baars
2728
* @see <a href="https://www.vaultproject.io/docs/auth/token.html">Auth Backend: Token</a>
2829
*/
2930
public interface VaultTokenOperations {
@@ -34,7 +35,9 @@ public interface VaultTokenOperations {
3435
* @see <a href="https://www.vaultproject.io/docs/auth/token.html">POST
3536
* /auth/token/create</a>
3637
*/
37-
VaultTokenResponse create() throws VaultException;
38+
default VaultTokenResponse create() throws VaultException {
39+
return create(VaultTokenRequest.empty());
40+
}
3841

3942
/**
4043
* Create a new token for the given {@link VaultTokenRequest}.
@@ -51,16 +54,20 @@ public interface VaultTokenOperations {
5154
* @return a {@link VaultTokenResponse}
5255
* @see <a href="https://www.vaultproject.io/docs/auth/token.html">POST
5356
* /auth/token/create/:role</a>
57+
* @since 3.1
5458
*/
55-
VaultTokenResponse create(String role) throws VaultException;
59+
default VaultTokenResponse create(String role) throws VaultException {
60+
return create(role, VaultTokenRequest.empty());
61+
}
5662

5763
/**
5864
* Create a new token for the given {@code role} and {@link VaultTokenRequest}.
59-
* @param role must not be {@literal null}.
65+
* @param role must not be {@literal null} or empty.
6066
* @param request must not be {@literal null}.
6167
* @return a {@link VaultTokenResponse}
6268
* @see <a href="https://www.vaultproject.io/docs/auth/token.html">POST
6369
* /auth/token/create/:role</a>
70+
* @since 3.1
6471
*/
6572
VaultTokenResponse create(String role, VaultTokenRequest request) throws VaultException;
6673

@@ -70,7 +77,9 @@ public interface VaultTokenOperations {
7077
* @see <a href="https://www.vaultproject.io/docs/auth/token.html">POST
7178
* /auth/token/create-orphan</a>
7279
*/
73-
VaultTokenResponse createOrphan();
80+
default VaultTokenResponse createOrphan() {
81+
return createOrphan(VaultTokenRequest.empty());
82+
}
7483

7584
/**
7685
* Create a new orphan token for the given {@link VaultTokenRequest}.

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

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
* Default implementation of {@link VaultTokenOperations}.
3535
*
3636
* @author Mark Paluch
37+
* @author Nanne Baars
3738
*/
3839
public class VaultTokenTemplate implements VaultTokenOperations {
3940

@@ -50,11 +51,6 @@ public VaultTokenTemplate(VaultOperations vaultOperations) {
5051
this.vaultOperations = vaultOperations;
5152
}
5253

53-
@Override
54-
public VaultTokenResponse create() {
55-
return create(VaultTokenRequest.builder().build());
56-
}
57-
5854
@Override
5955
public VaultTokenResponse create(VaultTokenRequest request) {
6056

@@ -63,24 +59,13 @@ public VaultTokenResponse create(VaultTokenRequest request) {
6359
return writeAndReturn("auth/token/create", request, VaultTokenResponse.class);
6460
}
6561

66-
@Override
67-
public VaultTokenResponse create(String role) throws VaultException {
68-
69-
return create(role, VaultTokenRequest.builder().build());
70-
}
71-
7262
@Override
7363
public VaultTokenResponse create(String role, VaultTokenRequest request) throws VaultException {
7464

75-
Assert.notNull(role, "role must not be null");
65+
Assert.hasText(role, "Role must not be null or empty");
7666
Assert.notNull(request, "VaultTokenRequest must not be null");
7767

78-
return writeAndReturn("auth/token/create/%s".formatted(role), request, VaultTokenResponse.class);
79-
}
80-
81-
@Override
82-
public VaultTokenResponse createOrphan() {
83-
return createOrphan(VaultTokenRequest.builder().build());
68+
return writeAndReturn(String.format("auth/token/create/%s", role), request, VaultTokenResponse.class);
8469
}
8570

8671
@Override

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

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,23 @@
2222
import java.util.List;
2323
import java.util.Map;
2424
import java.util.concurrent.TimeUnit;
25+
import java.util.stream.StreamSupport;
2526

2627
import com.fasterxml.jackson.annotation.JsonProperty;
2728

28-
import java.util.stream.StreamSupport;
2929
import org.springframework.lang.Nullable;
3030
import org.springframework.util.Assert;
3131

3232
/**
3333
* Value object to bind Vault HTTP Token API requests.
3434
*
3535
* @author Mark Paluch
36+
* @author Nanne Baars
3637
*/
3738
public class VaultTokenRequest {
3839

40+
private static final VaultTokenRequest EMPTY = VaultTokenRequest.builder().build();
41+
3942
@Nullable
4043
private final String id;
4144

@@ -61,15 +64,15 @@ public class VaultTokenRequest {
6164
@JsonProperty("display_name")
6265
private final String displayName;
6366

64-
@JsonProperty("num_uses")
65-
private final int numUses;
66-
6767
@JsonProperty("entity_alias")
6868
private final String entityAlias;
6969

70+
@JsonProperty("num_uses")
71+
private final int numUses;
72+
7073
VaultTokenRequest(@Nullable String id, List<String> policies, Map<String, String> meta, boolean noParent,
7174
boolean noDefaultPolicy, boolean renewable, @Nullable String ttl, @Nullable String explicitMaxTtl,
72-
String displayName, int numUses, String entityAlias) {
75+
String displayName, String entityAlias, int numUses) {
7376

7477
this.id = id;
7578
this.policies = policies;
@@ -91,6 +94,14 @@ public static VaultTokenRequestBuilder builder() {
9194
return new VaultTokenRequestBuilder();
9295
}
9396

97+
/**
98+
* @return an empty token request.
99+
* @since 3.1
100+
*/
101+
public static VaultTokenRequest empty() {
102+
return EMPTY;
103+
}
104+
94105
/**
95106
* @return Id of the client token.
96107
*/
@@ -159,19 +170,19 @@ public String getDisplayName() {
159170
}
160171

161172
/**
162-
* @return the number of allowed token uses.
173+
* @return then name of the entity alias to associate with during token creation. Only
174+
* works in combination with role name.
175+
* @since 3.1
163176
*/
164-
public int getNumUses() {
165-
return this.numUses;
177+
public String getEntityAlias() {
178+
return this.entityAlias;
166179
}
167180

168181
/**
169-
* @return then name of the entity alias to associate with during token creation. Only
170-
* works in combination with role_name argument and used entity alias must be listed
171-
* in allowed_entity_aliases
182+
* @return the number of allowed token uses.
172183
*/
173-
public String getEntityAlias() {
174-
return this.entityAlias;
184+
public int getNumUses() {
185+
return this.numUses;
175186
}
176187

177188
/**
@@ -200,17 +211,18 @@ public static class VaultTokenRequestBuilder {
200211

201212
private String displayName = "";
202213

203-
private int numUses;
204-
214+
@Nullable
205215
private String entityAlias;
206216

217+
private int numUses;
218+
207219
VaultTokenRequestBuilder() {
208220
}
209221

210222
/**
211-
* Configure a the Id of the client token. Can only be specified by a root token.
212-
* Otherwise, the token Id is a randomly generated UUID.
213-
* @param id the token Id.
223+
* Configure the token identifier. Can only be specified by a root token.
224+
* Otherwise, the token identifier is a randomly generated UUID.
225+
* @param id the token identifier.
214226
* @return {@code this} {@link VaultTokenRequestBuilder}.
215227
*/
216228
public VaultTokenRequestBuilder id(String id) {
@@ -386,21 +398,6 @@ public VaultTokenRequestBuilder explicitMaxTtl(Duration explicitMaxTtl) {
386398
return this;
387399
}
388400

389-
/**
390-
* Configure the maximum uses for the token. This can be used to create a
391-
* one-time-token or limited use token. Defaults to {@literal 0}, which has no
392-
* limit to the number of uses.
393-
* @param numUses number of uses, must not be negative.
394-
* @return {@code this} {@link VaultTokenRequestBuilder}.
395-
*/
396-
public VaultTokenRequestBuilder numUses(int numUses) {
397-
398-
Assert.isTrue(numUses >= 0, "Number of uses must not be negative");
399-
400-
this.numUses = numUses;
401-
return this;
402-
}
403-
404401
/**
405402
* Configure a display name for the token, defaults to "token".
406403
* @param displayName must not be empty or {@literal null}.
@@ -418,6 +415,7 @@ public VaultTokenRequestBuilder displayName(String displayName) {
418415
* Configure the entity alias for the token.
419416
* @param entityAlias must not be empty or {@literal null}.
420417
* @return {@code this} {@link VaultTokenRequestBuilder}.
418+
* @since 3.1
421419
*/
422420
public VaultTokenRequestBuilder entityAlias(String entityAlias) {
423421

@@ -428,9 +426,20 @@ public VaultTokenRequestBuilder entityAlias(String entityAlias) {
428426
}
429427

430428
/**
431-
* Build a new {@link VaultTokenRequest} instance.
432-
* @return a new {@link VaultCertificateRequest}.
429+
* Configure the maximum uses for the token. This can be used to create a
430+
* one-time-token or limited use token. Defaults to {@literal 0}, which has no
431+
* limit to the number of uses.
432+
* @param numUses number of uses, must not be negative.
433+
* @return {@code this} {@link VaultTokenRequestBuilder}.
433434
*/
435+
public VaultTokenRequestBuilder numUses(int numUses) {
436+
437+
Assert.isTrue(numUses >= 0, "Number of uses must not be negative");
438+
439+
this.numUses = numUses;
440+
return this;
441+
}
442+
434443
/**
435444
* Build a new {@link VaultTokenRequest} instance.
436445
* @return a new {@link VaultCertificateRequest}.
@@ -442,13 +451,12 @@ public VaultTokenRequest build() {
442451
case 1 -> List.of(this.policies.get(0));
443452
default -> List.copyOf(this.policies);
444453
};
445-
Map<String, String> meta = switch (this.meta.size()) {
446-
case 0 -> Map.of();
447-
default -> Collections.unmodifiableMap(new LinkedHashMap<>(this.meta));
448-
};
454+
455+
Map<String, String> meta = this.meta.isEmpty() ? Map.of()
456+
: Collections.unmodifiableMap(new LinkedHashMap<>(this.meta));
449457

450458
return new VaultTokenRequest(this.id, policies, meta, this.noParent, this.noDefaultPolicy, this.renewable,
451-
this.ttl, this.explicitMaxTtl, this.displayName, this.numUses, this.entityAlias);
459+
this.ttl, this.explicitMaxTtl, this.displayName, this.entityAlias, this.numUses);
452460
}
453461

454462
private static <E> List<E> toList(Iterable<E> iter) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
* Integration tests for {@link VaultTokenTemplate} through {@link VaultTokenOperations}.
4848
*
4949
* @author Mark Paluch
50+
* @author Nanne Baars
5051
*/
5152
@ExtendWith(SpringExtension.class)
5253
@ContextConfiguration(classes = VaultIntegrationTestConfiguration.class)
@@ -100,8 +101,7 @@ void createTokenWithRoleShouldCreateAToken() {
100101

101102
@Test
102103
void noTokenWhenRoleDoesNotExists() {
103-
104-
assertThatThrownBy(() -> this.tokenOperations.create("unknown-role")).isInstanceOf(VaultException.class);
104+
assertThatExceptionOfType(VaultException.class).isThrownBy(() -> this.tokenOperations.create("unknown-role"));
105105
}
106106

107107
@Test

0 commit comments

Comments
 (0)