-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: AwsSecretsManagerConfig.java 구현
- Loading branch information
1 parent
0c729df
commit 60e4ebc
Showing
1 changed file
with
80 additions
and
0 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
...e/src/main/java/org/depromeet/spot/infrastructure/aws/config/AwsSecretsManagerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |