Add capability to pass options map when writing to Vault

This is required to create or update KV v2 secrets. The existing write
method delegates to the new one with null-value for the options map.
This commit is contained in:
Stefan Kalscheuer 2018-11-20 11:26:02 +01:00
parent 068a87d915
commit e3f2193df2
2 changed files with 32 additions and 5 deletions

View File

@ -650,15 +650,29 @@ public class HTTPVaultConnector implements VaultConnector {
} }
@Override @Override
public final void write(final String key, final Map<String, Object> data) throws VaultConnectorException { public final void write(final String key, final Map<String, Object> data, final Map<String, Object> options) throws VaultConnectorException {
if (!isAuthorized()) if (!isAuthorized()) {
throw new AuthorizationRequiredException(); throw new AuthorizationRequiredException();
}
if (key == null || key.isEmpty()) if (key == null || key.isEmpty()) {
throw new InvalidRequestException("Secret path must not be empty."); throw new InvalidRequestException("Secret path must not be empty.");
}
if (!requestPost(key, data).isEmpty()) // By default data is directly passed as payload.
Object payload = data;
// If options are given, split payload in two parts.
if (options != null) {
Map<String, Object> payloadMap = new HashMap<>();
payloadMap.put("data", data);
payloadMap.put("options", options);
payload = payloadMap;
}
if (!requestPost(key, payload).isEmpty()) {
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE); throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE);
}
} }
@Override @Override

View File

@ -474,7 +474,20 @@ public interface VaultConnector extends AutoCloseable, Serializable {
* @throws VaultConnectorException on error * @throws VaultConnectorException on error
* @since 0.5.0 * @since 0.5.0
*/ */
void write(final String key, final Map<String, Object> data) throws VaultConnectorException; default void write(final String key, final Map<String, Object> data) throws VaultConnectorException {
write(key, data, null);
}
/**
* Write value to Vault.
*
* @param key Secret path
* @param data Secret content. Value must be be JSON serializable.
* @param options Secret options (optional).
* @throws VaultConnectorException on error
* @since 0.8 {@code options} parameter added
*/
void write(final String key, final Map<String, Object> data, final Map<String, Object> options) throws VaultConnectorException;
/** /**
* Write secret to Vault. * Write secret to Vault.