01 / 20

Secrets
Management

Learn why hardcoding passwords is dangerous, how secrets vaults protect sensitive data, and the principles behind keeping credentials safe at scale.

Cyber Security

What Are Secrets?

In cyber security, a secret is any piece of sensitive information that grants access to a system, service, or dataset. Secrets must be kept confidential: if an attacker obtains one, they can impersonate a legitimate user or service.

Passwords
User and service account credentials used for authentication.
API Keys
Tokens that identify and authorize applications calling external services.
Certificates
TLS/SSL certificates and private keys enabling encrypted connections.

Why Secrets Management Matters

A single leaked database password can compromise millions of records. Secrets management is the practice of storing, distributing, and rotating sensitive credentials so they never appear where attackers can find them.

The Lifecycle of a Secret

Generate Store Distribute Rotate Revoke & re-generate

The Core Rule: Never Hardcode

Hardcoding means embedding a secret directly in source code, configuration files, or scripts that are checked into version control. Once committed, the secret lives in the repository's history: even if you delete the line, git remembers.

✕ Hardcoded // config.py DB_PASS = "s3cretP@ss!" Visible in git history forever ✓ Vault-injected // config.py DB_PASS = os.getenv( "DB_PASSWORD" ) Secret stays outside the repo

What Is a Secrets Vault?

A secrets vault is a dedicated, encrypted service designed to store, manage, and control access to secrets. Think of it as a digital safe: applications authenticate to the vault, request only the secrets they need, and receive them securely at runtime.

Encrypted at rest
Secrets are stored with strong encryption (e.g., AES-256) so raw disk access reveals nothing.
Access policies
Fine-grained rules control which service or user can read which secret.
Audit logging
Every access is recorded, enabling forensics and compliance reporting.

How a Vault Works

Application (web server) 1. Authenticate Identity Provider (token issued) 2. Request secret Secrets Vault AES-256 encrypted Policy check Access Policy Least privilege 3. Return secret (encrypted TLS) Audit Log recorded

Hardcoded vs. Vault-Managed

Hardcoded Vault-Managed
Storage Plaintext in source code Encrypted vault
Version control Committed to git history Never in repos
Rotation Manual code change + deploy Automated, zero-downtime
Access control Anyone with repo access Fine-grained policies
Audit trail None Full logging of every access
Breach impact All secrets exposed at once Scoped, revocable

Principle of Least Privilege

Every service or user should receive only the specific secrets it needs to perform its function: nothing more. This limits the blast radius if any single service is compromised.

Vault 100 secrets Web Server Gets 2 secrets Payment API Gets 3 secrets Email Service Gets 1 secret Analytics Gets 1 secret

How to Apply Secrets Management

1
Audit your codebase. Search for hardcoded credentials using tools like git-secrets, TruffleHog, or GitHub's built-in secret scanning.
2
Choose a vault solution. Options include HashiCorp Vault, AWS Secrets Manager, Azure Key Vault, or 1Password for teams.
3
Define access policies. Map each service to the minimum set of secrets it needs. Deny everything by default.
4
Inject at runtime. Replace all hardcoded strings with environment variables or vault API calls that fetch secrets on startup.
5
Automate rotation. Set secrets to expire and rotate on a schedule (e.g., every 90 days) without requiring code changes.

Example 1: Environment Variables

The simplest step away from hardcoding: store secrets in environment variables on the host machine, and read them at runtime.

✕ Before (hardcoded)
# config.py DATABASE_URL = "postgres://admin:MyP@ssw0rd@db.example.com/prod"
✓ After (environment variable)
# config.py import os DATABASE_URL = os.getenv("DATABASE_URL")

Example 2: Vault API Integration

For production systems, use a dedicated vault. The application authenticates and fetches secrets dynamically: no environment files needed.

Fetching a secret from HashiCorp Vault
import hvac # Authenticate using the app's role token client = hvac.Client(url="https://vault.internal:8200") client.auth.approle.login( role_id=os.getenv("VAULT_ROLE_ID"), secret_id=os.getenv("VAULT_SECRET_ID") ) # Read only the secret this app needs db_pass = client.secrets.kv.v2.read_secret( path="production/database" )["data"]["data"]["password"]

Example 3: Automatic Rotation

Rotation ensures that even if a secret is leaked, it becomes useless within a short window. Vaults can rotate database credentials automatically.

Day 0 New secret created Day 30 Vault auto- rotates Day 60 Rotates again Day 90 Old secret fully revoked Leak detected! Secret already expired in <30 days

Common Mistakes to Avoid

✕ Committing .env files
Adding .env to the repository defeats the purpose. Always add .env to .gitignore before your first commit.
✕ Sharing via chat
Sending secrets over Slack, email, or Discord leaves them in message logs that may be searchable or backed up forever.
✕ One key for everything
Using the same API key across all services violates least privilege. If one service is breached, all services are compromised.

Secrets Management Summary

Concept Key Idea Best Practice
Hardcoding Secrets in source code Never do this
Env Variables Runtime injection Good first step
Secrets Vault Centralized encrypted store Production standard
Least Privilege Minimum necessary access Scope every policy
Rotation Regular secret expiry Automate with vault
Audit Logging Track every access Alert on anomalies

Question 1 of 5

A developer writes API_KEY = "sk-abc123..." directly in their Python file and pushes it to GitHub. What is this practice called?

Question 2 of 5

Which of the following is the primary benefit of using a secrets vault over environment variables?

Question 3 of 5

Your company's Payment API only needs the Stripe secret key. Under the principle of least privilege, what should the vault policy allow?

Question 4 of 5

A secret was accidentally committed to a public GitHub repo 10 minutes ago. The developer immediately deletes the line and pushes a new commit. Is the secret still at risk?

Question 5 of 5

Which step in the secrets lifecycle limits the damage window if a credential is leaked?