Skip to content

WireGuard mesh between hosts, with routes to each host's bridge subnet - #96

Merged
wmantly merged 2 commits into
mainfrom
feat/wireguard-mesh
Nov 5, 2026
Merged

wmantly merged 2 commits into
mainfrom
feat/wireguard-mesh

Conversation

@wmantly

@wmantly wmantly commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Closes #84. Part of #43.

What changed

  • Config. fleet.yml gets mesh: with enabled, interface (default wg0), listen_port (default 51820) and subnet. Each host gets hosts.<name>.mesh with public_key, and optionally address and endpoint. A tunnel address is mesh.address if set; otherwise it is the subnet's (i+1)th address for the i-th host in sorted name order, so every daemon derives the same addresses. When the mesh is enabled, fleet.yml is refused if:

    • an address is outside the subnet, or two hosts share one;
    • the subnet overlaps a bridge subnet, or is too small for the hosts;
    • a key, interface, port or endpoint is malformed.

    A disabled mesh: is not checked (pkg/config/mesh.go).

  • Plan and apply (pkg/engine/mesh.go). FleetPlan gets a mesh part, and each daemon plans its own host's side:

    • mesh-key: generates the key on the host.
    • mesh-interface / mesh-peer: writes /etc/wireguard/wg0.conf. Each peer's AllowedIPs is that peer's tunnel /32 plus that peer's bridge subnet.
    • mesh-route: the bridge routes, which wg-quick creates from AllowedIPs.
    • mesh-restart: enables and starts wg-quick@wg0, and restarts it only when the config changed, a route is missing, or the unit is down.
    • mesh-forward: net.ipv4.ip_forward=1, persisted in /etc/sysctl.d/60-native-ops-mesh.conf.
    • mesh-firewall: the ufw rules.

    Apply reads the host again and plans again with the same function. If the result differs from the approved plan, it refuses and says to plan again. A converged host plans nothing, and a second apply changes nothing.

  • Key exchange. The private key is generated on the host by a script fed on stdin (wg genkey into /etc/wireguard/wg0.key, mode 0600) and never leaves it:

    • the config file has no PrivateKey line (PostUp = wg set %i private-key …);
    • the plan only checks that the key file exists, and reads the public half from wg0.pub.

    The steps:

    1. The first deploy generates the key. The job log prints the public key.
    2. The plan then blocks with this host's mesh public key is X; add it to fleet.yml as hosts.<name>.mesh.public_key, and the key is also in the plan JSON (mesh.public_key). The plan also blocks on a peer's missing key, a missing endpoint, or a key that differs from the host's. A plan never replaces a host's key.
    3. Commit the key, then deploy again.
  • Read-only. The plan reads the host through one fixed probe script (pkg/remote/meshprobe.go). remote.ReadOnly lets it through only when both the command and the stdin match it byte for byte; every other script is still refused.

  • Firewall (A service's declared forwards: publishes a port, but the host firewall is not part of the plan #75). This is the smallest correct step. When ufw is active, the daemon manages only rules with comment 'native-ops mesh':

    • allow <port>/udp;
    • input on wg0 from each peer's tunnel address to this host's;
    • routed traffic on wg0 from each peer's tunnel address and bridge subnet to this host's bridge subnet.

    It removes mesh rules that are no longer needed and never touches other rules. When ufw is inactive, the plan notes that no rules were written.

  • Docs. A "Mesh between hosts" section in docs/daemon.md covers the key exchange, how bridge IPs become reachable, and instance migrate over the tunnel address. The README's fleet.yml reference and its migrate note are updated, and so are the roadmap and the migrate bullet that pointed at WireGuard mesh between hosts, with routes for each host's bridge subnet #84.

Mesh off is unchanged. With mesh: absent or enabled: false, nothing is read or planned. TestMeshOffChangesNothing checks that the text, JSON and hash match the existing golden from main. The hash only takes a new shape when a plan has a mesh part.

Design choices to review

  • wg-quick rather than systemd-networkd.
    • The engine already manages host config with idempotent shell commands (hostBootstrapCommands), and the bootstrap does not use networkd.
    • Enabling networkd on an ifupdown or cloud-init host is a real side effect on the host: with its ManageForeignRoutes default it can drop routes it doesn't own.
    • wg-quick creates the bridge routes straight from AllowedIPs.
    • With PostUp setting the key, the config file has no secret in it, so the plan can compare the whole file.
    • The cost: a config change restarts the unit, and the tunnel drops briefly, but only when something changed.
  • Missing keys are a plan blocker, not a load-time error. A fleet.yml that refused a host without a key would make the first deploy, the one that generates the keys, impossible. Malformed keys, addresses and subnets are still refused when the file loads.
  • Key read-back goes through the plan output (blocker text and JSON) plus the apply log line. There is no new endpoint or CLI, because that is the smallest option and keeps everything in the plan.

Not done here (needs #75 or follow-ups)

  • The bootstrap's own ufw rules (route allow out on <bridge> lets any forwarded traffic reach the bridge), and the rest of the host firewall, are still outside the plan (A service's declared forwards: publishes a port, but the host firewall is not part of the plan #75).
  • The Incus remote for migrate (incus remote add <peer> https://<tunnel>:8443, plus core.https_address) is documented, not automated.
  • To check on a real host: that ufw show added prints the mesh rules in exactly the form the plan compares against (ufw route allow in on wg0 out on incusbr0 from A to B comment 'native-ops mesh'). If it does not, the plan would keep showing firewall changes on a converged host. The ufw commands themselves are idempotent, so nothing would break.

Checks

  • gofmt -l .: prints nothing
  • go mod tidy && git diff --exit-code go.mod go.sum: clean
  • go vet ./...: passes
  • go test -race ./...: all packages pass
  • node --check pkg/server/ui/static/js/app.js: passes
  • The CLI is unchanged, so the README CLI Usage test passes as part of go test.

No self-merge.

🤖 Generated with Claude Code

wmantly and others added 2 commits November 5, 2026 00:52
#84)

fleet.yml's `mesh:` (enabled, interface, listen_port, subnet) and each host's
`hosts.<name>.mesh` (public_key, optional address and endpoint). Each daemon
converges its own side through the normal plan and apply: a wg-quick@wg0 unit
whose config has no private key (PostUp sets it from the key file), one peer per
other host with AllowedIPs = its tunnel address and its bridge subnet (so
wg-quick routes every bridge over the mesh), IP forwarding, and ufw rules marked
'native-ops mesh' for the WireGuard port and mesh traffic between tunnel
addresses and bridges.

The private key is generated on the host by a script fed on stdin and never
leaves it. The first apply generates it; the plan then blocks with "this host's
mesh public key is X; add it to hosts.<name>.mesh.public_key" until fleet.yml
has every host's matching key. The plan reads the host through one fixed probe
script that the read-only executor lets through byte for byte.

With mesh absent or disabled nothing is read or planned, and plans, their JSON
and their hashes are unchanged (golden test).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wmantly
wmantly merged commit 1e0c858 into main Nov 5, 2026
1 check passed
@wmantly
wmantly deleted the feat/wireguard-mesh branch November 5, 2026 17:07
wmantly added a commit that referenced this pull request Nov 5, 2026
… reconcile

Only the docs conflicted: README's node-02 example now shows both its
incus_remote and its mesh key, and the daemon.md roadmap lists the mesh,
DNS and reconcile per host, host-keyed backups and the migrate job as done.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WireGuard mesh between hosts, with routes for each host's bridge subnet

1 participant