| 1 | """Compare published release inputs with package owners in an audited vpac root.""" |
| 2 | import argparse |
| 3 | import csv |
| 4 | import subprocess |
| 5 | from collections import Counter, defaultdict |
| 6 | from pathlib import Path |
| 7 | |
| 8 | |
| 9 | # Most Alpine split packages are combined under their source name in Vinix. |
| 10 | # These are expected owners. Actual ownership comes from the installed database. |
| 11 | SOURCE_OWNERS = { |
| 12 | "alpine-base": ("vinix-release",), |
| 13 | "alpine-baselayout": ("vinix-filesystem", "vinix-accounts", "netbase"), |
| 14 | "alpine-keys": ("vinix-keyring",), |
| 15 | "apk-tools": ("vpac",), |
| 16 | "hwdata": ("hwdata-pci",), |
| 17 | "llvm17": ("llvm17-libs",), |
| 18 | "llvm19": ("llvm19-libs",), |
| 19 | "mpc1": ("mpc",), |
| 20 | "mpfr4": ("mpfr",), |
| 21 | "openssh": ("openssh-client",), |
| 22 | "pax-utils": ("scanelf",), |
| 23 | } |
| 24 | BINARY_OWNERS = { |
| 25 | "ca-certificates-bundle": ("ca-certificates-bundle",), |
| 26 | "libuuid": ("libuuid",), |
| 27 | "musl-utils": ("musl-utils",), |
| 28 | "ssl_client": ("ssl-client",), |
| 29 | "mesa-dev": ("mesa", "mesa-platform-headers", "mesa-gallium-sdk"), |
| 30 | "mesa-dri-gallium": ("mesa-gallium-sdk",), |
| 31 | "mesa-osmesa": ("mesa-gallium-sdk",), |
| 32 | "mesa-xatracker": ("mesa-gallium-sdk",), |
| 33 | } |
| 34 | |
| 35 | |
| 36 | def providers(row): |
| 37 | if row["origin"] == "mesa" and row["repository"] == "v3.20/main": |
| 38 | return ("mesa-glx-legacy",) |
| 39 | return BINARY_OWNERS.get(row["binary"], SOURCE_OWNERS.get(row["origin"], (row["origin"],))) |
| 40 | |
| 41 | |
| 42 | def field(body, name): |
| 43 | for group in body.strip().split("\n\n"): |
| 44 | marker, _, value = group.partition("\n") |
| 45 | if marker == "%" + name + "%": |
| 46 | return value.splitlines() |
| 47 | return [] |
| 48 | |
| 49 | |
| 50 | def installed(client, arch, auditor): |
| 51 | result = subprocess.check_output([auditor, arch, client], text=True) |
| 52 | names = set() |
| 53 | owners = {} |
| 54 | for description in sorted((client / "db/local").glob("*/desc")): |
| 55 | name, = field(description.read_text(), "NAME") |
| 56 | names.add(name) |
| 57 | for path in field(description.with_name("files").read_text(), "FILES"): |
| 58 | if not path.endswith("/"): |
| 59 | if path in owners: |
| 60 | raise ValueError(f"multiple owners for {path}") |
| 61 | owners[path] = name |
| 62 | return names, owners, result |
| 63 | |
| 64 | |
| 65 | def rows(path): |
| 66 | with path.open() as stream: |
| 67 | yield from csv.DictReader(stream, delimiter="\t") |
| 68 | |
| 69 | |
| 70 | def key(row): |
| 71 | return row["binary"], row["version"], row["repository"] |
| 72 | |
| 73 | |
| 74 | def action(row, state, names, expected, has_client): |
| 75 | if int(row["unavailable"]): |
| 76 | return "obtain and compare original source input" |
| 77 | if not state: |
| 78 | if int(row["different"]): |
| 79 | return "review changed published paths and identify their owner" |
| 80 | if row["profiles"] == "x11-index-dependency": |
| 81 | return "check whether native X11 builds require this index dependency" |
| 82 | return "verify as a build or staging input" |
| 83 | if not has_client: |
| 84 | return "install and audit the target packages" |
| 85 | if row["origin"] in ("alpine-keys", "apk-tools"): |
| 86 | return "verify the Vinix trust and package-management replacements in the image" |
| 87 | if not names.intersection(expected): |
| 88 | return "build and install the expected packages" |
| 89 | if state["unowned"] or state["other_owner"]: |
| 90 | return "resolve missing paths, alternate owners and intentional replacements" |
| 91 | return "verify the package behavior in the release image" |
| 92 | |
| 93 | |
| 94 | def compare(args): |
| 95 | names, owners, audit = installed(args.client, args.arch, args.auditor) if args.client else (set(), {}, "") |
| 96 | packages = {key(row): row for row in rows(args.comparison / "packages.tsv")} |
| 97 | details = defaultdict(Counter) |
| 98 | seen = set() |
| 99 | args.output.mkdir() |
| 100 | if audit: |
| 101 | (args.output / "installed-root.log").write_text(audit) |
| 102 | with (args.output / "paths.tsv").open("x") as stream: |
| 103 | writer = csv.writer(stream, delimiter="\t", lineterminator="\n") |
| 104 | writer.writerow(["binary", "version", "repository", "layer", "published_path", |
| 105 | "original_comparison", "candidate_path", "installed_owner", "ownership"]) |
| 106 | for row in rows(args.comparison / "package-files.tsv"): |
| 107 | k = key(row) |
| 108 | path = row["published_path"] |
| 109 | owner = owners.get(path, "") |
| 110 | known = row["comparison"] != "different" |
| 111 | expected = providers(packages[k]) |
| 112 | match = ("unmeasured" if not args.client else "unowned" if not owner else |
| 113 | "expected_owner" if owner in expected else "other_owner") |
| 114 | writer.writerow([*k, row["layer"], path, row["comparison"], row["candidate_path"], owner, match]) |
| 115 | observation = k, row["layer"], path |
| 116 | if known and observation not in seen: |
| 117 | seen.add(observation) |
| 118 | details[k][match] += 1 |
| 119 | with (args.output / "packages.tsv").open("x") as stream: |
| 120 | writer = csv.writer(stream, delimiter="\t", lineterminator="\n") |
| 121 | writer.writerow(["binary", "version", "repository", "origin", "profiles", "expected_packages", |
| 122 | "installed_expected_packages", "published_matching_paths", "expected_owner", |
| 123 | "other_owner", "unowned", "unmeasured", "next_check"]) |
| 124 | for k, row in packages.items(): |
| 125 | expected = providers(row) |
| 126 | state = details[k] |
| 127 | writer.writerow([*k, row["origin"], row["profiles"], ",".join(expected), |
| 128 | ",".join(sorted(names.intersection(expected))), sum(state.values()), |
| 129 | *[state[s] for s in ("expected_owner", "other_owner", "unowned", "unmeasured")], |
| 130 | action(row, state, names, expected, args.client is not None)]) |
| 131 | print(f"{args.arch}: {len(packages)} release input rows, {len(names)} installed packages, " |
| 132 | f"{len(seen)} matching input/layer/path observations") |
| 133 | |
| 134 | |
| 135 | if __name__ == "__main__": |
| 136 | parser = argparse.ArgumentParser(description=__doc__) |
| 137 | parser.add_argument("arch", choices=("x86_64", "aarch64")) |
| 138 | parser.add_argument("comparison", type=Path) |
| 139 | parser.add_argument("output", type=Path) |
| 140 | parser.add_argument("--client", type=Path, help="fresh signed vpac client to audit") |
| 141 | parser.add_argument("--auditor", type=Path, help="compiled tests/installed-root.v") |
| 142 | args = parser.parse_args() |
| 143 | if bool(args.client) != bool(args.auditor): |
| 144 | parser.error("--client and --auditor must be used together") |
| 145 | compare(args) |
| 146 | |