-
Notifications
You must be signed in to change notification settings - Fork 0
feat(secrets): add SOPS + age secrets management with stackctl.sh integration #491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
55f4503
chore(secrets): update .sops.yaml for service-local .env.enc pattern
wax911 36ddaf7
docs: update README with secrets workflow reference
wax911 432cfe0
docs(secrets): update Phase 1 status in proposal
wax911 5e0abd4
docs(secrets): rewrite for service-local .env.enc workflow
wax911 ad8df4f
feat(secrets): add secrets subcommand to stackctl.sh
wax911 9551328
chore(secrets): allowlist .env.enc in gitignore
wax911 32f8bef
docs(secrets): update all READMEs and proposal for consistency
wax911 b8c282a
chore: remove .mcp.json and .opencode.json, add design spec
wax911 8178220
fix: harden secrets handling per PR #491 review
wax911 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| .env | ||
| .env.bak* | ||
| !*.env.enc | ||
| data | ||
| htpasswd | ||
| *.log | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| # SOPS configuration for encrypting secrets committed to the repo | ||
| # SOPS configuration for encrypting service-local .env files | ||
| # Each service directory (e.g., postgres/, traefik/) stores an encrypted .env.enc | ||
| # alongside its .env.example. Decrypt with: stackctl.sh secrets decrypt [service] | ||
| creation_rules: | ||
| - path_regex: secrets/.*\.(env|yaml|yml)$ | ||
| # Replace with your age public key(s) | ||
| age: | ||
| - "age1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Replace with your real age public key | ||
| - path_regex: \.env$ | ||
| key_groups: | ||
| - age: | ||
| - age12ph7sgtptrrcxzxdue28j3lesnu9gj73ae9ewuvg66awp6jpae8smyvspx | ||
| encrypted_regex: '^(?!#)' | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,68 +1,142 @@ | ||
| # Managing Secrets Securely | ||
|
|
||
| Never store real secrets in `.env` files in the repo. Keep `.env.example` as documentation with placeholders only. | ||
| Secrets are encrypted at rest in git using SOPS + age and decrypted just-in-time at deploy time. Each service directory stores an encrypted `.env.enc` alongside its `.env.example`. | ||
|
|
||
| ## Recommended approach (encrypted files committed to git) | ||
| ## Workflow Overview | ||
|
|
||
| Use `sops` + age (or GPG) to encrypt per-environment secrets that can be safely committed. | ||
| ``` | ||
| .env.example → .env (fill values) → .env.enc (encrypt) → git commit | ||
| ↓ | ||
| .git (encrypted) → .env.enc → .env (decrypt) → deploy → shred .env | ||
| ``` | ||
|
|
||
| ## Setup | ||
|
|
||
| ### 1) Install tools | ||
| - age: https://age-encryption.org | ||
| - sops: https://github.com/getsops/sops | ||
| ### 1. Install tools | ||
|
|
||
| ### 2) Create an age key pair (once) | ||
| ```bash | ||
| # writes to ~/.config/sops/age/keys.txt | ||
| # macOS | ||
| brew install sops age | ||
|
|
||
| # Linux | ||
| # sops: https://github.com/getsops/sops/releases | ||
| # age: https://github.com/FiloSottile/age/releases | ||
| ``` | ||
|
|
||
| ### 2. Generate an age key pair (once per machine) | ||
|
|
||
| ```bash | ||
| mkdir -p ~/.config/sops/age | ||
| age-keygen -o ~/.config/sops/age/keys.txt | ||
| # The public key is printed — add it to .sops.yaml key_groups | ||
| ``` | ||
| Add the public recipient from that file (starts with `age1...`) to your repository SOPS config. | ||
|
|
||
| ### 3) Add a SOPS config | ||
| Create `.sops.yaml` at repo root: | ||
| ### 3. Add your public key to `.sops.yaml` | ||
|
|
||
| Edit `.sops.yaml` and add your age public key to the `key_groups` list. Multiple recipients can be listed for team access: | ||
|
|
||
| ```yaml | ||
| # Encrypt files matching these globs with the recipient below | ||
| creation_rules: | ||
| - path_regex: secrets/.*\.(env|yaml|yml)$ | ||
| age: ["AGE1_PUBLIC_KEY_HERE"] | ||
| encrypted_regex: '^(?!#)' | ||
| - path_regex: \.env$ | ||
| key_groups: | ||
| - age: | ||
| - age1existingkey... # existing recipient | ||
| - age1yournewkey... # your new key | ||
| ``` | ||
| Replace `AGE1_PUBLIC_KEY_HERE` with your public age key. | ||
|
|
||
| ### 4) Create encrypted secret files | ||
| Place per-environment secrets under `secrets/` and encrypt with sops: | ||
| ## Encrypting secrets | ||
|
|
||
| For a single service: | ||
|
|
||
| ```bash | ||
| mkdir -p secrets | ||
| printf "TRAEFIK_ENABLE=true\nSSO_CREDENTIALS=admin:$apr1$...\n" > secrets/traefik.dev.env | ||
| sops -e -i secrets/traefik.dev.env | ||
| # Create .env from .env.example and fill in real values | ||
| cp postgres/.env.example postgres/.env | ||
| # Edit postgres/.env with real values... | ||
|
|
||
| # Encrypt | ||
| ./stackctl.sh secrets encrypt postgres | ||
| ``` | ||
| The file is now encrypted at rest and safe to commit. | ||
|
|
||
| ### 5) Decrypt for local use | ||
| For all services at once: | ||
|
|
||
| ```bash | ||
| # Produces a plaintext file for docker usage (do not commit this) | ||
| sops -d secrets/traefik.dev.env > traefik/.env | ||
| ./stackctl.sh secrets encrypt | ||
| ``` | ||
| You can add a simple make/script target to automate decrypt -> deploy -> clean. | ||
|
|
||
| ### 6) CI/CD or remote deploy | ||
| On a deployment host, provision the age private key (read-only, secured). Decrypt secrets just-in-time before `docker stack deploy`. | ||
| This runs `sops --encrypt --input-type dotenv --output-type dotenv .env > .env.enc` for each service directory that has a `.env` file. | ||
|
|
||
| ## Using Docker Swarm secrets (optional/advanced) | ||
| Docker Swarm supports native secrets. You can combine sops+age with `docker secret create`: | ||
| ## Deploying | ||
|
|
||
| Deploy decrypts, renders, deploys, and shreds in one step: | ||
|
|
||
| 1) Decrypt locally in memory and pipe to secret create: | ||
| ```bash | ||
| sops -d secrets/traefik.dev.env | docker secret create traefik_env - | ||
| # Deploy a specific service's stack | ||
| ./stackctl.sh secrets deploy postgres | ||
|
|
||
| # Deploy all services | ||
| ./stackctl.sh secrets deploy | ||
| ``` | ||
| 2) Reference the secret in your stack file using `secrets:` and `env_file` alternatives where appropriate. | ||
|
|
||
| This is more granular and keeps values out of env vars in the container filesystem, but requires adjusting service configs to read from files or environment sourced from secrets. | ||
| The deploy operation: | ||
| 1. Decrypts `.env.enc` → `.env` for each target service | ||
| 2. Regenerates rendered stack files (variable substitution) | ||
| 3. Deploys the relevant Docker Swarm stacks | ||
| 4. Shreds all plaintext `.env` files | ||
|
|
||
| ## Decrypting (manual) | ||
|
|
||
| If you need to inspect or edit secrets without deploying: | ||
|
|
||
| ```bash | ||
| # Decrypt a single service | ||
| ./stackctl.sh secrets decrypt postgres | ||
|
|
||
| # Decrypt all services | ||
| ./stackctl.sh secrets decrypt | ||
| ``` | ||
|
|
||
| **Remember to clean up plaintext files after editing:** | ||
|
|
||
| ```bash | ||
| ./stackctl.sh secrets clean | ||
| ``` | ||
|
|
||
| ## Cleaning up | ||
|
|
||
| Remove all plaintext `.env` files that have a corresponding `.env.enc`: | ||
|
|
||
| ```bash | ||
| ./stackctl.sh secrets clean | ||
| ``` | ||
|
|
||
| This uses `shred -u` when available, falling back to `rm -f` on systems without `shred`. | ||
|
|
||
| ## Key rotation | ||
|
|
||
| After adding a new recipient to `.sops.yaml`: | ||
|
|
||
| ```bash | ||
| sops updatekeys --yes **/.env.enc | ||
|
wax911 marked this conversation as resolved.
Outdated
|
||
| ``` | ||
|
|
||
| ## Re-encrypting after editing | ||
|
|
||
| ```bash | ||
| # Decrypt, edit, re-encrypt | ||
| ./stackctl.sh secrets decrypt postgres | ||
| # Edit postgres/.env... | ||
| ./stackctl.sh secrets encrypt postgres | ||
| ./stackctl.sh secrets clean | ||
| ``` | ||
|
|
||
| ## Git hygiene | ||
| - Commit only `.env.example` files and encrypted files under `secrets/`. | ||
| - Never commit plaintext `.env`. | ||
| - Add a `.gitignore` rule for `**/.env` and `**/*.env.decrypted` as needed. | ||
|
|
||
| - **Commit**: `.env.example` (placeholders) and `.env.enc` (encrypted secrets) | ||
| - **Never commit**: `.env` (plaintext, gitignored) | ||
| - The `.gitignore` rule `!*.env.enc` ensures encrypted files are tracked | ||
|
|
||
| ## Troubleshooting | ||
| - If `sops` can’t decrypt: ensure the age private key is in `~/.config/sops/age/keys.txt`. | ||
| - For team usage: include multiple recipients in `.sops.yaml` so each developer can decrypt. | ||
|
|
||
| - **`sops` can't decrypt**: Ensure the age private key is at `~/.config/sops/age/keys.txt` and the corresponding public key is in `.sops.yaml`. | ||
| - **`shred` not found**: The script falls back to `rm -f`. Install `shred` (part of `coreutils` on Linux) for secure deletion. | ||
| - **`sops` or `age` not found**: Run `./stackctl.sh doctor` to check prerequisites, or install them manually. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.