main.tf 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # SPDX-License-Identifier: Apache-2.0
  2. # The public edge host (DMZ). One immutable droplet with no SSH, no Twingate
  3. # connector and no admin path, a reserved IP that survives a rebuild, and a
  4. # cloud-init that hardens the machine. Every change is a pull request, a plan
  5. # and a rebuild. Design: docs/superpowers/specs/2026-10-19-edge-dmz-design.md.
  6. # Unapplied: no stack calls this module yet; each edge service gets its own stack,
  7. # built and approved later. The module installs no service.
  8. locals {
  9. # Names use resource_key when it is set and region otherwise.
  10. name_key = var.resource_key != "" ? var.resource_key : var.region
  11. # One nftables accept per public port, and one per restricted port that has at
  12. # least one source. A restricted port with no source renders nothing, so it is
  13. # closed here exactly as it is in the Cloud Firewall.
  14. public_rules = [for r in var.public_ports : "${r.protocol} dport ${r.port_range} accept"]
  15. restricted_rules = [
  16. for r in var.restricted_ports :
  17. "${r.protocol} dport ${r.port_range} ip saddr { ${join(", ", r.sources)} } accept"
  18. if length(r.sources) > 0
  19. ]
  20. # Already indented for the cloud-init block scalar, each line ending in a newline.
  21. host_rules = join("", [for l in concat(local.public_rules, local.restricted_rules) : " ${l}\n"])
  22. slots = jsondecode(file("${path.module}/reboot-slots.json")).slots
  23. reboot_time = coalesce(var.reboot_time, local.slots[var.service])
  24. # One Doppler config per edge service, nothing shared between services (the
  25. # telemetry pattern: a config of its own and one short-lived, read-only token
  26. # for it). Only the NAME is computed here; no token is rendered or fetched yet.
  27. doppler_config = "cah-edge-${var.service}"
  28. host_rule_descriptors = sort(concat(
  29. [for r in var.public_ports : "${r.protocol}/${r.port_range}/any"],
  30. [for r in var.restricted_ports : "${r.protocol}/${r.port_range}/restricted" if length(r.sources) > 0],
  31. ))
  32. }
  33. # The reserved IP is its own resource, assigned by a second one, so a rebuilt
  34. # droplet keeps the address people and firewall rules know. Unassigned it costs
  35. # money (design section 5.1); the cost gate treats it as a priced type.
  36. resource "digitalocean_reserved_ip" "edge" {
  37. region = var.region
  38. }
  39. resource "digitalocean_reserved_ip_assignment" "edge" {
  40. ip_address = digitalocean_reserved_ip.edge.ip_address
  41. droplet_id = digitalocean_droplet.edge.id
  42. }
  43. # Attach the droplet and its reserved IP to the existing project. This module is
  44. # only reached through a service stack's switch, so this is behind it too.
  45. resource "digitalocean_project_resources" "edge" {
  46. project = var.project_id
  47. resources = [digitalocean_droplet.edge.urn, digitalocean_reserved_ip.edge.urn]
  48. }
  49. # No ssh_keys on purpose: there is no admin path. The cloud-init locks the root
  50. # password and stops sshd; the Droplet Console is not part of this design.
  51. #
  52. # Trivy DIG-0004 flags a droplet with no SSH key. It is accepted here: the
  53. # design (section 4.2) removes the admin path, so a key would only add one.
  54. # Every change is a pull request, a plan and a rebuild, and a rebuild is the
  55. # only recovery. Scoped to this resource and rule only.
  56. #trivy:ignore:AVD-DIG-0004
  57. resource "digitalocean_droplet" "edge" {
  58. name = "${var.layer}-${local.name_key}-${var.service}"
  59. region = var.region
  60. size = var.size
  61. image = "ubuntu-24-04-x64"
  62. vpc_uuid = var.vpc_id
  63. tags = var.tags
  64. ipv6 = false
  65. backups = false
  66. monitoring = true
  67. user_data = templatefile("${path.module}/cloud-init.yaml.tftpl", {
  68. timezone = var.timezone
  69. reboot_time = local.reboot_time
  70. host_rules = local.host_rules
  71. })
  72. }