main.tf 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. locals {
  2. name = "${var.layer}-${var.region}-${var.purpose}"
  3. # The same five outbound ports as the DigitalOcean firewall. Never "all".
  4. default_outbound = [
  5. { protocol = "tcp", port_range = "443" },
  6. { protocol = "tcp", port_range = "80" },
  7. { protocol = "tcp", port_range = "53" },
  8. { protocol = "udp", port_range = "53" },
  9. { protocol = "udp", port_range = "123" },
  10. ]
  11. outbound = concat(local.default_outbound, var.extra_outbound_rules)
  12. # "443" -> 443..443, "30000-31000" -> 30000..31000, "all" -> 0..65535.
  13. # The protocol stays tcp or udp: the every-protocol rule that would
  14. # re-create AWS's default allow-all is not expressible through this module.
  15. address_rules = {
  16. for r in local.outbound : "${r.protocol}-${r.port_range}" => {
  17. protocol = r.protocol
  18. from = r.port_range == "all" ? 0 : tonumber(split("-", r.port_range)[0])
  19. to = r.port_range == "all" ? 65535 : tonumber(reverse(split("-", r.port_range))[0])
  20. }
  21. }
  22. member_ingress_rules = {
  23. for r in var.inbound_from_member_rules : "${r.protocol}-${r.port_range}" => {
  24. protocol = r.protocol
  25. from = r.port_range == "all" ? 0 : tonumber(split("-", r.port_range)[0])
  26. to = r.port_range == "all" ? 65535 : tonumber(reverse(split("-", r.port_range))[0])
  27. }
  28. }
  29. # Keyed by protocol, ports and source group so a reorder changes nothing.
  30. # The ports are already validated as a number or an ascending range.
  31. other_group_ingress_rules = {
  32. for r in var.inbound_from_other_groups : "${r.protocol}-${r.port_range}-${r.security_group_id}" => {
  33. protocol = r.protocol
  34. from = tonumber(split("-", r.port_range)[0])
  35. to = tonumber(reverse(split("-", r.port_range))[0])
  36. source = r.security_group_id
  37. }
  38. }
  39. member_egress_rules = {
  40. for r in var.outbound_to_member_rules : "${r.protocol}-${r.port_range}" => {
  41. protocol = r.protocol
  42. from = r.port_range == "all" ? 0 : tonumber(split("-", r.port_range)[0])
  43. to = r.port_range == "all" ? 65535 : tonumber(reverse(split("-", r.port_range))[0])
  44. }
  45. }
  46. }
  47. # One security group, attached to the machine's network interface when the
  48. # machine launches (vpc_security_group_ids), so there is no unprotected moment.
  49. #
  50. # No ingress or egress block is declared inline. Every rule is its own
  51. # aws_vpc_security_group_*_rule resource below. AWS gives a new group one
  52. # allow-all outbound rule; the AWS provider removes that default when it creates
  53. # a group inside a VPC, so the only egress that exists is what is listed here.
  54. # (UNVERIFIED against a live account: the tests prove the module declares no
  55. # allow-all rule, not what AWS shows after an apply. See the README.)
  56. resource "aws_security_group" "this" {
  57. name = local.name
  58. description = "${var.purpose} machines of ${var.layer} in ${var.region}: no inbound from the internet"
  59. vpc_id = var.vpc_id
  60. tags = { Name = local.name }
  61. }
  62. # Inbound is deny-all: a security group allows nothing in that no rule allows,
  63. # and inbound_from_member_rules is empty by default. When it lists something,
  64. # the source is this group itself (referenced_security_group_id), never an
  65. # address, so only machines wearing this group can connect.
  66. resource "aws_vpc_security_group_ingress_rule" "member" {
  67. for_each = local.member_ingress_rules
  68. security_group_id = aws_security_group.this.id
  69. referenced_security_group_id = aws_security_group.this.id
  70. ip_protocol = each.value.protocol
  71. from_port = each.value.from
  72. to_port = each.value.to
  73. description = "from members of this group, ${each.key}"
  74. }
  75. # Inbound from ANOTHER security group (the telemetry source group, for the
  76. # metrics ports). The source is always referenced_security_group_id; the
  77. # resource sets no cidr_ipv4, cidr_ipv6 or prefix_list_id, and no input of this
  78. # module feeds one, so a rule from an address cannot be written here.
  79. resource "aws_vpc_security_group_ingress_rule" "other_group" {
  80. for_each = local.other_group_ingress_rules
  81. security_group_id = aws_security_group.this.id
  82. referenced_security_group_id = each.value.source
  83. ip_protocol = each.value.protocol
  84. from_port = each.value.from
  85. to_port = each.value.to
  86. description = "from members of ${each.value.source}, ${each.value.protocol} ${each.value.from}-${each.value.to}"
  87. # This resource is for OTHER groups. A source equal to the target group would
  88. # admit every machine wearing it (for connectors: connector-to-connector
  89. # metrics), which is the member rules' job and a separate decision. The id of
  90. # a group that does not exist yet is unknown at plan time, so this is checked
  91. # at apply for a first creation and at plan afterwards.
  92. lifecycle {
  93. precondition {
  94. condition = each.value.source != aws_security_group.this.id
  95. error_message = "inbound_from_other_groups names this security group itself (${each.value.source}) as a source. Use a different group; rules that admit the group's own members belong in inbound_from_member_rules."
  96. }
  97. }
  98. }
  99. # Trivy AWS-0104 flags egress to any address. It is accepted here for the same
  100. # reason as on DigitalOcean: the any-address rules name specific ports (never
  101. # all protocols), inbound is zero unless a rule admits this group's own
  102. # members, and the hosts these machines must reach (Twingate relays, package
  103. # mirrors, DNS and NTP servers) have no fixed addresses to pin. IPv4 only: the
  104. # VPC has no IPv6.
  105. #trivy:ignore:AVD-AWS-0104
  106. resource "aws_vpc_security_group_egress_rule" "internet" {
  107. for_each = local.address_rules
  108. security_group_id = aws_security_group.this.id
  109. cidr_ipv4 = "0.0.0.0/0"
  110. ip_protocol = each.value.protocol
  111. from_port = each.value.from
  112. to_port = each.value.to
  113. description = "internet, ${each.key}"
  114. }
  115. # The sending half of the member rule. A security group is stateful for
  116. # replies, but opening a connection needs an outbound rule at the sender
  117. # whenever the default allow-all is gone.
  118. resource "aws_vpc_security_group_egress_rule" "member" {
  119. for_each = local.member_egress_rules
  120. security_group_id = aws_security_group.this.id
  121. referenced_security_group_id = aws_security_group.this.id
  122. ip_protocol = each.value.protocol
  123. from_port = each.value.from
  124. to_port = each.value.to
  125. description = "to members of this group, ${each.key}"
  126. }