variables.tf 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. variable "layer" {
  2. type = string
  3. }
  4. variable "region" {
  5. type = string
  6. }
  7. variable "remote_network_id" {
  8. type = string
  9. description = "Twingate remote network (site) shared by every connector of the stack."
  10. }
  11. variable "instance" {
  12. type = string
  13. description = "One lowercase letter or digit that tells this connector from its siblings (a, b)."
  14. validation {
  15. condition = can(regex("^[a-z0-9]$", var.instance))
  16. error_message = "instance must be a single lowercase letter or digit."
  17. }
  18. }
  19. variable "twingate_network" {
  20. type = string
  21. sensitive = true
  22. description = "Bare Twingate network subdomain (acme for acme.twingate.com). Sensitive because the tenant slug identifies the tenant."
  23. validation {
  24. condition = can(regex("^[a-z0-9-]+$", var.twingate_network))
  25. error_message = "twingate_network must be the bare Twingate network subdomain (lowercase letters, digits, hyphens), not a URL and with no quotes."
  26. }
  27. }
  28. variable "admin_ssh_public_key" {
  29. type = string
  30. description = "Public key for the cah-admin user: '<algorithm> <key>' with an optional trailing comment of one word of safe characters. The comment is dropped before the key reaches cloud-init, so the machine sees only '<algorithm> <key>'. Key-only access; no password exists."
  31. validation {
  32. condition = can(regex("^(ssh-ed25519|ssh-rsa|ecdsa-sha2-nistp(256|384|521)) [A-Za-z0-9+/]+=*( [A-Za-z0-9@._+:=/-]{1,64})?$", var.admin_ssh_public_key))
  33. error_message = "admin_ssh_public_key must be '<ssh-ed25519|ssh-rsa|ecdsa-sha2-nistp256|384|521> <base64 key>' with at most one trailing comment word of letters, digits and @ . _ + : = / - (no spaces or line breaks)."
  34. }
  35. }
  36. variable "reboot_time" {
  37. type = string
  38. description = "HH:MM (in `timezone`, the droplet's local time; America/New_York by default) when unattended-upgrades may reboot this droplet. Give each connector a different time so they never reboot together."
  39. validation {
  40. condition = can(regex("^([01][0-9]|2[0-3]):[0-5][0-9]$", var.reboot_time))
  41. error_message = "reboot_time must be HH:MM in 24-hour time, for example 04:00."
  42. }
  43. }
  44. variable "resource_key" {
  45. type = string
  46. default = ""
  47. 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."
  48. validation {
  49. condition = var.resource_key == "" || can(regex("^[a-z][a-z0-9-]{1,23}$", var.resource_key))
  50. error_message = "resource_key must be empty (meaning region) or 2 to 24 characters: a lowercase letter, then lowercase letters, digits or hyphens."
  51. }
  52. }
  53. variable "timezone" {
  54. type = string
  55. default = "America/New_York"
  56. description = "IANA time zone name of the machine (UTC or Area/Location, for example America/New_York), also the zone reboot_time and the weekly update are read in. Default America/New_York. Rendered into cloud-init, so changing it changes user_data and replaces the droplet."
  57. validation {
  58. condition = can(regex("^(UTC|[A-Za-z_]+(/[A-Za-z0-9_+-]+){1,2})$", var.timezone)) && contains(split("\n", file(join("/", [path.module, "timezones.txt"]))), var.timezone)
  59. error_message = "timezone must be a name in modules/twingate-bootstrap/timezones.txt (the IANA zones, for example America/New_York or UTC): no spaces or line breaks, and the zone must exist."
  60. }
  61. }
  62. variable "connector_telemetry" {
  63. type = object({
  64. status_updates_enabled = optional(bool)
  65. log_level = optional(number)
  66. connection_logs = optional(bool, false)
  67. metrics_port = optional(number)
  68. labels = optional(map(string), {})
  69. })
  70. default = {}
  71. nullable = false
  72. description = "Connector telemetry. The default ({}) is the live state: it adds no line to the connector config, so user_data is byte-identical and nothing is replaced. status_updates_enabled is a Twingate provider setting; unset (the default) it is not sent at all, so the connector keeps what it has today (Twingate's default, true), and set to true or false it is sent. It never touches user_data. The other four add lines to /etc/twingate/connector.conf and therefore change user_data, which REPLACES the machine: log_level (TWINGATE_LOG_LEVEL, 3 4 5 or 7; unset leaves Twingate's default of 3), connection_logs (true adds TWINGATE_LOG_ANALYTICS=v3, real-time connection logs to the journal), metrics_port (TWINGATE_METRICS_PORT, Prometheus metrics on that port, 1024 to 65535 and not 9100) and labels (a map; KEY=value becomes TWINGATE_LABEL_KEY=value, shown in the Twingate Admin Console, so never put a secret in a label). No firewall port is opened here: that is a separate change."
  73. validation {
  74. condition = var.connector_telemetry.log_level == null ? true : contains([3, 4, 5, 7], var.connector_telemetry.log_level)
  75. error_message = "connector_telemetry.log_level must be 3, 4, 5 or 7 (Twingate's documented levels), or unset."
  76. }
  77. validation {
  78. condition = var.connector_telemetry.metrics_port == null ? true : (
  79. var.connector_telemetry.metrics_port == floor(var.connector_telemetry.metrics_port) &&
  80. var.connector_telemetry.metrics_port >= 1024 &&
  81. var.connector_telemetry.metrics_port <= 65535 &&
  82. var.connector_telemetry.metrics_port != 9100
  83. )
  84. error_message = "connector_telemetry.metrics_port must be a whole number from 1024 to 65535, and not 9100 (kept for the node exporter), or unset."
  85. }
  86. validation {
  87. condition = alltrue([for k, v in var.connector_telemetry.labels : can(regex("^[A-Z][A-Z0-9_]{0,31}$", k))])
  88. error_message = "Each connector_telemetry.labels key must be 1 to 32 characters: an uppercase letter, then uppercase letters, digits or underscores (it becomes TWINGATE_LABEL_<KEY>)."
  89. }
  90. validation {
  91. condition = alltrue([for k, v in var.connector_telemetry.labels : can(regex("^[A-Za-z0-9._-]{1,64}$", v))])
  92. error_message = "Each connector_telemetry.labels value must be 1 to 64 characters of letters, digits, dot, underscore or hyphen (no spaces, quotes or line breaks, because it is written into the connector config)."
  93. }
  94. validation {
  95. condition = length(var.connector_telemetry.labels) <= 10
  96. error_message = "connector_telemetry.labels may hold at most 10 entries."
  97. }
  98. }
  99. variable "metadata_lock" {
  100. type = bool
  101. default = false
  102. nullable = false
  103. description = "Lock the droplet metadata endpoint (169.254.169.254, which serves this machine's user-data, connector tokens included) to root and to the do-agent user (DigitalOcean's monitoring agent reads it; the user is allowed only when it exists, so AWS needs no special case). Default false is the live state: user_data is byte-identical (golden hashes in the tests) and nothing is replaced. true adds an nftables table, a script, a systemd unit and the nftables package to user_data, which REPLACES the machine. Not tested on a real machine yet: roll out one connector at a time."
  104. }
  105. variable "host_firewall" {
  106. type = bool
  107. default = false
  108. nullable = false
  109. description = "A default-drop host firewall (nftables table inet cah_host, input chain only) as a second layer behind the cloud firewall. Inbound is allowed only for loopback, established traffic, the ICMP and DHCP the network card needs, TCP 22 from host_firewall_ssh_sources and, when connector_telemetry.metrics_port is set, that one port from the same sources. Outbound and forwarding are untouched. The script checks the ruleset first, applies it, then verifies sshd still listens and the ssh rule is loaded, and deletes the table if not. Default false is the live state: user_data is byte-identical (golden hashes in the tests) and nothing is replaced. true adds a script, a systemd unit and the nftables package to user_data, which REPLACES the machine. Provider-neutral (DigitalOcean and AWS). Not tested on a real machine yet: roll out one connector at a time."
  110. }
  111. variable "host_firewall_ssh_sources" {
  112. type = list(string)
  113. default = ["10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"]
  114. nullable = false
  115. description = "IPv4 CIDRs allowed to reach TCP 22 (and the telemetry metrics port) when host_firewall is true. Default the three private RFC 1918 blocks: the cloud firewall already limits SSH to the connector tag, and this only narrows it to private space, because this module does not know the VPC CIDR. Narrow it to the VPC range if you know it. IPv4 only (the machines have no public IPv6). No leading zeros in octets or prefix and a prefix of 1 to 32; a /0 is refused on purpose because it would defeat the narrowing (the cloud firewall is still the outer layer). Host bits and overlapping entries are allowed. Ignored while host_firewall is false."
  116. validation {
  117. condition = length(var.host_firewall_ssh_sources) >= 1 && length(distinct(var.host_firewall_ssh_sources)) == length(var.host_firewall_ssh_sources) && alltrue([
  118. for c in var.host_firewall_ssh_sources : can(regex("^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])/([1-9]|[12][0-9]|3[0-2])$", c)) && can(cidrnetmask(c))
  119. ])
  120. error_message = "host_firewall_ssh_sources must hold at least one IPv4 CIDR (for example 10.0.0.0/8), each at most once, written without leading zeros (nft rejects 10.0.0.0/08) and with a prefix of 1 to 32. A /0 (0.0.0.0/0) is refused on purpose: it would defeat the narrowing to private space, and the cloud firewall stays the outer layer either way."
  121. }
  122. }
  123. variable "node_exporter" {
  124. type = object({
  125. enabled = optional(bool, false)
  126. port = optional(number, 9100)
  127. })
  128. default = {}
  129. nullable = false
  130. description = "Opt-in host metrics: installs prometheus-node-exporter from the distro archive and binds it to the machine's private IPv4 address only (--web.listen-address=<private address>:<port>; a boot-time script picks the first RFC 1918 address and refuses to start the exporter when it finds none, so it never falls back to a wildcard address). The default ({}, enabled false) is the live state: user_data is byte-identical (golden hashes in the tests) and nothing is replaced. enabled = true adds a package, a script and a systemd drop-in to user_data, which REPLACES the machine (roll out one connector at a time). port is 1024 to 65535 (default 9100), is checked even while disabled, and must differ from connector_telemetry.metrics_port. Opens no cloud firewall port: that is the access stack's separate, tag-sourced rule; with host_firewall the port is allowed from host_firewall_ssh_sources. NOT applied and NOT tested on a real machine yet."
  131. validation {
  132. condition = var.node_exporter.port == floor(var.node_exporter.port) && var.node_exporter.port >= 1024 && var.node_exporter.port <= 65535
  133. error_message = "node_exporter.port must be a whole number from 1024 to 65535."
  134. }
  135. }
  136. variable "log_shipping" {
  137. type = object({
  138. enabled = optional(bool, false)
  139. manager_address = optional(string, "")
  140. manager_port = optional(number, 1514)
  141. enroll_port = optional(number, 1515)
  142. agent_name = optional(string, "")
  143. agent_group = optional(string, "")
  144. apt_key_fingerprint = optional(string, "")
  145. agent_version = optional(string, "")
  146. })
  147. default = {}
  148. nullable = false
  149. sensitive = true
  150. description = "Opt-in log shipping: installs the Wazuh agent from Wazuh's apt repository (the repository key is checked against apt_key_fingerprint before it is trusted), adds auditd with a short watch-only rule set (identity and sudo files, /etc/twingate; no execve), collects journald for the twingate-connector, ssh and sudo units plus /var/log/auth.log, and enrolls the agent at first boot with a secret pulled from Doppler (see log_shipping_bootstrap_token). The default ({}, enabled false) is the live state: user_data is byte-identical (golden hashes in the tests) and nothing is replaced. enabled = true adds packages, scripts and files to user_data, which REPLACES the machine (roll out one connector at a time). Required when enabled: manager_address (a host name or IPv4 literal; the manager as reached through Twingate), agent_name (<site>-connector-<instance>, for example site-tor1-connector-a, ending in -connector- and this machine's instance), and apt_key_fingerprint (the 40 hex digits of Wazuh's repository signing key; deliberately no default: read it from Wazuh's documentation and compare it yourself). agent_group is optional and must already exist on the manager. agent_version pins the agent package (for example 4.9.2-1); empty takes the newest 4.x, and an agent newer than the manager is unsupported by Wazuh, so pin it to the manager's version. manager_port (default 1514) and enroll_port (default 1515) are checked even while disabled and must agree with the logging_servers ports in stack 20. Sensitive because the manager address identifies the owner's network. No secret is an input here: the enrollment password, any Twingate service key and any Doppler secret value are never rendered into user_data. The Twingate headless client the agent needs to reach the manager is NOT part of this change. NOT applied and NOT tested on a real machine; the 1 GB droplet's memory headroom with the agent is UNMEASURED."
  151. validation {
  152. condition = alltrue([for p in [var.log_shipping.manager_port, var.log_shipping.enroll_port] : p == floor(p) && p >= 1 && p <= 65535])
  153. error_message = "log_shipping.manager_port and log_shipping.enroll_port must be whole numbers from 1 to 65535."
  154. }
  155. validation {
  156. condition = !var.log_shipping.enabled || can(regex("^[A-Za-z0-9]([A-Za-z0-9.-]{0,251}[A-Za-z0-9])?$", var.log_shipping.manager_address))
  157. error_message = "log_shipping.manager_address is required when enabled: a host name or IPv4 address of letters, digits, dots and hyphens (no scheme, port, path or spaces)."
  158. }
  159. validation {
  160. condition = !var.log_shipping.enabled || can(regex("^[a-z][a-z0-9-]{0,55}-connector-[a-z0-9]$", var.log_shipping.agent_name))
  161. error_message = "log_shipping.agent_name is required when enabled and must look like <site>-connector-<instance> (lowercase letters, digits and hyphens, for example site-tor1-connector-a)."
  162. }
  163. validation {
  164. condition = can(regex("^([A-Za-z0-9_][A-Za-z0-9_.-]{0,63})?$", var.log_shipping.agent_group))
  165. error_message = "log_shipping.agent_group must be empty or 1 to 64 letters, digits, dot, underscore or hyphen, not starting with a dot or hyphen."
  166. }
  167. validation {
  168. condition = !var.log_shipping.enabled || can(regex("^[0-9A-F]{40}$", var.log_shipping.apt_key_fingerprint))
  169. error_message = "log_shipping.apt_key_fingerprint is required when enabled: the 40 uppercase hex digits of Wazuh's repository signing key, no spaces. There is no default on purpose: read it from Wazuh's documentation and compare it before you set it."
  170. }
  171. validation {
  172. condition = can(regex("^([0-9]+[.][0-9]+[.][0-9]+-[0-9]+)?$", var.log_shipping.agent_version))
  173. error_message = "log_shipping.agent_version must be empty or a package version such as 4.9.2-1."
  174. }
  175. }
  176. variable "log_shipping_bootstrap_token" {
  177. type = string
  178. default = ""
  179. nullable = false
  180. sensitive = true
  181. description = "The one secret-shaped value that reaches user_data when log_shipping is enabled: a short-lived, read-only Doppler service token for the config cah-<resource_key>-hostboot, created by the maintainer out of band (not by OpenTofu). It is written to a root-only file that the first-boot script deletes before using it, and it expires on its own (the shortest lifetime Doppler allows is UNVERIFIED; revoke it after the first boot if that is long). Required when log_shipping.enabled, ignored (and never rendered) otherwise. It is still in the state, the provider API, the metadata endpoint and cloud-init's own copy of user_data until it expires or is revoked. Letters, digits, dot, underscore and hyphen only, 16 to 512 characters."
  182. validation {
  183. condition = var.log_shipping_bootstrap_token == "" || can(regex("^[A-Za-z0-9._-]{16,512}$", var.log_shipping_bootstrap_token))
  184. error_message = "log_shipping_bootstrap_token must be empty or 16 to 512 characters of letters, digits, dot, underscore and hyphen."
  185. }
  186. # The one placeholder rule of scripts/secret_rule.py, as an RE2 pattern
  187. # (tests/test_secret_rule.py pins it to that file).
  188. validation {
  189. condition = !can(regex("(?i)(^(your[_-]|changeme|change-me|replace[_-]with|xxx|dp[.]st[.]your|(todo|tbd|admin|password|secret|test)$)|^<.*>$|example|placeholder|changeme)", var.log_shipping_bootstrap_token))
  190. error_message = "log_shipping_bootstrap_token looks like a placeholder (your_*, changeme*, replace-with*, xxx*, <...>, example, placeholder). Set the real short-lived Doppler token, or leave it empty when log_shipping is off."
  191. }
  192. }