Patterns library
Secrets and key management
Keep passwords, API keys, certificates and encryption keys out of code and under control.
When to use it
- Your service uses passwords, API keys, certificates or encryption keys.
- Secrets are currently in configuration files, code or shared documents.
- You need to show who can use keys and how they are rotated.
How it fits together
Application and pipeline
- Uses its own workload identity
- No secrets in code
- asks for a secret at run time
Secrets vault
- Secrets, keys and certificates
- Access policy per secret
- Rotation and expiry
- master keys protected by
Hardware-backed key storage
- Hardware security module or cloud key service
- every access logged to
Security monitoring
- Alerts on unusual use
- Certificate expiry warnings
The parts
- Secrets vault
- A managed service that stores secrets and controls who can read them.
- Key management service
- Creates, stores and rotates encryption keys, and can encrypt data without exposing the key.
- Hardware-backed key storage
- A hardware security module or cloud key service that protects the most important keys.
- Workload identity
- Lets an application prove who it is without a stored password.
- Monitoring
- Logs every use of a secret or key and warns before certificates expire.
Main risks
- Secrets committed to code repositories or pasted into tickets.
- Keys that are never rotated, or are shared between environments.
- Too many people able to read production secrets.
- Lost keys making data impossible to recover.
- Expired certificates taking the service down.
Usual controls
- Secret scanning on code repositories, and no secrets in code or configuration files.
- Workload identities instead of stored passwords where possible.
- Least-privilege access for each secret, and separate secrets per environment.
- Automated rotation on a schedule, and immediately after suspected exposure.
- Separation of duties between key administrators and data users.
- Tested backup and recovery for keys, and alerts before certificates expire.
What it maps to
NCSC Cyber Assessment Framework principles
- CAF B3 Data security (opens in a new tab)
- CAF B2 Identity and access control (opens in a new tab)
- CAF C1 Security monitoring (opens in a new tab)
NCSC guidance
- NCSC using TLS to protect data (opens in a new tab)
- NCSC design and build a privately hosted public key infrastructure (opens in a new tab)
- NCSC protect your code repository (opens in a new tab)
- NCSC secure the build and deployment pipeline (opens in a new tab)
- NCSC timelines for migration to post-quantum cryptography (opens in a new tab)
Related templates and tools
Templates
GOV Assured tools
Common mistakes
- Leaving a “temporary” secret in a configuration file.
- Using one key for everything.
- Storing keys next to the data they protect.
- Having no plan for certificate expiry.
- Never testing whether keys can be restored from backup.