Watch
0
0
Fork
You've already forked hyperhive
0

swarm-nats: the pod's raw rule lets through the interfaces the firewall opens the client port on

The hostPort's raw-table drop for the queue's client port let through only the host's loopback, the bridge and the pods' interfaces, so a hive on another host, reaching the queue over wg0 where the firewall's own rule opens the port, timed out. The rule now also returns for every interface whose networking.firewall.interfaces entry opens the port. semi-spread dumps the queue's network state when the hive cannot reach it, and asserts the port answers over wg0 and not on the public address.
This commit is contained in:
atlas 2026-11-26 23:32:02 +01:00 • committed by maia
commit 4bb6c573ae
5 changed files with 98 additions and 8 deletions

View file

@ -227,8 +227,10 @@ strategy, and the removed container stops before the pod can start.
`deploy.nats.openFirewall` and the bridge's port list write, never see what it
rewrites. The module therefore adds rules to the raw table, which the kernel
reads before the DNAT: a packet for the node's address and the client port is
dropped unless it arrives on the host's loopback, the hive bridge or a pod
interface, or unless `deploy.nats.openFirewall` is on. The container's port
dropped unless it arrives on the host's loopback, the hive bridge, a pod
interface or an interface that `networking.firewall.interfaces` opens the port on
(the semi-spread example opens it on `wg0`), or unless `deploy.nats.openFirewall`
is on. The container's port
has that reach. The `hostPort` sends the bridge's agents to the cluster, which
a host with the egress intercept on refuses, so the module also adds the port
to `egress.publishedPorts`

View file

@ -9,8 +9,9 @@
#
# The client port is dropped for a packet addressed to the host unless it
# arrives on one of `trusted` (the host's loopback, the hive bridge and the
# pods' interfaces), or `openClient` is set, which is
# `deploy.nats.openFirewall`.
# pods' interfaces), on an interface whose `networking.firewall.interfaces`
# entry opens the port (`firewallInterfaces`, the option's value), or
# `openClient` is set, which is `deploy.nats.openFirewall`.
#
# Pure function — NOT a NixOS module. Returns the text for the firewall's
# `extraCommands`.
@ -19,18 +20,24 @@
chain,
clientPort,
trusted,
firewallInterfaces,
openClient,
}:
let
rule = "iptables -t raw -A ${chain}";
local = "-m addrtype --dst-type LOCAL -j DROP";
opens =
i:
lib.elem clientPort i.allowedTCPPorts
|| lib.any (r: r.from <= clientPort && clientPort <= r.to) i.allowedTCPPortRanges;
opened = lib.attrNames (lib.filterAttrs (_: opens) firewallInterfaces);
in
''
iptables -t raw -N ${chain} 2>/dev/null || iptables -t raw -F ${chain}
${lib.optionalString (!openClient) ''
${lib.concatMapStrings (i: ''
${rule} -p tcp --dport ${toString clientPort} -i ${i} -j RETURN
'') trusted}
'') (lib.unique (trusted ++ opened))}
${rule} -p tcp --dport ${toString clientPort} ${local}
''}
iptables -t raw -D PREROUTING -j ${chain} 2>/dev/null || true

View file

@ -1308,8 +1308,9 @@ in
address the other hives reach.
The pod's published port has the same reach: a rule in the firewall's raw
table drops it from every interface but the host's loopback, the bridge
and the pods' unless this is set.
table drops it from every interface but the host's loopback, the bridge,
the pods' and those that {option}`networking.firewall.interfaces` opens
the port on, unless this is set.
'';
};
@ -1585,6 +1586,7 @@ in
networkCfg.bridgeName
]
++ deployCfg.k8s.cniInterfaces;
firewallInterfaces = config.networking.firewall.interfaces;
openClient = deployCfg.nats.openFirewall;
}
);

View file

