"""Verify pinned source identity, not Cassandra behavior. Python 3 standard library.""" import hashlib import json from pathlib import Path import re import urllib.request COMMIT = "186272edca920c757b91bf95c2392bafa1a38d72" PREFIX = "https://raw.githubusercontent.com/apache/cassandra/" + COMMIT + "/" SOURCE_IDS = {"read-doc", "point-tests", "query-helper", "execution-helper", "repair-tests"} def validate_packet(packet): if packet.get("schema_version") != 1 or packet.get("upstream_commit") != COMMIT: raise ValueError("Unexpected schema or upstream commit") if packet.get("native_tests_executed") is not False: raise ValueError("This packet must not claim native execution") sources = packet.get("sources", []) if len(sources) != 5 or {source["id"] for source in sources} != SOURCE_IDS: raise ValueError("Missing or duplicate source") for source in sources: if source["url"] != PREFIX + source["path"]: raise ValueError("Source URL is not the pinned file") if not re.fullmatch(r"[a-f0-9]{64}", source["sha256"]): raise ValueError("Invalid digest") if type(source["bytes"]) is not int or not 0 < source["bytes"] < 1000000: raise ValueError("Invalid source size") for locator in source["locators"]: if type(locator["line"]) is not int or locator["line"] < 1: raise ValueError("Invalid source locator") return sources def verify_source(source, raw): if len(raw) != source["bytes"] or hashlib.sha256(raw).hexdigest() != source["sha256"]: raise ValueError("Source bytes changed: " + source["id"]) lines = raw.decode("utf-8").splitlines() for locator in source["locators"]: index = locator["line"] - 1 if index >= len(lines) or locator["symbol"] not in lines[index]: raise ValueError("Source locator changed: " + source["id"]) def main(): packet = json.loads(Path(__file__).with_name("source-review.json").read_text()) sources = validate_packet(packet) for source in sources: with urllib.request.urlopen(source["url"], timeout=30) as response: raw = response.read(1000001) verify_source(source, raw) print("Source identity verified: " + source["id"]) print("Verified five pinned files and locators. Cassandra tests were NOT executed.") if __name__ == "__main__": main()