Skip to content

Commit

Permalink
Remove unnecessary use of commons-lang
Browse files Browse the repository at this point in the history
  • Loading branch information
chadlwilson committed Jun 24, 2023
1 parent d87681d commit 821c0a9
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 20 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ ext {
}

dependencies {
implementation group: 'cd.go.plugin.base', name: 'gocd-plugin-base', version: '0.0.1'
implementation group: 'cd.go.plugin.base', name: 'gocd-plugin-base', version: '0.0.5'
compileOnly project.deps.gocdPluginApi
implementation group: 'com.google.code.gson', name: 'gson', version: '2.10.1'
implementation group: 'com.amazonaws', name: 'aws-java-sdk-secretsmanager', version: project.versions.awsSdk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.List;

import static com.thoughtworks.gocd.secretmanager.aws.AwsPlugin.LOGGER;
import static org.apache.commons.lang3.StringUtils.*;

public class AWSCredentialsProviderChain {
private final List<AWSCredentialsProvider> credentialsProviders = new LinkedList<AWSCredentialsProvider>();
Expand All @@ -39,20 +38,24 @@ public AWSCredentialsProviderChain(AWSCredentialsProvider... awsCredentialsProvi
}

private AWSStaticCredentialsProvider staticCredentialProvider(String accessKey, String secretKey) {
if (isNoneBlank(accessKey, secretKey)) {
if (!isBlank(accessKey) && !isBlank(secretKey)) {
return new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey));
}

if (isBlank(accessKey) && isNotBlank(secretKey)) {
if (isBlank(accessKey) && !isBlank(secretKey)) {
throw new AWSCredentialsException("Access key is mandatory if secret key is provided");
}

if (isNotBlank(accessKey) && isBlank(secretKey)) {
if (!isBlank(accessKey) && isBlank(secretKey)) {
throw new AWSCredentialsException("Secret key is mandatory if access key is provided");
}
return null;
}

private boolean isBlank(String value) {
return value == null || value.isBlank();
}

public AWSCredentialsProvider getAWSCredentialsProvider(String accessKey, String secretKey) {
final AWSStaticCredentialsProvider staticCredentialProvider = staticCredentialProvider(accessKey, secretKey);
if (staticCredentialProvider != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.thoughtworks.gocd.secretmanager.aws;

import cd.go.plugin.base.executors.secrets.LookupExecutor;
import com.thoughtworks.go.plugin.api.logging.Logger;
import com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse;
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
import com.thoughtworks.gocd.secretmanager.aws.models.Secrets;
Expand All @@ -31,6 +32,8 @@
import static java.util.Collections.singletonMap;

public class SecretConfigLookupExecutor extends LookupExecutor<SecretConfigRequest> {
private static final Logger LOGGER = Logger.getLoggerFor(SecretConfigLookupExecutor.class);

private AWSClientFactory awsClientFactory;

public SecretConfigLookupExecutor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@
import java.util.Map;

import static java.util.Collections.emptyMap;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

public class SecretManagerClient {
private AWSCredentialsProviderChain awsCredentialsProviderChain;
private final AWSCredentialsProviderChain awsCredentialsProviderChain;
private final SecretCache secretCache;
private final AWSSecretsManager awsSecretsManager;

Expand All @@ -47,7 +46,7 @@ public SecretManagerClient(SecretConfig secretConfig, AWSCredentialsProviderChai
public Map lookup(String secretId) {
String secretString = secretCache.getSecretString(secretId);

if (isNotBlank(secretString)) {
if (secretString != null && !secretString.isBlank()) {
return new Gson().fromJson(secretString, Map.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import cd.go.plugin.base.annotations.Property;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -111,7 +110,7 @@ public int hashCode() {
}

private long toLong(String valueAsString, long defaultValue) {
if (StringUtils.isBlank(valueAsString)) {
if (valueAsString == null || valueAsString.isBlank()) {
return defaultValue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@

package com.thoughtworks.gocd.secretmanager.aws.validators;

import cd.go.plugin.base.GsonTransformer;
import cd.go.plugin.base.validation.ValidationResult;
import cd.go.plugin.base.validation.Validator;
import com.thoughtworks.go.plugin.api.logging.Logger;
import com.thoughtworks.gocd.secretmanager.aws.AWSCredentialsProviderChain;
import com.thoughtworks.gocd.secretmanager.aws.exceptions.AWSCredentialsException;
import com.thoughtworks.gocd.secretmanager.aws.models.SecretConfig;

import java.util.Map;

import static cd.go.plugin.base.executors.Executor.GSON;
import static cd.go.plugin.base.executors.Executor.LOGGER;
import static com.thoughtworks.gocd.secretmanager.aws.models.SecretConfig.ACCESS_KEY;
import static com.thoughtworks.gocd.secretmanager.aws.models.SecretConfig.SECRET_ACCESS_KEY;
import static org.apache.commons.lang3.StringUtils.isBlank;

public class CredentialValidator implements Validator {
private static final Logger LOGGER = Logger.getLoggerFor(CredentialValidator.class);
private final AWSCredentialsProviderChain credentialsProviderChain;

public CredentialValidator() {
Expand All @@ -51,7 +51,7 @@ public ValidationResult validate(Map<String, String> requestBody) {
LOGGER.info(e.getMessage());
}

SecretConfig secretConfig = GSON.fromJson(GSON.toJson(requestBody), SecretConfig.class);
SecretConfig secretConfig = GsonTransformer.fromJson(GsonTransformer.toJson(requestBody), SecretConfig.class);

if (isBlank(secretConfig.getAwsAccessKey())) {
validationResult.add(ACCESS_KEY, "Must not be blank.");
Expand All @@ -63,4 +63,8 @@ public ValidationResult validate(Map<String, String> requestBody) {

return validationResult;
}

private boolean isBlank(String value) {
return value == null || value.isBlank();
}
}
12 changes: 6 additions & 6 deletions src/test/resources/secret-config-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,47 @@
{
"key": "Endpoint",
"metadata": {
"displayName": "",
"display_name": "",
"required": true,
"secure": false
}
},
{
"key": "AccessKey",
"metadata": {
"displayName": "",
"display_name": "",
"required": false,
"secure": true
}
},
{
"key": "SecretAccessKey",
"metadata": {
"displayName": "",
"display_name": "",
"required": false,
"secure": true
}
},
{
"key": "Region",
"metadata": {
"displayName": "",
"display_name": "",
"required": true,
"secure": false
}
},
{
"key": "SecretName",
"metadata": {
"displayName": "",
"display_name": "",
"required": true,
"secure": false
}
},
{
"key": "SecretCacheTTL",
"metadata": {
"displayName": "",
"display_name": "",
"required": false,
"secure": false
}
Expand Down

0 comments on commit 821c0a9

Please sign in to comment.