combine payload map creation into subroutines
We create payload maps with conditional argument sets in several places. Combine the generation into subroutines, to keep the actual endpoint calls short and clear.
This commit is contained in:
parent
39ac32a2f6
commit
712fab04e1
@ -145,11 +145,10 @@ public class HTTPVaultConnector implements VaultConnector {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final SealResponse unseal(final String key, final Boolean reset) throws VaultConnectorException {
|
public final SealResponse unseal(final String key, final Boolean reset) throws VaultConnectorException {
|
||||||
Map<String, String> param = new HashMap<>(2, 1);
|
Map<String, String> param = mapOfStrings(
|
||||||
param.put("key", key);
|
"key", key,
|
||||||
if (reset != null) {
|
"reset", reset
|
||||||
param.put("reset", reset.toString());
|
);
|
||||||
}
|
|
||||||
|
|
||||||
return request.put(PATH_UNSEAL, param, token, SealResponse.class);
|
return request.put(PATH_UNSEAL, param, token, SealResponse.class);
|
||||||
}
|
}
|
||||||
@ -215,11 +214,10 @@ public class HTTPVaultConnector implements VaultConnector {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final AuthResponse authAppRole(final String roleID, final String secretID) throws VaultConnectorException {
|
public final AuthResponse authAppRole(final String roleID, final String secretID) throws VaultConnectorException {
|
||||||
final Map<String, String> payload = new HashMap<>(2, 1);
|
final Map<String, String> payload = mapOfStrings(
|
||||||
payload.put("role_id", roleID);
|
"role_id", roleID,
|
||||||
if (secretID != null) {
|
"secret_id", secretID
|
||||||
payload.put("secret_id", secretID);
|
);
|
||||||
}
|
|
||||||
return queryAuth(PATH_AUTH_APPROLE + PATH_LOGIN, payload);
|
return queryAuth(PATH_AUTH_APPROLE + PATH_LOGIN, payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -424,10 +422,7 @@ public class HTTPVaultConnector implements VaultConnector {
|
|||||||
public final SecretResponse readSecretVersion(final String mount, final String key, final Integer version) throws VaultConnectorException {
|
public final SecretResponse readSecretVersion(final String mount, final String key, final Integer version) throws VaultConnectorException {
|
||||||
requireAuth();
|
requireAuth();
|
||||||
/* Request HTTP response and parse secret metadata */
|
/* Request HTTP response and parse secret metadata */
|
||||||
Map<String, String> args = new HashMap<>(1, 1);
|
Map<String, String> args = mapOfStrings("version", version);
|
||||||
if (version != null) {
|
|
||||||
args.put("version", version.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
return request.get(mount + PATH_DATA + key, args, token, MetaSecretResponse.class);
|
return request.get(mount + PATH_DATA + key, args, token, MetaSecretResponse.class);
|
||||||
}
|
}
|
||||||
@ -444,11 +439,10 @@ public class HTTPVaultConnector implements VaultConnector {
|
|||||||
public void updateSecretMetadata(final String mount, final String key, final Integer maxVersions, final boolean casRequired) throws VaultConnectorException {
|
public void updateSecretMetadata(final String mount, final String key, final Integer maxVersions, final boolean casRequired) throws VaultConnectorException {
|
||||||
requireAuth();
|
requireAuth();
|
||||||
|
|
||||||
Map<String, Object> payload = new HashMap<>(2, 1);
|
Map<String, Object> payload = mapOf(
|
||||||
if (maxVersions != null) {
|
"max_versions", maxVersions,
|
||||||
payload.put("max_versions", maxVersions);
|
"cas_required", casRequired
|
||||||
}
|
);
|
||||||
payload.put("cas_required", casRequired);
|
|
||||||
|
|
||||||
write(mount + PATH_METADATA + key, payload);
|
write(mount + PATH_METADATA + key, payload);
|
||||||
}
|
}
|
||||||
@ -462,12 +456,7 @@ public class HTTPVaultConnector implements VaultConnector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add CAS value to options map if present.
|
// Add CAS value to options map if present.
|
||||||
Map<String, Object> options;
|
Map<String, Object> options = mapOf("cas", cas);
|
||||||
if (cas != null) {
|
|
||||||
options = singletonMap("cas", cas);
|
|
||||||
} else {
|
|
||||||
options = emptyMap();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Issue request and parse metadata response */
|
/* Issue request and parse metadata response */
|
||||||
return request.post(
|
return request.post(
|
||||||
@ -578,11 +567,10 @@ public class HTTPVaultConnector implements VaultConnector {
|
|||||||
public final SecretResponse renew(final String leaseID, final Integer increment) throws VaultConnectorException {
|
public final SecretResponse renew(final String leaseID, final Integer increment) throws VaultConnectorException {
|
||||||
requireAuth();
|
requireAuth();
|
||||||
|
|
||||||
Map<String, String> payload = new HashMap<>(2, 1);
|
Map<String, String> payload = mapOfStrings(
|
||||||
payload.put("lease_id", leaseID);
|
"lease_id", leaseID,
|
||||||
if (increment != null) {
|
"increment", increment
|
||||||
payload.put("increment", increment.toString());
|
);
|
||||||
}
|
|
||||||
|
|
||||||
/* Issue request and parse secret response */
|
/* Issue request and parse secret response */
|
||||||
return request.put(PATH_RENEW, payload, token, SecretResponse.class);
|
return request.put(PATH_RENEW, payload, token, SecretResponse.class);
|
||||||
@ -701,4 +689,42 @@ public class HTTPVaultConnector implements VaultConnector {
|
|||||||
throw new AuthorizationRequiredException();
|
throw new AuthorizationRequiredException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a map of non-null {@link String} keys and values
|
||||||
|
*
|
||||||
|
* @param keyValues Key-value tuples as vararg.
|
||||||
|
* @return The map of non-null keys and values.
|
||||||
|
*/
|
||||||
|
private static Map<String, String> mapOfStrings(Object... keyValues) {
|
||||||
|
Map<String, String> map = new HashMap<>(keyValues.length / 2, 1);
|
||||||
|
for (int i = 0; i < keyValues.length -1; i = i + 2) {
|
||||||
|
Object key = keyValues[i];
|
||||||
|
Object val = keyValues[i + 1];
|
||||||
|
if (key instanceof String && val != null) {
|
||||||
|
map.put((String) key, val.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a map of non-null {@link String} keys and {@link Object} values
|
||||||
|
*
|
||||||
|
* @param keyValues Key-value tuples as vararg.
|
||||||
|
* @return The map of non-null keys and values.
|
||||||
|
*/
|
||||||
|
private static Map<String, Object> mapOf(Object... keyValues) {
|
||||||
|
Map<String, Object> map = new HashMap<>(keyValues.length / 2, 1);
|
||||||
|
for (int i = 0; i < keyValues.length; i = i + 2) {
|
||||||
|
Object key = keyValues[i];
|
||||||
|
Object val = keyValues[i + 1];
|
||||||
|
if (key instanceof String && val != null) {
|
||||||
|
map.put((String) key, val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return map;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user