From df696e9f17fa71d51b5492b5cbf6a5c187ff367c Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Wed, 16 Oct 2019 18:06:55 +0200 Subject: [PATCH] add token type to model and builder classes --- .../jvault/connector/model/Token.java | 62 +++++++++++++++++++ .../jvault/connector/model/TokenBuilder.java | 14 +++++ .../model/response/embedded/TokenData.java | 11 ++++ .../connector/HTTPVaultConnectorTest.java | 3 + .../connector/model/TokenBuilderTest.java | 5 +- 5 files changed, 94 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/stklcode/jvault/connector/model/Token.java b/src/main/java/de/stklcode/jvault/connector/model/Token.java index c6e2633..a87e0b1 100644 --- a/src/main/java/de/stklcode/jvault/connector/model/Token.java +++ b/src/main/java/de/stklcode/jvault/connector/model/Token.java @@ -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 policies, + final Map 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 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; + } + } } diff --git a/src/main/java/de/stklcode/jvault/connector/model/TokenBuilder.java b/src/main/java/de/stklcode/jvault/connector/model/TokenBuilder.java index 8fed8dd..ba17bb1 100644 --- a/src/main/java/de/stklcode/jvault/connector/model/TokenBuilder.java +++ b/src/main/java/de/stklcode/jvault/connector/model/TokenBuilder.java @@ -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, diff --git a/src/main/java/de/stklcode/jvault/connector/model/response/embedded/TokenData.java b/src/main/java/de/stklcode/jvault/connector/model/response/embedded/TokenData.java index 1e5f202..b45c3c4 100644 --- a/src/main/java/de/stklcode/jvault/connector/model/response/embedded/TokenData.java +++ b/src/main/java/de/stklcode/jvault/connector/model/response/embedded/TokenData.java @@ -45,6 +45,9 @@ public final class TokenData { @JsonProperty("id") private String id; + @JsonProperty("type") + private String type; + @JsonProperty("meta") private Map 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 */ diff --git a/src/test/java/de/stklcode/jvault/connector/HTTPVaultConnectorTest.java b/src/test/java/de/stklcode/jvault/connector/HTTPVaultConnectorTest.java index 752118a..dd2a394 100644 --- a/src/test/java/de/stklcode/jvault/connector/HTTPVaultConnectorTest.java +++ b/src/test/java/de/stklcode/jvault/connector/HTTPVaultConnectorTest.java @@ -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."); } diff --git a/src/test/java/de/stklcode/jvault/connector/model/TokenBuilderTest.java b/src/test/java/de/stklcode/jvault/connector/model/TokenBuilderTest.java index 30ca1d4..e236dbb 100644 --- a/src/test/java/de/stklcode/jvault/connector/model/TokenBuilderTest.java +++ b/src/test/java/de/stklcode/jvault/connector/model/TokenBuilderTest.java @@ -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));