38.6 Modified secrets in Secrets Manager (#38)

This commit is contained in:
Art
2021-09-29 23:27:18 +03:00
parent cc44da8452
commit 38614b3ade
2 changed files with 17 additions and 10 deletions

View File

@ -16,23 +16,30 @@ import java.util.Base64;
import java.util.Properties; import java.util.Properties;
@Slf4j @Slf4j
public class S3PropertiesListener implements ApplicationListener<ApplicationPreparedEvent> { public class SecretsManagerPropertiesListener implements ApplicationListener<ApplicationPreparedEvent> {
private ObjectMapper mapper = new ObjectMapper(); private ObjectMapper mapper = new ObjectMapper();
@Override @Override
public void onApplicationEvent(ApplicationPreparedEvent event) { public void onApplicationEvent(ApplicationPreparedEvent event) {
System.out.println("onApplicationEvent");
String activeProfiles = event.getApplicationContext().getEnvironment().getProperty("spring.profiles.active");
if (activeProfiles == null || !activeProfiles.contains("aws-rds")) return;
String secretJson = getSecret(); String secretJson = getSecret();
log.info("Retrieved secretJson from Secret Manager: {}", secretJson);
System.out.println("Retrieved secretJson from Secret Manager: " + secretJson); log.debug("Retrieved secretJson from Secret Manager: {}", secretJson);
String accessKey = getString(secretJson, "AMAZON_S3_ACCESS_KEY");
String secretKey = getString(secretJson, "AMAZON_S3_SECRET_KEY"); String jwtSecret = getString(secretJson, "app_jwt_secret");
String springDatasourceUsername = getString(secretJson, "spring_datasource_username");
String springDatasourcePassword = getString(secretJson, "spring_datasource_password");
ConfigurableEnvironment environment = event.getApplicationContext().getEnvironment(); ConfigurableEnvironment environment = event.getApplicationContext().getEnvironment();
Properties props = new Properties(); Properties props = new Properties();
props.put("app.amazon-s3.access-key", accessKey); props.put("app.jwt.secret", jwtSecret);
props.put("app.amazon-s3.secret-key", secretKey); props.put("spring.datasource.username", springDatasourceUsername);
props.put("spring.datasource.password", springDatasourcePassword);
environment.getPropertySources().addFirst(new PropertiesPropertySource("aws.secret.manager", props)); environment.getPropertySources().addFirst(new PropertiesPropertySource("aws.secret.manager", props));
} }
@ -43,7 +50,7 @@ public class S3PropertiesListener implements ApplicationListener<ApplicationPrep
private String getSecret() { private String getSecret() {
String secretName = "/image-s3/portal-api"; String secretName = "/support-portal";
String region = "eu-north-1"; String region = "eu-north-1";
// Create a Secrets Manager client // Create a Secrets Manager client

View File

@ -1,2 +1,2 @@
# file src/main/resources/META-INF/spring.factories # file src/main/resources/META-INF/spring.factories
org.springframework.context.ApplicationListener=net.shyshkin.study.fullstack.supportportal.backend.config.S3PropertiesListener org.springframework.context.ApplicationListener=net.shyshkin.study.fullstack.supportportal.backend.config.SecretsManagerPropertiesListener