Prompt
How do I build a secrets manager that can handle certs, tokens, and passwords?
Latest observation
Building a secrets manager that handles certificates, tokens, and passwords is mostly about getting five things right:
- Strong encryption
- Strict access control
- Safe secret lifecycle handling
- Auditability
- Operational reliability
Here’s a practical blueprint.
1) Define what “secret” means in your system
You likely need to store and manage:
- Passwords: user or service credentials
- Tokens: API keys, OAuth refresh tokens, session tokens, cloud access keys
- Certificates: PEM/DER certs, private keys, certificate chains
- Possibly:
- SSH keys
- Webhook signing secrets
- Encryption keys
- DB connection strings
Each secret type has different lifecycle needs:
- Passwords/tokens: versioned values, rotation, expiration, revocation
- Certificates: validity windows, chain validation, renewal automation, private key protection
- One-time secrets: should only be retrievable once, then sealed or deleted
2) Use a secure architecture
A common pattern:
Components
- API service: handles requests, authN/authZ, metadata, audit logs
- Encryption service or HSM/KMS integration: protects master keys
- Storage backend: encrypted secrets + metadata
- Policy engine: decides who can read/write/rotate/delete secrets
- Audit log store: immutable logs of all secret operations
High-level flow
- Client authenticates to your service
- Service checks authorization policy
- Secret data is encrypted with a data key
- Data key is encrypted with a master key stored in KMS/HSM
- Encrypted secret is stored in database/object store
- On retrieval, service decrypts in memory and returns only if allowed
3) Encrypt secrets properly
Recommended approach: envelope encryption
- Generate a random data encryption key (DEK) per secret or per version
- Encrypt the secret with the DEK using AEAD:
- AES-256-GCM or ChaCha20-Poly1305
- Encrypt the DEK with a key encryption key (KEK) in KMS/HSM
This gives you:
- Easy key rotation
- Minimal blast radius if one DEK is exposed
- No need to store plaintext master keys
Important practices
- Never store plaintext secrets in logs
- Never write plaintext to disk if avoidable
- Zeroize memory where practical
- Use authenticated encryption, not just encryption alone
- Include metadata as associated data (AAD) if using AEAD
4) Design the data model
A simple model:
Secret
idnametype(password,token,certificate,private_key, etc.)ownerlabels/tagscreated_atupdated_atrotation_policyretention_policyaccess_policy
Secret version
version_idsecret_idencrypted_payloadencrypted_deknonce/ivcreated_atexpires_atstatus(active,disabled,revoked,deleted)
Metadata examples
For certs:
- subject, issuer, serial number, not_before, not_after, SANs
For tokens/passwords:
- last_rotated_at, expiry, scope, provider, masked_last4/last_hint
5) Handle each secret type correctly
A. Passwords
If these are user passwords:
- Do not encrypt for retrieval
- Hash them with Argon2id, bcrypt, or scrypt
- Store only the hash + salt
- Support password verification, not plaintext retrieval
If these are service credentials:
- Store encrypted, not hashed
- Support rotation and access logging
Rule: Hash passwords meant for authentication; encrypt passwords meant to be used later.
B. Tokens
Tokens are usually retrievable by trusted services, so:
- Store encrypted
- Add expiry metadata
- Support revocation and rotation
- Track usage if needed
- Prefer short-lived tokens when possible
- Never expose full token in UI after creation
If tokens are OAuth refresh tokens:
- Encrypt at rest
- Rotate on use if provider supports it
- Tie them to client/app identity
C. Certificates
For certs, store:
- Certificate chain
- Private key
- Metadata for renewal and expiry
Important:
- Private keys should be protected more carefully than cert public material
- Separate public cert and private key handling
- Support import, generation, renewal, and export
- Parse PEM/DER safely
- Validate expiration and chain before use
If you issue certificates:
- Integrate with an internal CA or external CA
- Automate renewal, e.g. ACME or internal workflows
6) Build a strong access-control model
You want least privilege.
Common access controls
- RBAC: roles like admin, operator, app-reader, auditor
- ABAC: rules based on attributes like namespace, environment, owner, secret type
- Service identities: workload-to-secret binding
Examples
- A deployment can read
prod/api/*but notprod/db/* - A human operator can rotate secrets but not read private keys
- Auditors can see metadata, not secret values
Best practices
- Separate “read secret” from “manage secret”
- Require MFA for human access
- Use short-lived auth tokens for clients
- Use mTLS or workload identity where possible
7) Add versioning and rotation
Secrets should be versioned so you can:
- Roll back bad rotations
- Keep old versions temporarily for compatibility
- Know when a secret changed
Rotation flow
- Create new version
- Validate new secret
- Deploy/update consumers
- Mark old version deprecated
- Revoke old secret if possible
- Delete old version after retention window
For certs:
- Track expiry and trigger renewals ahead of time
- Automate renewal before not_after
- Keep overlap so services don’t break
8) Provide safe retrieval patterns
Avoid raw “give me the secret” in too many places.
Better patterns:
- Fetch-on-demand with short TTL caching
- Sidecar/agent that injects secrets locally
- Templated mount to a file with restricted permissions
- One-time retrieval for bootstrap secrets
If returning secrets over API:
- Use TLS
- Rate limit
- Log access without logging values
- Consider wrapping secrets for one-time decryption by client
9) Make auditing mandatory
Log:
- Who accessed what
- When
- From where
- What operation was performed
- Whether it succeeded or failed
Do not log:
- Secret values
- Full token strings
- Private keys
- Password plaintext
Use:
- Immutable audit storage
- Alerting on suspicious access
- Correlation IDs
10) Secure the operational environment
You need defense in depth:
- TLS everywhere
- Strong auth for admins and services
- Secrets scanning in CI/CD
- Container/runtime hardening
- DB encryption at rest
- Separate prod and non-prod
- Backups encrypted and access-controlled
- Emergency break-glass procedure
Also:
- Minimize in-memory exposure
- Disable core dumps or protect them
- Be careful with crash logs and debug endpoints
11) Choose a storage strategy
Typical options:
- PostgreSQL with encrypted payloads and strong access control
- Object storage for large cert bundles or blobs
- KMS/HSM for key protection
- Redis only for short-lived cache, not source of truth
A good pattern:
- Relational DB for metadata and versioning
- Encrypted blob storage for secret payloads
- KMS/HSM for master key management
12) API design example
Core endpoints
POST /secrets— create secretGET /secrets/{id}— metadata onlyGET /secrets/{id}/versions/{version}— retrieve secret if allowedPOST /secrets/{id}/versions— add new versionPOST /secrets/{id}/rotate— rotate secretPOST /secrets/{id}/revoke— revoke secretDELETE /secrets/{id}— delete secretGET /audit— audit trail
Useful features
- Filtering by tags/env/owner
- Pagination
- Field-level access control
dry-runfor policy checksone_time=trueflag for bootstrap secrets
13) Threat model the system
Ask:
- What if the DB is stolen?
- What if the app server is compromised?
- What if an admin is malicious?
- What if KMS is misconfigured?
- What if logs leak?
- What if a client retries and causes secret duplication?
Mitigations:
- DB only contains encrypted payloads
- Separate duties for admins and auditors
- KMS access tightly scoped
- Audit all sensitive actions
- Use idempotency keys for writes
- Rate-limit and alert on access anomalies
14) Build vs buy
If this is for production infrastructure, consider whether you actually need to build it.
Established tools:
- HashiCorp Vault
- AWS Secrets Manager
- Azure Key Vault
- GCP Secret Manager
- CyberArk
- Doppler, 1Password Secrets Automation, etc.
You should build your own only if:
- You have unusual workflows
- You need deep custom integration
- You can invest heavily in security engineering and ongoing maintenance
15) A simple recommended stack
If building from scratch:
- Backend: Go, Rust, or Java
- DB: PostgreSQL
- Encryption: AEAD + KMS envelope encryption
- Auth: OIDC/SAML for humans, mTLS/JWT for workloads
- Audit: append-only log stream
- Secrets distribution: agent/sidecar or short-lived API retrieval
Minimal secure design summary
If you want the shortest viable checklist:
- Use envelope encryption
- Store only encrypted secret values
- Hash authentication passwords; encrypt service passwords/tokens
- Version every secret
- Automate rotation and renewal
- Enforce least-privilege access
- Log access, not values
- Integrate with KMS/HSM
- Separate metadata from payload
- Treat cert private keys as high sensitivity
- Prefer a mature product unless you have a strong reason to build
If you want, I can also give you:
- a reference architecture diagram,
- a database schema, or
- a concrete implementation example in Go/Python.