From 8cb52ca3bcb9e6531a6c9853c6e921fe449a7e33 Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Sun, 15 Nov 2026 06:27:48 +0800 Subject: [PATCH] Authenticate release delivery evidence and reauthorize sends Bind accepted delivery records to exact trusted producer artifacts and workflow attempts. Recheck current consumer authorization immediately before POST, and retain regression coverage for forged content, incomplete records, revocation and reruns. --- .github/workflows/release-finalize.yml | 6 +- .github/workflows/release-redeliver.yml | 8 +- scripts/release_pipeline.py | 333 +++++++++++++++++---- tests/test_release_binding.py | 322 ++++++++++++++------ tests/test_release_pipeline.py | 372 ++++++++++++++++++++++++ tests/test_release_redeliver.py | 94 ++++-- tests/test_release_workflows.py | 34 ++- 7 files changed, 988 insertions(+), 181 deletions(-) create mode 100644 tests/test_release_pipeline.py diff --git a/.github/workflows/release-finalize.yml b/.github/workflows/release-finalize.yml index 77b0395..0a7d8b3 100644 --- a/.github/workflows/release-finalize.yml +++ b/.github/workflows/release-finalize.yml @@ -143,7 +143,7 @@ jobs: exit 0 - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: - ref: main + ref: ${{ github.sha }} persist-credentials: false - name: Require one configured consumer repository shell: bash @@ -168,6 +168,8 @@ jobs: - name: Dispatch pineforge-release from immutable bytes env: GH_TOKEN: ${{ steps.app.outputs.token }} + # Read authority from the hub; the App token is target-only. + HUB_TOKEN: ${{ github.token }} TAG: ${{ matrix.tag }} CONSUMER: ${{ matrix.consumer }} DELIVERY_ATTEMPT: ${{ matrix.attempt }} @@ -199,7 +201,7 @@ jobs: with: name: release-delivery-${{ matrix.consumer }}-${{ matrix.tag }}-${{ github.run_id }}-${{ github.run_attempt }} path: ${{ runner.temp }}/delivery.json - if-no-files-found: warn + if-no-files-found: error - name: Report failed dispatch if: ${{ failure() && !cancelled() }} env: diff --git a/.github/workflows/release-redeliver.yml b/.github/workflows/release-redeliver.yml index 1c2ac1e..5423855 100644 --- a/.github/workflows/release-redeliver.yml +++ b/.github/workflows/release-redeliver.yml @@ -45,7 +45,7 @@ jobs: exit 0 - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: - ref: main + ref: ${{ github.sha }} persist-credentials: false - name: Authorize consumer and pair before token mint shell: bash @@ -120,6 +120,12 @@ jobs: run: | python3 scripts/release_pipeline.py redeliver --tag "$TAG" --consumer "$CONSUMER" \ --output "$RUNNER_TEMP/redelivery.json" + - name: Preserve producing redelivery content + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: release-redelivery-${{ inputs.consumer }}-${{ inputs.tag }}-${{ github.run_id }}-${{ github.run_attempt }} + path: ${{ runner.temp }}/redelivery.json + if-no-files-found: error - name: Report failed redelivery if: ${{ failure() && !cancelled() }} env: diff --git a/scripts/release_pipeline.py b/scripts/release_pipeline.py index b90645b..f0333d7 100644 --- a/scripts/release_pipeline.py +++ b/scripts/release_pipeline.py @@ -8,11 +8,14 @@ import argparse import datetime as dt +import io import os import sys import tempfile import urllib.error import urllib.request +import zipfile +import zlib from pathlib import Path from urllib.parse import quote @@ -41,6 +44,11 @@ # Delay after each reserved attempt: two retries, then an acknowledgement grace # period before a terminal failure. Unsent reservations are released by cleanup. DELIVERY_BACKOFF_MINUTES = (15, 60, 15) +DELIVERY_SOURCE_FILES = ( + "scripts/release_pipeline.py", "scripts/release_contract.py", + "scripts/release_artifacts.py", "scripts/release_pair.py", + "scripts/parity_bundle.py", "scripts/release_sources.py", +) def now(): @@ -455,16 +463,183 @@ def require_consumer_unverified(store, row, event, consumer): require(not verified, "consumer already verified") -def confirm_redelivery_run(client, audit): - """A public asset is evidence only when the hub confirms its trusted run.""" +def _confirm_producing_run(client, audit, *, manual): + """Read the producing attempt, never the mutable latest rerun result.""" + positive(audit["run_id"]) + positive(audit["run_attempt"]) try: - run = client.api(f"repos/{HUB}/actions/runs/{audit['run_id']}") + run = client.api(f"repos/{HUB}/actions/runs/{audit['run_id']}/attempts/{audit['run_attempt']}") except Pending: - raise Invalid("redelivery workflow run unavailable") from None + raise Invalid("delivery producing workflow attempt unavailable (requires Actions: read)") from None + paths = (".github/workflows/release-redeliver.yml",) if manual else ( + ".github/workflows/release-finalize.yml", ".github/workflows/publish.yml") + require(type(run) is dict and type(run.get("repository")) is dict + and type(run.get("head_repository")) is dict, "delivery producing workflow attempt differs") require(type(run) is dict and type(run.get("id")) is int and run["id"] == audit["run_id"] - and run.get("path") == ".github/workflows/release-redeliver.yml" + and type(run.get("run_attempt")) is int and run["run_attempt"] == audit["run_attempt"] + and run.get("path") in paths and run.get("head_branch") == "main" and run.get("status") == "completed" - and run.get("conclusion") == "success", "redelivery workflow run differs") + and run.get("conclusion") == "success" + and run.get("repository", {}).get("full_name") == HUB + and run.get("head_repository", {}).get("full_name") == HUB + and run.get("event") in (("workflow_dispatch",) if manual else ( + "workflow_dispatch", "push", "schedule")), "delivery producing workflow attempt differs") + match(COMMIT, run["head_sha"]) + if manual: + require(type(run.get("actor")) is dict and run["actor"].get("login") == audit["actor"], + "redelivery actor differs") + timestamp = dt.datetime.fromisoformat(audit["timestamp"].replace("Z", "+00:00")) + start = dt.datetime.fromisoformat(run["run_started_at"].replace("Z", "+00:00")) + end = dt.datetime.fromisoformat(run["updated_at"].replace("Z", "+00:00")) + require(start.tzinfo is not None and end.tzinfo is not None, "producing attempt clock is not UTC") + require(start <= timestamp <= end, "delivery timestamp outside producing attempt") + return run + + +def confirm_redelivery_run(client, audit): + """Metadata is one part of proof; callers must also confirm producing bytes.""" + return _confirm_producing_run(client, audit, manual=True) + + +def delivery_context(raw, candidate_raw, data, *, manual, commit=None, read_source=None): + """Bind wire bytes, release inputs and all sender modules to their source.""" + commit = os.environ["GITHUB_SHA"] if commit is None else commit + match(COMMIT, commit) + workflows = (".github/workflows/release-redeliver.yml",) if manual else ( + ".github/workflows/release-finalize.yml", ".github/workflows/publish.yml") + if read_source is None: + root = Path(__file__).resolve().parent.parent + read_source = lambda name: (root / name).read_bytes() + event = payload(raw) + return {"tag": "v" + event["release_version"], "manifest_sha256": sha256(raw), + "candidate_sha256": sha256(candidate_raw) if candidate_raw is not None else None, + "dispatch_sha256": sha256(canonical(data)), "workflow_commit": commit, + "source_sha256": {name: sha256(read_source(name)) + for name in (*DELIVERY_SOURCE_FILES, *workflows)}} + + +def validate_delivery_record(raw, event_id, role, *, manual): + obj = decode(raw) + require(type(obj) is dict and obj.get("event_id") == event_id + and obj.get("consumer") == role and obj.get("state") == "dispatch_accepted", + "accepted delivery identity differs") + schemas = ("pineforge/release-redelivery/v1", "pineforge/release-redelivery/v2", + "pineforge/release-redelivery/v3") if manual else ( + "pineforge/release-delivery/v1", "pineforge/release-delivery/v2") + require(obj.get("schema") in schemas, "delivery evidence schema differs") + fields = ("schema", "event_id", "consumer", "attempt", "state", "run_id", "timestamp") + if manual: + fields += ("actor",) + current = obj["schema"] == schemas[-1] + if current: + fields += ("run_attempt", "context") + if current or (manual and obj["schema"] == "pineforge/release-redelivery/v2"): + fields += ("dispatch_status",) + keys(obj, fields) + positive(obj["attempt"]) + positive(obj["run_id"]) + if not manual: + require(obj["attempt"] <= len(DELIVERY_BACKOFF_MINUTES), "invalid delivery attempt") + if current: + positive(obj["run_attempt"]) + if "dispatch_status" in obj: + require(type(obj["dispatch_status"]) is int and obj["dispatch_status"] == 204, + "redelivery dispatch status differs") + if manual: + match(r"[A-Za-z0-9][A-Za-z0-9-]{0,38}(?:\[bot\])?", obj["actor"]) + match(r"\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ", obj["timestamp"]) + dt.datetime.fromisoformat(obj["timestamp"].replace("Z", "+00:00")) + require(canonical(obj) == raw, "delivery evidence bytes differ") + return obj + + +def _delivery_artifacts(client, run_id): + rows = [] + for page in range(1, 1001): + try: + result = client.api(f"repos/{HUB}/actions/runs/{run_id}/artifacts?per_page=100&page={page}") + except Pending: + raise Invalid("producing artifact inventory unavailable (requires Actions: read)") from None + require(type(result) is dict and type(result.get("artifacts")) is list, + "invalid producing artifact inventory") + rows.extend(result["artifacts"]) + if len(result["artifacts"]) < 100: + return rows + raise Invalid("producing artifact pagination limit exceeded") + + +def _delivery_archive(client, artifact, filename): + positive(artifact["id"]) + require(artifact.get("expired") is False, "producing artifact expired") + try: + archive = client.request(f"https://api.github.com/repos/{HUB}/actions/artifacts/{artifact['id']}/zip", + github=True) + except Pending: + raise Invalid("producing artifact unavailable (requires Actions: read)") from None + # Actions artifacts use the JSON media type (octet-stream returns HTTP 415). + require(artifact.get("digest") == "sha256:" + sha256(archive), "producing artifact digest differs") + try: + with zipfile.ZipFile(io.BytesIO(archive)) as zipped: + require(zipped.namelist() == [filename], "producing artifact inventory differs") + limit = 2 * 1024 * 1024 + require(zipped.getinfo(filename).file_size <= limit, "producing evidence exceeds size limit") + with zipped.open(filename) as entry: + raw = entry.read(limit + 1) + require(len(raw) <= limit, "producing evidence exceeds size limit") + return raw + except (zipfile.BadZipFile, EOFError, RuntimeError, zlib.error): + raise Invalid("producing evidence archive cannot be decoded") from None + + +def confirm_delivery_content(store, row, audit, raw, *, manual): + """A successful unrelated main run cannot authenticate a public audit. + + Old formats have no run_attempt. Only a unique exact-content artifact can + recover it; absent/expired legacy proof stays closed, with no latest-run + fallback. A v1 manual audit remains history only even when corroborated. + """ + client = store.client + tag = row["tag_name"] + kind = "redelivery" if manual else "delivery" + prefix = f"release-{kind}-{audit['consumer']}-{tag}-{audit['run_id']}-" + filename = "redelivery.json" if manual else "delivery.json" + matched = [] + for artifact in _delivery_artifacts(client, audit["run_id"]): + require(type(artifact) is dict and type(artifact.get("name")) is str, + "invalid producing artifact metadata") + name = artifact["name"] + if not name.startswith(prefix): + continue + suffix = name.removeprefix(prefix) + match(r"[1-9][0-9]*", suffix) + attempt = int(suffix) + if "run_attempt" in audit and attempt != audit["run_attempt"]: + continue + if _delivery_archive(client, artifact, filename) == raw: + matched.append((artifact, attempt)) + require(len(matched) == 1, "matching trusted producing content unavailable or ambiguous") + artifact, attempt = matched[0] + bound = {**audit, "run_attempt": attempt} + run = (confirm_redelivery_run(client, bound) if manual else + _confirm_producing_run(client, bound, manual=False)) + producing = artifact.get("workflow_run") + require(type(producing) is dict and type(producing.get("id")) is int + and producing["id"] == audit["run_id"] and producing.get("head_sha") == run["head_sha"], + "artifact producing workflow differs") + manifest_raw = store.read(row, ASSET) + event = payload(manifest_raw) + require(event["event_id"] == audit["event_id"] and tag == "v" + event["release_version"], + "producing release differs") + if "context" in audit: + candidate_raw = store.read(row, CANDIDATE) if EVENT_TYPES[audit["consumer"]] == "release-pair" else None + data = {"event_type": EVENT_TYPES[audit["consumer"]], + "client_payload": pair_payload(manifest_raw, candidate_raw) if candidate_raw is not None else event} + require(EVENT_TYPES[audit["consumer"]] != "release-pair" or candidate_raw is not None, + "producing candidate unavailable") + context = delivery_context( + manifest_raw, candidate_raw, data, manual=manual, commit=run["head_sha"], + read_source=lambda name: client.file(HUB, run["head_sha"], name)) + require(audit["context"] == context, "producing source or payload context differs") def delivery_accepted(store, row, event_id, role): @@ -477,40 +652,26 @@ def delivery_accepted(store, row, event_id, role): if asset["name"].startswith(prefix): evidence.append((asset["name"], store.read(row, asset["name"]))) attempts, audits = [], [] - has_acceptance = raw is not None + has_acceptance = False for asset_name, accepted in evidence: - obj = decode(accepted) - require(type(obj) is dict and obj.get("event_id") == event_id - and obj.get("consumer") == role and obj.get("state") == "dispatch_accepted", - "accepted delivery identity differs") if asset_name == name: + obj = validate_delivery_record(accepted, event_id, role, manual=False) + confirm_delivery_content(store, row, obj, accepted, manual=False) + has_acceptance = True continue # Match the append-only redelivery audit contract before using it to # complete a delivery-only role. Never infer success from its filename. - fields = ("schema", "event_id", "consumer", "attempt", "state", "run_id", "timestamp", "actor") - if obj.get("schema") == "pineforge/release-redelivery/v2": - fields += ("dispatch_status",) - keys(obj, fields) - require(obj["schema"] in ("pineforge/release-redelivery/v1", "pineforge/release-redelivery/v2"), - "redelivery history identity differs") - positive(obj["attempt"]) - positive(obj["run_id"]) - if obj["schema"] == "pineforge/release-redelivery/v2": - require(type(obj["dispatch_status"]) is int and obj["dispatch_status"] == 204, - "redelivery dispatch status differs") + obj = validate_delivery_record(accepted, event_id, role, manual=True) require(asset_name == f"{prefix}{obj['attempt']}.json" and canonical(obj) == accepted, "redelivery history asset differs") - match(r"[A-Za-z0-9][A-Za-z0-9-]{0,38}(?:\[bot\])?", obj["actor"]) - match(r"\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ", obj["timestamp"]) - dt.datetime.fromisoformat(obj["timestamp"].replace("Z", "+00:00")) attempts.append(obj["attempt"]) - audits.append(obj) + audits.append((obj, accepted)) require(sorted(attempts) == list(range(1, len(attempts) + 1)), "invalid redelivery attempt history") - for audit in audits: - confirm_redelivery_run(store.client, audit) + for audit, accepted in audits: + confirm_delivery_content(store, row, audit, accepted, manual=True) # Confirmed v1 audits remain readable history, but lack a recorded # dispatch response and cannot establish compute completion. - if audit["schema"] == "pineforge/release-redelivery/v2": + if audit["schema"] != "pineforge/release-redelivery/v1": has_acceptance = True return has_acceptance @@ -589,6 +750,8 @@ def _attempt_released(row, store, event, role, reservation): def plan_delivery(store, row, event, role): """Reserve at most three deliveries; accepted transport never retries.""" if store.read(row, f"delivery-{role}.json") is not None: + require(delivery_accepted(store, row, event["event_id"], role), + "delivery acceptance has no trusted provenance") return None, False failed_name = f"delivery-failed-{role}.json" failed = store.read(row, failed_name) @@ -677,8 +840,43 @@ def release_delivery_attempts(client, store, tag=""): "run_id": current_run, "run_attempt": current_attempt, "timestamp": now()})) -def redelivery_dispatch(client, target, data): - """Keep the actual dispatch HTTP status, which Client.api discards.""" +def _authorization_variable(client, name): + # These controls are repository variables per the release-chain contract. + # Queue-time vars/env are never fallback authority, including on HTTP 404. + route = f"repos/{HUB}/actions/variables/{name}" + try: + result = client.api(route) + except artifacts.RequestError as error: + raise ConfigurationError( + f"send authorization unavailable: GET {route}; {error}; requires repository Variables: read") from None + except (Pending, Invalid): + raise ConfigurationError( + f"send authorization unavailable: GET {route}; requires repository Variables: read") from None + require(type(result) is dict and result.get("name") == name and type(result.get("value")) is str, + "send authorization variable response differs") + return result["value"] + + +def authorize_delivery(client, consumer, data): + """Reauthorize at the POST boundary, after all queued/public evidence work.""" + require(data["event_type"] == EVENT_TYPES[consumer], "consumer dispatch contract differs") + enabled = configured_roles(_authorization_variable(client, "RELEASE_READY_CONSUMERS")) + require(consumer in enabled, "consumer is not authorized at send") + body = data["client_payload"] + release = version(body["pair_id"] if data["event_type"] == "release-pair" else body["release_version"]) + if consumer in DELIVERY_ONLY: + first = _authorization_variable(client, "RELEASE_COMPUTE_FIRST_PAIR") + try: + allowed = bool(first) and release >= version(first) + except (Invalid, ValueError, pair.RuleError): + raise ConfigurationError("RELEASE_COMPUTE_FIRST_PAIR value was not accepted at send") from None + require(allowed, "consumer pair is not authorized at send") + require(not (release.prerelease and consumer in ("application", "web", "data", "license")), + "consumer is skipped by policy at send") + + +def redelivery_dispatch(client, target, data, *, authority, consumer): + """Authorize the actual POST and keep the actual dispatch HTTP status.""" headers = {"User-Agent": "pineforge-release-reconciler", "Accept": "application/vnd.github+json", "Content-Type": "application/json", "X-GitHub-Api-Version": "2022-11-28"} if client.token: @@ -687,7 +885,9 @@ def redelivery_dispatch(client, target, data): f"https://api.github.com/repos/pineforge-4pass/{target}/dispatches", data=canonical(data), headers=headers, method="POST") try: - with urllib.request.build_opener(SafeRedirect()).open(request, timeout=120) as response: + opener = urllib.request.build_opener(SafeRedirect()) + authorize_delivery(authority, consumer, data) + with opener.open(request, timeout=120) as response: status = response.status require(type(status) is int and status == 204, "redelivery dispatch status differs") return status @@ -734,9 +934,7 @@ def redelivery_source(tag, consumer): public_store = Store(public) accepted = public_store.read(row, f"delivery-{consumer}.json") if accepted is not None: - obj = decode(accepted) - require(obj["event_id"] == event["event_id"] and obj["consumer"] == consumer - and obj["state"] == "dispatch_accepted", "accepted delivery identity differs") + validate_delivery_record(accepted, event["event_id"], consumer, manual=False) # With or without delivery-.json: any verified receipt, a malformed one or two # claiming the same identity refuses; a missing, failed or pending one still repairs. require_consumer_unverified(public_store, row, event, consumer) @@ -748,47 +946,44 @@ def redeliver(client, store, tag, consumer, output): public_store, row, event, client_payload = redelivery_source(tag, consumer) event_type = EVENT_TYPES[consumer] prefix = f"delivery-redelivered-{consumer}-" - actor_pattern = r"[A-Za-z0-9][A-Za-z0-9-]{0,38}(?:\[bot\])?" attempts, audits = [], [] for asset in public_store.get(tag)["assets"]: if not asset["name"].startswith(prefix): continue previous = public_store.read(row, asset["name"]) - obj = decode(previous) - fields = ("schema", "event_id", "consumer", "attempt", "state", "run_id", "timestamp", "actor") - if type(obj) is dict and obj.get("schema") == "pineforge/release-redelivery/v2": - fields += ("dispatch_status",) - keys(obj, fields) - require(obj["schema"] in ("pineforge/release-redelivery/v1", "pineforge/release-redelivery/v2") - and obj["event_id"] == event["event_id"] and obj["consumer"] == consumer - and obj["state"] == "dispatch_accepted", "redelivery history identity differs") - positive(obj["attempt"]) - positive(obj["run_id"]) - if obj["schema"] == "pineforge/release-redelivery/v2": - require(type(obj["dispatch_status"]) is int and obj["dispatch_status"] == 204, - "redelivery dispatch status differs") + obj = validate_delivery_record(previous, event["event_id"], consumer, manual=True) require(asset["name"] == f"{prefix}{obj['attempt']}.json" and canonical(obj) == previous, "redelivery history asset differs") - match(actor_pattern, obj["actor"]) - match(r"\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ", obj["timestamp"]) - dt.datetime.fromisoformat(obj["timestamp"].replace("Z", "+00:00")) attempts.append(obj["attempt"]) - audits.append(obj) + audits.append((obj, previous)) attempts.sort() require(attempts == list(range(1, len(attempts) + 1)), "invalid redelivery attempt history") - for audit in audits: - confirm_redelivery_run(store.client, audit) + for audit, previous in audits: + confirm_delivery_content(store, row, audit, previous, manual=True) attempt = len(attempts) + 1 positive(attempt) run_id = int(os.environ["GITHUB_RUN_ID"]) positive(run_id) - actor = match(actor_pattern, os.environ["GITHUB_ACTOR"]) + run_attempt = int(os.environ["GITHUB_RUN_ATTEMPT"]) + positive(run_attempt) + actor = match(r"[A-Za-z0-9][A-Za-z0-9-]{0,38}(?:\[bot\])?", os.environ["GITHUB_ACTOR"]) target = os.environ.get("TARGET_REPOSITORY", "") match(r"[A-Za-z0-9][A-Za-z0-9_.-]*", target) - dispatch_status = redelivery_dispatch(client, target, {"event_type": event_type, "client_payload": client_payload}) - audit = canonical({"schema": "pineforge/release-redelivery/v2", "event_id": event["event_id"], + data = {"event_type": event_type, "client_payload": client_payload} + manifest_raw = public_store.read(row, ASSET) + candidate_raw = public_store.read(row, CANDIDATE) if event_type == "release-pair" else None + # Reconcile the bytes used for the send with the final source read. A changed + # input cannot acquire an audit for a different event or dispatch body. + require(payload(manifest_raw) == event, "redelivery manifest changed before send") + if event_type == "release-pair": + require(pair_payload(manifest_raw, candidate_raw) == client_payload, + "redelivery candidate changed before send") + context = delivery_context(manifest_raw, candidate_raw, data, manual=True) + dispatch_status = redelivery_dispatch(client, target, data, authority=store.client, consumer=consumer) + audit = canonical({"schema": "pineforge/release-redelivery/v3", "event_id": event["event_id"], "consumer": consumer, "attempt": attempt, "state": "dispatch_accepted", - "run_id": run_id, "dispatch_status": dispatch_status, "timestamp": now(), "actor": actor}) + "run_id": run_id, "run_attempt": run_attempt, "context": context, + "dispatch_status": dispatch_status, "timestamp": now(), "actor": actor}) # No reservation or planner/receipt updates. An unsuccessful append fails # this run; absent an audit asset, the next run sends the same event again. store.put(row, f"{prefix}{attempt}.json", audit) @@ -1064,6 +1259,10 @@ def main(): require(not any(a["name"] == "release-rejected.json" for a in row["assets"]), "release rejected") accepted = Store(public).read(row, f"delivery-{args.consumer}.json") if accepted is not None: + obj = decode(accepted) + obj = validate_delivery_record(accepted, obj["event_id"], args.consumer, manual=False) + confirm_delivery_content(Store(Client(token=os.environ["HUB_TOKEN"])), row, + obj, accepted, manual=False) Path(args.output).write_bytes(accepted) return 0 exhausted = Store(public).read(row, f"delivery-failed-{args.consumer}.json") @@ -1081,17 +1280,25 @@ def main(): "release superseded before dispatch") event_type = EVENT_TYPES[args.consumer] client_payload = event + candidate_raw = None if event_type == "release-pair": candidate_raw = public.download(f"https://github.com/{HUB}/releases/download/{args.tag}/{CANDIDATE}", {"github.com"}) client_payload = pair_payload(raw, candidate_raw) target = os.environ.get("TARGET_REPOSITORY", "") match(r"[A-Za-z0-9][A-Za-z0-9_.-]*", target) + run_id = int(os.environ["GITHUB_RUN_ID"]) + run_attempt = int(os.environ["GITHUB_RUN_ATTEMPT"]) + positive(run_id) + positive(run_attempt) + data = {"event_type": event_type, "client_payload": client_payload} + context = delivery_context(raw, candidate_raw, data, manual=False) + authority = Client(token=os.environ["HUB_TOKEN"]) dispatch_time = now() - client.api(f"repos/pineforge-4pass/{target}/dispatches", method="POST", - data={"event_type": event_type, "client_payload": client_payload}) - Path(args.output).write_bytes(canonical({"schema": "pineforge/release-delivery/v1", "event_id": event["event_id"], + dispatch_status = redelivery_dispatch(client, target, data, authority=authority, consumer=args.consumer) + Path(args.output).write_bytes(canonical({"schema": "pineforge/release-delivery/v2", "event_id": event["event_id"], "consumer": args.consumer, "state": "dispatch_accepted", "attempt": args.attempt, - "run_id": int(os.environ["GITHUB_RUN_ID"]), "timestamp": dispatch_time})) + "run_id": run_id, "run_attempt": run_attempt, "context": context, + "dispatch_status": dispatch_status, "timestamp": dispatch_time})) else: report_issue(client, args.stage + "-failure", "Release chain: " + args.stage + " failed", run_link()) except Pending as error: diff --git a/tests/test_release_binding.py b/tests/test_release_binding.py index 38adc3a..3c3075a 100644 --- a/tests/test_release_binding.py +++ b/tests/test_release_binding.py @@ -127,12 +127,32 @@ def expected_pair_payload(manifest_raw, candidate_raw): } +DELIVERY_COMMIT = "a" * 40 +DELIVERY_CLOCK = "2026-10-23T00:00:30Z" +DELIVERY_SOURCES = ( + "scripts/release_pipeline.py", "scripts/release_contract.py", + "scripts/release_artifacts.py", "scripts/release_pair.py", + "scripts/parity_bundle.py", "scripts/release_sources.py", +) +RELEASE_STORE = pipeline.Store + + class MemoryStore: def __init__(self, *manifests): self.rows, self.assets, self.operations = {}, {}, [] - # Model the hub API independently of the production acceptance checker. + # Transport fixtures are independent of the production acceptance checker. + # Nothing uploaded to the release store acquires producer proof implicitly. self.client = self self.workflow_runs, self.run_reads = {}, [] + self.runs, self.artifacts, self.archives, self.artifact_runs = {}, [], {}, {} + self.reads = [] + self.token = "fixture-hub-token" + self.variables = {"RELEASE_READY_CONSUMERS": ",".join(contract.ROLES), + "RELEASE_COMPUTE_FIRST_PAIR": "1.0.0"} + self.sources = {name: (ROOT / name).read_bytes() for name in ( + *DELIVERY_SOURCES, ".github/workflows/release-finalize.yml", + ".github/workflows/publish.yml", ".github/workflows/release-redeliver.yml")} + self.after_download = None for obj in manifests: tag = "v" + obj["release_version"] self.rows[tag] = {"id": len(self.rows) + 1, "tag_name": tag, "draft": True, @@ -141,7 +161,7 @@ def __init__(self, *manifests): self.operations.clear() def get(self, tag): - return pipeline.Store(self).get(tag) + return RELEASE_STORE(self).get(tag) def pages(self, route): if route != f"repos/{contract.HUB}/releases": @@ -149,12 +169,36 @@ def pages(self, route): return self.releases() def api(self, route, *, method="GET", data=None): + self.reads.append(route) + variables = f"repos/{contract.HUB}/actions/variables/" + if route.startswith(variables): + if method != "GET" or data is not None: + raise AssertionError("send authority must only read") + name = route.removeprefix(variables) + value = self.variables.get(name) + if isinstance(value, Exception): + raise value + if value is None: + raise contract.Pending("published artifact unavailable") + return copy.deepcopy(value) if isinstance(value, dict) else {"name": name, "value": value} run_prefix = f"repos/{contract.HUB}/actions/runs/" if route.startswith(run_prefix): if method != "GET" or data is not None: raise AssertionError("workflow confirmation must be read-only") + suffix = route.removeprefix(run_prefix) + inventory = re.fullmatch(r"([1-9][0-9]*)/artifacts\?per_page=100&page=([1-9][0-9]*)", suffix) + if inventory: + run_id, page = map(int, inventory.groups()) + rows = [a for a in self.artifacts if self.artifact_runs[a["id"]] == run_id] + start = (page - 1) * 100 + return {"total_count": len(rows), "artifacts": copy.deepcopy(rows[start:start + 100])} + selected = re.fullmatch(r"([1-9][0-9]*)(?:/attempts/([1-9][0-9]*))?", suffix) + if selected is None: + raise AssertionError("unmodeled workflow route " + route) + run_id, attempt = selected.groups() self.run_reads.append(route) - run = self.workflow_runs.get(int(route.removeprefix(run_prefix))) + run = (self.workflow_runs.get(int(run_id)) if attempt is None else + self.runs.get((int(run_id), int(attempt)))) if run is None: # Client.request maps GitHub's HTTP 404 to Pending. raise contract.Pending("published artifact unavailable") @@ -178,6 +222,18 @@ def api(self, route, *, method="GET", data=None): raise AssertionError("unmodeled hub route " + route) def request(self, url, **kwargs): + prefix = f"https://api.github.com/repos/{contract.HUB}/actions/artifacts/" + if url.startswith(prefix) and url.endswith("/zip"): + self.reads.append(url) + if kwargs != {"github": True}: + raise AssertionError("artifact ZIP requires the authenticated JSON media type") + artifact_id = int(url.removeprefix(prefix).removesuffix("/zip")) + raw = self.archives.get(artifact_id) + if isinstance(raw, Exception): + raise raw + if raw is None: + raise contract.Pending("published artifact unavailable") + return raw prefix = f"https://api.github.com/repos/{contract.HUB}/releases/assets/" if url.startswith(prefix): asset_id = int(url.removeprefix(prefix)) @@ -187,8 +243,74 @@ def request(self, url, **kwargs): return self.read(row, asset["name"]) raise AssertionError("unmodeled hub asset request " + url) + def file(self, repo, commit, name): + self.reads.append((repo, commit, name)) + if repo != contract.HUB or commit != DELIVERY_COMMIT: + raise AssertionError("source must be read at the producing commit") + return self.sources[name] + + def download(self, url, origins): + prefix = f"https://github.com/{contract.HUB}/releases/download/" + if origins != {"github.com"} or not url.startswith(prefix): + raise AssertionError("unexpected release download") + self.reads.append(url) + tag, name = url.removeprefix(prefix).split("/") + # Public downloads resolve a published tag without listing releases. + raw = self.read(self.api(f"repos/{contract.HUB}/releases/tags/{tag}"), name) + if raw is None: + raise contract.Pending("published artifact unavailable") + if self.after_download: + self.after_download() + return raw + + def producing_run(self, run_id, attempt=1, *, manual=True): + workflow = "release-redeliver.yml" if manual else "release-finalize.yml" + return { + "id": run_id, "run_attempt": attempt, "path": ".github/workflows/" + workflow, + "head_branch": "main", "head_sha": DELIVERY_COMMIT, "status": "completed", + "conclusion": "success", "event": "workflow_dispatch", + "repository": {"full_name": contract.HUB}, + "head_repository": {"full_name": contract.HUB}, + "actor": {"login": "release-operator"}, + "run_started_at": "2026-10-23T00:00:00Z", "updated_at": "2026-10-23T00:01:00Z", + } + + def publish_delivery(self, record, *, manual=True, producing_attempt=None, contents=None, tag=None): + """Seed a separate Actions inventory and ZIP, never a synthetic validator result.""" + run_id = record["run_id"] + attempt = record.get("run_attempt", 1) if producing_attempt is None else producing_attempt + run = self.runs.setdefault((run_id, attempt), self.producing_run(run_id, attempt, manual=manual)) + latest = self.workflow_runs.get(run_id) + if latest is None or latest["run_attempt"] <= attempt: + self.workflow_runs[run_id] = run + number = max(self.archives, default=0) + 1 + archive = io.BytesIO() + filename = "redelivery.json" if manual else "delivery.json" + with zipfile.ZipFile(archive, "w") as zipped: + zipped.writestr(zipfile.ZipInfo(filename, (2026, 10, 23, 0, 0, 0)), + contents if contents is not None else contract.canonical(record)) + raw = archive.getvalue() + self.archives[number] = raw + self.artifact_runs[number] = run_id + kind = "redelivery" if manual else "delivery" + tag = tag or record.get("context", {}).get("tag", "v1.6.0") + self.artifacts.append({ + "id": number, "name": f"release-{kind}-{record['consumer']}-{tag}-{run_id}-{attempt}", + "expired": False, "digest": "sha256:" + contract.sha256(raw), + "workflow_run": {"id": run_id, "head_sha": DELIVERY_COMMIT}, + }) + + def accept_delivery(self, row, event, role="compute", *, run_id=900): + """Explicitly publish corroborating legacy bytes before seeding acceptance.""" + obj = {"schema": "pineforge/release-delivery/v1", "event_id": event["event_id"], + "consumer": role, "attempt": 1, "state": "dispatch_accepted", + "run_id": run_id, "timestamp": DELIVERY_CLOCK} + self.publish_delivery(obj, manual=False, tag=row["tag_name"]) + self.put(row, f"delivery-{role}.json", contract.canonical(obj)) + return obj + def facts(self): - return pipeline.Store(self).facts() + return RELEASE_STORE(self).facts() def releases(self): return list(self.rows.values()) @@ -688,8 +810,7 @@ def test_compute_delivery_completes_a_release_with_only_compute_pending(self): self.assertEqual(pending["consumers"], {**dict.fromkeys(contract.RECEIPT_ROLES, "verified"), "compute": "pending"}) self.assertFalse(pending["complete"]) - self.store.put(self.row, "delivery-compute.json", contract.canonical({ - "event_id": event["event_id"], "consumer": "compute", "state": "dispatch_accepted"})) + self.store.accept_delivery(self.row, event) delivered = pipeline.aggregate(self.store, self.row, event, enabled=contract.ROLES) self.assertEqual(delivered["consumers"], {**dict.fromkeys(contract.RECEIPT_ROLES, "verified"), "compute": "delivered"}) @@ -718,7 +839,9 @@ def test_compute_delivery_refuses_malformed_or_nonobject_acceptance(self): def test_compute_delivery_refuses_wrong_event_consumer_or_verdict(self): """Mutations: omit each of the three acceptance identity checks.""" event = contract.payload(contract.canonical(self.m)) - accepted = {"event_id": event["event_id"], "consumer": "compute", "state": "dispatch_accepted"} + accepted = {"schema": "pineforge/release-delivery/v1", "event_id": event["event_id"], + "consumer": "compute", "state": "dispatch_accepted", "attempt": 1, + "run_id": 900, "timestamp": DELIVERY_CLOCK} cases = ( # Exact reader input, followed by independent mutations of each field. {"consumer": "offline", "state": "failed", "event_id": "sha256:" + "0" * 64}, @@ -748,14 +871,14 @@ def test_compute_manual_acceptance_requires_valid_matching_audit_history(self): (name, contract.canonical({**audit, "consumer": "offline"}), "accepted delivery identity differs"), (name, contract.canonical({**audit, "state": "failed"}), "accepted delivery identity differs"), (name, contract.canonical({**audit, "schema": "pineforge/release-delivery/v1"}), - "redelivery history identity differs"), + "delivery evidence schema differs"), (name, contract.canonical({**audit, "extra": "not-audit-evidence"}), "unexpected object fields"), (name, contract.canonical({**audit, "attempt": 0}), "expected positive integer"), (name, contract.canonical({**audit, "attempt": True}), "expected positive integer"), (name, contract.canonical({**audit, "run_id": 0}), "expected positive integer"), (name, contract.canonical({**audit, "actor": "not/a/login"}), "malformed identity"), (name, contract.canonical({**audit, "timestamp": "not-a-timestamp"}), "malformed identity"), - (name, contract.canonical(audit) + b" ", "redelivery history asset differs"), + (name, contract.canonical(audit) + b" ", "delivery evidence bytes differ"), ("delivery-redelivered-compute-2.json", contract.canonical(audit), "redelivery history asset differs"), ("delivery-redelivered-compute-2.json", contract.canonical({**audit, "attempt": 2}), "invalid redelivery attempt history"), @@ -770,17 +893,17 @@ def test_compute_manual_acceptance_requires_valid_matching_audit_history(self): # Asset upload order does not change a valid, contiguous repair history. store = MemoryStore(self.m) row = store.get("v1.6.0") - store.workflow_runs[900] = { - "id": 900, "path": ".github/workflows/release-redeliver.yml", - "head_branch": "main", "status": "completed", "conclusion": "success"} + store.put(row, contract.ASSET, contract.canonical(self.m)) for attempt in (2, 1): - store.put(row, f"delivery-redelivered-compute-{attempt}.json", - contract.canonical({**audit, "schema": "pineforge/release-redelivery/v2", - "attempt": attempt, "dispatch_status": 204})) + obj = {**audit, "schema": "pineforge/release-redelivery/v2", + "attempt": attempt, "run_id": 899 + attempt, "dispatch_status": 204} + store.publish_delivery(obj) + store.put(row, f"delivery-redelivered-compute-{attempt}.json", contract.canonical(obj)) status = pipeline.aggregate(store, row, event, enabled=("compute",)) self.assertEqual(status["consumers"], {"compute": "delivered"}) self.assertTrue(status["complete"]) - self.assertEqual(store.run_reads, [f"repos/{contract.HUB}/actions/runs/900"] * 2) + self.assertEqual(store.run_reads, [f"repos/{contract.HUB}/actions/runs/{run_id}/attempts/1" + for run_id in (901, 900)]) def test_reader_forged_v1_compute_audit_cannot_complete_exhausted_delivery(self): """Mutation: return bool(evidence) without confirming the audit's run.""" @@ -816,7 +939,9 @@ def test_reader_forged_v1_compute_audit_cannot_complete_exhausted_delivery(self) "^release finalization failed; inspect release incidents$"): pipeline.finalize_all(self.client, self.store, self.registry, "v1.6.0", output) self.assertEqual(contract.decode(output.read_bytes()), []) - self.assertEqual(self.store.run_reads, [f"repos/{contract.HUB}/actions/runs/1"]) + self.assertEqual(self.store.run_reads, []) + self.assertIn(f"repos/{contract.HUB}/actions/runs/1/artifacts?per_page=100&page=1", + self.store.reads) self.assertEqual(self.store.assets, before) self.assertEqual(report.call_args.args[2], "Release v1.6.0: readiness failed") self.assertFalse(report.call_args.kwargs.get("complete", False)) @@ -827,20 +952,20 @@ def test_compute_manual_audits_require_each_workflow_run_binding(self): legacy = {"schema": "pineforge/release-redelivery/v1", "event_id": event["event_id"], "consumer": "compute", "attempt": 1, "state": "dispatch_accepted", "run_id": 900, "timestamp": "2026-10-23T00:00:00Z", "actor": "release-operator"} - success = {"id": 900, "path": ".github/workflows/release-redeliver.yml", - "head_branch": "main", "status": "completed", "conclusion": "success"} + success = self.store.producing_run(900) + differs = "delivery producing workflow attempt differs" cases = ( ("other workflow", {**success, "path": ".github/workflows/release-finalize.yml"}, - "redelivery workflow run differs"), - ("other branch", {**success, "head_branch": "repair"}, "redelivery workflow run differs"), - ("failure", {**success, "conclusion": "failure"}, "redelivery workflow run differs"), + differs), + ("other branch", {**success, "head_branch": "repair"}, differs), + ("failure", {**success, "conclusion": "failure"}, differs), ("in progress", {**success, "status": "in_progress", "conclusion": None}, - "redelivery workflow run differs"), - ("unfinished success", {**success, "status": "in_progress"}, "redelivery workflow run differs"), - ("other id", {**success, "id": 901}, "redelivery workflow run differs"), - ("boolean id", {**success, "id": True}, "redelivery workflow run differs"), - ("not an object", [], "redelivery workflow run differs"), - ("404", None, "redelivery workflow run unavailable"), + differs), + ("unfinished success", {**success, "status": "in_progress"}, differs), + ("other id", {**success, "id": 901}, differs), + ("boolean id", {**success, "id": True}, differs), + ("not an object", [], differs), + ("404", None, r"delivery producing workflow attempt unavailable \(requires Actions: read\)"), ("403", contract.Invalid("artifact request failed (HTTP 403)"), r"artifact request failed \(HTTP 403\)"), ("500", contract.Invalid("artifact request failed (HTTP 500)"), @@ -854,13 +979,16 @@ def test_compute_manual_audits_require_each_workflow_run_binding(self): with self.subTest(schema=audit["schema"], case=label): store = MemoryStore(self.m) row = store.get("v1.6.0") - if run is not None: - store.workflow_runs[900] = run + store.put(row, contract.ASSET, contract.canonical(self.m)) + store.publish_delivery(audit) + # Keep real matching bytes while changing only the run response. + store.runs[900, 1] = run + store.workflow_runs[900] = run store.put(row, "delivery-redelivered-compute-1.json", contract.canonical(audit)) before = copy.deepcopy(store.assets) with self.assertRaisesRegex(contract.Invalid, "^" + message + "$"): pipeline.aggregate(store, row, event, enabled=("compute",)) - self.assertEqual(store.run_reads, [f"repos/{contract.HUB}/actions/runs/900"]) + self.assertEqual(store.run_reads, [f"repos/{contract.HUB}/actions/runs/900/attempts/1"]) self.assertEqual(store.assets, before) def test_confirmed_legacy_audit_is_history_only_until_a_v2_dispatch(self): @@ -869,25 +997,25 @@ def test_confirmed_legacy_audit_is_history_only_until_a_v2_dispatch(self): audit = {"schema": "pineforge/release-redelivery/v1", "event_id": event["event_id"], "consumer": "compute", "attempt": 1, "state": "dispatch_accepted", "run_id": 900, "timestamp": "2026-10-23T00:00:00Z", "actor": "release-operator"} - self.store.workflow_runs[900] = { - "id": 900, "path": ".github/workflows/release-redeliver.yml", - "head_branch": "main", "status": "completed", "conclusion": "success"} + self.store.put(self.row, contract.ASSET, contract.canonical(self.m)) + self.store.publish_delivery(audit) self.store.put(self.row, "delivery-redelivered-compute-1.json", contract.canonical(audit)) status = pipeline.aggregate(self.store, self.row, event, enabled=("compute",)) self.assertEqual(status["consumers"], {"compute": "pending"}) self.assertFalse(status["complete"]) - self.assertEqual(self.store.run_reads, [f"repos/{contract.HUB}/actions/runs/900"]) - self.store.workflow_runs[901] = {**self.store.workflow_runs[900], "id": 901} - self.store.put(self.row, "delivery-redelivered-compute-2.json", contract.canonical({ + self.assertEqual(self.store.run_reads, [f"repos/{contract.HUB}/actions/runs/900/attempts/1"]) + accepted = { **audit, "schema": "pineforge/release-redelivery/v2", "attempt": 2, - "run_id": 901, "dispatch_status": 204})) + "run_id": 901, "dispatch_status": 204} + self.store.publish_delivery(accepted) + self.store.put(self.row, "delivery-redelivered-compute-2.json", contract.canonical(accepted)) before = copy.deepcopy(self.store.assets) self.store.run_reads.clear() status = pipeline.aggregate(self.store, self.row, event, enabled=("compute",)) self.assertEqual(status["consumers"], {"compute": "delivered"}) self.assertTrue(status["complete"]) - self.assertEqual(self.store.run_reads, [f"repos/{contract.HUB}/actions/runs/900", - f"repos/{contract.HUB}/actions/runs/901"]) + self.assertEqual(self.store.run_reads, [f"repos/{contract.HUB}/actions/runs/900/attempts/1", + f"repos/{contract.HUB}/actions/runs/901/attempts/1"]) self.assertEqual(self.store.assets, before) def test_v2_compute_audit_requires_recorded_204_and_a_positive_run_id(self): @@ -916,8 +1044,7 @@ def test_compute_is_never_superseded_before_or_after_delivery(self): event = self.finish() for accepted in (False, True): if accepted: - self.store.put(self.row, "delivery-compute.json", contract.canonical({ - "event_id": event["event_id"], "consumer": "compute", "state": "dispatch_accepted"})) + self.store.accept_delivery(self.row, event) with self.subTest(accepted=accepted): status = pipeline.aggregate(self.store, self.row, event, enabled=contract.ROLES, newest=pair.parse("1.6.1")) @@ -930,20 +1057,20 @@ def test_compute_prerelease_delivery_completes_while_application_keeps_policy_sk manifest = fixture_manifest("1.6.0-rc.1") store = MemoryStore(manifest) row = store.get("v1.6.0-rc.1") - event = contract.payload(contract.canonical(manifest)) + raw = contract.canonical(manifest) + store.put(row, contract.ASSET, raw) + event = contract.payload(raw) pending = pipeline.aggregate(store, row, event, enabled=("compute", "application")) self.assertEqual(pending["consumers"], {"compute": "pending", "application": "skipped_by_policy"}) self.assertFalse(pending["complete"]) - store.put(row, "delivery-compute.json", contract.canonical({ - "event_id": event["event_id"], "consumer": "compute", "state": "dispatch_accepted"})) + store.accept_delivery(row, event) delivered = pipeline.aggregate(store, row, event, enabled=("compute", "application")) self.assertEqual(delivered["consumers"], {"compute": "delivered", "application": "skipped_by_policy"}) self.assertTrue(delivered["complete"]) def test_compute_delivery_closes_finalizer_incident_without_a_consumer_receipt(self): event = self.finish() - self.store.put(self.row, "delivery-compute.json", contract.canonical({ - "event_id": event["event_id"], "consumer": "compute", "state": "dispatch_accepted"})) + self.store.accept_delivery(self.row, event) with tempfile.TemporaryDirectory() as tmp, patch.dict(os.environ, { "RELEASE_READY_CONSUMERS": "compute", "RELEASE_READY_EXCLUDE": self.exclude("compute"), "RELEASE_COMPUTE_FIRST_PAIR": "1.6.0", @@ -1285,7 +1412,7 @@ def api(self, route, **kwargs): def test_finalize_all_never_redispatches_after_delivery_acceptance(self): event = self.finish() - self.store.put(self.row, "delivery-offline.json", contract.canonical({"event_id": event["event_id"], "state": "dispatch_accepted"})) + self.store.accept_delivery(self.row, event, "offline") with tempfile.TemporaryDirectory() as tmp, patch.dict(os.environ, { "RELEASE_READY_CONSUMERS": "offline", "RELEASE_READY_EXCLUDE": self.exclude("offline"), "GITHUB_RUN_ID": "100", "GITHUB_RUN_ATTEMPT": "1", @@ -1359,11 +1486,7 @@ def test_release_delivery_attempts_preserves_accepted_delivery(self): event = self.finish() with patch.dict(os.environ, {"GITHUB_RUN_ID": "100", "GITHUB_RUN_ATTEMPT": "1"}): pipeline.plan_delivery(self.store, self.row, event, "offline") - self.store.put(self.row, "delivery-offline.json", contract.canonical({ - "schema": "pineforge/release-delivery/v1", "event_id": event["event_id"], - "consumer": "offline", "state": "dispatch_accepted", "attempt": 1, - "run_id": 100, "timestamp": pipeline.now(), - })) + self.store.accept_delivery(self.row, event, "offline", run_id=100) before = dict(self.store.assets) operations = list(self.store.operations) pipeline.release_delivery_attempts(self.client, self.store, self.row["tag_name"]) @@ -1463,11 +1586,16 @@ def test_plan_delivery_reads_legacy_reservation_release_markers(self): ({"consumer": "offline", "tag": "v1.6.0", "attempt": 1}, False)) def test_queued_delivery_rechecks_acceptance_and_exhaustion_before_sending(self): - self.finish() + event = self.finish() for asset, state in (("delivery-offline.json", "dispatch_accepted"), ("delivery-failed-hosted.json", "failed")): role = "offline" if state == "dispatch_accepted" else "hosted" - self.store.put(self.row, asset, contract.canonical({"consumer": role, "state": state})) - with tempfile.TemporaryDirectory() as tmp, patch.object(pipeline, "Client", return_value=self.store): + if state == "dispatch_accepted": + self.store.accept_delivery(self.row, event, role) + else: + self.store.put(self.row, asset, contract.canonical({"consumer": role, "state": state})) + with tempfile.TemporaryDirectory() as tmp, \ + patch.dict(os.environ, {"HUB_TOKEN": self.store.token}), \ + patch.object(pipeline, "Client", return_value=self.store): output = Path(tmp) / "delivery.json" with patch.object(sys, "argv", ["release_pipeline.py", "dispatch", "--tag", "v1.6.0", "--consumer", role, "--attempt", "3", "--output", str(output)]): @@ -1497,32 +1625,50 @@ def ready_store(self, *manifests): return store def send(self, store, consumer, tag="v1.6.0"): - sender = unittest.mock.Mock(spec=["api"]) - - def download(url, origins): - prefix = "https://github.com/pineforge-4pass/pineforge-release/releases/download/" - self.assertEqual(origins, {"github.com"}) - self.assertTrue(url.startswith(prefix)) - release_tag, asset = url.removeprefix(prefix).split("/") - self.assertIn(asset, ("release-manifest.json", "release-candidate.json")) - raw = store.assets.get((release_tag, asset)) - if raw is None: - raise contract.Pending("published asset unavailable") - return raw + sender = unittest.mock.Mock(spec=["open"]) + target = artifacts.Client(token="fixture-target-token") + def accepted(request, *, timeout): + response = io.BytesIO() + response.status = 204 + return response + + def client(token=None): + if token is None: + return target + self.assertIn(token, ("", store.token)) + return store + + sender.open.side_effect = accepted with tempfile.TemporaryDirectory() as tmp, patch.dict(os.environ, { - "TARGET_REPOSITORY": "fixture", "GITHUB_RUN_ID": "100"}), \ - patch.object(pipeline, "Client", side_effect=[sender, store]) as clients, \ - patch.object(store, "download", side_effect=download, create=True) as fetch, \ + "TARGET_REPOSITORY": "fixture", "GITHUB_RUN_ID": "100", "GITHUB_RUN_ATTEMPT": "1", + "GITHUB_SHA": DELIVERY_COMMIT, "HUB_TOKEN": store.token}), \ + patch.object(pipeline, "Client", side_effect=client) as clients, \ + patch.object(pipeline.urllib.request, "build_opener", return_value=sender), \ + patch.object(pipeline, "now", return_value=DELIVERY_CLOCK), \ + patch.object(store, "download", wraps=store.download) as fetch, \ patch.object(store, "pages", wraps=store.pages) as scan: output = Path(tmp) / "delivery.json" with patch.object(sys, "argv", ["release_pipeline.py", "dispatch", "--tag", tag, "--consumer", consumer, "--output", str(output)]): result = pipeline.main() written = output.read_bytes() if output.exists() else None - self.assertEqual(clients.call_args_list, [unittest.mock.call(), unittest.mock.call(token="")]) + expected = [unittest.mock.call(), unittest.mock.call(token="")] + if sender.open.called: + expected.append(unittest.mock.call(token=store.token)) + self.assertEqual(clients.call_args_list, expected) return result, sender, written, fetch.call_args_list, scan.call_args_list + def assert_sent(self, sender, data): + sender.open.assert_called_once() + request = sender.open.call_args.args[0] + self.assertEqual(request.full_url, "https://api.github.com/repos/pineforge-4pass/fixture/dispatches") + self.assertEqual(request.get_method(), "POST") + self.assertEqual(request.get_header("Authorization"), "Bearer fixture-target-token") + self.assertEqual(sender.open.call_args.kwargs, {"timeout": 120}) + self.assertEqual(request.data, contract.canonical(data)) + return contract.decode(request.data) + def test_application_dispatch_preserves_release_ready_v1_without_an_opt_in(self): """Mutation: map application to release-pair before its receiver is ready.""" ready_raw = (ROOT / "tests/fixtures/release-ready.json").read_bytes() @@ -1533,9 +1679,8 @@ def test_application_dispatch_preserves_release_ready_v1_without_an_opt_in(self) with patch.dict(os.environ, {"RELEASE_READY_CONSUMERS": "application"}, clear=True): result, sender, written, fetches, _ = self.send(store, "application") self.assertEqual(result, 0) - sender.api.assert_called_once_with("repos/pineforge-4pass/fixture/dispatches", method="POST", - data={"event_type": "pineforge-release", "client_payload": json.loads(ready_raw)}) - sent = sender.api.call_args.kwargs["data"]["client_payload"] + sent = self.assert_sent(sender, { + "event_type": "pineforge-release", "client_payload": json.loads(ready_raw)})["client_payload"] self.assertEqual(contract.canonical(sent), ready_raw) self.assertEqual(sent["schema"], "pineforge/release-ready/v1") self.assertEqual(set(sent), {"schema", "event_id", "release_version", "prerelease", "hub_commit", @@ -1554,9 +1699,9 @@ def test_pair_dispatch_posts_release_pair_with_exact_eight_key_payload(self): raw, candidate_raw = store.assets[(tag, contract.ASSET)], store.assets[(tag, pipeline.CANDIDATE)] result, sender, written, fetches, _ = self.send(store, consumer, tag) self.assertEqual(result, 0) - sender.api.assert_called_once_with("repos/pineforge-4pass/fixture/dispatches", method="POST", - data={"event_type": "release-pair", "client_payload": expected_pair_payload(raw, candidate_raw)}) - self.assertEqual(len(sender.api.call_args.kwargs["data"]["client_payload"]), 8) + sent = self.assert_sent(sender, { + "event_type": "release-pair", "client_payload": expected_pair_payload(raw, candidate_raw)}) + self.assertEqual(len(sent["client_payload"]), 8) self.assertEqual({call.args[0].rsplit("/", 1)[-1] for call in fetches}, {"release-manifest.json", "release-candidate.json"}) self.assertEqual(json.loads(written)["consumer"], consumer) @@ -1569,9 +1714,8 @@ def test_legacy_dispatch_keeps_full_manifest_payload_bytes_for_every_other_role( store = self.ready_store(fixture_manifest()) result, sender, written, fetches, _ = self.send(store, consumer) self.assertEqual(result, 0) - sent = sender.api.call_args.kwargs["data"] - sender.api.assert_called_once_with("repos/pineforge-4pass/fixture/dispatches", method="POST", - data={"event_type": "pineforge-release", "client_payload": json.loads(ready_raw)}) + sent = self.assert_sent(sender, { + "event_type": "pineforge-release", "client_payload": json.loads(ready_raw)}) self.assertEqual(contract.canonical(sent["client_payload"]), ready_raw) self.assertEqual(len(fetches), 1) self.assertTrue(fetches[0].args[0].endswith("/release-manifest.json")) @@ -1584,12 +1728,12 @@ def test_compute_dispatch_accepts_older_pairs_while_receipt_roles_refuse(self): result, sender, written, _, scans = self.send(store, consumer) if consumer == "compute": self.assertEqual(result, 0) - sender.api.assert_called_once() + sender.open.assert_called_once() self.assertEqual(json.loads(written)["consumer"], "compute") self.assertEqual(scans, []) else: self.assertEqual(result, 1) - sender.api.assert_not_called() + sender.open.assert_not_called() self.assertIsNone(written) self.assertTrue(scans) @@ -1603,7 +1747,7 @@ def test_compute_dispatch_without_a_candidate_stays_pending(self): store.operations.clear() result, sender, written, fetches, _ = self.send(store, "compute") self.assertEqual(result, 3) - sender.api.assert_not_called() + sender.open.assert_not_called() self.assertIsNone(written) self.assertEqual(store.assets, before) self.assertEqual(store.operations, []) @@ -1617,13 +1761,13 @@ def test_pair_dispatch_refuses_candidate_mismatch_before_posting(self): store.assets[("v1.6.0", pipeline.CANDIDATE)] = contract.canonical(built) result, sender, written, _, _ = self.send(store, "compute") self.assertEqual(result, 1) - sender.api.assert_not_called() + sender.open.assert_not_called() self.assertIsNone(written) result, sender, written, fetches, _ = self.send(store, "application") self.assertEqual(result, 0) - sender.api.assert_called_once_with("repos/pineforge-4pass/fixture/dispatches", method="POST", - data={"event_type": "pineforge-release", - "client_payload": json.loads((ROOT / "tests/fixtures/release-ready.json").read_bytes())}) + self.assert_sent(sender, { + "event_type": "pineforge-release", + "client_payload": json.loads((ROOT / "tests/fixtures/release-ready.json").read_bytes())}) self.assertEqual(len(fetches), 1) self.assertTrue(fetches[0].args[0].endswith("/release-manifest.json")) self.assertEqual(json.loads(written)["consumer"], "application") @@ -1633,7 +1777,7 @@ def test_compute_dispatch_still_refuses_rejected_pairs(self): store.put(store.get("v1.6.0"), "release-rejected.json", contract.canonical({"state": "rejected"})) result, sender, written, fetches, _ = self.send(store, "compute") self.assertEqual(result, 1) - sender.api.assert_not_called() + sender.open.assert_not_called() self.assertIsNone(written) self.assertEqual(fetches, []) diff --git a/tests/test_release_pipeline.py b/tests/test_release_pipeline.py new file mode 100644 index 0000000..080c283 --- /dev/null +++ b/tests/test_release_pipeline.py @@ -0,0 +1,372 @@ +"""Producer-content and send-boundary authorization controls for ordinary CI. + +Only transport and storage are fixtures. The real CLI, authorizer, serializer, +artifact reader and aggregation rules execute in every control. +""" +from __future__ import annotations + +import copy +import io +import os +import sys +import tempfile +import unittest +import zipfile +from contextlib import contextmanager +from pathlib import Path +from unittest.mock import patch + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / "scripts")) +import release_contract as contract # noqa: E402 +import release_pipeline as pipeline # noqa: E402 +from test_release_binding import (DELIVERY_SOURCES as SOURCE_FILES, # noqa: E402 + MemoryStore, fixture_manifest) + +TAG = "v1.6.0" +CLOCK = "2026-10-23T00:00:30Z" +COMMIT = "a" * 40 + + +class DeliveryAuthorityTest(unittest.TestCase): + def setUp(self): + self.manifest = fixture_manifest() + self.store = MemoryStore(self.manifest) + self.row = self.store.rows[TAG] + self.row.update(draft=False, prerelease=False) + self.raw = contract.canonical(self.manifest) + self.store.put(self.row, contract.ASSET, self.raw) + self.event = contract.payload(self.raw) + self.hub = self.store + self.hub.variables = {"RELEASE_READY_CONSUMERS": "application,compute", + "RELEASE_COMPUTE_FIRST_PAIR": "1.6.0"} + self.sent = [] + self.target = self + self.token = "fixture-target-token" + self.response_status = 204 + tmp = tempfile.TemporaryDirectory() + self.addCleanup(tmp.cleanup) + self.output = Path(tmp.name) / "delivery.json" + env = {"GH_TOKEN": self.token, "HUB_TOKEN": self.hub.token, + "GITHUB_RUN_ID": "901", "GITHUB_RUN_ATTEMPT": "2", + "GITHUB_SHA": COMMIT, "GITHUB_ACTOR": "release-operator", + "TARGET_REPOSITORY": "fixture-target", + "RELEASE_READY_CONSUMERS": "application,compute", + "RELEASE_COMPUTE_FIRST_PAIR": "1.6.0"} + self.start(patch.dict(os.environ, env, clear=True)) + self.start(patch.object(pipeline, "now", return_value=CLOCK)) + self.start(patch.object(pipeline, "age_minutes", return_value=1000)) + self.start(patch.object(pipeline, "Client", side_effect=self.client)) + self.start(patch.object(pipeline.urllib.request, "build_opener", return_value=self)) + + def start(self, patcher): + self.addCleanup(patcher.stop) + return patcher.start() + + def client(self, token=None): + if token is None: + return self.target + self.assertIn(token, ("", self.hub.token)) + return self.hub + + def api(self, route, *, method="GET", data=None): + # The old automatic sender uses Client.api; retain this baseline transport + # so the red control fails on its assertion, rather than a fixture error. + self.assertEqual((route, method), ("repos/pineforge-4pass/fixture-target/dispatches", "POST")) + self.sent.append(contract.canonical(data)) + return None + + def open(self, request, timeout): + self.assertEqual(request.full_url, "https://api.github.com/repos/pineforge-4pass/fixture-target/dispatches") + self.sent.append(request.data) + response = io.BytesIO() + response.status = self.response_status + return response + + def invoke(self, consumer, *, manual=False): + if manual: + return pipeline.redeliver(self.target, self.store, TAG, consumer, self.output) + with patch.object(sys, "argv", [ + "release_pipeline.py", "dispatch", "--tag", TAG, "--consumer", consumer, + "--output", str(self.output)]): + return pipeline.main() + + def record(self, *, consumer="compute", manual=True, legacy=False): + record = {"schema": "pineforge/release-redelivery/v2" if manual else "pineforge/release-delivery/v1", + "event_id": self.event["event_id"], "consumer": consumer, "attempt": 1, + "state": "dispatch_accepted", "run_id": 900, "timestamp": CLOCK} + if manual: + record.update(actor="release-operator", dispatch_status=204) + if legacy: + return record + candidate = self.store.read(self.row, contract.CANDIDATE) if consumer == "compute" else None + data = {"event_type": contract.EVENT_TYPES[consumer], + "client_payload": contract.pair_payload(self.raw, candidate) if candidate else self.event} + workflows = (".github/workflows/release-redeliver.yml",) if manual else ( + ".github/workflows/release-finalize.yml", ".github/workflows/publish.yml") + record.update( + schema="pineforge/release-redelivery/v3" if manual else "pineforge/release-delivery/v2", + run_attempt=1, dispatch_status=204, + context={"tag": TAG, "manifest_sha256": contract.sha256(self.raw), + "candidate_sha256": contract.sha256(candidate) if candidate else None, + "dispatch_sha256": contract.sha256(contract.canonical(data)), + "workflow_commit": COMMIT, + "source_sha256": {name: contract.sha256(self.hub.sources[name]) + for name in (*SOURCE_FILES, *workflows)}}) + return record + + def seed(self, record, *, manual=True): + name = (f"delivery-redelivered-{record['consumer']}-{record['attempt']}.json" + if manual else f"delivery-{record['consumer']}.json") + self.store.put(self.row, name, contract.canonical(record)) + + @contextmanager + def transport_case(self): + """Keep each control independent, including when a base assertion fails.""" + names = ("assets", "operations", "runs", "workflow_runs", "artifacts", "archives", + "artifact_runs", "variables", "sources", "reads", "run_reads") + saved = copy.deepcopy({name: getattr(self.store, name) for name in names}) + listed = copy.deepcopy(self.row["assets"]) + try: + yield + finally: + for name, value in saved.items(): + setattr(self.store, name, value) + self.row["assets"] = listed + self.hub.after_download = None + self.sent.clear() + self.output.unlink(missing_ok=True) + + def test_same_metadata_wrong_producing_content_cannot_complete_compute(self): + forged = self.record(legacy=True) + real = {**forged, "event_id": "sha256:" + "f" * 64} + self.hub.publish_delivery(real) + self.seed(forged) + with self.assertRaises(contract.Invalid): + pipeline.aggregate(self.store, self.row, self.event, enabled=("compute",)) + + def test_three_field_automatic_acceptance_has_no_trusted_provenance(self): + forged = {"event_id": self.event["event_id"], "consumer": "compute", "state": "dispatch_accepted"} + self.seed(forged, manual=False) + with self.assertRaises(contract.Invalid): + pipeline.aggregate(self.store, self.row, self.event, enabled=("compute",)) + with self.assertRaises(contract.Invalid): + pipeline.plan_delivery(self.store, self.row, self.event, "compute") + self.assertEqual(self.invoke("compute"), 1) + self.assertEqual(self.sent, []) + self.assertFalse(self.output.exists()) + + def test_legacy_automatic_acceptance_needs_matching_producer_bytes(self): + record = self.record(manual=False, legacy=True) + self.hub.publish_delivery(record, manual=False, contents=contract.canonical({**record, "attempt": 2})) + self.seed(record, manual=False) + with self.assertRaises(contract.Invalid): + pipeline.aggregate(self.store, self.row, self.event, enabled=("compute",)) + self.hub.artifacts.clear() + self.hub.archives.clear() + self.hub.publish_delivery(record, manual=False) + result = pipeline.aggregate(self.store, self.row, self.event, enabled=("compute",)) + self.assertEqual(result["consumers"], {"compute": "delivered"}) + + def test_revocation_after_planning_stops_both_send_paths(self): + for manual in (False, True): + for role in ("application", "compute"): + with self.subTest(manual=manual, role=role), self.transport_case(): + self.hub.variables["RELEASE_READY_CONSUMERS"] = "application,compute" + planned, failed = pipeline.plan_delivery(self.store, self.row, self.event, role) + self.assertFalse(failed) + self.assertIsNotNone(planned) + self.hub.after_download = lambda: self.hub.variables.update(RELEASE_READY_CONSUMERS="offline") + if manual: + with self.assertRaises(contract.Invalid): + self.invoke(role, manual=True) + else: + self.assertEqual(self.invoke(role), 1) + self.assertEqual(self.sent, []) + self.assertFalse(self.output.exists()) + + def test_changed_compute_floor_and_unknown_authority_deny_at_send(self): + denied = pipeline.artifacts.RequestError("artifact request failed (HTTP 403)") + cases = [("compute", {"RELEASE_COMPUTE_FIRST_PAIR": floor}) + for floor in ("1.6.1", "", "bad", None, denied)] + cases += [(role, {"RELEASE_READY_CONSUMERS": value}) + for role in ("application", "compute") + for value in (None, denied, "application,application", + {"name": "OTHER_VARIABLE", "value": "application,compute"}, + {"name": "RELEASE_READY_CONSUMERS", "value": True})] + for manual in (False, True): + for role, changes in cases: + with self.subTest(manual=manual, role=role, changes=changes), self.transport_case(): + self.hub.variables = {"RELEASE_READY_CONSUMERS": "application,compute", + "RELEASE_COMPUTE_FIRST_PAIR": "1.6.0"} + self.hub.after_download = lambda: self.hub.variables.update(changes) + if manual: + with self.assertRaises(contract.Invalid): + self.invoke(role, manual=True) + else: + self.assertEqual(self.invoke(role), 1) + self.assertEqual(self.sent, []) + self.assertFalse(self.output.exists()) + + def test_authorized_controls_preserve_wire_bytes_and_record_exact_attempt(self): + for manual in (False, True): + for role in ("application", "compute"): + with self.subTest(manual=manual, role=role), self.transport_case(): + result = self.invoke(role, manual=manual) + if not manual: + self.assertEqual(result, 0) + written = contract.decode(self.output.read_bytes()) + expected = self.record(consumer=role, manual=manual) + expected.update(run_id=901, run_attempt=2) + self.assertEqual(written, expected) + self.assertEqual(len(self.sent), 1) + self.assertEqual(contract.sha256(self.sent[0]), written["context"]["dispatch_sha256"]) + self.assertIn(f"repos/{contract.HUB}/actions/variables/RELEASE_READY_CONSUMERS", self.hub.reads) + self.assertNotIn(b"fixture-target", self.output.read_bytes()) + + def test_producing_attempt_survives_a_later_failed_rerun(self): + self.check_rerun(legacy=True) + + def test_recorded_attempt_survives_a_later_failed_rerun(self): + self.check_rerun(legacy=False) + + def check_rerun(self, *, legacy): + for manual in (False, True): + with self.subTest(manual=manual, legacy=legacy), self.transport_case(): + self.hub.artifacts.clear() + self.hub.archives.clear() + self.hub.artifact_runs.clear() + self.hub.runs.clear() + self.hub.workflow_runs.clear() + record = self.record(manual=manual, legacy=legacy) + self.hub.publish_delivery(record, manual=manual, tag=TAG) + failed = {**self.hub.runs[900, 1], "run_attempt": 2, "conclusion": "failure"} + self.hub.runs[900, 2] = failed + self.hub.workflow_runs[900] = failed + self.seed(record, manual=manual) + try: + result = pipeline.aggregate(self.store, self.row, self.event, enabled=("compute",)) + except contract.Invalid as error: + self.fail(f"successful producing attempt was invalidated by a later failed rerun: {error}") + self.assertEqual(result["consumers"], {"compute": "delivered"}) + self.assertIn(f"repos/{contract.HUB}/actions/runs/900/attempts/1", self.hub.reads) + self.assertNotIn(f"repos/{contract.HUB}/actions/runs/900", self.hub.reads) + + def test_replayed_attempt_or_missing_binding_cannot_complete(self): + original = self.record() + self.hub.publish_delivery(original) + cases = ({**original, "run_attempt": 2}, + {**original, "run_attempt": 0}, + {**original, "run_attempt": True}, + {**original, "attempt": 2}, + *[{k: v for k, v in original.items() if k != field} + for field in ("run_id", "run_attempt", "context", "dispatch_status", "schema")]) + for record in cases: + with self.subTest(record=record): + self.store.assets = {key: raw for key, raw in self.store.assets.items() + if not key[1].startswith("delivery-redelivered-")} + self.row["assets"] = [a for a in self.row["assets"] + if not a["name"].startswith("delivery-redelivered-")] + self.seed(record) + with self.assertRaises(contract.Invalid): + pipeline.aggregate(self.store, self.row, self.event, enabled=("compute",)) + + def test_trusted_content_still_requires_exact_sources_and_run_context(self): + for manual in (False, True): + with self.subTest(manual=manual), self.transport_case(): + self.check_producing_context(manual=manual) + + def check_producing_context(self, *, manual): + original = self.record(manual=manual) + self.hub.publish_delivery(original, manual=manual) + self.seed(original, manual=manual) + self.assertEqual(pipeline.aggregate(self.store, self.row, self.event, enabled=("compute",))["consumers"], + {"compute": "delivered"}) + run = copy.deepcopy(self.hub.runs[900, 1]) + changes_to_run = [ + {"run_attempt": 2}, {"head_branch": "side"}, {"event": "pull_request"}, + {"repository": {"full_name": "other/repo"}}, + {"head_repository": {"full_name": "other/repo"}}, {"conclusion": "failure"}, + {"path": ".github/workflows/release-finalize.yml" if manual else + ".github/workflows/release-redeliver.yml"}, + {"run_started_at": "2026-10-23T00:00:31Z"}, {"updated_at": "2026-10-23T00:00:29Z"}, + ] + if manual: + changes_to_run += [{"actor": {"login": "other"}}, {"event": "push"}] + else: + for changes in ({"path": ".github/workflows/publish.yml"}, + {"event": "schedule"}, {"event": "push"}): + with self.subTest(allowed_run=changes): + self.hub.runs[900, 1] = {**run, **changes} + self.assertEqual(pipeline.aggregate( + self.store, self.row, self.event, enabled=("compute",))["consumers"], + {"compute": "delivered"}) + for changes in changes_to_run: + with self.subTest(changes=changes): + self.hub.runs[900, 1] = {**run, **changes} + with self.assertRaises(contract.Invalid): + pipeline.aggregate(self.store, self.row, self.event, enabled=("compute",)) + self.hub.runs[900, 1] = run + for name in original["context"]["source_sha256"]: + with self.subTest(source=name): + source = self.hub.sources[name] + self.hub.sources[name] += b"\n# different producer source\n" + try: + with self.assertRaises(contract.Invalid): + pipeline.aggregate(self.store, self.row, self.event, enabled=("compute",)) + finally: + self.hub.sources[name] = source + for field, changed in (("tag", "v1.6.1"), ("manifest_sha256", "b" * 64), + ("candidate_sha256", None), ("dispatch_sha256", "b" * 64), + ("workflow_commit", "b" * 40), ("source_sha256", {})): + with self.subTest(context=field): + record = {**original, "context": {**original["context"], field: changed}} + self.hub.artifacts.clear() + self.hub.archives.clear() + self.hub.artifact_runs.clear() + self.hub.publish_delivery(record, manual=manual, tag=TAG) + name = "delivery-redelivered-compute-1.json" if manual else "delivery-compute.json" + self.store.assets.pop((TAG, name)) + self.row["assets"] = [a for a in self.row["assets"] if a["name"] != name] + self.seed(record, manual=manual) + with self.assertRaises(contract.Invalid): + pipeline.aggregate(self.store, self.row, self.event, enabled=("compute",)) + + def test_producing_artifact_integrity_and_uniqueness_are_required(self): + record = self.record() + self.hub.publish_delivery(record) + self.seed(record) + artifact = copy.deepcopy(self.hub.artifacts[0]) + for changes in ({"expired": True}, {"digest": "sha256:" + "b" * 64}, + {"workflow_run": {"id": 901, "head_sha": COMMIT}}, + {"name": artifact["name"].removesuffix("-1") + "-2"}): + with self.subTest(changes=changes): + self.hub.artifacts[:] = [{**artifact, **changes}] + with self.assertRaises(contract.Invalid): + pipeline.aggregate(self.store, self.row, self.event, enabled=("compute",)) + self.hub.artifacts[:] = [artifact, copy.deepcopy(artifact)] + with self.assertRaises(contract.Invalid): + pipeline.aggregate(self.store, self.row, self.event, enabled=("compute",)) + archive = self.hub.archives[artifact["id"]] + extra = io.BytesIO() + with zipfile.ZipFile(extra, "w") as zipped: + zipped.writestr("redelivery.json", contract.canonical(record)) + zipped.writestr("unrelated.json", b"{}\n") + for raw in (b"not a ZIP", extra.getvalue()): + with self.subTest(archive=raw): + self.hub.archives[artifact["id"]] = raw + self.hub.artifacts[:] = [{**artifact, "digest": "sha256:" + contract.sha256(raw)}] + with self.assertRaises(contract.Invalid): + pipeline.aggregate(self.store, self.row, self.event, enabled=("compute",)) + self.hub.archives[artifact["id"]] = archive + # The producer artifact can follow a full page of unrelated artifacts. + unrelated = [{**artifact, "id": index, "name": f"unrelated-{index}"} for index in range(2, 102)] + self.hub.artifact_runs.update({a["id"]: 900 for a in unrelated}) + self.hub.artifacts[:] = [*unrelated, artifact] + self.assertEqual(pipeline.aggregate(self.store, self.row, self.event, enabled=("compute",))["consumers"], + {"compute": "delivered"}) + self.assertIn(f"repos/{contract.HUB}/actions/runs/900/artifacts?per_page=100&page=2", self.hub.reads) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_release_redeliver.py b/tests/test_release_redeliver.py index 527340c..dec9fd4 100644 --- a/tests/test_release_redeliver.py +++ b/tests/test_release_redeliver.py @@ -25,7 +25,8 @@ import release_contract as contract # noqa: E402 import release_pipeline as pipeline # noqa: E402 import release_receipts as adapter # noqa: E402 -from test_release_binding import (FinalizeClient, MemoryRegistry, MemoryStore, # noqa: E402 +from test_release_binding import (DELIVERY_SOURCES as SOURCE_FILES, # noqa: E402 + FinalizeClient, MemoryRegistry, MemoryStore, expected_pair_payload, fixture_manifest) TAG = "v1.6.0" @@ -87,13 +88,23 @@ def setUp(self): self.row.update(draft=False, prerelease=False) self.store.workflow_runs[900] = { "id": 900, "path": ".github/workflows/release-redeliver.yml", - "head_branch": "main", "status": "completed", "conclusion": "success"} + "run_attempt": 1, "head_sha": "a" * 40, + "head_branch": "main", "status": "completed", "conclusion": "success", + "event": "workflow_dispatch", "repository": {"full_name": contract.HUB}, + "head_repository": {"full_name": contract.HUB}, + "actor": {"login": "release-operator"}, "run_started_at": CLOCK, + "updated_at": "2026-10-23T00:01:00Z"} + self.store.runs[900, 1] = self.store.workflow_runs[900] self.seed = self.store.put self.seed(self.row, contract.ASSET, self.raw) self.store.operations.clear() self.public = PublicHub(self.store) + self.authority = self.store + self.authority.variables = {"RELEASE_READY_CONSUMERS": ",".join(contract.ROLES), + "RELEASE_COMPUTE_FIRST_PAIR": "1.0.0"} + self.store.client = self.authority self.target_client = artifacts.Client(token="target-app-token") - self.hub_client = object() + self.hub_client = self.authority self.real_store = pipeline.Store self.tokens, self.requests, self.accepted, self.puts, self.effects = [], [], [], [], [] self.fail_send = self.fail_put = False @@ -104,7 +115,8 @@ def setUp(self): self.start_patch(patch.dict(os.environ, { "GH_TOKEN": "target-app-token", "HUB_TOKEN": "hub-write-token", "TARGET_REPOSITORY": TARGET, "RELEASE_READY_CONSUMERS": CONSUMER, - "GITHUB_RUN_ID": "900", "GITHUB_ACTOR": "release-operator", + "GITHUB_RUN_ID": "900", "GITHUB_RUN_ATTEMPT": "1", + "GITHUB_SHA": "a" * 40, "GITHUB_ACTOR": "release-operator", }, clear=True)) self.start_patch(patch.object(pipeline, "now", return_value=CLOCK)) self.start_patch(patch.object(pipeline, "Client", side_effect=self.client)) @@ -149,22 +161,43 @@ def put(self, row, name, raw): if self.fail_put: raise OSError("hub audit storage unavailable") self.seed(row, name, raw) + if name.startswith("delivery-redelivered-"): + self.publish_audit(contract.decode(raw)) def audit(self, attempt, *, consumer=CONSUMER): - return {"schema": "pineforge/release-redelivery/v2", "event_id": self.event["event_id"], + candidate = self.store.read(self.row, contract.CANDIDATE) if consumer == "compute" else None + body = {"event_type": contract.EVENT_TYPES[consumer], + "client_payload": contract.pair_payload(self.raw, candidate) if candidate else self.event} + source_files = (*SOURCE_FILES, ".github/workflows/release-redeliver.yml") + return {"schema": "pineforge/release-redelivery/v3", "event_id": self.event["event_id"], "consumer": consumer, "attempt": attempt, "state": "dispatch_accepted", - "run_id": 900, "dispatch_status": 204, "timestamp": CLOCK, "actor": "release-operator"} + "run_id": int(os.environ["GITHUB_RUN_ID"]), "run_attempt": 1, + "dispatch_status": 204, "timestamp": CLOCK, "actor": "release-operator", + "context": {"tag": self.row["tag_name"], "manifest_sha256": contract.sha256(self.raw), + "candidate_sha256": contract.sha256(candidate) if candidate else None, + "dispatch_sha256": contract.sha256(contract.canonical(body)), + "workflow_commit": "a" * 40, + "source_sha256": {name: contract.sha256(self.authority.sources[name]) + for name in source_files}}} + + def publish_audit(self, obj): + self.authority.publish_delivery(obj, tag=self.row["tag_name"]) def seed_audit(self, number, *, consumer=CONSUMER, changes=None, name=None): - obj = {**self.audit(number, consumer=consumer), **(changes or {})} + genuine = self.audit(number, consumer=consumer) + genuine["run_id"] = 700 + number + self.publish_audit(genuine) + obj = {**genuine, **(changes or {})} name = name or f"delivery-redelivered-{consumer}-{number}.json" self.seed(self.row, name, contract.canonical(obj)) def seed_acceptance(self): - self.seed(self.row, f"delivery-{CONSUMER}.json", contract.canonical({ + obj = { "schema": "pineforge/release-delivery/v1", "event_id": self.event["event_id"], "consumer": CONSUMER, "attempt": 3, "state": "dispatch_accepted", - "run_id": 800, "timestamp": CLOCK})) + "run_id": 800, "timestamp": CLOCK} + self.authority.publish_delivery(obj, manual=False, tag=self.row["tag_name"]) + self.seed(self.row, f"delivery-{CONSUMER}.json", contract.canonical(obj)) def receipt_body(self, state, *, consumer=CONSUMER, attempt=1, sequence=1): # A trusted hub summary, after the existing receipt adapter verified the @@ -377,8 +410,9 @@ def test_audit_records_environment_run_id_and_actual_dispatch_status(self): with patch.dict(os.environ, {"GITHUB_RUN_ID": "9123"}): self.invoke() audit = contract.decode(self.output.read_bytes()) - self.assertEqual(audit["schema"], "pineforge/release-redelivery/v2") + self.assertEqual(audit["schema"], "pineforge/release-redelivery/v3") self.assertEqual(audit["run_id"], 9123) + self.assertEqual(audit["run_attempt"], 1) self.assertEqual(audit["dispatch_status"], self.dispatch_status) self.assertEqual(audit["dispatch_status"], 204) self.assertEqual(self.puts, [(TAG, f"delivery-redelivered-{CONSUMER}-1.json", @@ -403,41 +437,52 @@ def test_unconfirmed_history_refuses_before_another_dispatch(self): """Mutation: omit confirm_redelivery_run from redeliver's history check.""" success = self.store.workflow_runs[900] cases = ( - ({**success, "path": ".github/workflows/release-finalize.yml"}, "redelivery workflow run differs"), - ({**success, "head_branch": "repair"}, "redelivery workflow run differs"), - ({**success, "conclusion": "failure"}, "redelivery workflow run differs"), - ({**success, "status": "in_progress", "conclusion": None}, "redelivery workflow run differs"), - (None, "redelivery workflow run unavailable"), + ({**success, "path": ".github/workflows/release-finalize.yml"}, "delivery producing workflow attempt differs"), + ({**success, "head_branch": "repair"}, "delivery producing workflow attempt differs"), + ({**success, "conclusion": "failure"}, "delivery producing workflow attempt differs"), + ({**success, "status": "in_progress", "conclusion": None}, "delivery producing workflow attempt differs"), + (None, "delivery producing workflow attempt unavailable (requires Actions: read)"), (contract.Invalid("artifact request failed (HTTP 500)"), "artifact request failed (HTTP 500)"), ) for schema in ("pineforge/release-redelivery/v1", "pineforge/release-redelivery/v2"): obj = {**self.audit(1), "schema": schema} + obj = {key: value for key, value in obj.items() if key not in ("run_attempt", "context")} if schema.endswith("/v1"): obj = {key: value for key, value in obj.items() if key != "dispatch_status"} + self.authority.artifacts.clear() + self.authority.archives.clear() + self.authority.artifact_runs.clear() + self.store.runs[900, 1] = copy.deepcopy(success) + self.store.workflow_runs[900] = self.store.runs[900, 1] + self.authority.publish_delivery(obj) for run, message in cases: with self.subTest(schema=schema, run=run): before = copy.deepcopy(self.store.assets) assets = copy.deepcopy(self.row["assets"]) self.store.workflow_runs = {} if run is None else {900: run} + self.store.runs = {} if run is None else {(900, 1): run} self.store.run_reads.clear() try: self.seed(self.row, f"delivery-redelivered-{CONSUMER}-1.json", contract.canonical(obj)) self.assert_refused(message=message) - self.assertEqual(self.store.run_reads, [f"repos/{contract.HUB}/actions/runs/900"]) + self.assertEqual(self.store.run_reads, [f"repos/{contract.HUB}/actions/runs/900/attempts/1"]) finally: self.store.assets = before self.row["assets"] = assets - def test_confirmed_v1_history_can_append_v2_without_rewriting_history(self): + def test_confirmed_v1_history_can_append_v3_without_rewriting_history(self): """Mutation: reject all legacy history instead of confirming its workflow run.""" legacy = {**self.audit(1), "schema": "pineforge/release-redelivery/v1"} - legacy = {key: value for key, value in legacy.items() if key != "dispatch_status"} + legacy = {key: value for key, value in legacy.items() + if key not in ("dispatch_status", "run_attempt", "context")} + self.publish_audit(legacy) self.seed(self.row, f"delivery-redelivered-{CONSUMER}-1.json", contract.canonical(legacy)) before = copy.deepcopy(self.store.assets) + os.environ["GITHUB_RUN_ID"] = "901" self.invoke() audit = contract.canonical(self.audit(2)) name = f"delivery-redelivered-{CONSUMER}-2.json" - self.assertEqual(self.store.run_reads, [f"repos/{contract.HUB}/actions/runs/900"]) + self.assertEqual(self.store.run_reads, [f"repos/{contract.HUB}/actions/runs/900/attempts/1"]) self.assertEqual(self.puts, [(TAG, name, audit)]) self.assertEqual(self.store.assets, {**before, (TAG, name): audit}) self.assertEqual(self.effects, ["send", "put"]) @@ -481,6 +526,7 @@ def test_numbers_are_contiguous_independent_of_planner_and_other_roles(self): self.seed_audit(7, consumer="offline") before = copy.deepcopy(self.store.assets) for number in (2, 3, 4): + os.environ["GITHUB_RUN_ID"] = str(900 + number) self.invoke() self.assertEqual(self.output.read_bytes(), contract.canonical(self.audit(number))) self.assertEqual([name for _, name, _ in self.puts], @@ -628,10 +674,12 @@ def test_compute_redelivery_keeps_an_eligible_older_prerelease_pair(self): row.update(id=2, draft=False, prerelease=False) self.store.rows[row["tag_name"]] = row self.seed(row, contract.ASSET, contract.canonical(newer)) - self.seed(self.row, "delivery-compute.json", contract.canonical({ + accepted = { "schema": "pineforge/release-delivery/v1", "event_id": self.event["event_id"], "consumer": "compute", "attempt": 1, "state": "dispatch_accepted", - "run_id": 800, "timestamp": CLOCK})) + "run_id": 800, "timestamp": CLOCK} + self.authority.publish_delivery(accepted, manual=False, tag=self.row["tag_name"]) + self.seed(self.row, "delivery-compute.json", contract.canonical(accepted)) before = copy.deepcopy(self.store.assets) tag = self.row["tag_name"] with patch.dict(os.environ, {"RELEASE_READY_CONSUMERS": "compute", @@ -700,7 +748,7 @@ def test_accepted_compute_repair_completes_finalization_and_preserves_exhaustion self.assertEqual(self.store.assets, {**before, (TAG, name): audit_raw}) self.assertEqual(contract.decode(deliveries.read_bytes()), []) self.assertFalse(report.call_args.kwargs.get("complete", False)) - self.assertEqual(self.store.run_reads, [f"repos/{contract.HUB}/actions/runs/900"]) + self.assertEqual(self.store.run_reads, [f"repos/{contract.HUB}/actions/runs/900/attempts/1"]) self.store.workflow_runs[900].update(status="completed", conclusion="success") self.store.run_reads.clear() report.reset_mock() @@ -714,7 +762,7 @@ def test_accepted_compute_repair_completes_finalization_and_preserves_exhaustion self.assertEqual(status["stale"], []) self.assertEqual(contract.decode(deliveries.read_bytes()), []) self.assertTrue(report.call_args.kwargs["complete"]) - self.assertEqual(self.store.run_reads, [f"repos/{contract.HUB}/actions/runs/900"]) + self.assertEqual(self.store.run_reads, [f"repos/{contract.HUB}/actions/runs/900/attempts/1"]) for key, raw in before.items(): self.assertEqual(self.store.assets[key], raw) self.assertFalse(any(name.startswith("consumer-") for _, name in self.store.assets)) diff --git a/tests/test_release_workflows.py b/tests/test_release_workflows.py index d1277b9..fa2a112 100644 --- a/tests/test_release_workflows.py +++ b/tests/test_release_workflows.py @@ -1974,6 +1974,26 @@ def test_dispatch_requires_finalized_manifest_plan(self): send = next(s for s in job["steps"] if s.get("name", "").startswith("Dispatch pineforge-release")) self.assertIn("release_pipeline.py dispatch", send["run"]) self.assertNotIn("client_payload[", send["run"]) + checkout = next(s for s in job["steps"] if s.get("uses", "").startswith("actions/checkout@")) + self.assertEqual(checkout["with"], {"ref": "${{ github.sha }}", "persist-credentials": "false"}) + self.assertEqual(send["env"], { + "GH_TOKEN": "${{ steps.app.outputs.token }}", "HUB_TOKEN": "${{ github.token }}", + "TAG": "${{ matrix.tag }}", "CONSUMER": "${{ matrix.consumer }}", + "DELIVERY_ATTEMPT": "${{ matrix.attempt }}", + }) + receipt = next(s for s in job["steps"] if s.get("name") == "Preserve delivery receipt") + self.assertEqual(receipt["env"]["GH_TOKEN"], "${{ github.token }}") + artifact = next(s for s in job["steps"] if s.get("name") == "Preserve this delivery attempt") + self.assertEqual(artifact["uses"], + "actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9") + self.assertEqual(artifact["if"], "${{ !cancelled() }}") + self.assertEqual(artifact["with"], { + "name": "release-delivery-${{ matrix.consumer }}-${{ matrix.tag }}-${{ github.run_id }}-${{ github.run_attempt }}", + "path": "${{ runner.temp }}/delivery.json", "if-no-files-found": "error", + }) + self.assertLess(job["steps"].index(checkout), job["steps"].index(send)) + self.assertLess(job["steps"].index(send), job["steps"].index(receipt)) + self.assertLess(job["steps"].index(receipt), job["steps"].index(artifact)) def test_all_consumer_roles_have_explicit_registry_and_policy(self): import release_contract as contract @@ -2350,12 +2370,13 @@ def test_one_target_app_scope_public_reads_and_hub_audit_token(self): self.assertEqual(target, sibling["env"]["TARGET_REPOSITORY"].replace("matrix.consumer", "inputs.consumer")) steps = job["steps"] self.assertEqual(steps[0], ReleaseTriggerWorkflowTest.APT_GUARD_STEP) - self.assertEqual(len([step for step in steps if "uses" in step]), 2) + self.assertEqual(len([step for step in steps if "uses" in step]), 3) actions = {step["uses"]: step for step in steps if "uses" in step} checkout = "actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1" mint = "actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510" - self.assertEqual(set(actions), {checkout, mint}) - self.assertEqual(actions[checkout]["with"], {"ref": "main", "persist-credentials": "false"}) + upload = "actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9" + self.assertEqual(set(actions), {checkout, mint, upload}) + self.assertEqual(actions[checkout]["with"], {"ref": "${{ github.sha }}", "persist-credentials": "false"}) self.assertEqual(actions[mint]["with"], { "app-id": "${{ secrets.PINEFORGE_APP_ID }}", "private-key": "${{ secrets.PINEFORGE_APP_PRIVATE_KEY }}", @@ -2385,6 +2406,13 @@ def test_one_target_app_scope_public_reads_and_hub_audit_token(self): self.assertEqual(sends[0]["run"], 'python3 scripts/release_pipeline.py redeliver --tag "$TAG" --consumer "$CONSUMER" \\\n' ' --output "$RUNNER_TEMP/redelivery.json"\n') + self.assertEqual(actions[upload]["with"], { + "name": "release-redelivery-${{ inputs.consumer }}-${{ inputs.tag }}-${{ github.run_id }}-${{ github.run_attempt }}", + "path": "${{ runner.temp }}/redelivery.json", "if-no-files-found": "error", + }) + self.assertLess(steps.index(sends[0]), steps.index(actions[upload])) + self.assertNotIn("env", actions[upload]) + self.assertNotIn("continue-on-error", actions[upload]) self.assertNotIn("release_receipts.py", str(steps)) self.assertNotIn("client_payload", str(steps))