From 847cce7bfb911c06852b50724d59929b800d20e9 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Thu, 1 Sep 2016 20:26:14 +0200 Subject: [PATCH] Fixed AuthMethodResponse for Vault 0.6.1 compatibility --- README.md | 4 +-- pom.xml | 2 +- .../jvault/connector/HTTPVaultConnector.java | 2 +- .../model/response/AuthMethodsResponse.java | 35 ++++++++++++------- .../model/response/embedded/AuthMethod.java | 24 ++++++++----- 5 files changed, 41 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 0340035..f0265ce 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Java Vault Connector is a connector library for [Vault](https://www.vaultproject * Write secrets * List secrets * Connector Factory with builder pattern -* Tested against Vault 0.6.0 +* Tested against Vault 0.6.1 **Usage Example** @@ -38,7 +38,7 @@ String secret = vault.readSecret("some/secret/key").getValue(); de.stklcode.jvault connector - 0.1.1 + 0.2.0 ``` diff --git a/pom.xml b/pom.xml index e4a6ce8..48b0560 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ de.stklcode.jvault connector - 0.1.2-SNAPSHOT + 0.2.0-SNAPSHOT diff --git a/src/main/java/de/stklcode/jvault/connector/HTTPVaultConnector.java b/src/main/java/de/stklcode/jvault/connector/HTTPVaultConnector.java index 9f06fe0..b64659b 100644 --- a/src/main/java/de/stklcode/jvault/connector/HTTPVaultConnector.java +++ b/src/main/java/de/stklcode/jvault/connector/HTTPVaultConnector.java @@ -159,7 +159,7 @@ public class HTTPVaultConnector implements VaultConnector { String response = requestGet(PATH_AUTH, new HashMap<>()); /* Parse response */ AuthMethodsResponse amr = jsonMapper.readValue(response, AuthMethodsResponse.class); - return amr.getSupportedMethods().stream().map(AuthMethod::getType).collect(Collectors.toList()); + return amr.getSupportedMethods().values().stream().map(AuthMethod::getType).collect(Collectors.toList()); } catch (IOException e) { throw new InvalidResponseException("Unable to parse response", e); } catch (URISyntaxException ignored) { diff --git a/src/main/java/de/stklcode/jvault/connector/model/response/AuthMethodsResponse.java b/src/main/java/de/stklcode/jvault/connector/model/response/AuthMethodsResponse.java index 9f7b11a..425e4d2 100644 --- a/src/main/java/de/stklcode/jvault/connector/model/response/AuthMethodsResponse.java +++ b/src/main/java/de/stklcode/jvault/connector/model/response/AuthMethodsResponse.java @@ -1,11 +1,12 @@ package de.stklcode.jvault.connector.model.response; -import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.databind.ObjectMapper; import de.stklcode.jvault.connector.exception.InvalidResponseException; import de.stklcode.jvault.connector.model.response.embedded.AuthMethod; -import java.util.ArrayList; -import java.util.List; +import java.io.IOException; +import java.util.HashMap; import java.util.Map; /** @@ -14,19 +15,27 @@ import java.util.Map; * @author Stefan Kalscheuer * @since 0.1 */ -public class AuthMethodsResponse implements VaultResponse { +@JsonIgnoreProperties(ignoreUnknown = true) +public class AuthMethodsResponse extends VaultDataResponse { + private Map supportedMethods; - private List supportedMethods; - - @JsonAnySetter - public void setMethod(String path, Map data) throws InvalidResponseException { - if (supportedMethods == null) - supportedMethods = new ArrayList<>(); - - supportedMethods.add(new AuthMethod(path, data.get("description"), data.get("type"))); + public AuthMethodsResponse() { + this.supportedMethods = new HashMap<>(); } - public List getSupportedMethods() { + @Override + public void setData(Map data) throws InvalidResponseException { + ObjectMapper mapper = new ObjectMapper(); + for (String path : data.keySet()) { + try { + this.supportedMethods.put(path, mapper.readValue(mapper.writeValueAsString(data.get(path)), AuthMethod.class)); + } catch (IOException e) { + throw new InvalidResponseException(); + } + } + } + + public Map getSupportedMethods() { return supportedMethods; } } diff --git a/src/main/java/de/stklcode/jvault/connector/model/response/embedded/AuthMethod.java b/src/main/java/de/stklcode/jvault/connector/model/response/embedded/AuthMethod.java index 663dcb1..89bbb37 100644 --- a/src/main/java/de/stklcode/jvault/connector/model/response/embedded/AuthMethod.java +++ b/src/main/java/de/stklcode/jvault/connector/model/response/embedded/AuthMethod.java @@ -1,8 +1,11 @@ package de.stklcode.jvault.connector.model.response.embedded; - +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonSetter; import de.stklcode.jvault.connector.model.AuthBackend; +import java.util.Map; + /** * Embedded authentication method response. * @@ -12,12 +15,15 @@ import de.stklcode.jvault.connector.model.AuthBackend; public class AuthMethod { private AuthBackend type; private String rawType; - private String path; + + @JsonProperty("description") private String description; - public AuthMethod(String path, String description, String type) { - this.path = path; - this.description = description; + @JsonProperty("config") + private Map config; + + @JsonSetter("type") + public void setType(String type) { this.rawType = type; this.type = AuthBackend.forType(type); } @@ -30,11 +36,11 @@ public class AuthMethod { return rawType; } - public String getPath() { - return path; - } - public String getDescription() { return description; } + + public Map getConfig() { + return config; + } }