summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorIgor Soarez <igor@soarez.org>2026-11-17 10:27:22 +0000
committerIgor Soarez <igor@soarez.org>2026-11-17 10:27:22 +0000
commit810877403e006bf8d145ed2cf236f0b3f1afc8d4 (patch)
tree9889cee96ba43c1cf845eceb31cbdd2100a390d5 /bin
parent75aa62d5920f4adf717ae780d7032c105cceb899 (diff)
git sz-push: sign master's newest commit with a YubiKey, and push it
One touch to sign, one to push, for a machine that pushes through git.soarez.net with a YubiKey. yubikey-git enable also turns on a pre-push guard for dot's clone, which stops a push to master that no YubiKey signed.
Diffstat (limited to 'bin')
-rwxr-xr-xbin/git-sz-push81
-rwxr-xr-xbin/yubikey-git32
2 files changed, 113 insertions, 0 deletions
diff --git a/bin/git-sz-push b/bin/git-sz-push
new file mode 100755
index 0000000..e5a9c62
--- /dev/null
+++ b/bin/git-sz-push
@@ -0,0 +1,81 @@
+#!/bin/bash
+# git sz-push [<remote>] -- push this branch's commit to <remote>'s master
+# (origin), signed with a YubiKey: one touch to sign, one to push.
+#
+# The git host takes a new master only at a commit an author signed, and every
+# host that fetches it checks the same; the commits under it need no signature,
+# since the newest one's covers them. So this signs the newest commit and
+# pushes, for a machine that has only this repo and the YubiKey git keys
+# (bin/yubikey-git). Commit as usual; push master with this.
+#
+# Signing amends HEAD with `--only`: nothing staged is folded in, hooks run as
+# usual, and the tree, message, author and parents stay; the hash and committer
+# date change. Fetches nothing: run `git fetch` first if master may have moved.
+#
+# SZ_PUSH_KEYS (signing key files) and SZ_PUSH_SIGNERS (public keys) are for
+# tests. Runs under macOS's /bin/bash 3.2.
+set -euo pipefail
+
+die() { >&2 echo "git sz-push: $*"; exit 1; }
+
+# This script's real directory, as bin/yubikey-git finds its own.
+resolve_repo() {
+ local src="$0" dir
+ while [[ -L "${src}" ]]; do
+ dir="$(cd "$(dirname "${src}")" && pwd)"
+ src="$(readlink "${src}")"
+ [[ "${src}" == /* ]] || src="${dir}/${src}"
+ done
+ cd "$(dirname "${src}")/.." && pwd
+}
+
+remote="${1:-origin}"
+pubs="${SZ_PUSH_SIGNERS:-$(resolve_repo)/config/ssh/yubikey-git/*.pub}"
+keys="${SZ_PUSH_KEYS:-${HOME}/.ssh/id_ed25519_sk_git_main_nfc ${HOME}/.ssh/id_ed25519_sk_git_backup_nfc ${HOME}/.ssh/id_ed25519_sk_git}"
+
+gd="$(git rev-parse --git-dir 2>/dev/null)" || die "not in a git repository"
+for f in rebase-merge rebase-apply MERGE_HEAD CHERRY_PICK_HEAD REVERT_HEAD; do
+ [[ ! -e "${gd}/${f}" ]] || die "a rebase, merge, cherry-pick or revert is in progress; finish it first"
+done
+branch="$(git symbolic-ref -q --short HEAD)" || die "HEAD is detached; check out a branch first"
+up="$(git rev-parse -q --verify "refs/remotes/${remote}/master")" || die "no ${remote}/master here: git fetch ${remote} first"
+
+if git merge-base --is-ancestor HEAD "${up}"; then
+ echo "git sz-push: nothing to push, ${remote}/master already has HEAD"
+ exit 0
+fi
+git merge-base --is-ancestor "${up}" HEAD || die "HEAD does not build on ${remote}/master: rebase onto it first"
+
+echo "git sz-push: to ${remote} master, $(git rev-list --count "${up}..HEAD") commit(s):"
+git --no-pager log --oneline "${up}..HEAD" | sed 's/^/ /'
+others="$(git branch --contains HEAD --format='%(refname:short)' | grep -vxF "${branch}" || true)"
+[[ -z "${others}" ]] || echo "git sz-push: also on ${others//$'\n'/, }, which will keep the unsigned commit"
+
+signers="$(mktemp)"
+trap 'rm -f "${signers}"' EXIT
+for p in ${pubs}; do
+ read -r t k c < "${p}" && echo "${c} namespaces=\"git\" ${t} ${k}"
+done > "${signers}"
+[[ -s "${signers}" ]] || die "no YubiKey public keys found (${pubs})"
+signed() {
+ [[ "$(git -c gpg.format=ssh -c gpg.ssh.allowedSignersFile="${signers}" log -1 --format=%G? HEAD 2>/dev/null)" == G ]]
+}
+
+if signed; then
+ echo "git sz-push: HEAD is already signed by a YubiKey"
+else
+ echo "git sz-push: touch your YubiKey to sign HEAD"
+ ok=""
+ for k in ${keys}; do
+ [[ -f "${k}" ]] || continue
+ if git -c gpg.format=ssh -c user.signingkey="${k}" commit -q --amend --only --no-edit --allow-empty -S; then
+ ok=1
+ break
+ fi
+ done
+ [[ -n "${ok}" ]] || die "no YubiKey signed (is one plugged in?); nothing was pushed"
+ signed || die "HEAD is signed, but not by a YubiKey this repo knows; nothing was pushed"
+fi
+
+echo "git sz-push: touch it again to push"
+git push "${remote}" HEAD:master
diff --git a/bin/yubikey-git b/bin/yubikey-git
index 6e46c1f..5653acb 100755
--- a/bin/yubikey-git
+++ b/bin/yubikey-git
@@ -26,6 +26,12 @@
# ~/.config/ssh/enabled.d/yubikey-git.conf
# a link to its config, which is what turns it
# on: config/ssh/entry includes enabled.d/*.conf
+# ~/.config/git/yubikey-git a link to config/git/yubikey-git: the
+# pre-push guard for dot's clone
+# ~/.config/git/config-yubikey-git
+# a link to its config, which config/git/config
+# includes: it turns the guard on for a clone
+# of git@git.soarez.net:sz/dot.git only
#
# On macOS it also needs Homebrew's OpenSSH, and installs it if Homebrew is
# there: Apple's ssh cannot talk to a security key. git runs whichever ssh is
@@ -42,6 +48,11 @@ keys=(id_ed25519_sk_git_main_nfc id_ed25519_sk_git_backup_nfc id_ed25519_sk_git)
cfg="${HOME}/.config/ssh"
dir_link="${cfg}/${name}"
conf_link="${cfg}/enabled.d/${name}.conf"
+# git's half: the pre-push guard for dot's clone (config/git/yubikey-git), and
+# `git sz-push`, which signs master's newest commit with the YubiKey.
+gcfg="${HOME}/.config/git"
+git_dir_link="${gcfg}/${name}"
+git_conf_link="${gcfg}/config-${name}"
die() { >&2 echo "${name}: $*"; exit 1; }
@@ -58,6 +69,7 @@ resolve_repo() {
}
src_dir="$(resolve_repo)/config/ssh/${name}"
[[ -f "${src_dir}/config" ]] || die "no ${src_dir}/config -- is this the dotfiles checkout?"
+git_src_dir="$(resolve_repo)/config/git/${name}"
# A link we made points at exactly this target; anything else there is not ours.
ours() { [[ -L "$1" && "$(readlink "$1")" == "$2" ]]; }
@@ -106,6 +118,22 @@ enable() {
fi
ln -sfn "${dir_link}/config" "${conf_link}"
+ # Where a private layer is installed, its own hooks check dot's clones, and
+ # this one's hooksPath would replace them.
+ if [[ -e "${gcfg}/config-fleet" ]]; then
+ echo "${name}: a private layer's git config is here (${gcfg}/config-fleet); its hooks check dot, so no git guard"
+ else
+ mkdir -p "${gcfg}"
+ if [[ -e "${git_dir_link}" || -L "${git_dir_link}" ]] && ! ours "${git_dir_link}" "${git_src_dir}"; then
+ die "${git_dir_link} exists and is not this repo's; move it aside first"
+ fi
+ ln -sfn "${git_src_dir}" "${git_dir_link}"
+ if [[ -e "${git_conf_link}" || -L "${git_conf_link}" ]] && ! ours "${git_conf_link}" "${git_dir_link}/config"; then
+ die "${git_conf_link} exists and is not this repo's; move it aside first"
+ fi
+ ln -sfn "${git_dir_link}/config" "${git_conf_link}"
+ fi
+
# Fail now, not at the first push: a config ssh cannot parse breaks every ssh.
if ! ssh -G git.soarez.net 2>&1 | grep -qix "identityfile ~/.ssh/id_ed25519_sk_git"; then
ssh -G git.soarez.net > /dev/null || true
@@ -115,12 +143,15 @@ enable() {
echo "${name}: enabled. With one of the YubiKeys plugged in:"
echo " git clone git@git.soarez.net:sz/<name>.git"
+ echo "and, in dot's clone, push master with: git sz-push"
}
disable() {
local k key
ours "${conf_link}" "${dir_link}/config" && rm "${conf_link}"
ours "${dir_link}" "${src_dir}" && rm "${dir_link}"
+ ours "${git_conf_link}" "${git_dir_link}/config" && rm "${git_conf_link}"
+ ours "${git_dir_link}" "${git_src_dir}" && rm "${git_dir_link}"
for k in "${keys[@]}"; do
key="${HOME}/.ssh/${k}"
if [[ -f "${key}" ]] && cmp -s "${key}" "${src_dir}/${k}"; then
@@ -134,6 +165,7 @@ status() {
local on=y k key
if ours "${conf_link}" "${dir_link}/config"; then echo "ssh config: enabled (${conf_link})"; else echo "ssh config: not enabled"; on=""; fi
if ours "${dir_link}" "${src_dir}"; then echo "config dir: ${dir_link}"; else echo "config dir: missing"; on=""; fi
+ if ours "${git_conf_link}" "${git_dir_link}/config" && ours "${git_dir_link}" "${git_src_dir}"; then echo "git guard: ${git_conf_link}"; else echo "git guard: not enabled"; on=""; fi
for k in "${keys[@]}"; do
key="${HOME}/.ssh/${k}"
if [[ -f "${key}" ]] && cmp -s "${key}" "${src_dir}/${k}"; then echo "key handle: ${key}"; else echo "key handle: ${k} missing"; on=""; fi