Intekhab Rizvi
Posts

Vault dynamic secrets: stop sharing database passwords

4 min read

A database password that lives in a config file, a CI variable and three laptops is no longer a secret. It is shared state. Vault's database secrets engine removes the sharing: every client asks for credentials when it needs them, and Vault creates a short-lived user just for that request.

How dynamic secrets work

Vault holds one privileged connection to the database. When an application asks for credentials, Vault connects, runs a creation statement that you defined, and returns a username and password with a lease attached. When the lease expires, Vault drops the user. Nobody has to remember to rotate anything.

Set it up

This example uses PostgreSQL. Enable the engine, then tell Vault how to reach the database and which accounts it may create.

vault secrets enable database

vault write database/config/app-db \
    plugin_name=postgresql-database-plugin \
    allowed_roles=readonly \
    connection_url="postgresql://{{username}}:{{password}}@db.internal:5432/app" \
    username="vault" \
    password="change-me"

vault write database/roles/readonly \
    db_name=app-db \
    creation_statements="CREATE ROLE \"{{name}}\" WITH LOGIN PASSWORD '{{password}}' VALID UNTIL '{{expiration}}'; GRANT SELECT ON ALL TABLES IN SCHEMA public TO \"{{name}}\";" \
    default_ttl=1h \
    max_ttl=24h

The vault account above is the one Vault uses to manage users. Rotate it straight away so that only Vault knows its password:

vault write -f database/rotate-root/app-db

Ask for credentials

vault read database/creds/readonly

Each call returns a different username and password, a lease ID and a lease duration. The application uses them like any other credentials, and they stop working when the lease ends.

What changes for your team

  • Leaks expire. A credential copied out of a log file is useless an hour later.
  • Credentials are attributable. Every request is tied to an identity in Vault's audit log, instead of everyone being app_user.
  • Revocation is one command. vault lease revoke -prefix database/creds/readonly invalidates every outstanding credential for that role.

Where it bites

Applications must cope with credentials that change. A connection pool that was opened once at start-up will break when its user is dropped, so fetch new credentials before the lease runs out, or let Vault Agent or the Vault Secrets Operator do it for you. Also remember that every lease is a real database user: size your TTLs and connection limits with that in mind.

More posts

Posts