Shōgun8
Shōgun8

Reputation: 562

How To Store and Retrieve Secrets From Hashicorp Vault using Docker-Compose?

I have setup an instance of Hashicorp Vault. I have successfully written and read secrets to and from it. Getting Vault up and running and is the easy part. Now, how do I use Vault as a store to replace the .env file in docker-compose.yml? How do I read secrets from Vault in all of my docker-compose files?

Even more difficult: how do I dynamically generate keys to access access the secrets in Vault, then use those keys in my docker-compose.yml files, without editing those files each time I restart a stack? How is that process automated? In short, just exactly how can I leverage Hashicorp Vault to secure the secrets that are otherwise exposed in the .env files?

I have read all of their literature and blog posts, and haven't been able to find anything that outlines that process. I am stuck and any tips will be greatly appreciated.

Note: This is not a question about running a Hashicorp Vault container with docker-compose, I have successfully done that already.

Also Note: I cannot modify the containers themselves; I can only modify the docker-compose.yml file

Upvotes: 3

Views: 8307

Answers (1)

BMitch
BMitch

Reputation: 264861

You would need to query the vault API to populate either your .env file or in the entrypoint of your container. My preference would be the container entrypoint at worst, and ideally directly in your application. The reason is because vault secrets could be short lived, and any container running for longer than that period would need to refresh it's secrets.

If you go with the worst case of doing this in the entrypoint, there are a few tools that come to mind. confd from Kelsey Hightower, and gomplate.

confd can run as a daemon and restart your app inside the container when the configuration changes. My only concern is that it is an older and less maintained project.

gomplate would be run by your entrypoint to expand a template file with the needed values. That file could just be an env.sh that you then source into your environment if you needed env vars. Or you can run it within your command line as a subshell, e.g.

your-app --arg "$(gomplate ...sometemplate...)"

If you only use these tools to set the value once and then start your app, make sure to configure a healthcheck and/or graceful exit your app when the credentials expire. Then run your container with orchestration (Kubernetes/Swarm Mode) or set a restart policy so that it restarts after any credentials expire to get the new credentials.

Upvotes: 1

Related Questions