summaryrefslogtreecommitdiff
path: root/config/git/yubikey-git/hooks
diff options
context:
space:
mode:
Diffstat (limited to 'config/git/yubikey-git/hooks')
-rwxr-xr-xconfig/git/yubikey-git/hooks/pre-push26
1 files changed, 26 insertions, 0 deletions
diff --git a/config/git/yubikey-git/hooks/pre-push b/config/git/yubikey-git/hooks/pre-push
new file mode 100755
index 0000000..de37a96
--- /dev/null
+++ b/config/git/yubikey-git/hooks/pre-push
@@ -0,0 +1,26 @@
+#!/bin/bash
+# Stops a push to master whose newest commit is not signed by one of the
+# YubiKeys, before it leaves this machine: the git host checks the same and
+# refuses it, as does every host that fetches it. `git sz-push` signs it, with
+# one touch, and pushes. Not a security boundary, only a shorter way to the
+# same answer; it also catches an agent's plain `git push`.
+set -uo pipefail
+
+here="$(cd "$(dirname "$0")" && pwd -P)"
+pubs="${here}/../../../ssh/yubikey-git"
+signers="$(mktemp)"
+trap 'rm -f "${signers}"' EXIT
+for p in "${pubs}"/*.pub; do
+ read -r t k c < "${p}" && echo "${c} namespaces=\"git\" ${t} ${k}"
+done > "${signers}"
+
+status=0
+while read -r _local_ref local_sha remote_ref _remote_sha; do
+ [[ "${remote_ref}" == refs/heads/master && ! "${local_sha}" =~ ^0+$ ]] || continue
+ if [[ "$(git -c gpg.format=ssh -c gpg.ssh.allowedSignersFile="${signers}" log -1 --format=%G? "${local_sha}" 2>/dev/null)" != G ]]; then
+ >&2 echo "pre-push: master's newest commit, ${local_sha:0:12}, is not signed by a YubiKey, so the git host would refuse it."
+ >&2 echo "pre-push: push it with: git sz-push"
+ status=1
+ fi
+done
+exit "${status}"