12-Factor App Configuration, Secrets Management & .env Cloud Runbook
Engineering strict separation between application code and runtime configuration. Learn how 12-factor architecture governs environment variables, how to securely manage secrets across local development and production Kubernetes clusters, and how to safely serialize .env files into JSON and YAML without credential leakage.
1. The 12-Factor Configuration Law: Strict Separation of Code and Config
// โ FATAL SECURITY FLAW: Hardcoded credentials committed to Git
const db = connect('postgres://admin:Password123@db.prod.internal:5432/app');
const stripeKey = 'sk_live_51Ny8bFEXAMPLEKEY9921';
// โ
COMPLIANT: Ingest configuration strictly from environment variables at runtime
import { z } from 'zod';
const EnvSchema = z.object({
DATABASE_URL: z.string().url(),
STRIPE_SECRET_KEY: z.string().startsWith('sk_'),
PORT: z.coerce.number().default(3000),
NODE_ENV: z.enum(['development', 'test', 'production']).default('development')
});
export const env = EnvSchema.parse(process.env);2. Local Development Hierarchies & The .env.example Template Standard
.env files. To avoid accidental check-ins while ensuring seamless onboarding, teams employ an explicit file hierarchy:
1. .env.example: Checked into Git; contains every required key with sanitized dummy values and setup instructions.
2. .env: Local baseline; ignored by Git.
3. .env.local: Machine-specific overrides; always ignored by Git.
4. .env.test: Dedicated fixtures for test runners (Jest, Vitest, Pytest).| Configuration File | Committed to Git Repository | Primary Contents | Security Sensitivity Level | Copy |
|---|---|---|---|---|
| .env.example | Yes (Publicly visible) | Key names with placeholder documentation | Zero (Strictly no real credentials permitted) | |
| .env | No (In .gitignore) | Local development endpoints & test credentials | Low to Medium (Local docker containers) | |
| .env.production | Never (Strictly forbidden) | Live database strings, API secrets, private keys | Critical (Instant compromise if exposed) |
3. Secret Masking & Automated Pre-Commit Defense
SECRET, PASSWORD, TOKEN, PRIVATE, and KEY into redacted values (********). Additionally, Git pre-commit hooks utilizing tools like gitleaks intercept commits containing private RSA keys or AWS tokens before they can be pushed to remote origins.# .pre-commit-config.yaml
repos:
- repo: https://github.com/gitleaks/gitleaks
rev: v8.18.0
hooks:
- id: gitleaks
# .gitignore - Essential Environment Block
.env
.env.*
!.env.example
*.pem
*.key4. Enterprise Secrets Architecture: Kubernetes ConfigMaps vs. Secrets vs. Cloud Vaults
| Infrastructure Tier | Storage Mechanism | Encryption at Rest | Primary Use Case | Copy |
|---|---|---|---|---|
| Kubernetes ConfigMap | Plain key-value etcd store | Standard etcd encryption | Non-sensitive parameters (PORT, LOG_LEVEL, HOSTNAMES) | |
| Kubernetes Secret | Base64 encoded etcd object | KMS envelope encryption | Database credentials, TLS certificates, bearer tokens | |
| Cloud Secret Manager | AWS Secrets Manager / Vault / GCP | Hardware Security Module (HSM) | Automated secret rotation, IAM-audited access logs |
5. Cross-Platform Serialization: Transforming .env to JSON, YAML & Shell
export KEY="val" syntax required when sourcing environment variables into CI/CD pipeline steps, SSH remote sessions, or Linux systemd services.
Automated converters parse string representations into native booleans (true/false) and numeric integers, eliminating manual typing errors across multi-cloud deployments.# Source .env:
DB_PORT=5432
ENABLE_CACHE=true
API_KEY=sk_test_99f8d7c
# Converted to JSON (with Type Coercion):
{
"DB_PORT": 5432,
"ENABLE_CACHE": true,
"API_KEY": "sk_test_99f8d7c"
}
# Converted to POSIX Shell Export:
export DB_PORT="5432"
export ENABLE_CACHE="true"
export API_KEY="sk_test_99f8d7c"Related Free Online Tools
Put these techniques into practice immediately with our privacy-first browser utilities:
Convert .env configuration files into strongly-typed JSON, YAML, or shell export scripts with type coercion and secret masking.
Deep semantic comparison between two JSON payloads to detect added, removed, and modified values regardless of key ordering.
Calculate SHA-256, SHA-512, SHA-1, and MD5 hashes from text or files via Web Crypto, and verify against expected release checksums.