feat(wireguard): admit a host's WireGuard listen ports #54

Merged
claudiomaradonna merged 2 commits from feat/wireguard-port into main 2026-11-18 12:14:53 +01:00
Collaborator

Why

fortify.yml loads the inet castrum nftables table on every Linux host,
with an input policy of drop that admits only SSH and the ports of the
service roles switched on. A host that terminates a WireGuard tunnel
listens on a UDP port for its peers' handshakes, and no role opened it:
the first run dropped the handshakes and took the tunnel down, and where
SSH runs over that tunnel, the host was then reachable only from its
console.

Closes #53

What changed

A host can now declare that it listens for WireGuard. Before, no setting
admitted its listen port. After, wireguard_enabled: true in the host's
variables adds a wireguard chain that admits new UDP packets on each
port in wireguard_ports (default [51820], one per interface's
ListenPort) from every source, IPv4 and IPv6.

The new wireguard role has the same shape as kdeconnect. It holds
only a flag and a port file, roles/wireguard/vars/firewall.yml, which
firewall-apply reads like any other service's. The role is in
service_roles and in the role catalogue. firewall-apply itself is
unchanged.

A host that does not set the flag renders the same table as before, so
its next run reloads nothing. The role leaves installing, configuring and
keying WireGuard to the host.

How to verify

Prerequisites: Setup, make setup. Base main
at 7ee3e7f, branch feat/wireguard-port at 13cfd32. From the checkout's
root on each revision, render the table for a host that declares
WireGuard on port 51821 and list what names it:

ANSIBLE_ROLES_PATH="$PWD/roles" .venv/bin/ansible-playbook \
  -i tests/fixtures/local.yml tests/fixtures/firewall_render/loader.yml \
  -e fixture_file=/tmp/castrum.nft \
  -e wireguard_enabled=true -e '{"wireguard_ports": [51821]}' >/dev/null
grep -n wireguard /tmp/castrum.nft
  • Base: grep prints nothing, so the handshake would be dropped.

  • Branch, run as written:

    23:    jump wireguard
    35:  chain wireguard {
    36:    udp dport { 51821 } ct state new accept comment "wireguard"
    

Checks run on 13cfd32:

  • make check passed.
  • tests/gates/check_firewall_render.sh case 6 holds two new checks:
    • the declared-port render. This regression test fails on the base,
      with "the host that declares WireGuard has no jump to a wireguard
      chain".
    • a pin that a host without the flag gets no wireguard rule. It passes
      on both revisions.
  • Case 4 now also renders the shipped port file, on its default 51820.

Untested: make test (molecule's nft -c and live load) did not run,
because the environment had no podman. Expected result: it passes
unchanged. firewall-apply and its scenarios are untouched, and the new
rule has the form kdeconnect's UDP rule already renders.

Considerations

  • The rule is open to every source, by decision. WireGuard answers no
    packet that is not authenticated to the host's key, and its peers are
    often roaming clients outside the inventory. Restricting it to
    from_group peers would also be IPv4-only.
  • A host whose ListenPort is not 51820 must set wireguard_ports.
    Otherwise the wrong port opens and the handshake is still dropped. The
    role README says so.
## Why `fortify.yml` loads the `inet castrum` nftables table on every Linux host, with an input policy of drop that admits only SSH and the ports of the service roles switched on. A host that terminates a WireGuard tunnel listens on a UDP port for its peers' handshakes, and no role opened it: the first run dropped the handshakes and took the tunnel down, and where SSH runs over that tunnel, the host was then reachable only from its console. Closes #53 ## What changed A host can now declare that it listens for WireGuard. Before, no setting admitted its listen port. After, `wireguard_enabled: true` in the host's variables adds a `wireguard` chain that admits new UDP packets on each port in `wireguard_ports` (default `[51820]`, one per interface's `ListenPort`) from every source, IPv4 and IPv6. The new `wireguard` role has the same shape as `kdeconnect`. It holds only a flag and a port file, `roles/wireguard/vars/firewall.yml`, which `firewall-apply` reads like any other service's. The role is in `service_roles` and in the role catalogue. `firewall-apply` itself is unchanged. A host that does not set the flag renders the same table as before, so its next run reloads nothing. The role leaves installing, configuring and keying WireGuard to the host. ## How to verify Prerequisites: [Setup](CONTRIBUTING.md#setup), `make setup`. Base `main` at 7ee3e7f, branch `feat/wireguard-port` at 13cfd32. From the checkout's root on each revision, render the table for a host that declares WireGuard on port 51821 and list what names it: ```bash ANSIBLE_ROLES_PATH="$PWD/roles" .venv/bin/ansible-playbook \ -i tests/fixtures/local.yml tests/fixtures/firewall_render/loader.yml \ -e fixture_file=/tmp/castrum.nft \ -e wireguard_enabled=true -e '{"wireguard_ports": [51821]}' >/dev/null grep -n wireguard /tmp/castrum.nft ``` - Base: `grep` prints nothing, so the handshake would be dropped. - Branch, run as written: ```text 23: jump wireguard 35: chain wireguard { 36: udp dport { 51821 } ct state new accept comment "wireguard" ``` Checks run on 13cfd32: - `make check` passed. - `tests/gates/check_firewall_render.sh` case 6 holds two new checks: - the declared-port render. This regression test fails on the base, with "the host that declares WireGuard has no jump to a wireguard chain". - a pin that a host without the flag gets no wireguard rule. It passes on both revisions. - Case 4 now also renders the shipped port file, on its default 51820. Untested: `make test` (molecule's `nft -c` and live load) did not run, because the environment had no podman. Expected result: it passes unchanged. `firewall-apply` and its scenarios are untouched, and the new rule has the form `kdeconnect`'s UDP rule already renders. ## Considerations - The rule is open to every source, by decision. WireGuard answers no packet that is not authenticated to the host's key, and its peers are often roaming clients outside the inventory. Restricting it to `from_group` peers would also be IPv4-only. - A host whose `ListenPort` is not 51820 must set `wireguard_ports`. Otherwise the wrong port opens and the handshake is still dropped. The role README says so.
A host that declares no WireGuard listen port renders no wireguard
chain, jump or rule; the role that admits one must keep it so.
feat(wireguard): admit a host's WireGuard listen ports
All checks were successful
check / check (pull_request) Successful in 20m16s
check / check (push) Successful in 22m10s
13cfd3229d
A host terminating a WireGuard tunnel had its handshakes dropped by the
castrum table's input policy at the first fortify.yml run, taking the
tunnel down and, where SSH runs over it, the host with it.

A port-only service role, shaped as kdeconnect, opens each port in
wireguard_ports (default 51820) on UDP to every source when
wireguard_enabled is set on the host: WireGuard answers nothing not
authenticated to the host's key, and its peers often roam. Hosts that
do not set the flag render a byte-identical table.

Closes #53
claudiomaradonna deleted branch feat/wireguard-port 2026-11-18 12:15:00 +01:00
Sign in to join this conversation.
No description provided.