Move 204 empty checks into request helper and make Error class private
This commit is contained in:
parent
22a48d4a90
commit
e4cf8a1dde
@ -18,9 +18,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.InvalidResponseException;
|
||||
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.model.AppRole;
|
||||
import de.stklcode.jvault.connector.model.AppRoleSecret;
|
||||
@ -335,12 +333,10 @@ public class HTTPVaultConnector implements VaultConnector {
|
||||
Map<String, String> payload = new HashMap<>();
|
||||
payload.put("value", policy);
|
||||
payload.put("display_name", displayName);
|
||||
/* Get response */
|
||||
String response = request.post(PATH_AUTH_APPID + "map/app-id/" + appID, payload, token);
|
||||
/* Response should be code 204 without content */
|
||||
if (!response.isEmpty()) {
|
||||
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE);
|
||||
}
|
||||
|
||||
/* Issue request anx expect code 204 with empty response */
|
||||
request.postWithoutResponse(PATH_AUTH_APPID + "map/app-id/" + appID, payload, token);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -350,24 +346,19 @@ public class HTTPVaultConnector implements VaultConnector {
|
||||
requireAuth();
|
||||
Map<String, String> payload = new HashMap<>();
|
||||
payload.put("value", appID);
|
||||
/* Get response */
|
||||
String response = request.post(PATH_AUTH_APPID + "map/user-id/" + userID, payload, token);
|
||||
/* Response should be code 204 without content */
|
||||
if (!response.isEmpty()) {
|
||||
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE);
|
||||
}
|
||||
|
||||
/* Issue request anx expect code 204 with empty response */
|
||||
request.postWithoutResponse(PATH_AUTH_APPID + "map/user-id/" + userID, payload, token);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean createAppRole(final AppRole role) throws VaultConnectorException {
|
||||
requireAuth();
|
||||
/* Get response */
|
||||
String response = request.post(String.format(PATH_AUTH_APPROLE_ROLE, role.getName(), ""), role, token);
|
||||
/* Response should be code 204 without content */
|
||||
if (!response.isEmpty()) {
|
||||
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE);
|
||||
}
|
||||
|
||||
/* Issue request anx expect code 204 with empty response */
|
||||
request.postWithoutResponse(String.format(PATH_AUTH_APPROLE_ROLE, role.getName(), ""), role, token);
|
||||
|
||||
/* Set custom ID if provided */
|
||||
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 {
|
||||
requireAuth();
|
||||
|
||||
/* Request HTTP response and expect empty result */
|
||||
String response = request.delete(String.format(PATH_AUTH_APPROLE_ROLE, roleName, ""), token);
|
||||
|
||||
/* Response should be code 204 without content */
|
||||
if (!response.isEmpty()) {
|
||||
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE);
|
||||
}
|
||||
/* Issue request anx expect code 204 with empty response */
|
||||
request.deleteWithoutResponse(String.format(PATH_AUTH_APPROLE_ROLE, roleName, ""), token);
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -413,11 +399,10 @@ public class HTTPVaultConnector implements VaultConnector {
|
||||
/* Request HTTP response and parse Secret */
|
||||
Map<String, String> payload = new HashMap<>();
|
||||
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 */
|
||||
if (!response.isEmpty()) {
|
||||
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE);
|
||||
}
|
||||
|
||||
/* Issue request anx expect code 204 with empty response */
|
||||
request.postWithoutResponse(String.format(PATH_AUTH_APPROLE_ROLE, roleName, "/role-id"), payload, token);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -461,17 +446,12 @@ public class HTTPVaultConnector implements VaultConnector {
|
||||
throws VaultConnectorException {
|
||||
requireAuth();
|
||||
|
||||
/* Request HTTP response and expect empty result */
|
||||
String response = request.post(
|
||||
/* Issue request anx expect code 204 with empty response */
|
||||
request.postWithoutResponse(
|
||||
String.format(PATH_AUTH_APPROLE_ROLE, roleName, "/secret-id/destroy"),
|
||||
new AppRoleSecret(secretID),
|
||||
token);
|
||||
|
||||
/* Response should be code 204 without content */
|
||||
if (!response.isEmpty()) {
|
||||
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -552,22 +532,16 @@ public class HTTPVaultConnector implements VaultConnector {
|
||||
payload = payloadMap;
|
||||
}
|
||||
|
||||
if (!request.post(key, payload, token).isEmpty()) {
|
||||
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE);
|
||||
}
|
||||
/* Issue request anx expect code 204 with empty response */
|
||||
request.postWithoutResponse(key, payload, token);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void delete(final String key) throws VaultConnectorException {
|
||||
requireAuth();
|
||||
|
||||
/* Request HTTP response and expect empty result */
|
||||
String response = request.delete(key, token);
|
||||
|
||||
/* Response should be code 204 without content */
|
||||
if (!response.isEmpty()) {
|
||||
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE);
|
||||
}
|
||||
/* Issue request anx expect code 204 with empty response */
|
||||
request.deleteWithoutResponse(key, token);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -611,25 +585,17 @@ public class HTTPVaultConnector implements VaultConnector {
|
||||
/* Request HTTP response and expect empty result */
|
||||
Map<String, Object> payload = new HashMap<>();
|
||||
payload.put("versions", versions);
|
||||
String response = request.post(mount + pathPart + key, payload, token);
|
||||
|
||||
/* Response should be code 204 without content */
|
||||
if (!response.isEmpty()) {
|
||||
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE);
|
||||
}
|
||||
/* Issue request anx expect code 204 with empty response */
|
||||
request.postWithoutResponse(mount + pathPart + key, payload, token);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void revoke(final String leaseID) throws VaultConnectorException {
|
||||
requireAuth();
|
||||
|
||||
/* Request HTTP response and expect empty result */
|
||||
String response = request.put(PATH_REVOKE + leaseID, new HashMap<>(), token);
|
||||
|
||||
/* Response should be code 204 without content */
|
||||
if (!response.isEmpty()) {
|
||||
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE);
|
||||
}
|
||||
/* Issue request anx expect code 204 with empty response */
|
||||
request.putWithoutResponse(PATH_REVOKE + leaseID, new HashMap<>(), token);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -22,13 +22,13 @@ package de.stklcode.jvault.connector.internal;
|
||||
* @author Stefan Kalscheuer
|
||||
* @since 0.8 Extracted from static inner class.
|
||||
*/
|
||||
public final class Error {
|
||||
public static final String READ_RESPONSE = "Unable to read response";
|
||||
public static final String PARSE_RESPONSE = "Unable to parse response";
|
||||
public static final String UNEXPECTED_RESPONSE = "Received response where none was expected";
|
||||
public static final String URI_FORMAT = "Invalid URI format";
|
||||
public static final String RESPONSE_CODE = "Invalid response code";
|
||||
public static final String INIT_SSL_CONTEXT = "Unable to intialize SSLContext";
|
||||
final class Error {
|
||||
static final String READ_RESPONSE = "Unable to read response";
|
||||
static final String PARSE_RESPONSE = "Unable to parse response";
|
||||
static final String UNEXPECTED_RESPONSE = "Received response where none was expected";
|
||||
static final String URI_FORMAT = "Invalid URI format";
|
||||
static final String RESPONSE_CODE = "Invalid response code";
|
||||
static final String INIT_SSL_CONTEXT = "Unable to intialize SSLContext";
|
||||
|
||||
/**
|
||||
* Constructor hidden, this class should not be instantiated.
|
||||
|
@ -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.
|
||||
*
|
||||
@ -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.
|
||||
*
|
||||
@ -192,6 +222,20 @@ public final class RequestHelper implements Serializable {
|
||||
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.
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user