Vault variables not fetched using AppRole auth method

I installed vault agent in my K8s cluster and the vault server resides in another cluster. I’m using the AppRole auth method. The authentication works without any error. The secrets are also mounted to my application pod, but it won’t source it and take it as a variable. Below are the details.

annotations: ‘true’ ‘approle’ ‘auth/approle’ ‘/vault/custom/role-id’ ‘/vault/custom/secret-id’ ‘my-approle’ ‘myrole’ ‘false’ ‘debug’ ‘true’ ‘kv/mysecrets/secrets’ |
{{ with secret “kv/mysecrets/secrets/” -}}
export testvar={{ }}
{{- end }}’

If I login to the application pod and cat /vault/secrets/config.env it shows below

export testvar=shows-correct-secret

I’m also doing

args: [ ‘sh’, ‘-c’, ‘source /vault/secrets/config.env’ ]

But when I do env, it doesnt show the testvar variable

I’m just starting with kubernetes but I think your start line should be:

args: [ ‘sh’, ‘-c’, ‘source /vault/secrets/config.env’  && '']

as an example where is your app. Starting a new shell is not going to source the dotfile containing your secrets.

Please share the command you are configuring in your Kubernetes yaml too, or the ENTRYPOINT of the Docker image being run. It is not possible to fully understand how args will be used without that.

Additionally, even if you successfully set environment variables this way to be present for your main process, I don’t think they will apply to kubectl exec sessions

This is my entrypoint. I’m only using ‘args’ and not using ‘command’

args: [ ‘sh’, ‘-c’, ‘source /vault/secrets/config.env && java -jar app.jar’ ]

I read the post you deleted, before you deleted it. In it, you said your Dockerfile was configured with ENTRYPOINT of java -jar app.jar.

When you combine that ENTRYPOINT with the args you’ve specified, you’re actually running the command:

java -jar app.jar sh -c 'source /vault/secrets/config.env && java -jar app.jar'

i.e. you’re not actually sourcing your environment variables at all, rather you’re passing the sh -c '...' command as program arguments to your Java application (which probably ignores it).

You should be using command instead of args in your K8s YAML, so that the K8s configuration takes full control of the command to be run, in this case.