Microservices Secrets Management

Every service holds secrets: database passwords, API keys, signing keys, and certificates. A leaked secret can expose customer data within minutes. With dozens of services and several environments, secrets multiply fast. A clear secrets management approach stores them safely, hands them out only to the right services, and rotates them on schedule.

Layman Explanation

A hotel keeps room keys at the front desk, not taped to the doors. Staff hand out a key to the right guest for the right room and collect it at checkout. Locksmiths change locks regularly. A secrets manager works like that desk for passwords and keys.

Where Secrets Must Never Live

  • Source code repositories, even private ones.
  • Container images, since anyone who pulls the image can read them.
  • Plain configuration files shared in chat or email.
  • Log files and error messages.

Diagram: Central Vault

+-----------+     1. prove identity      +-----------------+
| Orders    | -------------------------> |  Secrets vault  |
| service   | <------------------------- |  (encrypted)    |
+-----------+     2. short-lived secret  +--------+--------+
                                                  |
                                         audit log of every read

The service proves who it is, the vault checks a policy, and the vault returns only the secrets that service may use.

Tools

ToolHighlights
HashiCorp VaultDynamic secrets, fine policies, many integrations
AWS Secrets ManagerManaged storage with automatic rotation
Azure Key VaultSecrets, keys, and certificates in Azure
Google Secret ManagerVersioned secrets with access control
Kubernetes SecretsBasic storage that needs encryption at rest enabled

Core Practices

Least Privilege

Each service reads only its own secrets. The catalog service has no access to payment credentials.

Short-Lived and Dynamic Secrets

A vault can create a database account valid for one hour for a single service instance. A stolen credential expires quickly and costs the attacker little value.

Rotation

Change passwords and keys on a schedule, such as every 90 days, and right away after any suspected leak. Automated rotation avoids human error. Services that fetch secrets at runtime pick up new values without a redeploy.

Encryption Everywhere

Encrypt secrets at rest in the vault and in transit over TLS. Protect the master keys with hardware security modules or cloud key management.

Auditing

Record who read which secret and when. Review the log for unusual access patterns.

Delivering Secrets to Services

  • Environment variables are simple, though child processes and crash dumps can expose them.
  • Mounted files in memory-backed volumes keep secrets off disk and allow live updates.
  • Direct vault calls at startup fetch fresh values and work with short-lived credentials.

Service Identity

The vault must trust the service before it shares anything. Platforms provide identities such as Kubernetes service accounts or cloud IAM roles. Mutual TLS certificates issued by a service mesh prove identity too. The first secret a service receives should come from the platform, not from a hard-coded value.

If a Secret Leaks

  1. Revoke the exposed secret immediately.
  2. Issue a new one and deploy it.
  3. Review audit logs for misuse.
  4. Fix the cause, such as a committed file, and add scanning to stop a repeat.

Prevention Tools

Secret scanners such as gitleaks and GitHub secret scanning inspect every commit and block pushes that contain keys. Add them to the pipeline early.

Key Takeaways

Store secrets in a vault, grant least privilege, prefer short-lived credentials, rotate on schedule, and audit access. Keep secrets out of code, images, and logs.