main.tf 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. locals {
  2. default_outbound = [
  3. { protocol = "tcp", port_range = "443" },
  4. { protocol = "tcp", port_range = "80" },
  5. { protocol = "tcp", port_range = "53" },
  6. { protocol = "udp", port_range = "53" },
  7. { protocol = "udp", port_range = "123" },
  8. ]
  9. # The defaults carry no destinations, so they keep the any-address pair below.
  10. outbound = concat(
  11. [for r in local.default_outbound : merge(r, { destinations = [] })],
  12. var.extra_outbound_rules,
  13. )
  14. # Names use resource_key when it is set and region otherwise.
  15. name_key = var.resource_key != "" ? var.resource_key : var.region
  16. }
  17. # Every rule below is an allow rule: `action` is never set and the provider's
  18. # default is allow (DigitalOcean also supports deny rules, which take
  19. # precedence over allow rules, even across firewalls; not used here).
  20. # Inbound is deny-all: DigitalOcean denies all inbound traffic that no rule
  21. # allows, and inbound_from_tag_rules is empty by default. When it lists
  22. # something, each rule admits only droplets that carry this firewall's own tag
  23. # (source_tags), never an address. inbound_from_other_tags does the same for
  24. # another group's tag (one tag per rule), so nothing outside the tagged group gets in.
  25. # The sending side is the other half: DigitalOcean allows outbound traffic only
  26. # when an outbound rule permits it (replies to an admitted connection are
  27. # stateful, a new connection is not), so outbound_to_tag_rules opens the same
  28. # kind of rule outward, to droplets carrying this firewall's own tag
  29. # (destination_tags), never to an address.
  30. #
  31. # Trivy DIG-0003 flags egress to any address. It is accepted here: the
  32. # any-address outbound rules are restricted to specific ports (never "all"),
  33. # an extra rule may name destinations to narrow itself to known addresses,
  34. # inbound is zero unless a rule admits the firewall's own tag, tag-scoped
  35. # outbound rules name no address at all, and the hosts these droplets must
  36. # reach (Twingate relays, package mirrors, DNS and NTP servers) have no fixed
  37. # addresses, so there is no address list to pin.
  38. #trivy:ignore:AVD-DIG-0003
  39. resource "digitalocean_firewall" "this" {
  40. name = "${var.layer}-${local.name_key}-${var.purpose}"
  41. tags = [var.tag]
  42. # A precondition rather than a variable validation: it compares two variables,
  43. # which a validation cannot do on older OpenTofu. A rule for the firewall's own
  44. # tag in an "other" list would duplicate one in the own-tag lists (rules are
  45. # set-like), so it is rejected.
  46. lifecycle {
  47. precondition {
  48. condition = alltrue([for r in concat(var.inbound_from_other_tags, var.outbound_to_other_tags) : r.tag != var.tag])
  49. error_message = "inbound_from_other_tags and outbound_to_other_tags are for OTHER tags; use inbound_from_tag_rules or outbound_to_tag_rules for the firewall's own tag."
  50. }
  51. }
  52. dynamic "inbound_rule" {
  53. for_each = var.inbound_from_tag_rules
  54. content {
  55. protocol = inbound_rule.value.protocol
  56. port_range = inbound_rule.value.port_range
  57. source_tags = [var.tag]
  58. }
  59. }
  60. # Appended after the existing inbound rules so a call that leaves
  61. # inbound_from_other_tags at its default renders exactly what it did before.
  62. dynamic "inbound_rule" {
  63. for_each = var.inbound_from_other_tags
  64. content {
  65. protocol = inbound_rule.value.protocol
  66. port_range = inbound_rule.value.port_range
  67. source_tags = [inbound_rule.value.tag]
  68. }
  69. }
  70. dynamic "outbound_rule" {
  71. for_each = local.outbound
  72. content {
  73. protocol = outbound_rule.value.protocol
  74. port_range = outbound_rule.value.port_range
  75. # No destinations listed (every default rule, and any extra rule that
  76. # leaves the list out) renders the any-address pair exactly as before.
  77. # A list renders only what it names, so an IPv4-only list opens no IPv6.
  78. destination_addresses = length(outbound_rule.value.destinations) == 0 ? ["0.0.0.0/0", "::/0"] : outbound_rule.value.destinations
  79. }
  80. }
  81. dynamic "outbound_rule" {
  82. for_each = var.outbound_to_tag_rules
  83. content {
  84. protocol = outbound_rule.value.protocol
  85. port_range = outbound_rule.value.port_range
  86. destination_tags = [var.tag]
  87. }
  88. }
  89. # Appended last, for the same reason as the inbound block above.
  90. dynamic "outbound_rule" {
  91. for_each = var.outbound_to_other_tags
  92. content {
  93. protocol = outbound_rule.value.protocol
  94. port_range = outbound_rule.value.port_range
  95. destination_tags = [outbound_rule.value.tag]
  96. }
  97. }
  98. }