GPG ? Using SSH Signing Your Commit !

Share This Post

前言

為什麼會有這個主題呢?因為最近我又双叒叕重灌我的筆電,偏偏在 GPG, SSH 上又特別難處理,在這次重灌後,偏偏忘記備份我之前的 SSH Key … 導致我的 GitHub, GitLab, 遠端 SSH 連線的金鑰全部掰掰,所以求助於同事,這才發現,公司使用的 1password 其實有支援 SSH Key 存取,更甚至延伸到開發者的項目,自動幫你全域設定好 Keyless 的配置,更延伸下去,發現 1password 的配置其實已經轉換成用 SSH 來自動簽署你的 commit,不禁讓我好奇,HOW ????

Untitled

PGP ? GPG ?

PGP(英語:Pretty Good Privacy,直譯:優良保密協定)是一套用於訊息加密、驗證的應用程式。 PGP的主要開發者是菲爾·齊默曼。齊默曼於1991年將PGP在網際網路上免費發布。PGP本身是商業應用程式;開源並具有同類功能的工具名為GnuPG(GPG)。PGP及其同類產品均遵守OpenPGP資料加解密標準(RFC 4880)。 PGP加密由一系列雜湊資料壓縮對稱金鑰加密,以及公鑰加密的演算法組合而成。每個步驟均支援幾種演算法,使用者可以選擇一個使用。每個公鑰均繫結一個使用者名稱和/或者E-mail位址。該系統的最初版本通常稱為可信網或X.509系統;X.509系統使用的是基於數位憑證認證機構的分層方案,該方案後來被加入到PGP的實現中。當前的PGP加密版本通過一個自動金鑰管理伺服器來進行金鑰的可靠存放。

PGP – 维基百科,自由的百科全书

Signing With SSH ?

在 November 15, 2021 發佈的 Highlights from Git 2.34 中提到了:

You might be aware that Git allows you to sign your work by attaching your PGP signature to certain objects. For example, the Git project itself publishes tags signed by the maintainer in order to verify that each release comes from someone trustworthy.

But the experience of using GPG and maintaining keys can be somewhat cumbersome. One alternative is to use a new feature of OpenSSH (released back in OpenSSH 8.0) that allows using the SSH key you likely already have as a signing key. — Taylor Blau

意思是, git 的開發人員們自己也有發現,對於簽署 commit 這件事情,使用 GPG (or PGP) 其實對開發人員來說,有點複雜且笨重,為了解決這些問題,他們開發並使用了 SSH 的加密金鑰來為你的 commit 進行簽署,而不用另外核發一隻 GPG 來做簽署,達成同一把金鑰,可以同時做身份驗證及簽署 commit,畢竟他們本來就是同性質的內容。

Git – git-config Documentation

Highlights from Git 2.34 | The GitHub Blog

How ?

  1. 首先,你必須為 Git v2.34 以上的版本,請確認版本是否正確喔!
  2. ~/.gitconfig 中配置使用全域簽署的 SSH Key 是誰?是哪一把? [user] signingkey = ssh-ed25519 AAAAJAFOIWEFJAIWOEJFIEWJFAIEWJFIAWEJFAIOWEJFKMCEICMEIJRIAOECMEWIKRJEWI

[gpg]

format = ssh

[commit]

gpgsign = true

接著打開 ssh-agent 需要運行,Mac OS, Linux 請執行指令 eval "$(ssh-agent -s)"

Generating a new SSH key and adding it to the ssh-agent – GitHub Docs

  1. Windows 參考 Working with SSH key passphrases – GitHub Docs
  2. Mac OS, Linux 參考 Working with SSH key passphrases – GitHub Docs

ssh-agent 要添加上你的金鑰,請執行ssh-add ~/.ssh/id_ed25519 來添加

請執行 ssh-add -L確認是否有成功添加公鑰

接著試著透過指令提交你的 commit 試試看有沒有成功

git commit -S -m 'Create a signed commit'

如果你想要未來每一筆 commit 都自動簽名,不用另外加上面那句的 -S,可以使用

git config [--global] commit.gpgsign true
git config [--global] tag.gpgsign true

驗證是否成功

git log --show-signature
Untitled

QQ 結果 No signature ~ 沒關係我們接著看下面的步驟

如果沒有成功,接著嘗試在 ~/.config/git/allowed_signers 中配置 allowed signers,這邊會再將你的 email 以及 ssh key 作配對,讓 git 簽署時可以對照

# ~/.config/git/allowed_signers
mail@dbrgn.ch ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIE5bRSsXZ5HnUkMMEWS5/jxEQaqbLb+i6xjMyC6qiXsb danilo@jabba

接著告訴 git 你的 signer 檔案在哪

git config [--global] gpg.ssh.allowedSignersFile "$HOME/.config/git/allowed_signers"

在嘗試一下上面的驗證,這時候應該會看到成功簽署的說明

