diff --git a/scripts/tests/run.py b/scripts/tests/run.py index 8111f05..cc65ab2 100755 --- a/scripts/tests/run.py +++ b/scripts/tests/run.py @@ -192,11 +192,14 @@ # check-policy-admission.py is an approval gate, deciding whether the fleet is # admissible at Enforce; check-image-verification.py holds the signature- # verification contract; check-policy-validity.py holds every rendered -# ImageValidatingPolicy to the digest pin that verification depends on; and +# ImageValidatingPolicy to the digest pin that verification depends on; # verdicts.py decides what each refusal, run and time bound on the verification -# path in check-digest-rewrite.sh and kyverno-test.sh established. Each carries -# 100 lines and 100 branches in PER_GATE_FLOORS, so a branch added to any of -# them without a test fails this run rather than lowering a ratchet. +# path in check-digest-rewrite.sh and kyverno-test.sh established; and +# mask-aws-ids.py and check-workflow-id-masking.py, vendored from landing-zone, +# keep every AWS account id out of this public repository's Actions logs. Each +# carries 100 lines and 100 branches in PER_GATE_FLOORS, the id-masking gate +# just under for the reason given beside it, so a branch added to any of them +# without a test fails this run rather than lowering a ratchet. # The coverage floors over the gate scripts, enforced when `coverage` is # collecting. Statements and branches are measured apart, each against its own @@ -230,7 +233,7 @@ # just under what the suite reaches, so a covered file cannot regress silently, # and raising one is what lands with the tests that earn it. A file with no # branches reads as covering all of them. -PER_GATE_FLOORS: dict[str, tuple[int, int]] = { +PER_GATE_FLOORS: dict[str, tuple[float, float]] = { # The fork-safety gate blocks in CI, and its unit tests reach every line and # branch of it: the pattern, the floor, and the blocking and report-only modes. "scripts/check-hardcoded-org.py": (100, 100), @@ -250,6 +253,18 @@ "scripts/check-image-verification.py": (100, 100), "scripts/check-policy-validity.py": (100, 100), "scripts/verdicts.py": (100, 100), + # The mask script's core and the account-id gate, vendored from landing-zone + # (scripts/vendored.json). Their tests are their own self-tests, run under the + # suite: the gate's over this repository's workflows and the cases + # scripts/workflow-id-masking.toml plants. The core is at 100. The gate is + # held to the tenth just under what it reaches, so one more line or branch + # left unreached still fails. What it leaves, one line and two branches, is + # taken only by a tree with no scripts/vendored.json, whose policy vendors + # nothing. That is landing-zone's own tree, which reaches them there, and + # never a copy's. The self-test plants no such tree. When it does, the gate + # is re-vendored with its record and held at 100 here. + "scripts/mask-aws-ids.py": (100, 100), + "scripts/check-workflow-id-masking.py": (99.9, 99.6), # Every gate that reads a catalog chart reads what this renders, so a # branch no test reaches here is a render nothing proves. "scripts/catalog.py": (100, 100), @@ -272,12 +287,6 @@ "scripts/check-lb-scheme-inputs.py": (69, 73), # Both verdicts over stubbed reads, --sync and --self-test. "scripts/check-chart-deprecation.py": (86, 80), - # The account-id gate and the mask script's core, vendored from landing-zone - # (scripts/vendored.json). Their self-tests run under the suite, the gate's - # over this repository's workflows and scripts/workflow-id-masking.toml; the - # floors are what those self-tests reach here. - "scripts/check-workflow-id-masking.py": (98, 97), - "scripts/mask-aws-ids.py": (99, 98), } @@ -482,12 +491,12 @@ def check_coverage() -> int: f"the gate was renamed, or its tests stopped importing it" ) continue - for what, pct, floor in ( + for what, pct, file_floor in ( ("lines", figures.lines, line_floor), ("branches", figures.branches, branch_floor), ): - if pct < floor: - failures.append(f"{rel}: {what} at {shown(pct)} are below its {floor}% floor") + if pct < file_floor: + failures.append(f"{rel}: {what} at {shown(pct)} are below its {file_floor}% floor") # Read from every Python file in scripts/, so a gate added tomorrow is in # the population without anyone remembering to add it. diff --git a/scripts/tests/test_workflow_id_masking.py b/scripts/tests/test_workflow_id_masking.py index bf985d7..e02d959 100644 --- a/scripts/tests/test_workflow_id_masking.py +++ b/scripts/tests/test_workflow_id_masking.py @@ -6,9 +6,15 @@ tests are their own self-tests: the gate's plants every case scripts/workflow-id-masking.toml lists against this repository's workflows, and every rule's built-in shapes, policy refusals, floor shortfalls, vendored-record -failures and exit statuses; the mask core's runs its own cases. These run both in -this process, so the coverage run measures the vendored code, and require the -gate to pass the tree as it is. +failures and exit statuses; the mask core's runs its own cases. Both run here in +this process, the mask core's through its main() as `mask-aws-ids.sh +--self-test` runs it in CI, so the coverage run measures the vendored code, and +the gate must pass the tree as it is. + +One thing the gate's self-test proves runs in a child process, which the +coverage run does not see. Without tomllib or PyYAML the gate stops with exit 2 +rather than reporting a pass, so that refusal is run here as well, in this +process. A vendored file is held by its digest, not by this repository's formatter: ruff would re-lay it, and the copy would then no longer be the bytes recorded. So each @@ -22,8 +28,11 @@ import io import json import pathlib +import runpy +import sys import tomllib import unittest +from unittest import mock from gateloader import load @@ -31,6 +40,7 @@ mask = load("mask-aws-ids") ROOT = pathlib.Path(__file__).resolve().parents[2] +GATE = ROOT / "scripts" / "check-workflow-id-masking.py" def quiet(fn, *args, **kwargs) -> tuple[int, str]: @@ -54,11 +64,30 @@ def test_the_gate_rejects_every_case_the_policy_plants(self): self.assertIn("check-workflow-id-masking self-test passed", out) def test_the_mask_core_masks_every_case_it_carries(self): - code, out = quiet(mask.self_test) + code, out = quiet(mask.main, ["--self-test"]) self.assertEqual(code, 0, out[-4000:]) self.assertIn("mask-aws-ids self-test passed", out) +class TheGateWithoutWhatItReads(unittest.TestCase): + def test_the_gate_cannot_run_without_tomllib_or_pyyaml(self): + for module, says in ( + ("tomllib", "tomllib is not available"), + ("yaml", "PyYAML is not installed"), + ): + with self.subTest(module=module): + err = io.StringIO() + with ( + mock.patch.dict(sys.modules, {module: None}), + contextlib.redirect_stderr(err), + self.assertRaises(SystemExit) as raised, + ): + runpy.run_path(str(GATE), run_name=f"without_{module}") + self.assertEqual(raised.exception.code, gate.CANNOT_RUN) + self.assertIn(says, err.getvalue()) + self.assertIn("Refusing to report a pass", err.getvalue()) + + class TheVendoredFiles(unittest.TestCase): def test_ruff_excludes_exactly_the_vendored_python(self): record = json.loads((ROOT / "scripts/vendored.json").read_text()) diff --git a/scripts/workflow-id-masking.toml b/scripts/workflow-id-masking.toml index d58b899..1b27189 100644 --- a/scripts/workflow-id-masking.toml +++ b/scripts/workflow-id-masking.toml @@ -52,6 +52,11 @@ ECR_PUBLIC_PULL_ROLE_ARN = "the ECR Public pull role trusts this repository's pu # mutated file the finding must cite; otherwise it is a line `new` adds. # `must_say` holds words of the rule the case plants, which a finding at that # line must carry, so another rule firing there does not pass the case. +# +# Each [[accepted]] (at the end) edits a copy of one workflow in a way the rules +# permit, and the gate must pass the tree with it, reporting nothing anywhere. +# `old` must occur exactly once and is replaced by `new`, both strings. These +# keep the rules from refusing what they must allow. [[mutation]] name = 'role-to-assume from a variable' @@ -115,6 +120,19 @@ new = ''' ''' must_say = 'the whole `vars`' +[[mutation]] +name = 'every variable value through an object filter' +file = 'ci.yml' +old = ''' + run: ./scripts/render-addons.py --fetch +''' +new = ''' + env: + ALL: ${{ join(vars.*, ',') }} + run: ./scripts/render-addons.py --fetch +''' +must_say = 'a computed index or filter of the `vars`' + [[mutation]] name = 'every secret in a job-level env' file = 'ci.yml' @@ -147,6 +165,24 @@ new = ''' ''' must_say = 'can hold AWS credentials' +[[mutation]] +name = 'a variable read in a job granted write-all' +file = 'diff.yml' +old = ''' + diff: + runs-on: ubuntu-latest + timeout-minutes: 30 +''' +new = ''' + diff: + runs-on: ubuntu-latest + timeout-minutes: 30 + permissions: write-all + env: + CLUSTER: ${{ vars.DIFF_CLUSTER }} +''' +must_say = 'can hold AWS credentials' + [[mutation]] name = 'an id-named key fed from a variable of another name' file = 'ci.yml' @@ -160,6 +196,19 @@ new = ''' ''' must_say = 'sets ACCOUNT_ID' +[[mutation]] +name = 'a role ARN assembled around an account id secret' +file = 'ci.yml' +old = ''' + run: ./scripts/render-addons.py --fetch +''' +new = ''' + env: + ECR_ROLE_ARN: arn:aws:iam::${{ secrets.ECR_PUBLIC_ACCOUNT_ID }}:role/ecr-public-pull + run: ./scripts/render-addons.py --fetch +''' +must_say = 'sets ECR_ROLE_ARN' + [[mutation]] name = 'an account id as a dispatch input' file = 'diff.yml' @@ -251,6 +300,20 @@ env: ''' must_say = 'passes the id secret' +[[mutation]] +name = 'an id-named key in a workflow-level env' +file = 'ci.yml' +old = ''' +env: + # renovate: datasource=github-releases depName=kyverno/kyverno +''' +new = ''' +env: + # renovate: datasource=github-releases depName=kyverno/kyverno + ECR_PUBLIC_PULL_ROLE_ARN: ${{ secrets.ECR_PUBLIC_PULL_ROLE_ARN }} +''' +must_say = 'a secret or a mask-step output cannot be here' + [[mutation]] name = 'the secret in a job-level env' file = 'ci.yml' @@ -329,6 +392,17 @@ new = " deploy-probe:\n runs-on: ubuntu-latest\n timeout-minutes: 5\n line = '^\s+DEPLOY_ROLE_ARN: \$\{\{ secrets\.DEPLOY_ROLE_ARN \}\}' must_say = 'no job it needs' +[[mutation]] +name = 'an environment id whose needs share a job, none refusing the inherited name' +file = 'ci.yml' +old = ''' + merge-gate: + name: merge gate +''' +new = " deploy-probe:\n needs: [helm-render, policy-admission]\n runs-on: ubuntu-latest\n timeout-minutes: 5\n environment: probe\n steps:\n - name: Mask\n env:\n DEPLOY_ROLE_ARN: ${{ secrets.DEPLOY_ROLE_ARN }}\n ROLE_ARN_VARIABLE: ${{ vars.DEPLOY_ROLE_ARN != '' }}\n run: |\n bash \"$GITHUB_WORKSPACE/scripts/mask-aws-ids.sh\" DEPLOY_ROLE_ARN\n\n merge-gate:\n name: merge gate\n" +line = '^\s+DEPLOY_ROLE_ARN: \$\{\{ secrets\.DEPLOY_ROLE_ARN \}\}' +must_say = 'no job it needs' + [[mutation]] name = 'a step receives the secret before the mask step' file = 'ci.yml' @@ -594,6 +668,13 @@ old = "ROLE_ARN_VARIABLE: ${{ vars.ECR_PUBLIC_PULL_ROLE_ARN != '' }}" new = 'ROLE_ARN_VARIABLE: ${{ vars.ECR_PUBLIC_PULL_ROLE_ARN }}' must_say = 'from `vars.`' +[[mutation]] +name = 'the guard written with a double-quoted string' +file = 'ci.yml' +old = "ROLE_ARN_VARIABLE: ${{ vars.ECR_PUBLIC_PULL_ROLE_ARN != '' }}" +new = 'ROLE_ARN_VARIABLE: ${{ vars.ECR_PUBLIC_PULL_ROLE_ARN != "" }}' +must_say = 'this gate cannot read' + [[mutation]] name = 'a literal account id' file = 'ci.yml' @@ -697,3 +778,47 @@ new = ''' - name: Install Helm uses: azure/setup-helm@''' must_say = 'as a literal' + +# --- permitted edits ----------------------------------------------------------- + +[[accepted]] +name = 'a comment above the mask command' +file = 'ci.yml' +old = ''' + run: | + bash "$GITHUB_WORKSPACE/scripts/mask-aws-ids.sh" --optional ECR_PUBLIC_PULL_ROLE_ARN +''' +new = ''' + run: | + # Nothing in this step prints before the mask command. + bash "$GITHUB_WORKSPACE/scripts/mask-aws-ids.sh" --optional ECR_PUBLIC_PULL_ROLE_ARN +''' + +[[accepted]] +name = "AWS's documentation account id in a message" +file = 'ci.yml' +old = 'is not an IAM role ARN (arn:aws:iam:::role/).' +new = 'is not an IAM role ARN (one shaped like arn:aws:iam::123456789012:role/).' + +[[accepted]] +name = 'a secret named outside the rule, read in a job that can hold AWS credentials' +file = 'ci.yml' +old = ''' + run: ./scripts/render-addons.py --fetch +''' +new = ''' + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: ./scripts/render-addons.py --fetch +''' + +[[accepted]] +name = 'an id-named key fed from an output of the mask step' +file = 'ci.yml' +old = ''' + ECR_PUBLIC_REGION: *ecr-public-region +''' +new = ''' + ECR_PUBLIC_REGION: *ecr-public-region + ACCOUNT_ID: ${{ steps.ecr-public-role.outputs.account_id }} +'''