carlos / platform Public

Guard fixed identities across all region plan resource changes

Refuse changes that reference fixed instance, ENI, EIP, SG or IAM identities,
except target attachment creates/deletes and exact reviewed updates. Preserve
guarded names on moves and keep ASG decrease checks independent.

Add failing-before regression fixtures for review F1-F3, reject empty plans,
remove the duplicate ingress mock and regenerate the census proposal.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 5a420161044f026a415312cdbf8c7044e516322a parent 9007043
9 files changed, +460 −50
  • docs/superpowers/notes/census-proposal-region-ha-p3a.md +1 −1
  • docs/superpowers/notes/census-region-ha-p3a-claims.tsv +1 −1
  • infra/modules/region-host/ingress/tests/group_ingress.tftest.hcl +0 −8
  • internal/provision/testdata/plan14-root-reads-chain.tsv +1 −1
  • internal/provision/testdata/plan14-root-reads-region-ha-p3a.tsv +3 −3
  • scripts/check-region-group-plan.py +66 −36
  • scripts/check_region_group_plan_test.py +123 −0
  • scripts/testdata/region-group-plan/fixed-identities.json +74 −0
  • scripts/testdata/region-group-plan/fixed-identity-probes.json +191 −0

Intent

Human

<task-notification> <task-id>b3acmcc7e</task-id> <tool-use-id>toolu_015yMHsrmerHbHqCeEmJkmrW</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/6cb8c43c-d494-478a-8f04-1067486903df/tasks/b3acmcc7e.output</output-file> <status>completed</status> <summary>Background command "W=/home/paulca/amadan.net/carlos/platform/.claude/worktrees/region-ha-p3/.superpowers/sdd/2026-11-03-region-ha-p3a; until [ -s $W/task-2-astra-a.md ]; do sleep 60; done; cat $W/task-2-astra-a.md" completed (exit code 0)</summary> </task-notification>

Human

<task-notification> <task-id>bz7rfad26</task-id> <tool-use-id>toolu_01UWP9q6R2XDK4hWfKg1iNDY</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/6cb8c43c-d494-478a-8f04-1067486903df/tasks/bz7rfad26.output</output-file> <status>failed</status> <summary>Background command "W=/home/paulca/amadan.net/carlos/platform/.claude/worktrees/region-ha-p3/.superpowers/sdd/2026-11-03-region-ha-p3a; until [ -s $W/task-1-astra-fix1.md ]; do sleep 60; done; cat $W/task-1-astra-fix1.md" failed with exit code 1</summary> </task-notification>

