Vault

Vault Database Secrets Engine: Designing and Operating Dynamic Credentials

2026-04-19
NicheeLab Editorial Team

Every time an application requests credentials, Vault hands back a unique DB user and password that automatically expire when the TTL runs out. That is the core idea behind the Database secrets engine.

This article walks through why dynamic issuance is safer, the minimum setup steps, rotation and revocation, policy design, and the points that tend to show up on the exams — all in one place.

Why Dynamic Credentials: Reducing Risk and Operational Load

With dynamic credentials, Vault creates a short-lived DB user on each request and reliably tears it down when the TTL expires or it is explicitly revoked. Compared to long-lived shared passwords or manual handoffs, this dramatically shrinks the blast radius of any leak.

Auditability is also strong — Vault's audit log records who fetched credentials under which role and when. During incidents or rollbacks, revoking at the lease level takes effect immediately.

  • Principle: each request = temporary user + unique password + lease
  • TTL / Max TTL control the lifetime; expiration triggers automatic teardown (e.g. user deletion)
  • Audit logs plus policies give you both least privilege and accountability
ApproachValidityImpact if leakedRotation workflow
Dynamic credentials (Vault)Short-lived (TTL, Max TTL)Low (expires or revokes instantly)Automatic (new on each fetch)
Fixed in-app accountLong-lived or indefiniteHigh (spreads to every environment)Manual or script-dependent
Manual issuance (ticket-based)Case by caseMedium-high (revocation always lags)Manual every time

Architecture and How Leases Work

The Database secrets engine connects to the DB through a plugin (e.g. postgresql-database-plugin) and runs the SQL defined in the role to create a user. Vault binds the generated credentials to a lease and runs the revocation SQL when the TTL expires.

The application only needs read access to Vault — Vault handles the DB-side creation and deletion on its behalf. This decouples DB administrative privileges from the application itself.

  • Creation statements: SQL that creates the user and grants privileges
  • Revocation statements: SQL that drops or disables the user on expiry
  • Lease: tracks TTL and renewability; expiration triggers automatic teardown

Dynamic credential issuance flow

App/CItoken w/ policyVaultDB SEDatabasee.g. PG1. Read2. SQL: CREATE ROLE / GRANT3. Return creds (user/pw, TTL)4. Expire/Revoke: DROP ROLEDynamic credential issuance flow

Minimum Setup (PostgreSQL Example)

The following is a minimal walkthrough. The DB connection user (e.g. vault_admin) needs permission to create users, grant privileges, and drop users. Adjust the connection URL and TLS settings for your environment.

In creation_statements, accept the temporary username and password as Vault template variables, and grant only the minimum privileges required.

  • In production, require TLS on the DB connection and keep Vault's privileges strictly minimal
  • Set default_ttl and max_ttl carefully to match your requirements
  • Grant the application read access only on database/creds/<role>

Configuration and credential fetch via the Vault CLI (PostgreSQL)

export VAULT_ADDR=https://vault.example.com
export VAULT_TOKEN=hvs.xxxxx

# 1) Databaseシークレットエンジンを有効化
vault secrets enable database

# 2) DB接続設定(postgresql-database-plugin)
vault write database/config/my-postgresql \
  plugin_name=postgresql-database-plugin \
  allowed_roles=app-readonly \
  connection_url="postgresql://{{username}}:{{password}}@db.example.com:5432/postgres?sslmode=require" \
  username="vault_admin" \
  password="s3cr3t"

# 3) ロール定義(読み取り専用の一時ユーザを作成)
vault write database/roles/app-readonly \
  db_name=my-postgresql \
  creation_statements="CREATE ROLE \"{{name}}\" WITH LOGIN PASSWORD '{{password}}' VALID UNTIL '{{expiration}}'; GRANT SELECT ON ALL TABLES IN SCHEMA public TO \"{{name}}\"; ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO \"{{name}}\";" \
  revocation_statements="REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA public FROM \"{{name}}\"; DROP ROLE IF EXISTS \"{{name}}\";" \
  default_ttl=1h \
  max_ttl=24h

# 4) アプリが資格情報を取得(動的)
vault read database/creds/app-readonly
# 出力例
# Key                Value
# lease_id           database/creds/app-readonly/xxxxx
# lease_duration     1h
# lease_renewable    true
# password           A1b2C3d4e5f6g7h8
# username           v-token-app-readonly-1693258841-abcde

# 5) 最小権限ポリシー(アプリ用)
cat <<'EOF' > app-readonly.hcl
path "database/creds/app-readonly" {
  capabilities = ["read"]
}
EOF
vault policy write app-readonly app-readonly.hcl

