From 181891cd770bc3dc86e021461794c08dc4f2b03b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AD=A6=E8=94=B5=E4=B8=80=E5=BC=98?= Date: Sun, 15 Nov 2026 19:46:54 +0900 Subject: [PATCH] Add service-specific web egress rules --- envs/prod/main.tf | 15 +++++++++++++ modules/network_public/locals.tf | 10 +++++++++ modules/network_public/variables.tf | 21 +++++++++++++++++++ tests/network_group3/main.tf | 15 +++++++++++++ tests/network_group3/outputs.tf | 8 ++++++- .../tests/osaka_plan.tftest.hcl | 2 +- 6 files changed, 69 insertions(+), 2 deletions(-) diff --git a/envs/prod/main.tf b/envs/prod/main.tf index e3dc4b2..5ca8cd0 100644 --- a/envs/prod/main.tf +++ b/envs/prod/main.tf @@ -180,6 +180,21 @@ module "network_public" { public_tcp_ports = [80, 443] } + web_egress_service_rules = { + consul_serf_lan = { + description = "Allow Consul Serf LAN gossip to the homelab service network" + ip_protocol = "udp" + port = 8301 + cidr_ipv4 = "192.168.10.0/24" + } + loki_ingest = { + description = "Allow Loki ingest to the homelab service network" + ip_protocol = "tcp" + port = 3100 + cidr_ipv4 = "192.168.10.0/24" + } + } + vyos_network_interface_id = module.network_vyos.primary_network_interface_id vyos_security_group_id = module.network_vyos.security_group_id diff --git a/modules/network_public/locals.tf b/modules/network_public/locals.tf index 62b1782..86326d5 100644 --- a/modules/network_public/locals.tf +++ b/modules/network_public/locals.tf @@ -103,6 +103,16 @@ locals { cidr_ipv4 = "0.0.0.0/0" } }, + { + for key, rule in var.web_egress_service_rules : + "service_${key}" => { + description = rule.description + ip_protocol = rule.ip_protocol + from_port = rule.port + to_port = rule.port + cidr_ipv4 = rule.cidr_ipv4 + } + }, ) ssh_security_group_name = coalesce(var.ssh_security_group.name, "${var.name_prefix}-ssh-enabled") diff --git a/modules/network_public/variables.tf b/modules/network_public/variables.tf index 14c87ae..6b5b0f9 100644 --- a/modules/network_public/variables.tf +++ b/modules/network_public/variables.tf @@ -123,6 +123,27 @@ variable "web_egress_policy" { } } +variable "web_egress_service_rules" { + description = "Additional public-web egress rules for services on a single explicit destination CIDR." + type = map(object({ + description = string + ip_protocol = string + port = number + cidr_ipv4 = string + })) + + validation { + condition = alltrue([ + for rule in values(var.web_egress_service_rules) : + contains(["tcp", "udp"], rule.ip_protocol) && + rule.port >= 1 && rule.port <= 65535 && rule.port == floor(rule.port) && + trimspace(rule.description) != "" && + can(cidrnetmask(rule.cidr_ipv4)) + ]) + error_message = "Every service egress rule must have a description, TCP or UDP protocol, an integer port from 1 through 65535, and a valid IPv4 CIDR." + } +} + variable "web_instance" { description = "Existing public web EC2 instance. user_data is intentionally absent." type = object({ diff --git a/tests/network_group3/main.tf b/tests/network_group3/main.tf index 08c6466..40ed61d 100644 --- a/tests/network_group3/main.tf +++ b/tests/network_group3/main.tf @@ -46,6 +46,21 @@ module "network_public" { public_tcp_ports = [80, 443] } + web_egress_service_rules = { + consul_serf_lan = { + description = "Allow Consul Serf LAN gossip to the test service network" + ip_protocol = "udp" + port = 8301 + cidr_ipv4 = "203.0.113.0/24" + } + loki_ingest = { + description = "Allow Loki ingest to the test service network" + ip_protocol = "tcp" + port = 3100 + cidr_ipv4 = "203.0.113.0/24" + } + } + vyos_network_interface_id = aws_network_interface.route_target.id vyos_security_group_id = aws_security_group.vyos_source.id diff --git a/tests/network_group3/outputs.tf b/tests/network_group3/outputs.tf index 07fcf7a..c8ab64c 100644 --- a/tests/network_group3/outputs.tf +++ b/tests/network_group3/outputs.tf @@ -15,12 +15,18 @@ output "vyos_source_security_group_ids" { } output "web_egress_policy_matches" { - value = length(module.network_public.web_egress_rule_specs) == 18 && alltrue([ + value = length(module.network_public.web_egress_rule_specs) == 20 && alltrue([ for cidr in ["192.0.2.0/24", "198.51.100.0/24"] : toset([for rule in values(module.network_public.web_egress_rule_specs) : rule.from_port if rule.cidr_ipv4 == cidr && rule.ip_protocol == "tcp"]) == toset([3000, 3001, 8200, 8300, 8301, 9000, 25090]) && toset([for rule in values(module.network_public.web_egress_rule_specs) : rule.from_port if rule.cidr_ipv4 == cidr && rule.ip_protocol == "udp"]) == toset([123]) ]) && toset([for rule in values(module.network_public.web_egress_rule_specs) : rule.from_port if rule.cidr_ipv4 == "0.0.0.0/0" && rule.ip_protocol == "tcp"]) == toset([80, 443]) && alltrue([ for rule in values(module.network_public.web_egress_rule_specs) : rule.description != "" && rule.from_port == rule.to_port + ]) && toset([ + for key, rule in module.network_public.web_egress_rule_specs : "${key}|${rule.ip_protocol}|${rule.from_port}|${rule.cidr_ipv4}" + if startswith(key, "service_") + ]) == toset([ + "service_consul_serf_lan|udp|8301|203.0.113.0/24", + "service_loki_ingest|tcp|3100|203.0.113.0/24", ]) } diff --git a/tests/network_group3/tests/osaka_plan.tftest.hcl b/tests/network_group3/tests/osaka_plan.tftest.hcl index 5a736c0..fedb84d 100644 --- a/tests/network_group3/tests/osaka_plan.tftest.hcl +++ b/tests/network_group3/tests/osaka_plan.tftest.hcl @@ -65,7 +65,7 @@ run "plan_network_group3_osaka" { assert { condition = output.web_egress_policy_matches - error_message = "The Osaka test must use the same 18 restricted egress rules as production." + error_message = "The Osaka test must use the same 20-rule restricted egress structure as production." } assert {