Просмотр исходного кода

fix: let each connector open SSH to the other (outbound rule) (#8)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
ChiefGyk3D 1 месяц назад
Родитель
Сommit
00a7d84cb5

+ 11 - 2
docs/stages/01-access.md

@@ -65,9 +65,18 @@ The weekly update is the documented cron job, as `/etc/cron.d/twingate-connector
 
 Both connectors sit in one Twingate remote network, and each has a Twingate SSH resource at its own private address. Twingate chooses which connector carries a session, so a session to connector b's address can be routed through connector a. Connector a then has to open TCP 22 to connector b inside the VPC, and DigitalOcean cloud firewalls filter VPC-internal traffic too. With zero inbound rules that connection timed out during the SSH banner exchange, while the connector's own address kept working. Twingate's [connector best practices](https://www.twingate.com/docs/connector-best-practices) say every connector in a remote network needs identical network reach, so every connector must be able to reach every connector-address resource.
 
-The fix is the firewall module's `inbound_from_tag_rules`, which the connector firewall sets to TCP 22. Each rule uses `source_tags` equal to the firewall's own tag, so only droplets carrying the connector tag can connect. There are no source addresses and no `0.0.0.0/0`.
+The fix needs both directions, because the two connectors play different roles in one session. The target must accept the connection (inbound) and the sender must be allowed to open it (outbound). DigitalOcean allows outbound traffic only when an outbound rule permits it; the connector firewall's other outbound rules (tcp 443, 80, 53 and 30000-31000, udp) do not include TCP 22, so the first fix, inbound alone, still timed out. Replies to an admitted connection are stateful and need no rule.
 
-Access from outside is unchanged: there is still no public inbound, and SSH is reachable only through Twingate. After the apply, verify it from a host outside the VPC, for example `nmap -Pn -p 22 <droplet public address>` should report the port filtered, and the SSH resources should work from the client for both connector addresses.
+- Receive, on the target: the firewall module's `inbound_from_tag_rules`, which the connector firewall sets to TCP 22. Each rule uses `source_tags` equal to the firewall's own tag, so only droplets carrying the connector tag can connect.
+- Send, from the source: the module's `outbound_to_tag_rules`, also TCP 22. Each rule uses `destination_tags` equal to the firewall's own tag, so a connector can open SSH only to droplets carrying the connector tag.
+
+Neither rule has an address and neither uses `0.0.0.0/0`. The baseline firewall has neither variable set, so it still has zero inbound rules and only its five address-based outbound rules.
+
+Access from outside is unchanged: there is still no public inbound, and SSH is reachable only through Twingate. After the apply, verify:
+
+- From connector a (over Twingate SSH), check the peer's private address directly: `timeout 5 bash -c '</dev/tcp/<connector b private ip>/22' && echo open || echo blocked`. It should print `open`; do the same from b to a. Before the outbound rule it printed `blocked`.
+- From a host outside the VPC, `nmap -Pn -p 22 <droplet public address>` should still report the port filtered.
+- The SSH resources should work from the Twingate client for both connector addresses.
 
 ## 3. Accepted residual risk
 

+ 19 - 4
modules/firewall/main.tf

@@ -13,11 +13,17 @@ locals {
 # allows, and inbound_from_tag_rules is empty by default. When it lists
 # something, each rule admits only droplets that carry this firewall's own tag
 # (source_tags), never an address, so nothing outside the tagged group gets in.
+# The sending side is the other half: DigitalOcean allows outbound traffic only
+# when an outbound rule permits it (replies to an admitted connection are
+# stateful, a new connection is not), so outbound_to_tag_rules opens the same
+# kind of rule outward, to droplets carrying this firewall's own tag
+# (destination_tags), never to an address.
 #
-# Trivy DIG-0003 flags egress to any address. It is accepted here: outbound is
-# restricted to specific ports (never "all"), inbound is zero unless a rule
-# admits the firewall's own tag, and the hosts these droplets must reach
-# (Twingate relays, package mirrors, DNS and NTP servers) have no fixed
+# Trivy DIG-0003 flags egress to any address. It is accepted here: the
+# any-address outbound rules are restricted to specific ports (never "all"),
+# inbound is zero unless a rule admits the firewall's own tag, tag-scoped
+# outbound rules name no address at all, and the hosts these droplets must
+# reach (Twingate relays, package mirrors, DNS and NTP servers) have no fixed
 # addresses, so there is no address list to pin.
 #trivy:ignore:AVD-DIG-0003
 resource "digitalocean_firewall" "this" {
@@ -41,4 +47,13 @@ resource "digitalocean_firewall" "this" {
       destination_addresses = ["0.0.0.0/0", "::/0"]
     }
   }
+
+  dynamic "outbound_rule" {
+    for_each = var.outbound_to_tag_rules
+    content {
+      protocol         = outbound_rule.value.protocol
+      port_range       = outbound_rule.value.port_range
+      destination_tags = [var.tag]
+    }
+  }
 }

+ 14 - 2
modules/firewall/outputs.tf

@@ -10,12 +10,14 @@ output "outbound_rule_count" {
   value = length(digitalocean_firewall.this.outbound_rule)
 }
 
-# Sorted "<protocol>/<port_range>/<destinations>" descriptors, so tests (and the
-# stacks that call this module) can pin the exact outbound rule set. No secrets.
+# Sorted "<protocol>/<port_range>/<destinations>" descriptors for the
+# address-based outbound rules, so tests (and the stacks that call this module)
+# can pin the exact set. Tag-scoped rules are in outbound_tag_rules. No secrets.
 output "outbound_rules" {
   value = sort([
     for r in digitalocean_firewall.this.outbound_rule :
     "${r.protocol}/${r.port_range}/${join(",", sort(tolist(r.destination_addresses)))}"
+    if length(coalesce(r.destination_addresses, [])) > 0
   ])
 }
 
@@ -26,3 +28,13 @@ output "inbound_rules" {
     "${r.protocol}/${r.port_range}/${join(",", sort(tolist(r.source_tags)))}"
   ])
 }
+
+# Sorted "<protocol>/<port_range>/<destination tags>" descriptors for the outbound
+# rules that reach droplets by tag.
+output "outbound_tag_rules" {
+  value = sort([
+    for r in digitalocean_firewall.this.outbound_rule :
+    "${r.protocol}/${r.port_range}/${join(",", sort(tolist(r.destination_tags)))}"
+    if length(coalesce(r.destination_tags, [])) > 0
+  ])
+}

+ 70 - 0
modules/firewall/tests/firewall.tftest.hcl

@@ -126,3 +126,73 @@ run "an_inbound_rule_with_an_empty_port_range_is_rejected" {
   }
   expect_failures = [var.inbound_from_tag_rules]
 }
+
+run "default_has_no_tag_scoped_outbound_rules" {
+  command = plan
+  assert {
+    condition     = length(output.outbound_tag_rules) == 0
+    error_message = "No tag-scoped outbound rule may render unless outbound_to_tag_rules lists one."
+  }
+  assert {
+    condition = alltrue([
+      for r in digitalocean_firewall.this.outbound_rule :
+      length(coalesce(r.destination_tags, [])) == 0
+    ])
+    error_message = "By default no outbound rule may carry destination_tags."
+  }
+}
+
+run "outbound_to_tag_renders_only_tag_destined_rules" {
+  command = plan
+  variables {
+    outbound_to_tag_rules = [
+      { protocol = "tcp", port_range = "22" },
+      { protocol = "udp", port_range = "9999" },
+    ]
+  }
+  assert {
+    condition     = output.outbound_rule_count == 7
+    error_message = "The five defaults plus exactly the two listed tag rules must render."
+  }
+  assert {
+    condition     = jsonencode(output.outbound_tag_rules) == jsonencode(sort(["tcp/22/cah-baseline-nyc3", "udp/9999/cah-baseline-nyc3"]))
+    error_message = "Each tag-scoped outbound rule must be destined to this firewall's own tag and nothing else."
+  }
+  assert {
+    condition = alltrue([
+      for r in digitalocean_firewall.this.outbound_rule :
+      length(coalesce(r.destination_tags, [])) == 0 ? true : (length(coalesce(r.destination_addresses, [])) == 0 && length(coalesce(r.destination_droplet_ids, [])) == 0 && length(coalesce(r.destination_load_balancer_uids, [])) == 0 && toset(r.destination_tags) == toset(["cah-baseline-nyc3"]))
+    ])
+    error_message = "Tag-scoped outbound rules must carry destination_tags equal to the module tag and no destination addresses."
+  }
+  assert {
+    condition = jsonencode(output.outbound_rules) == jsonencode(sort([
+      "tcp/443/0.0.0.0/0,::/0",
+      "tcp/80/0.0.0.0/0,::/0",
+      "tcp/53/0.0.0.0/0,::/0",
+      "udp/53/0.0.0.0/0,::/0",
+      "udp/123/0.0.0.0/0,::/0",
+    ]))
+    error_message = "The address-based outbound list must be unchanged by tag-scoped rules."
+  }
+  assert {
+    condition     = output.inbound_rule_count == 0
+    error_message = "Outbound tag rules must not add inbound rules."
+  }
+}
+
+run "an_outbound_tag_rule_with_an_unknown_protocol_is_rejected" {
+  command = plan
+  variables {
+    outbound_to_tag_rules = [{ protocol = "icmp", port_range = "22" }]
+  }
+  expect_failures = [var.outbound_to_tag_rules]
+}
+
+run "an_outbound_tag_rule_with_an_empty_port_range_is_rejected" {
+  command = plan
+  variables {
+    outbound_to_tag_rules = [{ protocol = "tcp", port_range = "" }]
+  }
+  expect_failures = [var.outbound_to_tag_rules]
+}

+ 14 - 0
modules/firewall/variables.tf

@@ -38,3 +38,17 @@ variable "inbound_from_tag_rules" {
     error_message = "Each inbound rule needs protocol tcp or udp and a non-empty port_range."
   }
 }
+
+variable "outbound_to_tag_rules" {
+  type = list(object({
+    protocol   = string
+    port_range = string
+  }))
+  default     = []
+  description = "Outbound rules that reach only droplets carrying this firewall's own tag (no addresses). Empty means none."
+
+  validation {
+    condition     = alltrue([for r in var.outbound_to_tag_rules : contains(["tcp", "udp"], r.protocol) && length(r.port_range) > 0])
+    error_message = "Each outbound-to-tag rule needs protocol tcp or udp and a non-empty port_range."
+  }
+}

+ 12 - 0
stacks/00-foundation/tests/foundation.tftest.hcl

@@ -34,3 +34,15 @@ run "foundation_declares_no_costed_resources" {
     error_message = "Stage 0 must cost nothing."
   }
 }
+
+run "baseline_firewall_has_no_tag_scoped_rules_in_either_direction" {
+  command = plan
+  assert {
+    condition     = module.baseline_firewall.inbound_rule_count == 0 && module.baseline_firewall.outbound_rule_count == 5
+    error_message = "The baseline firewall must keep zero inbound rules and exactly its five default outbound rules."
+  }
+  assert {
+    condition     = length(module.baseline_firewall.outbound_tag_rules) == 0 && length(module.baseline_firewall.inbound_rules) == 0
+    error_message = "The baseline firewall must not carry any tag-scoped rule; that is the connector firewall's job."
+  }
+}

+ 9 - 2
stacks/10-access/main.tf

@@ -61,11 +61,15 @@ module "connector_b" {
 }
 
 # The baseline firewall already denies all inbound traffic. This one adds the
-# outbound ports the connector needs and one inbound rule, TCP 22 from other
+# outbound ports the connector needs and TCP 22 in both directions between
 # droplets with the connector tag. Twingate may route a session to either
 # connector, so each must reach the other's SSH (all connectors in a remote
 # network need identical reach:
-# https://www.twingate.com/docs/connector-best-practices). SSH is still only
+# https://www.twingate.com/docs/connector-best-practices). The target needs the
+# inbound rule to accept, and the sender needs the outbound rule to open the
+# connection: DigitalOcean firewalls are stateful for replies, but a new
+# connection is allowed out only if an outbound rule permits it. Both rules name
+# the connector tag, never an address. SSH is still only
 # reachable from outside through Twingate; there is no public inbound. Twingate's allowlist article
 # (https://help.twingate.com/articles/1198271128-allowlist-for-outbound-connections-to-twingate-infrastructure)
 # requires outbound UDP to any port for peer-to-peer connections, so UDP is
@@ -79,6 +83,9 @@ module "connector_firewall" {
   inbound_from_tag_rules = [
     { protocol = "tcp", port_range = "22" },
   ]
+  outbound_to_tag_rules = [
+    { protocol = "tcp", port_range = "22" },
+  ]
   extra_outbound_rules = [
     { protocol = "tcp", port_range = "30000-31000" },
     { protocol = "udp", port_range = "1-65535" },

+ 7 - 3
stacks/10-access/tests/access.tftest.hcl

@@ -132,7 +132,7 @@ run "an_empty_ssh_key_name_is_rejected" {
   expect_failures = [var.admin_ssh_key_name]
 }
 
-run "connector_has_its_own_tag_and_a_peer_ssh_only_inbound_firewall" {
+run "connector_has_its_own_tag_and_a_peer_ssh_only_firewall" {
   command = plan
   assert {
     condition     = digitalocean_tag.connector.name == "cah-connector-nyc3"
@@ -147,8 +147,8 @@ run "connector_has_its_own_tag_and_a_peer_ssh_only_inbound_firewall" {
     error_message = "The only inbound rule must be tcp 22 sourced from the connector tag, never an address."
   }
   assert {
-    condition     = module.connector_firewall.outbound_rule_count == 7
-    error_message = "Connector firewall must be the module's 5 default outbound rules plus the 2 Twingate extras."
+    condition     = module.connector_firewall.outbound_rule_count == 8
+    error_message = "Connector firewall must be the module's 5 default outbound rules, the 2 Twingate extras and the one tag-scoped SSH rule."
   }
   assert {
     condition = jsonencode(module.connector_firewall.outbound_rules) == jsonencode(sort([
@@ -162,6 +162,10 @@ run "connector_has_its_own_tag_and_a_peer_ssh_only_inbound_firewall" {
     ]))
     error_message = "Connector firewall outbound must be exactly the five defaults plus tcp 30000-31000 and udp 1-65535 (Twingate peer-to-peer needs outbound UDP to any port), each to 0.0.0.0/0 and ::/0."
   }
+  assert {
+    condition     = jsonencode(module.connector_firewall.outbound_tag_rules) == jsonencode(["tcp/22/cah-connector-nyc3"])
+    error_message = "The only tag-scoped outbound rule must be tcp 22 destined to the connector tag, so a connector can open SSH to its peer and nothing else."
+  }
 }
 
 run "connector_a_ssh_is_reachable_only_as_a_restricted_twingate_resource" {