Move 204 empty checks into request helper and make Error class private

This commit is contained in:
Stefan Kalscheuer 2019-03-22 10:19:06 +01:00
parent 22a48d4a90
commit e4cf8a1dde
3 changed files with 78 additions and 68 deletions

View File

@ -18,9 +18,7 @@ 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.InvalidRequestException;
import de.stklcode.jvault.connector.exception.InvalidResponseException;
import de.stklcode.jvault.connector.exception.VaultConnectorException; import de.stklcode.jvault.connector.exception.VaultConnectorException;
import de.stklcode.jvault.connector.internal.Error;
import de.stklcode.jvault.connector.internal.RequestHelper; import de.stklcode.jvault.connector.internal.RequestHelper;
import de.stklcode.jvault.connector.model.AppRole; import de.stklcode.jvault.connector.model.AppRole;
import de.stklcode.jvault.connector.model.AppRoleSecret; import de.stklcode.jvault.connector.model.AppRoleSecret;
@ -335,12 +333,10 @@ public class HTTPVaultConnector implements VaultConnector {
Map<String, String> payload = new HashMap<>(); Map<String, String> payload = new HashMap<>();
payload.put("value", policy); payload.put("value", policy);
payload.put("display_name", displayName); payload.put("display_name", displayName);
/* Get response */
String response = request.post(PATH_AUTH_APPID + "map/app-id/" + appID, payload, token); /* Issue request anx expect code 204 with empty response */
/* Response should be code 204 without content */ request.postWithoutResponse(PATH_AUTH_APPID + "map/app-id/" + appID, payload, token);
if (!response.isEmpty()) {
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE);
}
return true; return true;
} }
@ -350,24 +346,19 @@ public class HTTPVaultConnector implements VaultConnector {
requireAuth(); requireAuth();
Map<String, String> payload = new HashMap<>(); Map<String, String> payload = new HashMap<>();
payload.put("value", appID); payload.put("value", appID);
/* Get response */
String response = request.post(PATH_AUTH_APPID + "map/user-id/" + userID, payload, token); /* Issue request anx expect code 204 with empty response */
/* Response should be code 204 without content */ request.postWithoutResponse(PATH_AUTH_APPID + "map/user-id/" + userID, payload, token);
if (!response.isEmpty()) {
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE);
}
return true; return true;
} }
@Override @Override
public final boolean createAppRole(final AppRole role) throws VaultConnectorException { public final boolean createAppRole(final AppRole role) throws VaultConnectorException {
requireAuth(); requireAuth();
/* Get response */
String response = request.post(String.format(PATH_AUTH_APPROLE_ROLE, role.getName(), ""), role, token); /* Issue request anx expect code 204 with empty response */
/* Response should be code 204 without content */ request.postWithoutResponse(String.format(PATH_AUTH_APPROLE_ROLE, role.getName(), ""), role, token);
if (!response.isEmpty()) {
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE);
}
/* Set custom ID if provided */ /* Set custom ID if provided */
return !(role.getId() != null && !role.getId().isEmpty()) || setAppRoleID(role.getName(), role.getId()); return !(role.getId() != null && !role.getId().isEmpty()) || setAppRoleID(role.getName(), role.getId());
@ -384,13 +375,8 @@ public class HTTPVaultConnector implements VaultConnector {
public final boolean deleteAppRole(final String roleName) throws VaultConnectorException { public final boolean deleteAppRole(final String roleName) throws VaultConnectorException {
requireAuth(); requireAuth();
/* Request HTTP response and expect empty result */ /* Issue request anx expect code 204 with empty response */
String response = request.delete(String.format(PATH_AUTH_APPROLE_ROLE, roleName, ""), token); request.deleteWithoutResponse(String.format(PATH_AUTH_APPROLE_ROLE, roleName, ""), token);
/* Response should be code 204 without content */
if (!response.isEmpty()) {
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE);
}
return true; return true;
} }
@ -413,11 +399,10 @@ public class HTTPVaultConnector implements VaultConnector {
/* Request HTTP response and parse Secret */ /* Request HTTP response and parse Secret */
Map<String, String> payload = new HashMap<>(); Map<String, String> payload = new HashMap<>();
payload.put("role_id", roleID); payload.put("role_id", roleID);
String response = request.post(String.format(PATH_AUTH_APPROLE_ROLE, roleName, "/role-id"), payload, token);
/* Response should be code 204 without content */ /* Issue request anx expect code 204 with empty response */
if (!response.isEmpty()) { request.postWithoutResponse(String.format(PATH_AUTH_APPROLE_ROLE, roleName, "/role-id"), payload, token);
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE);
}
return true; return true;
} }
@ -461,17 +446,12 @@ public class HTTPVaultConnector implements VaultConnector {
throws VaultConnectorException { throws VaultConnectorException {
requireAuth(); requireAuth();
/* Request HTTP response and expect empty result */ /* Issue request anx expect code 204 with empty response */
String response = request.post( request.postWithoutResponse(
String.format(PATH_AUTH_APPROLE_ROLE, roleName, "/secret-id/destroy"), String.format(PATH_AUTH_APPROLE_ROLE, roleName, "/secret-id/destroy"),
new AppRoleSecret(secretID), new AppRoleSecret(secretID),
token); token);
/* Response should be code 204 without content */
if (!response.isEmpty()) {
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE);
}
return true; return true;
} }
@ -552,22 +532,16 @@ public class HTTPVaultConnector implements VaultConnector {
payload = payloadMap; payload = payloadMap;
} }
if (!request.post(key, payload, token).isEmpty()) { /* Issue request anx expect code 204 with empty response */
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE); request.postWithoutResponse(key, payload, token);
}
} }
@Override @Override
public final void delete(final String key) throws VaultConnectorException { public final void delete(final String key) throws VaultConnectorException {
requireAuth(); requireAuth();
/* Request HTTP response and expect empty result */ /* Issue request anx expect code 204 with empty response */
String response = request.delete(key, token); request.deleteWithoutResponse(key, token);
/* Response should be code 204 without content */
if (!response.isEmpty()) {
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE);
}
} }
@Override @Override
@ -611,25 +585,17 @@ public class HTTPVaultConnector implements VaultConnector {
/* Request HTTP response and expect empty result */ /* Request HTTP response and expect empty result */
Map<String, Object> payload = new HashMap<>(); Map<String, Object> payload = new HashMap<>();
payload.put("versions", versions); payload.put("versions", versions);
String response = request.post(mount + pathPart + key, payload, token);
/* Response should be code 204 without content */ /* Issue request anx expect code 204 with empty response */
if (!response.isEmpty()) { request.postWithoutResponse(mount + pathPart + key, payload, token);
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE);
}
} }
@Override @Override
public final void revoke(final String leaseID) throws VaultConnectorException { public final void revoke(final String leaseID) throws VaultConnectorException {
requireAuth(); requireAuth();
/* Request HTTP response and expect empty result */ /* Issue request anx expect code 204 with empty response */
String response = request.put(PATH_REVOKE + leaseID, new HashMap<>(), token); request.putWithoutResponse(PATH_REVOKE + leaseID, new HashMap<>(), token);
/* Response should be code 204 without content */
if (!response.isEmpty()) {
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE);
}
} }
@Override @Override

