add token type to model and builder classes

This commit is contained in:
Stefan Kalscheuer 2019-10-16 18:06:55 +02:00
parent 83a05fcd40
commit df696e9f17
5 changed files with 94 additions and 1 deletions

View File

@ -45,6 +45,10 @@ public final class Token {
@JsonInclude(JsonInclude.Include.NON_NULL) @JsonInclude(JsonInclude.Include.NON_NULL)
private String id; private String id;
@JsonProperty("type")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String type;
@JsonProperty("display_name") @JsonProperty("display_name")
@JsonInclude(JsonInclude.Include.NON_NULL) @JsonInclude(JsonInclude.Include.NON_NULL)
private String displayName; private String displayName;
@ -77,6 +81,33 @@ public final class Token {
@JsonInclude(JsonInclude.Include.NON_NULL) @JsonInclude(JsonInclude.Include.NON_NULL)
private Boolean renewable; private Boolean renewable;
/**
* Construct complete {@link Token} object with default type.
*
* @param id Token ID (optional)
* @param displayName Token display name (optional)
* @param noParent Token has no parent (optional)
* @param noDefaultPolicy Do not add default policy (optional)
* @param ttl Token TTL in seconds (optional)
* @param numUses Number of uses (optional)
* @param policies List of policies (optional)
* @param meta Metadata (optional)
* @param renewable Is the token renewable (optional)
* @deprecated As of 0.9, use {@link #Token(String, String, String, Boolean, Boolean, Integer, Integer, List, Map, Boolean)} instead.
*/
@Deprecated
public Token(final String id,
final String displayName,
final Boolean noParent,
final Boolean noDefaultPolicy,
final Integer ttl,
final Integer numUses,
final List<String> policies,
final Map<String, String> meta,
final Boolean renewable) {
this(id, Type.DEFAULT.value(), displayName, noParent, noDefaultPolicy, ttl, numUses, policies, meta, renewable);
}
/** /**
* Construct complete {@link Token} object. * Construct complete {@link Token} object.
* *
@ -91,6 +122,7 @@ public final class Token {
* @param renewable Is the token renewable (optional) * @param renewable Is the token renewable (optional)
*/ */
public Token(final String id, public Token(final String id,
final String type,
final String displayName, final String displayName,
final Boolean noParent, final Boolean noParent,
final Boolean noDefaultPolicy, final Boolean noDefaultPolicy,
@ -100,6 +132,7 @@ public final class Token {
final Map<String, String> meta, final Map<String, String> meta,
final Boolean renewable) { final Boolean renewable) {
this.id = id; this.id = id;
this.type = type;
this.displayName = displayName; this.displayName = displayName;
this.ttl = ttl; this.ttl = ttl;
this.numUses = numUses; this.numUses = numUses;
@ -117,6 +150,14 @@ public final class Token {
return id; return id;
} }
/**
* @return Token type
* @since 0.9
*/
public String getType() {
return type;
}
/** /**
* @return Token display name * @return Token display name
*/ */
@ -172,4 +213,25 @@ public final class Token {
public Boolean isRenewable() { public Boolean isRenewable() {
return renewable; return renewable;
} }
/**
* Constants for token types.
*/
public enum Type {
DEFAULT("default"),
BATCH("batch"),
SERVICE("service"),
DEFAULT_SERVICE("default-service"),
DEFAULT_BATCH("default-batch");
private final String value;
Type(String value) {
this.value = value;
}
public String value() {
return value;
}
}
} }

View File

@ -26,6 +26,7 @@ import java.util.*;
*/ */
public final class TokenBuilder { public final class TokenBuilder {
private String id; private String id;
private Token.Type type;
private String displayName; private String displayName;
private Boolean noParent; private Boolean noParent;
private Boolean noDefaultPolicy; private Boolean noDefaultPolicy;
@ -46,6 +47,18 @@ public final class TokenBuilder {
return this; return this;
} }
/**
* Specify token type.
*
* @param type the type
* @return self
* @since 0.9
*/
public TokenBuilder withType(final Token.Type type) {
this.type = type;
return this;
}
/** /**
* Add display name. * Add display name.
* *
@ -247,6 +260,7 @@ public final class TokenBuilder {
*/ */
public Token build() { public Token build() {
return new Token(id, return new Token(id,
type != null ? type.value() : null,
displayName, displayName,
noParent, noParent,
noDefaultPolicy, noDefaultPolicy,

View File

@ -45,6 +45,9 @@ public final class TokenData {
@JsonProperty("id") @JsonProperty("id")
private String id; private String id;
@JsonProperty("type")
private String type;
@JsonProperty("meta") @JsonProperty("meta")
private Map<String, Object> meta; private Map<String, Object> meta;
@ -98,6 +101,14 @@ public final class TokenData {
return id; return id;
} }
/**
* @return Token type
* @since 0.9
*/
public String getType() {
return type;
}
/** /**
* @return Number of uses * @return Number of uses
*/ */

View File

@ -1039,6 +1039,7 @@ public class HTTPVaultConnectorTest {
/* Create token */ /* Create token */
Token token = Token.builder() Token token = Token.builder()
.withId("test-id") .withId("test-id")
.withType(Token.Type.SERVICE)
.withDisplayName("test name") .withDisplayName("test name")
.build(); .build();
@ -1116,6 +1117,7 @@ public class HTTPVaultConnectorTest {
/* Create token with attributes */ /* Create token with attributes */
Token token = Token.builder() Token token = Token.builder()
.withId("my-token") .withId("my-token")
.withType(Token.Type.SERVICE)
.build(); .build();
try { try {
connector.createToken(token); connector.createToken(token);
@ -1131,6 +1133,7 @@ public class HTTPVaultConnectorTest {
assertThat("Unexpected token ID", res.getData().getId(), is(token.getId())); assertThat("Unexpected token ID", res.getData().getId(), is(token.getId()));
assertThat("Unexpected number of policies", res.getData().getPolicies(), hasSize(1)); assertThat("Unexpected number of policies", res.getData().getPolicies(), hasSize(1));
assertThat("Unexpected policy", res.getData().getPolicies(), contains("root")); assertThat("Unexpected policy", res.getData().getPolicies(), contains("root"));
assertThat("Unexpected token type", res.getData().getType(), is(token.getType()));
} catch (VaultConnectorException e) { } catch (VaultConnectorException e) {
fail("Token creation failed."); fail("Token creation failed.");
} }

View File

@ -53,7 +53,7 @@ public class TokenBuilderTest {
private static final String META_KEY_2 = "key2"; private static final String META_KEY_2 = "key2";
private static final String META_VALUE_2 = "value2"; private static final String META_VALUE_2 = "value2";
private static final Boolean RENEWABLE = true; private static final Boolean RENEWABLE = true;
private static final String JSON_FULL = "{\"id\":\"test-id\",\"display_name\":\"display-name\",\"no_parent\":false,\"no_default_policy\":false,\"ttl\":123,\"num_uses\":4,\"policies\":[\"policy\"],\"meta\":{\"key\":\"value\"},\"renewable\":true}"; private static final String JSON_FULL = "{\"id\":\"test-id\",\"type\":\"service\",\"display_name\":\"display-name\",\"no_parent\":false,\"no_default_policy\":false,\"ttl\":123,\"num_uses\":4,\"policies\":[\"policy\"],\"meta\":{\"key\":\"value\"},\"renewable\":true}";
@BeforeAll @BeforeAll
public static void init() { public static void init() {
@ -68,6 +68,7 @@ public class TokenBuilderTest {
public void buildDefaultTest() throws JsonProcessingException { public void buildDefaultTest() throws JsonProcessingException {
Token token = new TokenBuilder().build(); Token token = new TokenBuilder().build();
assertThat(token.getId(), is(nullValue())); assertThat(token.getId(), is(nullValue()));
assertThat(token.getType(), is(nullValue()));
assertThat(token.getDisplayName(), is(nullValue())); assertThat(token.getDisplayName(), is(nullValue()));
assertThat(token.getNoParent(), is(nullValue())); assertThat(token.getNoParent(), is(nullValue()));
assertThat(token.getNoDefaultPolicy(), is(nullValue())); assertThat(token.getNoDefaultPolicy(), is(nullValue()));
@ -88,6 +89,7 @@ public class TokenBuilderTest {
public void buildFullTest() throws JsonProcessingException { public void buildFullTest() throws JsonProcessingException {
Token token = new TokenBuilder() Token token = new TokenBuilder()
.withId(ID) .withId(ID)
.withType(Token.Type.SERVICE)
.withDisplayName(DISPLAY_NAME) .withDisplayName(DISPLAY_NAME)
.withNoParent(NO_PARENT) .withNoParent(NO_PARENT)
.withNoDefaultPolicy(NO_DEFAULT_POLICY) .withNoDefaultPolicy(NO_DEFAULT_POLICY)
@ -98,6 +100,7 @@ public class TokenBuilderTest {
.withRenewable(RENEWABLE) .withRenewable(RENEWABLE)
.build(); .build();
assertThat(token.getId(), is(ID)); assertThat(token.getId(), is(ID));
assertThat(token.getType(), is(Token.Type.SERVICE.value()));
assertThat(token.getDisplayName(), is(DISPLAY_NAME)); assertThat(token.getDisplayName(), is(DISPLAY_NAME));
assertThat(token.getNoParent(), is(NO_PARENT)); assertThat(token.getNoParent(), is(NO_PARENT));
assertThat(token.getNoDefaultPolicy(), is(NO_DEFAULT_POLICY)); assertThat(token.getNoDefaultPolicy(), is(NO_DEFAULT_POLICY));