diff --git a/modules/network_public/outputs.tf b/modules/network_public/outputs.tf new file mode 100644 index 0000000..c0f7280 --- /dev/null +++ b/modules/network_public/outputs.tf @@ -0,0 +1,63 @@ +output "vpc_id" { + description = "Managed VPC ID for dependent modules." + value = local.vpc.id +} + +output "subnet_ids" { + description = "Managed subnet IDs keyed by the caller's logical names." + value = local.subnet_ids +} + +output "key_pair_name" { + description = "Managed EC2 key pair name for dependent instances." + value = aws_key_pair.amazon.key_name +} + +output "web_instance_name" { + description = "Resolved Name tag for the public web instance." + value = local.web_instance_name +} + +output "ssh_security_group_name" { + description = "Resolved name for the public SSH security group." + value = local.ssh_security_group_name +} + +output "named_resource_names" { + description = "Names and Name tags for explicitly named managed resources." + value = merge( + { + vpc = lookup(local.vpc.tags, "Name", "") + internet_gateway = var.protect_from_destroy ? lookup(aws_internet_gateway.protected[0].tags, "Name", "") : lookup(aws_internet_gateway.unprotected[0].tags, "Name", "") + route_table_dmz = var.protect_from_destroy ? lookup(aws_default_route_table.protected[0].tags, "Name", "") : lookup(aws_default_route_table.unprotected[0].tags, "Name", "") + route_table_intra = var.protect_from_destroy ? lookup(aws_route_table.protected[0].tags, "Name", "") : lookup(aws_route_table.unprotected[0].tags, "Name", "") + dhcp_options = lookup(aws_vpc_dhcp_options.this.tags, "Name", "") + key_pair = aws_key_pair.amazon.key_name + public_security_group = aws_security_group.web.name + web_instance = local.web_instance_name + flow_log_s3 = lookup(aws_flow_log.s3.tags, "Name", "") + flow_log_cloudwatch = lookup(aws_flow_log.cloudwatch.tags, "Name", "") + }, + { + for key, subnet in merge(aws_subnet.protected, aws_subnet.unprotected) : + "subnet_${key}" => lookup(subnet.tags, "Name", "") + }, + ) +} + +output "vyos_route_network_interface_ids" { + description = "VyOS ENI targets on the five managed private/default routes." + value = { + for key in ["dmz_private_a", "dmz_private_b", "intra_private_a", "intra_private_b", "intra_default"] : + key => aws_route.this[key].network_interface_id + } +} + +output "vyos_source_security_group_ids" { + description = "VyOS source security group IDs on the managed public ingress rules." + value = { + for key, rule in aws_vpc_security_group_ingress_rule.web : + key => rule.referenced_security_group_id + if var.ssh_ingress_rules[key].use_vyos_security_group + } +} diff --git a/modules/network_vyos/outputs.tf b/modules/network_vyos/outputs.tf index 50fd967..7cb2e78 100644 --- a/modules/network_vyos/outputs.tf +++ b/modules/network_vyos/outputs.tf @@ -12,3 +12,18 @@ output "security_group_id" { description = "Managed VyOS security group ID for referenced ingress rules." value = local.security_group.id } + +output "source_dest_check" { + description = "Effective source/destination check setting on the VyOS instance." + value = local.instance.source_dest_check +} + +output "instance_name" { + description = "Resolved Name tag for the VyOS instance." + value = local.instance_name +} + +output "security_group_name" { + description = "Resolved name for the VyOS security group." + value = local.security_group_name +} diff --git a/tests/network_group3_4/.terraform.lock.hcl b/tests/network_group3_4/.terraform.lock.hcl new file mode 100644 index 0000000..4c68232 --- /dev/null +++ b/tests/network_group3_4/.terraform.lock.hcl @@ -0,0 +1,26 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "6.73.0" + constraints = "~> 6.73" + hashes = [ + "h1:87t8o/A0y2RE3PtCLx/BiASSrDrKSETYTF9jRvKc7aw=", + "zh:0f7403264fbfbcad526a5ddb9553be6ab12dba14e5901d0c2ccabc38d9ccf9a0", + "zh:26be4e8afc94ef29c98b8219dc49b19ce21a04d3f2b4e5be6116e9526ece0355", + "zh:2ba661a4a782fba053c411ce4d828dc148d7ed3998d073a28370a559400b1578", + "zh:2c422d0edd37bf7470c84f1d1f76487f30948768878204a65a18189bf878467a", + "zh:49f9d1486d48033d298977f7c288f8bbb22bb246f00926d1cb8b0bb98985451c", + "zh:4f946cf11667e1f3bce53fd5ad92eccf47fafaa1bbaa68c742173db61773a1da", + "zh:70beb5e13ddcdbeecb2a78be2b75d09a0fc6ce7e69b574c84a90924fd4262406", + "zh:8c5b974104ff9a887b29fdb2700cdbb5e6fd2eceef1cc3b28f523beda4fd8dfb", + "zh:9252068667bc6528ee6e83e837f3c2f901756136a33442455d61c6fa34e1160a", + "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425", + "zh:9d086b792598b43e87502d47c4a3f388465b3a21a6a6b2783e48c89dee1f0543", + "zh:b5e6e77a43c1655285d99145b2cf0cc2da7a6c6ea3531d000cf8cca2c717693b", + "zh:b7ebe893e5ec76b362b61ffb28c67b40d890e0f59f5f2f8829765b16ee3e53b0", + "zh:c8c1e186839ad3cba37b427181af1e62eaf06c6daa5dca29105e1981dfaeb4c2", + "zh:e988377e7aea51e1fefe0b52c78bc725a11327051e20a71f81069191ee077d2d", + "zh:ee9188027919459a48bf9bac8f8f1dd172f5be198b88c2c47789b6daa8bb8bcb", + ] +} diff --git a/tests/network_group3_4/apply-tests/osaka_apply.tftest.hcl b/tests/network_group3_4/apply-tests/osaka_apply.tftest.hcl new file mode 100644 index 0000000..f23d0dc --- /dev/null +++ b/tests/network_group3_4/apply-tests/osaka_apply.tftest.hcl @@ -0,0 +1,55 @@ +# This file is intentionally outside the default tests/ directory. +# Do not run it without the required approval and an existing Marketplace +# subscription for the selected VyOS AMI. + +provider "aws" { + region = var.region +} + +override_resource { + target = module.network_public.aws_flow_log.s3 + values = { id = "fl-test-s3" } +} + +override_resource { + target = module.network_public.aws_flow_log.cloudwatch + values = { id = "fl-test-cloudwatch" } +} + +variables { + region = "ap-northeast-3" + name_prefix = "tftest-g34-8c2f" + web_ami_id = "ami-0d82c759aab529d2d" + vyos_ami_id = "ami-096d9368e5980d5fb" + test_public_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHSftYPnELJxLgR9G8ftBeCIYo57KmlEc8A8BmqAHSLP tftest-g34-8c2f" +} + +run "apply_network_group3_4_osaka" { + command = apply + + assert { + condition = !output.vyos_source_dest_check + error_message = "The Osaka VyOS instance must disable source/destination checking." + } + + assert { + condition = length(output.vyos_route_network_interface_ids) == 5 && alltrue([ + for id in values(output.vyos_route_network_interface_ids) : id == output.vyos_primary_network_interface_id + ]) + error_message = "All five VyOS routes must target the created VyOS primary ENI." + } + + assert { + condition = length(output.vyos_source_security_group_ids) == 6 && alltrue([ + for id in values(output.vyos_source_security_group_ids) : id == output.vyos_security_group_id + ]) + error_message = "All six public ingress rules must reference the created VyOS security group." + } + + assert { + condition = alltrue([ + for name in values(output.named_resources) : startswith(name, var.name_prefix) + ]) + error_message = "Every named Osaka test resource must use the dedicated prefix." + } +} diff --git a/tests/network_group3_4/main.tf b/tests/network_group3_4/main.tf new file mode 100644 index 0000000..ee9b638 --- /dev/null +++ b/tests/network_group3_4/main.tf @@ -0,0 +1,270 @@ +module "network_public" { + source = "../../modules/network_public" + + region = var.region + name_prefix = var.name_prefix + protect_from_destroy = false + + vpc = { + cidr_block = "10.254.0.0/16" + tags = { Name = "${var.name_prefix}-vpc" } + } + + subnets = { + intra_a = { + cidr_block = "10.254.110.0/24" + availability_zone = "ap-northeast-3a" + tags = { Name = "${var.name_prefix}-intra-a" } + } + dmz = { + cidr_block = "10.254.1.0/24" + availability_zone = "ap-northeast-3b" + tags = { Name = "${var.name_prefix}-dmz" } + } + intra_d = { + cidr_block = "10.254.120.0/24" + availability_zone = "ap-northeast-3c" + tags = { Name = "${var.name_prefix}-intra-c" } + } + } + + route_table_tags = { + dmz = { Name = "${var.name_prefix}-dmz-routes" } + intra = { Name = "${var.name_prefix}-intra-routes" } + } + + internet_gateway_tags = { Name = "${var.name_prefix}-igw" } + + route_destinations = { + private_network_a = "192.0.2.0/24" + private_network_b = "198.51.100.0/24" + } + + vyos_network_interface_id = module.network_vyos.primary_network_interface_id + vyos_security_group_id = module.network_vyos.security_group_id + + dhcp_options = { + domain_name = "${var.region}.compute.internal" + domain_name_servers = ["AmazonProvidedDNS"] + tags = { Name = "${var.name_prefix}-dhcp" } + } + + ssh_security_group = { + description = "${var.name_prefix} public test security group" + } + + ssh_ingress_rules = { + icmp_private = { + description = "ICMP test network" + ip_protocol = "icmp" + cidr_ipv4 = "203.0.113.0/24" + } + icmp_vyos = { + description = "ICMP from VyOS" + ip_protocol = "icmp" + use_vyos_security_group = true + } + ssh_vyos = { + description = "SSH from VyOS" + ip_protocol = "tcp" + from_port = 22 + to_port = 22 + use_vyos_security_group = true + } + http = { + description = "HTTP" + ip_protocol = "tcp" + from_port = 80 + to_port = 80 + cidr_ipv4 = "0.0.0.0/0" + } + https = { + description = "HTTPS" + ip_protocol = "tcp" + from_port = 443 + to_port = 443 + cidr_ipv4 = "0.0.0.0/0" + } + http3 = { + description = "HTTP/3" + ip_protocol = "udp" + from_port = 443 + to_port = 443 + cidr_ipv4 = "0.0.0.0/0" + } + consul_server = { + description = "Consul server from VyOS" + ip_protocol = "tcp" + from_port = 8300 + to_port = 8300 + use_vyos_security_group = true + } + consul_lan_tcp = { + description = "Consul LAN TCP from VyOS" + ip_protocol = "tcp" + from_port = 8301 + to_port = 8301 + use_vyos_security_group = true + } + consul_lan_udp = { + description = "Consul LAN UDP from VyOS" + ip_protocol = "udp" + from_port = 8301 + to_port = 8301 + use_vyos_security_group = true + } + ssh_admin = { + description = "SSH test network" + ip_protocol = "tcp" + from_port = 22 + to_port = 22 + cidr_ipv4 = "203.0.113.0/24" + } + ssh_git = { + description = "SSH alternate port test network" + ip_protocol = "tcp" + from_port = 3843 + to_port = 3843 + cidr_ipv4 = "203.0.113.0/24" + } + wireguard = { + description = "WireGuard" + ip_protocol = "udp" + from_port = 51820 + to_port = 51820 + cidr_ipv4 = "0.0.0.0/0" + } + prometheus = { + description = "Prometheus from VyOS" + ip_protocol = "tcp" + from_port = 60000 + to_port = 60000 + use_vyos_security_group = true + } + } + + web_instance = { + ami = var.web_ami_id + instance_type = "t3.small" + subnet_key = "dmz" + associate_public_ip_address = true + source_dest_check = true + ebs_optimized = true + monitoring = false + + metadata_options = { + http_endpoint = "enabled" + http_tokens = "required" + http_put_response_hop_limit = 2 + http_protocol_ipv6 = "disabled" + instance_metadata_tags = "disabled" + } + + root_block_device = { + delete_on_termination = true + encrypted = true + volume_size = 8 + volume_type = "gp3" + iops = 3000 + throughput = 125 + } + + tags = { Purpose = "${var.name_prefix}-web-test" } + } + + key_pair = { + public_key = var.test_public_key + } + + # These syntactically valid, isolated ARNs are never used by AWS in tests: + # both Flow Log resources are explicitly overridden in each test file. + flow_logs = { + s3 = { + destination_arn = "arn:aws:s3:::${var.name_prefix}-flow-logs" + tags = { Name = "${var.name_prefix}-s3-flow-log" } + } + cloudwatch = { + log_group_arn = "arn:aws:logs:${var.region}:${var.account_id}:log-group:/${var.name_prefix}/flowlogs" + iam_role_arn = "arn:aws:iam::${var.account_id}:role/${var.name_prefix}-flow-logs" + tags = { Name = "${var.name_prefix}-cloudwatch-flow-log" } + } + } +} + +module "network_vyos" { + source = "../../modules/network_vyos" + + region = var.region + name_prefix = var.name_prefix + protect_from_destroy = false + + vpc_id = module.network_public.vpc_id + subnet_id = module.network_public.subnet_ids["dmz"] + key_name = module.network_public.key_pair_name + + security_group = { + description = "${var.name_prefix} VyOS test security group" + } + + ingress_rules = { + vpc_all = { + ip_protocol = "-1" + cidr_ipv4 = "10.254.0.0/16" + } + mosh = { + description = "Allow mosh" + ip_protocol = "udp" + from_port = 60000 + to_port = 61000 + cidr_ipv4 = "0.0.0.0/0" + } + home_all = { + description = "Test network" + ip_protocol = "-1" + cidr_ipv4 = "203.0.113.0/24" + } + ssh = { + description = "Allow SSH" + ip_protocol = "tcp" + from_port = 22 + to_port = 22 + cidr_ipv4 = "203.0.113.0/24" + } + wireguard = { + description = "Allow WireGuard" + ip_protocol = "udp" + from_port = 51820 + to_port = 51820 + cidr_ipv4 = "0.0.0.0/0" + } + } + + instance = { + ami = var.vyos_ami_id + instance_type = "t3.small" + private_ip = "10.254.1.10" + associate_public_ip_address = true + source_dest_check = false + ebs_optimized = true + monitoring = false + + metadata_options = { + http_endpoint = "enabled" + http_tokens = "required" + http_put_response_hop_limit = 1 + http_protocol_ipv6 = "disabled" + instance_metadata_tags = "disabled" + } + + root_block_device = { + delete_on_termination = true + encrypted = true + volume_size = 4 + volume_type = "gp3" + iops = 3000 + throughput = 125 + } + + tags = { Purpose = "${var.name_prefix}-vyos-test" } + } +} diff --git a/tests/network_group3_4/outputs.tf b/tests/network_group3_4/outputs.tf new file mode 100644 index 0000000..6cad27b --- /dev/null +++ b/tests/network_group3_4/outputs.tf @@ -0,0 +1,29 @@ +output "vyos_primary_network_interface_id" { + value = module.network_vyos.primary_network_interface_id +} + +output "vyos_security_group_id" { + value = module.network_vyos.security_group_id +} + +output "vyos_source_dest_check" { + value = module.network_vyos.source_dest_check +} + +output "vyos_route_network_interface_ids" { + value = module.network_public.vyos_route_network_interface_ids +} + +output "vyos_source_security_group_ids" { + value = module.network_public.vyos_source_security_group_ids +} + +output "named_resources" { + value = merge( + module.network_public.named_resource_names, + { + vyos_instance = module.network_vyos.instance_name + vyos_security_group = module.network_vyos.security_group_name + }, + ) +} diff --git a/tests/network_group3_4/tests/osaka_plan.tftest.hcl b/tests/network_group3_4/tests/osaka_plan.tftest.hcl new file mode 100644 index 0000000..e84b201 --- /dev/null +++ b/tests/network_group3_4/tests/osaka_plan.tftest.hcl @@ -0,0 +1,75 @@ +provider "aws" { + region = var.region +} + +# Flow Logs need pre-existing destinations, including a global IAM role for +# CloudWatch. Override only these two resources so the isolated test creates no +# global resource and never refers to production destinations. +override_resource { + target = module.network_public.aws_flow_log.s3 + values = { id = "fl-test-s3" } +} + +override_resource { + target = module.network_public.aws_flow_log.cloudwatch + values = { id = "fl-test-cloudwatch" } +} + +# Stable computed IDs let plan-mode assertions verify the eleven links without +# creating resources. The apply-mode file intentionally does not override these. +override_resource { + target = module.network_vyos.aws_security_group.unprotected[0] + override_during = plan + values = { + id = "sg-00000000000000000" + name = "tftest-g34-8c2f-vyos-router" + } +} + +override_resource { + target = module.network_vyos.aws_instance.unprotected[0] + override_during = plan + values = { + id = "i-00000000000000000" + primary_network_interface_id = "eni-00000000000000000" + source_dest_check = false + } +} + +variables { + region = "ap-northeast-3" + name_prefix = "tftest-g34-8c2f" + web_ami_id = "ami-0d82c759aab529d2d" + vyos_ami_id = "ami-096d9368e5980d5fb" + test_public_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHSftYPnELJxLgR9G8ftBeCIYo57KmlEc8A8BmqAHSLP tftest-g34-8c2f" +} + +run "plan_network_group3_4_osaka" { + command = plan + + assert { + condition = !output.vyos_source_dest_check + error_message = "The Osaka VyOS instance must disable source/destination checking." + } + + assert { + condition = length(output.vyos_route_network_interface_ids) == 5 && alltrue([ + for id in values(output.vyos_route_network_interface_ids) : id == output.vyos_primary_network_interface_id + ]) + error_message = "All five VyOS routes must target the tested VyOS primary ENI." + } + + assert { + condition = length(output.vyos_source_security_group_ids) == 6 && alltrue([ + for id in values(output.vyos_source_security_group_ids) : id == output.vyos_security_group_id + ]) + error_message = "All six public ingress rules must reference the tested VyOS security group." + } + + assert { + condition = alltrue([ + for name in values(output.named_resources) : startswith(name, var.name_prefix) + ]) + error_message = "Every named Osaka test resource must use the dedicated prefix." + } +} diff --git a/tests/network_group3_4/variables.tf b/tests/network_group3_4/variables.tf new file mode 100644 index 0000000..e318d49 --- /dev/null +++ b/tests/network_group3_4/variables.tf @@ -0,0 +1,36 @@ +variable "account_id" { + description = "AWS account ID used only to form syntactically valid test ARNs." + type = string + + validation { + condition = can(regex("^[0-9]{12}$", var.account_id)) + error_message = "account_id must contain exactly 12 digits." + } +} + +variable "region" { + description = "Isolated test region." + type = string + default = "ap-northeast-3" +} + +variable "name_prefix" { + description = "Dedicated prefix for every named test resource." + type = string + default = "tftest-g34-8c2f" +} + +variable "web_ami_id" { + description = "Public Ubuntu 24.04 AMI available in the test region." + type = string +} + +variable "vyos_ami_id" { + description = "VyOS AMI available in the test region." + type = string +} + +variable "test_public_key" { + description = "Public-only test key. No private key is committed or used for a connection." + type = string +} diff --git a/tests/network_group3_4/versions.tf b/tests/network_group3_4/versions.tf new file mode 100644 index 0000000..3d89ea3 --- /dev/null +++ b/tests/network_group3_4/versions.tf @@ -0,0 +1,10 @@ +terraform { + required_version = "= 1.17.2" + + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 6.73" + } + } +}