View File

@ -22,13 +22,13 @@ package de.stklcode.jvault.connector.internal;
* @author Stefan Kalscheuer * @author Stefan Kalscheuer
* @since 0.8 Extracted from static inner class. * @since 0.8 Extracted from static inner class.
*/ */
public final class Error { final class Error {
public static final String READ_RESPONSE = "Unable to read response"; static final String READ_RESPONSE = "Unable to read response";
public static final String PARSE_RESPONSE = "Unable to parse response"; static final String PARSE_RESPONSE = "Unable to parse response";
public static final String UNEXPECTED_RESPONSE = "Received response where none was expected"; static final String UNEXPECTED_RESPONSE = "Received response where none was expected";
public static final String URI_FORMAT = "Invalid URI format"; static final String URI_FORMAT = "Invalid URI format";
public static final String RESPONSE_CODE = "Invalid response code"; static final String RESPONSE_CODE = "Invalid response code";
public static final String INIT_SSL_CONTEXT = "Unable to intialize SSLContext"; static final String INIT_SSL_CONTEXT = "Unable to intialize SSLContext";
/** /**
* Constructor hidden, this class should not be instantiated. * Constructor hidden, this class should not be instantiated.

View File

@ -117,6 +117,20 @@ public final class RequestHelper implements Serializable {
} }
} }
/**
* Execute HTTP request using POST method and expect empty (204) response.
*
* @param path URL path (relative to base).
* @param token Vault token (may be {@code null}).
* @throws VaultConnectorException on connection error
* @since 0.8
*/
public void postWithoutResponse(final String path, final Object payload, final String token) throws VaultConnectorException {
if (!post(path, payload, token).isEmpty()) {
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE);
}
}
/** /**
* Execute HTTP request using PUT method. * Execute HTTP request using PUT method.
* *
@ -171,6 +185,22 @@ public final class RequestHelper implements Serializable {
} }
} }
/**
* Execute HTTP request using PUT method and expect empty (204) response.
*
* @param path URL path (relative to base).
* @param payload Map of payload values (will be converted to JSON).
* @param token Vault token (may be {@code null}).
* @throws VaultConnectorException on connection error
* @since 0.8
*/
public void putWithoutResponse(final String path, final Map<String, String> payload, final String token)
throws VaultConnectorException {
if (!put(path, payload, token).isEmpty()) {
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE);
}
}
/** /**
* Execute HTTP request using DELETE method. * Execute HTTP request using DELETE method.
* *
@ -192,6 +222,20 @@ public final class RequestHelper implements Serializable {
return request(delete, retries); return request(delete, retries);
} }
/**
* Execute HTTP request using DELETE method and expect empty (204) response.
*
* @param path URL path (relative to base).
* @param token Vault token (may be {@code null}).
* @throws VaultConnectorException on connection error
* @since 0.8
*/
public void deleteWithoutResponse(final String path, final String token) throws VaultConnectorException {
if (!delete(path, token).isEmpty()) {
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE);
}
}
/** /**
* Execute HTTP request using GET method. * Execute HTTP request using GET method.
* *