cloud-init.yaml.tftpl 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #cloud-config
  2. # SPDX-License-Identifier: Apache-2.0
  3. # Hardened baseline for the public edge host. There is no admin path: no SSH
  4. # key, no admin user, sshd stopped and masked, root password locked. Changing
  5. # this file changes user_data, which forces a droplet rebuild; a rebuild is the
  6. # only recovery. UNAPPLIED and untested on a real machine.
  7. #
  8. # No secret-shaped value is rendered here. A bootstrap token for fetching
  9. # runtime credentials is NOT part of this file yet (design section 3.5, risk 6).
  10. disable_root: true
  11. timezone: ${timezone}
  12. ssh_pwauth: false
  13. package_update: true
  14. package_upgrade: true
  15. # OS packages only, patched by unattended-upgrades. No service is installed here:
  16. # each service is its own stack and brings its own verified, exactly pinned install.
  17. packages:
  18. - unattended-upgrades
  19. - chrony
  20. - nftables
  21. write_files:
  22. - path: /etc/apt/apt.conf.d/20auto-upgrades
  23. permissions: "0644"
  24. content: |
  25. APT::Periodic::Update-Package-Lists "1";
  26. APT::Periodic::Unattended-Upgrade "1";
  27. - path: /etc/apt/apt.conf.d/52cah-unattended-upgrades
  28. permissions: "0644"
  29. content: |
  30. #clear Unattended-Upgrade::Allowed-Origins;
  31. Unattended-Upgrade::Allowed-Origins {
  32. "$${distro_id}:$${distro_codename}-security";
  33. "$${distro_id}ESMApps:$${distro_codename}-apps-security";
  34. "$${distro_id}ESM:$${distro_codename}-infra-security";
  35. };
  36. Unattended-Upgrade::Remove-Unused-Dependencies "true";
  37. Unattended-Upgrade::Automatic-Reboot "true";
  38. Unattended-Upgrade::Automatic-Reboot-WithUsers "true";
  39. Unattended-Upgrade::Automatic-Reboot-Time "${reboot_time}";
  40. - path: /etc/sysctl.d/99-cah-edge-hardening.conf
  41. permissions: "0644"
  42. content: |
  43. net.ipv4.conf.all.rp_filter = 1
  44. net.ipv4.conf.default.rp_filter = 1
  45. net.ipv4.conf.all.accept_redirects = 0
  46. net.ipv4.conf.default.accept_redirects = 0
  47. net.ipv4.conf.all.send_redirects = 0
  48. net.ipv4.conf.default.send_redirects = 0
  49. net.ipv4.ip_forward = 0
  50. net.ipv4.tcp_syncookies = 1
  51. net.ipv6.conf.all.disable_ipv6 = 1
  52. net.ipv6.conf.default.disable_ipv6 = 1
  53. # Lock the droplet metadata endpoint (it serves this very user-data) to root
  54. # and to the DigitalOcean monitoring agent's user. Same rule as the connectors'
  55. # metadata lock, in its own nftables table.
  56. - path: /usr/local/sbin/cah-metadata-lock
  57. permissions: "0750"
  58. owner: root:root
  59. content: |
  60. #!/usr/bin/env bash
  61. set -euo pipefail
  62. allowed="0"
  63. if agent_uid="$(id -u do-agent 2>/dev/null)"; then
  64. allowed="0, $agent_uid"
  65. fi
  66. nft -f - <<EOF
  67. table inet cah_metadata
  68. delete table inet cah_metadata
  69. table inet cah_metadata {
  70. chain output {
  71. type filter hook output priority 0; policy accept;
  72. ip daddr 169.254.169.254 meta skuid != { $allowed } reject with icmp type admin-prohibited
  73. }
  74. }
  75. EOF
  76. echo "cah-metadata-lock: metadata endpoint allowed for uids: $allowed"
  77. # A default-drop host firewall, the second layer behind the Cloud Firewall. It
  78. # lives in its own table (inet cah_host), so it never flushes any other rule.
  79. # Only input is filtered. The accepted ports are exactly the ones the Cloud
  80. # Firewall opens, rendered from the same list. There is no SSH rule.
  81. - path: /usr/local/sbin/cah-host-firewall
  82. permissions: "0750"
  83. owner: root:root
  84. content: |
  85. #!/usr/bin/env bash
  86. set -euo pipefail
  87. read -r -d '' ruleset <<EOF || true
  88. table inet cah_host
  89. delete table inet cah_host
  90. table inet cah_host {
  91. chain input {
  92. type filter hook input priority 0; policy drop;
  93. iifname "lo" accept
  94. ct state established,related accept
  95. ct state invalid drop
  96. icmp type { echo-request, destination-unreachable, time-exceeded } accept
  97. icmpv6 type { destination-unreachable, packet-too-big, time-exceeded, echo-request, nd-router-advert, nd-neighbor-solicit, nd-neighbor-advert } accept
  98. udp sport 67 udp dport 68 accept
  99. ${host_rules} }
  100. }
  101. EOF
  102. # Refuse a ruleset that does not parse, before anything changes.
  103. if ! printf '%s\n' "$ruleset" | nft -c -f -; then
  104. echo "cah-host-firewall: refusing to apply: the ruleset does not parse" >&2
  105. exit 1
  106. fi
  107. printf '%s\n' "$ruleset" | nft -f -
  108. echo "cah-host-firewall: loaded"
  109. # nftables.service flushes the whole ruleset on start, restart and reload, so
  110. # these units are ordered after it and follow its restarts (soft, no Requires=).
  111. - path: /etc/systemd/system/cah-metadata-lock.service
  112. permissions: "0644"
  113. content: |
  114. [Unit]
  115. Description=Restrict the droplet metadata endpoint to root and do-agent
  116. After=cloud-final.service network-online.target
  117. After=nftables.service
  118. PartOf=nftables.service
  119. ReloadPropagatedFrom=nftables.service
  120. Wants=network-online.target
  121. [Service]
  122. Type=oneshot
  123. RemainAfterExit=yes
  124. ExecStart=/usr/local/sbin/cah-metadata-lock
  125. ExecReload=/usr/local/sbin/cah-metadata-lock
  126. [Install]
  127. WantedBy=multi-user.target
  128. - path: /etc/systemd/system/cah-host-firewall.service
  129. permissions: "0644"
  130. content: |
  131. [Unit]
  132. Description=Default-drop host firewall (inet cah_host)
  133. After=cloud-final.service network-online.target
  134. After=nftables.service
  135. PartOf=nftables.service
  136. ReloadPropagatedFrom=nftables.service
  137. Wants=network-online.target
  138. [Service]
  139. Type=oneshot
  140. RemainAfterExit=yes
  141. ExecStart=/usr/local/sbin/cah-host-firewall
  142. ExecReload=/usr/local/sbin/cah-host-firewall
  143. [Install]
  144. WantedBy=multi-user.target
  145. runcmd:
  146. - [sysctl, --system]
  147. # No admin path: stop and mask sshd and lock the root password. The Droplet
  148. # Console is not relied on (design section 4.2).
  149. - [systemctl, disable, --now, ssh.service, ssh.socket]
  150. - [systemctl, mask, ssh.service, ssh.socket]
  151. - [passwd, -l, root]
  152. - [systemctl, enable, --now, chrony]
  153. - [systemctl, daemon-reload]
  154. # --no-block: these units wait for cloud-final, which runs this very command.
  155. - [systemctl, enable, --now, --no-block, cah-metadata-lock.service]
  156. - [systemctl, enable, --now, --no-block, cah-host-firewall.service]