Secrets Management

Keep API keys, passwords and certificates out of code, store them in a vault, rotate them and detect leaks early.

Intermediate · about 4 hours · difficulty 2/5

Learn first: Secure SDLC, IAM Operations and Privileged Access

Secrets are credentials that grant access: API keys, database passwords, tokens, private keys and certificates. Leaked secrets are one of the most common causes of cloud breaches, often because they were committed to a Git repository.

Rules of thumb

  • Never hardcode secrets in source code, container images or front-end bundles.
  • Store them in a dedicated secrets manager: HashiCorp Vault, AWS Secrets Manager, Azure Key Vault, Google Secret Manager, or your CI platform's encrypted secrets.
  • Inject at runtime via environment variables or mounted files, with access restricted to the service that needs each secret.
  • Prefer short-lived credentials (cloud IAM roles, workload identity, OIDC federation from CI) over long-lived static keys.
  • Rotate secrets on a schedule and immediately after any suspected exposure.

Detecting leaks

  • Run secret scanning in pre-commit hooks and CI with tools like Gitleaks or TruffleHog, and enable GitHub secret scanning and push protection.
  • Remember Git history is permanent: deleting the file in a new commit does not remove the secret. Revoke and rotate first, then clean history if needed.

Local development

Use a git-ignored .env file with a committed .env.example that holds only placeholder values.

Pitfalls

  • Logging full request headers that contain tokens.
  • One shared admin key used by every service, so rotation breaks everything.

Good enough to move on: you can move a hardcoded key from a project into a secrets manager, add a secret scanner to CI and explain the right response to a leaked key.

Key concepts

  • What counts as a secret
  • Secrets managers and vaults
  • Runtime injection
  • Short-lived credentials and OIDC
  • Rotation and revocation
  • Secret scanning (Gitleaks, TruffleHog)
  • Push protection

Mini project

Add Gitleaks as a pre-commit hook and CI step to a repository, move any secrets into your CI platform's encrypted secrets or a cloud secrets manager, and write a leaked-key response checklist.

Check yourself

Why is deleting a leaked secret from the latest commit not enough, and what must you do first?

Free resources