From 5450b9523a56c25a6ee5343473a11e877541fd16 Mon Sep 17 00:00:00 2001 From: diviocity Date: Mon, 30 Nov 2026 01:34:37 +0100 Subject: [PATCH] Assemble signed offline repositories and package records into images --- tests/FEED.md | 12 +++++ tools/stage-image.py | 115 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 tools/stage-image.py diff --git a/tests/FEED.md b/tests/FEED.md index 0b2a255..d44c0c9 100644 --- a/tests/FEED.md +++ b/tests/FEED.md @@ -117,3 +117,15 @@ python3 tools/keyring.py seed "$work/new-client/root" "$trusted_fingerprint" `vinix-keyring` owns the public certificate and fingerprint in `/usr/share/vpac/keyrings/`. Image assembly initializes the mutable GnuPG keybox and trust database under `/etc/vpac.d/gnupg/`. The seed operation accepts a new destination or the empty directory installed by the vpac port. The builder compares the independently supplied fingerprint before packaging and again before seeding. The worker used a disposable test signer in `foundation/keyring-04`. It rejected a wrong pin, installed the signed keyring, seeded an empty vpac key directory, verified the signed database and used that keyring alone for an offline vpac install of musl into another empty root. A second seed attempt preserved the existing keyring. The checksum manifest has SHA-256 `3ffb71fb6832d43af1cb2ec588d2d6a75ece24060d22bd1e70be59392a64d30d`. The target-vpac guest transaction and provisioning the release signer's public certificate remain release checks. + +### Assemble an image with an offline repository + +Start with a fresh `feed install` client that includes `vinix-keyring`. Run this on the build worker with GNU cp, GnuPG and the installed-root auditor. The resulting `$output/root` is the input to the architecture's image builder. + +```sh +python3 tools/stage-image.py "$arch" "$client" "$repo" "$output" "$work/audit" "$trusted_fingerprint" +``` + +The tool audits the package-owned root, makes a separate copy, and seeds the verification keyring using the distributor's fingerprint. It copies the local package records into `/var/lib/vpac/local`, checks their versions against the signed database, and installs the repository under `/var/lib/vinix/repository/$arch`. It verifies the database, file index and each included package signature, and compares the payload hashes with the signed database. The sync cache receives independent copies of the database and signature. Assembly exposes the final root only after these checks pass. + +On both targets, the worker installed musl and the public key package, then assembled a three-package offline repository. Using the build-box vpac with its paths directed into each resulting image, it installed the third package, `netbase`, from the embedded repository and recorded its ownership in that image. The logs are in `foundation/offline-stage-01` with receipt manifest SHA-256 `98bfcbdaed3aa81690bcdac5a8b0edbdfd91f5ab6ae8a7cc9f3fdd50e6252fb1`. diff --git a/tools/stage-image.py b/tools/stage-image.py new file mode 100644 index 0000000..ed7fa0f --- /dev/null +++ b/tools/stage-image.py @@ -0,0 +1,115 @@ +"""Assemble an offline image root from a signed vpac client and its package repository.""" +import argparse +import hashlib +import shutil +import subprocess +import sys +import tarfile +import tempfile +from pathlib import Path + + +def package_fields(data): + fields = {} + for block in data.strip().split("\n\n"): + name, _, value = block.partition("\n") + if name.startswith("%") and name.endswith("%"): + fields[name.strip("%")] = value + return fields + + +def write_directory(root, relative): + directory = root / relative + if directory.resolve() != directory: + raise ValueError(f"unexpected guest symlink at {relative}") + directory.mkdir(parents=True, exist_ok=True) + return directory + + +def snapshot(repo, dest, ring, arch, gpg_home): + def verify(path): + subprocess.run(["gpgv", "--homedir", gpg_home, "--keyring", ring, str(path) + ".sig", path], + check=True, stdout=subprocess.DEVNULL) + + def copy(name): + if Path(name).name != name or not name or name in (".", ".."): + raise ValueError(f"invalid repository filename {name!r}") + source = repo / name + if source.resolve().parent != repo or not source.is_file(): + raise ValueError(f"missing or external repository file: {name}") + target = dest / name + if target.exists(): + raise ValueError(f"duplicate repository filename: {name}") + shutil.copyfile(source, target) + return target + + for index in ("vinix.db", "vinix.files"): + file = copy(index) + copy(index + ".sig") + verify(file) + indexed = {} + with tarfile.open(dest / "vinix.db", "r:gz") as database: + for entry in database: + if not entry.isfile() or Path(entry.name).name != "desc": + continue + info = package_fields(database.extractfile(entry).read().decode()) + name, version, package_arch = (info[field] for field in ("NAME", "VERSION", "ARCH")) + filename = info["FILENAME"] + if (name in indexed or package_arch not in (arch, "any") or + filename != f"{name}-{version}-{package_arch}.pkg.tar.zst"): + raise ValueError(f"invalid database record for {name}") + archive = copy(filename) + copy(filename + ".sig") + with archive.open("rb") as stream: + if hashlib.file_digest(stream, "sha256").hexdigest() != info["SHA256SUM"]: + raise ValueError(f"archive differs from signed index: {filename}") + verify(archive) + indexed[name] = version + if not indexed: + raise ValueError("empty signed package index") + return indexed + + +def stage(args): + client, repo, output = args.client.resolve(), args.repo.resolve(), args.output.resolve() + if any(output.is_relative_to(path) for path in (client, repo)): + raise ValueError("put image output outside the client and repository") + output.mkdir() + audit = subprocess.check_output([args.auditor, args.arch, client], text=True) + (output / "audited-root.log").write_text(audit) + with tempfile.TemporaryDirectory(dir=output) as directory: + root = Path(directory) / "image" + subprocess.run(["cp", "-a", "--reflink=auto", client / "root", root], check=True) + # This compares the installed certificate with the independently supplied pin. + seed = Path(__file__).resolve().with_name("keyring.py") + subprocess.run([sys.executable, seed, "seed", root, args.fingerprint], check=True) + package_dir = write_directory(root, "var/lib/vinix/repository/" + args.arch) + if any(package_dir.iterdir()): + raise ValueError("seed repository directory is already populated") + with tempfile.TemporaryDirectory() as gpg_home: + indexed = snapshot(repo / args.arch, package_dir, root / "usr/share/vpac/keyrings/vinix.gpg", + args.arch, gpg_home) + records = client / "db/local" + for description in records.glob("*/desc"): + info = package_fields(description.read_text()) + if indexed.get(info["NAME"]) != info["VERSION"]: + raise ValueError(f"installed package missing from seed at its exact version: {info['NAME']}") + db = write_directory(root, "var/lib/vpac") + if any(db.iterdir()): + raise ValueError("package database directory is already populated") + shutil.copytree(records, db / "local") + sync = write_directory(root, "var/lib/vpac/sync") + for name in ("vinix.db", "vinix.db.sig"): + shutil.copyfile(package_dir / name, sync / name) + root.rename(output / "root") + print(audit, end="") + print(f"staged {len(indexed)} signed seed packages and the installed database") + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("arch", choices=("x86_64", "aarch64")) + for arg in ("client", "repo", "output", "auditor"): + parser.add_argument(arg, type=Path) + parser.add_argument("fingerprint", help="trusted distributor fingerprint from outside the feed") + stage(parser.parse_args()) -- 2.56.1