Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 31 additions & 21 deletions .github/workflows/pm-console-android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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<code>, 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<code>, 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]
Expand All @@ -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
Expand All @@ -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'
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/pm-stack-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions doc/fleet-schedule-registry.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
2 changes: 1 addition & 1 deletion pm-stack/console/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 33 additions & 12 deletions pm-stack/console/android/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<code>`: that build. Once made it is never
changed.
Expand All @@ -72,19 +68,44 @@ 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 <run-id> -n pm-console-apk-unsigned -D /tmp/pm-apk
nix shell nixpkgs#apksigner -c bash pm-stack/console/android/sign.sh /tmp/pm-apk/app-release-unsigned.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
Expand All @@ -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

Expand Down
148 changes: 148 additions & 0 deletions pm-stack/console/android/release-built.sh
Original file line number Diff line number Diff line change
@@ -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"
Loading