diff --git a/.github/workflows/iam-data-drift.yml b/.github/workflows/iam-data-drift.yml index 7577f43545..4088024567 100644 --- a/.github/workflows/iam-data-drift.yml +++ b/.github/workflows/iam-data-drift.yml @@ -77,6 +77,7 @@ on: - 'tools/aws/regen_partitions.py' - 'tools/aws/regen_query_services.py' - '.github/workflows/iam-data-drift.yml' + - 'tools/aws/test_iam_data_verify.py' schedule: # Mondays at 06:00 UTC, before the working week rather than during it. - cron: '0 6 * * 1' @@ -110,6 +111,9 @@ jobs: with: python-version: "3.x" + - name: Test IAM verification selection + run: python3 -m unittest discover -s tools/aws -p test_iam_data_verify.py + # IAM role-tag coverage also depends on which operations name a role in pinned botocore. - name: Install the pinned botocore run: pip install -r tools/aws/requirements.txt @@ -121,7 +125,11 @@ jobs: EVENT_NAME: ${{ github.event_name }} run: | if [ "$EVENT_NAME" = pull_request ]; then - make iam-data-verify CHANGED="$(git diff --name-only HEAD^1 HEAD | tr '\n' ' ')" + changed_file="$RUNNER_TEMP/iam-changed-paths.txt" + if ! git diff --name-only HEAD^1 HEAD > "$changed_file"; then + : > "$changed_file" + fi + make iam-data-verify CHANGED_FILE="$changed_file" else make iam-data-verify fi diff --git a/Makefile b/Makefile index 100abd3644..ff6661135b 100644 --- a/Makefile +++ b/Makefile @@ -584,6 +584,7 @@ iam-action-resources-verify: ## CI gate (online): every vendored IAM action reso # module that generator imports. Given CHANGED, the paths a pull request changes, iam-data-verify # runs only the checks covering one of them, so a pull request is not failed by drift in a file it # leaves alone; without it, as on the weekly run, it runs every check. +# CHANGED_FILE carries the same paths without putting a large diff in argv or the environment. IAM_PASSED_ROLES_GENERATOR := tools/aws/regen_iam_passed_roles.py tools/aws/regen_iam_authorized_actions.py \ tools/aws/regen_partitions.py tools/aws/regen_query_services.py IAM_NAMESPACES_PATHS := src/main/resources/aws/iam-service-namespaces.json \ @@ -624,9 +625,11 @@ iam-data-verify: ## CI gate (online): every vendored IAM file must still match A @# state. Sequential steps in CI stop at the first failure, which once left the action @# resource gate unexercised while the namespace list was stale. @namespaces=0; actions=0; authorized=0; passed=0; connectors=0; tagged=0; requested=0; dynamodb=0; conditionkeys=0; s3control=0; requestkeys=0; routes=0; \ + changed="$(strip $(CHANGED))"; \ + if [ -n "$(CHANGED_FILE)" ]; then changed=$$(cat "$(CHANGED_FILE)") || exit 1; fi; \ covers() { \ - [ -z "$(strip $(CHANGED))" ] && return 0; \ - for path in $(CHANGED); do case " $$1 " in *" $$path "*) return 0;; esac; done; \ + [ -z "$$changed" ] && return 0; \ + for path in $$changed; do case " $$1 " in *" $$path "*) return 0;; esac; done; \ echo "not checked, as the change touches none of its files: $$2"; \ return 1; \ }; \ diff --git a/tools/aws/test_iam_data_verify.py b/tools/aws/test_iam_data_verify.py new file mode 100644 index 0000000000..c81431da31 --- /dev/null +++ b/tools/aws/test_iam_data_verify.py @@ -0,0 +1,80 @@ +"""Exercise IAM gate selection without contacting the live AWS reference.""" +import os +from pathlib import Path +import subprocess +import tempfile +import unittest + + +class IamDataVerifyTest(unittest.TestCase): + def setUp(self): + self.temporary = tempfile.TemporaryDirectory() + self.addCleanup(self.temporary.cleanup) + self.directory = Path(self.temporary.name) + self.calls = self.directory / "calls" + self.make = self.directory / "make" + self.make.write_text( + '#!/bin/sh\nprintf "%s\\n" "$*" >> "$CALLS"\n' + '[ "$*" != "--no-print-directory $FAIL_TARGET" ]\n' + ) + self.make.chmod(0o700) + + def run_gate(self, *arguments, fail_target=""): + result = subprocess.run( + ["make", "--no-print-directory", "iam-data-verify", f"MAKE={self.make}", *arguments], + cwd=Path(__file__).resolve().parents[2], + env={**os.environ, "CALLS": str(self.calls), "FAIL_TARGET": fail_target}, + text=True, capture_output=True, + ) + calls = self.calls.read_text().splitlines() if self.calls.exists() else [] + return result, [line.split()[-1] for line in calls] + + def changed_file(self, text): + path = self.directory / "changed" + path.write_text(text) + return f"CHANGED_FILE={path}" + + def test_large_diff_selects_only_affected_check(self): + paths = "".join(f"src/test/unrelated/path_{index}.java\n" for index in range(10000)) + paths += "src/main/resources/aws/iam-service-namespaces.json\n" + self.assertGreater(len(paths), 131072) + result, calls = self.run_gate(self.changed_file(paths)) + self.assertEqual(0, result.returncode, result.stderr) + self.assertEqual(["iam-namespaces-verify"], calls) + + def test_unrelated_diff_runs_no_checks(self): + result, calls = self.run_gate(self.changed_file("docs/index.md\n")) + self.assertEqual(0, result.returncode, result.stderr) + self.assertEqual([], calls) + + def test_empty_diff_keeps_full_verification(self): + result, calls = self.run_gate(self.changed_file("")) + self.assertEqual(0, result.returncode, result.stderr) + self.assertEqual(12, len(calls)) + + def test_weekly_run_checks_every_file(self): + result, calls = self.run_gate() + self.assertEqual(0, result.returncode, result.stderr) + self.assertEqual(12, len(calls)) + + def test_legacy_changed_argument_still_selects(self): + result, calls = self.run_gate("CHANGED= tools/aws/regen_service_namespaces.py ") + self.assertEqual(0, result.returncode, result.stderr) + self.assertEqual(["iam-namespaces-verify"], calls) + + def test_missing_file_fails_without_silently_skipping(self): + result, calls = self.run_gate(f"CHANGED_FILE={self.directory / 'missing'}") + self.assertNotEqual(0, result.returncode) + self.assertEqual([], calls) + + def test_failed_check_does_not_hide_later_checks(self): + result, calls = self.run_gate(self.changed_file( + "tools/aws/regen_service_namespaces.py\ntools/aws/regen_iam_action_resources.py\n" + ), fail_target="iam-namespaces-verify") + self.assertNotEqual(0, result.returncode) + self.assertEqual(["iam-namespaces-verify", "iam-action-resources-verify"], calls) + self.assertIn("the service namespace list", result.stdout) + + +if __name__ == "__main__": + unittest.main()