Sfoglia il codice sorgente

Add Spacelift plan policy denying inbound rules and IPv6 (#88) (#140)

Denies a created or updated firewall rule that is not tag-scoped, AWS
ingress from a CIDR, and IPv6 on droplets and AWS networks. Exceptions
only through an allowlist of exact addresses in the reviewed file.

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
ChiefGyk3D 1 mese fa
parent
commit
64e8656ada

+ 225 - 0
.spacelift/policies/plan.rego

@@ -0,0 +1,225 @@
+# Plan policy for every stack in this repository (cah-00-foundation,
+# cah-10-access, the AWS stacks, and later ones). Explained in
+# docs/spacelift-secrets.md, section 9a, and on the Spacelift wiki page.
+#
+# Spacelift does not read this file from the repository. Create a plan policy
+# in Spacelift with this body and attach it to each stack (or give it the
+# label autoattach:cah, the same label the push policy and the Doppler context
+# use). The tests next to it run in CI on every pull request.
+#
+# What it decides: whether a plan may be applied. A plan that would open a
+# port on a core machine, or turn on IPv6, is denied before anything is
+# applied. The firewall module's own tests catch the same mistakes in CI, but
+# only for the code they exercise; this checks the plan Spacelift is about to
+# apply, whatever code produced it.
+#
+# It denies a created or updated resource that is:
+#
+#   - a digitalocean_firewall with an inbound rule that is not tag-scoped.
+#     The firewall module only ever emits source_tags (the firewall's own tag,
+#     or one other group's tag), so a source address, droplet id, load
+#     balancer or Kubernetes source is a change the module cannot produce, and
+#     0.0.0.0/0 or ::/0 is the whole internet.
+#   - an AWS security group rule that admits a CIDR range (IPv4 or IPv6). The
+#     AWS firewall module admits only a reference to the group itself.
+#   - a droplet with ipv6 turned on, or an AWS VPC, subnet or instance that
+#     takes an IPv6 range or address. IPv6 stays closed in this repository.
+#
+# Pure deletes and no-ops are ignored: removing a rule never opens anything.
+#
+# The escape hatch is this file. A deliberate inbound rule is allowed only by
+# adding the exact resource address of the firewall or rule to
+# inbound_allowlist below, in a pull request that gets reviewed. Nothing a plan
+# can set itself (a stack label, an environment variable, a field in the plan)
+# is read, so a branch cannot grant its own exception. The IPv6 checks have no
+# escape hatch.
+#
+# The destroy and autodeploy guard is a separate policy (issue #92).
+#
+# Plan policies run on tracked runs here, before the apply. Proposed runs do
+# not exist in this repository (see push.rego), so this is the gate on the
+# plan a human is about to confirm.
+
+package spacelift
+
+import rego.v1
+
+# Exact resource addresses (as shown in the plan, for example
+# `module.fw.digitalocean_firewall.this`) whose inbound rules are allowed even
+# though they are not tag-scoped. Empty by default. Every entry needs a
+# threat-model note (docs/wiki/Threat-Model.md) and a pull request review that
+# names the port, the source and the reason. Remove the entry when the rule
+# goes away.
+inbound_allowlist := set()
+
+deny contains msg if {
+	some rc in changed_resources("digitalocean_firewall")
+	not allowlisted(rc)
+	some rule in rc.change.after.inbound_rule
+	world_open(rule)
+	msg := sprintf(
+		"%s: inbound rule %s opens the port to the whole internet (%s). Inbound on core tags must come from a tag only.",
+		[rc.address, rule_label(rule), source_summary(rule)],
+	)
+}
+
+deny contains msg if {
+	some rc in changed_resources("digitalocean_firewall")
+	not allowlisted(rc)
+	some rule in rc.change.after.inbound_rule
+	not world_open(rule)
+	not tag_scoped(rule)
+	msg := sprintf(
+		"%s: inbound rule %s is not tag-scoped (%s). Inbound on core tags must come from a tag only.",
+		[rc.address, rule_label(rule), source_summary(rule)],
+	)
+}
+
+deny contains msg if {
+	some entry in aws_ingress_cidrs
+	not entry.address in inbound_allowlist
+	msg := sprintf(
+		"%s: %s admits inbound traffic from %s. AWS ingress here may only reference a security group, never a CIDR range.",
+		[entry.address, entry.type, entry.cidr],
+	)
+}
+
+deny contains msg if {
+	some rc in changed_resources("digitalocean_droplet")
+	rc.change.after.ipv6 == true
+	msg := sprintf("%s: ipv6 = true. IPv6 must stay closed on droplets.", [rc.address])
+}
+
+deny contains msg if {
+	some entry in ipv6_findings
+	msg := sprintf("%s: %s. IPv6 must stay closed.", [entry.address, entry.reason])
+}
+
+# Resources of one type that this plan creates or updates. A replacement
+# (delete then create) counts; a pure delete or no-op does not.
+changed_resources(type) := {rc |
+	some rc in input.terraform.resource_changes
+	rc.type == type
+	some action in rc.change.actions
+	action in {"create", "update"}
+}
+
+allowlisted(rc) if rc.address in inbound_allowlist
+
+# A list-valued field, or [] when it is missing or null (a real plan uses
+# both for "nothing here").
+values(obj, key) := v if {
+	v := object.get(obj, key, [])
+	v != null
+} else := []
+
+filled(obj, key) if count(values(obj, key)) > 0
+
+filled_string(obj, key) if {
+	v := object.get(obj, key, null)
+	is_string(v)
+	v != ""
+}
+
+world_addresses := {"0.0.0.0/0", "::/0"}
+
+world_open(rule) if {
+	some address in values(rule, "source_addresses")
+	address in world_addresses
+}
+
+# The only sources the firewall module emits are tags. Any other kind of
+# source, next to a tag or alone, makes the rule not tag-scoped.
+non_tag_sources := {
+	"source_addresses",
+	"source_droplet_ids",
+	"source_load_balancer_uids",
+	"source_kubernetes_ids",
+}
+
+tag_scoped(rule) if {
+	filled(rule, "source_tags")
+	not has_non_tag_source(rule)
+}
+
+has_non_tag_source(rule) if {
+	some key in non_tag_sources
+	filled(rule, key)
+}
+
+rule_label(rule) := sprintf("%s/%s", [object.get(rule, "protocol", "?"), object.get(rule, "port_range", "?")])
+
+# What the rule admits, for the message. Tags are not listed: they are the
+# allowed kind, and a tag name is not what is wrong.
+source_summary(rule) := concat(", ", sort(parts)) if {
+	parts := array.concat(
+		[sprintf("address %s", [a]) | some a in values(rule, "source_addresses")],
+		array.concat(
+			[sprintf("droplet id %v", [i]) | some i in values(rule, "source_droplet_ids")],
+			array.concat(
+				[sprintf("load balancer %v", [l]) | some l in values(rule, "source_load_balancer_uids")],
+				[sprintf("kubernetes cluster %v", [k]) | some k in values(rule, "source_kubernetes_ids")],
+			),
+		),
+	)
+	count(parts) > 0
+} else := "no source tag"
+
+# AWS ingress from an address. A reference to a security group (or to the group
+# itself) carries no CIDR and is the one admitted kind.
+aws_ingress_cidrs contains {"address": rc.address, "type": "aws_vpc_security_group_ingress_rule", "cidr": cidr} if {
+	some rc in changed_resources("aws_vpc_security_group_ingress_rule")
+	some key in {"cidr_ipv4", "cidr_ipv6"}
+	filled_string(rc.change.after, key)
+	cidr := rc.change.after[key]
+}
+
+aws_ingress_cidrs contains {"address": rc.address, "type": "aws_security_group_rule", "cidr": cidr} if {
+	some rc in changed_resources("aws_security_group_rule")
+	rc.change.after.type == "ingress"
+	some key in {"cidr_blocks", "ipv6_cidr_blocks"}
+	some cidr in values(rc.change.after, key)
+}
+
+aws_ingress_cidrs contains {"address": rc.address, "type": "aws_security_group", "cidr": cidr} if {
+	some rc in changed_resources("aws_security_group")
+	some block in values(rc.change.after, "ingress")
+	some key in {"cidr_blocks", "ipv6_cidr_blocks"}
+	some cidr in values(block, key)
+}
+
+# AWS settings that give a network, subnet or machine an IPv6 range or address.
+ipv6_findings contains {"address": rc.address, "reason": "assign_generated_ipv6_cidr_block = true"} if {
+	some rc in changed_resources("aws_vpc")
+	rc.change.after.assign_generated_ipv6_cidr_block == true
+}
+
+ipv6_findings contains {"address": rc.address, "reason": sprintf("%s is set", [key])} if {
+	some type, keys in {
+		"aws_vpc": {"ipv6_cidr_block", "ipv6_ipam_pool_id"},
+		"aws_subnet": {"ipv6_cidr_block"},
+	}
+	some rc in changed_resources(type)
+	some key in keys
+	filled_string(rc.change.after, key)
+}
+
+ipv6_findings contains {"address": rc.address, "reason": sprintf("%s = true", [key])} if {
+	some rc in changed_resources("aws_subnet")
+	some key in {"assign_ipv6_address_on_creation", "ipv6_native"}
+	rc.change.after[key] == true
+}
+
+ipv6_findings contains {"address": rc.address, "reason": "ipv6_address_count is above zero"} if {
+	some rc in changed_resources("aws_instance")
+	rc.change.after.ipv6_address_count > 0
+}
+
+ipv6_findings contains {"address": rc.address, "reason": "ipv6_addresses is set"} if {
+	some rc in changed_resources("aws_instance")
+	filled(rc.change.after, "ipv6_addresses")
+}
+
+# Keep a sample of real plan inputs in Spacelift's policy workbench, so the
+# input shape this policy assumes can be checked against a real run.
+sample := true

+ 341 - 0
.spacelift/policies/plan_test.rego

@@ -0,0 +1,341 @@
+# Tests for plan.rego. Run with `opa test .spacelift/policies -v`; CI runs
+# them on every pull request through git-your-ship-together's tofu-ci.yml.
+#
+# The fixtures follow the shape of Spacelift's plan input: a list of
+# input.terraform.resource_changes, each with an address, a type and a change
+# holding actions and the planned `after` values. Only the fields the policy
+# reads are filled in, plus the ones a real plan leaves empty or null.
+
+package spacelift_test
+
+import rego.v1
+
+import data.spacelift
+
+plan(changes) := {"terraform": {"resource_changes": changes}}
+
+change(address, type, actions, after) := {
+	"address": address,
+	"type": type,
+	"change": {"actions": actions, "after": after},
+}
+
+create(address, type, after) := change(address, type, ["create"], after)
+
+fw := "module.fw.digitalocean_firewall.this"
+
+# What the firewall module renders for the connector: zero inbound rules.
+connector_firewall := create(fw, "digitalocean_firewall", {
+	"name": "access-nyc3-connector",
+	"tags": ["cah-access"],
+	"inbound_rule": [],
+	"outbound_rule": [{"protocol": "tcp", "port_range": "443", "destination_addresses": ["0.0.0.0/0", "::/0"]}],
+})
+
+# What inbound_from_tag_rules / inbound_from_other_tags can render.
+tag_rule := {
+	"protocol": "tcp",
+	"port_range": "8080",
+	"source_tags": ["cah-access"],
+	"source_addresses": null,
+	"source_droplet_ids": [],
+	"source_load_balancer_uids": [],
+	"source_kubernetes_ids": [],
+}
+
+# The same rule with its source replaced.
+with_source(overrides) := object.union(tag_rule, overrides)
+
+firewall_with(rules) := create(fw, "digitalocean_firewall", {"inbound_rule": rules})
+
+denied(changes) := reasons if {
+	reasons := spacelift.deny with input as plan(changes)
+}
+
+# Clean plans.
+
+test_connector_firewall_with_zero_inbound_passes if {
+	count(denied([connector_firewall])) == 0
+}
+
+test_tag_scoped_inbound_rule_passes if {
+	count(denied([firewall_with([tag_rule])])) == 0
+}
+
+test_empty_plan_passes if {
+	count(denied([])) == 0
+}
+
+# Denials on DigitalOcean.
+
+test_source_address_is_denied if {
+	rule := with_source({"source_tags": null, "source_addresses": ["203.0.113.7/32"]})
+	reasons := denied([firewall_with([rule])])
+	count(reasons) == 1
+	some reason in reasons
+	contains(reason, fw)
+	contains(reason, "tcp/8080")
+	contains(reason, "203.0.113.7/32")
+}
+
+test_open_to_ipv4_world_is_denied if {
+	rule := with_source({"source_tags": [], "source_addresses": ["0.0.0.0/0"]})
+	reasons := denied([firewall_with([rule])])
+	count(reasons) == 1
+	some reason in reasons
+	contains(reason, "whole internet")
+}
+
+test_open_to_ipv6_world_is_denied if {
+	rule := with_source({"source_tags": [], "source_addresses": ["::/0"]})
+	reasons := denied([firewall_with([rule])])
+	count(reasons) == 1
+	some reason in reasons
+	contains(reason, "whole internet")
+}
+
+# A tag next to an address is still an address: the address opens the port.
+test_tag_plus_address_is_denied if {
+	rule := with_source({"source_addresses": ["0.0.0.0/0"]})
+	count(denied([firewall_with([rule])])) == 1
+}
+
+test_droplet_id_source_is_denied if {
+	rule := with_source({"source_tags": [], "source_droplet_ids": [12345]})
+	count(denied([firewall_with([rule])])) == 1
+}
+
+test_load_balancer_source_is_denied if {
+	rule := with_source({"source_tags": [], "source_load_balancer_uids": ["lb-1"]})
+	count(denied([firewall_with([rule])])) == 1
+}
+
+test_kubernetes_source_is_denied if {
+	rule := with_source({"source_tags": [], "source_kubernetes_ids": ["k8s-1"]})
+	count(denied([firewall_with([rule])])) == 1
+}
+
+test_rule_with_no_source_at_all_is_denied if {
+	count(denied([firewall_with([{"protocol": "tcp", "port_range": "22"}])])) == 1
+}
+
+test_one_bad_rule_among_good_ones_is_denied if {
+	bad := with_source({"source_tags": [], "source_addresses": ["198.51.100.0/24"]})
+	count(denied([firewall_with([tag_rule, bad])])) == 1
+}
+
+test_firewall_update_is_checked_too if {
+	bad := with_source({"source_tags": [], "source_addresses": ["198.51.100.0/24"]})
+	update := change(fw, "digitalocean_firewall", ["update"], {"inbound_rule": [bad]})
+	count(denied([update])) == 1
+}
+
+test_firewall_replacement_is_checked_too if {
+	bad := with_source({"source_tags": [], "source_addresses": ["198.51.100.0/24"]})
+	replace := change(fw, "digitalocean_firewall", ["delete", "create"], {"inbound_rule": [bad]})
+	count(denied([replace])) == 1
+}
+
+# Empty and null fields do not crash and do not deny.
+
+test_missing_and_null_inbound_rule_do_not_crash if {
+	count(denied([
+		create(fw, "digitalocean_firewall", {"name": "x"}),
+		create("module.fw2.digitalocean_firewall.this", "digitalocean_firewall", {"inbound_rule": null}),
+	])) == 0
+}
+
+test_delete_with_null_after_is_ignored if {
+	gone := change(fw, "digitalocean_firewall", ["delete"], null)
+	count(denied([gone])) == 0
+}
+
+test_deleting_an_open_rule_is_ignored if {
+	bad := with_source({"source_tags": [], "source_addresses": ["0.0.0.0/0"]})
+	gone := change(fw, "digitalocean_firewall", ["delete"], {"inbound_rule": [bad]})
+	count(denied([gone])) == 0
+}
+
+test_no_op_is_ignored if {
+	bad := with_source({"source_tags": [], "source_addresses": ["0.0.0.0/0"]})
+	same := change(fw, "digitalocean_firewall", ["no-op"], {"inbound_rule": [bad]})
+	count(denied([same])) == 0
+}
+
+test_plan_without_resource_changes_does_not_crash if {
+	reasons := spacelift.deny with input as {"terraform": {}}
+	count(reasons) == 0
+}
+
+# The escape hatch: exact addresses in the policy file, nothing a plan sets.
+
+test_allowlisted_address_passes if {
+	bad := with_source({"source_tags": [], "source_addresses": ["198.51.100.0/24"]})
+	reasons := spacelift.deny with input as plan([firewall_with([bad])]) with data.spacelift.inbound_allowlist as {fw}
+	count(reasons) == 0
+}
+
+test_same_rule_at_another_address_still_fails if {
+	bad := with_source({"source_tags": [], "source_addresses": ["198.51.100.0/24"]})
+	other := create("module.other.digitalocean_firewall.this", "digitalocean_firewall", {"inbound_rule": [bad]})
+	reasons := spacelift.deny with input as plan([other]) with data.spacelift.inbound_allowlist as {fw}
+	count(reasons) == 1
+}
+
+test_allowlist_is_empty_by_default if {
+	count(spacelift.inbound_allowlist) == 0
+}
+
+# A plan cannot allowlist itself: a field or label in the input changes nothing.
+test_plan_cannot_set_its_own_exception if {
+	bad := with_source({"source_tags": [], "source_addresses": ["198.51.100.0/24"]})
+	doc := object.union(plan([firewall_with([bad])]), {
+		"inbound_allowlist": [fw],
+		"spacelift": {"stack": {"labels": ["allow-inbound", "cah"]}},
+	})
+	reasons := spacelift.deny with input as doc
+	count(reasons) == 1
+}
+
+# Denials on AWS.
+
+ingress_rule_address := "module.aws_fw.aws_vpc_security_group_ingress_rule.web"
+
+sg_cidr := create(ingress_rule_address, "aws_vpc_security_group_ingress_rule", {
+	"ip_protocol": "tcp",
+	"from_port": 443,
+	"to_port": 443,
+	"cidr_ipv4": "0.0.0.0/0",
+	"cidr_ipv6": null,
+	"referenced_security_group_id": null,
+})
+
+test_aws_ingress_rule_from_cidr_is_denied if {
+	reasons := denied([sg_cidr])
+	count(reasons) == 1
+	some reason in reasons
+	contains(reason, ingress_rule_address)
+	contains(reason, "0.0.0.0/0")
+}
+
+test_aws_ingress_rule_from_ipv6_cidr_is_denied if {
+	rule := create("a.aws_vpc_security_group_ingress_rule.v6", "aws_vpc_security_group_ingress_rule", {"cidr_ipv4": null, "cidr_ipv6": "::/0"})
+	count(denied([rule])) == 1
+}
+
+# The module's own member rule: a reference to the group, no address.
+test_aws_ingress_rule_from_security_group_passes if {
+	member := create("module.aws_fw.aws_vpc_security_group_ingress_rule.member[\"tcp-8080\"]", "aws_vpc_security_group_ingress_rule", {
+		"ip_protocol": "tcp",
+		"from_port": 8080,
+		"to_port": 8080,
+		"cidr_ipv4": null,
+		"cidr_ipv6": null,
+		"referenced_security_group_id": "sg-0abc",
+	})
+	count(denied([member])) == 0
+}
+
+test_aws_egress_rule_to_cidr_passes if {
+	egress := create("module.aws_fw.aws_vpc_security_group_egress_rule.internet[\"tcp-443\"]", "aws_vpc_security_group_egress_rule", {"cidr_ipv4": "0.0.0.0/0"})
+	count(denied([egress])) == 0
+}
+
+test_aws_security_group_rule_ingress_from_cidr_is_denied if {
+	rule := create("a.aws_security_group_rule.in", "aws_security_group_rule", {"type": "ingress", "cidr_blocks": ["10.0.0.0/8"], "ipv6_cidr_blocks": null})
+	count(denied([rule])) == 1
+}
+
+test_aws_security_group_rule_ingress_from_ipv6_is_denied if {
+	rule := create("a.aws_security_group_rule.in6", "aws_security_group_rule", {"type": "ingress", "cidr_blocks": [], "ipv6_cidr_blocks": ["::/0"]})
+	count(denied([rule])) == 1
+}
+
+test_aws_security_group_rule_ingress_from_group_passes if {
+	rule := create("a.aws_security_group_rule.in", "aws_security_group_rule", {"type": "ingress", "cidr_blocks": null, "ipv6_cidr_blocks": null, "source_security_group_id": "sg-0abc", "self": false})
+	count(denied([rule])) == 0
+}
+
+test_aws_security_group_rule_egress_to_cidr_passes if {
+	rule := create("a.aws_security_group_rule.out", "aws_security_group_rule", {"type": "egress", "cidr_blocks": ["0.0.0.0/0"]})
+	count(denied([rule])) == 0
+}
+
+test_aws_security_group_inline_ingress_from_cidr_is_denied if {
+	sg := create("a.aws_security_group.this", "aws_security_group", {"ingress": [{"from_port": 22, "to_port": 22, "protocol": "tcp", "cidr_blocks": ["0.0.0.0/0"], "ipv6_cidr_blocks": [], "security_groups": []}], "egress": []})
+	reasons := denied([sg])
+	count(reasons) == 1
+	some reason in reasons
+	contains(reason, "a.aws_security_group.this")
+}
+
+test_aws_security_group_with_no_rules_passes if {
+	sg := create("module.aws_fw.aws_security_group.this", "aws_security_group", {"name": "x", "ingress": null, "egress": null})
+	count(denied([sg])) == 0
+}
+
+test_aws_allowlisted_address_passes if {
+	reasons := spacelift.deny with input as plan([sg_cidr]) with data.spacelift.inbound_allowlist as {ingress_rule_address}
+	count(reasons) == 0
+}
+
+# IPv6 stays closed.
+
+droplet(ipv6) := create("module.connector.digitalocean_droplet.this", "digitalocean_droplet", {"name": "c", "ipv6": ipv6})
+
+test_droplet_with_ipv6_is_denied if {
+	reasons := denied([droplet(true)])
+	count(reasons) == 1
+	some reason in reasons
+	contains(reason, "module.connector.digitalocean_droplet.this")
+	contains(reason, "ipv6")
+}
+
+test_droplet_without_ipv6_passes if {
+	count(denied([droplet(false), create("d.digitalocean_droplet.x", "digitalocean_droplet", {"name": "x"})])) == 0
+}
+
+test_aws_vpc_with_generated_ipv6_is_denied if {
+	vpc := create("module.vpc.aws_vpc.this", "aws_vpc", {"assign_generated_ipv6_cidr_block": true})
+	count(denied([vpc])) == 1
+}
+
+test_aws_vpc_with_ipv6_pool_is_denied if {
+	vpc := create("module.vpc.aws_vpc.this", "aws_vpc", {"assign_generated_ipv6_cidr_block": false, "ipv6_ipam_pool_id": "ipam-pool-1"})
+	count(denied([vpc])) == 1
+}
+
+test_aws_vpc_without_ipv6_passes if {
+	vpc := create("module.vpc.aws_vpc.this", "aws_vpc", {"cidr_block": "10.0.0.0/16", "assign_generated_ipv6_cidr_block": false, "ipv6_cidr_block": null, "ipv6_ipam_pool_id": null})
+	count(denied([vpc])) == 0
+}
+
+test_aws_subnet_with_ipv6_cidr_is_denied if {
+	subnet := create("module.vpc.aws_subnet.this", "aws_subnet", {"ipv6_cidr_block": "2001:db8::/64"})
+	count(denied([subnet])) == 1
+}
+
+test_aws_subnet_assigning_ipv6_addresses_is_denied if {
+	subnet := create("module.vpc.aws_subnet.this", "aws_subnet", {"assign_ipv6_address_on_creation": true})
+	count(denied([subnet])) == 1
+}
+
+test_aws_subnet_without_ipv6_passes if {
+	subnet := create("module.vpc.aws_subnet.this", "aws_subnet", {"cidr_block": "10.0.1.0/24", "ipv6_cidr_block": null, "assign_ipv6_address_on_creation": false})
+	count(denied([subnet])) == 0
+}
+
+test_aws_instance_with_ipv6_address_count_is_denied if {
+	inst := create("module.c.aws_instance.this", "aws_instance", {"ipv6_address_count": 1})
+	count(denied([inst])) == 1
+}
+
+test_aws_instance_with_zero_ipv6_passes if {
+	inst := create("module.c.aws_instance.this", "aws_instance", {"ipv6_address_count": 0})
+	count(denied([inst])) == 0
+}
+
+# Findings accumulate: two problems in one plan are two reasons.
+test_two_problems_are_two_reasons if {
+	count(denied([sg_cidr, droplet(true)])) == 2
+}

+ 1 - 1
README.md

@@ -105,7 +105,7 @@ Clouds-Against-Humanity/
   .spacelift/config.yml                    which secrets each stack loads
   .github/workflows/ci.yml                 GYST callers only (tofu, python, security) + CI green
   .github/workflows/wiki.yml               publishes docs/wiki through GYST's wiki-publish.yml
-  .spacelift/policies/                     Spacelift push policy and its OPA tests
+  .spacelift/policies/                     Spacelift push and plan policies and their OPA tests
   docs/wiki/                               the wiki pages (source of truth)
   docs/stages/                             operator guides: decisions, verify, rollback, per stage  (00-foundation, 01-access, 02-telemetry, aws-access)
   docs/cost.md                             cost gate explained

+ 44 - 0
docs/spacelift-secrets.md

@@ -242,3 +242,47 @@ opa fmt --list --fail .spacelift/policies
 ```
 
 Use OPA 1.21.0, the version CI pins (`opa_linux_amd64_static`, SHA-256 `5eef70644868bb04d0556bcc795ee42f2ab379e73f51d1bfa30f83e1305bc9b9`).
+
+## 9a. The plan policy
+
+`.spacelift/policies/plan.rego` decides whether a plan may be applied. Like the push policy, it is a policy object in Spacelift with this file as its body; Spacelift does not read it from the repository. Its tests (`plan_test.rego`) run on every pull request in the same `policy (OPA)` CI job.
+
+Why it exists: a careless pull request could open a port on a connector. The firewall module's tests catch that after the fact, in CI, for the code they exercise. The plan policy checks the plan Spacelift is about to apply, so a tracked run with an open port is denied before anyone can confirm the apply.
+
+It denies a created or updated resource that is:
+
+| Resource | Denied when |
+|---|---|
+| `digitalocean_firewall` | An inbound rule has any source other than a tag: a source address (and `0.0.0.0/0` or `::/0` is called out as the whole internet), a droplet id, a load balancer or a Kubernetes cluster. The firewall module only ever emits `source_tags`, so a tag-scoped rule passes. |
+| `aws_vpc_security_group_ingress_rule`, `aws_security_group_rule` (ingress), `aws_security_group` (inline ingress) | The source is a CIDR range, IPv4 or IPv6. A reference to a security group passes. |
+| `digitalocean_droplet` | `ipv6 = true`. |
+| `aws_vpc`, `aws_subnet`, `aws_instance` | The plan turns on an IPv6 range or address. |
+
+Deletes and no-ops are ignored. The destroy and autodeploy guard is a separate policy (issue #92) and is not part of this one.
+
+**Plan check first.** Spacelift's documentation lists policies under **Enforce Guardrails > Policies**, and the push policy type is paid-plan only (section 9). I have not confirmed whether the plan policy type is available on the maintainer's plan; if **Create policy** offers no **Plan** type, the policy stays in the repository, tested, until the plan changes. Until it is attached, the firewall module's own tests in CI are the only check on inbound rules, and they run on the pull request, not on the plan Spacelift applies.
+
+### Attach it (once)
+
+1. In Spacelift, open **Enforce Guardrails > Policies**, choose **Create policy**, name it `cah-plan`, and pick the type **Plan**.
+2. Paste the contents of `.spacelift/policies/plan.rego` as the body. Do not paste `plan_test.rego`.
+3. Add the label `autoattach:cah`, so it attaches to every stack labelled `cah`, or attach it by hand to each stack.
+4. When the policy changes in a pull request, paste the new body into the same policy after the merge. The file in the repository is the reviewed copy, not the live one.
+
+### A deliberate exception
+
+A deliberate inbound rule is allowed only by a reviewed code change. Add the exact resource address, as the plan shows it (for example `module.fw.digitalocean_firewall.this`), to `inbound_allowlist` in `plan.rego`, in a pull request. Every entry needs a threat-model note (`docs/wiki/Threat-Model.md`) naming the port, the source and the reason, and a review. After the merge, paste the new body into Spacelift. There is no stack label, environment variable or plan field that grants an exception, on purpose: a branch must not be able to grant its own. The list is empty today. The IPv6 checks have no exception.
+
+### Reading a denial
+
+A denial names the resource address and the rule, for example:
+
+```text
+module.fw.digitalocean_firewall.this: inbound rule tcp/8080 opens the port to the whole internet (address 0.0.0.0/0). Inbound on core tags must come from a tag only.
+```
+
+Find that address in the plan, then change the code so the rule uses a tag source (or, for a reviewed exception, see above). Messages carry addresses and rule shapes only, never a secret.
+
+### Verify it
+
+The input shape (`input.terraform.resource_changes`, each with `address`, `type`, and `change.actions` and `change.after`) comes from Spacelift's published plan-policy contract and the Terraform JSON plan format, not from a real run on this account. After attaching it, check those fields against a real run's plan input in the policy workbench (the policy sets `sample := true`, so Spacelift keeps recent inputs). Local tests are the same as section 9: `opa test .spacelift/policies -v`.

+ 1 - 1
docs/wiki/Roadmap.md

@@ -97,7 +97,7 @@ Handoff: [2026-10-12 edge DMZ handoff](https://github.com/ChiefGyk3D/Clouds-Agai
 
 - A separate VPC (`rns-dmz`, no peering to core) with one immutable droplet and a reserved IP, no SSH and no connector. It would run Reticulum transport with an LXMF propagation node, i2pd, a private Meshtastic MQTT broker over TLS and an authenticated iperf3 server, behind a tag-based Cloud Firewall and host nftables.
 - iperf3 measured over two paths to the same endpoint, direct and through Twingate, cheap probes often and throughput tests rarely.
-- Stage 1 hardening first: nftables default-drop on core droplets, a plan policy denying inbound rules on core tags, an IPv6 check, an external zero-open-ports check and the metadata-endpoint restriction.
+- Stage 1 hardening first: nftables default-drop on core droplets, a plan policy denying inbound rules on core tags (written and tested in `.spacelift/policies/plan.rego`, not yet attached in Spacelift; see the secrets runbook, section 9a), an IPv6 check, an external zero-open-ports check and the metadata-endpoint restriction.
 - Stage 3 amendments: a cloud Uptime Kuma, the LLM contract through a LiteLLM proxy, the Discord bridge decision order, and webhook receivers on DigitalOcean Functions writing to the bucket so the workload host keeps zero inbound.
 - Cost: this lands past the $30 ceiling (stage 3 is already at $29). The design must lay out raising it, deferring something or moving pieces; the ceiling is not raised by default. The `edge` phase holds a $35 placeholder (stage 3's $29 plus one $6 droplet) that the design replaces with a costed number.
 - Honeypot: a design doc only, on a separate account or provider, never in this project's apply path.

+ 6 - 0
docs/wiki/Spacelift.md

@@ -85,6 +85,12 @@ This is the one place this wiki states the caveat in full; other pages link here
 
 **Push policies are a paid Spacelift feature.** On the free plan the same effect is only half available. Spacelift's built-in behaviour already ignores changes outside a stack's project root and project globs, so setting those narrowly (the stack's own directory plus `modules/**`, `scripts/spacelift/**` and `.spacelift/**`) stops docs-only pull requests and merges from starting anything. A pull request that changes a stack still gets a proposed run that fails without credentials. That is a real limit of the free plan, written down rather than hidden. The steps are in [section 9 of the secrets runbook](https://github.com/ChiefGyk3D/Clouds-Against-Humanity/blob/main/docs/spacelift-secrets.md#9-the-push-policy). [Lessons learned, "A red check from a run that could never pass"](Lessons-Learned.md#10-a-red-check-from-a-run-that-could-never-pass) is the story of the red check that prompted it.
 
+### The plan policy: no open ports in a plan
+
+A second policy, `.spacelift/policies/plan.rego`, reads the plan before an apply and denies it if it would open a port on a core machine: a firewall inbound rule with a source address or `0.0.0.0/0`, an AWS ingress rule from a CIDR range, or IPv6 turned on for a droplet or an AWS network. A rule that admits only a tag (the only kind the firewall module emits) passes. The firewall module's tests catch the same mistakes in CI; this catches them in the plan that is about to be applied.
+
+A deliberate exception is a reviewed code change: the exact resource address goes in an allowlist inside the policy file, with a threat-model note. A stack label or a plan field cannot grant one. It is written and tested but **not yet attached**, and it is a policy in Spacelift like the push policy, so the same paid-plan caveat applies; whether the plan policy type is available on the maintainer's plan is unconfirmed. The steps are in [section 9a of the secrets runbook](https://github.com/ChiefGyk3D/Clouds-Against-Humanity/blob/main/docs/spacelift-secrets.md#9a-the-plan-policy). The destroy and autodeploy guard is a separate policy.
+
 ### Why autodeploy is off
 
 Every apply needs a human to read the plan and press confirm. For infrastructure that costs money and controls access, the plan is the review. A plan that shows "1 to destroy" on a connector when you expected an in-place change is exactly the thing you want a person to catch.