# 必要ならアプリ用トークンを発行(例)
vault token create -policy=app-readonly -display-name=app-A

# 6) 失効(インシデント対応など、即時剥奪)
vault lease revoke database/creds/app-readonly/xxxxx

# 7) 接続ユーザのローテーション(定期的なベストプラクティス)
vault write -f database/rotate-root/my-postgresql

Operations: Best Practices for TTL, Leases, Rotation, and Teardown

Leases can be renewed within the bounds of max_ttl. Renewal extends Vault's revocation schedule and pushes back the user-drop timing. If long-lived connections are not required by the role, keep default_ttl short.

The connection user — the fixed account Vault uses to reach the DB — can be rotated with rotate-root. This shortens the window during which the static Vault-to-DB credential is exposed.

  • lease renew extends the deadline; it cannot exceed max_ttl
  • During incidents, use lease revoke for immediate revocation
  • Run database/rotate-root/<db_name> on a regular cadence
  • Review the audit log to confirm who pulled which role

Policy Design: Minimizing App Permissions and Enforcing Boundaries

As a rule, give the application only read access on database/creds/<role>. Write and list operations are unnecessary. Separating paths per role clarifies the boundaries between services.

Even for the same application, use distinct role names for dev / staging / prod so credentials cannot cross environments. Separating namespaces or mount paths is also effective.

  • Scope paths to a single role
  • Prevent privilege bleed with per-environment roles and paths
  • Review policies and audit logs on a regular schedule

Troubleshooting and Exam-Relevant Points

Common failures include insufficient privileges on the connection user (cannot CREATE ROLE or GRANT), bad escaping in creation_statements, and DB connections that don't meet TLS requirements. Start by reading both the Vault log and the DB server log side by side.

For the Associate / Ops exams, common topics include the relationship between Lease / TTL / Max TTL, the meaning of rotate-root, the effect of lease revoke, and constraints around allowed_roles.

  • If the role name is missing from allowed_roles, the fetch fails
  • Misconfiguring max_ttl < default_ttl causes a creation error
  • Clock drift (no NTP) makes TTL behavior look erratic
  • Watch out for DB-side object privilege inheritance and default privileges

Check Your Understanding

Associate / Ops

問題 1

An application has just fetched credentials from database/creds/readrole via the Vault Database secrets engine. You want to invalidate those credentials immediately. Which is the most appropriate action?

  1. Run vault lease revoke against the lease_id that was returned
  2. Run vault write -f database/rotate-root/<db_name>
  3. Run vault token revoke against the application's token
  4. Change the role's default_ttl to 0

正解: A

Revoking the lease is the fastest way to immediately invalidate a specific set of issued credentials. rotate-root rotates the Vault-to-DB connection user and does not affect already-issued temporary users. Revoking the token stops future fetches but leaves any already-issued DB users in place via a different path. Setting TTL to 0 does not retroactively affect existing leases.

Frequently Asked Questions

What permissions does the connection user (the fixed account Vault uses to connect to the DB) need?

It needs whatever permissions the role's creation and revocation SQL statements require. Typically that means user creation, privilege granting, and deletion (CREATE ROLE / GRANT / DROP ROLE, etc.). Follow least-privilege and avoid unnecessary superuser rights.

Are usernames reused, or is a new one generated each time?

Dynamic credentials generate a unique username every time (e.g. v-token-...). When the lease expires, the user is removed via the configured revocation_statements.

Does renewing the TTL change the password on the database side?

Normally, renewing a lease only extends Vault's revocation schedule. The DB-side username and password stay the same (within max_ttl). The revocation SQL only runs at expiration or explicit revoke.

Check what you learned with practice questions

Practice with certification-focused question sets

無料で問題を解いてみる
Author

NicheeLab Editorial Team

NicheeLab editorial team focused on data engineering and cloud certification learning. Content is structured around practical study needs and official exam domains.


Related articles
Vault

Vault Core Concepts: Sealed/Unsealed, Auth, Secrets (2026)

Vault fundamentals — sealed/unsealed state, auth methods, se...

Vault

Vault Operations Professional (VOP-003): Complete Guide (2026)

Pass the Vault Operations Professional exam — enterprise pat...

Vault

Vault Path-Based Routing: API URL Structure (2026)

How Vault's path-based routing works — mount points, sub-pat...

Vault

Vault Tokens: Auth Token Mechanics (2026)

Token fundamentals — service vs. batch tokens, accessor, ren...

Vault

Vault Token Types: Service, Batch, Periodic (2026)

Service vs. batch tokens compared — performance, ACL behavio...

Browse all Vault articles (101)
© 2026 NicheeLab All rights reserved.