@ -1741,12 +1741,49 @@ let
"br0"
"cni0"
];
firewallInterfaces = { };
};
closed = rules (args // { openClient = false; });
open = rules (args // { openClient = true; });
# An interface the firewall opens the port on, by the port or by a
# range, is let through; one that opens other ports is not.
viaInterfaces = rules (
args
// {
openClient = false;
firewallInterfaces = {
wg0 = {
allowedTCPPorts = [ 4222 ];
allowedTCPPortRanges = [ ];
};
wg1 = {
allowedTCPPorts = [ ];
allowedTCPPortRanges = [
{
from = 4000;
to = 4300;
}
];
};
eth1 = {
allowedTCPPorts = [ 80 ];
allowedTCPPortRanges = [
{
from = 5000;
to = 5100;
}
];
};
};
}
);
has = hasInfix;
in
has "-p tcp --dport 4222 -i br0 -j RETURN" closed
has "-p tcp --dport 4222 -i wg0 -j RETURN" viaInterfaces
&& has "-p tcp --dport 4222 -i wg1 -j RETURN" viaInterfaces
&& !(has "-i eth1" viaInterfaces)
&& !(has "-i wg0" closed)
&& has "-p tcp --dport 4222 -i br0 -j RETURN" closed
&& has "-p tcp --dport 4222 -i cni0 -j RETURN" closed
&& has "-p tcp --dport 4222 -m addrtype --dst-type LOCAL -j DROP" closed
# Open, the client port has no rule. The exporter's port, behind a

View file

@ -204,6 +204,32 @@ let
diagnose(m, unit, container)
raise
QUEUE_NAME = "${swarm.nats.domain}"
QUEUE_PORT = ${toString swarm.nats.port}
def dials_queue(address):
return f"timeout 5 bash -c 'exec 3<>/dev/tcp/{address}/{QUEUE_PORT}'"
def diagnose_queue():
# Where a dial to the queue dies: what the swarm machine listens on and drops
# in its raw table (with packet counters), the cluster's view of the queue's
# Service, and what each machine resolves and routes the queue's name to.
for m, command in [
(swarm, "iptables -t raw -S"),
(swarm, "iptables -t raw -L nixos-fw-nats -v -n"),
(swarm, f"iptables -S | grep -E '{QUEUE_PORT}|wg0'"),
(swarm, "ss -ltn"),
(swarm, "k3s kubectl get pods,svc,endpoints,networkpolicy -A -o wide"),
(hive, f"getent ahostsv4 {QUEUE_NAME}"),
(heavyduty, f"getent ahostsv4 {QUEUE_NAME}"),
(hive, f"ip route get $(getent ahostsv4 {QUEUE_NAME} | awk 'NR==1 {{print $1}}')"),
(hive, dials_queue(QUEUE_NAME) + "; echo dial exit $?"),
(heavyduty, dials_queue(QUEUE_NAME) + "; echo dial exit $?"),
(swarm, dials_queue(QUEUE_NAME) + "; echo dial exit $?"),
]:
print(f"--- {m.name}: {command}")
print(m.execute(command)[1])
HIVE_UNITS = ["hive-priv.socket", "hive-priv.service", "hive-c0re"]
def hive_fault():
@ -284,6 +310,7 @@ let
if fault:
break
time.sleep(10)
diagnose_queue()
diagnose(hive, HIVE_UNITS)
diagnose(swarm, ["swarm-controller"])
diagnose(heavyduty, "swarm-builder", "swarm-builder")
@ -524,6 +551,17 @@ let
# The public names are not on heavyduty or the hive either.
assert http(outside, NAMES["forge"], "/api/healthz", resolve=LAN["heavyduty"]) == "000"
with subtest("the queue's port answers on the swarm machine and over wg0, and not on its public address"):
# The control first: the pod listens, reached from the machine that runs it.
try:
swarm.wait_until_succeeds(dials_queue(QUEUE_NAME), timeout=900)
for m in (hive, heavyduty):
m.wait_until_succeeds(dials_queue(QUEUE_NAME), timeout=120)
except Exception:
diagnose_queue()
raise
assert outside.execute(dials_queue(LAN["swarm"]))[0] != 0, "the queue answers on the swarm machine's public address"
with subtest("the forge holds hyperhive/hyperhive at the fixture's commit"):
settle(
swarm,
@ -793,6 +831,10 @@ pkgs.testers.runNixOSTest (
bao.machine = "stub";
dashboardDomains.${hiveName} = "dashboard.stub";
attic.domain = "attic.stub";
nats = {
domain = "nats.stub";
port = 4222;
};
};
nameOf.stub = "stub.stub";
internalNames.stub = "stub";