Secret deletion implemented and tested

This commit is contained in:
2016-10-16 18:24:36 +02:00
parent 048e4d12b4
commit fc493a2e73
3 changed files with 80 additions and 13 deletions

View File

@ -23,10 +23,7 @@ import de.stklcode.jvault.connector.model.AuthBackend;
import de.stklcode.jvault.connector.model.response.*;
import de.stklcode.jvault.connector.model.response.embedded.AuthMethod;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.*;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
@ -315,6 +312,16 @@ public class HTTPVaultConnector implements VaultConnector {
return requestPost(PATH_SECRET + "/" + key, param).equals("");
}
@Override
public boolean deleteSecret(String key) throws VaultConnectorException {
if (!isAuthorized())
throw new AuthorizationRequiredException();
/* Request HTTP response and expect empty result */
String response = requestDelete(PATH_SECRET + "/" + key);
return response.equals("");
}
@Override
public boolean revoke(String leaseID) throws VaultConnectorException {
if (!isAuthorized())
@ -369,7 +376,7 @@ public class HTTPVaultConnector implements VaultConnector {
* @throws VaultConnectorException on connection error
*/
private String requestPut(final String path, final Map<String, String> payload) throws VaultConnectorException {
/* Initialize post */
/* Initialize put */
HttpPut put = new HttpPut(baseURL + path);
/* generate JSON from payload */
StringEntity entity = null;
@ -387,6 +394,23 @@ public class HTTPVaultConnector implements VaultConnector {
return request(put);
}
/**
* Execute HTTP request using DELETE method.
*
* @param path URL path (relative to base)
* @return HTTP response
* @throws VaultConnectorException on connection error
*/
private String requestDelete(final String path) throws VaultConnectorException {
/* Initialize delete */
HttpDelete delete = new HttpDelete(baseURL + path);
/* Set X-Vault-Token header */
if (token != null)
delete.addHeader("X-Vault-Token", token);
return request(delete);
}
/**
* Execute HTTP request using GET method.
*

View File

@ -182,6 +182,15 @@ public interface VaultConnector {
*/
boolean writeSecret(final String key, final String value) throws VaultConnectorException;
/**
* Delete secret from Vault.
*
* @param key Secret path
* @return TRUE on succevss
* @throws VaultConnectorException on error
*/
boolean deleteSecret(final String key) throws VaultConnectorException;
/**
* Revoke given lease immediately.
*