Git crypt
git-crypt
git-crypt 对 git 仓库中的数据进行透明的加解密,选择保护的文件在提交时会加密,在检出时会解密。
生成一个密钥使得当前的 git 库可以使用 git-crypt
检查文件加密状态
创建 .gitattributes 来指定加密的文件
确保将敏感文件移出存储库并提交更改,然后添加 .gitattributes 文件,然后将敏感文件移回存储库。
SOPS
Comparison of secret managing schemes - NixOS Wiki > GitHub - Mic92/sops-nix: Atomic secret provisioning for NixOS based on sops
对一系列的配置文件进行加密的方法。
生成密钥
生成 ed25519 私钥
生成 age 私钥
由 ed25519 私钥生成相应的 age 私钥
mkdir -p ~/.config/sops/age
nix-shell -p ssh-to-age --run "ssh-to-age -private-key -i ~/.ssh/id_ed25519 > ~/.config/sops/age/keys.txt"
由 age 私钥找到相应的公钥
将 ssh 公钥转化为 age 公钥
nix-shell -p ssh-to-age --run 'ssh-keyscan my-server.com | ssh-to-age'
nix-shell -p ssh-to-age --run 'cat /etc/ssh/ssh_host_ed25519_key.pub | ssh-to-age'
配置文件
.sops.yaml密钥配置,索引 secrets 的文件secrets.yaml存储 secrets 的文件
keys 指定用户与相应公钥
path_regex 正则匹配需加密的文件,key_groups 指定密钥类型与用户
配置 .sops.yaml 后,使用 EDITOR 打开一个新文件,指示的路径需要匹配 config 中的正则规则。
例如:
# Files must always have a string value
example-key: example-value
# Nesting the key results in the creation of directories.
# These directories will be owned by root:keys and have permissions 0751.
myservice:
my_subdir:
my_secret: password1
保存后的文件就是加密后的文件
之后使用配置文件(声明)
解密
再次打开加密后的文件时,会尝试用密钥进行解密。
本地配置的 age 私钥 ~/.config/sops/age/keys.txt 需要匹配规则其一。
sops-nix 会在 activate 过程中进行解密,内容放置在 /run/secrets,
通过 config.sops.secrets.example-key.path 指定的内容将被替换为 /run/secrets/example-key
sops-install-secrets 自动使用 ssh 目录中的密钥
也可以通过 sops.age.keyFile = "/path/to/keys.txt"; 指定
用户组
总结
对于同一用户组下的一个用户端与另一个远程主机的简单实践(都能访问 secrets.yaml)
- 用户或远程主机:编辑加密的配置文件
sops secrets.yaml需要在本地有一个 age 私钥- 这里的 age 私钥与
.sops.yaml中指定的公钥对应
- 这里的 age 私钥与
- 用户:用
sops-nix部署用户配置文件需要在配置中指定 age 私钥的位置- 需要在
sops.age.keyFile = "/path/to/keys.txt";指定 age 私钥路径
- 需要在
- 远程主机:用
sops-nix部署服务使用 ssh 私钥生成的 age 私钥- 默认使用 root 的 ssh 私钥,若默认的路径不正确,需要在
sops.age.keyFile = "/path/to/keys.txt";指定 age 私钥路径
- 默认使用 root 的 ssh 私钥,若默认的路径不正确,需要在
若用户主机的 age 公钥定义为 &user age123456789,之后编辑与部署都指向相应的私钥路径
sops编辑默认路径在~/.config/sops/age/keys.txtsops.age.keyFile = "/home/user/.config/sops/age/keys.txt";
若远程主机的 age 公钥由 ssh 公钥生成,定义在 &server age987654321,之后编辑与部署都指向相应的私钥路径
sops编辑默认路径在~/.config/sops/age/keys.txt- 若 age 私钥是由 ssh 私钥生成的,
sops-nix会自动生成对应 age 私钥解密。
这样同一用户组下都能编辑与部署 sops.yaml