9f38e38f6a
Each installation generates a private Ed25519 key once (0600, under ~/.config/pipelines), signs version.json with a detached version.json.sig when publishing APK and desktop builds, and embeds its own and listed trusted public keys in app builds. The app verifies the feed signature before trusting the artifact sha256, refuses unsigned or foreign feeds when it has keys, and requires the sha256 and Android signer in signed feeds.
142 lines
7.4 KiB
Python
142 lines
7.4 KiB
Python
import importlib.util
|
|
import io
|
|
import json
|
|
from contextlib import redirect_stdout
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
spec = importlib.util.spec_from_file_location("app_build", Path(__file__).with_name("app_build.py"))
|
|
app_build = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(app_build)
|
|
|
|
|
|
class Runner:
|
|
def __init__(self):
|
|
self.calls = []
|
|
|
|
def __call__(self, command, **kwargs):
|
|
self.calls.append(list(command))
|
|
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="")
|
|
|
|
|
|
class AppBuildTests(unittest.TestCase):
|
|
def test_web_urls_include_saved_port_lan_and_public_origin(self):
|
|
manifest = {"web": {"host": "0.0.0.0", "port": 8096},
|
|
"public_url": "http://192.168.1.20:8090",
|
|
"opencode_env": {"PIPELINES_WEB_PUBLIC_URL": "https://pipelines.example.com"}}
|
|
with redirect_stdout(io.StringIO()) as output:
|
|
app_build.print_web_urls(manifest)
|
|
self.assertEqual(output.getvalue().splitlines()[-3:], [
|
|
" http://localhost:8096/", " http://192.168.1.20:8096/",
|
|
" https://pipelines.example.com/"])
|
|
|
|
def test_web_install_ends_with_access_urls(self):
|
|
manifest = {"web": {"host": "127.0.0.1", "port": 8083, "root": "/unused"}}
|
|
with patch.object(app_build, "docker_available", return_value=True), \
|
|
patch.object(app_build, "load_manifest", return_value=manifest), \
|
|
patch.object(app_build, "build"), patch.object(app_build, "install_tree"), \
|
|
redirect_stdout(io.StringIO()) as output:
|
|
app_build.main(["web"])
|
|
self.assertEqual(output.getvalue().splitlines()[-1], " http://localhost:8083/")
|
|
|
|
def setUp(self):
|
|
self.home = tempfile.TemporaryDirectory()
|
|
self.addCleanup(self.home.cleanup)
|
|
patcher = patch.object(app_build.Path, "home", return_value=Path(self.home.name))
|
|
patcher.start()
|
|
self.addCleanup(patcher.stop)
|
|
|
|
def test_apk_is_stamped_and_reused_while_sources_are_unchanged(self):
|
|
runner = Runner()
|
|
cache = Path(self.home.name) / "cache"
|
|
first = app_build.build("apk", runner=runner, cache=cache, arguments=("--dart-define=X=1",), log=lambda *_: None)
|
|
run = next(call for call in runner.calls if call[:2] == ["docker", "run"])
|
|
built = json.loads((first / ".complete").read_text())["built"]
|
|
self.assertIn(f"--build-number={built // 60}", run)
|
|
self.assertIn("--dart-define=X=1", run)
|
|
self.assertIn("linux/amd64", run)
|
|
runner.calls.clear()
|
|
again = app_build.build("apk", runner=runner, cache=cache, arguments=("--dart-define=X=1",), log=lambda *_: None)
|
|
self.assertEqual(again, first)
|
|
self.assertEqual(runner.calls, [])
|
|
# Different build arguments are a different build.
|
|
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())
|
|
# Signed with the installation key generated (0600) in the scratch home.
|
|
key = Path(self.home.name) / ".config/pipelines/update-signing-key.pem"
|
|
self.assertEqual(key.stat().st_mode & 0o777, 0o600)
|
|
signature = json.loads((updates / "version.json.sig").read_text())
|
|
self.assertEqual(signature["publicKey"], app_build.update_signing.public_key(key))
|
|
(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)
|
|
run = next(call for call in runner.calls if call[:2] == ["docker", "run"])
|
|
self.assertFalse(any(part.startswith("--build-number") for part in run))
|
|
self.assertNotIn("linux/amd64", run)
|
|
identity = app_build.web_build_id(json.loads((output / ".complete").read_text())["key"])
|
|
self.assertIn(f"--dart-define=PIPELINES_WEB_BUILD_ID={identity}", run)
|
|
self.assertEqual(json.loads((output / "release.json").read_text()), {
|
|
"schema": 1, "platform": "web", "buildId": identity})
|
|
|
|
def test_old_web_cache_without_release_metadata_is_rebuilt(self):
|
|
runner = Runner()
|
|
cache = Path(self.home.name) / "cache"
|
|
first = app_build.build("web", runner=runner, cache=cache, log=lambda *_: None)
|
|
(first / "release.json").unlink()
|
|
runner.calls.clear()
|
|
rebuilt = app_build.build("web", runner=runner, cache=cache, log=lambda *_: None)
|
|
self.assertEqual(rebuilt, first)
|
|
self.assertTrue((rebuilt / "release.json").is_file())
|
|
self.assertTrue(any(call[:2] == ["docker", "run"] for call in runner.calls))
|
|
|
|
def test_web_identity_changes_when_sources_change(self):
|
|
app = Path(self.home.name) / "app"
|
|
(app / "lib").mkdir(parents=True)
|
|
(app / ".flutter-version").write_text("3.0")
|
|
before = app_build.digest("web", app)
|
|
(app / "lib/main.dart").write_text("changed")
|
|
after = app_build.digest("web", app)
|
|
self.assertNotEqual(app_build.web_build_id(before), app_build.web_build_id(after))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|