Merge branch 'develop' for version 0.2.0
This commit is contained in:
commit
351ea1fc62
@ -14,7 +14,7 @@ Java Vault Connector is a connector library for [Vault](https://www.vaultproject
|
|||||||
* Write secrets
|
* Write secrets
|
||||||
* List secrets
|
* List secrets
|
||||||
* Connector Factory with builder pattern
|
* Connector Factory with builder pattern
|
||||||
* Tested against Vault 0.6.0
|
* Tested against Vault 0.6.1
|
||||||
|
|
||||||
**Usage Example**
|
**Usage Example**
|
||||||
|
|
||||||
@ -38,7 +38,7 @@ String secret = vault.readSecret("some/secret/key").getValue();
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>de.stklcode.jvault</groupId>
|
<groupId>de.stklcode.jvault</groupId>
|
||||||
<artifactId>connector</artifactId>
|
<artifactId>connector</artifactId>
|
||||||
<version>0.1.1</version>
|
<version>0.2.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
19
pom.xml
19
pom.xml
@ -6,14 +6,14 @@
|
|||||||
|
|
||||||
<groupId>de.stklcode.jvault</groupId>
|
<groupId>de.stklcode.jvault</groupId>
|
||||||
<artifactId>connector</artifactId>
|
<artifactId>connector</artifactId>
|
||||||
<version>0.1.1</version>
|
<version>0.2.0</version>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>3.3</version>
|
<version>3.5.1</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<source>1.8</source>
|
<source>1.8</source>
|
||||||
<target>1.8</target>
|
<target>1.8</target>
|
||||||
@ -24,36 +24,31 @@
|
|||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
|
||||||
<groupId>commons-io</groupId>
|
|
||||||
<artifactId>commons-io</artifactId>
|
|
||||||
<version>2.4</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.httpcomponents</groupId>
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
<artifactId>httpcore</artifactId>
|
<artifactId>httpcore</artifactId>
|
||||||
<version>4.0.1</version>
|
<version>4.4.5</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.httpcomponents</groupId>
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
<artifactId>httpclient</artifactId>
|
<artifactId>httpclient</artifactId>
|
||||||
<version>4.0.2</version>
|
<version>4.5.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.fasterxml.jackson.core</groupId>
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
<artifactId>jackson-core</artifactId>
|
<artifactId>jackson-core</artifactId>
|
||||||
<version>2.7.2</version>
|
<version>2.8.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.fasterxml.jackson.core</groupId>
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
<artifactId>jackson-databind</artifactId>
|
<artifactId>jackson-databind</artifactId>
|
||||||
<version>2.7.2</version>
|
<version>2.8.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<version>4.11</version>
|
<version>4.12</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -6,21 +6,23 @@ import de.stklcode.jvault.connector.exception.*;
|
|||||||
import de.stklcode.jvault.connector.model.AuthBackend;
|
import de.stklcode.jvault.connector.model.AuthBackend;
|
||||||
import de.stklcode.jvault.connector.model.response.*;
|
import de.stklcode.jvault.connector.model.response.*;
|
||||||
import de.stklcode.jvault.connector.model.response.embedded.AuthMethod;
|
import de.stklcode.jvault.connector.model.response.embedded.AuthMethod;
|
||||||
import org.apache.commons.io.IOUtils;
|
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
import org.apache.http.client.HttpClient;
|
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.client.methods.HttpPost;
|
||||||
import org.apache.http.client.methods.HttpPut;
|
import org.apache.http.client.methods.HttpPut;
|
||||||
import org.apache.http.client.methods.HttpRequestBase;
|
import org.apache.http.client.methods.HttpRequestBase;
|
||||||
|
import org.apache.http.client.utils.URIBuilder;
|
||||||
import org.apache.http.entity.StringEntity;
|
import org.apache.http.entity.StringEntity;
|
||||||
import org.apache.http.impl.client.DefaultHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.params.BasicHttpParams;
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||||||
import org.apache.http.params.HttpParams;
|
import org.apache.http.util.EntityUtils;
|
||||||
import org.apache.http.protocol.HTTP;
|
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@ -45,11 +47,11 @@ public class HTTPVaultConnector implements VaultConnector {
|
|||||||
|
|
||||||
private final ObjectMapper jsonMapper;
|
private final ObjectMapper jsonMapper;
|
||||||
|
|
||||||
private final HttpClient httpClient; /* HTTP client for connection */
|
|
||||||
private final String baseURL; /* Base URL of Vault */
|
private final String baseURL; /* Base URL of Vault */
|
||||||
|
|
||||||
private boolean authorized = false; /* authorization status */
|
private boolean authorized = false; /* authorization status */
|
||||||
private String token; /* current token */
|
private String token; /* current token */
|
||||||
|
private long tokenTTL = 0; /* expiration time for current token */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create connector using hostname and schema.
|
* Create connector using hostname and schema.
|
||||||
@ -90,13 +92,13 @@ public class HTTPVaultConnector implements VaultConnector {
|
|||||||
*/
|
*/
|
||||||
public HTTPVaultConnector(String baseURL) {
|
public HTTPVaultConnector(String baseURL) {
|
||||||
this.baseURL = baseURL;
|
this.baseURL = baseURL;
|
||||||
this.httpClient = new DefaultHttpClient();
|
|
||||||
this.jsonMapper = new ObjectMapper();
|
this.jsonMapper = new ObjectMapper();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void resetAuth() {
|
public void resetAuth() {
|
||||||
token = null;
|
token = null;
|
||||||
|
tokenTTL = 0;
|
||||||
authorized = false;
|
authorized = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,6 +110,9 @@ public class HTTPVaultConnector implements VaultConnector {
|
|||||||
} catch (VaultConnectorException | IOException e) {
|
} catch (VaultConnectorException | IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
|
} catch (URISyntaxException ignored) {
|
||||||
|
/* this should never occur and may leak sensible information */
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,10 +129,10 @@ public class HTTPVaultConnector implements VaultConnector {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SealResponse unseal(final String key, final Boolean reset) {
|
public SealResponse unseal(final String key, final Boolean reset) {
|
||||||
Map<String, Object> param = new HashMap<>();
|
Map<String, String> param = new HashMap<>();
|
||||||
param.put("key", key);
|
param.put("key", key);
|
||||||
if (reset != null)
|
if (reset != null)
|
||||||
param.put("reset", reset);
|
param.put("reset", reset.toString());
|
||||||
try {
|
try {
|
||||||
String response = requestPut(PATH_UNSEAL, param);
|
String response = requestPut(PATH_UNSEAL, param);
|
||||||
return jsonMapper.readValue(response, SealResponse.class);
|
return jsonMapper.readValue(response, SealResponse.class);
|
||||||
@ -139,7 +144,7 @@ public class HTTPVaultConnector implements VaultConnector {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isAuthorized() {
|
public boolean isAuthorized() {
|
||||||
return authorized;
|
return authorized && (tokenTTL == 0 || tokenTTL >= System.currentTimeMillis());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -154,9 +159,12 @@ public class HTTPVaultConnector implements VaultConnector {
|
|||||||
String response = requestGet(PATH_AUTH, new HashMap<>());
|
String response = requestGet(PATH_AUTH, new HashMap<>());
|
||||||
/* Parse response */
|
/* Parse response */
|
||||||
AuthMethodsResponse amr = jsonMapper.readValue(response, AuthMethodsResponse.class);
|
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) {
|
} catch (IOException e) {
|
||||||
throw new InvalidResponseException("Unable to parse response", e);
|
throw new InvalidResponseException("Unable to parse response", e);
|
||||||
|
} catch (URISyntaxException ignored) {
|
||||||
|
/* this should never occur and may leak sensible information */
|
||||||
|
throw new InvalidRequestException("Invalid URI format.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -164,6 +172,7 @@ public class HTTPVaultConnector implements VaultConnector {
|
|||||||
public TokenResponse authToken(final String token) throws VaultConnectorException {
|
public TokenResponse authToken(final String token) throws VaultConnectorException {
|
||||||
/* set token */
|
/* set token */
|
||||||
this.token = token;
|
this.token = token;
|
||||||
|
this.tokenTTL = 0;
|
||||||
try {
|
try {
|
||||||
String response = requestPost(PATH_TOKEN_LOOKUP, new HashMap<>());
|
String response = requestPost(PATH_TOKEN_LOOKUP, new HashMap<>());
|
||||||
TokenResponse res = jsonMapper.readValue(response, TokenResponse.class);
|
TokenResponse res = jsonMapper.readValue(response, TokenResponse.class);
|
||||||
@ -185,6 +194,7 @@ public class HTTPVaultConnector implements VaultConnector {
|
|||||||
AuthResponse upr = jsonMapper.readValue(response, AuthResponse.class);
|
AuthResponse upr = jsonMapper.readValue(response, AuthResponse.class);
|
||||||
/* verify response */
|
/* verify response */
|
||||||
this.token = upr.getAuth().getClientToken();
|
this.token = upr.getAuth().getClientToken();
|
||||||
|
this.tokenTTL = System.currentTimeMillis() + upr.getAuth().getLeaseDuration() * 1000L;
|
||||||
this.authorized = true;
|
this.authorized = true;
|
||||||
return upr;
|
return upr;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -204,6 +214,7 @@ public class HTTPVaultConnector implements VaultConnector {
|
|||||||
AuthResponse auth = jsonMapper.readValue(response, AuthResponse.class);
|
AuthResponse auth = jsonMapper.readValue(response, AuthResponse.class);
|
||||||
/* verify response */
|
/* verify response */
|
||||||
this.token = auth.getAuth().getClientToken();
|
this.token = auth.getAuth().getClientToken();
|
||||||
|
this.tokenTTL = System.currentTimeMillis() + auth.getAuth().getLeaseDuration() * 1000L;
|
||||||
this.authorized = true;
|
this.authorized = true;
|
||||||
return auth;
|
return auth;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -250,6 +261,9 @@ public class HTTPVaultConnector implements VaultConnector {
|
|||||||
return jsonMapper.readValue(response, SecretResponse.class);
|
return jsonMapper.readValue(response, SecretResponse.class);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new InvalidResponseException("Unable to parse response", e);
|
throw new InvalidResponseException("Unable to parse response", e);
|
||||||
|
} catch (URISyntaxException ignored) {
|
||||||
|
/* this should never occur and may leak sensible information */
|
||||||
|
throw new InvalidRequestException("Invalid URI format.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -258,12 +272,15 @@ public class HTTPVaultConnector implements VaultConnector {
|
|||||||
if (!isAuthorized())
|
if (!isAuthorized())
|
||||||
throw new AuthorizationRequiredException();
|
throw new AuthorizationRequiredException();
|
||||||
|
|
||||||
String response = requestGet(PATH_SECRET + "/" + path + "/?list=true", new HashMap<>());
|
|
||||||
try {
|
try {
|
||||||
|
String response = requestGet(PATH_SECRET + "/" + path + "/?list=true", new HashMap<>());
|
||||||
SecretListResponse secrets = jsonMapper.readValue(response, SecretListResponse.class);
|
SecretListResponse secrets = jsonMapper.readValue(response, SecretListResponse.class);
|
||||||
return secrets.getKeys();
|
return secrets.getKeys();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new InvalidResponseException("Unable to parse response", e);
|
throw new InvalidResponseException("Unable to parse response", e);
|
||||||
|
} catch (URISyntaxException ignored) {
|
||||||
|
/* this should never occur and may leak sensible information */
|
||||||
|
throw new InvalidRequestException("Invalid URI format.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -284,7 +301,7 @@ public class HTTPVaultConnector implements VaultConnector {
|
|||||||
* @param path URL path (relative to base)
|
* @param path URL path (relative to base)
|
||||||
* @param payload Map of payload values (will be converted to JSON)
|
* @param payload Map of payload values (will be converted to JSON)
|
||||||
* @return HTTP response
|
* @return HTTP response
|
||||||
* @throws VaultConnectorException
|
* @throws VaultConnectorException on connection error
|
||||||
*/
|
*/
|
||||||
private String requestPost(final String path, final Map payload) throws VaultConnectorException {
|
private String requestPost(final String path, final Map payload) throws VaultConnectorException {
|
||||||
/* Initialize post */
|
/* Initialize post */
|
||||||
@ -292,8 +309,8 @@ public class HTTPVaultConnector implements VaultConnector {
|
|||||||
/* generate JSON from payload */
|
/* generate JSON from payload */
|
||||||
StringEntity input;
|
StringEntity input;
|
||||||
try {
|
try {
|
||||||
input = new StringEntity(jsonMapper.writeValueAsString(payload), HTTP.UTF_8);
|
input = new StringEntity(jsonMapper.writeValueAsString(payload), StandardCharsets.UTF_8);
|
||||||
} catch (UnsupportedEncodingException | JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
throw new InvalidRequestException("Unable to parse response", e);
|
throw new InvalidRequestException("Unable to parse response", e);
|
||||||
}
|
}
|
||||||
input.setContentEncoding("UTF-8");
|
input.setContentEncoding("UTF-8");
|
||||||
@ -311,9 +328,9 @@ public class HTTPVaultConnector implements VaultConnector {
|
|||||||
* @param path URL path (relative to base)
|
* @param path URL path (relative to base)
|
||||||
* @param payload Map of payload values (will be converted to JSON)
|
* @param payload Map of payload values (will be converted to JSON)
|
||||||
* @return HTTP response
|
* @return HTTP response
|
||||||
* @throws VaultConnectorException
|
* @throws VaultConnectorException on connection error
|
||||||
*/
|
*/
|
||||||
private String requestPut(final String path, final Map<String, Object> payload) throws VaultConnectorException {
|
private String requestPut(final String path, final Map<String, String> payload) throws VaultConnectorException {
|
||||||
/* Initialize post */
|
/* Initialize post */
|
||||||
HttpPut put = new HttpPut(baseURL + path);
|
HttpPut put = new HttpPut(baseURL + path);
|
||||||
/* generate JSON from payload */
|
/* generate JSON from payload */
|
||||||
@ -337,15 +354,15 @@ public class HTTPVaultConnector implements VaultConnector {
|
|||||||
* @param path URL path (relative to base)
|
* @param path URL path (relative to base)
|
||||||
* @param payload Map of payload values (will be converted to JSON)
|
* @param payload Map of payload values (will be converted to JSON)
|
||||||
* @return HTTP response
|
* @return HTTP response
|
||||||
* @throws VaultConnectorException
|
* @throws VaultConnectorException on connection error
|
||||||
*/
|
*/
|
||||||
private String requestGet(final String path, final Map<String, Object> payload) throws VaultConnectorException {
|
private String requestGet(final String path, final Map<String, String> payload) throws VaultConnectorException, URISyntaxException {
|
||||||
/* Initialize post */
|
/* Add parameters to URI */
|
||||||
HttpGet get = new HttpGet(baseURL + path);
|
URIBuilder uriBuilder = new URIBuilder(baseURL + path);
|
||||||
/* Parse parameters */
|
payload.forEach(uriBuilder::addParameter);
|
||||||
HttpParams params = new BasicHttpParams();
|
|
||||||
payload.forEach(params::setParameter);
|
/* Initialize request */
|
||||||
get.setParams(params);
|
HttpGet get = new HttpGet(uriBuilder.build());
|
||||||
|
|
||||||
/* Set X-Vault-Token header */
|
/* Set X-Vault-Token header */
|
||||||
if (token != null)
|
if (token != null)
|
||||||
@ -358,21 +375,24 @@ public class HTTPVaultConnector implements VaultConnector {
|
|||||||
* Execute prepared HTTP request and return result
|
* Execute prepared HTTP request and return result
|
||||||
* @param base Prepares Request
|
* @param base Prepares Request
|
||||||
* @return HTTP response
|
* @return HTTP response
|
||||||
* @throws VaultConnectorException
|
* @throws VaultConnectorException on connection error
|
||||||
*/
|
*/
|
||||||
private String request(HttpRequestBase base) throws VaultConnectorException {
|
private String request(HttpRequestBase base) throws VaultConnectorException {
|
||||||
/* Set JSON Header */
|
/* Set JSON Header */
|
||||||
base.addHeader("accept", "application/json");
|
base.addHeader("accept", "application/json");
|
||||||
|
|
||||||
HttpResponse response = null;
|
HttpResponse response = null;
|
||||||
try {
|
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
|
||||||
response = httpClient.execute(base);
|
response = httpClient.execute(base);
|
||||||
/* Check if response is valid */
|
/* Check if response is valid */
|
||||||
if (response == null)
|
if (response == null)
|
||||||
throw new InvalidResponseException("Response unavailable");
|
throw new InvalidResponseException("Response unavailable");
|
||||||
|
|
||||||
switch (response.getStatusLine().getStatusCode()) {
|
switch (response.getStatusLine().getStatusCode()) {
|
||||||
case 200:
|
case 200:
|
||||||
return IOUtils.toString(response.getEntity().getContent());
|
try(BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()))) {
|
||||||
|
return br.lines().collect(Collectors.joining("\n"));
|
||||||
|
} catch (IOException ignored) { }
|
||||||
case 204:
|
case 204:
|
||||||
return "";
|
return "";
|
||||||
case 403:
|
case 403:
|
||||||
@ -380,19 +400,18 @@ public class HTTPVaultConnector implements VaultConnector {
|
|||||||
default:
|
default:
|
||||||
InvalidResponseException ex = new InvalidResponseException("Invalid response code")
|
InvalidResponseException ex = new InvalidResponseException("Invalid response code")
|
||||||
.withStatusCode(response.getStatusLine().getStatusCode());
|
.withStatusCode(response.getStatusLine().getStatusCode());
|
||||||
try {
|
if (response.getEntity() != null) {
|
||||||
/* Try to parse error response */
|
try (BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()))) {
|
||||||
ErrorResponse er = jsonMapper.readValue(IOUtils.toString(response.getEntity().getContent()),
|
String responseString = br.lines().collect(Collectors.joining("\n"));
|
||||||
ErrorResponse.class);
|
ErrorResponse er = jsonMapper.readValue(responseString, ErrorResponse.class);
|
||||||
/* Check for "permission denied" response */
|
/* Check for "permission denied" response */
|
||||||
if (er.getErrors().size() > 0 && er.getErrors().get(0).equals("permission denied"))
|
if (er.getErrors().size() > 0 && er.getErrors().get(0).equals("permission denied"))
|
||||||
throw new PermissionDeniedException();
|
throw new PermissionDeniedException();
|
||||||
|
throw ex.withResponse(er.toString());
|
||||||
throw ex.withResponse(er.toString());
|
} catch (IOException ignored) {
|
||||||
}
|
}
|
||||||
catch (IOException e) {
|
|
||||||
throw ex;
|
|
||||||
}
|
}
|
||||||
|
throw ex;
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new InvalidResponseException("Unable to read response", e);
|
throw new InvalidResponseException("Unable to read response", e);
|
||||||
@ -400,7 +419,7 @@ public class HTTPVaultConnector implements VaultConnector {
|
|||||||
finally {
|
finally {
|
||||||
if (response != null && response.getEntity() != null)
|
if (response != null && response.getEntity() != null)
|
||||||
try {
|
try {
|
||||||
response.getEntity().consumeContent();
|
EntityUtils.consume(response.getEntity());
|
||||||
} catch (IOException ignored) {
|
} catch (IOException ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -60,6 +60,7 @@ public interface VaultConnector {
|
|||||||
/**
|
/**
|
||||||
* Get all availale authentication backends.
|
* Get all availale authentication backends.
|
||||||
* @return List of backends
|
* @return List of backends
|
||||||
|
* @throws VaultConnectorException on error
|
||||||
*/
|
*/
|
||||||
List<AuthBackend> getAuthBackends() throws VaultConnectorException;
|
List<AuthBackend> getAuthBackends() throws VaultConnectorException;
|
||||||
|
|
||||||
@ -67,6 +68,7 @@ public interface VaultConnector {
|
|||||||
* Authorize to Vault using token.
|
* Authorize to Vault using token.
|
||||||
* @param token The token
|
* @param token The token
|
||||||
* @return Token response
|
* @return Token response
|
||||||
|
* @throws VaultConnectorException on error
|
||||||
*/
|
*/
|
||||||
TokenResponse authToken(final String token) throws VaultConnectorException;
|
TokenResponse authToken(final String token) throws VaultConnectorException;
|
||||||
|
|
||||||
@ -75,7 +77,7 @@ public interface VaultConnector {
|
|||||||
* @param username The username
|
* @param username The username
|
||||||
* @param password The password
|
* @param password The password
|
||||||
* @return Authorization result
|
* @return Authorization result
|
||||||
* @throws VaultConnectorException
|
* @throws VaultConnectorException on error
|
||||||
*/
|
*/
|
||||||
AuthResponse authUserPass(final String username, final String password) throws VaultConnectorException;
|
AuthResponse authUserPass(final String username, final String password) throws VaultConnectorException;
|
||||||
|
|
||||||
@ -84,6 +86,7 @@ public interface VaultConnector {
|
|||||||
* @param appID The App ID
|
* @param appID The App ID
|
||||||
* @param userID The User ID
|
* @param userID The User ID
|
||||||
* @return TRUE on success
|
* @return TRUE on success
|
||||||
|
* @throws VaultConnectorException on error
|
||||||
*/
|
*/
|
||||||
AuthResponse authAppId(final String appID, final String userID) throws VaultConnectorException;
|
AuthResponse authAppId(final String appID, final String userID) throws VaultConnectorException;
|
||||||
|
|
||||||
@ -93,7 +96,7 @@ public interface VaultConnector {
|
|||||||
* @param policy The policy to associate with
|
* @param policy The policy to associate with
|
||||||
* @param displayName Arbitrary name to display
|
* @param displayName Arbitrary name to display
|
||||||
* @return TRUE on success
|
* @return TRUE on success
|
||||||
* @throws VaultConnectorException
|
* @throws VaultConnectorException on error
|
||||||
*/
|
*/
|
||||||
boolean registerAppId(final String appID, final String policy, final String displayName) throws VaultConnectorException;
|
boolean registerAppId(final String appID, final String policy, final String displayName) throws VaultConnectorException;
|
||||||
|
|
||||||
@ -102,7 +105,7 @@ public interface VaultConnector {
|
|||||||
* @param appID The App-ID
|
* @param appID The App-ID
|
||||||
* @param userID The User-ID
|
* @param userID The User-ID
|
||||||
* @return TRUE on success
|
* @return TRUE on success
|
||||||
* @throws VaultConnectorException
|
* @throws VaultConnectorException on error
|
||||||
*/
|
*/
|
||||||
boolean registerUserId(final String appID, final String userID) throws VaultConnectorException;
|
boolean registerUserId(final String appID, final String userID) throws VaultConnectorException;
|
||||||
|
|
||||||
@ -113,7 +116,7 @@ public interface VaultConnector {
|
|||||||
* @param displayName Arbitrary name to display
|
* @param displayName Arbitrary name to display
|
||||||
* @param userID The User-ID
|
* @param userID The User-ID
|
||||||
* @return TRUE on success
|
* @return TRUE on success
|
||||||
* @throws VaultConnectorException
|
* @throws VaultConnectorException on error
|
||||||
*/
|
*/
|
||||||
default boolean registerAppUserId(final String appID, final String policy, final String displayName, final String userID) throws VaultConnectorException {
|
default boolean registerAppUserId(final String appID, final String policy, final String displayName, final String userID) throws VaultConnectorException {
|
||||||
return registerAppId(appID, policy, userID) && registerUserId(appID, userID);
|
return registerAppId(appID, policy, userID) && registerUserId(appID, userID);
|
||||||
@ -129,6 +132,7 @@ public interface VaultConnector {
|
|||||||
* Retrieve secret form Vault.
|
* Retrieve secret form Vault.
|
||||||
* @param key Secret identifier
|
* @param key Secret identifier
|
||||||
* @return Secret response
|
* @return Secret response
|
||||||
|
* @throws VaultConnectorException on error
|
||||||
*/
|
*/
|
||||||
SecretResponse readSecret(final String key) throws VaultConnectorException;
|
SecretResponse readSecret(final String key) throws VaultConnectorException;
|
||||||
|
|
||||||
@ -136,6 +140,7 @@ public interface VaultConnector {
|
|||||||
* List available secrets from Vault.
|
* List available secrets from Vault.
|
||||||
* @param path Root path to search
|
* @param path Root path to search
|
||||||
* @return List of secret keys
|
* @return List of secret keys
|
||||||
|
* @throws VaultConnectorException on error
|
||||||
*/
|
*/
|
||||||
List<String> listSecrets(final String path) throws VaultConnectorException;
|
List<String> listSecrets(final String path) throws VaultConnectorException;
|
||||||
|
|
||||||
@ -144,6 +149,7 @@ public interface VaultConnector {
|
|||||||
* @param key Secret path
|
* @param key Secret path
|
||||||
* @param value Secret value
|
* @param value Secret value
|
||||||
* @return TRUE on success
|
* @return TRUE on success
|
||||||
|
* @throws VaultConnectorException on error
|
||||||
*/
|
*/
|
||||||
boolean writeSecret(final String key, final String value) throws VaultConnectorException;
|
boolean writeSecret(final String key, final String value) throws VaultConnectorException;
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
package de.stklcode.jvault.connector.model.response;
|
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.exception.InvalidResponseException;
|
||||||
import de.stklcode.jvault.connector.model.response.embedded.AuthMethod;
|
import de.stklcode.jvault.connector.model.response.embedded.AuthMethod;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -14,19 +15,27 @@ import java.util.Map;
|
|||||||
* @author Stefan Kalscheuer
|
* @author Stefan Kalscheuer
|
||||||
* @since 0.1
|
* @since 0.1
|
||||||
*/
|
*/
|
||||||
public class AuthMethodsResponse implements VaultResponse {
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
|
public class AuthMethodsResponse extends VaultDataResponse {
|
||||||
|
private Map<String, AuthMethod> supportedMethods;
|
||||||
|
|
||||||
private List<AuthMethod> supportedMethods;
|
public AuthMethodsResponse() {
|
||||||
|
this.supportedMethods = new HashMap<>();
|
||||||
@JsonAnySetter
|
|
||||||
public void setMethod(String path, Map<String, String> data) throws InvalidResponseException {
|
|
||||||
if (supportedMethods == null)
|
|
||||||
supportedMethods = new ArrayList<>();
|
|
||||||
|
|
||||||
supportedMethods.add(new AuthMethod(path, data.get("description"), data.get("type")));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<AuthMethod> getSupportedMethods() {
|
@Override
|
||||||
|
public void setData(Map<String, Object> 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<String, AuthMethod> getSupportedMethods() {
|
||||||
return supportedMethods;
|
return supportedMethods;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
package de.stklcode.jvault.connector.model.response.embedded;
|
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 de.stklcode.jvault.connector.model.AuthBackend;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Embedded authentication method response.
|
* Embedded authentication method response.
|
||||||
*
|
*
|
||||||
@ -12,12 +15,15 @@ import de.stklcode.jvault.connector.model.AuthBackend;
|
|||||||
public class AuthMethod {
|
public class AuthMethod {
|
||||||
private AuthBackend type;
|
private AuthBackend type;
|
||||||
private String rawType;
|
private String rawType;
|
||||||
private String path;
|
|
||||||
|
@JsonProperty("description")
|
||||||
private String description;
|
private String description;
|
||||||
|
|
||||||
public AuthMethod(String path, String description, String type) {
|
@JsonProperty("config")
|
||||||
this.path = path;
|
private Map<String, String> config;
|
||||||
this.description = description;
|
|
||||||
|
@JsonSetter("type")
|
||||||
|
public void setType(String type) {
|
||||||
this.rawType = type;
|
this.rawType = type;
|
||||||
this.type = AuthBackend.forType(type);
|
this.type = AuthBackend.forType(type);
|
||||||
}
|
}
|
||||||
@ -30,11 +36,11 @@ public class AuthMethod {
|
|||||||
return rawType;
|
return rawType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPath() {
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getDescription() {
|
public String getDescription() {
|
||||||
return description;
|
return description;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Map<String, String> getConfig() {
|
||||||
|
return config;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user