Secrets management across environments: a practical ladder
A secrets management ladder for dev, staging and prod: from .env files to SOPS and age, cloud secret managers, Vault or OpenBao, and External Secrets Operator.
Keep one source of truth per environment and never let a plaintext secret reach git. For a solo project that can be a .env file outside the repo. Once a team or a second environment is involved, commit the secrets encrypted with SOPS and age, or move them into a secret manager (your cloud's, or Vault or OpenBao), and let the runtime fetch them. Add a scanner such as gitleaks so mistakes are caught before they ship.
Key takeaways
- Climb the ladder only as far as you need:
.env, then SOPS and age, then a cloud secret manager, then Vault or OpenBao, then External Secrets Operator for Kubernetes. - Separate keys per environment: a staging leak must not unlock production.
- Deleting a leaked file in a later commit does not remove it from history. Rotate the secret.
- Kubernetes Secrets are not encrypted by default; they are a delivery mechanism, not a vault.
Tested on 2026-12-01 with SOPS 3.13.3, age 1.3.2, gitleaks 8.30.1 and Docker Compose v5.7.0.
Why do secrets get hard across environments?
The first project has one .env. Then come staging, production, a second service, a teammate and a CI pipeline. Soon the same database password exists in five places and nobody knows which copy is current.
Leaks rarely start in the app. In the 1Password incident disclosed on 2026-11-25, an attacker used an employee's compromised GitHub account to reach source code and CI/CD infrastructure credentials. 1Password says customer vaults were not affected. The lesson for everyone else: CI tokens and deploy keys are secrets too, and they need scoping and rotation like any password.
Which approach fits your stage?
| Stage | Tool | Where secrets live | Good for | Watch out for |
|---|---|---|---|---|
| 1 | .env file, git-ignored | Each machine | Solo, one environment | Copies drift; no audit; easy to commit |
| 2 | SOPS and age | Encrypted in git | Small teams, GitOps, a few environments | Key distribution; rotation is manual |
| 3 | Cloud secret manager | AWS, Google Cloud or Azure service | Apps already on one cloud | Lock-in; usage-based pricing |
| 4 | Vault or OpenBao | Your own cluster | Many teams, dynamic credentials, multi-cloud | You operate a critical service |
| 5 | External Secrets Operator | Any of 2 to 4, synced into Kubernetes | Kubernetes workloads | Synced values are plain Kubernetes Secrets |
The cloud options are AWS Secrets Manager, Google Secret Manager and Azure Key Vault. On licensing: HashiCorp Vault is under the Business Source License 1.1 since 1.15. OpenBao describes itself as "an open source, community-driven secrets manager and fork of Vault managed by the Linux Foundation's OpenSSF".
How do you encrypt secrets in git with SOPS and age?
SOPS encrypts the values of a YAML, JSON, ENV or INI file and leaves the keys readable, so diffs and reviews still make sense. age provides the key pair. We ran this round trip in a scratch folder:
age-keygen -o key.txt # prints "Public key: age1..."
PUB=$(age-keygen -y key.txt)
cat > .sops.yaml <<EOF
creation_rules:
- path_regex: secrets/.*\.yaml$
age: $PUB
EOF
mkdir -p secrets
cat > secrets/staging.yaml <<'EOF'
database_url: postgres://app:not-a-real-password@db:5432/app
stripe_key: sk_test_example_only
log_level: debug
EOF
sops encrypt -i secrets/staging.yaml
The committed file now looks like this (truncated):
database_url: ENC[AES256_GCM,data:1qZgTSqlGpYH...,iv:...,tag:...,type:str]
stripe_key: ENC[AES256_GCM,data:7LdOQeZ+sgs5...,iv:...,tag:...,type:str]
log_level: ENC[AES256_GCM,data:vCTGY8I=,iv:...,tag:...,type:str]
sops:
...
Decrypting needs the private key. Point SOPS_AGE_KEY_FILE at it, or put it at the default location, $XDG_CONFIG_HOME/sops/age/keys.txt on Linux:
SOPS_AGE_KEY_FILE=key.txt sops decrypt --extract '["database_url"]' secrets/staging.yaml
# postgres://app:not-a-real-password@db:5432/app
# run a command with the values as environment variables, nothing written to disk
SOPS_AGE_KEY_FILE=key.txt sops exec-env secrets/staging.yaml 'echo "log level is $log_level"'
# log level is debug
Without the key, sops decrypt fails with "no master key was able to decrypt the file". That is the point.
To keep environments separate, give each one its own age key and its own creation rule, for example secrets/staging/ and secrets/prod/. Developers hold the staging key; only the production deploy job holds the prod key. SOPS also supports AWS KMS, GCP KMS, Azure Key Vault and PGP, so you can swap age for a cloud key later without changing the file layout.
How should containers receive secrets?
Prefer files over environment variables. Docker's Compose secrets guide warns that environment variables "can also be printed in logs when debugging errors without your knowledge". Compose mounts each secret at /run/secrets/<name>:
services:
db:
image: postgres:18
environment:
POSTGRES_PASSWORD_FILE: /run/secrets/db_password
secrets:
- db_password
secrets:
db_password:
file: ./db_password.txt # decrypted by your deploy step, git-ignored
In our run, docker inspect showed only POSTGRES_PASSWORD_FILE=/run/secrets/db_password, never the password itself. The file kept its host permissions (0600, owned by our user). That worked because the Postgres entrypoint reads it as root, but an app running as a different user would need the mode adjusted.
What about Kubernetes?
Read the Kubernetes caution first: "Kubernetes Secrets are, by default, stored unencrypted in the API server's underlying data store (etcd)." The docs ask you to enable encryption at rest, apply least-privilege RBAC and consider external secret stores.
External Secrets Operator (v2.15.0 as of 2026-11-10) syncs values from a provider into a Kubernetes Secret. Its getting-started guide uses the external-secrets.io/v1 API:
apiVersion: external-secrets.io/v1
kind: ExternalSecret
metadata:
name: app-db
spec:
refreshInterval: 1h0m0s
secretStoreRef:
name: prod-store
kind: SecretStore
target:
name: app-db
creationPolicy: Owner
data:
- secretKey: password
remoteRef:
key: prod/app/db-password
ESO has providers for the cloud managers, Vault and others, including Bitwarden Secrets Manager. That provider needs the separate Bitwarden SDK Server, which must run over HTTPS. We did not run a cluster for this guide; the YAML follows the official docs.
How do you catch a leaked secret before it ships?
Scan the repository, including history. We committed a fake GitHub token in .env, then removed the file in a second commit, and ran gitleaks:
gitleaks git -v .
# Finding: GITHUB_TOKEN=ghp_…
# RuleID: github-pat
# File: .env
# Commit: 0547783…
# INF 2 commits scanned.
# WRN leaks found: 1
echo $? # 1 when leaks are found
The second commit did not help: the token still lives in the first. Rotate it, then clean history if you must. gitleaks also scans plain folders (gitleaks dir) and ships a pre-commit hook, and its non-zero exit code fails a CI job.
Checklist: secrets across environments
- Never commit plaintext secrets. Git-ignore
.env*and keep a committed.env.examplewith fake values. - Run gitleaks in a pre-commit hook and in CI.
- Scope per environment: separate keys, accounts and tokens for dev, staging and prod.
- Give CI the narrowest token that works, and prefer short-lived credentials.
- Rotate on a schedule, and immediately after a leak or a departure.
- Deliver secrets as files, not environment variables, where the app supports it.
- On Kubernetes, enable encryption at rest and restrict who can read Secrets.
- Write down who holds each key, and test that you can recover if they leave.
Sources
Transfer to
UniqBlog Desk · Editors
The editors of UniqBlog. Tested tutorials and tech news for working developers.
Further along the line

PostgreSQL vs MySQL in 2026: which database should you pick?
PostgreSQL vs MySQL compared on current versions: JSON, CTEs, window functions, extensions, replication, licensing and cost, with SQL we ran on both.

Kubernetes vs Docker: which one do you actually need?
Kubernetes vs Docker is not either-or: Docker builds and runs containers, Kubernetes schedules them across machines. Here is when each layer earns its cost.

Docker Compose volumes: bind mounts, tmpfs and backups
Docker Compose volumes explained with a tested compose file: when to use named volumes, bind mounts or tmpfs, how to name them, and how to back up and restore one.

Git rebase vs merge: when to use each, with a demo
Git rebase vs merge with a reproducible demo: how each shapes history, when to rebase, the golden rule, safe force-pushes, and git pull vs fetch.