All guides

Dynamic database credentials with HashiCorp Vault

Vault's database secrets engine generates database credentials on request, from roles you configure. Each credential comes with a lease, and Vault revokes it when the lease expires.

Lines of code on a monitor, seen at an angle

How it works

The database secrets engine creates database credentials on demand, from the roles you configure. Services no longer hardcode credentials. They request them from Vault and use Vault's leasing mechanism.

Every service accesses the database with unique credentials. HashiCorp's documentation points out what that does for auditing: the SQL username leads from a questionable data access to the exact service instance behind it.

1. Enable the engine and connect it

HashiCorp highly recommends creating a user within the database specifically for Vault. Vault uses it to manipulate the dynamic users. The documentation calls it the "root" user.

Shell

vault secrets enable database

vault write database/config/my-postgresql-database \
    plugin_name="postgresql-database-plugin" \
    allowed_roles="my-role" \
    connection_url="postgresql://{{username}}:{{password}}@localhost:5432/database-name" \
    username="vaultuser" \
    password="<password>" \
    password_authentication="scram-sha-256"

This is the documented command. The connection name, the host, the port, the database name and the user are yours to replace. Keep {{username}} and {{password}} in the URL as written: root credential rotation requires a connection URL that is templated.

  • allowed_roles lists the roles allowed to use this connection. It defaults to empty, which means no roles. A * lets any role use it.
  • password_authentication="scram-sha-256" makes Vault hash passwords before PostgreSQL stores them. It requires PostgreSQL 10 as a minimum. The default is password, which sends passwords to PostgreSQL in plaintext, where they may appear in PostgreSQL logs.

2. Create a role

Shell

vault write database/roles/my-role \
    db_name="my-postgresql-database" \
    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"

A role maps a name in Vault to the SQL statements that create the database credential. Vault substitutes the {{name}}, {{password}} and {{expiration}} values. Change the GRANT to what your application needs.

  • default_ttl specifies the TTL for the leases associated with the role.
  • max_ttl specifies the maximum TTL for those leases. The values 1h and 24h are the ones in HashiCorp's example, so choose your own.
  • revocation_statements is optional. If it is not provided, the plugin defaults to a generic drop user statement.

3. Read credentials

A user or a machine holding a Vault token with the right permission gets a credential by reading the creds endpoint for the role's name:

Shell

vault read database/creds/my-role

Text

Key                Value
---                -----
lease_id           database/creds/my-role/<lease-uuid>
lease_duration     1h
lease_renewable    true
password           <generated-password>
username           <generated-username>

Leases and revocation

With every dynamic secret, Vault creates a lease: metadata that includes a time duration and renewability. Lease IDs are structured so that their prefix is always the path the secret was requested from. That lets you revoke trees of secrets.

Shell

vault lease renew -increment=3600 <lease-id>
vault lease revoke <lease-id>
vault lease revoke -prefix database/creds/my-role
  • Renewal. The increment counts from the current time and does not extend the end of the current TTL. The requested increment is completely advisory.
  • Revocation. Revoking a lease invalidates the secret immediately and prevents any further renewals. HashiCorp calls prefix revocation very useful after an intrusion into one specific system.
  • Expiry. When a lease is expired, Vault automatically revokes it. Whoever consumes a secret has to return to Vault regularly, either to renew the lease or to ask for a replacement secret.

If Vault cannot revoke a lease, the lease could eventually become an irrevocable lease. vault lease revoke -force -prefix force revokes such a lease.

Root credential rotation

Shell

vault write -force database/rotate-root/my-postgresql-database

HashiCorp strongly recommends this step once configuration is done. After it, Vault itself is the only party able to use the Vault user. When it is done, the password for that user is no longer accessible. Because of this, the documentation highly recommends a user created specifically for Vault. When it rotates passwords, Vault treats standard credentials and root credentials the same way.

What to check

  1. The connection uses a database user created specifically for Vault, and its password has been rotated with rotate-root.
  2. password_authentication is set to scram-sha-256.
  3. allowed_roles names the roles that need the connection and does not use *.
  4. Each role sets its own default_ttl and max_ttl.
  5. Each client renews its lease or requests a replacement before the lease expires.
  6. No lease has become irrevocable.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides