I'd like to retrieve my database password from the AWS Secret Manager based off of a key name set in my properties files. It doesn't seem like I can read properties from the application.yml
file like I had hoped, however. In the code below, my.project.aws-secret
and my.project.region
are both retrieved as null
.
Am I wrong to expect those properties to be retrievable at this point? And is this the right approach for achieving this, or is there an easier way?
Here is the code. I am currently trying to do this using an ApplicationListener
, but I have seen the same problem implementing this as an EnvironmentPostProcessor
as well:
ApplicationListener
public class DatasourceAwsProperties implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
ConfigurableEnvironment environment = event.getEnvironment();
Properties props = new Properties();
String datasourcePassword = environment.getProperty("my.project.aws-secret"); // why is this null?
String awsRegion = environment.getProperty("my.project.region"); // why is this null?
String secret = getSecret(datasourcePassword, awsRegion);
MapPropertySource newProperties = new MapPropertySource("newProperties", ImmutableMap.of("spring.datasource.password", secret));
environment.getPropertySources().addLast(newProperties);
}
public static String getSecret(String secretName, String region) {
// gets secret from aws
}
application.yml
lots:
of:
properties: ...
my:
project:
aws-secret: the-name-of-the-secret-in-aws
region: us-east-1