#!/usr/bin/env python3 """Verify that a product lock is fully represented by signed component feeds.""" from __future__ import annotations import argparse import importlib.util from pathlib import Path import re from types import ModuleType from typing import Any, NoReturn def fail(message: str) -> NoReturn: raise SystemExit(f"verify-locked-component-feeds: {message}") def load_helpers() -> ModuleType: path = Path(__file__).with_name("publish-component-feeds.py") specification = importlib.util.spec_from_file_location( "echoion_verify_component_feeds", path ) if specification is None or specification.loader is None: fail(f"cannot load shared feed helpers from {path}") module = importlib.util.module_from_spec(specification) specification.loader.exec_module(module) return module def main() -> None: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--registry", type=Path, default=Path("components.json")) parser.add_argument( "--lock", type=Path, default=Path("components.lock.json") ) parser.add_argument("--feed-root", type=Path, required=True) parser.add_argument( "--channel", choices=("stable", "beta", "nightly"), default="stable" ) parser.add_argument("--key-id", required=True) parser.add_argument("--public-key", type=Path, required=True) arguments = parser.parse_args() helpers = load_helpers() registry = helpers.load_object(arguments.registry, "component registry") lock = helpers.load_object(arguments.lock, "component lock") definitions = registry.get("components") locked = lock.get("components") if ( registry.get("schemaVersion") != 1 or lock.get("schemaVersion") != 2 or not isinstance(definitions, dict) or not isinstance(locked, dict) or set(definitions) != set(locked) ): fail("unsupported or inconsistent component metadata") feed_root = arguments.feed_root.resolve(strict=True) verifier = helpers.PublicKeyVerifier( arguments.public_key, arguments.key_id ) try: catalog_path = ( feed_root / "feeds" / arguments.channel / "catalog.json" ) catalog = verifier.verify( catalog_path.read_bytes(), f"{arguments.channel} catalog" ) if ( catalog.get("schemaVersion") != 3 or catalog.get("document") != "echoion-component-catalog" or catalog.get("channel") != arguments.channel ): fail("catalog is not an independent component catalog") entries = catalog.get("components") if not isinstance(entries, list): fail("catalog components must be an array") catalog_by_id: dict[str, dict[str, Any]] = {} for entry in entries: if not isinstance(entry, dict): fail("catalog component entry must be an object") component_id = entry.get("componentId") if ( not isinstance(component_id, str) or component_id in catalog_by_id ): fail("catalog contains an invalid or duplicate component ID") catalog_by_id[component_id] = entry for component_id in sorted(definitions): definition = definitions[component_id] expected = locked[component_id] entry = catalog_by_id.get(component_id) if entry is None: fail(f"catalog is missing locked component {component_id}") if entry.get("componentVersion") != expected["componentVersion"]: fail(f"catalog version mismatch for {component_id}") relative = helpers.component_path(registry, component_id) release_path = ( feed_root / "feeds" / arguments.channel / relative / f"{expected['componentVersion']}.json" ) release = verifier.verify( release_path.read_bytes(), f"{component_id} release" ) required = { "schemaVersion": 3, "document": "echoion-component-release", "channel": arguments.channel, "componentId": component_id, "componentVersion": expected["componentVersion"], "sourceSha": expected["revision"], "sourceTreeHash": expected["sourceTreeHash"], "abiHash": expected["abiHash"], "artifactIdentity": expected["artifactIdentity"], "activation": definition["activation"], "dependencies": definition["dependencies"], "buildType": "RelWithDebInfo", } for field, value in required.items(): if release.get(field) != value: fail(f"{component_id} release {field} does not match lock") artifact = release.get("artifact") if ( not isinstance(artifact, dict) or not re.fullmatch( r"[0-9a-f]{64}", str(artifact.get("sha256", "")) ) or not str(artifact.get("url", "")).startswith("https://") ): fail(f"{component_id} release artifact is invalid") except OSError as error: fail(f"cannot read component feed: {error}") finally: verifier.close() print( f"verified {len(definitions)} locked components in " f"{arguments.channel}" ) if __name__ == "__main__": main()