This repository has been archived on 2026-11-08. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
platform-ci/tests/test_outer_scoped_identity.py
forge-recoveryandClaude Sonnet 5.5 a36f17b857
KofTwentyTwo/ci-policy: ci / fork-guard (pull_request) Skipped
KofTwentyTwo/ci-policy: ci / go-0 (pull_request) Skipped
KofTwentyTwo/ci-policy: ci / go-nix-1 (pull_request) Skipped
KofTwentyTwo/ci-policy: ci / gradle-2 (pull_request) Skipped
KofTwentyTwo/ci-policy: ci / gradle-3 (pull_request) Skipped
KofTwentyTwo/ci-policy: ci / java-docker-4 (pull_request) Skipped
KofTwentyTwo/ci-policy: ci / java-maven-5 (pull_request) Skipped
KofTwentyTwo/ci-policy: ci / manifests-6 (pull_request) Skipped
KofTwentyTwo/ci-policy: ci / nix-7 (pull_request) Skipped
KofTwentyTwo/ci-policy: ci / node-8 (pull_request) Skipped
KofTwentyTwo/ci-policy: ci / python-9 (pull_request) Skipped
KofTwentyTwo/ci-policy: ci / rust-10 (pull_request) Skipped
KofTwentyTwo/ci-policy: ci / rust-11 (pull_request) Skipped
KofTwentyTwo/ci-policy: ci / rust-12 (pull_request) Skipped
KofTwentyTwo/ci-policy: ci / source-config-13 (pull_request) Skipped
KofTwentyTwo/ci-policy: ci / source-config-14 (pull_request) Skipped
KofTwentyTwo/ci-policy: ci / image-build-0 (pull_request) Skipped
KofTwentyTwo/ci-policy: ci / image-build-1 (pull_request) Skipped
KofTwentyTwo/ci-policy: ci / image-build-2 (pull_request) Skipped
KofTwentyTwo/ci-policy: ci / image-build-3 (pull_request) Skipped
KofTwentyTwo/ci-policy: ci / image-build-4 (pull_request) Skipped
KofTwentyTwo/ci-policy: ci / image-build-5 (pull_request) Skipped
KofTwentyTwo/ci-policy: ci / image-build-6 (pull_request) Skipped
KofTwentyTwo/ci-policy: ci / image-build-7 (pull_request) Skipped
KofTwentyTwo/ci-policy: ci / not-enrolled (pull_request) Skipped
KofTwentyTwo/ci-policy: ci / content-validate-0 (pull_request) Skipped
KofTwentyTwo/ci-policy: ci / standards-0 (pull_request) Skipped
KofTwentyTwo/ci-policy: ci / node-matrix-0 (pull_request) Skipped
KofTwentyTwo/ci-policy: ci / source-config-15 (pull_request) Blocked by required conditions
KofTwentyTwo/ci-policy: Shared source and config CI / verify (pull_request) Blocked by required conditions
KofTwentyTwo/ci-policy: Shared source and config CI / secrets (pull_request) Blocked by required conditions
KofTwentyTwo/ci-policy: Shared source and config CI / gate (pull_request) Waiting for needed jobs
KofTwentyTwo/ci-policy: ci / ci-result (pull_request) Waiting for needed jobs
KofTwentyTwo/ci-policy: native / fork-deny (pull_request) Skipped
KofTwentyTwo/ci-policy: native / build-macos-0 (pull_request) Skipped
KofTwentyTwo/ci-policy: native / build-macos-1 (pull_request) Skipped
KofTwentyTwo/ci-policy: native / build-macos-2 (pull_request) Skipped
KofTwentyTwo/ci-policy: native / build-macos-3 (pull_request) Skipped
KofTwentyTwo/ci-policy: native / build-windows-4 (pull_request) Skipped
KofTwentyTwo/ci-policy: native / build-windows-5 (pull_request) Skipped
KofTwentyTwo/ci-policy: native / build-windows-6 (pull_request) Skipped
KofTwentyTwo/ci-policy: native / build-windows-7 (pull_request) Skipped
KofTwentyTwo/ci-policy: native / dotnet-8 (pull_request) Skipped
KofTwentyTwo/ci-policy: native / dotnet-9 (pull_request) Skipped
KofTwentyTwo/ci-policy: native / dotnet-10 (pull_request) Skipped
KofTwentyTwo/ci-policy: native / dotnet-11 (pull_request) Skipped
KofTwentyTwo/ci-policy: native / swift-12 (pull_request) Skipped
KofTwentyTwo/ci-policy: native / swift-13 (pull_request) Skipped
KofTwentyTwo/ci-policy: native / swift-14 (pull_request) Skipped
KofTwentyTwo/ci-policy: native / swift-15 (pull_request) Skipped
KofTwentyTwo/ci-policy: native / swift-16 (pull_request) Skipped
KofTwentyTwo/ci-policy: native / swift-17 (pull_request) Skipped
KofTwentyTwo/ci-policy: native / swift-18 (pull_request) Skipped
KofTwentyTwo/ci-policy: native / swift-19 (pull_request) Skipped
KofTwentyTwo/ci-policy: native / native-result (pull_request) Canceled after 6s
feat(ci): advance shared pipeline capabilities
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-05 10:30:25 +00:00

