上 git-secret:在 Git 存储库中加密和存储密钥( 三 )

git-secret 用法初始化 git-secretgit-secret 通过在 Git 存储库的根目录中运行的以下命令进行初始化 。
git secret init$ git secret initgit-secret: init created: '/var/www/app/.gitsecret/'只需这样操作一次,稍后会把文件夹提交到 Git,将包含以下文件:
$ git status | grep ".gitsecret"new file: .gitsecret/keys/pubring.kbxnew file: .gitsecret/keys/pubring.kbx~new file: .gitsecret/keys/trustdb.gpgnew file: .gitsecret/paths/mapping.cfgpubring.kbx~文件(带有波浪号~)只是一个临时文件,可以安全地被 git 忽略 。
git-secret Directory 和 gpg-agent Socket要 git-secret 在主机系统和 Docker 之间共享的目录中使用,还需要运行以下命令:
tee .gitsecret/keys/S.gpg-agent <<EOF%Assuan%socket=/tmp/S.gpg-agentEOFtee .gitsecret/keys/S.gpg-agent.ssh <<EOF%Assuan%socket=/tmp/S.gpg-agent.sshEOFtee .gitsecret/keys/gpg-agent.conf <<EOFextra-socket /tmp/S.gpg-agent.extrabrowser-socket /tmp/S.gpg-agent.browserEOF这一步很必要,因为 git-secret 在主机系统和 Docker 容器之间共享代码库的设置中使用时存在问题,具体如下:

  • gpg 使用gpg-agent 来执行其任务,这两个工具通过在 pgp-agent--home-directory 中创建的套接字进行通信 。
  • 代理通过 git-secret 使用的 gpg 命令隐式启动,使用 .gitsecret/keys 目录作为 --home-directory
  • 由于 --home-directory 的位置与主机系统共享,因此套接字创建将失败 。
对应的错误信息是:
gpg: can't connect to the agent: IPC connect call failedgpg-agent: error binding socket to '/var/www/app/.gitsecret/keys/S.gpg-agent': I/O error解决此问题,可以通过将其他 gpg 配置文件放在 .gitsecret/keys 目录中,将 gpg 配置为对套接字使用不同的位置:
S.gpg-agent
%Assuan%socket=/tmp/S.gpg-agents.gpg-agent.ssh
%Assuan%socket=/tmp/S.gpg-agentgpg-agent.conf
extra-socket /tmp/S.gpg-agent.extrabrowser-socket /tmp/S.gpg-agent.browser添加、列出和删除用户要添加新用户,必须首先导入其公用 gpg 密钥 。然后运行:
email="pascal.landau@example.com"git secret tell "$email"在这种情况下,用户pascal.landau@example.com 现在将能够解密这些密钥 。要显示用户请运行:
git secret whoknows$ git secret whoknowspascal.landau@example.com要删除用户,请运行:
email="pascal@example.com"git secret killperson "$email"这时此命令在 git-secret >= 0.5.0 中已重命名为 removeperson
$ git secret killperson pascal.landau@example.comgit-secret: removed keys.git-secret: now [pascal.landau@example.com] do not have an access to the repository.git-secret: make sure to hide the existing secrets again.用户 pascal@example.com 将无法再解密这些密钥 。
注意删除用户后需要重新加密机密,并轮换加密的密钥 。
添加、列出和删除文件以进行加密运行 git secret add [filenames...] 来为文件加密:
git secret add .env如果 .env 没有被添加到 .gitignoregit-secret 将显示警告并自动添加 。
git-secret: these files are not in .gitignore: .envgit-secret: auto adding them to .envgit-secret: 1 item(s) added.如已添加,则添加文件时不会发出警告 。
$ git secret add .envgit-secret: 1 item(s) added.只需要添加一次文件 。然后将它们存在

经验总结扩展阅读