Sign and notarize the macOS builds with Developer ID

With the repository variable MACOS_SIGN_IDENTITY set, the launcher's Horizon.app and .dmg and the
game's three programs are signed with the hardened runtime and a timestamp, notarized and (app,
dmg) stapled, from a keychain of the Mac mini's own that keychain.sh unlocks with its password from
1Password. Unset, everything is signed ad hoc as before. The launcher checks an updated bundle's
signature before and after swapping it in, and puts the old one back when it doesn't hold.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-15 00:32:54 +02:00
parent 4d2e05dda7
commit 3ffbb1cb9f
7 changed files with 314 additions and 25 deletions
+64 -14
View File
@@ -14,11 +14,71 @@
swapped in by renaming, then restarted); a bare `horizon-launcher` binary, as launchers before
the app shipped, finds the binary inside the zip and replaces only itself.
## Signing and Gatekeeper
## Signing and notarization
The app is signed **ad hoc** (`codesign --sign -`): no Apple account is needed, the bundle is
consistent and runs on Apple Silicon, but Gatekeeper doesn't know the developer. The first time
a player opens it after downloading:
Releases are signed with Ludovic's **Developer ID** ("Developer ID Application: Ludovic Marie
(YYJU63HSD4)"), with the hardened runtime and a secure timestamp, then **notarized** by Apple and
**stapled**, so Gatekeeper opens them on any Mac without a warning, offline too:
- the launcher (`package.sh`): Horizon.app is signed, notarized and stapled first, so the zip the
launcher updates itself from and the .dmg both hold it stapled; then the .dmg is signed,
notarized and stapled. The script ends with what Gatekeeper says of each, which must read
`accepted` and `source=Notarized Developer ID`.
- the game (`.github/workflows/release.yml`): `horizon-client`, `horizon-ctl` and
`horizon-prepare` are signed right after they are built (so the checks and the 1500-frame smoke
run use them as players will), and the zip is notarized. Programs outside a bundle can't hold a
ticket: Gatekeeper asks Apple online the first time each one runs.
No entitlements are needed. The launcher checks an update's signature before swapping the new
Horizon.app in, and again where it lands; when it doesn't hold, the old app is put back.
The switch is the repository variable `MACOS_SIGN_IDENTITY`: set to the identity's name, the
builds sign and notarize; empty (as in forks), they are signed **ad hoc** as before.
### The signing keychain on the Mac mini
The release runner (`ludovics-mac-mini-release`, label `horizon-release`, `MAC_RUNNER`) is a
LaunchAgent whose jobs run in their own security session, where the login keychain is locked.
So signing uses a keychain of its own, `~/Library/Keychains/horizon-signing.keychain-db`,
holding only the Developer ID identity, Apple's "Developer ID Certification Authority" and the
notarytool profile `horizon-notary` (the App Store Connect team key "App Store Connect API -
GitHub CI (DISPO)"). Its password is the item "Horizon signing keychain" in the Agents vault.
`keychain.sh unlock` (each job's first macOS step) reads it with the fleet's 1Password service
account, unlocks the keychain, checks the identity and the profile are there and hands both to
the later steps; `keychain.sh lock` ends the job. Nothing secret is in the repository or on
GitHub, and nothing is printed.
Setting it up again (a new Mac, a renewed certificate):
1. `packaging/macos/keychain.sh create`: the keychain, locked after two hours idle and on sleep.
2. In Keychain Access on the Mac (the login keychain, *My Certificates*), right-click
"Developer ID Application: Ludovic Marie (YYJU63HSD4)", *Export…*, save it as a .p12 with a
password; then in the Mac's Terminal `packaging/macos/keychain.sh import FILE.p12`. It asks for
that password, imports the identity and its intermediate, removes any other identity, lets
`codesign` use the key without a prompt and deletes the .p12. (The login keychain also holds
other people's Apple Development and Distribution identities: never those.)
3. `packaging/macos/keychain.sh notary`: the notarytool profile, from the App Store Connect key
(written to a private temporary file and deleted).
4. The repository variable `MACOS_SIGN_IDENTITY`.
The certificate and its intermediate expire on 1 February 2027: renew the certificate in the
Apple Developer account before then and repeat step 2.
Signing by hand, as the workflow does:
```sh
export MACOS_SIGN_IDENTITY="Developer ID Application: Ludovic Marie (YYJU63HSD4)"
export MACOS_SIGN_KEYCHAIN=~/Library/Keychains/horizon-signing.keychain-db MACOS_NOTARY_PROFILE=horizon-notary
packaging/macos/keychain.sh unlock
packaging/macos/package.sh target/release/horizon-launcher 0.0.0 out
packaging/macos/keychain.sh lock
```
### Gatekeeper with an ad hoc build
An ad hoc build (a fork's, or `package.sh` without `MACOS_SIGN_IDENTITY`) is consistent and runs
on Apple Silicon, but Gatekeeper doesn't know the developer. The first time it is opened after
downloading:
- macOS 14 and earlier: right-click (or Control-click) Horizon in Applications, choose **Open**,
then **Open** in the dialog.
@@ -27,13 +87,3 @@ a player opens it after downloading:
After that it opens normally, and its own updates don't ask again (they aren't downloaded by a
browser, so they carry no quarantine flag).
To sign and notarize for real (Ludovic's Apple Developer account): import the "Developer ID
Application" certificate into the runner's keychain, set `MACOS_SIGN_IDENTITY` to its name for
`package.sh` (it then signs with the hardened runtime and a timestamp), and after it, notarize and
staple the disk image:
```sh
xcrun notarytool submit dist/horizon-launcher-macos-arm64.dmg --keychain-profile horizon --wait
xcrun stapler staple dist/horizon-launcher-macos-arm64.dmg
```
+99
View File
@@ -0,0 +1,99 @@
#!/usr/bin/env bash
# The keychain the macOS builds are signed and notarized from: a keychain of its own (not the
# login keychain, which a runner's job can't unlock), holding only the "Developer ID
# Application" identity, its intermediate certificate and the notarytool profile. Its password
# is in 1Password (the Agents vault, read with the fleet's service account), so a job unlocks
# it without anyone typing anything. See packaging/macos/README.md.
#
# packaging/macos/keychain.sh unlock # a job's first macOS step: unlock, check, export the env
# packaging/macos/keychain.sh lock # its last
# packaging/macos/keychain.sh create # once: the keychain, with the password from 1Password
# packaging/macos/keychain.sh import P12 # once, from the Mac's own Terminal: the identity
# packaging/macos/keychain.sh notary # once: the notarytool profile from the App Store Connect key
#
# MACOS_SIGN_IDENTITY names the identity (empty: nothing is signed, see package.sh);
# MACOS_SIGN_KEYCHAIN the keychain; MACOS_NOTARY_PROFILE the profile in it; MACOS_KEYCHAIN_PASSWORD,
# when set, is the keychain's password instead of 1Password's.
set -euo pipefail
kc=${MACOS_SIGN_KEYCHAIN:-$HOME/Library/Keychains/horizon-signing.keychain-db}
profile=${MACOS_NOTARY_PROFILE:-horizon-notary}
identity=${MACOS_SIGN_IDENTITY:-Developer ID Application: Ludovic Marie (YYJU63HSD4)}
# The 1Password CLI, signed in as the fleet's agents (`op` elsewhere), and the items it reads.
op=${OP:-$HOME/fleet/op}
[[ -x "$op" ]] || op=op
password_ref=${MACOS_KEYCHAIN_PASSWORD_REF:-op://Agents/Horizon signing keychain/password}
# "App Store Connect API - GitHub CI (DISPO)": a team key; by id, as the name's parentheses
# can't be in a secret reference.
notary_item=${MACOS_NOTARY_ITEM:-wwweyy32dwt6oylm6ypcfivsxe}
# The keychain's password: $MACOS_KEYCHAIN_PASSWORD (a CI secret elsewhere), or from 1Password.
password() {
if [[ -n "${MACOS_KEYCHAIN_PASSWORD:-}" ]]; then
printf '%s' "$MACOS_KEYCHAIN_PASSWORD"
else
"$op" read "$password_ref" < /dev/null
fi
}
case "${1:-}" in
unlock)
security unlock-keychain -p "$(password)" "$kc"
security find-identity -v -p codesigning "$kc" | grep -F "\"$identity\"" > /dev/null ||
{ echo "::error::$kc has no valid \"$identity\" (packaging/macos/keychain.sh import)"; exit 1; }
xcrun notarytool history --keychain-profile "$profile" --keychain "$kc" > /dev/null ||
{ echo "::error::no notarytool profile $profile in $kc (packaging/macos/keychain.sh notary)"; exit 1; }
echo "Unlocked $kc: \"$identity\" and the notarytool profile $profile"
if [[ -n "${GITHUB_ENV:-}" ]]; then
{ echo "MACOS_SIGN_KEYCHAIN=$kc"; echo "MACOS_NOTARY_PROFILE=$profile"; } >> "$GITHUB_ENV"
fi
;;
lock)
security lock-keychain "$kc"
;;
create)
[[ -e "$kc" ]] && { echo "$kc already exists"; exit 1; }
security create-keychain -p "$(password)" "$kc"
# Locks itself after two hours idle and when the Mac sleeps.
security set-keychain-settings -t 7200 -l "$kc"
echo "Created $kc"
;;
import)
p12=${2:?usage: keychain.sh import IDENTITY.p12}
pass=$(password)
security unlock-keychain -p "$pass" "$kc"
read -rsp "The password you gave the .p12 when exporting it: " p12pass && echo
security import "$p12" -k "$kc" -f pkcs12 -P "$p12pass" -T /usr/bin/codesign
unset p12pass
# The intermediate, so codesign builds the chain without the login keychain.
issuer=$(security find-certificate -c "$identity" -p "$kc" | openssl x509 -noout -issuer |
sed -n 's/.*CN *= *\([^,]*\).*/\1/p')
if [[ -n "$issuer" ]] && ! security find-certificate -c "$issuer" "$kc" > /dev/null 2>&1 &&
security find-certificate -c "$issuer" -p "$HOME/Library/Keychains/login.keychain-db" > "$kc.ca.pem"; then
security import "$kc.ca.pem" -k "$kc"
fi
rm -f "$kc.ca.pem"
# Only the one identity: anything else the .p12 held goes.
security find-identity -v -p codesigning "$kc" | sed -n 's/^ *[0-9]*) \([0-9A-F]*\) "\(.*\)"$/\1 \2/p' |
while read -r hash name; do
[[ "$name" == "$identity" ]] || security delete-identity -Z "$hash" "$kc"
done
# codesign may use the key without asking.
security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k "$pass" "$kc" > /dev/null
rm -f "$p12"
security find-identity -v -p codesigning "$kc"
echo "Imported; $p12 is deleted."
;;
notary)
security unlock-keychain -p "$(password)" "$kc"
key=$(mktemp) && chmod 600 "$key"
trap 'rm -f "$key"' EXIT
"$op" read "op://Agents/$notary_item/private_key_p8" < /dev/null > "$key"
xcrun notarytool store-credentials "$profile" --key "$key" --keychain "$kc" \
--key-id "$("$op" read "op://Agents/$notary_item/key_id" < /dev/null)" \
--issuer "$("$op" read "op://Agents/$notary_item/issuer_id" < /dev/null)"
;;
*)
sed -n '/^set -e/q; 2,$s/^# \{0,1\}//p' "$0"
exit 1
;;
esac
+37
View File
@@ -0,0 +1,37 @@
#!/usr/bin/env bash
# Notarizes signed macOS builds with Apple's notary service and staples the ticket to what can
# hold one: an .app (sent as a zip) and a .dmg are stapled, a .zip of bare programs can't be
# (Gatekeeper asks Apple online the first time one runs).
#
# packaging/macos/notarize.sh FILE...
#
# Uses the notarytool profile $MACOS_NOTARY_PROFILE in the keychain $MACOS_SIGN_KEYCHAIN, which
# `packaging/macos/keychain.sh unlock` unlocks and exports.
set -euo pipefail
profile=${MACOS_NOTARY_PROFILE:?the notarytool profile (packaging/macos/keychain.sh unlock)}
keychain=()
[[ -n "${MACOS_SIGN_KEYCHAIN:-}" ]] && keychain=(--keychain "$MACOS_SIGN_KEYCHAIN")
for file in "$@"; do
file=${file%/}
sent=$file
if [[ "$file" == *.app ]]; then
sent=$(mktemp -d)/$(basename "$file").zip
ditto -c -k --norsrc --noextattr --noqtn --keepParent "$file" "$sent"
fi
echo "Notarizing $(basename "$file")…"
result=$(xcrun notarytool submit "$sent" --keychain-profile "$profile" ${keychain[@]+"${keychain[@]}"} \
--wait --timeout 30m --output-format json)
[[ "$sent" == "$file" ]] || rm -rf "$(dirname "$sent")"
id=$(plutil -extract id raw -o - - <<< "$result")
status=$(plutil -extract status raw -o - - <<< "$result")
echo "$(basename "$file"): $status (submission $id)"
if [[ "$status" != Accepted ]]; then
xcrun notarytool log "$id" --keychain-profile "$profile" ${keychain[@]+"${keychain[@]}"} || true
echo "::error::$(basename "$file") was not notarized: $status"
exit 1
fi
case "$file" in
*.app | *.dmg) xcrun stapler staple "$file" && xcrun stapler validate "$file" ;;
esac
done
+25 -5
View File
@@ -7,9 +7,11 @@
# packaging/macos/package.sh BINARY VERSION OUTDIR
#
# Needs macOS (ditto, codesign, hdiutil) and dmgbuild (pip install dmgbuild==1.6.5; $DMGBUILD
# names it if it isn't on PATH). Signing: ad hoc by default, which needs no Apple account;
# set MACOS_SIGN_IDENTITY to a "Developer ID Application: …" identity in the keychain to sign
# for real (then notarize the .dmg: see packaging/macos/README.md).
# names it if it isn't on PATH). Signing: ad hoc by default, which needs no Apple account. With
# MACOS_SIGN_IDENTITY set to a "Developer ID Application: …" identity (in $MACOS_SIGN_KEYCHAIN
# when set), the app and the .dmg are signed with the hardened runtime and a timestamp, and with
# MACOS_NOTARY_PROFILE too, both are notarized and stapled (keychain.sh unlock sets the two;
# see packaging/macos/README.md).
set -euo pipefail
here=$(cd "$(dirname "$0")" && pwd)
binary=$1
@@ -27,12 +29,19 @@ printf 'APPL????' > "$app/Contents/PkgInfo"
plutil -lint "$app/Contents/Info.plist"
identity=${MACOS_SIGN_IDENTITY:--}
keychain=()
[[ -n "${MACOS_SIGN_KEYCHAIN:-}" ]] && keychain=(--keychain "$MACOS_SIGN_KEYCHAIN")
if [[ "$identity" == "-" ]]; then
codesign --force --sign - "$app"
else
codesign --force --options runtime --timestamp --sign "$identity" "$app"
codesign --force --options runtime --timestamp ${keychain[@]+"${keychain[@]}"} --sign "$identity" "$app"
fi
codesign --verify --strict --verbose=2 "$app"
# Still starts under the hardened runtime.
"$app/Contents/MacOS/horizon-launcher" --version
notarize=$([[ "$identity" != "-" && -n "${MACOS_NOTARY_PROFILE:-}" ]] && echo yes || true)
# The app first, so the zip and the .dmg both carry it stapled.
[[ -z "$notarize" ]] || "$here/notarize.sh" "$app"
rm -f "$out/horizon-launcher-macos-arm64.zip" "$out/horizon-launcher-macos-arm64.dmg"
# No resource forks or extended attributes: they would land as ._ files inside the bundle.
@@ -53,5 +62,16 @@ cat > "$out/dmg.json" <<JSON
JSON
"${DMGBUILD:-dmgbuild}" -s "$out/dmg.json" Horizon "$out/horizon-launcher-macos-arm64.dmg"
rm "$out/dmg.json"
hdiutil verify "$out/horizon-launcher-macos-arm64.dmg" >/dev/null
dmg=$out/horizon-launcher-macos-arm64.dmg
if [[ "$identity" != "-" ]]; then
codesign --force --timestamp ${keychain[@]+"${keychain[@]}"} --sign "$identity" "$dmg"
codesign --verify --strict --verbose=2 "$dmg"
fi
hdiutil verify "$dmg" >/dev/null
if [[ -n "$notarize" ]]; then
"$here/notarize.sh" "$dmg"
# What Gatekeeper says of a download: both must be "source=Notarized Developer ID".
spctl --assess --verbose=2 "$app"
spctl --assess --type open --context context:primary-signature --verbose=2 "$dmg"
fi
echo "Packaged $out/horizon-launcher-macos-arm64.zip and .dmg"