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.

Backend Stop 1 of 2 By UniqBlog Desk 30 November 2026 Updated 1 December 2026 5 min read

Soft 3D illustration: three open safe doors of increasing size nested one behind another, with a blue key on a ring floating through the smallest

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?

StageToolWhere secrets liveGood forWatch out for
1.env file, git-ignoredEach machineSolo, one environmentCopies drift; no audit; easy to commit
2SOPS and ageEncrypted in gitSmall teams, GitOps, a few environmentsKey distribution; rotation is manual
3Cloud secret managerAWS, Google Cloud or Azure serviceApps already on one cloudLock-in; usage-based pricing
4Vault or OpenBaoYour own clusterMany teams, dynamic credentials, multi-cloudYou operate a critical service
5External Secrets OperatorAny of 2 to 4, synced into KubernetesKubernetes workloadsSynced 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

  1. Never commit plaintext secrets. Git-ignore .env* and keep a committed .env.example with fake values.
  2. Run gitleaks in a pre-commit hook and in CI.
  3. Scope per environment: separate keys, accounts and tokens for dev, staging and prod.
  4. Give CI the narrowest token that works, and prefer short-lived credentials.
  5. Rotate on a schedule, and immediately after a leak or a departure.
  6. Deliver secrets as files, not environment variables, where the app supports it.
  7. On Kubernetes, enable encryption at rest and restrict who can read Secrets.
  8. Write down who holds each key, and test that you can recover if they leave.

Sources

UniqBlog Desk · Editors

The editors of UniqBlog. Tested tutorials and tech news for working developers.

Further along the line