add token type to model and builder classes
This commit is contained in:
parent
83a05fcd40
commit
df696e9f17
@ -45,6 +45,10 @@ public final class Token {
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
private String id;
|
||||
|
||||
@JsonProperty("type")
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
private String type;
|
||||
|
||||
@JsonProperty("display_name")
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
private String displayName;
|
||||
@ -77,6 +81,33 @@ public final class Token {
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
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.
|
||||
*
|
||||
@ -91,6 +122,7 @@ public final class Token {
|
||||
* @param renewable Is the token renewable (optional)
|
||||
*/
|
||||
public Token(final String id,
|
||||
final String type,
|
||||
final String displayName,
|
||||
final Boolean noParent,
|
||||
final Boolean noDefaultPolicy,
|
||||
@ -100,6 +132,7 @@ public final class Token {
|
||||
final Map<String, String> meta,
|
||||
final Boolean renewable) {
|
||||
this.id = id;
|
||||
this.type = type;
|
||||
this.displayName = displayName;
|
||||
this.ttl = ttl;
|
||||
this.numUses = numUses;
|
||||
@ -117,6 +150,14 @@ public final class Token {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Token type
|
||||
* @since 0.9
|
||||
*/
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Token display name
|
||||
*/
|
||||
@ -172,4 +213,25 @@ public final class Token {
|
||||
public Boolean isRenewable() {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ import java.util.*;
|
||||
*/
|
||||
public final class TokenBuilder {
|
||||
private String id;
|
||||
private Token.Type type;
|
||||
private String displayName;
|
||||
private Boolean noParent;
|
||||
private Boolean noDefaultPolicy;
|
||||
@ -46,6 +47,18 @@ public final class TokenBuilder {
|
||||
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.
|
||||
*
|
||||
@ -247,6 +260,7 @@ public final class TokenBuilder {
|
||||
*/
|
||||
public Token build() {
|
||||
return new Token(id,
|
||||
type != null ? type.value() : null,
|
||||
displayName,
|
||||
noParent,
|
||||
noDefaultPolicy,
|
||||
|
@ -45,6 +45,9 @@ public final class TokenData {
|
||||
@JsonProperty("id")
|
||||
private String id;
|
||||
|
||||
@JsonProperty("type")
|
||||
private String type;
|
||||
|
||||
@JsonProperty("meta")
|
||||
private Map<String, Object> meta;
|
||||
|
||||
@ -98,6 +101,14 @@ public final class TokenData {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Token type
|
||||
* @since 0.9
|
||||
*/
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Number of uses
|
||||
*/
|
||||
|
@ -1039,6 +1039,7 @@ public class HTTPVaultConnectorTest {
|
||||
/* Create token */
|
||||
Token token = Token.builder()
|
||||
.withId("test-id")
|
||||
.withType(Token.Type.SERVICE)
|
||||
.withDisplayName("test name")
|
||||
.build();
|
||||
|
||||
@ -1116,6 +1117,7 @@ public class HTTPVaultConnectorTest {
|
||||
/* Create token with attributes */
|
||||
Token token = Token.builder()
|
||||
.withId("my-token")
|
||||
.withType(Token.Type.SERVICE)
|
||||
.build();
|
||||
try {
|
||||
connector.createToken(token);
|
||||
@ -1131,6 +1133,7 @@ public class HTTPVaultConnectorTest {
|
||||
assertThat("Unexpected token ID", res.getData().getId(), is(token.getId()));
|
||||
assertThat("Unexpected number of policies", res.getData().getPolicies(), hasSize(1));
|
||||
assertThat("Unexpected policy", res.getData().getPolicies(), contains("root"));
|
||||
assertThat("Unexpected token type", res.getData().getType(), is(token.getType()));
|
||||
} catch (VaultConnectorException e) {
|
||||
fail("Token creation failed.");
|
||||
}
|
||||
|
@ -53,7 +53,7 @@ public class TokenBuilderTest {
|
||||
private static final String META_KEY_2 = "key2";
|
||||
private static final String META_VALUE_2 = "value2";
|
||||
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
|
||||
public static void init() {
|
||||
@ -68,6 +68,7 @@ public class TokenBuilderTest {
|
||||
public void buildDefaultTest() throws JsonProcessingException {
|
||||
Token token = new TokenBuilder().build();
|
||||
assertThat(token.getId(), is(nullValue()));
|
||||
assertThat(token.getType(), is(nullValue()));
|
||||
assertThat(token.getDisplayName(), is(nullValue()));
|
||||
assertThat(token.getNoParent(), is(nullValue()));
|
||||
assertThat(token.getNoDefaultPolicy(), is(nullValue()));
|
||||
@ -88,6 +89,7 @@ public class TokenBuilderTest {
|
||||
public void buildFullTest() throws JsonProcessingException {
|
||||
Token token = new TokenBuilder()
|
||||
.withId(ID)
|
||||
.withType(Token.Type.SERVICE)
|
||||
.withDisplayName(DISPLAY_NAME)
|
||||
.withNoParent(NO_PARENT)
|
||||
.withNoDefaultPolicy(NO_DEFAULT_POLICY)
|
||||
@ -98,6 +100,7 @@ public class TokenBuilderTest {
|
||||
.withRenewable(RENEWABLE)
|
||||
.build();
|
||||
assertThat(token.getId(), is(ID));
|
||||
assertThat(token.getType(), is(Token.Type.SERVICE.value()));
|
||||
assertThat(token.getDisplayName(), is(DISPLAY_NAME));
|
||||
assertThat(token.getNoParent(), is(NO_PARENT));
|
||||
assertThat(token.getNoDefaultPolicy(), is(NO_DEFAULT_POLICY));
|
||||
|
Loading…
x
Reference in New Issue
Block a user