Skip to content

Git crypt

git-crypt

在 git-ops 中使用 git-crypt 保护敏感数据 | Zhu.Yang

git-crypt 对 git 仓库中的数据进行透明的加解密,选择保护的文件在提交时会加密,在检出时会解密。

生成一个密钥使得当前的 git 库可以使用 git-crypt

git-crypt init

检查文件加密状态

git-crypt status

创建 .gitattributes 来指定加密的文件

<file-name-to-encrypt> filter=git-crypt diff=git-crypt

确保将敏感文件移出存储库并提交更改,然后添加 .gitattributes 文件,然后将敏感文件移回存储库。

SOPS

Comparison of secret managing schemes - NixOS Wiki > GitHub - Mic92/sops-nix: Atomic secret provisioning for NixOS based on sops

对一系列的配置文件进行加密的方法。

生成密钥

Upgrade Your SSH Key to Ed25519. If you’re a DevOps engineer or a web… | by Risan Bagja | Code | Medium

生成 ed25519 私钥

ssh-keygen -o -a 100 -t ed25519 -f ~/.ssh/id_ed25519 -C "comment"

生成 age 私钥

mkdir -p ~/.config/sops/age
age-keygen -o ~/.config/sops/age/keys.txt

由 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 私钥找到相应的公钥

age-keygen -y ~/.config/sops/age/keys.txt

将 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 中的正则规则。

nix-shell -p sops --run "sops secrets/example.yaml"

例如:

# 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

保存后的文件就是加密后的文件

之后使用配置文件(声明)

{
  sops.secrets.example-key = {};
  sops.secrets."myservice/my_subdir/my_secret" = {};
}

解密

再次打开加密后的文件时,会尝试用密钥进行解密。 本地配置的 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 中指定的公钥对应
  • 用户:用 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 私钥路径

若用户主机的 age 公钥定义为 &user age123456789,之后编辑与部署都指向相应的私钥路径

  • sops 编辑默认路径在 ~/.config/sops/age/keys.txt
  • sops.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