Read/write/list arbitrary keys
This commit is contained in:
parent
2f5b6d1523
commit
2f312d3937
2
pom.xml
2
pom.xml
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
<groupId>de.stklcode.jvault</groupId>
|
<groupId>de.stklcode.jvault</groupId>
|
||||||
<artifactId>connector</artifactId>
|
<artifactId>connector</artifactId>
|
||||||
<version>0.4.1</version>
|
<version>0.5.0-SNAPSHOT</version>
|
||||||
|
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
@ -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_USERPASS = "auth/userpass/login/";
|
||||||
private static final String PATH_AUTH_APPID = "auth/app-id/";
|
private static final String PATH_AUTH_APPID = "auth/app-id/";
|
||||||
private static final String PATH_AUTH_APPROLE = "auth/approle/";
|
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 static final String PATH_REVOKE = "sys/revoke/";
|
||||||
|
|
||||||
private final ObjectMapper jsonMapper;
|
private final ObjectMapper jsonMapper;
|
||||||
@ -463,12 +462,12 @@ public class HTTPVaultConnector implements VaultConnector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SecretResponse readSecret(final String key) throws VaultConnectorException {
|
public SecretResponse read(final String key) throws VaultConnectorException {
|
||||||
if (!isAuthorized())
|
if (!isAuthorized())
|
||||||
throw new AuthorizationRequiredException();
|
throw new AuthorizationRequiredException();
|
||||||
/* Request HTTP response and parse Secret */
|
/* Request HTTP response and parse Secret */
|
||||||
try {
|
try {
|
||||||
String response = requestGet(PATH_SECRET + "/" + key, new HashMap<>());
|
String response = requestGet(key, new HashMap<>());
|
||||||
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);
|
||||||
@ -479,12 +478,12 @@ public class HTTPVaultConnector implements VaultConnector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> listSecrets(final String path) throws VaultConnectorException {
|
public List<String> list(final String path) throws VaultConnectorException {
|
||||||
if (!isAuthorized())
|
if (!isAuthorized())
|
||||||
throw new AuthorizationRequiredException();
|
throw new AuthorizationRequiredException();
|
||||||
|
|
||||||
try {
|
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);
|
SecretListResponse secrets = jsonMapper.readValue(response, SecretListResponse.class);
|
||||||
return secrets.getKeys();
|
return secrets.getKeys();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -496,7 +495,7 @@ public class HTTPVaultConnector implements VaultConnector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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())
|
if (!isAuthorized())
|
||||||
throw new AuthorizationRequiredException();
|
throw new AuthorizationRequiredException();
|
||||||
|
|
||||||
@ -505,17 +504,17 @@ public class HTTPVaultConnector implements VaultConnector {
|
|||||||
|
|
||||||
Map<String, String> param = new HashMap<>();
|
Map<String, String> param = new HashMap<>();
|
||||||
param.put("value", value);
|
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.");
|
throw new InvalidResponseException("Received response where none was expected.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deleteSecret(String key) throws VaultConnectorException {
|
public void delete(String key) throws VaultConnectorException {
|
||||||
if (!isAuthorized())
|
if (!isAuthorized())
|
||||||
throw new AuthorizationRequiredException();
|
throw new AuthorizationRequiredException();
|
||||||
|
|
||||||
/* Request HTTP response and expect empty result */
|
/* Request HTTP response and expect empty result */
|
||||||
String response = requestDelete(PATH_SECRET + "/" + key);
|
String response = requestDelete(key);
|
||||||
|
|
||||||
/* Response should be code 204 without content */
|
/* Response should be code 204 without content */
|
||||||
if (!response.equals(""))
|
if (!response.equals(""))
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
package de.stklcode.jvault.connector;
|
package de.stklcode.jvault.connector;
|
||||||
|
|
||||||
import de.stklcode.jvault.connector.exception.AuthorizationRequiredException;
|
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.exception.VaultConnectorException;
|
||||||
import de.stklcode.jvault.connector.model.*;
|
import de.stklcode.jvault.connector.model.*;
|
||||||
import de.stklcode.jvault.connector.model.response.*;
|
import de.stklcode.jvault.connector.model.response.*;
|
||||||
@ -32,6 +33,8 @@ import java.util.List;
|
|||||||
* @since 0.1
|
* @since 0.1
|
||||||
*/
|
*/
|
||||||
public interface VaultConnector {
|
public interface VaultConnector {
|
||||||
|
String PATH_SECRET = "secret";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reset authorization information.
|
* Reset authorization information.
|
||||||
*/
|
*/
|
||||||
@ -358,39 +361,93 @@ public interface VaultConnector {
|
|||||||
boolean isAuthorized();
|
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
|
* @param key Secret identifier
|
||||||
* @return Secret response
|
* @return Secret response
|
||||||
* @throws VaultConnectorException on error
|
* @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.
|
* List available secrets from Vault.
|
||||||
|
* Prefix "secret/" is automatically added to path.
|
||||||
*
|
*
|
||||||
* @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
|
* @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.
|
* Write secret to Vault.
|
||||||
|
* Prefix "secret/" is automatically added to path.
|
||||||
*
|
*
|
||||||
* @param key Secret path
|
* @param key Secret path
|
||||||
* @param value Secret value
|
* @param value Secret value
|
||||||
* @throws VaultConnectorException on error
|
* @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.
|
* Delete secret from Vault.
|
||||||
|
* Prefix "secret/" is automatically added to path.
|
||||||
*
|
*
|
||||||
* @param key Secret path
|
* @param key Secret path
|
||||||
* @throws VaultConnectorException on error
|
* @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.
|
* Revoke given lease immediately.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user