Environment variables with Spring Boot & Kubernetes
In my OS, the following command retrieves a key managed by K8s:
kubectl exec -it nginx-secret-7665cd8fb8-mj26h cat /mnt/secrets/ScAppCryptKey
As mentioned, the result is something like this:
xPeShVmYq3t6w9z#B#E?H@McQfTjWnZr4u7x%D*F-JaNdRgUkXp2s5v8y#B?E%
Now, I have to find a way to get this result (key) and use it in my Spring Boot Application. I think the most proper way to express my problem is:
During runtime (or when starting my app), I need to execute a command in the OS and get its result. Is there a way to automatically execute commands in the terminal with Spring? Let me try to explain through code:
// If I had an environment variable, this would be like:
String key = System.getenv("myKey");
// But in my case, I need to execute a command, like this:
String keyFromKubernetes = System.getenv(
"kubectl exec -it nginx-secret-7665cd8fb8-mj26h cat /mnt/secrets/ScAppCryptKey"
);