Secrets management

Secrets management ensures that sensitive credentials such as API keys, database passwords, encryption keys, tokens, certificates, SSH keys, and cloud access keys are stored, accessed, rotated, and used securely. Proper secrets management prevents unauthorized access, supply chain attacks, environment compromise, and large-scale breaches caused by leaked or exposed secrets. Secrets management is a mandatory component of secure development, secure pipelines, and secure operations.

Why Secrets Management Is Critical

Secrets are among the highest-value assets in any system. If an attacker gains access to a single privileged secret, they can escalate, pivot, extract data, or take full control of infrastructure. Most major breaches originate from leaked or hardcoded secrets.

Common failure points include:

• Hardcoded credentials in source code
• Secrets inside Git history
• Tokens stored in config files
• Secrets printed in logs
• Long-lived static keys
• Exposed environment variables
• CI/CD pipeline leaks
• Misconfigured access to secrets vaults

Secrets management eliminates these risks through strong storage, controlled access, dynamic retrieval, rotation, and auditability.

What Secrets Management Must Achieve

Secure Storage

Secrets must never appear in source code or unencrypted files. They must be stored in encrypted vaults or KMS systems.

Least Privilege Access

Only systems or users that require a secret should have access to it.

Rotation

Secrets must be rotated frequently to limit damage if compromised.

Never Log Secrets

Audit logs must never contain tokens or passwords.

Ephemeral Secrets

Where possible, use short-lived, auto-expiring secrets instead of static credentials.

Encrypted Transport

Secrets must be delivered over secure channels such as TLS.

Monitoring and Auditing

Every secret access should be logged and tracked.

Versioning and Change Control

Changes to secrets must follow controlled workflows.

Kinds of Secrets

• API keys
• Database credentials
• Access tokens
• SSH keys
• Encryption keys
• OAuth client secrets
• Cloud provider keys
• Application config secrets
• CI/CD deploy keys
• JWT signing keys
• TLS private keys

Each requires strong protection.

Why Hardcoding Secrets Is Dangerous

Hardcoded secrets lead to:

• Git history exposure
• Backup leaks
• IDE auto-index leaks
• Copy/paste exposure
• Insider misuse
• Third-party reviewers accessing keys
• Full environment compromise

Once leaked, they often persist in forks, logs, caches, and build servers. Hardcoded secrets must be strictly banned.

Secure Storage Options

Secrets Vaults

Use vaults such as:

• HashiCorp Vault
• AWS Secrets Manager
• Azure Key Vault
• GCP Secret Manager

Vaults provide encryption, access control, rotation, and auditing.

KMS (Key Management Service)

KMS services protect encryption keys and allow controlled decryption operations.

Environment Injection Systems

CI/CD platforms can inject secrets at runtime without storing them in files.

Encrypted Files (Only When Necessary)

Tools like sops encrypt secrets in Git, but decryption keys must be restricted.


Secrets Handling Patterns

Pattern 1: Do Not Store Secrets in Code

Unsafe:

API_KEY = "abcd1234"

Safe:

API_KEY = os.getenv("API_KEY")

Secrets injected securely at runtime.

Pattern 2: Do Not Store Secrets in Version Control

Avoid storing in:

• Git repos
• Public repos
• Private repos
• Commit history
• Config files
• .env files (unless encrypted)

Pattern 3: Dynamic Secrets

Use systems that generate:

• Temporary DB credentials
• Temporary API tokens
• Temporary SSH keys

These secrets auto-expire and minimize risk.

Pattern 4: Access Control Policies

• Use role-based permissions
• Restrict access by environment
• Limit which services can read which secrets
• Enforce MFA for humans accessing vaults
• Use short access lifespans

Pattern 5: Encryption Everywhere

Secrets must be encrypted:

• At rest
• In transit
• In memory if possible

Pattern 6: Secure Secret Retrieval

Secrets must be retrieved:

• On-demand
• In memory only
• Never written to disk
• Never printed in logs
• Never included in error messages


Secrets Rotation

Secrets must be rotated when:

• A developer leaves
• A contractor leaves
• A secret is suspected to be leaked
• A secret is accidentally committed
• Before expiration time (scheduled)
• After each major release
• After any system compromise

Rotation reduces exposure duration.


Secret Lifecycle

  1. Generate secret

  2. Store securely

  3. Distribute securely

  4. Use securely

  5. Monitor access

  6. Rotate regularly

  7. Retire and revoke

Every stage must follow strict rules.


Extensive Practical Section

Below are deep, hands-on practical exercises covering real-world secrets management.


Practical 1: Extract Hardcoded Secrets from a Project

Search a codebase for secrets:

grep -R "password" -n .
grep -R "API_KEY" -n .
grep -R "secret" -n .

Remove all occurrences.

Replace with environment variable retrieval patterns.


Practical 2: Initialize HashiCorp Vault Locally

Install Vault.

Start server:

