373 lines
19 KiB
Python
373 lines
19 KiB
Python
import contextlib
|
|
import io
|
|
import json
|
|
import os
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
import release_handoff
|
|
from release_handoff import descriptor_request, validate_source
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
GITEA_UPLOAD_ARTIFACT = "https://gitea.com/actions/gitea-upload-artifact@055b8b3f04a4a7ed853f2c6ab04256f83e4874dc"
|
|
|
|
|
|
def workflow_step_env(path, step_name, payload):
|
|
lines = path.read_text(encoding="utf-8").splitlines()
|
|
marker = f" - name: {step_name}"
|
|
start = lines.index(marker)
|
|
block = []
|
|
for line in lines[start + 1 :]:
|
|
if line.startswith(" - "):
|
|
break
|
|
block.append(line)
|
|
env_start = block.index(" env:") + 1
|
|
env = {}
|
|
for line in block[env_start:]:
|
|
match = re.fullmatch(r" ([A-Z_]+): \$\{\{ (gitea(?:\.[a-z_]+)+) \}\}", line)
|
|
if not match:
|
|
if line.startswith(" ") and not line.startswith(" "):
|
|
break
|
|
continue
|
|
key, expression = match.groups()
|
|
value = payload
|
|
for component in expression.split("."):
|
|
value = value[component]
|
|
env[key] = str(value)
|
|
return env
|
|
|
|
|
|
class ReleaseWorkflowTests(unittest.TestCase):
|
|
def test_latest_main_lookup_uses_explicit_checkout(self):
|
|
with tempfile.TemporaryDirectory() as directory, patch.object(
|
|
release_handoff.subprocess, "run",
|
|
return_value=type("Result", (), {"stdout": "a" * 40 + "\trefs/heads/main\n"})(),
|
|
) as run:
|
|
self.assertEqual(release_handoff.latest_main_sha(Path(directory)), "a" * 40)
|
|
self.assertEqual(run.call_args.args[0][:3], ["git", "-C", directory])
|
|
|
|
def test_publisher_runs_only_after_successful_main_ci_and_binds_source_sha(self):
|
|
workflow = (ROOT / ".gitea/workflows/release-images.yaml").read_text(encoding="utf-8")
|
|
self.assertIn('workflows: ["CI"]', workflow)
|
|
self.assertIn("branches: [main]", workflow)
|
|
self.assertIn("types: [completed]", workflow)
|
|
self.assertIn("gitea.event.workflow_run.conclusion == 'success'", workflow)
|
|
self.assertIn("gitea.event.workflow_run.event == 'push'", workflow)
|
|
self.assertIn("gitea.event.workflow_run.head_branch == 'main'", workflow)
|
|
self.assertIn("ref: ${{ gitea.event.workflow_run.head_sha }}", workflow)
|
|
self.assertIn("python3 infra/deploy/release_handoff.py validate-ci-source", workflow)
|
|
self.assertNotIn("workflow_dispatch:", workflow)
|
|
self.assertIn("secrets.REGISTRY_USERNAME", workflow)
|
|
self.assertIn("secrets.REGISTRY_PASSWORD", workflow)
|
|
self.assertIn(GITEA_UPLOAD_ARTIFACT, workflow)
|
|
self.assertIn("release/release-manifest.json.sha256", workflow)
|
|
self.assertIn("retention-days: 90", workflow)
|
|
self.assertIn("if-no-files-found: error", workflow)
|
|
self.assertNotIn("gitea.run_attempt", workflow)
|
|
self.assertIn("${{ gitea.run_id }}", workflow)
|
|
self.assertIn("${{ gitea.event.workflow_run.head_sha }}", workflow)
|
|
self.assertIn("steps.descriptor.outputs.descriptor_sha256", workflow)
|
|
self.assertIn("(cd release && sha256sum -c release-manifest.json.sha256)", workflow)
|
|
self.assertNotIn("docker compose", workflow)
|
|
|
|
guard = workflow.index("Verify successful CI source before using registry credentials")
|
|
registry_secret = workflow.index("Require explicitly configured Gitea registry credentials")
|
|
self.assertLess(guard, registry_secret)
|
|
|
|
def test_workflow_yaml_maps_gitea_125_payload_into_both_source_guards(self):
|
|
head_sha = "a" * 40
|
|
cases = (
|
|
("release-images.yaml", "Verify successful CI source before using registry credentials",
|
|
"validate-ci-source", "CI", "ci.yaml@refs/heads/main", "push", "583"),
|
|
("deploy-handoff.yaml", "Verify source release identity before artifact lookup",
|
|
"validate-handoff-source", "Release production images",
|
|
"release-images.yaml@refs/heads/main", "workflow_run", "584"),
|
|
)
|
|
for filename, step, command, name, path, event, run_id in cases:
|
|
payload = {
|
|
"action": "completed",
|
|
"workflow": {"id": "workflow-id", "name": name, "path": path.split("@")[0], "state": "active"},
|
|
"workflow_run": {
|
|
"id": int(run_id), "path": path, "event": event, "conclusion": "success",
|
|
"head_branch": "main", "head_sha": head_sha,
|
|
},
|
|
"repository": {"full_name": "admin/akyldash"},
|
|
}
|
|
workflow_path = ROOT / ".gitea/workflows" / filename
|
|
source_env = workflow_step_env(workflow_path, step, {"gitea": {"event": payload}})
|
|
self.assertEqual(set(source_env), {
|
|
"SOURCE_WORKFLOW", "SOURCE_WORKFLOW_PATH", "SOURCE_EVENT", "SOURCE_CONCLUSION",
|
|
"SOURCE_BRANCH", "SOURCE_RUN_ID", "SOURCE_SHA", "SOURCE_REPOSITORY",
|
|
})
|
|
self.assertIn(f"release_handoff.py {command}", workflow_path.read_text(encoding="utf-8"))
|
|
with (
|
|
patch.dict(os.environ, source_env),
|
|
patch.object(sys, "argv", ["release_handoff.py", command]),
|
|
patch.object(release_handoff, "latest_main_sha", return_value=head_sha),
|
|
):
|
|
self.assertEqual(release_handoff.main(), 0)
|
|
|
|
with self.subTest(filename=filename, invalid="repository"), patch.dict(
|
|
os.environ, source_env | {"SOURCE_REPOSITORY": "attacker/akyldash"}
|
|
), patch.object(sys, "argv", ["release_handoff.py", command]), patch.object(
|
|
release_handoff, "latest_main_sha", return_value=head_sha
|
|
):
|
|
self.assertEqual(release_handoff.main(), 1)
|
|
|
|
def test_deploy_handoff_is_dry_and_uses_the_source_release_run_id(self):
|
|
workflow = (ROOT / ".gitea/workflows/deploy-handoff.yaml").read_text(encoding="utf-8")
|
|
self.assertIn("name: Production handoff status (not deployed)", workflow)
|
|
self.assertIn("name: NOT DEPLOYED - handoff status only", workflow)
|
|
self.assertIn('workflows: ["Release production images"]', workflow)
|
|
self.assertIn("branches: [main]", workflow)
|
|
self.assertIn("types: [completed]", workflow)
|
|
self.assertIn("gitea.event.workflow_run.conclusion == 'success'", workflow)
|
|
self.assertIn("gitea.event.workflow_run.event == 'workflow_run'", workflow)
|
|
self.assertIn("gitea.event.workflow_run.head_branch == 'main'", workflow)
|
|
self.assertIn("SOURCE_RUN_ID: ${{ gitea.event.workflow_run.id }}", workflow)
|
|
self.assertNotIn("SOURCE_RUN_ID: ${{ gitea.run_id }}", workflow)
|
|
self.assertIn("gitea-api GET", workflow)
|
|
self.assertNotIn("curl ", workflow)
|
|
self.assertNotIn("AKYLDASH_DEPLOY_SSH_KEY", workflow)
|
|
self.assertNotIn("docker.sock", workflow)
|
|
self.assertIn("host handoff are not verified", workflow)
|
|
self.assertIn("DEPLOYMENT STATUS: NOT DEPLOYED", workflow)
|
|
self.assertIn("No Windows execution runner or automatic runtime binding has been verified.", workflow)
|
|
self.assertNotIn("receive_external.py", workflow)
|
|
self.assertNotIn("akyldash-production-windows", workflow)
|
|
|
|
def test_source_guards_reject_wrong_event_or_stale_main(self):
|
|
valid = dict(
|
|
workflow="CI", workflow_path="ci.yaml@refs/heads/main", repository="admin/akyldash",
|
|
event="push", conclusion="success", branch="main",
|
|
run_id="301", head_sha="a" * 40, stage="release", current_main="a" * 40,
|
|
)
|
|
validate_source(**valid)
|
|
for change in (
|
|
{"event": "pull_request"},
|
|
{"branch": "feature/test"},
|
|
{"conclusion": "failure"},
|
|
{"workflow": "untrusted workflow"},
|
|
{"workflow_path": "untrusted.yaml@refs/heads/main"},
|
|
{"repository": "untrusted/repository"},
|
|
{"head_sha": "A" * 40},
|
|
{"run_id": "3/../1"},
|
|
{"current_main": "b" * 40},
|
|
):
|
|
with self.subTest(change=change), self.assertRaises(ValueError):
|
|
validate_source(**(valid | change))
|
|
|
|
def test_handoff_tuple_binds_artifact_run_commit_and_digest(self):
|
|
run_id = "302"
|
|
head_sha = "a" * 40
|
|
digest = "b" * 64
|
|
response = {
|
|
"artifacts": [{
|
|
"id": 9,
|
|
"name": f"release-descriptor-{run_id}-{digest}",
|
|
"expired": False,
|
|
"workflow_run": {
|
|
"id": int(run_id), "head_sha": head_sha, "head_branch": "main",
|
|
"event": "workflow_run", "conclusion": "success",
|
|
},
|
|
}]
|
|
}
|
|
request = descriptor_request(response, run_id=run_id, head_sha=head_sha)
|
|
self.assertEqual(request, {"run_id": run_id, "commit": head_sha, "descriptor_sha256": digest})
|
|
self.assertEqual(set(request), {"run_id", "commit", "descriptor_sha256"})
|
|
for artifact in (
|
|
{**response["artifacts"][0], "name": f"release-descriptor-303-{digest}"},
|
|
{**response["artifacts"][0], "name": f"release-descriptor-{run_id}-not-a-digest"},
|
|
{**response["artifacts"][0], "workflow_run": {"id": 303}},
|
|
{**response["artifacts"][0], "expired": True},
|
|
):
|
|
with self.subTest(artifact=artifact), self.assertRaises(ValueError):
|
|
descriptor_request({"artifacts": [artifact]}, run_id=run_id, head_sha=head_sha)
|
|
with self.assertRaises(ValueError):
|
|
descriptor_request({"artifacts": [response["artifacts"][0], response["artifacts"][0]]}, run_id=run_id, head_sha=head_sha)
|
|
|
|
def test_dry_run_validates_the_tuple_without_opening_a_host_connection(self):
|
|
run_id = "302"
|
|
head_sha = "a" * 40
|
|
artifacts = {
|
|
"artifacts": [{
|
|
"name": f"release-descriptor-{run_id}-{'b' * 64}",
|
|
"expired": False,
|
|
"workflow_run": {
|
|
"id": int(run_id), "head_sha": head_sha, "head_branch": "main",
|
|
"event": "workflow_run", "conclusion": "success",
|
|
},
|
|
}]
|
|
}
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
metadata = Path(directory) / "artifacts.json"
|
|
metadata.write_text(json.dumps(artifacts), encoding="utf-8")
|
|
environment = {
|
|
"SOURCE_WORKFLOW": "Release production images",
|
|
"SOURCE_WORKFLOW_PATH": "release-images.yaml@refs/heads/main",
|
|
"SOURCE_EVENT": "workflow_run",
|
|
"SOURCE_CONCLUSION": "success",
|
|
"SOURCE_BRANCH": "main",
|
|
"SOURCE_RUN_ID": run_id,
|
|
"SOURCE_SHA": head_sha,
|
|
"SOURCE_REPOSITORY": "admin/akyldash",
|
|
}
|
|
output = io.StringIO()
|
|
with (
|
|
patch.dict(os.environ, environment),
|
|
patch.object(sys, "argv", ["release_handoff.py", "dry-run", "--artifact-metadata", str(metadata)]),
|
|
patch.object(release_handoff, "latest_main_sha", return_value=head_sha),
|
|
contextlib.redirect_stdout(output),
|
|
):
|
|
self.assertEqual(release_handoff.main(), 0)
|
|
self.assertIn("descriptor_sha256=" + "b" * 64, output.getvalue())
|
|
self.assertIn("ZIP bytes were not fetched", output.getvalue())
|
|
self.assertIn("no host connection was opened", output.getvalue())
|
|
|
|
def test_pr_ci_has_small_synthetic_artifact_upload_without_registry_secrets(self):
|
|
workflow = (ROOT / ".gitea/workflows/ci.yaml").read_text(encoding="utf-8")
|
|
self.assertIn("Verify Gitea runtime artifact upload", workflow)
|
|
self.assertIn(GITEA_UPLOAD_ARTIFACT, workflow)
|
|
self.assertIn("retention-days: 1", workflow)
|
|
self.assertIn("if-no-files-found: error", workflow)
|
|
self.assertNotIn("github.run_attempt", workflow)
|
|
self.assertIn("${{ github.run_id }}", workflow)
|
|
self.assertIn("artifact-smoke/marker.txt", workflow)
|
|
self.assertNotIn("REGISTRY_PASSWORD", workflow)
|
|
|
|
def test_release_checksum_survives_flattened_artifact_layout(self):
|
|
workflow = (ROOT / ".gitea/workflows/release-images.yaml").read_text(encoding="utf-8")
|
|
generate = "(cd release && sha256sum release-manifest.json > release-manifest.json.sha256)"
|
|
verify = "(cd release && sha256sum -c release-manifest.json.sha256)"
|
|
self.assertIn(generate, workflow)
|
|
self.assertIn(verify, workflow)
|
|
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
root = Path(temp)
|
|
release = root / "release"
|
|
release.mkdir()
|
|
(release / "release-manifest.json").write_text(
|
|
"synthetic manifest\n", encoding="utf-8"
|
|
)
|
|
|
|
generated = subprocess.run(
|
|
["bash", "-e", "-c", generate], cwd=root, text=True, capture_output=True
|
|
)
|
|
self.assertEqual(generated.returncode, 0, generated.stderr)
|
|
verified = subprocess.run(
|
|
["bash", "-e", "-c", verify], cwd=root, text=True, capture_output=True
|
|
)
|
|
self.assertEqual(verified.returncode, 0, verified.stderr)
|
|
|
|
extracted = root / "artifact-root"
|
|
extracted.mkdir()
|
|
(extracted / "release-manifest.json").write_bytes((release / "release-manifest.json").read_bytes())
|
|
(extracted / "release-manifest.json.sha256").write_bytes(
|
|
(release / "release-manifest.json.sha256").read_bytes()
|
|
)
|
|
downloaded = subprocess.run(
|
|
["sha256sum", "-c", "release-manifest.json.sha256"],
|
|
cwd=extracted,
|
|
text=True,
|
|
capture_output=True,
|
|
)
|
|
self.assertEqual(downloaded.returncode, 0, downloaded.stderr)
|
|
|
|
def test_pr_build_remains_no_push_and_push_action_has_no_engine_socket(self):
|
|
build_action = (ROOT / ".gitea/actions/kaniko-build/action.yml").read_text(encoding="utf-8")
|
|
push_action = (ROOT / ".gitea/actions/kaniko-push/release-build.sh").read_text(encoding="utf-8")
|
|
self.assertIn("--no-push", build_action)
|
|
self.assertIn("--no-push-cache", build_action)
|
|
self.assertIn("REGISTRY_USERNAME:?", push_action)
|
|
self.assertIn("REGISTRY_PASSWORD:?", push_action)
|
|
self.assertNotIn("docker.sock", push_action)
|
|
self.assertNotIn("DOCKER_HOST", push_action)
|
|
self.assertIn("--digest-file=", push_action)
|
|
|
|
def test_release_validator_accepts_workflow_tags_without_credentials_or_kaniko(self):
|
|
workflow = (ROOT / ".gitea/workflows/release-images.yaml").read_text(encoding="utf-8")
|
|
dockerfile = ROOT / ".gitea/actions/kaniko-push/Dockerfile"
|
|
release_builder = (ROOT / ".gitea/actions/kaniko-push/release-build.sh").read_text(encoding="utf-8")
|
|
validator = ROOT / ".gitea/actions/kaniko-push/validate-destination.sh"
|
|
self.assertIn(
|
|
"COPY validate-destination.sh /usr/local/bin/validate-destination.sh",
|
|
dockerfile.read_text(encoding="utf-8"),
|
|
)
|
|
self.assertIn(
|
|
'/busybox/sh /usr/local/bin/validate-destination.sh "$dockerfile" "$destination"',
|
|
release_builder,
|
|
)
|
|
self.assertIn(
|
|
"destination: git.vakanaut.ru/admin/akyldash/backend:${{ gitea.event.workflow_run.head_sha }}-${{ gitea.run_id }}",
|
|
workflow,
|
|
)
|
|
self.assertIn(
|
|
"destination: git.vakanaut.ru/admin/akyldash/opensearch:${{ gitea.event.workflow_run.head_sha }}-${{ gitea.run_id }}",
|
|
workflow,
|
|
)
|
|
examples = (
|
|
("deploy/production/Dockerfile.backend", "git.vakanaut.ru/admin/akyldash/backend"),
|
|
("deploy/production/Dockerfile.opensearch", "git.vakanaut.ru/admin/akyldash/opensearch"),
|
|
)
|
|
environment = {"PATH": os.environ.get("PATH", "/usr/bin:/bin")}
|
|
for dockerfile_path, repository in examples:
|
|
destination = f"{repository}:{'a' * 40}-567"
|
|
with self.subTest(repository=repository):
|
|
accepted = subprocess.run(
|
|
["sh", str(validator), dockerfile_path, destination],
|
|
text=True,
|
|
capture_output=True,
|
|
env=environment,
|
|
)
|
|
self.assertEqual(accepted.returncode, 0, accepted.stderr)
|
|
|
|
for invalid in (
|
|
destination + "-1",
|
|
destination.replace(repository, "registry.invalid/other/image"),
|
|
f"{repository}:{'A' * 40}-567",
|
|
f"{repository}:{'a' * 39}-567",
|
|
f"{repository}:{'a' * 40}-",
|
|
f"{repository}:{'a' * 40}-not-a-run-id",
|
|
):
|
|
rejected = subprocess.run(
|
|
["sh", str(validator), dockerfile_path, invalid],
|
|
text=True,
|
|
capture_output=True,
|
|
env=environment,
|
|
)
|
|
self.assertNotEqual(rejected.returncode, 0, invalid)
|
|
|
|
rejected_dockerfile = subprocess.run(
|
|
["sh", str(validator), "deploy/production/Dockerfile.unapproved", destination],
|
|
text=True,
|
|
capture_output=True,
|
|
env=environment,
|
|
)
|
|
self.assertNotEqual(rejected_dockerfile.returncode, 0)
|
|
|
|
def test_forced_command_dispatcher_has_only_exact_status_and_deploy_literals(self):
|
|
dispatcher = ROOT / "infra/deploy/akyldash-release-dispatch.sh"
|
|
installer = (ROOT / "infra/deploy/install-helper.sh").read_text(encoding="utf-8")
|
|
source = dispatcher.read_text(encoding="utf-8")
|
|
self.assertEqual(subprocess.run(["bash", "-n", str(dispatcher)]).returncode, 0)
|
|
self.assertIn('/usr/local/sbin/akyldash-release-status)', source)
|
|
self.assertIn('/usr/local/sbin/akyldash-release-deploy)', source)
|
|
self.assertIn('[[ $EUID -ne 0 || $# -ne 0 ]]', source)
|
|
self.assertNotIn("eval", source)
|
|
self.assertIn('install -o root -g root -m 0750 "$release_dispatch_source" /usr/local/sbin/akyldash-release-dispatch', installer)
|
|
self.assertIn('bash -n "$release_deploy_source" "$release_rollback_source" "$release_dispatch_source"', installer)
|
|
rejected = subprocess.run(
|
|
["bash", str(dispatcher)], env={**os.environ, "SSH_ORIGINAL_COMMAND": "id"},
|
|
text=True, capture_output=True,
|
|
)
|
|
self.assertNotEqual(rejected.returncode, 0)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|