← Back to Home
πŸ”’

Secrets Management

A leaked credential is one of the fastest ways to get breached.

⏱10 min readπŸ“šDevOps Fundamentals

Passwords, API keys, tokens, certificates β€” secrets are the keys to your kingdom. A single one committed to a repository has caused countless real breaches. Managing secrets properly is one of the highest-impact security practices you can adopt.


🎯 Learning Objectives

By the end of this lesson you will:


The Danger of Hard-Coded Secrets

Putting a secret in code feels convenient. It’s a disaster:

⚠ Git never forgets

Committing a secret and then deleting it does NOT remove it β€” it stays in the Git history forever, readable by anyone with repo access. Once a secret hits a remote repo, treat it as compromised and rotate it immediately. Automated bots scan public GitHub for leaked keys within seconds.

javascript
# NEVER do this
const dbPassword = "SuperSecret123";
const apiKey = "sk_live_abc123...";

# Do this β€” read from the environment / a secret store
const dbPassword = process.env.DB_PASSWORD;

Catch Secrets Before They’re Committed

Prevention beats cleanup. Two layers:

1. Pre-commit hooks stop a secret from ever being committed:

bash
# Using gitleaks as a pre-commit hook
gitleaks protect --staged --verbose

2. CI scanning catches anything that slips through:

bash β€” 80Γ—24
student@devops:~$gitleaks detect --source . --verbose

πŸ’‘ Defence in depth

Run secret scanning both locally (pre-commit) and in CI. The pre-commit hook is your first line; the CI scan is the safety net for developers who bypass or lack the hook.


Secrets Managers / Vaults

Instead of scattering secrets in env files, store them in a dedicated secrets manager that provides encryption, access control, and auditing:

Tool Where it lives
HashiCorp Vault Self-hosted, cloud-agnostic
AWS Secrets Manager AWS
Azure Key Vault Azure
GCP Secret Manager Google Cloud

Apps fetch secrets at runtime, authenticated by their identity:

bash
# App requests a secret at startup, using its own identity
vault kv get -field=password secret/prod/db

# It's never stored on disk β€” fetched into memory when needed

Benefits over env files: central control, encryption at rest, audit logs of who accessed what, and easy rotation.


Rotation and Least Privilege

Two principles limit the damage if a secret does leak:

πŸ’‘ Scope every credential tightly

Never use one all-powerful admin key everywhere. Give each service its own narrowly scoped credential. If one leaks, the blast radius is small and contained.


πŸ§ͺ Hands-on Lab

πŸ“

Design a Secrets Strategy

  1. Add a secret-scanning step (gitleaks) to a pre-commit hook and to CI
  2. Move a hard-coded credential into an environment variable sourced from a vault
  3. Define a rotation policy and least-privilege scope for a database credential

🧠 Knowledge Check

Knowledge Check

You accidentally committed an API key and pushed it. What should you do?

Knowledge Check

What is the main advantage of a secrets manager over storing secrets in .env files?


πŸ’Ό Interview Preparation

Interview Q&A

How do you manage secrets securely across the development lifecycle?


Summary

You now know why hard-coded secrets are so dangerous, how to scan for them, how vaults centralise and protect them, and how rotation plus least privilege limit the damage of a leak. Next, we secure the containers you ship.

Up Next

Container Security

You've mastered this lesson. Continue your journey to becoming a DevOps Engineer.

Start Next Lesson→