| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- #cloud-config
- # SPDX-License-Identifier: Apache-2.0
- # Hardened baseline for the public edge host. There is no admin path: no SSH
- # key, no admin user, sshd stopped and masked, root password locked. Changing
- # this file changes user_data, which forces a droplet rebuild; a rebuild is the
- # only recovery. UNAPPLIED and untested on a real machine.
- #
- # No secret-shaped value is rendered here. A bootstrap token for fetching
- # runtime credentials is NOT part of this file yet (design section 3.5, risk 6).
- disable_root: true
- timezone: ${timezone}
- ssh_pwauth: false
- package_update: true
- package_upgrade: true
- # OS packages only, patched by unattended-upgrades. No service is installed here:
- # each service is its own stack and brings its own verified, exactly pinned install.
- packages:
- - unattended-upgrades
- - chrony
- - nftables
- write_files:
- - path: /etc/apt/apt.conf.d/20auto-upgrades
- permissions: "0644"
- content: |
- APT::Periodic::Update-Package-Lists "1";
- APT::Periodic::Unattended-Upgrade "1";
- - path: /etc/apt/apt.conf.d/52cah-unattended-upgrades
- permissions: "0644"
- content: |
- #clear Unattended-Upgrade::Allowed-Origins;
- Unattended-Upgrade::Allowed-Origins {
- "$${distro_id}:$${distro_codename}-security";
- "$${distro_id}ESMApps:$${distro_codename}-apps-security";
- "$${distro_id}ESM:$${distro_codename}-infra-security";
- };
- Unattended-Upgrade::Remove-Unused-Dependencies "true";
- Unattended-Upgrade::Automatic-Reboot "true";
- Unattended-Upgrade::Automatic-Reboot-WithUsers "true";
- Unattended-Upgrade::Automatic-Reboot-Time "${reboot_time}";
- - path: /etc/sysctl.d/99-cah-edge-hardening.conf
- permissions: "0644"
- content: |
- net.ipv4.conf.all.rp_filter = 1
- net.ipv4.conf.default.rp_filter = 1
- net.ipv4.conf.all.accept_redirects = 0
- net.ipv4.conf.default.accept_redirects = 0
- net.ipv4.conf.all.send_redirects = 0
- net.ipv4.conf.default.send_redirects = 0
- net.ipv4.ip_forward = 0
- net.ipv4.tcp_syncookies = 1
- net.ipv6.conf.all.disable_ipv6 = 1
- net.ipv6.conf.default.disable_ipv6 = 1
- # Lock the droplet metadata endpoint (it serves this very user-data) to root
- # and to the DigitalOcean monitoring agent's user. Same rule as the connectors'
- # metadata lock, in its own nftables table.
- - path: /usr/local/sbin/cah-metadata-lock
- permissions: "0750"
- owner: root:root
- content: |
- #!/usr/bin/env bash
- set -euo pipefail
- allowed="0"
- if agent_uid="$(id -u do-agent 2>/dev/null)"; then
- allowed="0, $agent_uid"
- fi
- nft -f - <<EOF
- table inet cah_metadata
- delete table inet cah_metadata
- table inet cah_metadata {
- chain output {
- type filter hook output priority 0; policy accept;
- ip daddr 169.254.169.254 meta skuid != { $allowed } reject with icmp type admin-prohibited
- }
- }
- EOF
- echo "cah-metadata-lock: metadata endpoint allowed for uids: $allowed"
- # A default-drop host firewall, the second layer behind the Cloud Firewall. It
- # lives in its own table (inet cah_host), so it never flushes any other rule.
- # Only input is filtered. The accepted ports are exactly the ones the Cloud
- # Firewall opens, rendered from the same list. There is no SSH rule.
- - path: /usr/local/sbin/cah-host-firewall
- permissions: "0750"
- owner: root:root
- content: |
- #!/usr/bin/env bash
- set -euo pipefail
- read -r -d '' ruleset <<EOF || true
- table inet cah_host
- delete table inet cah_host
- table inet cah_host {
- chain input {
- type filter hook input priority 0; policy drop;
- iifname "lo" accept
- ct state established,related accept
- ct state invalid drop
- icmp type { echo-request, destination-unreachable, time-exceeded } accept
- icmpv6 type { destination-unreachable, packet-too-big, time-exceeded, echo-request, nd-router-advert, nd-neighbor-solicit, nd-neighbor-advert } accept
- udp sport 67 udp dport 68 accept
- ${host_rules} }
- }
- EOF
- # Refuse a ruleset that does not parse, before anything changes.
- if ! printf '%s\n' "$ruleset" | nft -c -f -; then
- echo "cah-host-firewall: refusing to apply: the ruleset does not parse" >&2
- exit 1
- fi
- printf '%s\n' "$ruleset" | nft -f -
- echo "cah-host-firewall: loaded"
- # nftables.service flushes the whole ruleset on start, restart and reload, so
- # these units are ordered after it and follow its restarts (soft, no Requires=).
- - path: /etc/systemd/system/cah-metadata-lock.service
- permissions: "0644"
- content: |
- [Unit]
- Description=Restrict the droplet metadata endpoint to root and do-agent
- After=cloud-final.service network-online.target
- After=nftables.service
- PartOf=nftables.service
- ReloadPropagatedFrom=nftables.service
- Wants=network-online.target
- [Service]
- Type=oneshot
- RemainAfterExit=yes
- ExecStart=/usr/local/sbin/cah-metadata-lock
- ExecReload=/usr/local/sbin/cah-metadata-lock
- [Install]
- WantedBy=multi-user.target
- - path: /etc/systemd/system/cah-host-firewall.service
- permissions: "0644"
- content: |
- [Unit]
- Description=Default-drop host firewall (inet cah_host)
- After=cloud-final.service network-online.target
- After=nftables.service
- PartOf=nftables.service
- ReloadPropagatedFrom=nftables.service
- Wants=network-online.target
- [Service]
- Type=oneshot
- RemainAfterExit=yes
- ExecStart=/usr/local/sbin/cah-host-firewall
- ExecReload=/usr/local/sbin/cah-host-firewall
- [Install]
- WantedBy=multi-user.target
- runcmd:
- - [sysctl, --system]
- # No admin path: stop and mask sshd and lock the root password. The Droplet
- # Console is not relied on (design section 4.2).
- - [systemctl, disable, --now, ssh.service, ssh.socket]
- - [systemctl, mask, ssh.service, ssh.socket]
- - [passwd, -l, root]
- - [systemctl, enable, --now, chrony]
- - [systemctl, daemon-reload]
- # --no-block: these units wait for cloud-final, which runs this very command.
- - [systemctl, enable, --now, --no-block, cah-metadata-lock.service]
- - [systemctl, enable, --now, --no-block, cah-host-firewall.service]
|