Untitled

最後你的 gitconfig 應該會長這樣

[user]
	name = Tilda
	email = tilda.lu@trunk-studio.com
	signingkey = ssh-ed25519 WJFIOEJCMCVKLEGIMCKLEJHTIEWKOCKOEPKF

[core]

excludesfile = /Users/XXX/.gitignore_global

[gpg]

format = ssh

[gpg “ssh”]

program = “/Applications/1Password.app/Contents/MacOS/op-ssh-sign” allowedSignersFile = /Users/XXX/.ssh/allowed_signers

[commit]

gpgsign = true

~/.ssh/allowed_signers

tilda.lu@trunk-studio.com ssh-ed25519 WJFIOEJCMCVKLEGIMCKLEJHTIEWKOCKOEPKF

Sign Git commits with SSH | 1Password Developer Documentation

Signing Git Commits with SSH Keys

GitHub 上的配置

SSH commit signature verification

You can use SSH to sign commits with an SSH key that you generate yourself. For more information, see the Git reference documentation for user.Signingkey. If you already use an SSH key to authenticate with GitHub, you can also upload that same key again for use as a signing key. There’s no limit on the number of signing keys you can add to your account.

GitHub uses ssh_data, an open source Ruby library, to confirm that your locally signed commits and tags are cryptographically verifiable against a public key you have added to your account on GitHub.com.

Note: SSH signature verification is available in Git 2.34 or later. To update your version of Git, see the Gitwebsite.

To sign commits using SSH and have those commits verified on GitHub, follow these steps:

  1. Check for existing SSH keys
  2. Generate a new SSH key
  3. Add a SSH signing key to your GitHub account
  4. Tell Git about your signing key
  5. Sign commits
  6. Sign tags

前面的增加 SSH Key 應該大家都會了,這邊就不贅述,集中介紹在如何將 SSH Key Signing Commit 提交給 GitHub 的部分

  1. 先複製你的 SSH 公鑰 pbcopy < ~/.ssh/id_ed25519.pub
  2. 在設定中打開 Access >  SSH and GPG keys Untitled Untitled
  3. Title 可以取一個你好記的名字,接著貼上公鑰 Untitled
  4. 這邊 Key Type 他好像選 Auth 就會自動包含 Signing 功能,如果你要個別設定 Signing Key 及 Auth Key 不同,也可以在這裡選擇各自鑰匙的用途
  5. 完成配置,你可以到 commit 中看是否有 verify Untitled

About commit signature verification – GitHub Docs

那 GitLab ?

目前似乎正在努力中,但可以確認的是,我在 GitHub 中有成功簽署的內容,在 GitLab 中顯示是沒有 Verified 的狀態XD

讓我們期待 GitLab 的努力!!!

SSH key management enhancements to support SSH signed commits (#375258) · Issues · GitLab.org / GitLab · GitLab

結語

身為一個前陣子在搞定公司帳號以及個人帳號,對於 GPG 配置感到無力、複雜的人,看到這個發明真是覺得這是世界的福音!

GPG (or PGP) 的配置雖然讓 commit 可以明確知道這是否被驗證過,但配置上實在是太過繁雜,導致很多開發人員在這塊踩過各種不同的雷,現在有了 SSH 可以一次搞定這個驗證,更能省下我們配置的時間,多多拿來開發。

疑難雜症

  1. 我的 VS Code Git Graph 不能正常顯示!
    1. 當你的 Git 版本在 2.34.0 以上(才有支援 SSH Signing Commit)
    2. 設置  gpg.format = ssh
    3. 得到 error: unsupported value for gpg.format: ssh fatal: bad config variable 'gpg.format' in file '.git/config' at line 9 的錯誤 Untitled
    解決方法
    1. 在 VS Code 的 setting.json 中加上 git 的所在位置: 注意!請選擇符合你設定的位置做添加(使用者、工作區),如果有成功套用文字會顯示正常顏色,沒有套用會顯示文字灰色! { "git.path": "<path to your git>", }
    2. 如果不知道 git 安裝在哪,請使用指令查找: which git
    3. 如果是用 Homebrew 安裝,他的預設所在會在: { "git.path": "/opt/homebrew/bin/git", }
    4. 設置完成後,請重開 VS Code 或點 Retry 看看是否成功套用
    Missing support for git ssh signing · Issue #638 · mhutchie/vscode-git-graph

訂閱研究文章

Get updates and learn from the best

More To Explore

Scroll to Top

hurry up !

軟體工程師培訓

限時免費報名中

藉由與「真實世界軟體專案」相同的技術、工具與開發流程,化簡成與商業機密無關、門檻較低更容易上手的「模擬專案」,讓你有機會在職場前輩的陪伴下,完成真槍實彈的練習,動手解決真實的問題,快速累積個人的經驗與作品,而不只是「學習技術」而已。