"""Verify a Gitea release and its exact uploaded descriptor through gitea-api.""" from __future__ import annotations import hashlib import json import os import re import stat import subprocess import zipfile from pathlib import Path, PurePosixPath from release_handoff import descriptor_request, latest_main_sha, validate_source from validate_release import read_json, unique_object, validate_manifest REPOSITORY = "admin/akyldash" RELEASE_WORKFLOW = ".gitea/workflows/release-images.yaml" CI_WORKFLOW = ".gitea/workflows/ci.yaml" MAX_DESCRIPTOR_BYTES = 1024 * 1024 def api_json(path: str, *, helper: str = "gitea-api", env: dict | None = None) -> dict: result = subprocess.run( [helper, "GET", path], check=True, text=True, capture_output=True, env=env ) value = json.loads(result.stdout, object_pairs_hook=unique_object) if not isinstance(value, dict): raise ValueError("Gitea API returned a non-object response") return value def ensure_private_scratch(path: Path) -> None: path.mkdir(mode=0o700, parents=True, exist_ok=True) if path.is_symlink() or path.stat().st_uid != 0 or path.stat().st_mode & 0o077: raise ValueError("artifact scratch directory must be a private root-owned directory") def verify_run(run_id: str, *, stage: str, commit: str, api=api_json, current_main: str | None = None, checkout: Path | None = None) -> dict: if not run_id.isdigit() or not re.fullmatch(r"[0-9a-f]{40}", commit): raise ValueError("Gitea run identity is invalid") run = api(f"/repos/{REPOSITORY}/actions/runs/{run_id}") if not isinstance(run, dict) or str(run.get("id")) != run_id: raise ValueError("Gitea returned a different workflow run") if run.get("status") not in {"completed", "success"} or run.get("conclusion") != "success": raise ValueError("workflow run is not successful") workflow_id = run.get("workflow_id") if not isinstance(workflow_id, (int, str)) or not str(workflow_id): raise ValueError("workflow run has no workflow identity") workflow = api(f"/repos/{REPOSITORY}/actions/workflows/{workflow_id}") expected_workflow, path, event = ( ("CI", CI_WORKFLOW, "push") if stage == "ci" else ("Release production images", RELEASE_WORKFLOW, "workflow_run") ) if (workflow.get("name") != expected_workflow or workflow.get("path") != path or str(workflow.get("id")) != str(workflow_id)): raise ValueError("workflow ID does not resolve to the approved workflow name and path") if run.get("name") not in {None, expected_workflow}: raise ValueError("workflow run name is not approved") if run.get("head_sha") != commit: raise ValueError("workflow run commit does not match the release request") branch = run.get("head_branch") if branch != "main" or run.get("event") != event: raise ValueError("workflow run is not the approved main event") validate_source( workflow=expected_workflow, workflow_path=f"{Path(path).name}@refs/heads/main", event=event, conclusion="success", branch=branch, run_id=run_id, head_sha=commit, repository=REPOSITORY, stage="release" if stage == "ci" else "handoff", current_main=current_main or latest_main_sha(checkout), ) return run def validate_artifact_zip(path: Path, *, descriptor_sha256: str, release_run_id: str, ci_run_id: str | None, commit: str, policy: object) -> tuple[dict, bytes]: if not re.fullmatch(r"[0-9a-f]{64}", descriptor_sha256): raise ValueError("requested descriptor digest is invalid") try: with zipfile.ZipFile(path) as archive: members = archive.infolist() names = [member.filename for member in members] expected = {"release-manifest.json", "release-manifest.json.sha256"} if len(names) != 2 or set(names) != expected: raise ValueError("release artifact must contain exactly the manifest and checksum files") payloads = {} for member in members: name = PurePosixPath(member.filename) mode = member.external_attr >> 16 kind = stat.S_IFMT(mode) if (member.is_dir() or name.is_absolute() or len(name.parts) != 1 or name.as_posix() != member.filename or kind not in {0, stat.S_IFREG} or member.file_size > MAX_DESCRIPTOR_BYTES or member.compress_size > MAX_DESCRIPTOR_BYTES): raise ValueError("release artifact contains an unsafe or oversized member") with archive.open(member) as stream: chunks = [] size = 0 while chunk := stream.read(64 * 1024): size += len(chunk) if size > MAX_DESCRIPTOR_BYTES: raise ValueError("release artifact member exceeds its size limit") chunks.append(chunk) if size != member.file_size: raise ValueError("release artifact member size does not match its ZIP metadata") payloads[member.filename] = b"".join(chunks) except (OSError, zipfile.BadZipFile, RuntimeError) as error: raise ValueError(f"release artifact ZIP is invalid: {error}") from error manifest_bytes = payloads["release-manifest.json"] if hashlib.sha256(manifest_bytes).hexdigest() != descriptor_sha256: raise ValueError("release descriptor bytes do not match the requested SHA-256") checksum = payloads["release-manifest.json.sha256"] expected_sidecar = f"{descriptor_sha256} release-manifest.json\n".encode("ascii") if checksum != expected_sidecar: raise ValueError("release descriptor checksum sidecar is invalid") try: manifest = json.loads(manifest_bytes, object_pairs_hook=unique_object) except (UnicodeError, json.JSONDecodeError) as error: raise ValueError("release descriptor JSON is invalid") from error validated = validate_manifest(manifest, policy) if (validated["run_id"] != release_run_id or (ci_run_id is not None and validated["source_run_id"] != ci_run_id) or validated["commit"] != commit): raise ValueError("release descriptor does not bind the verified CI and release runs") return validated, manifest_bytes def fetch_release(request: dict, *, policy: object, helper: str = "gitea-api", temp_dir: Path = Path("/run/akyldash"), env: dict | None = None, api=api_json, current_main: str | None = None, checkout: Path | None = None) -> tuple[dict, bytes]: run_id, commit, descriptor_sha256 = ( request["run_id"], request["commit"], request["descriptor_sha256"] ) current_main = current_main or latest_main_sha(checkout) release_run = verify_run(run_id, stage="handoff", commit=commit, api=api, current_main=current_main, checkout=checkout) metadata = api(f"/repos/{REPOSITORY}/actions/runs/{run_id}/artifacts") tuple_data = descriptor_request(metadata, run_id=run_id, head_sha=commit) if tuple_data["descriptor_sha256"] != descriptor_sha256: raise ValueError("requested descriptor digest does not match the Gitea run artifact") artifact = metadata["artifacts"][0] artifact_id = artifact.get("id") if not isinstance(artifact_id, (int, str)) or not str(artifact_id).isdigit(): raise ValueError("release descriptor artifact has no numeric ID") ensure_private_scratch(temp_dir) from tempfile import TemporaryDirectory with TemporaryDirectory(prefix="akyldash-release-", dir=temp_dir) as scratch: zip_path = Path(scratch) / "artifact.zip" result = subprocess.run( [helper, "DOWNLOAD", f"/repos/{REPOSITORY}/actions/artifacts/{artifact_id}/zip", str(zip_path)], check=True, text=True, capture_output=True, env=env, ) del result manifest, manifest_bytes = validate_artifact_zip( zip_path, descriptor_sha256=tuple_data["descriptor_sha256"], release_run_id=run_id, ci_run_id=None, commit=commit, policy=policy, ) source_run_id = manifest["source_run_id"] verify_run(source_run_id, stage="ci", commit=commit, api=api, current_main=current_main, checkout=checkout) return manifest, manifest_bytes