sops-age-secrets
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSOPS + age Secrets Management
SOPS + age 密钥管理
Encrypt secrets in git with SOPS using age keys. Values are encrypted with AES256-GCM; keys are simple X25519 keypairs.
使用age密钥通过SOPS加密git中的密钥。数值采用AES256-GCM加密;密钥为简单的X25519密钥对。
Quick Reference
快速参考
| Task | Command |
|---|---|
| Generate age key | |
| Extract public key | |
| Encrypt file | |
| Decrypt file | |
| Edit encrypted file | |
| Update recipients | |
| Rotate data key | |
| 任务 | 命令 |
|---|---|
| 生成age密钥 | |
| 提取公钥 | |
| 加密文件 | |
| 解密文件 | |
| 编辑加密文件 | |
| 更新接收方 | |
| 轮换数据密钥 | |
Initial Setup
初始设置
1. Generate Keys
1. 生成密钥
bash
undefinedbash
undefinedCreate key directory
创建密钥目录
mkdir -p ~/.config/sops/age
mkdir -p ~/.config/sops/age
Generate keypair
生成密钥对
age-keygen -o ~/.config/sops/age/keys.txt
age-keygen -o ~/.config/sops/age/keys.txt
Output: public key: age1abc123...
输出: public key: age1abc123...
undefinedundefined2. Create .sops.yaml
2. 创建.sops.yaml
At repository root:
yaml
creation_rules:
- age: age1yourpublickeyhere...在仓库根目录下创建:
yaml
creation_rules:
- age: age1yourpublickeyhere...3. Encrypt First File
3. 加密第一个文件
bash
sops encrypt config/secrets.yaml > config/secrets.enc.yaml
rm config/secrets.yaml
git add config/secrets.enc.yaml .sops.yamlbash
sops encrypt config/secrets.yaml > config/secrets.enc.yaml
rm config/secrets.yaml
git add config/secrets.enc.yaml .sops.yamlCore Workflows
核心工作流
Encrypt New File
加密新文件
bash
undefinedbash
undefinedCreate plaintext file
创建明文文件
cat > secrets.yaml << 'EOF'
database:
password: secret123
api_key: abc-xyz
EOF
cat > secrets.yaml << 'EOF'
database:
password: secret123
api_key: abc-xyz
EOF
Encrypt (uses .sops.yaml rules)
加密(使用.sops.yaml中的规则)
sops encrypt secrets.yaml > secrets.enc.yaml
rm secrets.yaml
undefinedsops encrypt secrets.yaml > secrets.enc.yaml
rm secrets.yaml
undefinedEdit Encrypted File
编辑加密文件
bash
undefinedbash
undefinedOpens decrypted in $EDITOR, re-encrypts on save
在$EDITOR中打开解密后的内容,保存时自动重新加密
sops edit secrets.enc.yaml
undefinedsops edit secrets.enc.yaml
undefinedDecrypt for Use
解密使用
bash
undefinedbash
undefinedTo stdout
输出到标准输出
sops decrypt secrets.enc.yaml
sops decrypt secrets.enc.yaml
To file
输出到文件
sops decrypt secrets.enc.yaml > secrets.yaml
sops decrypt secrets.enc.yaml > secrets.yaml
Extract single value
提取单个值
sops decrypt --extract '["database"]["password"]' secrets.enc.yaml
undefinedsops decrypt --extract '["database"]["password"]' secrets.enc.yaml
undefinedPass to Process (No File)
传递给进程(不生成文件)
bash
undefinedbash
undefinedAs environment variables
作为环境变量
sops exec-env secrets.enc.yaml './deploy.sh'
sops exec-env secrets.enc.yaml './deploy.sh'
As temporary file
作为临时文件
sops exec-file secrets.enc.yaml 'source {}'
undefinedsops exec-file secrets.enc.yaml 'source {}'
undefinedMulti-Environment Configuration
多环境配置
yaml
undefinedyaml
undefined.sops.yaml
.sops.yaml
creation_rules:
Production - admin + CI only
- path_regex: ^config/secrets/prod..* age: >- age1admin..., age1cicd...
Staging/Dev - broader access
- path_regex: ^config/secrets/.* age: >- age1admin..., age1cicd..., age1dev...
undefinedcreation_rules:
生产环境 - 仅管理员和CI可访问
- path_regex: ^config/secrets/prod..* age: >- age1admin..., age1cicd...
预发/开发环境 - 更广的访问权限
- path_regex: ^config/secrets/.* age: >- age1admin..., age1cicd..., age1dev...
undefinedSelective Encryption
选择性加密
Only encrypt sensitive keys (keeps file readable):
yaml
creation_rules:
- age: age1...
encrypted_regex: ^(password|secret|token|key|api_key|private)$Result:
yaml
database:
host: localhost # plaintext
password: ENC[AES256_GCM,data:...,type:str] # encrypted仅加密敏感键(保持文件可读性):
yaml
creation_rules:
- age: age1...
encrypted_regex: ^(password|secret|token|key|api_key|private)$效果:
yaml
database:
host: localhost # 明文
password: ENC[AES256_GCM,data:...,type:str] # 加密CI/CD Integration
CI/CD集成
GitHub Actions
GitHub Actions
yaml
- name: Decrypt secrets
env:
SOPS_AGE_KEY: ${{ secrets.SOPS_AGE_KEY }}
run: sops decrypt config/secrets.enc.yaml > secrets.yamlStore in repository secrets as .
AGE-SECRET-KEY-1...SOPS_AGE_KEYyaml
- name: Decrypt secrets
env:
SOPS_AGE_KEY: ${{ secrets.SOPS_AGE_KEY }}
run: sops decrypt config/secrets.enc.yaml > secrets.yaml将作为存储在仓库密钥中。
AGE-SECRET-KEY-1...SOPS_AGE_KEYEnvironment Variables
环境变量
bash
undefinedbash
undefinedKey file location
密钥文件路径
export SOPS_AGE_KEY_FILE=/path/to/keys.txt
export SOPS_AGE_KEY_FILE=/path/to/keys.txt
Key value directly (CI/CD)
直接传入密钥值(适用于CI/CD)
export SOPS_AGE_KEY="AGE-SECRET-KEY-1..."
undefinedexport SOPS_AGE_KEY="AGE-SECRET-KEY-1..."
undefinedKey Management
密钥管理
Add New Recipient
添加新接收方
- Update with new public key
.sops.yaml - Re-encrypt existing files:
bash
sops updatekeys -y file.enc.yaml
- 在中添加新的公钥
.sops.yaml - 重新加密现有文件:
bash
sops updatekeys -y file.enc.yaml
Remove Recipient
移除接收方
- Remove from
.sops.yaml - Re-encrypt and rotate:
bash
sops updatekeys -y file.enc.yaml sops rotate -i file.enc.yaml
- 从中删除对应公钥
.sops.yaml - 重新加密并轮换密钥:
bash
sops updatekeys -y file.enc.yaml sops rotate -i file.enc.yaml
Key Locations
密钥默认路径
| Platform | Default Path |
|---|---|
| Linux | |
| macOS | |
| 平台 | 默认路径 |
|---|---|
| Linux | |
| macOS | |
Reference Files
参考文件
| File | When to Read |
|---|---|
| age-keys.md | Key generation, storage, distribution patterns |
| sops-config.md | .sops.yaml syntax, path rules, key groups |
| cli-reference.md | Full command reference, all flags |
| ci-cd-patterns.md | GitHub Actions, GitLab CI, Docker integration |
| troubleshooting.md | Common errors and solutions |
| 文件 | 适用场景 |
|---|---|
| age-keys.md | 密钥生成、存储、分发模式 |
| sops-config.md | .sops.yaml语法、路径规则、密钥组 |
| cli-reference.md | 完整命令参考、所有参数 |
| ci-cd-patterns.md | GitHub Actions、GitLab CI、Docker集成 |
| troubleshooting.md | 常见错误与解决方案 |
Common Issues
常见问题
| Problem | Solution |
|---|---|
| "could not decrypt data key" | Wrong key - check |
| "no matching keys found" | File uses Shamir key groups - need multiple keys |
| Run from repo root or ensure file is in parent directory |
| path_regex not matching | Use regex syntax ( |
| 问题 | 解决方案 |
|---|---|
| "could not decrypt data key" | 密钥错误 - 检查 |
| "no matching keys found" | 文件使用Shamir密钥组 - 需要多个密钥才能解密 |
未找到 | 从仓库根目录运行命令,或确保文件位于父目录中 |
| path_regex不匹配 | 使用正则语法( |
Security Notes
安全注意事项
- Never commit private keys - Add ,
keys.txtto*.agekey.gitignore - Use dedicated CI keys - Easier to rotate, limit scope
- Rotate data keys - Run periodically
sops rotate - Limit recipients - Production files should have minimal access
- 绝对不要提交私钥 - 将、
keys.txt添加到*.agekey中.gitignore - 使用专用CI密钥 - 更易于轮换,权限范围更小
- 轮换数据密钥 - 定期执行
sops rotate - 限制接收方范围 - 生产环境文件的可访问人员应尽可能少
Official Documentation
官方文档
| Topic | URL |
|---|---|
| SOPS | https://getsops.io/docs/ |
| SOPS GitHub | https://github.com/getsops/sops |
| age | https://github.com/FiloSottile/age |
| 主题 | URL |
|---|---|
| SOPS | https://getsops.io/docs/ |
| SOPS GitHub | https://github.com/getsops/sops |
| age | https://github.com/FiloSottile/age |