Read/write/list arbitrary keys

This commit is contained in:
Stefan Kalscheuer 2017-01-08 12:45:07 +01:00
parent 2f5b6d1523
commit 2f312d3937
3 changed files with 71 additions and 15 deletions

View File

@ -4,7 +4,7 @@
<groupId>de.stklcode.jvault</groupId>
<artifactId>connector</artifactId>
<version>0.4.1</version>
<version>0.5.0-SNAPSHOT</version>
<packaging>jar</packaging>

View File

@ -58,7 +58,6 @@ public class HTTPVaultConnector implements VaultConnector {
private static final String PATH_AUTH_USERPASS = "auth/userpass/login/";
private static final String PATH_AUTH_APPID = "auth/app-id/";
private static final String PATH_AUTH_APPROLE = "auth/approle/";
private static final String PATH_SECRET = "secret";
private static final String PATH_REVOKE = "sys/revoke/";
private final ObjectMapper jsonMapper;
@ -463,12 +462,12 @@ public class HTTPVaultConnector implements VaultConnector {
}
@Override
public SecretResponse readSecret(final String key) throws VaultConnectorException {
public SecretResponse read(final String key) throws VaultConnectorException {
if (!isAuthorized())
throw new AuthorizationRequiredException();
/* Request HTTP response and parse Secret */
try {
String response = requestGet(PATH_SECRET + "/" + key, new HashMap<>());
String response = requestGet(key, new HashMap<>());
return jsonMapper.readValue(response, SecretResponse.class);
} catch (IOException e) {
throw new InvalidResponseException("Unable to parse response", e);
@ -479,12 +478,12 @@ public class HTTPVaultConnector implements VaultConnector {
}
@Override
public List<String> listSecrets(final String path) throws VaultConnectorException {
public List<String> list(final String path) throws VaultConnectorException {
if (!isAuthorized())
throw new AuthorizationRequiredException();
try {
String response = requestGet(PATH_SECRET + "/" + path + "/?list=true", new HashMap<>());
String response = requestGet(path + "/?list=true", new HashMap<>());
SecretListResponse secrets = jsonMapper.readValue(response, SecretListResponse.class);
return secrets.getKeys();
} catch (IOException e) {
@ -496,7 +495,7 @@ public class HTTPVaultConnector implements VaultConnector {
}
@Override
public void writeSecret(final String key, final String value) throws VaultConnectorException {
public void write(final String key, final String value) throws VaultConnectorException {
if (!isAuthorized())
throw new AuthorizationRequiredException();
@ -505,17 +504,17 @@ public class HTTPVaultConnector implements VaultConnector {
Map<String, String> param = new HashMap<>();
param.put("value", value);
if (!requestPost(PATH_SECRET + "/" + key, param).equals(""))
if (!requestPost(key, param).equals(""))
throw new InvalidResponseException("Received response where none was expected.");
}
@Override
public void deleteSecret(String key) throws VaultConnectorException {
public void delete(String key) throws VaultConnectorException {
if (!isAuthorized())
throw new AuthorizationRequiredException();
/* Request HTTP response and expect empty result */
String response = requestDelete(PATH_SECRET + "/" + key);
String response = requestDelete(key);
/* Response should be code 204 without content */
if (!response.equals(""))

View File

@ -17,6 +17,7 @@
package de.stklcode.jvault.connector;
import de.stklcode.jvault.connector.exception.AuthorizationRequiredException;
import de.stklcode.jvault.connector.exception.InvalidRequestException;
import de.stklcode.jvault.connector.exception.VaultConnectorException;
import de.stklcode.jvault.connector.model.*;
import de.stklcode.jvault.connector.model.response.*;
@ -32,6 +33,8 @@ import java.util.List;
* @since 0.1
*/
public interface VaultConnector {
String PATH_SECRET = "secret";
/**
* Reset authorization information.
*/
@ -358,39 +361,93 @@ public interface VaultConnector {
boolean isAuthorized();
/**
* Retrieve secret form Vault.
* Retrieve any nodes content from Vault.
*
* @param key Secret identifier
* @return Secret response
* @throws VaultConnectorException on error
* @since 0.5.0
*/
SecretResponse read(final String key) throws VaultConnectorException;
/**
* Retrieve secret from Vault.
* Prefix "secret/" is automatically added to key.
*
* @param key Secret identifier
* @return Secret response
* @throws VaultConnectorException on error
*/
SecretResponse readSecret(final String key) throws VaultConnectorException;
default SecretResponse readSecret(final String key) throws VaultConnectorException {
return read(PATH_SECRET + "/" + key);
}
/**
* List available nodes from Vault.
*
* @param path Root path to search
* @return List of secret keys
* @throws VaultConnectorException on error
* @since 0.5.0
*/
List<String> list(final String path) throws VaultConnectorException;
/**
* List available secrets from Vault.
* Prefix "secret/" is automatically added to path.
*
* @param path Root path to search
* @return List of secret keys
* @throws VaultConnectorException on error
*/
List<String> listSecrets(final String path) throws VaultConnectorException;
default List<String> listSecrets(final String path) throws VaultConnectorException {
return list(PATH_SECRET + "/" + path);
}
/**
* Write value to Vault.
* Prefix "secret/" is automatically added to path.
*
* @param key Secret path
* @param value Secret value
* @throws VaultConnectorException on error
* @since 0.5.0
*/
void write(final String key, final String value) throws VaultConnectorException;
/**
* Write secret to Vault.
* Prefix "secret/" is automatically added to path.
*
* @param key Secret path
* @param value Secret value
* @throws VaultConnectorException on error
*/
void writeSecret(final String key, final String value) throws VaultConnectorException;
default void writeSecret(final String key, final String value) throws VaultConnectorException {
if (key == null || key.isEmpty())
throw new InvalidRequestException("Secret path must not be empty.");
write(PATH_SECRET + "/" + key, value);
}
/**
* Delete key from Vault.
*
* @param key Secret path
* @throws VaultConnectorException on error
* @since 0.5.0
*/
void delete(final String key) throws VaultConnectorException;
/**
* Delete secret from Vault.
* Prefix "secret/" is automatically added to path.
*
* @param key Secret path
* @throws VaultConnectorException on error
*/
void deleteSecret(final String key) throws VaultConnectorException;
default void deleteSecret(final String key) throws VaultConnectorException {
delete(PATH_SECRET + "/" + key);
}
/**
* Revoke given lease immediately.