From 5c7ed4239d99ff28b839e544299bd387cf1ac99b Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 22 Nov 2026 23:29:13 -0600 Subject: [PATCH] feat(pm-console): every Android build GitHub makes is signed and released, where the key is GitHub Actions builds the app on every change, but does not hold the signing key, so its build was unsigned and nothing was released: the release waited for someone to put the key into GitHub by hand. The key is on the pm-stack host and stays there, so the signing now happens there, by itself. - android/release-built.sh (new), run every ten minutes by pm-console-android-release.timer: takes the newest unsigned build this repository made on main, signs it on the host (sign.sh, which refuses a result that does not carry the certificate the site lists), releases it (release.sh: the numbered release and the fixed address), and puts it where the console serves the app from. When the newest build is already released it asks GitHub one question and does nothing. The key is read by apksigner there and goes nowhere; what leaves is the signed APK, which is public. - Only this repository's own finished build on main, made by a push or by hand, is ever signed: never a pull request, another branch, a fork or another repository's run, whatever it is called. - A failure (wrong certificate, GitHub not answering, the release not made) leaves the release, the served app and the record as they were, and the next run tries again. The unit shows on the Services screen and in notifications like any other. - The workflow's artifact now carries the version the build was made as, so whoever signs it releases it under that version. - install.sh installs the timer and redeploy.sh watches its units, so the host picks all of this up from the merge with nobody at it. - release.sh can be told how gh is run (fleet gh on the host), and says in the release where the build was signed. - If GitHub's environment is given the key, the workflow signs and releases by itself as before, and the host finds nothing to do. The test (release-built.test.sh, in the scheduled pm-stack tests) runs the real three scripts against stand-ins for gh, fleet and apksigner. It found one fault before this was committed: with plain gh, the wrapper called itself for ever. Seventeen planted faults are each caught. Not checked here: the timer on the host itself, and apksigner's own signing. Both are checked by the release it makes: the APK it publishes is verified against signing-fingerprint.txt with apksigner from here. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01YLT5K1QVuzZjfWGFhXSSNF --- .github/workflows/pm-console-android.yml | 52 ++- .github/workflows/pm-stack-tests.yml | 8 + doc/fleet-schedule-registry.md | 1 + pm-stack/console/README.md | 2 +- pm-stack/console/android/README.md | 45 +- pm-stack/console/android/release-built.sh | 148 +++++++ .../console/android/release-built.test.sh | 385 ++++++++++++++++++ pm-stack/console/android/release.sh | 18 +- pm-stack/console/install.sh | 13 +- pm-stack/console/redeploy.sh | 3 +- pm-stack/console/test/redeploy.test.ts | 2 + .../pm-console-android-release.service | 15 + .../systemd/pm-console-android-release.timer | 12 + 13 files changed, 664 insertions(+), 40 deletions(-) create mode 100755 pm-stack/console/android/release-built.sh create mode 100644 pm-stack/console/android/release-built.test.sh create mode 100644 pm-stack/systemd/pm-console-android-release.service create mode 100644 pm-stack/systemd/pm-console-android-release.timer diff --git a/.github/workflows/pm-console-android.yml b/.github/workflows/pm-console-android.yml index 8c5c7ae2d2..9dae46fe4b 100644 --- a/.github/workflows/pm-console-android.yml +++ b/.github/workflows/pm-console-android.yml @@ -4,29 +4,35 @@ name: pm-console android # so it keeps a push trigger (restricted to main, and to the app's own # files). # -# Builds, signs and releases the PM console's Android app -# (pm-stack/console/android): a Trusted Web Activity over -# https://pm.security.cab, plus what only an installed app has. It runs -# on every push to main that touches the app, and by hand. +# Builds the PM console's Android app (pm-stack/console/android): a +# Trusted Web Activity over https://pm.security.cab, plus what only an +# installed app has. It runs on every push to main that touches the app, +# and by hand. Every build ends up signed and released with nobody +# involved; where the signing happens depends on where the key is. # # The version is the commit's (build.sh): a code that rises with the # commit's time, so each build installs over the one before. # -# Signing needs the app's key, as two secrets of the pm-console-android -# environment: PM_CONSOLE_ANDROID_KEYSTORE_B64 and -# PM_CONSOLE_ANDROID_KEYSTORE_PASSWORD. An environment, not the -# repository, so that it can be limited to runs on main. Putting the key -# there is the owner's to do, by hand; no script or agent holds or moves -# it (pm-stack/console/android/README.md). +# The key is not here (how it starts): the build is unsigned and kept as +# the artifact pm-console-apk-unsigned, with the version it carries. The +# machine that holds the key looks for it every ten minutes +# (pm-console-android-release.timer, android/release-built.sh), signs it +# there, checks it against the certificate the site lists +# (signing-fingerprint.txt) and releases it. The key never leaves that +# machine. # -# With the key: the APK is signed here, checked against the certificate -# the site lists (signing-fingerprint.txt), and released twice with -# nobody involved: as pm-console-android-v, which is never -# changed again, and as pm-console-android, whose address stays the same -# and always holds the newest build. The site hands out the second. +# The key is here: as two secrets of the pm-console-android environment, +# PM_CONSOLE_ANDROID_KEYSTORE_B64 and PM_CONSOLE_ANDROID_KEYSTORE_PASSWORD +# (an environment, not the repository, so that it can be limited to runs +# on main). Then the APK is signed and released by this run, and the +# other machine finds nothing to do. Putting the key here is the +# owner's choice and the owner's to do; no script or agent holds or +# moves it (pm-stack/console/android/README.md). # -# Without the key: the build is unsigned and kept as an artifact, and -# the run says so. Nothing is released. +# Either way it is released twice: as pm-console-android-v, which +# is never changed again, and as pm-console-android, whose address stays +# the same and always holds the newest build. The site hands out the +# second. on: push: branches: [main] @@ -45,7 +51,7 @@ concurrency: jobs: build: - name: "build, sign and release the APK" + name: "build the APK, and sign and release it when the key is here" runs-on: ubuntu-latest timeout-minutes: 25 environment: pm-console-android @@ -69,13 +75,17 @@ jobs: echo "signed=true" >>"$GITHUB_OUTPUT" else echo "signed=false" >>"$GITHUB_OUTPUT" - echo "::notice::Built unsigned: this environment does not hold the signing key, so nothing is released. See pm-stack/console/android/README.md, Giving GitHub the key." - echo "Built $(sed -n 1p "$out/version") unsigned: the signing key is not here, so nothing was released." >>"$GITHUB_STEP_SUMMARY" + echo "::notice::Built unsigned: the signing key is not here. The machine that holds it signs and releases this build within about ten minutes (pm-console-android-release.timer)." + echo "Built $(sed -n 1p "$out/version") unsigned: the signing key is not here. The machine that holds it signs and releases this build within about ten minutes; it will appear at https://github.com/$GITHUB_REPOSITORY/releases/tag/pm-console-android" >>"$GITHUB_STEP_SUMMARY" fi - uses: actions/upload-artifact@v7 with: name: ${{ steps.build.outputs.signed == 'true' && 'pm-console-apk' || 'pm-console-apk-unsigned' }} - path: pm-stack/console/android/app/build/outputs/apk/release/*.apk + # The version goes with the build: whoever signs it releases + # it under the version it was built as. + path: | + pm-stack/console/android/app/build/outputs/apk/release/*.apk + pm-stack/console/android/app/build/outputs/apk/release/version if-no-files-found: error - name: Release if: steps.build.outputs.signed == 'true' && github.ref == 'refs/heads/main' diff --git a/.github/workflows/pm-stack-tests.yml b/.github/workflows/pm-stack-tests.yml index 4c799e4751..17c64dec8d 100644 --- a/.github/workflows/pm-stack-tests.yml +++ b/.github/workflows/pm-stack-tests.yml @@ -106,6 +106,14 @@ jobs: - name: Test the Android app's release script run: bash pm-stack/console/android/release.test.sh + # The other half (pm-stack/console/android/release-built.sh): on + # the machine that holds the key, which of GitHub's builds is + # signed (this repository's own, on main, the newest), that + # nothing is released or served unless it carries the certificate + # the site lists, and that a failure leaves everything as it was. + - name: Test the Android app's sign-and-release script + run: bash pm-stack/console/android/release-built.test.sh + # Script suite for #5993: pm-stack/scripts/ # tailnet-bindings.test.sh (the #781 regression pin for the # Pad/Grist tailnet bindings) was run by no workflow. The diff --git a/doc/fleet-schedule-registry.md b/doc/fleet-schedule-registry.md index 3773cacda7..2c1b1b7381 100644 --- a/doc/fleet-schedule-registry.md +++ b/doc/fleet-schedule-registry.md @@ -391,6 +391,7 @@ The OS-timer half of surface 2. Cadence is the unit's own `OnCalendar`/`OnUnitAc | `pm-digest-daily.timer` | `OnCalendar=*-*-* 07:00:00` | `pm-digest-daily.service` | laptop (user systemd) | not recorded | not recorded | not recorded | 2026-11-04 | | `pm-console-expose.timer` | `OnUnitActiveUSec=15min,OnBootUSec=5min` | `pm-console-expose.service` | laptop (user systemd; code#12452; runs `pm-stack/console/scripts/expose.py`: keeps the public address as declared, turns Google sign-in on once its secrets exist) | not recorded | not recorded | 2026-11-22 | 2026-11-22 | | `pm-console-redeploy.timer` | `OnUnitActiveUSec=1min,OnBootUSec=2min` | `pm-console-redeploy.service` | laptop (user systemd; code#12505; runs `pm-stack/console/redeploy.sh`: puts merged console code into service after a self-check, and leaves out code that does not start) | not recorded | not recorded | 2026-11-22 | 2026-11-22 | +| `pm-console-android-release.timer` | `OnUnitActiveUSec=10min,OnActiveUSec=2min,OnBootUSec=6min` | `pm-console-android-release.service` | laptop (user systemd; runs `pm-stack/console/android/release-built.sh`: signs the newest Android build GitHub Actions made, on the machine that holds the key, and releases it; asks GitHub one question and does nothing when that build is already released) | not recorded | not recorded | 2026-11-23 | 2026-11-23 | | `pm-digest-weekly.timer` | `OnCalendar=Mon *-*-* 07:05:00` | `pm-digest-weekly.service` | laptop (user systemd) | not recorded | not recorded | not recorded | 2026-11-04 | | `pm-git-sync.timer` | `OnUnitActiveUSec=30s,OnBootUSec=30s` | `pm-git-sync.service` | laptop (user systemd) | not recorded | not recorded | not recorded | 2026-11-04 | | `pm-host-health.timer` | `OnCalendar=*-*-* *:00/5:00,OnBootUSec=1min` | `pm-host-health.service` | laptop (user systemd; code#10141) | not recorded | not recorded | 2026-11-14 | 2026-11-14 | diff --git a/pm-stack/console/README.md b/pm-stack/console/README.md index c925713d44..a5293e6c71 100644 --- a/pm-stack/console/README.md +++ b/pm-stack/console/README.md @@ -171,7 +171,7 @@ app's link to this origin), and the two phone paths under ## Run ```bash -bash pm-stack/console/install.sh # user unit pm-console, port 4872, loopback only; and its two timers +bash pm-stack/console/install.sh # user unit pm-console, port 4872, loopback only; and its three timers python3 pm-stack/console/scripts/expose.py --owner you@example.com # first time: Access, console env, tunnel route, DNS, the edge's route python3 pm-stack/console/scripts/expose.py --check # what public exposure would change python3 pm-stack/console/scripts/expose.py # later runs (the timer's): the owner is remembered diff --git a/pm-stack/console/android/README.md b/pm-stack/console/android/README.md index 5d4ed9b58c..72ce96762f 100644 --- a/pm-stack/console/android/README.md +++ b/pm-stack/console/android/README.md @@ -59,11 +59,7 @@ A build names its own version from its commit (`build.sh`): a code that rises with the commit's time, so each build installs over the one before, and a name that says the day and the commit. -What happens next depends on who holds the signing key. - -**GitHub holds it.** The build is signed in the workflow, checked -against the certificate in `signing-fingerprint.txt`, and released by -`release.sh` with nobody involved, twice: +Every build ends up signed and released with nobody involved, twice: - `pm-console-android-v`: that build. Once made it is never changed. @@ -72,10 +68,27 @@ against the certificate in `signing-fingerprint.txt`, and released by Each carries `pm-console.apk`, `pm-console.apk.sha256` and a `version.json` (name, code, commit, sha256, size, when, certificate). - -**Only the laptop holds it** (how it starts). The workflow's build is -unsigned, kept as the artifact `pm-console-apk-unsigned`, and the run -says nothing was released. It can be signed and put in place by hand: +`release.sh` makes both. Where the signing happens depends on where the +key is. + +**Only the pm-stack host holds the key** (how it starts). The workflow's +build is unsigned and kept as the artifact `pm-console-apk-unsigned`, +with the version it was built as. On the host, +`pm-console-android-release.timer` runs `release-built.sh` every ten +minutes: it takes the newest unsigned build made on `main`, signs it +there with `sign.sh` (which refuses a result that does not carry the +certificate in `signing-fingerprint.txt`), releases it, and puts it +where the console serves the app from. The key is read there by +`apksigner` and goes nowhere; what leaves is the signed APK, which is +public. A build is released within about ten minutes of being made, or +when the host is next up. If that unit fails it shows on the console's +Services screen and in its notifications, like any other. + +Only a build this repository made on `main`, by a push or by hand, is +ever signed: a run of a pull request, another branch or a fork is not, +whatever it is called. + +By hand, the same steps: ```bash gh run download -n pm-console-apk-unsigned -D /tmp/pm-apk @@ -83,8 +96,16 @@ nix shell nixpkgs#apksigner -c bash pm-stack/console/android/sign.sh /tmp/pm-apk install -m 644 /tmp/pm-apk/pm-console.apk ~/.local/state/pm-console/android/pm-console.apk ``` +**GitHub holds the key too** (the owner's choice). The build is signed +in the workflow, checked against the same certificate, and released by +the same `release.sh`; the host then finds nothing to do, and a release +no longer waits for the host to be up. + ### Giving GitHub the key +Not needed for releases to happen: see above. It takes the host out of +the path. + The key is on the pm-stack host at `~/.local/state/pm-console/android/release.p12`, with its password in `keystore-password` beside it. The workflow signs once the @@ -100,9 +121,9 @@ environment is made by the workflow's first run; limiting it to the `main` branch (Settings, Environments) keeps a workflow on any other branch from reading the key. -A build signed by any other key fails in `build.sh` before anything is -released. Losing the key means a new key, a new fingerprint and a -reinstall; nothing else depends on it. +A build signed by any other key fails before anything is released, in +`build.sh` or in `sign.sh`. Losing the key means a new key, a new +fingerprint and a reinstall; nothing else depends on it. ## Install diff --git a/pm-stack/console/android/release-built.sh b/pm-stack/console/android/release-built.sh new file mode 100755 index 0000000000..0fc00db58a --- /dev/null +++ b/pm-stack/console/android/release-built.sh @@ -0,0 +1,148 @@ +#!/usr/bin/env bash +# release-built.sh — sign the newest build GitHub made of the app, on +# this machine, and release it. +# +# GitHub builds the app on every change to it (the pm-console android +# workflow) but does not hold the app's signing key, so what it builds +# is unsigned and nothing is released there. The key is on this machine +# and stays on it. pm-console-android-release.timer runs this script, +# which takes the newest unsigned build made on main, signs it here +# (sign.sh), releases it (release.sh) and puts it where the console +# serves the app from. Nothing of the key goes anywhere: what leaves is +# the signed APK, which is public. +# +# When the newest build is already released it asks GitHub one question +# and does nothing. If GitHub's environment is one day given the key, +# the workflow signs and releases by itself and this finds nothing to do. +# +# Usage: bash release-built.sh +# +# Inputs (environment), all optional: +# PM_GH how gh is run here (default "fleet gh" where +# fleet is installed, else "gh") +# PM_ANDROID_REPO owner/name (default developing-today/code) +# PM_CONSOLE_ANDROID_APK where the console serves the app from +# (default ~/.local/state/pm-console/android/pm-console.apk) +# PM_ANDROID_KEYSTORE, PM_ANDROID_KEYSTORE_PASSWORD_FILE +# as sign.sh reads them +# Needs python3, flock, and apksigner; where apksigner is not on PATH it +# is run through nix. +set -euo pipefail + +if [ "$#" -ne 0 ]; then + echo "usage: bash release-built.sh (it takes no arguments)" >&2 + exit 2 +fi +here="$(cd "$(dirname "$0")" && pwd)" +repo="${PM_ANDROID_REPO:-developing-today/code}" +serve="${PM_CONSOLE_ANDROID_APK:-$HOME/.local/state/pm-console/android/pm-console.apk}" +state="$(dirname "$serve")" +workflow="pm-console-android.yml" + +if ! printf '%s' "$repo" | grep -Eq '^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$'; then + echo "release-built: PM_ANDROID_REPO must be owner/name" >&2 + exit 2 +fi +if [ -z "${PM_GH:-}" ]; then + if command -v fleet >/dev/null 2>&1; then PM_GH="fleet gh"; else PM_GH="gh"; fi +fi +export PM_GH +read -r -a gh_cmd <<<"$PM_GH" +# "command": the program of that name, not this function, which is +# also called gh (PM_GH may be plain "gh"). +gh() { command "${gh_cmd[@]}" "$@"; } + +mkdir -p "$state" +# One at a time: a second start while the first is still signing leaves +# it to the first. +exec 9>"$state/release.lock" +if ! flock -n 9; then + echo "release-built: another run is at work; left to it" + exit 0 +fi + +# The newest build that is this repository's own, made on main by a push +# or by hand. A run of a pull request, of another branch or of a fork is +# never signed, whatever it is called. +runs="$(gh api "repos/$repo/actions/workflows/$workflow/runs?branch=main&status=success&per_page=30")" +newest="$(printf '%s' "$runs" | python3 -c 'import json, sys +repo = sys.argv[1] +runs = json.load(sys.stdin).get("workflow_runs", []) +mine = [ + r + for r in runs + if r.get("conclusion") == "success" + and r.get("head_branch") == "main" + and r.get("event") in ("push", "workflow_dispatch") + and (r.get("head_repository") or {}).get("full_name") == repo + and (r.get("repository") or {}).get("full_name") == repo + and isinstance(r.get("id"), int) + and isinstance(r.get("head_sha"), str) +] +mine.sort(key=lambda r: r["id"], reverse=True) +if mine: + print(mine[0]["id"], mine[0]["head_sha"])' "$repo")" +if [ -z "$newest" ]; then + echo "release-built: GitHub has no build of the app made on main yet" + exit 0 +fi +run="${newest%% *}" +sha="${newest##* }" +if ! printf '%s' "$sha" | grep -Eq '^[0-9a-f]{40}$'; then + echo "release-built: run $run names no commit" >&2 + exit 1 +fi +if [ "$(cat "$state/released-run" 2>/dev/null || true)" = "$run" ]; then + exit 0 +fi + +artifacts="$(gh api "repos/$repo/actions/runs/$run/artifacts?per_page=100" | python3 -c 'import json, sys +print(" ".join(a["name"] for a in json.load(sys.stdin).get("artifacts", []) if not a.get("expired")))')" +case " $artifacts " in +*" pm-console-apk "*) + # GitHub held the key for this one: it signed and released it itself. + printf '%s\n' "$run" >"$state/released-run" + echo "release-built: run $run was signed and released by GitHub; nothing to do here" + exit 0 + ;; +*" pm-console-apk-unsigned "*) ;; +*) + echo "release-built: run $run kept no build to sign (its artifacts: ${artifacts:-none}); waiting for the next build" + exit 0 + ;; +esac + +work="$(mktemp -d)" +trap 'rm -rf "$work"' EXIT +mkdir "$work/in" +gh run download "$run" --repo "$repo" --name pm-console-apk-unsigned --dir "$work/in" +mapfile -t apks < <(find "$work/in" -type f -name '*.apk') +if [ "${#apks[@]}" -ne 1 ]; then + echo "release-built: run $run holds ${#apks[@]} APKs, not one; nothing is signed" >&2 + exit 1 +fi +version="$(find "$work/in" -type f -name version | head -n 1)" +if [ -z "$version" ]; then + # A build from before builds carried their version. It will never + # carry one, so this is not a failure to repeat every ten minutes. + echo "release-built: run $run does not say which version it built; nothing is signed, waiting for the next build" + exit 0 +fi +name="$(sed -n 1p "$version")" +code="$(sed -n 2p "$version")" + +sign=(bash "$here/sign.sh" "${apks[0]}" "$work/pm-console.apk") +if command -v apksigner >/dev/null 2>&1; then + "${sign[@]}" +else + nix --extra-experimental-features "nix-command flakes" shell nixpkgs#apksigner -c "${sign[@]}" +fi + +VERSION_NAME="$name" VERSION_CODE="$code" GITHUB_SHA="$sha" GITHUB_REPOSITORY="$repo" GITHUB_RUN_ID="$run" \ + PM_SIGNED_HERE=1 bash "$here/release.sh" "$work/pm-console.apk" + +# Released: the console serves the same file, and this run is done with. +install -D -m 644 "$work/pm-console.apk" "$serve.new" +mv -f "$serve.new" "$serve" +printf '%s\n' "$run" >"$state/released-run" +echo "release-built: $name (code $code), built by run $run, is signed, released and served" diff --git a/pm-stack/console/android/release-built.test.sh b/pm-stack/console/android/release-built.test.sh new file mode 100644 index 0000000000..a5febad8af --- /dev/null +++ b/pm-stack/console/android/release-built.test.sh @@ -0,0 +1,385 @@ +#!/usr/bin/env bash +# release-built.test.sh — pins what release-built.sh does on the machine +# that holds the key: which of GitHub's builds it signs, that nothing is +# released or served unless it carries the right certificate, that a +# build is handled once, and that every failure leaves things as they +# were so the next run tries again. +# +# The scripts under test are the real release-built.sh, sign.sh and +# release.sh. Stand-ins: gh keeps releases in a directory and answers +# the two Actions questions from files; apksigner "signs" by copying and +# reports the certificate a file names; fleet passes gh through. Whether +# apksigner signs correctly is apksigner's business, and sign.sh's check +# of the certificate is what is relied on here. +set -euo pipefail + +if [ "$#" -gt 0 ]; then + echo "usage: release-built.test.sh (it takes no arguments)" >&2 + exit 2 +fi + +here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +tmp="$(mktemp -d)" +trap 'rm -rf "$tmp"' EXIT +gh_state="$tmp/github" +home="$tmp/home" +mkdir -p "$tmp/bin" "$tmp/fleet-bin" "$gh_state" "$home" + +# ---- stand-ins -------------------------------------------------------------- +cat >"$tmp/bin/gh" <<'GH' +#!/usr/bin/env bash +# Releases live under $GH_STATE// (as in release.test.sh). The runs +# of the workflow are $GH_STATE/.runs.json; a run's artifacts are +# $GH_STATE/.artifacts-.json and their files $GH_STATE/.files-/. +set -euo pipefail +echo "$*" >>"$GH_STATE/.log" +if [ "$1" = "api" ]; then + path="" sha="" + for arg in "$@"; do + case "$arg" in + repos/*) path="$arg" ;; + sha=*) sha="${arg#sha=}" ;; + esac + done + case "$path" in + repos/*/actions/workflows/pm-console-android.yml/runs\?*) + [ -e "$GH_STATE/.github-is-down" ] && exit 1 + # What is asked for is part of what is pinned: only finished builds on main. + case "$path" in *branch=main*status=success*) ;; *) exit 64 ;; esac + cat "$GH_STATE/.runs.json" + ;; + repos/*/actions/runs/*/artifacts\?*) + id="${path#repos/*/actions/runs/}" + cat "$GH_STATE/.artifacts-${id%%/*}.json" + ;; + repos/*/git/refs/tags/*) + mkdir -p "$GH_STATE/.tags" + echo "$sha" >"$GH_STATE/.tags/${path##*/}" + ;; + *) exit 64 ;; + esac + exit 0 +fi +if [ "$1" = "run" ] && [ "$2" = "download" ]; then + id="$3" name="" out="" + shift 3 + while [ "$#" -gt 0 ]; do + case "$1" in + --repo) shift ;; + --name) name="$2" && shift ;; + --dir) out="$2" && shift ;; + *) exit 64 ;; + esac + shift + done + [ "$name" = "pm-console-apk-unsigned" ] || exit 64 + mkdir -p "$out" + cp -r "$GH_STATE/.files-$id/." "$out/" + exit 0 +fi +[ "$1" = "release" ] || exit 64 +verb="$2" tag="$3" +shift 3 +dir="$GH_STATE/$tag" +files=() patterns=() out="" +while [ "$#" -gt 0 ]; do + case "$1" in + --repo) shift ;; + --target) target="$2" && shift ;; + --title) title="$2" && shift ;; + --notes) notes="$2" && shift ;; + --dir) out="$2" && shift ;; + --pattern) patterns+=("$2") && shift ;; + --latest=false) latest=false ;; + --draft=false) draft=false ;; + --clobber) ;; + --*) exit 64 ;; + *) files+=("$1") ;; + esac + shift +done +case "$verb" in +view) [ -d "$dir" ] ;; +create) + [ -e "$GH_STATE/.cannot-release" ] && exit 1 + [ ! -d "$dir" ] || exit 1 + mkdir -p "$dir/assets" + echo false >"$dir/draft" + echo "${target:-}" >"$dir/target" + echo "${notes:-}" >"$dir/notes" + cp "${files[@]}" "$dir/assets/" + ;; +upload) + [ -d "$dir" ] || exit 1 + cp "${files[@]}" "$dir/assets/" + ;; +download) + [ -d "$dir" ] || exit 1 + found=0 + for pattern in "${patterns[@]}"; do + if [ -f "$dir/assets/$pattern" ]; then + cp "$dir/assets/$pattern" "$out/" + found=1 + fi + done + [ "$found" -eq 1 ] + ;; +edit) + [ -d "$dir" ] || exit 1 + [ -n "${notes:-}" ] && echo "$notes" >"$dir/notes" + true + ;; +*) exit 64 ;; +esac +GH + +# fleet: only "fleet gh ..." is known, and it is gh. +cat >"$tmp/fleet-bin/fleet" <<'FLEET' +#!/usr/bin/env bash +set -euo pipefail +[ "$1" = "gh" ] || exit 64 +shift +echo "$*" >>"$GH_STATE/.fleet-log" +exec gh "$@" +FLEET + +# apksigner: "sign" copies the build behind a mark, having seen the key +# and its password file; "verify --print-certs" names the certificate in +# $APKSIGNER_CERT (lower-case hex, as apksigner prints it). +cat >"$tmp/bin/apksigner" <<'APKSIGNER' +#!/usr/bin/env bash +set -euo pipefail +verb="$1" +shift +case "$verb" in +sign) + ks="" pass="" out="" in="" + while [ "$#" -gt 0 ]; do + case "$1" in + --ks) ks="$2" && shift ;; + --ks-type | --ks-key-alias) shift ;; + --ks-pass) pass="$2" && shift ;; + --out) out="$2" && shift ;; + --*) exit 64 ;; + *) in="$1" ;; + esac + shift + done + [ -f "$ks" ] && [ -f "${pass#file:}" ] && [ "${pass#file:}" != "$pass" ] || exit 1 + echo "sign $in" >>"$APKSIGNER_LOG" + { + printf 'signed:' + cat "$in" + } >"$out" + ;; +verify) + [ "$1" = "--print-certs" ] || exit 64 + echo "Signer #1 certificate DN: CN=PM console" + echo "Signer #1 certificate SHA-256 digest: $(cat "$APKSIGNER_CERT")" + ;; +*) exit 64 ;; +esac +APKSIGNER +chmod +x "$tmp/bin/gh" "$tmp/bin/apksigner" "$tmp/fleet-bin/fleet" + +fail() { + echo "FAIL: $*" >&2 + exit 1 +} +field() { python3 -c 'import json, sys; print(json.load(open(sys.argv[1]))[sys.argv[2]])' "$1" "$2"; } +calls() { if [ -f "$gh_state/.log" ]; then wc -l <"$gh_state/.log"; else echo 0; fi; } +signed() { if [ -f "$tmp/apksigner.log" ]; then wc -l <"$tmp/apksigner.log"; else echo 0; fi; } + +right_cert="$(tr -d '[:space:]:' <"$here/signing-fingerprint.txt" | tr 'A-F' 'a-f')" +echo "$right_cert" >"$tmp/cert" +printf 'a keystore' >"$tmp/key.p12" +printf 'its password\n' >"$tmp/key-password" +serve="$home/.local/state/pm-console/android/pm-console.apk" +state="$(dirname "$serve")" +fixed="$gh_state/pm-console-android" +sha_a="$(printf 'a%.0s' {1..40})" +sha_b="$(printf 'b%.0s' {1..40})" +sha_c="$(printf 'c%.0s' {1..40})" + +export GH_STATE="$gh_state" APKSIGNER_LOG="$tmp/apksigner.log" APKSIGNER_CERT="$tmp/cert" +export HOME="$home" PM_ANDROID_REPO="example/code" +export PM_ANDROID_KEYSTORE="$tmp/key.p12" PM_ANDROID_KEYSTORE_PASSWORD_FILE="$tmp/key-password" +unset PM_GH PM_CONSOLE_ANDROID_APK GITHUB_STEP_SUMMARY GITHUB_SHA GITHUB_REPOSITORY GITHUB_RUN_ID VERSION_NAME VERSION_CODE +with_fleet="$tmp/fleet-bin:$tmp/bin:$PATH" +without_fleet="$tmp/bin:$PATH" +export PATH="$with_fleet" + +run_json() { # run_json + printf '{"id": %s, "event": "%s", "head_branch": "%s", "head_repository": {"full_name": "%s"}, "repository": {"full_name": "example/code"}, "conclusion": "%s", "head_sha": "%s"}' "$@" +} +runs() { # runs ... + local joined="" r + for r in "$@"; do joined="${joined:+$joined, }$r"; done + printf '{"workflow_runs": [%s]}\n' "$joined" >"$gh_state/.runs.json" +} +build() { # build + if [ -n "$2" ]; then + printf '{"artifacts": [{"name": "%s", "expired": false}, {"name": "something-else", "expired": false}]}\n' "$2" >"$gh_state/.artifacts-$1.json" + else + printf '{"artifacts": [{"name": "pm-console-apk-unsigned", "expired": true}]}\n' >"$gh_state/.artifacts-$1.json" + fi + rm -rf "$gh_state/.files-$1" + mkdir -p "$gh_state/.files-$1" + printf '%s' "$3" >"$gh_state/.files-$1/app-release-unsigned.apk" + printf '%s\n%s\n' "$4" "$5" >"$gh_state/.files-$1/version" +} +go() { bash "$here/release-built.sh" >"$tmp/out" 2>&1; } +refused() { # refused : the run must fail and change nothing that was released, served or recorded + local before_run before_apk before_fixed + before_run="$(cat "$state/released-run" 2>/dev/null || true)" + before_apk="$(cat "$serve" 2>/dev/null || true)" + before_fixed="$(cat "$fixed/assets/version.json" 2>/dev/null || true)" + if go; then fail "$1: it did not fail: $(cat "$tmp/out")"; fi + [ "$(cat "$state/released-run" 2>/dev/null || true)" = "$before_run" ] || fail "$1: the run was recorded as done" + [ "$(cat "$serve" 2>/dev/null || true)" = "$before_apk" ] || fail "$1: the served app changed" + [ "$(cat "$fixed/assets/version.json" 2>/dev/null || true)" = "$before_fixed" ] || fail "$1: the release changed" +} + +# 1. Of everything GitHub lists, only this repository's own finished +# build on main is signed: not a run of another repository or of a +# fork (whatever its event and branch are called), not a pull +# request, not another branch, not a failed run, not a schedule. +runs \ + "$(run_json 920 push main example/code success "$sha_c" | sed 's|"repository": {"full_name": "example/code"}|"repository": {"full_name": "someone/else"}|')" \ + "$(run_json 910 workflow_dispatch main someone/fork success "$sha_c")" \ + "$(run_json 900 pull_request main someone/fork success "$sha_c")" \ + "$(run_json 890 push feature example/code success "$sha_c")" \ + "$(run_json 880 schedule main example/code success "$sha_c")" \ + "$(run_json 870 push main example/code failure "$sha_c")" \ + "$(run_json 850 push main example/code success "$sha_b")" \ + "$(run_json 860 workflow_dispatch main example/code success "$sha_a")" +for id in 920 910 900 890 880 870; do build "$id" pm-console-apk-unsigned "not ours $id" "0.0.0-bad" 99999999; done +build 850 pm-console-apk-unsigned "an older build" "2026.11.22-bbbbbbb" 21756300 +build 860 pm-console-apk-unsigned "build 860" "2026.11.23-aaaaaaa" 21756400 +go || fail "1: $(cat "$tmp/out")" +[ "$(cat "$state/released-run")" = 860 ] || fail "1: the run it handled is not written down" +[ "$(cat "$serve")" = "signed:build 860" ] || fail "1: the console is not serving the signed build" +[ "$(stat -c %a "$serve")" = 644 ] || fail "1: the served file is not readable by the console" +numbered="$gh_state/pm-console-android-v21756400" +[ "$(cat "$numbered/assets/pm-console.apk")" = "signed:build 860" ] || fail "1: the numbered release does not hold the signed build" +[ "$(cat "$fixed/assets/pm-console.apk")" = "signed:build 860" ] || fail "1: the fixed address does not hold the signed build" +[ "$(field "$fixed/assets/version.json" name)" = "2026.11.23-aaaaaaa" ] || fail "1: wrong name released" +[ "$(field "$fixed/assets/version.json" code)" = 21756400 ] || fail "1: wrong code released" +[ "$(field "$fixed/assets/version.json" commit)" = "$sha_a" ] || fail "1: wrong commit released" +[ "$(cat "$numbered/target")" = "$sha_a" ] || fail "1: the release is not of the commit that was built" +grep -q "actions/runs/860" "$numbered/notes" || fail "1: the release does not say which run built it" +grep -q "Signed and released on the machine that holds the app's key" "$numbered/notes" || fail "1: the release does not say where it was signed" +grep -q "^sign .*app-release-unsigned.apk$" "$tmp/apksigner.log" || fail "1: apksigner was not given the build" +[ "$(signed)" = 1 ] || fail "1: signed $(signed) times, not once" +grep -q "^run download 860 " "$gh_state/.log" || fail "1: the build was not the one fetched" +! grep -q "download 920\|download 910\|download 900\|download 890\|download 880\|download 870\|download 850" "$gh_state/.log" || fail "1: a build that is not ours, or not the newest, was fetched" +# With fleet installed, every word to GitHub goes through it, release.sh's too. +[ "$(wc -l <"$gh_state/.fleet-log")" = "$(calls)" ] || fail "1: some gh calls did not go through fleet" + +# 2. The same build again: one question to GitHub, and nothing else. +before="$(calls)" +go || fail "2: $(cat "$tmp/out")" +[ "$(calls)" = "$((before + 1))" ] || fail "2: asked GitHub $(($(calls) - before)) things, not one" +[ "$(signed)" = 1 ] || fail "2: signed again" + +# 3. A newer build that comes out signed by another certificate is not +# released, not served and not written down; once the certificate is +# right, the next run releases it. +runs "$(run_json 950 push main example/code success "$sha_b")" "$(run_json 860 push main example/code success "$sha_a")" +build 950 pm-console-apk-unsigned "build 950" "2026.11.24-bbbbbbb" 21756500 +echo "00$(printf '%s' "$right_cert" | cut -c3-)" >"$tmp/cert" +refused "3 (wrong certificate)" +[ ! -d "$gh_state/pm-console-android-v21756500" ] || fail "3: a build with the wrong certificate was released" +grep -q "not by the certificate the site lists" "$tmp/out" || fail "3: it does not say why: $(cat "$tmp/out")" +echo "$right_cert" >"$tmp/cert" +go || fail "3: $(cat "$tmp/out")" +[ "$(cat "$state/released-run")" = 950 ] || fail "3: the newer run is not written down" +[ "$(cat "$serve")" = "signed:build 950" ] || fail "3: the console is not serving the newer build" +[ "$(field "$fixed/assets/version.json" code)" = 21756500 ] || fail "3: the fixed address does not hold the newer build" +[ "$(cat "$numbered/assets/pm-console.apk")" = "signed:build 860" ] || fail "3: the earlier numbered release was changed" + +# 4. GitHub cannot release it (create fails): nothing is served or +# written down, and the next run does it. +runs "$(run_json 960 push main example/code success "$sha_c")" +build 960 pm-console-apk-unsigned "build 960" "2026.11.25-ccccccc" 21756600 +touch "$gh_state/.cannot-release" +refused "4 (the release cannot be made)" +rm "$gh_state/.cannot-release" +go || fail "4: $(cat "$tmp/out")" +[ "$(cat "$serve")" = "signed:build 960" ] || fail "4: the build was not released on the next run" + +# 5. What a build must carry: exactly one APK, and its version. +runs "$(run_json 970 push main example/code success "$sha_a")" +build 970 pm-console-apk-unsigned "build 970" "2026.11.26-aaaaaaa" 21756700 +printf 'a second apk' >"$gh_state/.files-970/another.apk" +refused "5 (two APKs)" +# A build from before builds carried their version is passed over, and +# that is said; it is not a failure, because it can never be mended. +rm "$gh_state/.files-970/another.apk" "$gh_state/.files-970/version" +before_signed="$(signed)" +go || fail "5 (no version): $(cat "$tmp/out")" +grep -q "does not say which version it built" "$tmp/out" || fail "5: it does not say the version is missing: $(cat "$tmp/out")" +[ "$(signed)" = "$before_signed" ] || fail "5: a build without its version was signed" +[ "$(cat "$state/released-run")" = 960 ] || fail "5: a build without its version was written down as done" +[ "$(cat "$serve")" = "signed:build 960" ] || fail "5: the served app changed" +printf '%s\n%s\n' "--draft" 21756700 >"$gh_state/.files-970/version" +refused "5 (a version name that is an option)" +printf '%s\n%s\n' "2026.11.26-aaaaaaa" "not a number" >"$gh_state/.files-970/version" +refused "5 (a version code that is not a number)" +[ ! -d "$gh_state/pm-console-android-vnot a number" ] || fail "5: released under a code that is not a number" + +# 6. A build GitHub signed itself (it holds the key) is not signed again +# here: it is written down, and nothing else happens. +runs "$(run_json 980 push main example/code success "$sha_b")" +build 980 pm-console-apk "signed by GitHub" "2026.11.27-bbbbbbb" 21756800 +before_signed="$(signed)" +go || fail "6: $(cat "$tmp/out")" +[ "$(signed)" = "$before_signed" ] || fail "6: a build GitHub had signed was signed again" +[ "$(cat "$state/released-run")" = 980 ] || fail "6: the run is not written down" +[ "$(cat "$serve")" = "signed:build 960" ] || fail "6: the served app changed" + +# 7. A build whose artifact is gone (they expire) cannot be signed: that +# is said, it is not a failure, and it is looked at again next time. +runs "$(run_json 990 push main example/code success "$sha_c")" +build 990 "" "gone" "2026.11.28-ccccccc" 21756900 +go || fail "7: $(cat "$tmp/out")" +grep -q "kept no build to sign" "$tmp/out" || fail "7: it does not say the build is gone: $(cat "$tmp/out")" +[ "$(cat "$state/released-run")" = 980 ] || fail "7: a run with nothing to sign was written down as done" + +# 8. GitHub does not answer: a failure, so that the unit shows it. +touch "$gh_state/.github-is-down" +refused "8 (GitHub does not answer)" +rm "$gh_state/.github-is-down" + +# 9. No build of ours at all: nothing to do, and not a failure. +runs "$(run_json 995 pull_request main someone/fork success "$sha_c")" +go || fail "9: $(cat "$tmp/out")" +grep -q "no build of the app made on main" "$tmp/out" || fail "9: $(cat "$tmp/out")" + +# 10. Two at once: the second leaves it to the first and asks nothing. +runs "$(run_json 996 push main example/code success "$sha_a")" +build 996 pm-console-apk-unsigned "build 996" "2026.11.29-aaaaaaa" 21757000 +before="$(calls)" +( + flock 8 + go || fail "10: $(cat "$tmp/out")" +) 8>"$state/release.lock" +grep -q "another run is at work" "$tmp/out" || fail "10: $(cat "$tmp/out")" +[ "$(calls)" = "$before" ] || fail "10: the second run went to GitHub" + +# 11. Where fleet is not installed, plain gh is used, and the build that +# was waiting behind the lock is released. +export PATH="$without_fleet" +fleet_before="$(wc -l <"$gh_state/.fleet-log")" +go || fail "11: $(cat "$tmp/out")" +[ "$(cat "$serve")" = "signed:build 996" ] || fail "11: the waiting build was not released" +[ "$(wc -l <"$gh_state/.fleet-log")" = "$fleet_before" ] || fail "11: fleet was used though it is not installed" +export PATH="$with_fleet" + +# 12. Nothing is asked of GitHub on bad input. +before="$(calls)" +if bash "$here/release-built.sh" something >/dev/null 2>&1; then fail "12: took an argument"; fi +if PM_ANDROID_REPO="not a repository" bash "$here/release-built.sh" >/dev/null 2>&1; then fail "12: took a bad repository"; fi +[ "$(calls)" = "$before" ] || fail "12: went to GitHub on bad input" + +echo "release-built.test.sh: ok" diff --git a/pm-stack/console/android/release.sh b/pm-stack/console/android/release.sh index 116ed72ebb..3c00426088 100644 --- a/pm-stack/console/android/release.sh +++ b/pm-stack/console/android/release.sh @@ -7,6 +7,10 @@ # VERSION_NAME, VERSION_CODE the build's version (build.sh writes both) # GITHUB_SHA, GITHUB_REPOSITORY, GITHUB_RUN_ID, GITHUB_SERVER_URL # as a workflow run has them +# PM_GH how gh is run, where it is not plain gh +# ("fleet gh" on the host) +# PM_SIGNED_HERE set when the build was made by that run +# and signed on this machine (release-built.sh) # Needs gh, signed in with leave to write releases (GH_TOKEN), and python3. # # Two releases come of it: @@ -56,9 +60,21 @@ if ! printf '%s' "$repo" | grep -Eq '^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$'; then exit 2 fi +if [ -n "${PM_GH:-}" ]; then + read -r -a gh_cmd <<<"$PM_GH" + # "command": the program of that name, not this function, which is + # also called gh (PM_GH may be plain "gh"). + gh() { command "${gh_cmd[@]}" "$@"; } +fi + numbered="pm-console-android-v$code" fixed="pm-console-android" run_url="${GITHUB_SERVER_URL:-https://github.com}/$repo/actions/runs/${GITHUB_RUN_ID:-0}" +if [ -n "${PM_SIGNED_HERE:-}" ]; then + made="Built by $run_url. Signed and released on the machine that holds the app's key, which never leaves it." +else + made="Built, signed and released by $run_url." +fi certificate="$(tr -d '[:space:]' <"$here/signing-fingerprint.txt")" work="$(mktemp -d)" trap 'rm -rf "$work"' EXIT @@ -121,7 +137,7 @@ open(out, "a").write("\n")' "$name" "$code" "$sha" "$digest" "$certificate" "$wo --title "PM console for Android $name" \ --notes "The PM console as an Android app, build $name (version code $code), from commit $sha. -Built, signed and released by $run_url. Signed by the certificate $certificate, the one the site lists, so it installs over earlier builds. +$made Signed by the certificate $certificate, the one the site lists, so it installs over earlier builds. The newest build is always at https://github.com/$repo/releases/download/$fixed/pm-console.apk" \ "$work/new/pm-console.apk" "$work/new/pm-console.apk.sha256" "$work/new/version.json" diff --git a/pm-stack/console/install.sh b/pm-stack/console/install.sh index 89c71c3e3f..0d3897757e 100755 --- a/pm-stack/console/install.sh +++ b/pm-stack/console/install.sh @@ -32,15 +32,20 @@ if [ ! -f "$STATE/.env" ]; then fi install -m 644 "$UNIT_SRC" "$UNIT_DST" # One timer keeps the public address as declared, and turns Google -# sign-in on by itself once its two secrets exist. The other puts merged +# sign-in on by itself once its two secrets exist. Another puts merged # console code into service, so a merge needs nobody at this machine. -for unit in pm-console-expose.service pm-console-expose.timer pm-console-redeploy.service pm-console-redeploy.timer; do - install -m 644 "$(dirname "$SRC")/systemd/$unit" "$(dirname "$UNIT_DST")/$unit" +# The third signs the Android build GitHub makes, here where the key is, +# and releases it. +TIMERS=(pm-console-expose pm-console-redeploy pm-console-android-release) +for timer in "${TIMERS[@]}"; do + for unit in "$timer.service" "$timer.timer"; do + install -m 644 "$(dirname "$SRC")/systemd/$unit" "$(dirname "$UNIT_DST")/$unit" + done done systemctl --user daemon-reload systemctl --user enable pm-console.service >/dev/null systemctl --user restart pm-console.service -systemctl --user enable --now pm-console-expose.timer pm-console-redeploy.timer >/dev/null +systemctl --user enable --now "${TIMERS[@]/%/.timer}" >/dev/null # systemd strips one pair of quotes from an EnvironmentFile value, so # PM_CONSOLE_PORT="4872" is a valid line: read it the same way. diff --git a/pm-stack/console/redeploy.sh b/pm-stack/console/redeploy.sh index 7c15dca1c5..f2e92b09cb 100755 --- a/pm-stack/console/redeploy.sh +++ b/pm-stack/console/redeploy.sh @@ -24,7 +24,8 @@ SRC="$PWD" REPO="$(git -C "$SRC" rev-parse --show-toplevel)" STATE="$HOME/.local/state/pm-console" BUN="${PM_CONSOLE_BUN:-/run/current-system/sw/bin/bun}" -UNITS=(pm-console.service pm-console-expose.service pm-console-expose.timer pm-console-redeploy.service pm-console-redeploy.timer) +UNITS=(pm-console.service pm-console-expose.service pm-console-expose.timer pm-console-redeploy.service pm-console-redeploy.timer + pm-console-android-release.service pm-console-android-release.timer) # What is in the checkout: the console's tree, and the units it runs # under. A checkout that is part-way through an update (files that do diff --git a/pm-stack/console/test/redeploy.test.ts b/pm-stack/console/test/redeploy.test.ts index e9a27e28fd..ee562263fe 100644 --- a/pm-stack/console/test/redeploy.test.ts +++ b/pm-stack/console/test/redeploy.test.ts @@ -18,6 +18,8 @@ const UNITS = [ "pm-console-expose.timer", "pm-console-redeploy.service", "pm-console-redeploy.timer", + "pm-console-android-release.service", + "pm-console-android-release.timer", ]; let dir: string; diff --git a/pm-stack/systemd/pm-console-android-release.service b/pm-stack/systemd/pm-console-android-release.service new file mode 100644 index 0000000000..8d07d86ee2 --- /dev/null +++ b/pm-stack/systemd/pm-console-android-release.service @@ -0,0 +1,15 @@ +[Unit] +Description=PM console — sign the newest Android build GitHub made, here, and release it (pm-stack) +Documentation=https://github.com/developing-today/code/tree/main/pm-stack/console/android +After=network-online.target + +[Service] +Type=oneshot +# Asks GitHub one question and does nothing, unless GitHub has built a +# version of the app that is not released yet. Then the build is signed +# on this machine, which holds the key, and released. The key is read +# by apksigner here and goes nowhere. +ExecStart=/run/current-system/sw/bin/bash %h/git/code/pm-stack/console/android/release-built.sh +Environment=PATH=%h/.local/bin:/etc/profiles/per-user/%u/bin:/run/current-system/sw/bin:/usr/bin:/bin +# The first run may have to fetch apksigner. +TimeoutStartSec=900 diff --git a/pm-stack/systemd/pm-console-android-release.timer b/pm-stack/systemd/pm-console-android-release.timer new file mode 100644 index 0000000000..983505328e --- /dev/null +++ b/pm-stack/systemd/pm-console-android-release.timer @@ -0,0 +1,12 @@ +[Unit] +Description=PM console — look every ten minutes for an Android build to sign and release (pm-stack) + +[Timer] +# Soon after it is first installed, soon after a boot, then every ten minutes. +OnActiveSec=2min +OnBootSec=6min +OnUnitActiveSec=10min +RandomizedDelaySec=30 + +[Install] +WantedBy=timers.target