Attributed to this commit: 21.1 g CO₂e (2,383,883 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on region-ha-p3-group, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.

diff --git a/docs/superpowers/notes/census-proposal-region-ha-p3a.md b/docs/superpowers/notes/census-proposal-region-ha-p3a.md
index 2f45b81f2..d3e6a98fc 100644
--- a/docs/superpowers/notes/census-proposal-region-ha-p3a.md
+++ b/docs/superpowers/notes/census-proposal-region-ha-p3a.md
@@ -32,6 +32,6 @@ New dispositions, which census-propose wrote and the reviewer checks against the
- new `internal/host/boxroles.go:boxRoleReader.pass`: reads +file:Read; written legacy, accepted-legacy, frozen
- new `internal/host/boxroles_config_linux.go:readRoleRegionConfig`: reads +file:Fstat, +file:Openat2, +file:ReadAll; written legacy, accepted-legacy, frozen
-Seal: `c68e2453bfc8afaf3794eb4c08850428982fa6954269b6b99bddfda4e6242929` → `a27d94a93013e3e0c9f69bacad970ece395fb39d9b0f3c7cac0c47ab55542dbf`.
+Seal: `c68e2453bfc8afaf3794eb4c08850428982fa6954269b6b99bddfda4e6242929` → `ad0fc07cb89ad62b50b6f816621bf18256850fdb1540e5979e7b5edc132f6baf`.
Overlay: `internal/provision/testdata/plan14-root-reads-region-ha-p3a.tsv`, appended to `internal/provision/testdata/plan14-root-reads-chain.tsv`.
diff --git a/docs/superpowers/notes/census-region-ha-p3a-claims.tsv b/docs/superpowers/notes/census-region-ha-p3a-claims.tsv
index f6912495b..d8ec023f5 100644
--- a/docs/superpowers/notes/census-region-ha-p3a-claims.tsv
+++ b/docs/superpowers/notes/census-region-ha-p3a-claims.tsv
@@ -4,7 +4,7 @@ infra/modules/region-host/ingress/tests/fixed-targets/main.tf:external Add dev-r
infra/modules/region-host/ingress/tests/fixed-targets/tests/targets.tftest.hcl:external Add plan-only mock witnesses for target attachments and out-of-zone refusal; no live API calls or root runtime reads.
infra/modules/region-host/ingress/tests/fixed-targets/variables.tf:external Define explicit ingress and literal target inputs for the dev-root fixture; no root runtime reads.
infra/modules/region-host/ingress/tests/fixed-targets/versions.tf:external Declare OpenTofu/AWS provider requirements for the dev-root fixture; no root runtime reads.
-infra/modules/region-host/ingress/tests/group_ingress.tftest.hcl:external Add plan-only mock tests for NLB protocol, health, zone/VPC boundary, protected allocations and restricted SG traffic; no live API calls or root runtime reads.
+infra/modules/region-host/ingress/tests/group_ingress.tftest.hcl:external Add plan-only mock tests for NLB protocol, health, zone/VPC boundary, protected allocations and restricted SG traffic; remove the duplicate empty-subnet-result test in Task 1 fix round 1; no live API calls or root runtime reads.
infra/modules/region-host/ingress/variables.tf:external Add ingress-only inputs and topology/CIDR validation; no root runtime reads.
infra/modules/region-host/ingress/versions.tf:external Declare OpenTofu and AWS provider requirements for the isolated ingress child; no root runtime reads.
infra/modules/region-host/tests/fixed_state_unchanged.tftest.hcl:external Add mock regression for the unchanged fixed instance/profile/SG/root-volume wiring and legacy outputs; no root runtime reads.
diff --git a/infra/modules/region-host/ingress/tests/group_ingress.tftest.hcl b/infra/modules/region-host/ingress/tests/group_ingress.tftest.hcl
index 5c90e6ee2..083d37296 100644
--- a/infra/modules/region-host/ingress/tests/group_ingress.tftest.hcl
+++ b/infra/modules/region-host/ingress/tests/group_ingress.tftest.hcl
@@ -147,14 +147,6 @@ run "wrong_vpc_refused" {
}
expect_failures = [aws_lb.nlb]
}
-run "wrong_zone_refused" {
- command = plan
- override_data {
- target = data.aws_subnets.zone
- values = { ids = [] }
- }
- expect_failures = [aws_lb.nlb]
-}
run "wrong_cidr_refused" {
command = plan
variables { target_vpc_cidrs = ["0.0.0.0/0"] }
diff --git a/internal/provision/testdata/plan14-root-reads-chain.tsv b/internal/provision/testdata/plan14-root-reads-chain.tsv
index 415ef587f..e05cba085 100644
--- a/internal/provision/testdata/plan14-root-reads-chain.tsv
+++ b/internal/provision/testdata/plan14-root-reads-chain.tsv
@@ -75,4 +75,4 @@ plan14-root-reads-domains-check-country.tsv b0fc198c53bb0dacba33a54c3d8bf8fe5ac0
plan14-root-reads-static-stream-extract.tsv 7fb932b118e67e23b33a374c6a4020b38976667c7ec4cfb855c0f8b598b1756b
plan14-root-reads-ls284.tsv 744b16d970f22476b79982e57c326157ca5137fa96ccaee4a7ae10c25248d124
plan14-root-reads-region-ha-p1b3b-t.tsv c68e2453bfc8afaf3794eb4c08850428982fa6954269b6b99bddfda4e6242929
-plan14-root-reads-region-ha-p3a.tsv a27d94a93013e3e0c9f69bacad970ece395fb39d9b0f3c7cac0c47ab55542dbf
+plan14-root-reads-region-ha-p3a.tsv ad0fc07cb89ad62b50b6f816621bf18256850fdb1540e5979e7b5edc132f6baf
diff --git a/internal/provision/testdata/plan14-root-reads-region-ha-p3a.tsv b/internal/provision/testdata/plan14-root-reads-region-ha-p3a.tsv
index 2b6f33f5a..249136880 100644
--- a/internal/provision/testdata/plan14-root-reads-region-ha-p3a.tsv
+++ b/internal/provision/testdata/plan14-root-reads-region-ha-p3a.tsv
@@ -8,7 +8,7 @@
@legacy b113d1b975dae34ecf57a2b5c98e94c8d6f89fd7476e321202c25886b866cb08 infra/modules/region-host/ingress/tests/fixed-targets/tests/targets.tftest.hcl external legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p3a;basis=Add plan-only mock witnesses for target attachments and out-of-zone refusal; no live API calls or root runtime reads. file:external@0 9fc255e828aaff0d55d156caa417e40fcf3afb8d9ef29b025f4178ca669799fb
@legacy 5d72b4a4f0a34f5cc4ef70092940738197fb5c1aa4ec382127cabdd9c82702bc infra/modules/region-host/ingress/tests/fixed-targets/variables.tf external legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p3a;basis=Define explicit ingress and literal target inputs for the dev-root fixture; no root runtime reads. file:external@0 644a067756d767c5668463eca9c1e4a45af902aacf140ef9cca437f3a24efde4
@legacy 5f2c8be4b3fe6775f9542426e5c729a4b16115dafa420b0e63040f71bd80f685 infra/modules/region-host/ingress/tests/fixed-targets/versions.tf external legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p3a;basis=Declare OpenTofu/AWS provider requirements for the dev-root fixture; no root runtime reads. file:external@0 bb3be812da255c0f031b2bfdfee462e4687de3681c742b529b55693a8875136b
-@legacy aa756aff32e21027c70a4766381edfbb2acfc2bacf921909a5dfe735816f4a0c infra/modules/region-host/ingress/tests/group_ingress.tftest.hcl external legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p3a;basis=Add plan-only mock tests for NLB protocol, health, zone/VPC boundary, protected allocations and restricted SG traffic; no live API calls or root runtime reads. file:external@0 ab86b091ae5ef1bfcd20d99b0643d85d8eb888a003e47bf06e28922c5adbdc40
+@legacy 32d1441eaf910683f882c5fa9399470814b595490b6c5bb2ef3a51dfbb62796c infra/modules/region-host/ingress/tests/group_ingress.tftest.hcl external legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p3a;basis=Add plan-only mock tests for NLB protocol, health, zone/VPC boundary, protected allocations and restricted SG traffic; remove the duplicate empty-subnet-result test in Task 1 fix round 1; no live API calls or root runtime reads. file:external@0 36665eefee5d2a298571fa56f805159eb0dab79d3bd3b0913b5bbcc07a50d42c
@legacy 0c7e79156fc210a3460e9cbce1a7e26edbf22f0c799710f3e82e1a09a483d994 infra/modules/region-host/ingress/variables.tf external legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p3a;basis=Add ingress-only inputs and topology/CIDR validation; no root runtime reads. file:external@0 a1599da50d212d5e64f527719ee781e5a0a494185d144f0a958433741f4c9d36
@legacy 134888d1b095e8268cdd1cd04df27087407ae3fee1c4742f0df5a431520783ef infra/modules/region-host/ingress/versions.tf external legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p3a;basis=Declare OpenTofu and AWS provider requirements for the isolated ingress child; no root runtime reads. file:external@0 bb3be812da255c0f031b2bfdfee462e4687de3681c742b529b55693a8875136b
@legacy 73e967bb630a74f28c53f50b63c997d086154b025cf7b59074d5b0fe2fc3bea7 infra/modules/region-host/tests/fixed_state_unchanged.tftest.hcl external legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p3a;basis=Add mock regression for the unchanged fixed instance/profile/SG/root-volume wiring and legacy outputs; no root runtime reads. file:external@0 cab0fdd9e01c4497f06a617db6c41aa9887da80bd0518c76fe8eba935be42c85
@@ -23,7 +23,7 @@ infra/modules/region-host/ingress/tests/fixed-targets/main.tf external legacy ac
infra/modules/region-host/ingress/tests/fixed-targets/tests/targets.tftest.hcl external legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p3a;basis=Add plan-only mock witnesses for target attachments and out-of-zone refusal; no live API calls or root runtime reads. file:external@0 9fc255e828aaff0d55d156caa417e40fcf3afb8d9ef29b025f4178ca669799fb
infra/modules/region-host/ingress/tests/fixed-targets/variables.tf external legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p3a;basis=Define explicit ingress and literal target inputs for the dev-root fixture; no root runtime reads. file:external@0 644a067756d767c5668463eca9c1e4a45af902aacf140ef9cca437f3a24efde4
infra/modules/region-host/ingress/tests/fixed-targets/versions.tf external legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p3a;basis=Declare OpenTofu/AWS provider requirements for the dev-root fixture; no root runtime reads. file:external@0 bb3be812da255c0f031b2bfdfee462e4687de3681c742b529b55693a8875136b
-infra/modules/region-host/ingress/tests/group_ingress.tftest.hcl external legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p3a;basis=Add plan-only mock tests for NLB protocol, health, zone/VPC boundary, protected allocations and restricted SG traffic; no live API calls or root runtime reads. file:external@0 ab86b091ae5ef1bfcd20d99b0643d85d8eb888a003e47bf06e28922c5adbdc40
+infra/modules/region-host/ingress/tests/group_ingress.tftest.hcl external legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p3a;basis=Add plan-only mock tests for NLB protocol, health, zone/VPC boundary, protected allocations and restricted SG traffic; remove the duplicate empty-subnet-result test in Task 1 fix round 1; no live API calls or root runtime reads. file:external@0 36665eefee5d2a298571fa56f805159eb0dab79d3bd3b0913b5bbcc07a50d42c
infra/modules/region-host/ingress/variables.tf external legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p3a;basis=Add ingress-only inputs and topology/CIDR validation; no root runtime reads. file:external@0 a1599da50d212d5e64f527719ee781e5a0a494185d144f0a958433741f4c9d36
infra/modules/region-host/ingress/versions.tf external legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p3a;basis=Declare OpenTofu and AWS provider requirements for the isolated ingress child; no root runtime reads. file:external@0 bb3be812da255c0f031b2bfdfee462e4687de3681c742b529b55693a8875136b
infra/modules/region-host/tests/fixed_state_unchanged.tftest.hcl external legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p3a;basis=Add mock regression for the unchanged fixed instance/profile/SG/root-volume wiring and legacy outputs; no root runtime reads. file:external@0 cab0fdd9e01c4497f06a617db6c41aa9887da80bd0518c76fe8eba935be42c85
@@ -31,7 +31,7 @@ internal/boxroles/roles.go Read legacy accepted-legacy accepted-legacy;rev3=cens
internal/host/boxroles.go boxRoleReader.pass legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p3a;basis=Apply desired roles through the startup-injected callback with one existing ReadGate and bounded retry backoff; preserve restrictions and close new starts on unknown evidence; no filesystem read. file:Read@268 832d15595492589b64928bad0831273d08ecd355a0f4cd939774acaa2d0bf821
internal/host/boxroles_config_linux.go readRoleRegionConfig legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p3a;basis=Read the fixed operator-delivered /etc/carlos/region-config.json through no-symlink openat2, fstat root ownership/regular-file/non-writable checks, single-link check and 4097-byte limited read; validate explicit canonical code and unchanged raw placement, never infer from AWS. file:Fstat@414,file:Openat2@90,file:ReadAll@733 2ab0091c9dad48273774eb0feb4b9289b823b9ed7a76852e6c6d26b34cb6e231
@coverage infra/modules/region-host/ingress 009777ca5464e8e6e0bfda45c00229b8924e0dc55f39d7d3036bca2b2d4f871e
-@coverage infra/modules/region-host/ingress/tests 2d7e20b48fd461253375a250c0fd01a19160a42873136e33777b588770a0b3d5
+@coverage infra/modules/region-host/ingress/tests b6467c0277af3f8f58a86f9d7bb703907d44bd5216c66c97acf9e3c8c031b416
@coverage infra/modules/region-host/ingress/tests/fixed-targets f99d9d22e9ad6820b3fcc4e5210dad538a62274005c6fdd7c4a3af292aef91cc
@coverage infra/modules/region-host/ingress/tests/fixed-targets/tests 567e4b97ac15cea31cb88aba77dccf739e58382158dba13d6b03e22935995b52
@coverage infra/modules/region-host/tests 18abb8044924e7422d67f4266905e6da7d024426cd7846868fca55932adbb9ae
diff --git a/scripts/check-region-group-plan.py b/scripts/check-region-group-plan.py
index dccf197aa..c375dccd5 100755
--- a/scripts/check-region-group-plan.py
+++ b/scripts/check-region-group-plan.py
@@ -50,6 +50,7 @@ def address_parts(address):
def fixed(kind, name):
return (kind == 'aws_instance' or kind in RULES or
kind in {'aws_security_group', 'aws_eip'} and name == 'host' or
+ kind == 'aws_cloudwatch_metric_alarm' and name == 'host_auto_recover' or
kind in HOST_IAM and (name == 'host' or name.startswith('host_') or
name == 'store_user_boundary'))
@@ -72,6 +73,8 @@ def changes(document):
if document.get('errored') is True:
raise Refused(UNKNOWN % '<plan>')
rows = document['resource_changes']
+ if not rows:
+ raise Refused(UNKNOWN % '<plan>')
drift = document.get('resource_drift', [])
if not isinstance(drift, list):
raise ValueError('invalid resource_drift')
@@ -107,6 +110,12 @@ def parse_resource(row):
previous = row.get('previous_address')
if previous is not None:
old = address_parts(previous)
+ if (old is not None and
+ (fixed(old['type'], old['name']) or fixed(parts['type'], parts['name']) or
+ old['type'] in {'aws_eip', 'aws_autoscaling_group'} or
+ parts['type'] in {'aws_eip', 'aws_autoscaling_group'}) and
+ (old['type'], old['name']) != (parts['type'], parts['name'])):
+ raise Refused(FIXED % (address, '/'.join(actions)))
if (old is None or old['type'] != parts['type'] or old['data'] != parts['data'] or
actions != ['no-op'] or not same(before, after) or unknown(change['after_unknown'])):
raise Refused(UNKNOWN % address)
@@ -186,7 +195,7 @@ def allowed(address, change, entries):
any(diff[:len(path)] == path for path in paths) for diff in diffs)
-def state_resources(document):
+def state_resources(document, section='prior_state'):
def walk(module):
if not isinstance(module, dict):
raise ValueError('invalid prior_state module')
@@ -200,15 +209,45 @@ def state_resources(document):
yield row
for child in children:
yield from walk(child)
- prior = document.get('prior_state', {})
+ prior = document.get(section, {})
if not isinstance(prior, dict):
raise ValueError('invalid prior_state')
- values = prior.get('values', {})
+ values = prior.get('values', {}) if section == 'prior_state' else prior
if not isinstance(values, dict):
raise ValueError('invalid prior_state values')
return list(walk(values.get('root_module', {})))
+def strings(value):
+ if isinstance(value, str):
+ yield value
+ elif isinstance(value, dict):
+ for child in value.values():
+ yield from strings(child)
+ elif isinstance(value, list):
+ for child in value:
+ yield from strings(child)
+
+
+def fixed_identities(resources):
+ identities = set()
+ for _, kind, name, values in resources:
+ if not fixed(kind, name):
+ continue
+ keys = []
+ if kind == 'aws_instance':
+ keys = ['id', 'primary_network_interface_id']
+ identities.update(v for v in strings(values) if v.startswith('eni-'))
+ elif kind == 'aws_eip':
+ keys = ['id', 'allocation_id', 'public_ip']
+ elif kind == 'aws_security_group':
+ keys = ['id']
+ elif kind in {'aws_iam_role', 'aws_iam_instance_profile'}:
+ keys = ['id', 'name', 'arn']
+ identities.update(v for key in keys if isinstance(v := values.get(key), str) and v)
+ return identities
+
+
def cleanup_entries(manifest, rows):
if manifest is None:
return set()
@@ -274,26 +313,24 @@ def check(document, entries, manifest):
if row['mode'] == 'managed' and change['before'] is not None]
existing += [(row['address'], row.get('type'), row.get('name'), row['values'])
for row in state_resources(document) if row.get('mode') == 'managed']
- fixed_modules, roles, allocations, instances, public_ips = set(), set(), set(), set(), set()
+ fixed_modules = set()
for address, kind, name, values in existing:
parts = address_parts(address)
if parts is None or not isinstance(name, str):
raise Refused(UNKNOWN % address)
if fixed(kind, name) and kind not in RULES:
fixed_modules.add(address[:parts.start('type')])
- if kind == 'aws_iam_role' and name == 'host':
- roles.update(v for k in ['name', 'id', 'arn'] if isinstance(v := values.get(k), str))
- if kind == 'aws_eip':
- allocations.update(v for k in ['id', 'allocation_id'] if isinstance(v := values.get(k), str))
- if isinstance(values.get('public_ip'), str):
- public_ips.add(values['public_ip'])
- if kind == 'aws_instance' and isinstance(values.get('id'), str):
- instances.add(values['id'])
- host_sgs = {c['before'].get('id') for _, kind, name, c in parsed
- if kind == 'aws_security_group' and name == 'host' and c['before']}
- host_sgs.update(row['values'].get('id') for row in state_resources(document)
- if row.get('type') == 'aws_security_group' and row.get('name') == 'host')
- host_sgs.discard(None)
+ created = {address for address, _, _, change in parsed
+ if change['actions'] == ['create']}
+ existing_addresses = {address for address, _, _, _ in existing}
+ planned = [(row['address'], row.get('type'), row.get('name'), row['values'])
+ for row in state_resources(document, 'planned_values')
+ if row.get('mode') == 'managed' and
+ (row['address'] not in created or row['address'] in existing_addresses)]
+ planned += [(address, kind, name, change['after'])
+ for row, (address, kind, name, change) in zip(rows, parsed)
+ if row['mode'] == 'managed' and change['before'] is not None and change['after'] is not None]
+ identities = fixed_identities(existing + planned)
for row, (address, kind, name, change) in zip(rows, parsed):
if row['mode'] == 'data':
continue
@@ -302,39 +339,32 @@ def check(document, entries, manifest):
# are refused; this module uses inline rules for its new NLB SG.
if kind in RULES and actions == ['create']:
sg = after.get('security_group_id')
- if not sg or sg in host_sgs or unknown(change['after_unknown']):
+ if not sg or unknown(change['after_unknown']):
raise Refused(FIXED % (address, 'create'))
parts = address_parts(address)
same_module = address[:parts.start('type')] in fixed_modules
- related = False
- if kind in HOST_IAM:
- for values in [before, after]:
- if values is not None:
- related = related or values.get('role') in roles or bool(set(values.get('roles') or []) & roles)
- if same_module and unknown(change['after_unknown']):
- related = True
- if kind == 'aws_eip_association':
- for values in [before, after]:
- if values is not None:
- related = (related or values.get('allocation_id') in allocations or
- values.get('instance_id') in instances or values.get('public_ip') in public_ips)
- related = related or unknown(change['after_unknown'])
+ related = any(value in identities for value in strings([before, after]))
+ if kind == 'aws_lb_target_group_attachment' and actions in [['create'], ['delete']]:
+ related = False
+ if unknown(change['after_unknown']) and (kind == 'aws_eip_association' or
+ kind in HOST_IAM and same_module):
+ related = True
is_fixed = fixed(kind, name) or related
if before is None:
- if actions == ['create'] and (related or is_fixed and same_module):
- raise Refused(FIXED % (address, 'create'))
+ if related or is_fixed and same_module:
+ raise Refused(FIXED % (address, '/'.join(actions)))
continue
guarded = is_fixed or kind in {'aws_eip', 'aws_autoscaling_group'}
if guarded and (unknown(change['after_unknown']) or actions == ['read']):
raise Refused(UNKNOWN % address)
- if address in disposable:
- continue
if is_fixed:
if actions == ['no-op'] and same(before, after):
continue
if not allowed(address, change, entries):
raise Refused(FIXED % (address, '/'.join(actions)))
- elif kind == 'aws_eip':
+ if address in disposable and not is_fixed:
+ continue
+ if kind == 'aws_eip':
if 'delete' in actions:
raise Refused(PROTECTED % address)
if actions == ['no-op'] and not same(before, after):
diff --git a/scripts/check_region_group_plan_test.py b/scripts/check_region_group_plan_test.py
index 0116f78df..3efa157d3 100644
--- a/scripts/check_region_group_plan_test.py
+++ b/scripts/check_region_group_plan_test.py
@@ -24,6 +24,129 @@ def plan(*resources):
class GateTests(unittest.TestCase):
+ def fixture(self, name):
+ return json.loads((GATE.parent / 'testdata/region-group-plan' / name).read_text())
+
+ def test_review_fixed_identity_probes(self):
+ for probe in self.fixture('fixed-identity-probes.json'):
+ with self.subTest(probe=probe['probe']):
+ document = self.fixture('fixed-identities.json')
+ document['resource_changes'] = [probe['resource']]
+ result = self.gate(document, 1)
+ self.assertIn('fixed box resources cannot be changed', result.stderr)
+
+ def test_review_guarded_rename_then_change(self):
+ for kind, old_name, new_name, values in [
+ ('aws_eip', 'host', 'public', {'id': 'eipalloc-fixed', 'instance': 'i-fixed'}),
+ ('aws_security_group', 'host', 'nlb', {'id': 'sg-fixed'}),
+ ('aws_cloudwatch_metric_alarm', 'host_auto_recover', 'renamed', {'id': 'alarm-fixed'}),
+ ('aws_eip', 'public', 'renamed', {'id': 'eipalloc-protected'}),
+ ('aws_autoscaling_group', 'group', 'renamed',
+ {'min_size': 2, 'max_size': 2, 'desired_capacity': 2})
+ ]:
+ with self.subTest(kind=kind, step='rename'):
+ moved = resource(f'module.probe.{kind}.{new_name}[0]', kind, new_name,
+ values, values, actions=['no-op'],
+ previous_address=f'module.ie.{kind}.{old_name}')
+ self.gate(plan(moved), 1)
+ with self.subTest(kind=kind, step='index-only'):
+ moved['address'] = f'module.probe.{kind}.{old_name}[0]'
+ moved['name'] = old_name
+ self.gate(plan(moved), 0)
+ # Even a previously renamed EIP must not detach the still-fixed instance.
+ document = self.fixture('fixed-identities.json')
+ document['resource_changes'] = [resource(EIP, 'aws_eip', 'public',
+ {'id': 'eipalloc-other', 'instance': 'i-fixed'},
+ {'id': 'eipalloc-other', 'instance': 'i-other'})]
+ self.gate(document, 1)
+
+ def test_fixed_identity_sources_and_nested_references(self):
+ baseline = self.fixture('fixed-identities.json')
+ resources = baseline['prior_state']['values']['root_module']['child_modules'][0]['resources']
+ identities = ['i-fixed', 'eni-fixed', 'eni-secondary', 'eipalloc-fixed', '192.0.2.1',
+ 'sg-fixed', 'host-role', 'role-fixed-id', 'arn:aws:iam::123456789012:role/host-role',
+ 'host-profile', 'profile-fixed-id',
+ 'arn:aws:iam::123456789012:instance-profile/host-profile']
+ for source in ['prior_state', 'planned_values', 'before', 'after']:
+ for identity in identities:
+ with self.subTest(source=source, identity=identity):
+ document = plan()
+ if source == 'prior_state':
+ document['prior_state'] = baseline['prior_state']
+ elif source == 'planned_values':
+ document['planned_values'] = baseline['prior_state']['values']
+ else:
+ document['resource_changes'] = [resource(r['address'], r['type'], r['name'],
+ r['values'], r['values'], actions=['no-op']) for r in resources]
+ values = {'nested': [{'reference': identity}]}
+ probe = resource('module.any.custom_resource.probe', 'custom_resource', 'probe',
+ None if source != 'before' else values,
+ values if source != 'before' else {'nested': []},
+ actions=['create'] if source != 'before' else ['update'])
+ document['resource_changes'].insert(0, probe)
+ self.gate(document, 1)
+
+ def test_fixed_identity_attachment_exception_is_narrow(self):
+ for actions, expected in [(['create'], 0), (['delete'], 0), (['update'], 1),
+ (['delete', 'create'], 1), (['create', 'delete'], 1),
+ (['no-op'], 0)]:
+ with self.subTest(actions=actions):
+ document = self.fixture('fixed-identities.json')
+ old = {'target_id': 'i-fixed', 'port': 80}
+ new = dict(old, port=443) if actions == ['update'] else old
+ document['resource_changes'] = [resource('aws_lb_target_group_attachment.fixed',
+ 'aws_lb_target_group_attachment', 'fixed', None if actions == ['create'] else old,
+ None if actions == ['delete'] else new, actions=actions,
+ unknown={'id': True} if actions == ['create'] else {})]
+ self.gate(document, expected)
+
+ def test_fixed_identity_allow_list_and_cleanup_order(self):
+ document = self.fixture('fixed-identities.json')
+ address = 'aws_ec2_instance_state.probe'
+ document['resource_changes'] = [resource(address, 'aws_ec2_instance_state', 'probe',
+ {'instance_id': 'i-fixed', 'state': 'running'},
+ {'instance_id': 'i-fixed', 'state': 'stopped'})]
+ entry = dict(address=address, attribute='/state', before='running', after='stopped',
+ purpose='Reviewed state change', approval='review-123')
+ self.gate(document, 0, allow=[entry])
+ document['resource_changes'][0]['change']['after_unknown'] = {'id': True}
+ self.gate(document, 1, allow=[entry])
+ document['resource_changes'] = [resource(EIP, 'aws_eip', 'public',
+ {'id': 'eipalloc-probe', 'instance': 'i-fixed'}, None, actions=['delete'])]
+ manifest = dict(purpose='End probe', approval='review-123',
+ resources=[dict(address=EIP, allocation_id='eipalloc-probe')],
+ creation_plan=plan(resource(EIP, 'aws_eip', 'public', None, {}, actions=['create'])))
+ self.gate(document, 1, cleanup=manifest)
+
+ def test_empty_plan_refused(self):
+ self.gate(plan(), 1)
+
+ def test_related_allow_list_cannot_allow_asg_decrease(self):
+ document = self.fixture('fixed-identities.json')
+ address = 'aws_autoscaling_group.group'
+ before = dict(min_size=2, max_size=2, desired_capacity=2,
+ tag=[dict(key='fixed-reference', value='i-fixed')])
+ document['resource_changes'] = [resource(address, 'aws_autoscaling_group', 'group',
+ before, dict(before, desired_capacity=1))]
+ entry = dict(address=address, attribute='/desired_capacity', before=2, after=1,
+ purpose='Reviewed change', approval='review-123')
+ self.gate(document, 1, allow=[entry])
+
+ def test_related_managed_read_is_refused(self):
+ document = self.fixture('fixed-identities.json')
+ document['resource_changes'] = [resource('custom_resource.probe', 'custom_resource', 'probe',
+ None, {'reference': 'i-fixed'}, actions=['read'])]
+ self.gate(document, 1)
+
+ def test_planned_new_host_identity_is_not_preexisting(self):
+ row = resource('module.new.aws_iam_role.host', 'aws_iam_role', 'host',
+ None, {'name': 'new-role'}, actions=['create'], unknown={'arn': True})
+ document = plan(row)
+ document['planned_values'] = {'root_module': {'child_modules': [{'resources': [
+ dict(address=row['address'], mode='managed', type=row['type'], name=row['name'],
+ values=row['change']['after'])]}]}}
+ self.gate(document, 0)
+
def gate(self, document, expected, allow=None, cleanup=None):
with tempfile.TemporaryDirectory(dir=os.environ.get('TMPDIR')) as tmp:
root = Path(tmp)
diff --git a/scripts/testdata/region-group-plan/fixed-identities.json b/scripts/testdata/region-group-plan/fixed-identities.json
new file mode 100644
index 000000000..593faf8a4
--- /dev/null
+++ b/scripts/testdata/region-group-plan/fixed-identities.json
@@ -0,0 +1,74 @@
+{
+ "format_version": "1.2",
+ "resource_changes": [],
+ "prior_state": {
+ "values": {
+ "root_module": {
+ "child_modules": [
+ {
+ "address": "module.ie",
+ "resources": [
+ {
+ "address": "module.ie.aws_instance.host",
+ "mode": "managed",
+ "type": "aws_instance",
+ "name": "host",
+ "values": {
+ "id": "i-fixed",
+ "primary_network_interface_id": "eni-fixed",
+ "network_interface": [
+ {
+ "network_interface_id": "eni-secondary"
+ }
+ ]
+ }
+ },
+ {
+ "address": "module.ie.aws_eip.host",
+ "mode": "managed",
+ "type": "aws_eip",
+ "name": "host",
+ "values": {
+ "id": "eipalloc-fixed",
+ "public_ip": "192.0.2.1",
+ "instance": "i-fixed"
+ }
+ },
+ {
+ "address": "module.ie.aws_security_group.host",
+ "mode": "managed",
+ "type": "aws_security_group",
+ "name": "host",
+ "values": {
+ "id": "sg-fixed"
+ }
+ },
+ {
+ "address": "module.ie.aws_iam_role.host",
+ "mode": "managed",
+ "type": "aws_iam_role",
+ "name": "host",
+ "values": {
+ "id": "role-fixed-id",
+ "name": "host-role",
+ "arn": "arn:aws:iam::123456789012:role/host-role"
+ }
+ },
+ {
+ "address": "module.ie.aws_iam_instance_profile.host",
+ "mode": "managed",
+ "type": "aws_iam_instance_profile",
+ "name": "host",
+ "values": {
+ "id": "profile-fixed-id",
+ "name": "host-profile",
+ "arn": "arn:aws:iam::123456789012:instance-profile/host-profile"
+ }
+ }
+ ]
+ }
+ ]
+ }
+ }
+ }
+}
diff --git a/scripts/testdata/region-group-plan/fixed-identity-probes.json b/scripts/testdata/region-group-plan/fixed-identity-probes.json
new file mode 100644
index 000000000..ff484ee33
--- /dev/null
+++ b/scripts/testdata/region-group-plan/fixed-identity-probes.json
@@ -0,0 +1,191 @@
+[
+ {
+ "probe": "F1-eip-instance",
+ "resource": {
+ "address": "module.probe.aws_eip.probe",
+ "mode": "managed",
+ "type": "aws_eip",
+ "name": "probe",
+ "change": {
+ "actions": [
+ "create"
+ ],
+ "before": null,
+ "after": {
+ "instance": "i-fixed"
+ },
+ "after_unknown": {}
+ }
+ }
+ },
+ {
+ "probe": "F1-eip-network-interface",
+ "resource": {
+ "address": "module.probe.aws_eip.probe",
+ "mode": "managed",
+ "type": "aws_eip",
+ "name": "probe",
+ "change": {
+ "actions": [
+ "create"
+ ],
+ "before": null,
+ "after": {
+ "network_interface": "eni-fixed"
+ },
+ "after_unknown": {}
+ }
+ }
+ },
+ {
+ "probe": "F1-association-network-interface",
+ "resource": {
+ "address": "module.probe.aws_eip_association.probe",
+ "mode": "managed",
+ "type": "aws_eip_association",
+ "name": "probe",
+ "change": {
+ "actions": [
+ "create"
+ ],
+ "before": null,
+ "after": {
+ "allocation_id": "eipalloc-other",
+ "network_interface_id": "eni-fixed"
+ },
+ "after_unknown": {}
+ }
+ }
+ },
+ {
+ "probe": "F3-instance-state",
+ "resource": {
+ "address": "module.probe.aws_ec2_instance_state.probe",
+ "mode": "managed",
+ "type": "aws_ec2_instance_state",
+ "name": "probe",
+ "change": {
+ "actions": [
+ "create"
+ ],
+ "before": null,
+ "after": {
+ "instance_id": "i-fixed",
+ "state": "stopped"
+ },
+ "after_unknown": {}
+ }
+ }
+ },
+ {
+ "probe": "F3-network-interface-sg",
+ "resource": {
+ "address": "module.probe.aws_network_interface_sg_attachment.probe",
+ "mode": "managed",
+ "type": "aws_network_interface_sg_attachment",
+ "name": "probe",
+ "change": {
+ "actions": [
+ "create"
+ ],
+ "before": null,
+ "after": {
+ "network_interface_id": "eni-fixed",
+ "security_group_id": "sg-other"
+ },
+ "after_unknown": {}
+ }
+ }
+ },
+ {
+ "probe": "F3-volume",
+ "resource": {
+ "address": "module.probe.aws_volume_attachment.probe",
+ "mode": "managed",
+ "type": "aws_volume_attachment",
+ "name": "probe",
+ "change": {
+ "actions": [
+ "create"
+ ],
+ "before": null,
+ "after": {
+ "instance_id": "i-fixed",
+ "volume_id": "vol-other"
+ },
+ "after_unknown": {}
+ }
+ }
+ },
+ {
+ "probe": "F3-role-policies",
+ "resource": {
+ "address": "module.probe.aws_iam_role_policies_exclusive.probe",
+ "mode": "managed",
+ "type": "aws_iam_role_policies_exclusive",
+ "name": "probe",
+ "change": {
+ "actions": [
+ "create"
+ ],
+ "before": null,
+ "after": {
+ "role_name": "host-role",
+ "policy_names": []
+ },
+ "after_unknown": {}
+ }
+ }
+ },
+ {
+ "probe": "F3-role-attachments",
+ "resource": {
+ "address": "module.probe.aws_iam_role_policy_attachments_exclusive.probe",
+ "mode": "managed",
+ "type": "aws_iam_role_policy_attachments_exclusive",
+ "name": "probe",
+ "change": {
+ "actions": [
+ "create"
+ ],
+ "before": null,
+ "after": {
+ "role_name": "host-role",
+ "policy_arns": []
+ },
+ "after_unknown": {}
+ }
+ }
+ },
+ {
+ "probe": "F3-auto-recover",
+ "resource": {
+ "address": "module.probe.aws_cloudwatch_metric_alarm.host_auto_recover",
+ "mode": "managed",
+ "type": "aws_cloudwatch_metric_alarm",
+ "name": "host_auto_recover",
+ "change": {
+ "actions": [
+ "update"
+ ],
+ "before": {
+ "dimensions": {
+ "InstanceId": "i-fixed"
+ },
+ "alarm_actions": [
+ "arn:aws:automate:eu-west-1:ec2:recover"
+ ]
+ },
+ "after": {
+ "dimensions": {
+ "InstanceId": "i-fixed"
+ },
+ "alarm_actions": [
+ "arn:aws:automate:eu-west-1:ec2:terminate"
+ ]
+ },
+ "after_unknown": {}
+ }
+ }
+ }
+]