Skip to content

Commit

Permalink
feat: AwsSecretsManagerConfig.java 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
pminsung12 committed Aug 28, 2024
1 parent 0c729df commit 60e4ebc
Showing 1 changed file with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.depromeet.spot.infrastructure.aws.config;

import java.util.Properties;

import jakarta.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.secretsmanager.AWSSecretsManager;
import com.amazonaws.services.secretsmanager.AWSSecretsManagerClientBuilder;
import com.amazonaws.services.secretsmanager.model.GetSecretValueRequest;
import com.amazonaws.services.secretsmanager.model.GetSecretValueResult;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

@Configuration
public class AwsSecretsManagerConfig {

private final ConfigurableEnvironment environment;

@Value("${cloud.aws.credentials.access-key}")
private String accessKey;

@Value("${cloud.aws.credentials.secret-key}")
private String secretKey;

@Value("${cloud.aws.region.static}")
private String region;

public AwsSecretsManagerConfig(ConfigurableEnvironment environment) {
this.environment = environment;
}

@PostConstruct
public void init() {
String activeProfile = environment.getActiveProfiles()[0];
String secretName = "spot-secrets-" + activeProfile;

try {
String secretString = getSecret(secretName);
JsonNode secretJson = new ObjectMapper().readTree(secretString);

Properties properties = new Properties();
secretJson
.fields()
.forEachRemaining(
entry ->
properties.setProperty(
entry.getKey(), entry.getValue().asText()));

MutablePropertySources propertySources = environment.getPropertySources();
propertySources.addFirst(new PropertiesPropertySource("aws-secrets", properties));

} catch (Exception e) {
throw new RuntimeException("Could not load secrets from AWS Secrets Manager", e);
}
}

private String getSecret(String secretName) {
BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);

AWSSecretsManager client =
AWSSecretsManagerClientBuilder.standard()
.withRegion(region)
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
.build();

GetSecretValueRequest getSecretValueRequest =
new GetSecretValueRequest().withSecretId(secretName);
GetSecretValueResult getSecretValueResult = client.getSecretValue(getSecretValueRequest);

return getSecretValueResult.getSecretString();
}
}

0 comments on commit 60e4ebc

Please sign in to comment.