Add restricted web egress rules
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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({
|
||||
|
||||
Reference in New Issue
Block a user