163 lines
9.5 KiB
Python

"""A run delegated through an OUTER scoped workflow is identified by the outer source commit, with the nested prepare commit validated, never substituted.
The outer dispatcher names its own opt-in job `Scoped workflow [workflow <sha>]`; the server evaluates that name exactly as it does the canonical `Prepare plan [workflow <sha>]`.
Doubles reuse the listing/jobs shapes of `test_scoped_identity`; live values (the label on a real run, the nested commit) still need qualification.
"""
import unittest
import urllib.parse
from typing import ClassVar, override
from platform_ci.delivery.enrollment import _parse_scoped_workflows # pyright: ignore[reportPrivateUsage]
from platform_ci.delivery.errors import EnrollmentError
from platform_ci.delivery.forge import CurrentRun, RepositoryScope, current_workflow_trust
from platform_ci.delivery.scoped_identity import ScopedRunIdentity, ScopedSourceExpectation, ScopedWorkflow, prove_scoped_run
from platform_ci.pipeline_contract import OUTER_IDENTITY_JOB_NAME_PATTERN, normalized_job_name
from tests.support import PLATFORM_REVISION
from tests.synthetic_services import REPOSITORY, REPOSITORY_ID
from tests.test_scoped_identity import HEAD, RUN_ID, ListingForge, listed_run, named
OUTER_ID = 167
OUTER_SHA = "1" * 40
NESTED_SHA = "2" * 40
OTHER_SHA = "3" * 40
EXPECTED = ScopedSourceExpectation(OUTER_ID, "ci.yaml", HEAD, "push")
def outer(sha: str = OUTER_SHA) -> dict[str, object]:
"""The outer opt-in job as the jobs listing gives it."""
return named(f"Scoped workflow [workflow {sha}]")
def prepare(sha: str) -> dict[str, object]:
"""The nested canonical prepare job."""
return named(f"Prepare plan [workflow {sha}]")
def forge_for(jobs: list[dict[str, object]], source: int = OUTER_ID) -> ListingForge:
"""A forge whose source-filtered listing contains the run only for `source`."""
return ListingForge({source: [listed_run()]}, jobs)
class RunForge(ListingForge):
"""Adds the one run object `current_workflow_trust` reads."""
@override
def get(self, path: str) -> object:
"""Answer the run object, everything else as the listing double does."""
if urllib.parse.urlsplit(path).path == f"/repos/{REPOSITORY}/actions/runs/{RUN_ID}":
return {"id": RUN_ID, "path": "ci.yaml@refs/heads/develop", "event": "push"}
return super().get(path)
def scope(*workflows: ScopedWorkflow) -> RepositoryScope:
"""An enrolled scope trusting nothing but the given scoped workflows."""
return RepositoryScope(REPOSITORY, REPOSITORY_ID, 1000, 10, frozenset({PLATFORM_REVISION}), scoped_workflows=frozenset(workflows))
class OuterIdentityTest(unittest.TestCase):
"""What the outer identity proves and what it refuses."""
def test_the_outer_commit_is_the_identity_and_an_equal_nested_commit_adds_nothing(self) -> None:
identity = prove_scoped_run(forge_for([outer(), prepare(OUTER_SHA), named("Report")]), REPOSITORY, RUN_ID, EXPECTED)
self.assertEqual(identity, ScopedRunIdentity(RUN_ID, OUTER_ID, "ci.yaml", OUTER_SHA, ""))
def test_a_different_nested_commit_is_carried_separately_and_never_becomes_the_identity(self) -> None:
identity = prove_scoped_run(forge_for([outer(), prepare(NESTED_SHA)]), REPOSITORY, RUN_ID, EXPECTED)
self.assertEqual(identity, ScopedRunIdentity(RUN_ID, OUTER_ID, "ci.yaml", OUTER_SHA, NESTED_SHA))
assert identity is not None
self.assertEqual(identity.workflow, ScopedWorkflow(OUTER_ID, "ci.yaml", OUTER_SHA, NESTED_SHA))
def test_a_directly_scoped_canonical_run_is_identified_exactly_as_before(self) -> None:
identity = prove_scoped_run(forge_for([prepare(NESTED_SHA), named("Report")]), REPOSITORY, RUN_ID, EXPECTED)
self.assertEqual(identity, ScopedRunIdentity(RUN_ID, OUTER_ID, "ci.yaml", NESTED_SHA, ""))
def test_malformed_repeated_or_conflicting_identities_prove_nothing(self) -> None:
cases = {
"short outer": [named("Scoped workflow [workflow " + OUTER_SHA[:39] + "]"), prepare(OUTER_SHA)],
"outer suffix": [named(f"Scoped workflow [workflow {OUTER_SHA}] extra"), prepare(OUTER_SHA)],
"outer label only": [named("Scoped workflow"), prepare(OUTER_SHA)],
"uppercase outer": [named(f"Scoped workflow [workflow {OUTER_SHA.upper().replace('1', 'A')}]"), prepare(OUTER_SHA)],
"two outer jobs, same": [outer(), outer(), prepare(OUTER_SHA)],
"two outer jobs, different": [outer(), outer(OTHER_SHA), prepare(OUTER_SHA)],
"outer without a nested prepare": [outer(), named("Report")],
"two different nested commits": [outer(), prepare(OUTER_SHA), prepare(NESTED_SHA)],
"malformed outer beside a good one": [outer(), named("Scoped workflow [workflow x]"), prepare(OUTER_SHA)],
}
for label, jobs in cases.items():
with self.subTest(label):
self.assertIsNone(prove_scoped_run(forge_for(jobs), REPOSITORY, RUN_ID, EXPECTED))
def test_a_job_of_another_run_does_not_count(self) -> None:
foreign: dict[str, object] = {"name": f"Scoped workflow [workflow {OTHER_SHA}]", "run_id": RUN_ID + 1}
identity = prove_scoped_run(forge_for([outer(), foreign, prepare(OUTER_SHA)]), REPOSITORY, RUN_ID, EXPECTED)
self.assertEqual(identity, ScopedRunIdentity(RUN_ID, OUTER_ID, "ci.yaml", OUTER_SHA, ""))
def test_a_wrong_source_fails_closed_whatever_the_job_names_say(self) -> None:
"""The id the pipeline asks about only selects the server-side filter: asking about another source (the old constant, a typo) lists no run."""
jobs = [outer(), prepare(OUTER_SHA)]
for asked in (3, OUTER_ID + 1):
with self.subTest(asked):
self.assertIsNone(prove_scoped_run(forge_for(jobs), REPOSITORY, RUN_ID, ScopedSourceExpectation(asked, "ci.yaml", HEAD, "push")))
def test_the_label_is_not_a_required_job_name_and_the_prepare_name_still_normalizes(self) -> None:
match = OUTER_IDENTITY_JOB_NAME_PATTERN.fullmatch(f"Scoped workflow [workflow {OUTER_SHA}]")
assert match is not None
self.assertEqual(match.group(1), OUTER_SHA)
self.assertEqual(normalized_job_name(f"Scoped workflow [workflow {OUTER_SHA}]"), f"Scoped workflow [workflow {OUTER_SHA}]")
self.assertEqual(normalized_job_name(f"Prepare plan [workflow {NESTED_SHA}]"), "Prepare plan")
class OuterTrustTest(unittest.TestCase):
"""The current run's trust compares like with like: the outer commit through the enrollment, the nested commit with the recorded nested value."""
def trust(self, jobs: list[dict[str, object]], env_sha: str, *workflows: ScopedWorkflow, source: int = OUTER_ID) -> RepositoryScope | None:
"""Run the current-run trust lookup against a forge double holding one outer-delegated run."""
return current_workflow_trust(RunForge({OUTER_ID: [listed_run()]}, jobs), scope(*workflows), CurrentRun(RUN_ID, HEAD, source, env_sha))
def test_an_equal_nested_commit_is_validated_against_the_outer_commit(self) -> None:
jobs = [outer(), prepare(OUTER_SHA)]
trusted = self.trust(jobs, OUTER_SHA)
assert trusted is not None
self.assertEqual(trusted.scoped_workflows, frozenset({ScopedWorkflow(OUTER_ID, "ci.yaml", OUTER_SHA)}))
self.assertIsNone(self.trust(jobs, NESTED_SHA), "a nested value the job does not record must be refused, not dropped")
def test_a_different_nested_commit_must_equal_the_recorded_one_and_is_pinned_by_the_enrollment(self) -> None:
jobs = [outer(), prepare(NESTED_SHA)]
trusted = self.trust(jobs, NESTED_SHA)
assert trusted is not None
self.assertEqual(trusted.scoped_workflows, frozenset({ScopedWorkflow(OUTER_ID, "ci.yaml", OUTER_SHA, NESTED_SHA)}))
self.assertIsNone(self.trust(jobs, OUTER_SHA), "the outer commit is not the nested value")
self.assertIsNone(self.trust(jobs, OTHER_SHA))
def test_the_enrollment_must_pin_both_values_exactly(self) -> None:
"""The scope returned for the current run is what later runs are matched against: a pin that omits or changes either value matches nothing."""
identity_without_pin = ScopedWorkflow(OUTER_ID, "ci.yaml", OUTER_SHA)
identity_with_pin = ScopedWorkflow(OUTER_ID, "ci.yaml", OUTER_SHA, NESTED_SHA)
self.assertNotEqual(identity_without_pin, identity_with_pin)
self.assertNotEqual(identity_with_pin, ScopedWorkflow(OUTER_ID, "ci.yaml", OUTER_SHA, OTHER_SHA))
self.assertNotEqual(identity_with_pin, ScopedWorkflow(OUTER_ID, "ci.yaml", NESTED_SHA))
def test_a_wrong_or_missing_source_id_fails_closed(self) -> None:
jobs = [outer(), prepare(OUTER_SHA)]
for source in (3, OUTER_ID + 1, 0):
with self.subTest(source):
self.assertIsNone(self.trust(jobs, OUTER_SHA, source=source))
class EnrollmentPinTest(unittest.TestCase):
"""The optional nested pin of a scoped workflow entry."""
BASE: ClassVar[dict[str, object]] = {"source_repository_id": OUTER_ID, "workflow_file": "ci.yaml", "workflow_sha": OUTER_SHA}
def test_the_pin_is_optional_and_exact(self) -> None:
self.assertEqual(_parse_scoped_workflows([self.BASE]), (ScopedWorkflow(OUTER_ID, "ci.yaml", OUTER_SHA),))
self.assertEqual(_parse_scoped_workflows([self.BASE | {"callee_workflow_sha": NESTED_SHA}]), (ScopedWorkflow(OUTER_ID, "ci.yaml", OUTER_SHA, NESTED_SHA),))
def test_a_malformed_or_redundant_pin_is_refused(self) -> None:
for pin in ("short", NESTED_SHA.upper().replace("2", "A"), OUTER_SHA, "", 5):
with self.subTest(pin), self.assertRaises(EnrollmentError):
_parse_scoped_workflows([self.BASE | {"callee_workflow_sha": pin}])
with self.assertRaises(EnrollmentError):
_parse_scoped_workflows([self.BASE | {"unknown": "x"}])