Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 36 ยท Projects and Practice

Project: Secure a Web App End to End with Vault

Design and rehearse a complete Vault workflow for a web app: KV secrets, dynamic database credentials, transit encryption, AppRole login, least-privilege policies, TTLs and a verification script.

In this project you design the Vault setup for a small web shop called Shopfront. The app has a web service and a background worker. Both need database access, the web service stores customer card tokens encrypted, and the worker needs a payment API key. You will write the policies, plan the authentication and then check your design with a runnable audit script.

Vault is not available on this site, so you will not run the commands here. Treat them as a checklist for a real dev server (vault server -dev). The JavaScript blocks are stand-ins that check your design on paper.

Requirements

  1. The web service reads its configuration and gets database credentials that expire in one hour.
  2. The web service can encrypt card tokens but must not be able to decrypt them.
  3. The worker can decrypt and read the payment API key but cannot create database users.
  4. No application ever holds a long-lived admin token.
  5. Operators can audit who did what.

Step 1: engines

Terminal
vault secrets enable -path=secret kv-v2
vault secrets enable database
vault secrets enable transit

vault kv put secret/shopfront/payments api_key="example-only"
vault write -f transit/keys/cards

Configure the database connection and the web role as in database credentials with default_ttl=1h.

Step 2: policies

HCL
# shopfront-web.hcl
path "database/creds/web"          { capabilities = ["read"] }
path "transit/encrypt/cards"       { capabilities = ["update"] }
path "secret/data/shopfront/web/*" { capabilities = ["read"] }
HCL
# shopfront-worker.hcl
path "transit/decrypt/cards"          { capabilities = ["update"] }
path "secret/data/shopfront/payments" { capabilities = ["read"] }
path "database/creds/worker"          { capabilities = ["read"] }

Splitting encrypt and decrypt implements requirement 2 with policy alone (transit, policies).

Step 3: authentication

Use AppRole (or the Kubernetes method on a cluster). Each service gets its own role with a short token TTL:

Terminal
vault auth enable approle
vault write auth/approle/role/shopfront-web token_policies=shopfront-web token_ttl=1h token_max_ttl=4h secret_id_ttl=10m secret_id_num_uses=1
vault write auth/approle/role/shopfront-worker token_policies=shopfront-worker token_ttl=1h

Your deployment tool fetches a single-use, wrapped secret_id and hands it to Vault Agent, which logs in and renders db.properties for the app.

Step 4: verify your design

The script below models the two policies and tests each requirement. It is a stand-in using the simple matching rules of path matching.

JS Stand-in: check the requirements

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

No output.

Step 5: operations

  • Enable a file and a syslog audit device before go-live.
  • Alert on permission denied bursts from the app roles.
  • Rotate the transit key quarterly and rewrap (rotation).
  • Take snapshots and rehearse a restore (backup).
JS Stand-in: token and credential budget

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

No output.

Extensions

Add a readonly reporting role with its own policy, move the secret-id delivery to response wrapping, or add a templated policy so each developer gets a personal secret/data/dev/<name>/* space (identity).

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. How does the design stop the web service from decrypting card tokens?

    Encrypt and decrypt are separate paths, so the policy grants only encrypt.
  2. 2. Why give secret_id_num_uses=1?

    A single-use secret ID limits the value of interception.
  3. 3. Which component renders db.properties for the app?

    Agent logs in, fetches credentials and renders the template.

Summary

  • Combine KV, database and transit engines with per-service policies.
  • Separate encrypt and decrypt permissions to enforce duties.
  • Use AppRole or Kubernetes auth with short TTLs and single-use secret IDs.
  • Test policies against requirements, then add audit, rotation and backups.

Next: Project: Build a Policy Linter