vault server -dev

Set environment variable:

export VAULT_ADDR="http://127.0.0.1:8200"

Store a secret:

vault kv put secret/app db_password="MySecurePass123"

Retrieve:

vault kv get secret/app

Practical 3: Inject Secrets at Runtime Using Environment Variables

Example Python app:

import os
db_password = os.getenv("DB_PASSWORD")

Start application with:

DB_PASSWORD="SuperSecure!" python app.py

No secret stored in file.


Practical 4: Rotate Database Credentials Using Vault Dynamic Secrets

Enable database secrets engine:

vault secrets enable database

Configure DB connection:

vault write database/config/mydb \
  plugin_name=postgresql-database-plugin \
  connection_url="postgres://root:pass@localhost:5432/mydb"

Generate temporary credentials:

vault read database/creds/mydb-role

Observe TTL and auto-expiration.


Practical 5: Scan Repositories for Existing Leaked Secrets

Install GitLeaks:

gitleaks detect

Try scanning your project.

Fix exposed secrets by:

• Removing them
• Rotating them
• Rewriting Git history if necessary


Practical 6: Create Encrypted Secrets Files Using SOPS

Install:

sudo apt install sops

Encrypt:

sops -e secrets.yaml > secrets.enc.yaml

Decrypt (restricted to certain roles):

sops -d secrets.enc.yaml

Practical 7: Set Up Secrets in GitHub Actions

Add secrets:

repo > Settings > Secrets and Variables

Use them in workflow:

env:
  API_KEY: ${{ secrets.API_KEY }}

Test that secrets do not appear in logs.


Practical 8: Disable Plaintext Secrets in CI/CD Logs

Configure CI to mask sensitive patterns.

Mask typical patterns:

• API Keys
• JWTs
• Passwords
• Tokens
• Private keys

In GitLab:

Settings > CI/CD > Masked Variables

Practical 9: Manage SSH Keys Securely

Generate SSH key:

ssh-keygen -t ed25519

Store private key in vault.
Store public key in authorized keys.

Do not store keys in repos or config files.


Practical 10: Validate Secrets Access Using IAM Roles

Use AWS example:

List principals with access to a secret:

aws secretsmanager get-resource-policy --secret-id my-secret

Restrict access to specific roles only.


Practical 11: Build a Secrets Rotation Cron Job

Example rotation:

#!/bin/bash
NEW_KEY=$(openssl rand -hex 32)
vault kv put secret/app API_KEY=$NEW_KEY

Schedule:

crontab -e

Practical 12: Prevent Secrets from Leaking via Debug Logs

Replace unsafe:

print(config)

With safe:

print("Configuration loaded")

Audit logs for accidental leaks.


Practical 13: Create a Secrets Access Audit System

Track access:

vault audit enable file file_path=/var/log/vault_audit.log

Review:

cat /var/log/vault_audit.log

Identify unusual access patterns.


Practical 14: Integrate Secrets Scanning Into CI Pipeline

Add to GitHub Actions:

- name: Scan for Secrets
  run: gitleaks detect --no-banner

Fail pipeline when secrets appear.


Practical 15: Replace Long-Lived Keys With Short-Lived Tokens

Use systems like:

• OAuth short-lived tokens
• Vault dynamic credentials
• AWS STS temporary tokens

Reduce attack window drastically.


Practical 16: Enforce Zero-Trust Access to Secrets

Implement:

• MFA for console access
• IP restrictions
• On-demand approval
• Role separation

Secrets only accessible when required.


Practical 17: Build a Multi-Environment Secrets Layout

Structure:

/secrets/
  dev/
  staging/
  prod/

Enforced by role separation and environment-based access.


Practical 18: Build a Secrets Naming Convention Policy

Define consistent names:

app/service/environment/secret_name

Examples:

web/api/prod/jwt_signing_key
db/user/dev/password

This improves organization and auditing.


Practical 19: Implement Secrets Caching With Expiration

Cache secrets for short usage windows:

• Memory cache
• TTL expiry
• Secure erase after usage

Avoid re-fetching from vault repeatedly.


Practical 20: Build a Full Secrets Management Architecture Diagram

Include:

• Secrets vault
• Access layer
• Applications
• CI/CD integration
• Monitoring
• Logging
• Rotation workflows
• IAM and RBAC

Use this diagram in team guidelines.


Intel Dump

• Secrets management protects credentials, tokens, and keys from exposure
• Hardcoded secrets are banned and must be replaced with environment injection or vault retrieval
• Vaults store secrets securely with encryption, RBAC, auditing, and rotation
• Least privilege access reduces risk of compromise
• Secrets must be rotated frequently and retrieved dynamically
• Logs must never contain sensitive data
• Extensive practicals cover vault setup, secret injection, scanning, rotation, IAM controls, CI integration, encrypted files, auditing, SSH key security, and environment segregation

HOME LEARN COMMUNITY DASHBOARD