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