|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Promote the exact reviewed build-only distributions before PyPI upload.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import argparse |
| 7 | +import hashlib |
| 8 | +import json |
| 9 | +import os |
| 10 | +import re |
| 11 | +import subprocess |
| 12 | +import tarfile |
| 13 | +from pathlib import Path, PurePosixPath |
| 14 | + |
| 15 | +REPOSITORY = "durable-workflow/sdk-python" |
| 16 | +MAX_FILE_BYTES = 64 * 1024 * 1024 |
| 17 | +SHA = re.compile(r"[0-9a-f]{40}") |
| 18 | +DIGEST = re.compile(r"[0-9a-f]{64}") |
| 19 | +KEYS = {"schema", "version", "source_commit", "source_tree", "run_id", "run_attempt", |
| 20 | + "artifact_id", "artifact_sha256", "files"} |
| 21 | + |
| 22 | + |
| 23 | +def unique_object(pairs: list[tuple[str, object]]) -> dict: |
| 24 | + result = {} |
| 25 | + for key, value in pairs: |
| 26 | + if key in result: |
| 27 | + raise ValueError("release approval contains a duplicate member") |
| 28 | + result[key] = value |
| 29 | + return result |
| 30 | + |
| 31 | + |
| 32 | +def distribution_names(version: str) -> set[str]: |
| 33 | + if re.fullmatch(r"[0-9]+\.[0-9]+\.[0-9]+", version) is None: |
| 34 | + raise ValueError("release version must be exact stable SemVer") |
| 35 | + return {f"durable_workflow-{version}-py3-none-any.whl", f"durable_workflow-{version}.tar.gz"} |
| 36 | + |
| 37 | + |
| 38 | +def require_digest(value: object) -> None: |
| 39 | + if not isinstance(value, str) or DIGEST.fullmatch(value) is None: |
| 40 | + raise ValueError("release approval requires exact artifact digests") |
| 41 | + |
| 42 | + |
| 43 | +def validate_descriptor(approval: dict, version: str) -> None: |
| 44 | + if (not isinstance(approval, dict) or set(approval) != KEYS |
| 45 | + or approval["schema"] != "sdk-python.reviewed-release/v1" or approval["version"] != version): |
| 46 | + raise ValueError("release approval schema or version mismatch") |
| 47 | + for key in ("source_commit", "source_tree"): |
| 48 | + if not isinstance(approval[key], str) or SHA.fullmatch(approval[key]) is None: |
| 49 | + raise ValueError("release approval requires exact source identities") |
| 50 | + for key in ("run_id", "run_attempt", "artifact_id"): |
| 51 | + if type(approval[key]) is not int or approval[key] <= 0: |
| 52 | + raise ValueError("release approval requires positive numeric producer identities") |
| 53 | + files = approval["files"] |
| 54 | + expected = distribution_names(version) |
| 55 | + if not isinstance(files, dict) or set(files) != expected: |
| 56 | + raise ValueError("release approval must name exactly the wheel and sdist") |
| 57 | + for value in (approval["artifact_sha256"], *files.values()): |
| 58 | + require_digest(value) |
| 59 | + |
| 60 | + |
| 61 | +def load_approval(root: Path, version: str) -> dict: |
| 62 | + distribution_names(version) |
| 63 | + name = f".github/release-candidates/{version}.json" |
| 64 | + path = root / name |
| 65 | + if path.is_symlink() or not path.is_file() or path.stat().st_size > 65536: |
| 66 | + raise ValueError("release approval must be a bounded regular file") |
| 67 | + approval = json.loads(path.read_text(), object_pairs_hook=unique_object) |
| 68 | + validate_descriptor(approval, version) |
| 69 | + source = approval["source_commit"] |
| 70 | + if SHA.fullmatch(source) is None: |
| 71 | + raise ValueError("reviewed source must be an exact commit") |
| 72 | + directory = str(root.resolve()) |
| 73 | + subprocess.run(["git", "-C", directory, "merge-base", "--is-ancestor", "--", source, "HEAD"], check=True) |
| 74 | + tree = subprocess.check_output( |
| 75 | + ["git", "-C", directory, "rev-parse", "--verify", "--end-of-options", f"{source}^{{tree}}"], text=True, |
| 76 | + ).strip() |
| 77 | + if tree != approval["source_tree"]: |
| 78 | + raise ValueError("reviewed source tree mismatch") |
| 79 | + # The approval cannot be embedded in its own reviewed source commit. |
| 80 | + # Every executable/configuration/package change must precede the build. |
| 81 | + changed = subprocess.check_output( |
| 82 | + ["git", "-C", directory, "diff", "--no-ext-diff", "--no-textconv", "--no-renames", |
| 83 | + "--name-status", source, "HEAD", "--"], text=True, |
| 84 | + ).strip() |
| 85 | + if changed != f"A\t{name}": |
| 86 | + raise ValueError("publication source differs from reviewed source beyond its approval file") |
| 87 | + return approval |
| 88 | + |
| 89 | + |
| 90 | +def verify_authority(approval: dict, run: dict, artifact: dict, repository_id: int) -> None: |
| 91 | + repository = {"id": repository_id, "full_name": REPOSITORY} |
| 92 | + if (run.get("id") != approval["run_id"] or run.get("run_attempt") != approval["run_attempt"] |
| 93 | + or run.get("head_sha") != approval["source_commit"] or run.get("event") != "workflow_dispatch" |
| 94 | + or run.get("path") != ".github/workflows/publish.yml" or run.get("status") != "completed" |
| 95 | + or run.get("conclusion") != "success" |
| 96 | + or any(run.get(key, {}).get(field) != value |
| 97 | + for key in ("repository", "head_repository") for field, value in repository.items())): |
| 98 | + raise ValueError("reviewed build run authority mismatch") |
| 99 | + producer = artifact.get("workflow_run", {}) |
| 100 | + if (artifact.get("id") != approval["artifact_id"] or artifact.get("name") != "dist-handoff.tar" |
| 101 | + or artifact.get("expired") is not False |
| 102 | + or artifact.get("digest") != "sha256:" + approval["artifact_sha256"] |
| 103 | + or type(artifact.get("size_in_bytes")) is not int |
| 104 | + or not 0 < artifact["size_in_bytes"] <= 2 * MAX_FILE_BYTES + 65536 |
| 105 | + or producer.get("id") != approval["run_id"] |
| 106 | + or producer.get("head_sha") != approval["source_commit"] |
| 107 | + or producer.get("repository_id") != repository_id |
| 108 | + or producer.get("head_repository_id") != repository_id): |
| 109 | + raise ValueError("reviewed artifact authority mismatch") |
| 110 | + |
| 111 | + |
| 112 | +def api(path: str) -> dict: |
| 113 | + result = subprocess.check_output(["gh", "api", f"repos/{REPOSITORY}/{path}"]) |
| 114 | + if len(result) > 1048576: |
| 115 | + raise ValueError("producer metadata exceeds its bound") |
| 116 | + return json.loads(result) |
| 117 | + |
| 118 | + |
| 119 | +def file_sha256(path: Path) -> str: |
| 120 | + digest = hashlib.sha256() |
| 121 | + with path.open("rb") as handle: |
| 122 | + for chunk in iter(lambda: handle.read(1048576), b""): |
| 123 | + digest.update(chunk) |
| 124 | + return digest.hexdigest() |
| 125 | + |
| 126 | + |
| 127 | +def extract_reviewed(approval: dict, incoming: Path, dist: Path) -> None: |
| 128 | + archive = incoming / "dist-handoff.tar" |
| 129 | + if (incoming.is_symlink() or not incoming.is_dir() or set(incoming.iterdir()) != {archive} |
| 130 | + or archive.is_symlink() or not archive.is_file() |
| 131 | + or archive.stat().st_size > 2 * MAX_FILE_BYTES + 65536 |
| 132 | + or file_sha256(archive) != approval["artifact_sha256"]): |
| 133 | + raise ValueError("downloaded producer artifact differs from reviewed bytes") |
| 134 | + contents = {} |
| 135 | + with tarfile.open(archive, mode="r:") as handle: |
| 136 | + for member in handle: |
| 137 | + if member.name in (".", "./") and member.isdir() and member.size == 0: |
| 138 | + continue |
| 139 | + name = PurePosixPath(member.name) |
| 140 | + if (name.is_absolute() or len(name.parts) != 1 or "\\" in member.name |
| 141 | + or name.name not in approval["files"] or name.name in contents |
| 142 | + or not member.isreg() or member.mode != 0o644 or not 0 < member.size <= MAX_FILE_BYTES): |
| 143 | + raise ValueError("producer artifact has unexpected, duplicate or unsafe members") |
| 144 | + with handle.extractfile(member) as stream: |
| 145 | + data = stream.read(MAX_FILE_BYTES + 1) |
| 146 | + if len(data) != member.size or hashlib.sha256(data).hexdigest() != approval["files"][name.name]: |
| 147 | + raise ValueError("distribution differs from reviewed bytes") |
| 148 | + contents[name.name] = data |
| 149 | + if set(contents) != set(approval["files"]): |
| 150 | + raise ValueError("producer artifact does not contain both reviewed distributions") |
| 151 | + # Validate everything before creating output; never extract archive paths. |
| 152 | + dist.mkdir() |
| 153 | + for name in sorted(distribution_names(approval["version"])): |
| 154 | + descriptor = os.open(dist / name, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o644) |
| 155 | + with os.fdopen(descriptor, "wb") as output: |
| 156 | + output.write(contents[name]) |
| 157 | + |
| 158 | + |
| 159 | +def main() -> None: |
| 160 | + parser = argparse.ArgumentParser(description=__doc__) |
| 161 | + parser.add_argument("operation", choices=("resolve", "extract")) |
| 162 | + parser.add_argument("--version", required=True) |
| 163 | + args = parser.parse_args() |
| 164 | + approval = load_approval(Path.cwd(), args.version) |
| 165 | + if args.operation == "resolve": |
| 166 | + verify_authority(approval, api(f"actions/runs/{approval['run_id']}"), |
| 167 | + api(f"actions/artifacts/{approval['artifact_id']}"), int(os.environ["GITHUB_REPOSITORY_ID"])) |
| 168 | + with Path(os.environ["GITHUB_OUTPUT"]).open("a") as output: |
| 169 | + output.write(f"run-id={approval['run_id']}\nartifact-id={approval['artifact_id']}\n") |
| 170 | + for name in sorted(distribution_names(args.version)): |
| 171 | + kind = "wheel" if name.endswith(".whl") else "sdist" |
| 172 | + output.write(f"{kind}-sha256={approval['files'][name]}\n") |
| 173 | + else: |
| 174 | + extract_reviewed(approval, Path(".release-candidate"), Path("dist")) |
| 175 | + |
| 176 | + |
| 177 | +if __name__ == "__main__": |
| 178 | + main() |
0 commit comments