infra: Task 28 review 2, refuse unread inputs and unproven arguments
plan-check refuses a root or module holding .tf.json, .tofu or override files, or a .tf file its reader cannot parse, before proving anything. A module argument feeding an unknown policy must be a literal or a plain root variable reference, proven from source and equal to the plan's constant; any other expression is refused. Regressions: a JSON override widening the connector policy with the same references, and account_id = replace(var.run, ...).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+166
−16
docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md+16 −0infra/hosted/README.md+4 −1infra/hosted/scripts/plan-check.py+79 −15infra/hosted/scripts/plan_check_test.py+67 −0
CI failed — run details
CI log
=== prepare ===
ci-box: machine: aarch64, 32 CPUs, 62 GiB memory, kernel 7.0.0-1017-aws
ci-box: python 3.12.3; Docker version 29.1.3, build 29.1.3-0ubuntu3~24.04.2
ci-box: user namespaces allowed: 233151
ci-box: cgroup: cgroup2fs
ci-box: disk /home/ci/work/repos/oficina__equipo/wt/hosted-runners-2: Type Size Avail | ext4 96G 84G
ci-box: disk /home/ci/.cache/equipo-ci-box: Type Size Avail | ext4 96G 84G
ci-box: bwrap: fetched and verified in 0.3s
ci-box: claude: fetched and verified in 2.0s
ci-box: codex: fetched and verified in 4.5s
ci-box: codex-code-mode-host: fetched and verified in 1.6s
ci-box: libwrap: fetched and verified in 0.3s
ci-box: socat: fetched and verified in 0.4s
ci-box: tofu: fetched and verified in 2.0s
npm warn deprecated node-domexception@1.0.0: Use your platform's native DOMException instead
ci-box: image pulled in 10.0s
ci-box: pi: installed and verified in 7.4s
ci-box: opencode: installed and verified in 2.3s
ci-box: chromium 156.0.8078.79-1~deb13u1: 76 packages fetched, verified and unpacked in 8.7s
ci-box: clone deepened in 6.4s
ci-box: file times set from content: 3978 files, 298 directories
ci-box: ready in 21.0s
=== copy ===
EQUIPO_RELEASE_COPY_CHECK=1 CGO_ENABLED=0 go test ./internal/equipotest -run '^TestReleaseCopyReviewed$' -count=1
--- FAIL: TestReleaseCopyReviewed (0.09s)
task_pages_test.go:236: COPY REVIEW REQUIRED: copy-status.py verify found copy pending review or stale bookkeeping
FAIL
rastrillo-budget/v1 ran 87ms
FAIL amadan.net/oficina/equipo/internal/equipotest 0.236s
FAIL
make: *** [Makefile:297: copy-release-check] Error 1
amadan: step copy failed: exit status 2
amadan: job failed at step copy — its output is above, not at the tail of this log
diff --git a/docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md b/docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md| index c599801b..cd84758f 100644 |
| --- a/docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md |
| +++ b/docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md |
| @@ -2321,3 +2321,19 @@ Mutations (each restored and checked identical): |
| - **R9** proxy A's status alarm on `var.proxy_b_instance_id`. Same command: exit 1, `tests/alerts.tftest.hcl line 38`, "an alarm's metric, evaluation or dimensions differ from account plan 5.1". Restored. (plan-check side: `Alarms.test_substituted_host_refused` and `test_substituted_host_at_the_module_call_refused`.) |
| - **R10** the heartbeat's `actions_enabled` pin removed from phases.json (services). `python3 -I infra/hosted/scripts/plan_check_test.py`: exit 1, `FAIL: test_disabled_actions_refused: [] is not true : the plan was accepted` (and `test_alarms_pin_actions_evaluation_and_dimensions` errors on the missing key). Restored. |
| - **R11** `managed_policy_arns = ["arn:aws:iam::aws:policy/AdministratorAccess"]` on the controller role in modules/config/main.tf. `tofu -chdir=infra/hosted/envs/prod/config test`: exit 1, run "stack" fail at `tests/iam.tftest.hcl line 32` (the host role assertion). Restored. |
| + |
| +### Task 28 review round 2 (Astra): finding 1, two bypasses |
| + |
| +RED: six new tests in `plan_check_test.py`, all `FAIL` against the round-1 checker (92 tests, `FAILED (failures=6)`): `SourceInputs.test_json_override_widening_a_policy_with_the_same_references_refused`, `test_other_unread_inputs_refused`, `test_unparseable_configuration_refused`, `ModuleArguments.test_transformed_account_argument_refused` (root `run = "149757027610"`, `account_id = replace(var.run, "149757027610", "123456789012")`), `test_interpolated_argument_refused`, `test_argument_literal_differing_from_the_plan_refused`. |
| + |
| +Fixes (refusal, not more parsing): |
| +- **(a)** Before any proof, plan-check reads the root and every module it calls. A `.tf.json`, `.tofu`, `.tofu.json`, `override.tf` or `*_override.tf` file, or a `.tf` file the reader cannot parse, refuses the plan ("the configuration is not provable: ..."), and every proof from that directory fails too. |
| +- **(b)** A module variable used in an unknown policy is proven from the module call in source: it must be a literal, or a plain reference to a root variable the plan has. A function call, interpolation or anything else is refused, and so is a missing argument (a default is not proven). The literal must also equal the plan's recorded constant. |
| + |
| +GREEN: `make hosted-infra-check` exit 0: 92 plan-check tests OK, fmt clean, seven roots valid, 15 tofu test runs passed. |
| + |
| +Mutations (each restored and checked identical with `cmp`): |
| +- **S1** unread inputs not detected (`unread = []`). `python3 -I infra/hosted/scripts/plan_check_test.py`: exit 1, `FAIL: test_json_override_widening_a_policy_with_the_same_references_refused` and `test_other_unread_inputs_refused`, each `[] is not true : the plan was accepted`. Restored. |
| +- **S2** inputs not checked before proving (the `check_inputs` refusals dropped). Same command: exit 1, `FAIL` on all three `SourceInputs` tests, `'the configuration is not provable' not found in ...`. Restored. |
| +- **S3** a module variable taken from the plan's dependency again (`argument_value` replaced by `module_var_value`). Same command: exit 1, `FAIL: test_transformed_account_argument_refused: the plan was accepted`; `test_interpolated_argument_refused: 'region' not found`; `test_argument_literal_differing_from_the_plan_refused: 'account_id' not found`. Restored. |
| +- **S4** an argument the reader cannot evaluate falls back to the plan's dependency (the refusal replaced by `value = self.module_var_value(prefix, name)`). Same command: exit 1, `FAIL: test_transformed_account_argument_refused: the plan was accepted` and `test_interpolated_argument_refused: 'region' not found`. Restored. |
diff --git a/infra/hosted/README.md b/infra/hosted/README.md| index f0e43ba7..455fd575 100644 |
| --- a/infra/hosted/README.md |
| +++ b/infra/hosted/README.md |
| @@ -88,7 +88,10 @@ It refuses a plan unless all of the following hold: |
| the account, a listed bucket, or a listed AWS-owned ARN. |
| An unknown planned value passes only where its pin allows one, and only if |
| -the configuration proves it. plan-check reads the phase's root (named in |
| +the configuration proves it. The root and its modules may hold only `.tf` |
| +files this reader parses: a `.tf.json`, `.tofu` or override file is |
| +refused before any proof. A module argument that feeds a proven value must |
| +be a literal or a plain root variable reference. plan-check reads the phase's root (named in |
| phases.json) and its modules from this checkout, and checks at every step |
| that the plan's configuration records the same references as the source. |
diff --git a/infra/hosted/scripts/plan-check.py b/infra/hosted/scripts/plan-check.py| index 758ff534..43e957e3 100644 |
| --- a/infra/hosted/scripts/plan-check.py |
| +++ b/infra/hosted/scripts/plan-check.py |
| @@ -20,7 +20,9 @@ A plan is accepted only if all of these hold: |
| X's addresses, each in the prior state, with delete or no-op actions, and |
| the prior state holds nothing else. |
| - Configuration: every planned create has exactly one configuration entry, |
| - in a module the plan's configuration has, from infra/hosted/modules. |
| + in a module the plan's configuration has, from infra/hosted/modules. The |
| + root and its modules hold only .tf files this reader parses: a .tf.json, |
| + .tofu or .tofu.json file, or an override file, is refused before any proof. |
| - Pinned values (create phases): each attribute phases.json pins equals the |
| planned value. A planned value that is unknown passes only where the pin |
| allows it, and only if the trusted configuration proves it: plan-check |
| @@ -46,7 +48,8 @@ Pin rules in phases.json, per attribute path ("block.0.attr"): |
| and the planned value equals that variable's |
| {"policy": {...}} a JSON policy equal by structure; if unknown, the |
| source must be jsonencode(...) whose value, with |
| - module variables from the plan and only the |
| + each module variable proven from the module call |
| + (a literal or a plain root variable) and only the |
| "unknown_refs" left as ${type.name.attr}, is the |
| pinned document |
| {"length": N} a known list of N elements |
| @@ -616,17 +619,47 @@ class Source: |
| if not rel or not re.match(r"^\.\./\.\./\.\./modules/[a-z_]+$", rel): |
| raise HclError("module %s has source %r, not one under infra/hosted/modules" % (ctx, src)) |
| directory = os.path.normpath(os.path.join(self.root, rel)) |
| - items = [] |
| - try: |
| - names = sorted(n for n in os.listdir(directory) if n.endswith(".tf")) |
| - except OSError as e: |
| - raise HclError("cannot read the source of %s: %s" % (ctx or "the root", e)) |
| - for n in names: |
| - with open(os.path.join(directory, n)) as f: |
| - items += parse_body(f.read())[0] |
| - self.bodies[ctx] = items |
| + self.bodies[ctx] = self.read_directory(directory, ctx or "the root") |
| return self.bodies[ctx] |
| + def read_directory(self, directory, what): |
| + """Every item of a configuration directory. OpenTofu also reads JSON |
| + configuration, .tofu files and override files, which this reader does |
| + not merge, so any of them makes the directory unprovable.""" |
| + try: |
| + names = sorted(n for n in os.listdir(directory) if os.path.isfile(os.path.join(directory, n))) |
| + except OSError as e: |
| + raise HclError("cannot read the source of %s: %s" % (what, e)) |
| + unread = [n for n in names if n.endswith((".tf.json", ".tofu", ".tofu.json")) |
| + or re.match(r"^(.*_)?override\.tf$", n)] |
| + if unread: |
| + raise HclError("the source of %s has configuration this reader does not merge: %s" % (what, ", ".join(unread))) |
| + items = [] |
| + for n in names: |
| + if not n.endswith(".tf"): |
| + continue |
| + with open(os.path.join(directory, n)) as f: |
| + text = f.read() |
| + try: |
| + items += parse_body(text)[0] |
| + except HclError as e: |
| + raise HclError("cannot read %s in the source of %s: %s" % (n, what, e)) |
| + return items |
| + |
| + def check_inputs(self): |
| + """Read the root and every module it calls, before anything is proven.""" |
| + out = [] |
| + try: |
| + root = self.body("") |
| + except HclError as e: |
| + return [str(e)] |
| + for b in self.blocks(root, "module"): |
| + try: |
| + self.body(b[2][0]) |
| + except HclError as e: |
| + out.append(str(e)) |
| + return out |
| + |
| def blocks(self, items, kind, *labels): |
| return [b for b in items if b[0] == "block" and b[1] == kind and tuple(b[2][:len(labels)]) == labels] |
| @@ -838,10 +871,7 @@ class Plan: |
| if kind == "trav" and all(isinstance(x, str) for x in node[1]): |
| parts = node[1] |
| if parts[0] == "var" and len(parts) == 2: |
| - v = self.module_var_value(prefix, parts[1]) |
| - if v is MISSING: |
| - raise HclError("var.%s has no known value" % parts[1]) |
| - return v |
| + return self.argument_value(ctx, prefix, parts[1]) |
| if parts[0] == "local" and len(parts) == 2: |
| return self.evaluate(ctx, prefix, self.source.local(ctx, parts[1]), allowed) |
| if ".".join(parts) in allowed: |
| @@ -849,6 +879,36 @@ class Plan: |
| raise HclError("the policy uses %s, which the pin does not allow" % describe(node)) |
| + def argument_value(self, ctx, prefix, name): |
| + """A variable's value, proven from source: in a module, the call's |
| + argument must be a literal or a plain root variable reference.""" |
| + if ctx == "": |
| + if name not in self.declared: |
| + raise HclError("var.%s is not a variable of the plan" % name) |
| + return self.variables[name] |
| + src = self.source.attr_source(self.source.module_call(ctx), name) |
| + if src is None: |
| + raise HclError("module %s does not set %s; a default is not proven" % (ctx, name)) |
| + node = parse_expression(src) |
| + self.agree(node, "call", ctx, name) |
| + if node[0] == "tmpl" and all(isinstance(x, str) for x in node[1]): |
| + value = "".join(node[1]) |
| + elif node[0] in ("num", "lit"): |
| + value = node[1] |
| + elif node[0] == "trav" and len(node[1]) == 2 and node[1][0] == "var" and isinstance(node[1][1], str): |
| + if node[1][1] not in self.declared: |
| + raise HclError("module %s argument %s is var.%s, which the plan does not have" % (ctx, name, node[1][1])) |
| + value = self.variables[node[1][1]] |
| + else: |
| + raise HclError("module %s argument %s is %s, not a literal or a plain variable reference" |
| + % (ctx, name, describe(node) if node[0] != "tmpl" else "an interpolated string")) |
| + expr = (self.calls.get(ctx, {}).get("expressions") or {}).get(name) or {} |
| + if "constant_value" in expr and canon(expr["constant_value"]) != canon(value): |
| + raise HclError("module %s argument %s is %s in the source and %s in the plan" |
| + % (ctx, name, canon(value), canon(expr["constant_value"]))) |
| + return value |
| + |
| + |
| def describe(node): |
| if node[0] == "trav": |
| return ".".join(str(x) for x in node[1]) |
| @@ -875,6 +935,10 @@ def check(plan, phase_name, account, region, phases, hosted=None): |
| if plan.get("errored"): |
| out.append("the plan errored") |
| p = Plan(plan, hosted or HOSTED, ph.get("root")) |
| + if p.source is not None: |
| + # Before anything is proven: every input of the root and its modules |
| + # is one this reader handles, or nothing from that source is trusted. |
| + out += ["the configuration is not provable: %s" % e for e in p.source.check_inputs()] |
| out += check_providers(p, account, region) |
| out += check_configuration(p) |
| listed = ph.get("resources") or {} |
diff --git a/infra/hosted/scripts/plan_check_test.py b/infra/hosted/scripts/plan_check_test.py| index b8332218..d2b7ff4e 100644 |
| --- a/infra/hosted/scripts/plan_check_test.py |
| +++ b/infra/hosted/scripts/plan_check_test.py |
| @@ -554,6 +554,73 @@ class SourceProofs(Base): |
| self.assertRefused(check(p), "network", "source") |
| +class SourceInputs(Base): |
| + """Round 2, finding 1a: inputs the reader does not handle are refused |
| + before anything is proven.""" |
| + |
| + JSON_OVERRIDE = {"resource": {"aws_iam_role_policy": {"connector_delete_eni": {"policy": ( |
| + "${jsonencode({Version = \"2012-10-17\", Statement = [{Effect = \"Allow\", Action = \"*\", Resource = \"*\"}," |
| + " {Effect = \"Allow\", Action = \"ec2:DeleteNetworkInterface\"," |
| + " Resource = \"arn:aws:ec2:${var.region}:${var.account_id}:subnet/${aws_subnet.vm.id}\"}]})}")}}}} |
| + |
| + def test_json_override_widening_a_policy_with_the_same_references_refused(self): |
| + with Source() as src: |
| + path = os.path.join(src.hosted, "modules/network/security_override.tf.json") |
| + with open(path, "w") as f: |
| + json.dump(self.JSON_OVERRIDE, f) |
| + self.assertRefused(check(FRESH, hosted=src.hosted), "the configuration is not provable", "security_override.tf.json") |
| + |
| + def test_other_unread_inputs_refused(self): |
| + for rel, text in (("modules/network/override.tf", "# empty\n"), |
| + ("modules/network/extra_override.tf", "# empty\n"), |
| + ("modules/network/main.tofu", "# empty\n"), |
| + ("modules/network/x.tofu.json", "{}\n"), |
| + ("envs/prod/network/root_override.tf", "# empty\n"), |
| + ("envs/prod/network/more.tf.json", "{}\n")): |
| + with Source() as src: |
| + with open(os.path.join(src.hosted, rel), "w") as f: |
| + f.write(text) |
| + self.assertRefused(check(FRESH, hosted=src.hosted), "the configuration is not provable", os.path.basename(rel)) |
| + |
| + def test_unparseable_configuration_refused(self): |
| + with Source() as src: |
| + with open(os.path.join(src.hosted, "modules/network/broken.tf"), "w") as f: |
| + f.write('resource "aws_vpc" "x" {\n') |
| + self.assertRefused(check(FRESH, hosted=src.hosted), "the configuration is not provable", "broken.tf") |
| + |
| + |
| +class ModuleArguments(Base): |
| + """Round 2, finding 1b: a module argument feeding an unknown value must |
| + be a plain variable reference or a literal, proven from source.""" |
| + |
| + def test_transformed_account_argument_refused(self): |
| + # The run variable holds the account; the argument rewrites it to |
| + # another account. The plan records only a dependency on var.run. |
| + with Source() as src: |
| + src.edit("envs/prod/network/main.tf", 'account_id = "149757027610"', |
| + 'account_id = replace(var.run, "149757027610", "123456789012")') |
| + p = copy.deepcopy(FRESH) |
| + p["variables"]["run"] = {"value": "149757027610"} |
| + p["configuration"]["root_module"]["module_calls"]["network"]["expressions"]["account_id"] = { |
| + "references": ["var.run"]} |
| + self.assertRefused(check(p, hosted=src.hosted), "connector_delete_eni", "account_id", "replace") |
| + |
| + def test_interpolated_argument_refused(self): |
| + with Source() as src: |
| + src.edit("envs/prod/network/main.tf", 'region = "eu-west-1"', |
| + 'region = "${var.vm_az}"') |
| + p = copy.deepcopy(FRESH) |
| + p["configuration"]["root_module"]["module_calls"]["network"]["expressions"]["region"] = { |
| + "references": ["var.vm_az"]} |
| + self.assertRefused(check(p, hosted=src.hosted), "connector_delete_eni", "region") |
| + |
| + def test_argument_literal_differing_from_the_plan_refused(self): |
| + p = copy.deepcopy(FRESH) |
| + p["configuration"]["root_module"]["module_calls"]["network"]["expressions"]["account_id"] = { |
| + "constant_value": "123456789012"} |
| + self.assertRefused(check(p), "connector_delete_eni", "account_id") |
| + |
| + |
| class VariablePins(Base): |
| """Finding 2: a variable pin holds the planned value to the root variable.""" |
| CTL = "module.controller." |