Publish APK certificate identity for multi-server compatibility

This commit is contained in:
2026-11-08 16:58:47 +00:00
parent bd9509b744
commit 8ddf893de9
4 changed files with 66 additions and 1 deletions
+6
View File
@@ -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 "$@"
+6
View File
@@ -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.
+23 -1
View File
@@ -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")
+31
View File
@@ -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)