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

  1. Application and pipeline

    • Uses its own workload identity
    • No secrets in code
  2. Secrets vault

    • Secrets, keys and certificates
    • Access policy per secret
    • Rotation and expiry
  3. Hardware-backed key storage

    • Hardware security module or cloud key service
  4. Security monitoring

    • Alerts on unusual use
    • Certificate expiry warnings
What the diagram shows: The application and the delivery pipeline prove their identity and ask a secrets vault for what they need at run time. The vault holds secrets, keys and certificates under access policies, and its master keys are protected by hardware-backed key storage. Every access is logged to security monitoring.

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

NCSC guidance

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.

All patterns