variables.tf 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. variable "layer" {
  2. type = string
  3. }
  4. variable "region" {
  5. type = string
  6. }
  7. variable "purpose" {
  8. type = string
  9. description = "Role in the firewall name (for example baseline or connector)."
  10. }
  11. variable "tag" {
  12. type = string
  13. description = "Droplet tag this firewall attaches to."
  14. }
  15. variable "extra_outbound_rules" {
  16. type = list(object({
  17. protocol = string
  18. port_range = string
  19. destinations = optional(list(string), [])
  20. allow_world_destinations = optional(bool, false)
  21. }))
  22. default = []
  23. description = "Outbound rules appended to the defaults. destinations (optional) lists the only addresses the rule may reach: IPv4 and IPv6 CIDRs or plain addresses (203.0.113.0/24, 203.0.113.7, 2001:db8::/32, 2001:db8::7). Empty or unset keeps the rule as it always was: 0.0.0.0/0 and ::/0. A list of IPv4 entries opens no IPv6 and the reverse. A /0 entry (0.0.0.0/0, ::/0) in the list is refused unless that rule sets allow_world_destinations = true."
  24. validation {
  25. condition = alltrue(flatten([
  26. for r in var.extra_outbound_rules : [
  27. for d in r.destinations :
  28. can(regex("^[0-9A-Fa-f:.]+(/[0-9]{1,3})?$", d)) && can(cidrhost(strcontains(d, "/") ? d : format("%s/%d", d, strcontains(d, ":") ? 128 : 32), 0))
  29. ]
  30. ]))
  31. error_message = "Each destinations entry must be an IPv4 or IPv6 address or CIDR, for example 203.0.113.0/24, 203.0.113.7, 2001:db8::/32 or 2001:db8::7."
  32. }
  33. validation {
  34. condition = alltrue([
  35. for r in var.extra_outbound_rules :
  36. length(distinct(r.destinations)) == length(r.destinations)
  37. ])
  38. error_message = "A destinations list may not repeat an entry."
  39. }
  40. validation {
  41. condition = alltrue(flatten([
  42. for r in var.extra_outbound_rules : [
  43. for d in r.destinations :
  44. r.allow_world_destinations || !(can(regex("/0+$", d)) || d == "0.0.0.0" || can(regex("^[0:]*:[0:]*$", d)))
  45. ]
  46. ]))
  47. error_message = "A destinations entry that is a /0 (0.0.0.0/0 or ::/0) or an unspecified address (0.0.0.0, ::, or any all-zero IPv6 spelling such as 0:0:0:0:0:0:0:0) opens the rule to the whole internet. Leave destinations empty for that, or set allow_world_destinations = true on the rule to say so on purpose."
  48. }
  49. validation {
  50. condition = alltrue([
  51. for r in var.extra_outbound_rules :
  52. !r.allow_world_destinations || length([for d in r.destinations : d if can(regex("/0+$", d)) || d == "0.0.0.0" || can(regex("^[0:]*:[0:]*$", d))]) > 0
  53. ])
  54. error_message = "allow_world_destinations = true is only for a rule whose destinations list includes a /0 entry; remove the flag, or list the world entry it is meant to allow."
  55. }
  56. }
  57. variable "inbound_from_tag_rules" {
  58. type = list(object({
  59. protocol = string
  60. port_range = string
  61. }))
  62. default = []
  63. description = "Inbound rules that admit only droplets carrying this firewall's own tag. Empty means deny-all inbound."
  64. validation {
  65. condition = alltrue([for r in var.inbound_from_tag_rules : contains(["tcp", "udp"], r.protocol) && length(r.port_range) > 0])
  66. error_message = "Each inbound rule needs protocol tcp or udp and a non-empty port_range."
  67. }
  68. }
  69. variable "outbound_to_tag_rules" {
  70. type = list(object({
  71. protocol = string
  72. port_range = string
  73. }))
  74. default = []
  75. description = "Outbound rules that reach only droplets carrying this firewall's own tag (no addresses). Empty means none."
  76. validation {
  77. condition = alltrue([for r in var.outbound_to_tag_rules : contains(["tcp", "udp"], r.protocol) && length(r.port_range) > 0])
  78. error_message = "Each outbound-to-tag rule needs protocol tcp or udp and a non-empty port_range."
  79. }
  80. }
  81. variable "inbound_from_other_tags" {
  82. type = list(object({
  83. protocol = string
  84. port_range = string
  85. tag = string
  86. }))
  87. default = []
  88. description = "Inbound rules that admit only droplets carrying the given OTHER tag (one rule per entry, source_tags = [tag], never an address). Additive to inbound_from_tag_rules. Empty means none."
  89. validation {
  90. condition = alltrue([
  91. for r in var.inbound_from_other_tags :
  92. contains(["tcp", "udp"], r.protocol) && try(can(regex("^[1-9][0-9]{0,4}(-[1-9][0-9]{0,4})?$", r.port_range)) && tonumber(split("-", r.port_range)[0]) <= tonumber(reverse(split("-", r.port_range))[0]) && tonumber(reverse(split("-", r.port_range))[0]) <= 65535, false)
  93. ])
  94. error_message = "Each rule needs protocol tcp or udp and a port_range of a port or range between 1 and 65535 in ascending order (443, 30000-31000)."
  95. }
  96. validation {
  97. condition = alltrue([for r in var.inbound_from_other_tags : can(regex("^[A-Za-z0-9:_-]{1,255}$", r.tag))])
  98. error_message = "Each tag must be 1 to 255 characters: letters, digits, colon, hyphen or underscore (the DigitalOcean tag shape)."
  99. }
  100. validation {
  101. condition = length(distinct([for r in var.inbound_from_other_tags : join("/", [r.protocol, r.port_range, r.tag])])) == length(var.inbound_from_other_tags)
  102. error_message = "Each (protocol, port_range, tag) may appear only once."
  103. }
  104. }
  105. variable "outbound_to_other_tags" {
  106. type = list(object({
  107. protocol = string
  108. port_range = string
  109. tag = string
  110. }))
  111. default = []
  112. description = "Outbound rules that reach only droplets carrying the given OTHER tag (one rule per entry, destination_tags = [tag], never an address). Additive to outbound_to_tag_rules. Empty means none."
  113. validation {
  114. condition = alltrue([
  115. for r in var.outbound_to_other_tags :
  116. contains(["tcp", "udp"], r.protocol) && try(can(regex("^[1-9][0-9]{0,4}(-[1-9][0-9]{0,4})?$", r.port_range)) && tonumber(split("-", r.port_range)[0]) <= tonumber(reverse(split("-", r.port_range))[0]) && tonumber(reverse(split("-", r.port_range))[0]) <= 65535, false)
  117. ])
  118. error_message = "Each rule needs protocol tcp or udp and a port_range of a port or range between 1 and 65535 in ascending order (443, 30000-31000)."
  119. }
  120. validation {
  121. condition = alltrue([for r in var.outbound_to_other_tags : can(regex("^[A-Za-z0-9:_-]{1,255}$", r.tag))])
  122. error_message = "Each tag must be 1 to 255 characters: letters, digits, colon, hyphen or underscore (the DigitalOcean tag shape)."
  123. }
  124. validation {
  125. condition = length(distinct([for r in var.outbound_to_other_tags : join("/", [r.protocol, r.port_range, r.tag])])) == length(var.outbound_to_other_tags)
  126. error_message = "Each (protocol, port_range, tag) may appear only once."
  127. }
  128. }
  129. variable "resource_key" {
  130. type = string
  131. default = ""
  132. description = "Key used in resource NAMES in place of region. Empty means region, so every name is the same as before this setting existed. Immutable on a live stack: changing it changes names, causes widespread plan changes and may replace resources, including through tags or user_data. Never derived from site_name."
  133. validation {
  134. condition = var.resource_key == "" || can(regex("^[a-z][a-z0-9-]{1,23}$", var.resource_key))
  135. error_message = "resource_key must be empty (meaning region) or 2 to 24 characters: a lowercase letter, then lowercase letters, digits or hyphens."
  136. }
  137. }