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