Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 36 ยท Dynamic Secrets and Encryption

Vault Dynamic Secrets and Leases Explained

Understand why dynamic secrets beat static passwords: how Vault creates credentials on demand, attaches leases, renews and revokes them, and how to model the lifecycle in code.

A static password is created once, shared widely and rotated rarely. If it leaks, you may not know for months. Dynamic secrets invert this: Vault creates a credential on demand, for one client, with a short life, and deletes it when the time is up. Nobody ever copies a shared password, and a leaked credential expires by itself.

Vault is not available on this site. The CLI commands are real; the JavaScript lifecycle model is a stand-in.

How it works

When a client reads a role on a dynamic secrets engine (database, AWS, Azure, GCP, SSH, PKI, Consul...), Vault calls the target system's API, creates a new user or key, and returns it together with a lease.

Terminal
vault read database/creds/readonly
# Key                Value
# lease_id           database/creds/readonly/<id>
# lease_duration     1h
# lease_renewable    true
# username           v-token-readonly-...
# password           <random>

The lease ties the credential to a clock. When it expires, Vault revokes the credential at the source (for a database: DROP ROLE).

sequenceDiagram
    participant App
    participant Vault
    participant DB
    App->>Vault: read database/creds/readonly
    Vault->>DB: CREATE ROLE temp...
    Vault-->>App: username, password, lease_id (1h)
    App->>Vault: renew lease (optional)
    Note over Vault,DB: Lease ends
    Vault->>DB: DROP ROLE temp...

Each client gets its own credential; Vault cleans it up.

Lease operations

Command Effect
vault lease lookup <id> Show TTL and expiry
vault lease renew <id> Extend until max TTL
vault lease revoke <id> Revoke now
vault lease revoke -prefix database/creds/readonly Revoke every lease of a role

Each role has a default_ttl and a max_ttl. Renewal extends the lease but never beyond max_ttl; after that the client must request fresh credentials. Revoking a token revokes the leases it created, so killing a compromised token also kills the credentials it fetched.

JS Stand-in: lease lifecycle

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Why this is safer

  • Unique per client: audit logs show exactly which pod touched the database.
  • Short-lived: a stolen credential is useless soon.
  • Revocable in bulk: one command removes everything a role issued.
  • No shared rotation project: Vault rotates continuously.

Design tips

  • Pick TTLs longer than the app's slowest operation but as short as practical; many apps simply fetch new credentials at startup and on a timer.
  • Make apps handle credential change: reconnect with new credentials before the lease ends. Vault Agent automates this.
  • Watch database load: creating users is real work. Set max connections and use reasonable TTLs, not seconds.
  • Static roles (database/static-roles) rotate the password of an existing account on a schedule, for systems that cannot create users.
JS Stand-in: how many credentials per hour?

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does Vault do when a dynamic secret's lease expires?

    Expiry triggers revocation, for example dropping the database user.
  2. 2. What limits how long renewals can extend a lease?

    Renewal is capped at the role's max_ttl, then new credentials are required.
  3. 3. Which command revokes every lease under a role path?

    The -prefix flag revokes all leases whose id starts with the path.
  4. 4. A key benefit of dynamic secrets is:

    Unique, short-lived credentials limit blast radius and make audits precise.

Summary

  • Dynamic secrets are created on demand with a lease and removed at expiry.
  • Leases can be looked up, renewed up to max TTL, and revoked singly or by prefix.
  • Revoking a token revokes the leases it owns.
  • Design apps to refresh credentials; mind the load on the target system.

Next: Database Dynamic Credentials