Code style: add curly braces to all one-line if-else blocks

This commit is contained in:
Stefan Kalscheuer 2018-11-20 14:36:29 +01:00
parent 3b2a3dd70a
commit 263669362f
10 changed files with 141 additions and 67 deletions

View File

@ -270,8 +270,9 @@ public class HTTPVaultConnector implements VaultConnector {
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<>(); Map<String, String> param = new HashMap<>();
param.put("key", key); param.put("key", key);
if (reset != null) if (reset != null) {
param.put("reset", reset.toString()); param.put("reset", reset.toString());
}
try { try {
String response = requestPut(PATH_UNSEAL, param); String response = requestPut(PATH_UNSEAL, param);
return jsonMapper.readValue(response, SealResponse.class); return jsonMapper.readValue(response, SealResponse.class);
@ -355,8 +356,9 @@ public class HTTPVaultConnector implements VaultConnector {
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<>(); final Map<String, String> payload = new HashMap<>();
payload.put("role_id", roleID); payload.put("role_id", roleID);
if (secretID != null) if (secretID != null) {
payload.put("secret_id", secretID); payload.put("secret_id", secretID);
}
return queryAuth(PATH_AUTH_APPROLE + "login", payload); return queryAuth(PATH_AUTH_APPROLE + "login", payload);
} }
@ -389,43 +391,49 @@ public class HTTPVaultConnector implements VaultConnector {
@Deprecated @Deprecated
public final boolean registerAppId(final String appID, final String policy, final String displayName) public final boolean registerAppId(final String appID, final String policy, final String displayName)
throws VaultConnectorException { throws VaultConnectorException {
if (!isAuthorized()) if (!isAuthorized()) {
throw new AuthorizationRequiredException(); throw new AuthorizationRequiredException();
}
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 */ /* Get response */
String response = requestPost(PATH_AUTH_APPID + "map/app-id/" + appID, payload); String response = requestPost(PATH_AUTH_APPID + "map/app-id/" + appID, payload);
/* Response should be code 204 without content */ /* Response should be code 204 without content */
if (!response.isEmpty()) if (!response.isEmpty()) {
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE); throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE);
}
return true; return true;
} }
@Override @Override
@Deprecated @Deprecated
public final boolean registerUserId(final String appID, final String userID) throws VaultConnectorException { public final boolean registerUserId(final String appID, final String userID) throws VaultConnectorException {
if (!isAuthorized()) if (!isAuthorized()) {
throw new AuthorizationRequiredException(); throw new AuthorizationRequiredException();
}
Map<String, String> payload = new HashMap<>(); Map<String, String> payload = new HashMap<>();
payload.put("value", appID); payload.put("value", appID);
/* Get response */ /* Get response */
String response = requestPost(PATH_AUTH_APPID + "map/user-id/" + userID, payload); String response = requestPost(PATH_AUTH_APPID + "map/user-id/" + userID, payload);
/* Response should be code 204 without content */ /* Response should be code 204 without content */
if (!response.isEmpty()) if (!response.isEmpty()) {
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE); 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 {
if (!isAuthorized()) if (!isAuthorized()) {
throw new AuthorizationRequiredException(); throw new AuthorizationRequiredException();
}
/* Get response */ /* Get response */
String response = requestPost(String.format(PATH_AUTH_APPROLE_ROLE, role.getName(), ""), role); String response = requestPost(String.format(PATH_AUTH_APPROLE_ROLE, role.getName(), ""), role);
/* Response should be code 204 without content */ /* Response should be code 204 without content */
if (!response.isEmpty()) if (!response.isEmpty()) {
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE); 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());
@ -433,8 +441,9 @@ public class HTTPVaultConnector implements VaultConnector {
@Override @Override
public final AppRoleResponse lookupAppRole(final String roleName) throws VaultConnectorException { public final AppRoleResponse lookupAppRole(final String roleName) throws VaultConnectorException {
if (!isAuthorized()) if (!isAuthorized()) {
throw new AuthorizationRequiredException(); throw new AuthorizationRequiredException();
}
/* Request HTTP response and parse Secret */ /* Request HTTP response and parse Secret */
try { try {
String response = requestGet(String.format(PATH_AUTH_APPROLE_ROLE, roleName, ""), new HashMap<>()); String response = requestGet(String.format(PATH_AUTH_APPROLE_ROLE, roleName, ""), new HashMap<>());
@ -449,23 +458,26 @@ public class HTTPVaultConnector implements VaultConnector {
@Override @Override
public final boolean deleteAppRole(final String roleName) throws VaultConnectorException { public final boolean deleteAppRole(final String roleName) throws VaultConnectorException {
if (!isAuthorized()) if (!isAuthorized()) {
throw new AuthorizationRequiredException(); throw new AuthorizationRequiredException();
}
/* Request HTTP response and expect empty result */ /* Request HTTP response and expect empty result */
String response = requestDelete(String.format(PATH_AUTH_APPROLE_ROLE, roleName, "")); String response = requestDelete(String.format(PATH_AUTH_APPROLE_ROLE, roleName, ""));
/* Response should be code 204 without content */ /* Response should be code 204 without content */
if (!response.isEmpty()) if (!response.isEmpty()) {
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE); throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE);
}
return true; return true;
} }
@Override @Override
public final String getAppRoleID(final String roleName) throws VaultConnectorException { public final String getAppRoleID(final String roleName) throws VaultConnectorException {
if (!isAuthorized()) if (!isAuthorized()) {
throw new AuthorizationRequiredException(); throw new AuthorizationRequiredException();
}
/* Request HTTP response and parse Secret */ /* Request HTTP response and parse Secret */
try { try {
String response = requestGet(String.format(PATH_AUTH_APPROLE_ROLE, roleName, "/role-id"), new HashMap<>()); String response = requestGet(String.format(PATH_AUTH_APPROLE_ROLE, roleName, "/role-id"), new HashMap<>());
@ -480,29 +492,33 @@ public class HTTPVaultConnector implements VaultConnector {
@Override @Override
public final boolean setAppRoleID(final String roleName, final String roleID) throws VaultConnectorException { public final boolean setAppRoleID(final String roleName, final String roleID) throws VaultConnectorException {
if (!isAuthorized()) if (!isAuthorized()) {
throw new AuthorizationRequiredException(); throw new AuthorizationRequiredException();
}
/* 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 = requestPost(String.format(PATH_AUTH_APPROLE_ROLE, roleName, "/role-id"), payload); String response = requestPost(String.format(PATH_AUTH_APPROLE_ROLE, roleName, "/role-id"), payload);
/* Response should be code 204 without content */ /* Response should be code 204 without content */
if (!response.isEmpty()) if (!response.isEmpty()) {
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE); throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE);
}
return true; return true;
} }
@Override @Override
public final AppRoleSecretResponse createAppRoleSecret(final String roleName, final AppRoleSecret secret) public final AppRoleSecretResponse createAppRoleSecret(final String roleName, final AppRoleSecret secret)
throws VaultConnectorException { throws VaultConnectorException {
if (!isAuthorized()) if (!isAuthorized()) {
throw new AuthorizationRequiredException(); throw new AuthorizationRequiredException();
}
/* Get response */ /* Get response */
String response; String response;
if (secret.getId() != null && !secret.getId().isEmpty()) if (secret.getId() != null && !secret.getId().isEmpty()) {
response = requestPost(String.format(PATH_AUTH_APPROLE_ROLE, roleName, "/custom-secret-id"), secret); response = requestPost(String.format(PATH_AUTH_APPROLE_ROLE, roleName, "/custom-secret-id"), secret);
else } else {
response = requestPost(String.format(PATH_AUTH_APPROLE_ROLE, roleName, "/secret-id"), secret); response = requestPost(String.format(PATH_AUTH_APPROLE_ROLE, roleName, "/secret-id"), secret);
}
try { try {
/* Extract the secret ID from response */ /* Extract the secret ID from response */
@ -515,8 +531,9 @@ public class HTTPVaultConnector implements VaultConnector {
@Override @Override
public final AppRoleSecretResponse lookupAppRoleSecret(final String roleName, final String secretID) public final AppRoleSecretResponse lookupAppRoleSecret(final String roleName, final String secretID)
throws VaultConnectorException { throws VaultConnectorException {
if (!isAuthorized()) if (!isAuthorized()) {
throw new AuthorizationRequiredException(); throw new AuthorizationRequiredException();
}
/* Request HTTP response and parse Secret */ /* Request HTTP response and parse Secret */
try { try {
String response = requestPost( String response = requestPost(
@ -531,8 +548,9 @@ public class HTTPVaultConnector implements VaultConnector {
@Override @Override
public final boolean destroyAppRoleSecret(final String roleName, final String secretID) public final boolean destroyAppRoleSecret(final String roleName, final String secretID)
throws VaultConnectorException { throws VaultConnectorException {
if (!isAuthorized()) if (!isAuthorized()) {
throw new AuthorizationRequiredException(); throw new AuthorizationRequiredException();
}
/* Request HTTP response and expect empty result */ /* Request HTTP response and expect empty result */
String response = requestPost( String response = requestPost(
@ -540,16 +558,18 @@ public class HTTPVaultConnector implements VaultConnector {
new AppRoleSecret(secretID)); new AppRoleSecret(secretID));
/* Response should be code 204 without content */ /* Response should be code 204 without content */
if (!response.isEmpty()) if (!response.isEmpty()) {
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE); throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE);
}
return true; return true;
} }
@Override @Override
public final List<String> listAppRoles() throws VaultConnectorException { public final List<String> listAppRoles() throws VaultConnectorException {
if (!isAuthorized()) if (!isAuthorized()) {
throw new AuthorizationRequiredException(); throw new AuthorizationRequiredException();
}
try { try {
String response = requestGet(PATH_AUTH_APPROLE + "role?list=true", new HashMap<>()); String response = requestGet(PATH_AUTH_APPROLE + "role?list=true", new HashMap<>());
@ -565,8 +585,9 @@ public class HTTPVaultConnector implements VaultConnector {
@Override @Override
public final List<String> listAppRoleSecrets(final String roleName) throws VaultConnectorException { public final List<String> listAppRoleSecrets(final String roleName) throws VaultConnectorException {
if (!isAuthorized()) if (!isAuthorized()) {
throw new AuthorizationRequiredException(); throw new AuthorizationRequiredException();
}
try { try {
String response = requestGet( String response = requestGet(
@ -584,8 +605,9 @@ public class HTTPVaultConnector implements VaultConnector {
@Override @Override
public final SecretResponse read(final String key) throws VaultConnectorException { public final SecretResponse read(final String key) throws VaultConnectorException {
if (!isAuthorized()) if (!isAuthorized()) {
throw new AuthorizationRequiredException(); throw new AuthorizationRequiredException();
}
/* Request HTTP response and parse Secret */ /* Request HTTP response and parse Secret */
try { try {
String response = requestGet(key, new HashMap<>()); String response = requestGet(key, new HashMap<>());
@ -600,8 +622,9 @@ public class HTTPVaultConnector implements VaultConnector {
@Override @Override
public final List<String> list(final String path) throws VaultConnectorException { public final List<String> list(final String path) throws VaultConnectorException {
if (!isAuthorized()) if (!isAuthorized()) {
throw new AuthorizationRequiredException(); throw new AuthorizationRequiredException();
}
try { try {
String response = requestGet(path + "/?list=true", new HashMap<>()); String response = requestGet(path + "/?list=true", new HashMap<>());
@ -617,51 +640,60 @@ 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) 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()) if (!requestPost(key, data).isEmpty()) {
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE); throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE);
} }
}
@Override @Override
public final void delete(final String key) throws VaultConnectorException { public final void delete(final String key) throws VaultConnectorException {
if (!isAuthorized()) if (!isAuthorized()) {
throw new AuthorizationRequiredException(); throw new AuthorizationRequiredException();
}
/* Request HTTP response and expect empty result */ /* Request HTTP response and expect empty result */
String response = requestDelete(key); String response = requestDelete(key);
/* Response should be code 204 without content */ /* Response should be code 204 without content */
if (!response.isEmpty()) if (!response.isEmpty()) {
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE); 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 {
if (!isAuthorized()) if (!isAuthorized()) {
throw new AuthorizationRequiredException(); throw new AuthorizationRequiredException();
}
/* Request HTTP response and expect empty result */ /* Request HTTP response and expect empty result */
String response = requestPut(PATH_REVOKE + leaseID, new HashMap<>()); String response = requestPut(PATH_REVOKE + leaseID, new HashMap<>());
/* Response should be code 204 without content */ /* Response should be code 204 without content */
if (!response.isEmpty()) if (!response.isEmpty()) {
throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE); throw new InvalidResponseException(Error.UNEXPECTED_RESPONSE);
} }
}
@Override @Override
public final SecretResponse renew(final String leaseID, final Integer increment) throws VaultConnectorException { public final SecretResponse renew(final String leaseID, final Integer increment) throws VaultConnectorException {
if (!isAuthorized()) if (!isAuthorized()) {
throw new AuthorizationRequiredException(); throw new AuthorizationRequiredException();
}
Map<String, String> payload = new HashMap<>(); Map<String, String> payload = new HashMap<>();
payload.put("lease_id", leaseID); payload.put("lease_id", leaseID);
if (increment != null) if (increment != null) {
payload.put("increment", increment.toString()); payload.put("increment", increment.toString());
}
/* Request HTTP response and parse Secret */ /* Request HTTP response and parse Secret */
try { try {
@ -684,8 +716,9 @@ public class HTTPVaultConnector implements VaultConnector {
@Override @Override
public final AuthResponse createToken(final Token token, final String role) throws VaultConnectorException { public final AuthResponse createToken(final Token token, final String role) throws VaultConnectorException {
if (role == null || role.isEmpty()) if (role == null || role.isEmpty()) {
throw new InvalidRequestException("No role name specified."); throw new InvalidRequestException("No role name specified.");
}
return createTokenInternal(token, PATH_TOKEN + PATH_CREATE + "/" + role); return createTokenInternal(token, PATH_TOKEN + PATH_CREATE + "/" + role);
} }
@ -706,11 +739,13 @@ public class HTTPVaultConnector implements VaultConnector {
* @throws VaultConnectorException on error * @throws VaultConnectorException on error
*/ */
private AuthResponse createTokenInternal(final Token token, final String path) throws VaultConnectorException { private AuthResponse createTokenInternal(final Token token, final String path) throws VaultConnectorException {
if (!isAuthorized()) if (!isAuthorized()) {
throw new AuthorizationRequiredException(); throw new AuthorizationRequiredException();
}
if (token == null) if (token == null) {
throw new InvalidRequestException("Token must be provided."); throw new InvalidRequestException("Token must be provided.");
}
String response = requestPost(path, token); String response = requestPost(path, token);
try { try {
@ -722,8 +757,10 @@ public class HTTPVaultConnector implements VaultConnector {
@Override @Override
public final TokenResponse lookupToken(final String token) throws VaultConnectorException { public final TokenResponse lookupToken(final String token) throws VaultConnectorException {
if (!isAuthorized()) if (!isAuthorized()) {
throw new AuthorizationRequiredException(); throw new AuthorizationRequiredException();
}
/* Request HTTP response and parse Secret */ /* Request HTTP response and parse Secret */
try { try {
String response = requestGet(PATH_TOKEN + "/lookup/" + token, new HashMap<>()); String response = requestGet(PATH_TOKEN + "/lookup/" + token, new HashMap<>());
@ -749,6 +786,7 @@ public class HTTPVaultConnector implements VaultConnector {
private String requestPost(final String path, final Object payload) throws VaultConnectorException { private String requestPost(final String path, final Object payload) throws VaultConnectorException {
/* Initialize post */ /* Initialize post */
HttpPost post = new HttpPost(baseURL + path); HttpPost post = new HttpPost(baseURL + path);
/* generate JSON from payload */ /* generate JSON from payload */
StringEntity input; StringEntity input;
try { try {
@ -759,9 +797,11 @@ public class HTTPVaultConnector implements VaultConnector {
input.setContentEncoding("UTF-8"); input.setContentEncoding("UTF-8");
input.setContentType("application/json"); input.setContentType("application/json");
post.setEntity(input); post.setEntity(input);
/* Set X-Vault-Token header */ /* Set X-Vault-Token header */
if (token != null) if (token != null) {
post.addHeader(HEADER_VAULT_TOKEN, token); post.addHeader(HEADER_VAULT_TOKEN, token);
}
return request(post, retries); return request(post, retries);
} }
@ -777,6 +817,7 @@ public class HTTPVaultConnector implements VaultConnector {
private String requestPut(final String path, final Map<String, String> payload) throws VaultConnectorException { private String requestPut(final String path, final Map<String, String> payload) throws VaultConnectorException {
/* Initialize put */ /* Initialize put */
HttpPut put = new HttpPut(baseURL + path); HttpPut put = new HttpPut(baseURL + path);
/* generate JSON from payload */ /* generate JSON from payload */
StringEntity entity = null; StringEntity entity = null;
try { try {
@ -784,11 +825,14 @@ public class HTTPVaultConnector implements VaultConnector {
} catch (UnsupportedEncodingException | JsonProcessingException e) { } catch (UnsupportedEncodingException | JsonProcessingException e) {
throw new InvalidRequestException("Payload serialization failed", e); throw new InvalidRequestException("Payload serialization failed", e);
} }
/* Parse parameters */ /* Parse parameters */
put.setEntity(entity); put.setEntity(entity);
/* Set X-Vault-Token header */ /* Set X-Vault-Token header */
if (token != null) if (token != null) {
put.addHeader(HEADER_VAULT_TOKEN, token); put.addHeader(HEADER_VAULT_TOKEN, token);
}
return request(put, retries); return request(put, retries);
} }
@ -803,9 +847,11 @@ public class HTTPVaultConnector implements VaultConnector {
private String requestDelete(final String path) throws VaultConnectorException { private String requestDelete(final String path) throws VaultConnectorException {
/* Initialize delete */ /* Initialize delete */
HttpDelete delete = new HttpDelete(baseURL + path); HttpDelete delete = new HttpDelete(baseURL + path);
/* Set X-Vault-Token header */ /* Set X-Vault-Token header */
if (token != null) if (token != null) {
delete.addHeader(HEADER_VAULT_TOKEN, token); delete.addHeader(HEADER_VAULT_TOKEN, token);
}
return request(delete, retries); return request(delete, retries);
} }
@ -829,8 +875,9 @@ public class HTTPVaultConnector implements VaultConnector {
HttpGet get = new HttpGet(uriBuilder.build()); HttpGet get = new HttpGet(uriBuilder.build());
/* Set X-Vault-Token header */ /* Set X-Vault-Token header */
if (token != null) if (token != null) {
get.addHeader(HEADER_VAULT_TOKEN, token); get.addHeader(HEADER_VAULT_TOKEN, token);
}
return request(get, retries); return request(get, retries);
} }
@ -853,13 +900,17 @@ public class HTTPVaultConnector implements VaultConnector {
.setSSLSocketFactory(createSSLSocketFactory()) .setSSLSocketFactory(createSSLSocketFactory())
.build()) { .build()) {
/* Set custom timeout, if defined */ /* Set custom timeout, if defined */
if (this.timeout != null) if (this.timeout != null) {
base.setConfig(RequestConfig.copy(RequestConfig.DEFAULT).setConnectTimeout(timeout).build()); base.setConfig(RequestConfig.copy(RequestConfig.DEFAULT).setConnectTimeout(timeout).build());
}
/* Execute request */ /* Execute request */
response = httpClient.execute(base); response = httpClient.execute(base);
/* Check if response is valid */ /* Check if response is valid */
if (response == null) if (response == null) {
throw new InvalidResponseException("Response unavailable"); throw new InvalidResponseException("Response unavailable");
}
switch (response.getStatusLine().getStatusCode()) { switch (response.getStatusLine().getStatusCode()) {
case 200: case 200:
@ -885,7 +936,7 @@ public class HTTPVaultConnector implements VaultConnector {
} catch (IOException e) { } catch (IOException e) {
throw new InvalidResponseException(Error.READ_RESPONSE, e); throw new InvalidResponseException(Error.READ_RESPONSE, e);
} finally { } finally {
if (response != null && response.getEntity() != null) if (response != null && response.getEntity() != null) {
try { try {
EntityUtils.consume(response.getEntity()); EntityUtils.consume(response.getEntity());
} catch (IOException ignored) { } catch (IOException ignored) {
@ -893,6 +944,7 @@ public class HTTPVaultConnector implements VaultConnector {
} }
} }
} }
}
/** /**
* Handle successful result. * Handle successful result.
@ -923,8 +975,9 @@ public class HTTPVaultConnector implements VaultConnector {
String responseString = br.lines().collect(Collectors.joining("\n")); String responseString = br.lines().collect(Collectors.joining("\n"));
ErrorResponse er = jsonMapper.readValue(responseString, ErrorResponse.class); ErrorResponse er = jsonMapper.readValue(responseString, ErrorResponse.class);
/* Check for "permission denied" response */ /* Check for "permission denied" response */
if (!er.getErrors().isEmpty() && er.getErrors().get(0).equals("permission denied")) if (!er.getErrors().isEmpty() && er.getErrors().get(0).equals("permission denied")) {
throw new PermissionDeniedException(); throw new PermissionDeniedException();
}
throw new InvalidResponseException(Error.RESPONSE_CODE, throw new InvalidResponseException(Error.RESPONSE_CODE,
response.getStatusLine().getStatusCode(), er.toString()); response.getStatusLine().getStatusCode(), er.toString());
} catch (IOException ignored) { } catch (IOException ignored) {

View File

@ -103,7 +103,7 @@ public final class AppRole {
/** /**
* Construct complete {@link AppRole} object. * Construct complete {@link AppRole} object.
* * <p>
* This constructor is used for transition from {@code bound_cidr_list} to {@code secret_id_bound_cidrs} only. * This constructor is used for transition from {@code bound_cidr_list} to {@code secret_id_bound_cidrs} only.
* *
* @param name Role name (required) * @param name Role name (required)
@ -182,8 +182,9 @@ public final class AppRole {
@JsonGetter("bound_cidr_list") @JsonGetter("bound_cidr_list")
@JsonInclude(JsonInclude.Include.NON_EMPTY) @JsonInclude(JsonInclude.Include.NON_EMPTY)
public String getBoundCidrListString() { public String getBoundCidrListString() {
if (boundCidrList == null || boundCidrList.isEmpty()) if (boundCidrList == null || boundCidrList.isEmpty()) {
return ""; return "";
}
return String.join(",", boundCidrList); return String.join(",", boundCidrList);
} }
@ -238,8 +239,9 @@ public final class AppRole {
@JsonGetter("policies") @JsonGetter("policies")
@JsonInclude(JsonInclude.Include.NON_EMPTY) @JsonInclude(JsonInclude.Include.NON_EMPTY)
public String getPoliciesString() { public String getPoliciesString() {
if (policies == null || policies.isEmpty()) if (policies == null || policies.isEmpty()) {
return ""; return "";
}
return String.join(",", policies); return String.join(",", policies);
} }

View File

@ -140,8 +140,9 @@ public final class AppRoleBuilder {
* @return self * @return self
*/ */
public AppRoleBuilder withPolicies(final List<String> policies) { public AppRoleBuilder withPolicies(final List<String> policies) {
if (this.policies == null) if (this.policies == null) {
this.policies = new ArrayList<>(); this.policies = new ArrayList<>();
}
this.policies.addAll(policies); this.policies.addAll(policies);
return this; return this;
} }
@ -153,8 +154,9 @@ public final class AppRoleBuilder {
* @return self * @return self
*/ */
public AppRoleBuilder withPolicy(final String policy) { public AppRoleBuilder withPolicy(final String policy) {
if (this.policies == null) if (this.policies == null) {
this.policies = new ArrayList<>(); this.policies = new ArrayList<>();
}
policies.add(policy); policies.add(policy);
return this; return this;
} }

View File

@ -126,8 +126,9 @@ public final class AppRoleSecret {
*/ */
@JsonGetter("cidr_list") @JsonGetter("cidr_list")
public String getCidrListString() { public String getCidrListString() {
if (cidrList == null || cidrList.isEmpty()) if (cidrList == null || cidrList.isEmpty()) {
return ""; return "";
}
return String.join(",", cidrList); return String.join(",", cidrList);
} }

View File

@ -48,9 +48,11 @@ public enum AuthBackend {
* @return Auth backend value * @return Auth backend value
*/ */
public static AuthBackend forType(final String type) { public static AuthBackend forType(final String type) {
for (AuthBackend v : values()) for (AuthBackend v : values()) {
if (v.type.equalsIgnoreCase(type)) if (v.type.equalsIgnoreCase(type)) {
return v; return v;
}
}
return UNKNOWN; return UNKNOWN;
} }
} }

View File

@ -159,8 +159,9 @@ public final class TokenBuilder {
* @return self * @return self
*/ */
public TokenBuilder withPolicies(final List<String> policies) { public TokenBuilder withPolicies(final List<String> policies) {
if (this.policies == null) if (this.policies == null) {
this.policies = new ArrayList<>(); this.policies = new ArrayList<>();
}
this.policies.addAll(policies); this.policies.addAll(policies);
return this; return this;
} }
@ -172,8 +173,9 @@ public final class TokenBuilder {
* @return self * @return self
*/ */
public TokenBuilder withPolicy(final String policy) { public TokenBuilder withPolicy(final String policy) {
if (this.policies == null) if (this.policies == null) {
this.policies = new ArrayList<>(); this.policies = new ArrayList<>();
}
policies.add(policy); policies.add(policy);
return this; return this;
} }
@ -185,8 +187,9 @@ public final class TokenBuilder {
* @return self * @return self
*/ */
public TokenBuilder withMeta(final Map<String, String> meta) { public TokenBuilder withMeta(final Map<String, String> meta) {
if (this.meta == null) if (this.meta == null) {
this.meta = new HashMap<>(); this.meta = new HashMap<>();
}
this.meta.putAll(meta); this.meta.putAll(meta);
return this; return this;
} }
@ -199,8 +202,9 @@ public final class TokenBuilder {
* @return self * @return self
*/ */
public TokenBuilder withMeta(final String key, final String value) { public TokenBuilder withMeta(final String key, final String value) {
if (this.meta == null) if (this.meta == null) {
this.meta = new HashMap<>(); this.meta = new HashMap<>();
}
this.meta.put(key, value); this.meta.put(key, value);
return this; return this;
} }

View File

@ -42,7 +42,9 @@ public final class AppRoleResponse extends VaultDataResponse {
/* null empty strings on list objects */ /* null empty strings on list objects */
Map<String, Object> filteredData = new HashMap<>(); Map<String, Object> filteredData = new HashMap<>();
data.forEach((k, v) -> { data.forEach((k, v) -> {
if (!(v instanceof String && ((String) v).isEmpty())) filteredData.put(k, v); if (!(v instanceof String && ((String) v).isEmpty())) {
filteredData.put(k, v);
}
}); });
this.role = mapper.readValue(mapper.writeValueAsString(filteredData), AppRole.class); this.role = mapper.readValue(mapper.writeValueAsString(filteredData), AppRole.class);
} catch (IOException e) { } catch (IOException e) {

View File

@ -42,7 +42,9 @@ public final class AppRoleSecretResponse extends VaultDataResponse {
/* null empty strings on list objects */ /* null empty strings on list objects */
Map<String, Object> filteredData = new HashMap<>(); Map<String, Object> filteredData = new HashMap<>();
data.forEach((k, v) -> { data.forEach((k, v) -> {
if (!(v instanceof String && ((String) v).isEmpty())) filteredData.put(k, v); if (!(v instanceof String && ((String) v).isEmpty())) {
filteredData.put(k, v);
}
}); });
this.secret = mapper.readValue(mapper.writeValueAsString(filteredData), AppRoleSecret.class); this.secret = mapper.readValue(mapper.writeValueAsString(filteredData), AppRoleSecret.class);
} catch (IOException e) { } catch (IOException e) {

View File

@ -32,8 +32,9 @@ public final class CredentialsResponse extends SecretResponse {
*/ */
public String getUsername() { public String getUsername() {
Object username = get("username"); Object username = get("username");
if (username != null) if (username != null) {
return username.toString(); return username.toString();
}
return null; return null;
} }
@ -42,8 +43,9 @@ public final class CredentialsResponse extends SecretResponse {
*/ */
public String getPassword() { public String getPassword() {
Object password = get("password"); Object password = get("password");
if (password != null) if (password != null) {
return password.toString(); return password.toString();
}
return null; return null;
} }
} }

View File

@ -46,8 +46,9 @@ public class SecretResponse extends VaultDataResponse {
* @since 0.4.0 * @since 0.4.0
*/ */
public final Map<String, Object> getData() { public final Map<String, Object> getData() {
if (data == null) if (data == null) {
return new HashMap<>(); return new HashMap<>();
}
return data; return data;
} }
@ -59,8 +60,9 @@ public class SecretResponse extends VaultDataResponse {
* @since 0.4.0 * @since 0.4.0
*/ */
public final Object get(final String key) { public final Object get(final String key) {
if (data == null) if (data == null) {
return null; return null;
}
return getData().get(key); return getData().get(key);
} }
@ -74,8 +76,9 @@ public class SecretResponse extends VaultDataResponse {
@Deprecated @Deprecated
public final String getValue() { public final String getValue() {
Object value = get("value"); Object value = get("value");
if (value == null) if (value == null) {
return null; return null;
}
return value.toString(); return value.toString();
} }
@ -107,8 +110,9 @@ public class SecretResponse extends VaultDataResponse {
public final <T> T get(final String key, final Class<T> type) throws InvalidResponseException { public final <T> T get(final String key, final Class<T> type) throws InvalidResponseException {
try { try {
Object rawValue = get(key); Object rawValue = get(key);
if (rawValue == null) if (rawValue == null) {
return null; return null;
}
return new ObjectMapper().readValue(rawValue.toString(), type); return new ObjectMapper().readValue(rawValue.toString(), type);
} catch (IOException e) { } catch (IOException e) {
throw new InvalidResponseException("Unable to parse response payload: " + e.getMessage()); throw new InvalidResponseException("Unable to parse response payload: " + e.getMessage());