Skip to content

Commit

Permalink
Merge pull request #47 from dvsa/cacheScrts
Browse files Browse the repository at this point in the history
feat: caching secrets
  • Loading branch information
sr4850 authored Jun 28, 2024
2 parents 08c0102 + 307b91a commit 4c4be98
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/main/java/activesupport/aws/s3/SecretsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
import org.apache.logging.log4j.Logger;
import org.json.JSONObject;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SecretsManager {

public static String secretsId = "OLCS-DEVAPPCI-DEVCI-BATCHTESTRUNNER-MAIN-APPLICATION";

private static final Logger LOGGER = LogManager.getLogger(SecretsManager.class);
private static final Map<String, String> cache = new ConcurrentHashMap<>();
private static final AWSSecretsManager secretsManager = awsClientSetup();

public static AWSSecretsManager awsClientSetup(){
private static AWSSecretsManager awsClientSetup() {
Regions region = Regions.EU_WEST_1;
return AWSSecretsManagerClientBuilder
.standard()
Expand All @@ -25,15 +29,18 @@ public static AWSSecretsManager awsClientSetup(){
}

public static String getSecretValue(String secretKey) {
if (cache.containsKey(secretKey)) {
return cache.get(secretKey);
}

String secret = null;

GetSecretValueRequest getSecretValueRequest = new GetSecretValueRequest()
.withSecretId(secretsId);
GetSecretValueResult getSecretValueResult = null;

try {
getSecretValueResult = awsClientSetup().getSecretValue(getSecretValueRequest);

getSecretValueResult = secretsManager.getSecretValue(getSecretValueRequest);
} catch (ResourceNotFoundException e) {
LOGGER.info("The requested secret " + secretKey + " was not found");
} catch (InvalidRequestException e) {
Expand All @@ -42,12 +49,11 @@ public static String getSecretValue(String secretKey) {
LOGGER.info("The request had invalid params: " + e.getMessage());
}

assert getSecretValueResult != null;

if (getSecretValueResult.getSecretString() != null) {
if (getSecretValueResult != null && getSecretValueResult.getSecretString() != null) {
secret = getSecretValueResult.getSecretString();
JSONObject jsonObject = new JSONObject(secret);
secret = jsonObject.getString(secretKey);
cache.put(secretKey, secret);
}
return secret;
}
Expand All @@ -57,7 +63,7 @@ public static void updateSecret(String secretId, String secretValue) {
UpdateSecretRequest updateSecretRequest = new UpdateSecretRequest()
.withSecretId(secretId)
.withSecretString(String.format("{password:%s}", secretValue));
awsClientSetup().updateSecret(updateSecretRequest);
secretsManager.updateSecret(updateSecretRequest);
} catch (AWSSecretsManagerException e) {
LOGGER.info(" You've either entered an Invalid name. 1) Must be a valid name containing alphanumeric characters, or any of the following: -/_+=.@!" +
"or 2)The secretId '" + secretId + "' does not exist");
Expand All @@ -70,12 +76,11 @@ public static void setSecretKey(String secretId, String secretValue) {
.withDescription("password for testing")
.withName(secretId)
.withSecretString(String.format("{password:%s}", secretValue));
awsClientSetup().createSecret(request);
secretsManager.createSecret(request);
LOGGER.info("Secret has been set");
} catch (ResourceExistsException e) {
LOGGER.info("The secret key '" + secretId + "' already exists... " +
"please use the updateSecretKey method instead or use a new key");
}
}

}

0 comments on commit 4c4be98

Please sign in to comment.