diff --git a/envs/prod/main.tf b/envs/prod/main.tf index 0d1f5da..b012c28 100644 --- a/envs/prod/main.tf +++ b/envs/prod/main.tf @@ -175,6 +175,12 @@ module "network_public" { default_ipv4 = "0.0.0.0/0" } + web_egress_policy = { + homelab_tcp_ports = [3000, 3001, 8200, 8300, 8301, 9000, 25090] + homelab_udp_ports = [123] + public_tcp_ports = [80, 443] + } + 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 36326ca..62b1782 100644 --- a/modules/network_public/locals.tf +++ b/modules/network_public/locals.tf @@ -67,6 +67,44 @@ locals { } } + web_egress_homelab_cidrs = { + private_network_a = var.route_destinations.private_network_a + private_network_b = var.route_destinations.private_network_b + } + + web_egress_rules = merge( + { + for pair in setproduct(keys(local.web_egress_homelab_cidrs), var.web_egress_policy.homelab_tcp_ports) : + "${pair[0]}_tcp_${pair[1]}" => { + description = "Allow TCP ${pair[1]} to homelab ${replace(pair[0], "_", " ")} via VyOS" + ip_protocol = "tcp" + from_port = pair[1] + to_port = pair[1] + cidr_ipv4 = local.web_egress_homelab_cidrs[pair[0]] + } + }, + { + for pair in setproduct(keys(local.web_egress_homelab_cidrs), var.web_egress_policy.homelab_udp_ports) : + "${pair[0]}_udp_${pair[1]}" => { + description = "Allow UDP ${pair[1]} to homelab ${replace(pair[0], "_", " ")} via VyOS" + ip_protocol = "udp" + from_port = pair[1] + to_port = pair[1] + cidr_ipv4 = local.web_egress_homelab_cidrs[pair[0]] + } + }, + { + for port in var.web_egress_policy.public_tcp_ports : + "public_tcp_${port}" => { + description = "Allow TCP ${port} to any IPv4 destination" + ip_protocol = "tcp" + from_port = port + to_port = port + cidr_ipv4 = "0.0.0.0/0" + } + }, + ) + ssh_security_group_name = coalesce(var.ssh_security_group.name, "${var.name_prefix}-ssh-enabled") web_instance_name = coalesce(var.web_instance.name, "${var.name_prefix}-web") key_pair_name = coalesce(var.key_pair.name, "${var.name_prefix}-key") diff --git a/modules/network_public/outputs.tf b/modules/network_public/outputs.tf index c0f7280..5c3f2d4 100644 --- a/modules/network_public/outputs.tf +++ b/modules/network_public/outputs.tf @@ -61,3 +61,8 @@ output "vyos_source_security_group_ids" { if var.ssh_ingress_rules[key].use_vyos_security_group } } + +output "web_egress_rule_specs" { + description = "Restricted public-web egress rules keyed by stable destination, protocol, and port identifiers." + value = local.web_egress_rules +} diff --git a/modules/network_public/security_groups.tf b/modules/network_public/security_groups.tf index b7c637b..9c2e498 100644 --- a/modules/network_public/security_groups.tf +++ b/modules/network_public/security_groups.tf @@ -20,6 +20,21 @@ resource "aws_vpc_security_group_ingress_rule" "web" { referenced_security_group_id = each.value.use_vyos_security_group ? var.vyos_security_group_id : null } +resource "aws_vpc_security_group_egress_rule" "web" { + for_each = local.web_egress_rules + + region = var.region + + security_group_id = aws_security_group.web.id + description = each.value.description + ip_protocol = each.value.ip_protocol + from_port = each.value.from_port + to_port = each.value.to_port + cidr_ipv4 = each.value.cidr_ipv4 +} + +# Keep the imported all-egress rule during stage 1. Stage 2 removes this +# resource only after host inventory and the REJECT-monitoring query are ready. resource "aws_vpc_security_group_egress_rule" "web_all_ipv4" { region = var.region diff --git a/modules/network_public/variables.tf b/modules/network_public/variables.tf index 88ad914..14c87ae 100644 --- a/modules/network_public/variables.tf +++ b/modules/network_public/variables.tf @@ -103,6 +103,26 @@ variable "ssh_ingress_rules" { })) } +variable "web_egress_policy" { + description = "Restricted outbound ports for the public web instance. Homelab destinations are the two VyOS route CIDRs; public TCP destinations use IPv4 any-address." + type = object({ + homelab_tcp_ports = set(number) + homelab_udp_ports = set(number) + public_tcp_ports = set(number) + }) + + validation { + condition = alltrue([ + for port in setunion( + var.web_egress_policy.homelab_tcp_ports, + var.web_egress_policy.homelab_udp_ports, + var.web_egress_policy.public_tcp_ports, + ) : port >= 1 && port <= 65535 && port == floor(port) + ]) + error_message = "Every web egress port must be an integer from 1 through 65535." + } +} + variable "web_instance" { description = "Existing public web EC2 instance. user_data is intentionally absent." type = object({ diff --git a/tests/network_group3/apply-tests/osaka_apply.tftest.hcl b/tests/network_group3/apply-tests/osaka_apply.tftest.hcl index 2488160..e56720a 100644 --- a/tests/network_group3/apply-tests/osaka_apply.tftest.hcl +++ b/tests/network_group3/apply-tests/osaka_apply.tftest.hcl @@ -39,6 +39,11 @@ run "apply_network_group3_osaka" { error_message = "All six public ingress rules must reference the created substitute security group." } + assert { + condition = output.web_egress_policy_matches + error_message = "The Osaka apply test must use the same 18 restricted egress rules as production." + } + assert { condition = alltrue([ for name in values(output.named_resources) : startswith(name, var.name_prefix) diff --git a/tests/network_group3/main.tf b/tests/network_group3/main.tf index 44b1fd9..08c6466 100644 --- a/tests/network_group3/main.tf +++ b/tests/network_group3/main.tf @@ -40,6 +40,12 @@ module "network_public" { private_network_b = "198.51.100.0/24" } + web_egress_policy = { + homelab_tcp_ports = [3000, 3001, 8200, 8300, 8301, 9000, 25090] + homelab_udp_ports = [123] + public_tcp_ports = [80, 443] + } + 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 0337003..07fcf7a 100644 --- a/tests/network_group3/outputs.tf +++ b/tests/network_group3/outputs.tf @@ -14,6 +14,16 @@ output "vyos_source_security_group_ids" { value = module.network_public.vyos_source_security_group_ids } +output "web_egress_policy_matches" { + value = length(module.network_public.web_egress_rule_specs) == 18 && 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 + ]) +} + output "named_resources" { value = merge( module.network_public.named_resource_names, diff --git a/tests/network_group3/tests/osaka_plan.tftest.hcl b/tests/network_group3/tests/osaka_plan.tftest.hcl index 5f22152..5a736c0 100644 --- a/tests/network_group3/tests/osaka_plan.tftest.hcl +++ b/tests/network_group3/tests/osaka_plan.tftest.hcl @@ -1,5 +1,10 @@ provider "aws" { - region = var.region + region = var.region + access_key = "mock_access_key" + secret_key = "mock_secret_key" + skip_credentials_validation = true + skip_metadata_api_check = true + skip_requesting_account_id = true } # Flow Logs need pre-existing destinations, including a global IAM role for @@ -58,6 +63,11 @@ run "plan_network_group3_osaka" { error_message = "All six public ingress rules must reference the substitute security group." } + assert { + condition = output.web_egress_policy_matches + error_message = "The Osaka test must use the same 18 restricted egress rules as production." + } + assert { condition = alltrue([ for name in values(output.named_resources) : startswith(name, var.name_prefix)