Secrets management means storing database credentials, API keys, and SSH keys outside application code and controlling who can read them. This guide covers environment variables, secret files, Docker secrets, and what to avoid.
Why secrets do not belong in code
Hardcoding secrets in source code is one of the most common security mistakes. When code is committed to version control, secrets are committed with it. Even private repositories are eventually accessed by former employees, contractors, or compromised accounts.
A secret committed to Git is never truly removed. Rewriting history does not help if the repository was cloned or pushed to a remote before the secret was removed.
Option 1: Environment variables
Environment variables keep secrets out of source code. The application reads them at runtime:
“`bash
export DATABASE_URL=”postgres://user:password@localhost:5432/mydb”
export API_KEY=”sk_live_1234567890abcdef”
“`
In a script or application:
“`python
import os
database_url = os.environ.get(“DATABASE_URL”)
api_key = os.environ.get(“API_KEY”)
“`
Store environment variables in a .env file that is excluded from version control:
“`bash
# .gitignore
.env
“`
Environment variables work well for single-server deployments. They are harder to manage across many servers or containers.
Option 2: Secret files with restricted permissions
Store secrets in files with strict filesystem permissions:
“`bash
echo “sk_live_1234567890abcdef” > /etc/myapp/api_key
chmod 600 /etc/myapp/api_key
“`
The application reads the file at startup. Only the user running the application can read it.
“`python
with open(“/etc/myapp/api_key”, “r”) as f:
api_key = f.read().strip()
“`
Option 3: Docker secrets
Docker Swarm and Docker Compose support secrets, which are mounted into containers as temporary files in /run/secrets/:
“`yaml
# docker-compose.yml
services:
app:
image: myapp:latest
secrets:
– db_password
– api_key
secrets:
db_password:
file: ./db_password.txt
api_key:
file: ./api_key.txt
“`
The application reads /run/secrets/db_password at runtime. Secrets are not stored in the image layer and are only available to containers that need them.
Option 4: Dedicated secret managers
For production, use a dedicated secret management service:
- AWS Secrets Manager: Stores and rotates secrets automatically
- HashiCorp Vault: Open-source secret management with fine-grained access control
- Azure Key Vault: Microsoft’s secret management service
- Google Secret Manager: Google Cloud’s secret management service
These services provide encryption at rest, access auditing, and automatic rotation. They require more setup but are the right choice for production environments.
What to avoid
- Never commit
.envfiles,config.phpwith passwords, orsettings.pywith API keys to Git. - Never email secrets or paste them into chat systems like Slack or Discord.
- Do not store secrets in Docker image layers.
ENVinstructions in Dockerfiles are visible to anyone who can inspect the image. - Do not rotate secrets without updating all services that use them. Coordinate rotation with deployment.
Rotation basics
Secrets should be rotated periodically and immediately after:
- A team member leaves
- A repository is accidentally made public
- A service is compromised
- A secret is accidentally logged or exposed
When rotating, update the secret in your secret manager, deploy the new value to all services, then revoke the old secret.
Related reading
For SSH key management, see SSH Key Permissions Too Open: Why SSH Rejects Your Key. For SSH hardening including key-based access, see SSH Hardening Guide: Ubuntu 24.04.