Read secrets in Springboot deployed on Kubernetes

1/14/2019

I have a many secrets configured in config/yaml file. There is one secret that is causing trouble. I just want to print out what value is being injected into that secret.

apiVersion: v1
kind: ConfigMap
metadata:
name: myapplication-config
data:
  config.yaml: |
    'mysecret1': ${DB_PASSWORD}
    'mysecret2': ${ANOTHER_SECRET}

When my application is all started up, I have a controller where I make a GET request and just try to print out the secret:

@Autowired
Environment env;

@GetMapping("/test")
public String print(){
  System.out.println(env.getProperty("mysecret2"));
}

I check this in controller on purpose so Application is fully up and running and has set and resolved all the dependencies, etc.

When I print it, it throws an error:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'mysecret2' in value "${mysecret2}" at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:172)

Any idea how I can check the secrets sent down in the application from config/env/dev/config.yaml?

-- Faraz
java
kubernetes
spring-boot

1 Answer

1/15/2019

I was able to read them thru environment variable:

@Autowired 
private org.springframework.core.env.Environment env;

//inside some method
@GetMapping("/test")
public String print(){
  System.out.println(env.getProperty("mysecret2"));
}

I tried using below but that didn't work.

@Value("${mysecret2})
private String mySecret2; //didn't work

....

System.getEnv("mySecret2"); //didn't work
System.getProperty("mySecret2"); //didn't work
-- Faraz
Source: StackOverflow