From 8ddf893de917bedf8d538ea7793df146664360e5 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sun, 8 Nov 2026 16:58:47 +0000 Subject: [PATCH] Publish APK certificate identity for multi-server compatibility --- app/docker/build.sh | 6 ++++++ docs/notes/desktop-client-builds.md | 6 ++++++ scripts/app_build.py | 24 +++++++++++++++++++++- scripts/test_app_build.py | 31 +++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) diff --git a/app/docker/build.sh b/app/docker/build.sh index c0bddd78..8deca337 100755 --- a/app/docker/build.sh +++ b/app/docker/build.sh @@ -31,6 +31,12 @@ kotlin.compiler.execution.strategy=in-process PROPERTIES flutter build apk --release "$@" cp build/app/outputs/flutter-apk/app-release.apk /out/pipelines.apk + # Public certificate identity only; never export the signing key. + apksigner=$(find "$ANDROID_SDK_ROOT/build-tools" -name apksigner -type f | sort -V | tail -n 1) + test -x "$apksigner" + "$apksigner" verify --print-certs /out/pipelines.apk > /tmp/apk-certificates.txt + awk -F ': ' '/^Signer #[0-9]+ certificate SHA-256 digest:/ { print tolower($2) }' \ + /tmp/apk-certificates.txt > /out/signing-certificate-sha256.txt ;; linux) flutter build linux --release "$@" diff --git a/docs/notes/desktop-client-builds.md b/docs/notes/desktop-client-builds.md index 32e0a86f..7711156d 100644 --- a/docs/notes/desktop-client-builds.md +++ b/docs/notes/desktop-client-builds.md @@ -31,3 +31,9 @@ Linux plugin registries are regenerated inside the private build from the tracke pubspec/lockfile; they are ignored and excluded from source hashing so host `flutter pub get` does not change the source key. A focused regression verifies this while confirming a changed dependency lock still invalidates the build. + +Multi-server Android compatibility: Docker verifies the built APK with SDK +`apksigner` and exports only its public SHA-256 certificate fingerprint. APK feeds +now include `signingCertificateSha256` and archive `sha256`, so native discovery +can avoid offering a build signed independently on another machine. Missing or +malformed signer metadata cannot be cached/published; no private key is exported. diff --git a/scripts/app_build.py b/scripts/app_build.py index d050ca1a..72c4a090 100644 --- a/scripts/app_build.py +++ b/scripts/app_build.py @@ -144,12 +144,29 @@ def signing_dir(): return directory +def apk_signing_fingerprint(output): + """Read one verified APK signer identity, never private key material.""" + try: + value = (output / "signing-certificate-sha256.txt").read_text().strip().lower() + except OSError as error: + raise ValueError("APK build is missing its verified signing certificate fingerprint") from error + if len(value) != 64 or any(c not in "0123456789abcdef" for c in value): + raise ValueError("APK build must report exactly one SHA-256 signing certificate fingerprint") + return value + + def build(kind, runner=subprocess.run, app=APP, cache=None, arguments=(), log=print): """The directory holding the finished [kind] build, built only when needed.""" cache = Path(cache) if cache else default_cache() key = digest(kind, app, arguments) entry = cache / f"{kind}-{key}" usable = web_cache_complete(entry) if kind == "web" else (entry / ".complete").is_file() + if kind == "apk" and usable: + try: + apk_signing_fingerprint(entry) + usable = (entry / "pipelines.apk").is_file() + except ValueError: + usable = False if kind == "linux": usable = usable and (entry / "pipelines-app").is_file() and (entry / "data/flutter_assets").is_dir() and (entry / "lib").is_dir() if usable: @@ -188,6 +205,8 @@ def build(kind, runner=subprocess.run, app=APP, cache=None, arguments=(), log=pr code = built // 60 stamp = (f"--build-number={code}", f"--build-name=0.1.{code}") docker(runner, *command, tag, "pipelines-app-build", kind, *build_arguments, *stamp) + if kind == "apk": + apk_signing_fingerprint(work) if kind == "linux" and not ((work / "pipelines-app").is_file() and (work / "data/flutter_assets").is_dir() and (work / "lib").is_dir()): raise RuntimeError("Linux build did not produce its complete executable/data/lib bundle") @@ -253,6 +272,8 @@ def build_apk(prefix, server_url=None, notes="", runner=subprocess.run, log=prin # reuse one APK and every rebuild is newer than the last. arguments = (f"--dart-define=PIPELINES_SERVER_URL={server}",) output = build("apk", runner, arguments=arguments, log=log) + fingerprint = apk_signing_fingerprint(output) + apk_sha256 = hashlib.sha256((output / "pipelines.apk").read_bytes()).hexdigest() built = json.loads((output / ".complete").read_text())["built"] code = built // 60 target = Path(updates) @@ -260,7 +281,8 @@ def build_apk(prefix, server_url=None, notes="", runner=subprocess.run, log=prin shutil.copyfile(output / "pipelines.apk", target / "pipelines.apk.next") os.replace(target / "pipelines.apk.next", target / "pipelines.apk") (target / "version.json").write_text(json.dumps({ - "versionCode": code, "versionName": f"0.1.{code}", "apk": "pipelines.apk", "notes": notes}) + "\n") + "versionCode": code, "versionName": f"0.1.{code}", "apk": "pipelines.apk", "notes": notes, + "signingCertificateSha256": fingerprint, "sha256": apk_sha256}) + "\n") log(f"APK ready: {target / 'pipelines.apk'} (build {code}, server {server}).") log("Phones get it from Settings → App updates → Check for updates, or download " f"{server.replace('wss://', 'https://').replace('ws://', 'http://').removesuffix('/connect')}/updates/pipelines.apk") diff --git a/scripts/test_app_build.py b/scripts/test_app_build.py index 6fd18770..b31b33d7 100644 --- a/scripts/test_app_build.py +++ b/scripts/test_app_build.py @@ -22,6 +22,7 @@ class Runner: if command[:2] == ["docker", "run"]: out = Path(command[command.index("-v", command.index("-v") + 1) + 1].split(":")[0]) (out / "pipelines.apk").write_bytes(b"apk") + (out / "signing-certificate-sha256.txt").write_text("ab" * 32 + "\n") return subprocess.CompletedProcess(command, 0, stdout="", stderr="") @@ -69,6 +70,36 @@ class AppBuildTests(unittest.TestCase): other = app_build.build("apk", runner=runner, cache=cache, arguments=("--dart-define=X=2",), log=lambda *_: None) self.assertNotEqual(other, first) + def test_apk_publication_includes_verified_signer_and_file_hash(self): + output = Path(self.home.name) / "artifact" + output.mkdir() + (output / "pipelines.apk").write_bytes(b"verified apk bytes") + (output / "signing-certificate-sha256.txt").write_text("AB" * 32 + "\n") + (output / ".complete").write_text(json.dumps({"built": 1800000000})) + updates = Path(self.home.name) / "updates" + manifest = {"web": {"updates": str(updates)}} + with patch.object(app_build, "load_manifest", return_value=manifest), patch.object(app_build, "build", return_value=output): + app_build.build_apk(Path(self.home.name), server_url="wss://example.com/connect", log=lambda *_: None) + feed = json.loads((updates / "version.json").read_text()) + self.assertEqual(feed["signingCertificateSha256"], "ab" * 32) + self.assertEqual(feed["sha256"], app_build.hashlib.sha256((updates / "pipelines.apk").read_bytes()).hexdigest()) + (output / "signing-certificate-sha256.txt").write_text("invalid") + with patch.object(app_build, "load_manifest", return_value=manifest), patch.object(app_build, "build", return_value=output): + with self.assertRaisesRegex(ValueError, "exactly one"): + app_build.build_apk(Path(self.home.name), server_url="wss://example.com/connect", log=lambda *_: None) + self.assertEqual(json.loads((updates / "version.json").read_text()), feed) + + def test_apk_cache_without_signer_metadata_is_rebuilt(self): + runner = Runner() + cache = Path(self.home.name) / "cache" + first = app_build.build("apk", runner=runner, cache=cache, log=lambda *_: None) + (first / "signing-certificate-sha256.txt").unlink() + runner.calls.clear() + rebuilt = app_build.build("apk", runner=runner, cache=cache, log=lambda *_: None) + self.assertEqual(rebuilt, first) + self.assertTrue(any(c[:2] == ["docker", "run"] for c in runner.calls)) + self.assertEqual(app_build.apk_signing_fingerprint(rebuilt), "ab" * 32) + def test_web_build_has_no_build_number(self): runner = Runner() output = app_build.build("web", runner=runner, cache=Path(self.home.name) / "cache", log=lambda *_: None)