I have created a secret in Kubernetes and mounted it in a volume, so part of the yaml file looks like this:
volumeMounts:
- name: test-key
readOnly: true
mountPath: /opt/key
Then the secret itself contains:
My problem comes when trying to retrieve it using in Spring. How I'm supposed to do it?
What I've tried so far is setting in the application.properties --> spring.datasource.private-key=${PRIVATE_KEY}
but it's not working. It gives me a placeholder error:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.datasource.private-key' in value "${spring.datasource.private-key}"
Any idea what I'm doing wrong?
UPDATE:
This is the way that Im reading the secret in spring, but it still give me the same error related to the placeholder when deploying:
@Value("${spring.datasource.private-key}")
private String privateKey;
@Bean
public PrivateKey getPrivateKeyFromEnvironmentVariable() throws IOException, NoSuchAlgorithmException {
List<String> activeProfiles = Arrays.asList(environment.getActiveProfiles());
String key;
if(activeProfiles.contains(LOCAL_ENVIRONMENT_NAME) ) {
key = resourceUtil.asString(LOCAL_PRIVATE_KEY_RESOURCE_PATH);
} else if(activeProfiles.contains(TEST_ENVIRONMENT_NAME)) {
key = generatePrivateKey();
} else {
//key = System.getenv(PRIVATE_KEY_ENVIRONMENT_VARIABLE_NAME);
key = privateKey;
}