Last active
June 11, 2022 11:43
-
-
Save wilkom2009/d16f0e62d7f01170d04f2a0b95106bb8 to your computer and use it in GitHub Desktop.
docker-demo2-gist-files
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
package com.wilkom.dockerdemo.utils; | |
import javax.sql.DataSource; | |
import org.springframework.boot.jdbc.DataSourceBuilder; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
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.google.gson.Gson; | |
/** | |
* Config class to manage AWS Secret manager value retrieving | |
* @author Wilson | |
*/ | |
@Configuration | |
public class JavaConfig { | |
private Gson gson = new Gson(); | |
/** | |
* Customize the data source config values reading from Bean class | |
* Instead of reading from application.yaml | |
*/ | |
@Bean | |
public DataSource dataSource() { | |
SecretValue secretValue = getSecretValue(); | |
return DataSourceBuilder.create() | |
.driverClassName("com.mysql.cj.jdbc.Driver") | |
.password(secretValue.getPassword()) | |
.username(secretValue.getUsername()) | |
.url("jdbc:" + secretValue.getEngine() + "://" + secretValue.getHost() + ":" + secretValue.getPort() | |
+ "/" + secretValue.getDbname()) | |
.build(); | |
} | |
private SecretValue getSecretValue() { | |
String secretName = "demodb/test"; | |
String region = "us-east-1"; | |
// Create a Secrets Manager client | |
AWSSecretsManager client = AWSSecretsManagerClientBuilder.standard() | |
.withRegion(region) | |
.build(); | |
String secret; | |
GetSecretValueRequest getSecretValueRequest = new GetSecretValueRequest() | |
.withSecretId(secretName); | |
GetSecretValueResult getSecretValueResult = null; | |
try { | |
getSecretValueResult = client.getSecretValue(getSecretValueRequest); | |
} catch (Exception e) { | |
throw e; | |
} | |
if (getSecretValueResult.getSecretString() != null) { | |
secret = getSecretValueResult.getSecretString(); | |
return gson.fromJson(secret, SecretValue.class); | |
} | |
return null; | |
} | |
} |
Author
wilkom2009
commented
Jun 11, 2022
•
- Line 41: the secret name created in AWS Secrets Manager
- Line 42: the AWS Region where the secret is created
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment