Files
akyldash/infra/deploy/test_release_source.py
codex 710c483f34
CI / tests-and-images (pull_request) Successful in 2m15s
Runner smoke test / runner-smoke (pull_request) Successful in 3s
Harden external release preparation workflow
2026-11-03 09:38:37 +03:00

356 lines
17 KiB
Python

import hashlib
import base64
import json
import stat
import sys
import tempfile
import unittest
from urllib.parse import parse_qs, urlsplit
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_runtime_files_allow_bootstrap_a_to_release_b_only_when_configs_match(self):
commit_b = "f" * 40
contents = {
"deploy/production/compose.yaml": b"compose-v1\n",
"deploy/production/Caddyfile": b"caddy-v1\n",
"infra/deploy/release-policy.json": b"policy-v1\n",
}
def response(path):
payload = contents[path]
blob_sha = hashlib.sha1(b"blob " + str(len(payload)).encode() + b"\0" + payload).hexdigest()
return {"type": "file", "path": path, "encoding": "base64",
"content": base64.b64encode(payload).decode(), "size": len(payload), "sha": blob_sha}
def api(request):
self.assertIn(f"?ref={commit_b}", request)
path = request.split("/contents/", 1)[1].split("?", 1)[0]
return response(path)
with tempfile.TemporaryDirectory() as directory:
checkout = Path(directory)
(checkout / "deploy/production").mkdir(parents=True)
(checkout / "deploy/production/compose.yaml").write_bytes(contents["deploy/production/compose.yaml"])
(checkout / "deploy/production/Caddyfile").write_bytes(contents["deploy/production/Caddyfile"])
policy = checkout / "policy.json"
policy.write_bytes(contents["infra/deploy/release-policy.json"])
self.assertIsNone(release_source.runtime_compatibility_reason(
commit_b, checkout=checkout, policy_file=policy, api=api,
))
for relative in ("deploy/production/compose.yaml", "deploy/production/Caddyfile"):
local_path = checkout / relative
expected = contents[relative]
local_path.write_bytes(b"changed\n")
self.assertEqual(release_source.runtime_compatibility_reason(
commit_b, checkout=checkout, policy_file=policy, api=api,
), "runtime_config_changed")
local_path.write_bytes(expected)
policy.write_bytes(b"policy-changed\n")
self.assertEqual(release_source.runtime_compatibility_reason(
commit_b, checkout=checkout, policy_file=policy, api=api,
), "release_policy_changed")
def test_runtime_file_rejects_forged_git_blob_identity(self):
payload = b"trusted content\n"
response = {"type": "file", "path": "deploy/production/compose.yaml", "encoding": "base64",
"content": base64.b64encode(payload).decode(), "size": len(payload), "sha": "a" * 40}
with self.assertRaisesRegex(ValueError, "does not match"):
release_source.repository_file(
"deploy/production/compose.yaml", COMMIT,
api=lambda _path: response,
)
def test_runtime_compatibility_rejects_local_symlink_even_when_bytes_match(self):
content = b"protected\n"
with tempfile.TemporaryDirectory() as directory:
checkout = Path(directory)
config_dir = checkout / "deploy/production"
config_dir.mkdir(parents=True)
(config_dir / "compose.yaml").write_bytes(content)
(config_dir / "Caddyfile").write_bytes(content)
policy = checkout / "policy.json"
policy.write_bytes(content)
external = checkout / "outside.yaml"
external.write_bytes(content)
(config_dir / "Caddyfile").unlink()
(config_dir / "Caddyfile").symlink_to(external)
blob_sha = hashlib.sha1(b"blob " + str(len(content)).encode() + b"\0" + content).hexdigest()
with self.assertRaisesRegex(ValueError, "not a regular file"):
release_source.runtime_compatibility_reason(
COMMIT, checkout=checkout, policy_file=policy,
api=lambda _path: {
"type": "file", "path": "deploy/production/compose.yaml",
"encoding": "base64", "content": base64.b64encode(content).decode(),
"size": len(content), "sha": blob_sha,
},
)
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 api_helper(command, **kwargs):
calls.append(command)
path = Path(command[-1])
write_artifact(path)
return b"PASS"
# 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.run_api_helper", side_effect=api_helper):
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))
def test_latest_release_request_uses_bounded_filtered_listing_and_verifies_details(self):
descriptor_sha = "e" * 64
responses = {
"/repos/admin/akyldash/actions/workflows/release-images.yaml/runs": {
"total_count": 2,
"workflow_runs": [
{"id": 410, "head_sha": None, "workflow_id": None, "head_branch": None},
{"id": 411, "head_sha": None, "workflow_id": None, "head_branch": None},
],
},
"/repos/admin/akyldash/actions/runs/411": {
"id": 411, "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",
},
"/repos/admin/akyldash/actions/runs/411/artifacts": {
"artifacts": [{
"id": 9, "name": f"release-descriptor-411-{descriptor_sha}", "expired": False,
"workflow_run": {"id": 411, "head_sha": COMMIT, "head_branch": "main",
"event": "workflow_run", "conclusion": "success"},
}]
},
}
calls = []
def api(path):
calls.append(path)
parsed = urlsplit(path)
return responses[parsed.path]
request = release_source.latest_release_request(current_main=COMMIT, api=api)
self.assertEqual(request, {"run_id": "411", "commit": COMMIT, "descriptor_sha256": descriptor_sha})
filters = parse_qs(urlsplit(calls[0]).query)
self.assertEqual(filters, {
"event": ["workflow_run"], "branch": ["main"], "status": ["success"],
"head_sha": [COMMIT], "page": ["1"], "limit": ["10"],
})
self.assertIn("/actions/workflows/release-images.yaml/runs?", calls[0])
self.assertIn("/repos/admin/akyldash/actions/runs/411", calls)
def test_latest_release_request_fails_closed_on_incomplete_listing(self):
with self.assertRaisesRegex(ValueError, "incomplete"):
release_source.latest_release_request(
current_main=COMMIT,
api=lambda _path: {"total_count": 11, "workflow_runs": [{"id": i} for i in range(10)]},
)
def test_latest_release_request_rejects_wrong_detailed_source(self):
responses = {
"/repos/admin/akyldash/actions/workflows/release-images.yaml/runs": {
"total_count": 1, "workflow_runs": [{"id": 411}],
},
"/repos/admin/akyldash/actions/runs/411": {
"id": 411, "workflow_id": "release-images.yaml", "status": "completed",
"conclusion": "success", "head_sha": "f" * 40,
"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",
},
}
def api(path):
return responses[urlsplit(path).path]
with self.assertRaisesRegex(ValueError, "does not match"):
release_source.latest_release_request(current_main=COMMIT, api=api)
def test_api_json_caps_response_and_passes_no_token_argument(self):
with patch.object(release_source, "run_api_helper", return_value=b'{"ok":true}') as run:
self.assertEqual(release_source.api_json("/repos/admin/akyldash"), {"ok": True})
command = run.call_args.args[0]
self.assertEqual(command, ["gitea-api", "GET", "/repos/admin/akyldash"])
self.assertEqual(run.call_args.kwargs["timeout"], release_source.API_TIMEOUT_SECONDS)
with patch.object(release_source, "run_api_helper", return_value=b"x" * (release_source.MAX_API_RESPONSE_BYTES + 1)):
with self.assertRaisesRegex(ValueError, "size limit"):
release_source.api_json("/repos/admin/akyldash")
def test_api_helper_timeout_is_bounded_and_fails_closed(self):
with self.assertRaisesRegex(ValueError, "timed out"):
release_source.run_api_helper(
[sys.executable, "-c", "import time; time.sleep(10)"], env=None,
timeout=0.05, max_stdout_bytes=32, max_file_bytes=32,
)
if __name__ == "__main__":
unittest.main()