Read secrets in Springboot deployed on Kubernetes

Viewed 3100

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

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

I make a GET request to the controller to print out the secret:

@Autowired
Environment env;

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

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 to the application from config/env/dev/config.yaml?

1 Answers

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
Related