Add restricted web egress rules

This commit is contained in:
2026-11-15 08:46:37 +09:00
parent 7eded181e7
commit 16b5332e96
9 changed files with 116 additions and 1 deletions
+6
View File
@@ -175,6 +175,12 @@ module "network_public" {
default_ipv4 = "0.0.0.0/0" 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_network_interface_id = module.network_vyos.primary_network_interface_id
vyos_security_group_id = module.network_vyos.security_group_id vyos_security_group_id = module.network_vyos.security_group_id
+38
View File
@@ -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") 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") web_instance_name = coalesce(var.web_instance.name, "${var.name_prefix}-web")
key_pair_name = coalesce(var.key_pair.name, "${var.name_prefix}-key") key_pair_name = coalesce(var.key_pair.name, "${var.name_prefix}-key")
+5
View File
@@ -61,3 +61,8 @@ output "vyos_source_security_group_ids" {
if var.ssh_ingress_rules[key].use_vyos_security_group 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
}
+15
View File
@@ -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 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" { resource "aws_vpc_security_group_egress_rule" "web_all_ipv4" {
region = var.region region = var.region
+20
View File
@@ -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" { variable "web_instance" {
description = "Existing public web EC2 instance. user_data is intentionally absent." description = "Existing public web EC2 instance. user_data is intentionally absent."
type = object({ type = object({
@@ -39,6 +39,11 @@ run "apply_network_group3_osaka" {
error_message = "All six public ingress rules must reference the created substitute security group." 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 { assert {
condition = alltrue([ condition = alltrue([
for name in values(output.named_resources) : startswith(name, var.name_prefix) for name in values(output.named_resources) : startswith(name, var.name_prefix)
+6
View File
@@ -40,6 +40,12 @@ module "network_public" {
private_network_b = "198.51.100.0/24" 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_network_interface_id = aws_network_interface.route_target.id
vyos_security_group_id = aws_security_group.vyos_source.id vyos_security_group_id = aws_security_group.vyos_source.id
+10
View File
@@ -14,6 +14,16 @@ output "vyos_source_security_group_ids" {
value = module.network_public.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" { output "named_resources" {
value = merge( value = merge(
module.network_public.named_resource_names, module.network_public.named_resource_names,
@@ -1,5 +1,10 @@
provider "aws" { 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 # 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." 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 { assert {
condition = alltrue([ condition = alltrue([
for name in values(output.named_resources) : startswith(name, var.name_prefix) for name in values(output.named_resources) : startswith(name, var.name_prefix)