import hashlib import json import stat import tempfile import unittest import zipfile from pathlib import Path from unittest.mock import patch import release_source POLICY = { "schema_version": 1, "repositories": { "backend": ["registry.test/akyldash/backend"], "opensearch": ["registry.test/akyldash/opensearch"], }, } COMMIT = "a" * 40 RELEASE_RUN = "302" CI_RUN = "301" MANIFEST = { "schema_version": 4, "source_ref": "refs/heads/main", "repository": "admin/akyldash", "commit": COMMIT, "workflow": ".gitea/workflows/release-images.yaml", "run_id": RELEASE_RUN, "source_run_id": CI_RUN, "builder": {"name": "Kaniko", "image": "ghcr.io/osscontainertools/kaniko:v1.28.5-debug@sha256:c73ca67a3b494bce79a8fb6090c4f73c18997ab272d80bffc460a62343aeb412"}, "images": { "backend": "registry.test/akyldash/backend@sha256:" + "b" * 64, "opensearch": "registry.test/akyldash/opensearch@sha256:" + "c" * 64, }, } def write_artifact(path, manifest=MANIFEST, extra=()): raw = json.dumps(manifest, sort_keys=True).encode() digest = hashlib.sha256(raw).hexdigest() with zipfile.ZipFile(path, "w", compression=zipfile.ZIP_DEFLATED) as archive: archive.writestr("release-manifest.json", raw) archive.writestr("release-manifest.json.sha256", f"{digest} release-manifest.json\n") for name, payload in extra: archive.writestr(name, payload) return digest, raw class ReleaseSourceTests(unittest.TestCase): def test_verify_run_binds_run_id_workflow_path_event_branch_commit_and_main(self): def api(path): if path.endswith("/actions/runs/301"): return {"id": 301, "workflow_id": "ci.yaml", "name": "CI", "status": "completed", "conclusion": "success", "head_sha": COMMIT, "head_branch": "main", "event": "push"} return {"id": "ci.yaml", "name": "CI", "path": ".gitea/workflows/ci.yaml"} run = release_source.verify_run(CI_RUN, stage="ci", commit=COMMIT, api=api, current_main=COMMIT) self.assertEqual(run["head_sha"], COMMIT) with self.assertRaisesRegex(ValueError, "workflow run is not successful"): release_source.verify_run(CI_RUN, stage="ci", commit=COMMIT, api=lambda path: {"id": 301, "status": "failure"}, current_main=COMMIT) def test_completed_run_requires_success_conclusion(self): responses = { f"/repos/admin/akyldash/actions/runs/{CI_RUN}": { "id": int(CI_RUN), "workflow_id": "ci.yaml", "status": "completed", "conclusion": "cancelled", "head_sha": COMMIT, "head_branch": "main", "event": "push", }, } with self.assertRaisesRegex(ValueError, "workflow run is not successful"): release_source.verify_run(CI_RUN, stage="ci", commit=COMMIT, api=lambda path: responses[path], current_main=COMMIT) def test_zip_descriptor_checks_exact_members_digest_source_ids_and_allowlist(self): with tempfile.TemporaryDirectory() as directory: path = Path(directory) / "artifact.zip" digest, _ = write_artifact(path) manifest, raw = release_source.validate_artifact_zip( path, descriptor_sha256=digest, release_run_id=RELEASE_RUN, ci_run_id=CI_RUN, commit=COMMIT, policy=POLICY, ) self.assertEqual(manifest, MANIFEST) self.assertEqual(hashlib.sha256(raw).hexdigest(), digest) with self.assertRaisesRegex(ValueError, "requested SHA-256"): release_source.validate_artifact_zip( path, descriptor_sha256="d" * 64, release_run_id=RELEASE_RUN, ci_run_id=CI_RUN, commit=COMMIT, policy=POLICY, ) with self.assertRaisesRegex(ValueError, "does not bind"): release_source.validate_artifact_zip( path, descriptor_sha256=digest, release_run_id=RELEASE_RUN, ci_run_id="999", commit=COMMIT, policy=POLICY, ) def test_zip_refuses_extra_traversal_duplicate_and_symlink_members(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) extras = ( (("../outside", b"x"),), (("release-manifest.json", b"duplicate"),), ) for extra in extras: path = root / f"bad-{len(list(root.iterdir()))}.zip" digest, _ = write_artifact(path, extra=extra) with self.assertRaises(ValueError): release_source.validate_artifact_zip( path, descriptor_sha256=digest, release_run_id=RELEASE_RUN, ci_run_id=CI_RUN, commit=COMMIT, policy=POLICY, ) path = root / "symlink.zip" raw = json.dumps(MANIFEST, sort_keys=True).encode() digest = hashlib.sha256(raw).hexdigest() with zipfile.ZipFile(path, "w") as archive: info = zipfile.ZipInfo("release-manifest.json") info.create_system = 3 info.external_attr = (stat.S_IFLNK | 0o777) << 16 archive.writestr(info, raw) archive.writestr("release-manifest.json.sha256", f"{digest} release-manifest.json\n") with self.assertRaisesRegex(ValueError, "unsafe"): release_source.validate_artifact_zip( path, descriptor_sha256=digest, release_run_id=RELEASE_RUN, ci_run_id=CI_RUN, commit=COMMIT, policy=POLICY, ) def test_fetch_release_rechecks_both_runs_and_uses_only_gitea_api_helper(self): with tempfile.TemporaryDirectory() as fixture_dir: fixture_zip = Path(fixture_dir) / "artifact.zip" descriptor_sha, _ = write_artifact(fixture_zip) request = {"run_id": RELEASE_RUN, "commit": COMMIT, "descriptor_sha256": descriptor_sha} artifact_id = 77 run_responses = { f"/repos/admin/akyldash/actions/runs/{RELEASE_RUN}": { "id": int(RELEASE_RUN), "workflow_id": "release-images.yaml", "name": "Release production images", "status": "completed", "conclusion": "success", "head_sha": COMMIT, "head_branch": "main", "event": "workflow_run", }, "/repos/admin/akyldash/actions/workflows/release-images.yaml": { "id": "release-images.yaml", "name": "Release production images", "path": ".gitea/workflows/release-images.yaml", }, f"/repos/admin/akyldash/actions/runs/{CI_RUN}": { "id": int(CI_RUN), "workflow_id": "ci.yaml", "name": "CI", "status": "completed", "conclusion": "success", "head_sha": COMMIT, "head_branch": "main", "event": "push", }, "/repos/admin/akyldash/actions/workflows/ci.yaml": { "id": "ci.yaml", "name": "CI", "path": ".gitea/workflows/ci.yaml", }, f"/repos/admin/akyldash/actions/runs/{RELEASE_RUN}/artifacts": { "artifacts": [{ "id": artifact_id, "name": f"release-descriptor-{RELEASE_RUN}-{descriptor_sha}", "expired": False, "workflow_run": {"id": int(RELEASE_RUN), "head_sha": COMMIT, "head_branch": "main", "event": "workflow_run", "conclusion": "success"}, }] }, } with tempfile.TemporaryDirectory() as directory: scratch = Path(directory) / "scratch" scratch.mkdir(mode=0o700) calls = [] def api(path): calls.append(path) return run_responses[path] def subprocess_run(command, **kwargs): calls.append(command) path = Path(command[-1]) write_artifact(path) return type("Result", (), {"stdout": "PASS", "returncode": 0})() # The source helper requires a private root directory; this synthetic test isolates its owner check. with patch.object(release_source, "ensure_private_scratch"): with patch("release_source.subprocess.run", side_effect=subprocess_run): manifest, raw = release_source.fetch_release( request, policy=POLICY, temp_dir=scratch, api=api, current_main=COMMIT ) self.assertEqual(manifest, MANIFEST) self.assertEqual(hashlib.sha256(raw).hexdigest(), descriptor_sha) self.assertTrue(any(isinstance(call, list) and call[1] == "DOWNLOAD" for call in calls)) if __name__ == "__main__": unittest.main()