How to inject a secret key at runtime via AWS

Viewed 297

Using Spring boot and we have the following value currently existing in the properties file.

secret.key=123456 # lets assume 12345 is a secret key

We are using AWS beanstalk and wanted to stop adding secret data in code/git and instead inject it at runtime as an environment variable at aws as follows.

enter image description here

But how to achieve this in code cos as the value doesn't exist currently, unable to run it with following error message:

Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'secret.key' in value "${secret.key}"

Attempting to set it up in properties file as follows:

server.port=5000
#other settings
secret.data=${secret.key}

For example trying to use the secret value in code as follows:

@RestController
public class HomeController {

    @Value("${secret.data}")
    private String secretData;
    .....
}

Please advice if there is a way around this or a better way to achieve this to stop adding sensitive data in code and git. Thanks.

1 Answers

You are passing in a system property. So the annotation you want begins systemProperties. Like,

@Value("#{systemProperties.secret.key}")
private String secretData;
Related