diff --git a/.github/workflows/validate_georelays.yml b/.github/workflows/validate_georelays.yml new file mode 100644 index 00000000..5b3cffc5 --- /dev/null +++ b/.github/workflows/validate_georelays.yml @@ -0,0 +1,78 @@ +name: Validate GeoRelay Data + +# The weekly automation validates a relay snapshot before pushing its branch, +# but that check lives inside the scheduled job: a relay-directory change that +# arrives as a pull request is otherwise reviewed on faith. This runs the same +# validator against the reviewed baseline and publishes the resulting hash, so +# a proposed snapshot can be matched against the one the automation published. +on: + pull_request: + paths: + - "relays/online_relays_gps.csv" + - "scripts/validate_georelays.py" + - "scripts/tests/**" + - ".github/workflows/validate_georelays.yml" + +# This job runs PR-authored code. Read-only, no secrets, and never +# pull_request_target. +permissions: + contents: read + +# A superseded run has nothing left to report; the sibling PR workflows do not +# set this, so drop it if the repo prefers them uniform. +concurrency: + group: validate-georelays-${{ github.ref }} + cancel-in-progress: true + +jobs: + validate-relay-directory: + name: Validate relay directory + runs-on: ubuntu-latest + timeout-minutes: 10 + + steps: + - name: Checkout pull request + # @v5 matches the sibling read-only PR workflows. fetch_georelays.yml + # pins a SHA for a reason that does not apply here — it protects the + # code that receives that job's write-capable token; this job has none. + uses: actions/checkout@v5 + with: + persist-credentials: false + + - name: Test GeoRelay validator + # Exercises the pull request's own validator and workflow tests. The + # gate below deliberately does not use that copy. + run: python3 -m unittest discover -s scripts/tests -p "test_*.py" -v + + - name: Validate relay directory against the reviewed baseline + run: | + set -euo pipefail + + # actions/checkout gives a pull_request run the merge ref, not the + # base branch, so the reviewed copy has to be fetched explicitly. + git fetch --no-tags --depth=1 origin "+refs/heads/${GITHUB_BASE_REF}:refs/remotes/origin/${GITHUB_BASE_REF}" + git show "origin/${GITHUB_BASE_REF}:relays/online_relays_gps.csv" > "$RUNNER_TEMP/baseline.csv" + + # Gate with the reviewed validator rather than the pull request's copy, + # or a change that edits data and validator together certifies itself: + # dropping 86% of the directory passes if the same commit relaxes + # MIN_BASELINE_FRACTION. The scheduled job validates from a main + # checkout for the same reason. Stdlib-only, so a copy runs standalone. + git show "origin/${GITHUB_BASE_REF}:scripts/validate_georelays.py" > "$RUNNER_TEMP/reviewed-validator.py" + + # --output is required and receives a verbatim copy of --input. It + # stays in RUNNER_TEMP so validation can never write to the tracked + # file it is checking. + python3 "$RUNNER_TEMP/reviewed-validator.py" \ + --input relays/online_relays_gps.csv \ + --baseline "$RUNNER_TEMP/baseline.csv" \ + --output "$RUNNER_TEMP/validated-copy.csv" \ + --github-output "$RUNNER_TEMP/summary.txt" + + # Publish the hash so a reviewer can match a proposed snapshot against + # the one the automation published, instead of taking it on faith. + { + echo "### GeoRelay validation" + echo + sed 's/^/- /' "$RUNNER_TEMP/summary.txt" + } >> "$GITHUB_STEP_SUMMARY" diff --git a/scripts/tests/test_georelay_pr_workflow.py b/scripts/tests/test_georelay_pr_workflow.py new file mode 100644 index 00000000..a0032e24 --- /dev/null +++ b/scripts/tests/test_georelay_pr_workflow.py @@ -0,0 +1,90 @@ +import re +from pathlib import Path +import unittest + + +REPOSITORY_ROOT = Path(__file__).resolve().parents[2] +WORKFLOW_PATH = REPOSITORY_ROOT / ".github/workflows/validate_georelays.yml" +RELAY_DATA_PATH = "relays/online_relays_gps.csv" +VALIDATOR_PATH = "scripts/validate_georelays.py" + + +class ValidateGeoRelaysWorkflowTests(unittest.TestCase): + """The relay directory is reviewed data: a PR that changes it must be + checked by the same validator the weekly automation runs, and must publish + the hash a reviewer needs to match a proposed snapshot against the + automation's own published one.""" + + @classmethod + def setUpClass(cls) -> None: + cls.workflow = WORKFLOW_PATH.read_text(encoding="utf-8") + # Comments explain why a construct is absent and would otherwise trip + # the assertions looking for it, so directives are checked without them. + cls.directives = "\n".join( + line for line in cls.workflow.splitlines() if not line.lstrip().startswith("#") + ) + + def test_runs_on_untrusted_pull_request_trigger(self) -> None: + self.assertIn("pull_request:", self.directives) + # pull_request_target would run PR-authored code with repository secrets. + self.assertNotIn("pull_request_target", self.directives) + + def test_job_holds_no_write_scope(self) -> None: + self.assertIn("permissions:\n contents: read", self.directives) + write_scope = re.search(r"^\s*[\w-]+:\s*write\s*$", self.directives, re.MULTILINE) + self.assertIsNone(write_scope, "a job running PR-authored code must not hold a write scope") + + def test_checkout_does_not_persist_credentials(self) -> None: + self.assertIn("uses: actions/checkout@", self.directives) + self.assertIn("persist-credentials: false", self.directives) + + def test_relay_data_changes_trigger_the_job(self) -> None: + paths_block = re.search(r"paths:\n((?:\s+- .+\n)+)", self.directives) + self.assertIsNotNone(paths_block) + self.assertIn(RELAY_DATA_PATH, paths_block.group(1)) + self.assertIn(VALIDATOR_PATH, paths_block.group(1)) + + def test_validates_against_the_base_branch_copy(self) -> None: + required_fragments = [ + "python3 -m unittest discover -s scripts/tests -p \"test_*.py\" -v", + f"--input {RELAY_DATA_PATH}", + "python3 \"$RUNNER_TEMP/reviewed-validator.py\"", + "--baseline \"$RUNNER_TEMP/baseline.csv\"", + f"git show \"origin/${{GITHUB_BASE_REF}}:{RELAY_DATA_PATH}\"", + "--github-output", + "$GITHUB_STEP_SUMMARY", + ] + for fragment in required_fragments: + with self.subTest(fragment=fragment): + self.assertIn(fragment, self.directives) + + def test_gate_runs_the_reviewed_validator_not_the_pull_request_copy(self) -> None: + # A change that edits the relay data and the validator together would + # otherwise certify itself: dropping most of the directory passes if the + # same commit relaxes a threshold. The gate must read the base branch's + # validator; the PR's copy is exercised by the unit-test step instead. + self.assertIn( + f'git show "origin/${{GITHUB_BASE_REF}}:{VALIDATOR_PATH}"', + self.directives, + ) + self.assertIsNone( + re.search(rf"python3\s+\S*{re.escape(VALIDATOR_PATH)}", self.directives), + "the gate must not run the pull request's own validator, however the path is spelled", + ) + + def test_base_ref_is_fetched_before_it_is_read(self) -> None: + # actions/checkout gives a pull_request run the merge ref, so the base + # branch is not present unless the job fetches it first. + fetched = self.directives.index('git fetch --no-tags --depth=1 origin "+refs/heads/${GITHUB_BASE_REF}') + read = self.directives.index('git show "origin/${GITHUB_BASE_REF}') + self.assertLess(fetched, read) + + def test_validation_cannot_write_to_the_reviewed_file(self) -> None: + # --output is required and receives a verbatim copy of --input. Pointed + # at the tracked file, a validation run would write to reviewed data. + self.assertIn("--output \"$RUNNER_TEMP/", self.directives) + self.assertNotIn(f"--output {RELAY_DATA_PATH}", self.directives) + + +if __name__ == "__main__": + unittest.main()