Watch
0
0
Fork
You've already forked hyperhive
0

egress: the forward chain accepts the flows the host publishes for the agents

On a host with the egress intercept on, the forward chain rejected an agent's traffic to the queue once the queue ran as a pod: the pod's hostPort rewrites the destination to the pod, and the packet then leaves the bridge for the cluster. The chain now accepts TCP that the host's NAT redirected whose original destination is the bridge's address at a published port, which the queue adds when it runs in the cluster. Everything else from the bridge to the cluster stays refused. The nats switch test runs with the intercept on.
This commit is contained in:
atlas 2026-11-23 18:40:04 +01:00 • committed by maia
commit 0d857b8ae1
2 changed files with 121 additions and 9 deletions

View file

@ -173,6 +173,19 @@ let
}
'';
# The flows the host publishes for the agents: TCP that the host's own NAT
# redirected (`ct status dnat`), whose original destination is the bridge
# address, where the agents' resolver answers the service's name, at a
# published port. The redirect has run by the forward hook, so the packet
# leaves for the pod, and the conntrack entry still holds the destination the
# agent dialed (`ct original`). A pod dialed by its own address has no
# redirect, and a redirected flow to another address or port is not listed.
publishedRules =
lib.optional (egressCfg.publishedPorts != [ ])
"${fromBridge} meta nfproto ipv4 meta l4proto tcp ct status dnat ct original ip daddr ${cfg.bridgeIp} ct original proto-dst { ${
lib.concatMapStringsSep ", " toString egressCfg.publishedPorts
} } accept";
# Priority `filter - 10` runs before the NixOS firewall's forward handling,
# which sits at `filter` on both backends (`FORWARD` of the iptables backend,
# the `forward` chain of `nixos-fw`). An `accept` here ends this chain only,
@ -182,9 +195,9 @@ let
# both modes, so a `log` line names what `reject` would refuse. The TCP
# connections in `agent_allow` (an agent's `ssh_connections` and, for every
# agent, the swarm's store, queue, logs and metrics endpoints, all resolved
# by hive-c0re) are accepted next, with the same effect. Traffic to
# the host's own addresses (squid, dnsmasq, the gateway) is delivered
# locally and never reaches the forward hook.
# by hive-c0re) are accepted next, and the published flows after them, with
# the same effect. Traffic to the host's own addresses (squid, dnsmasq, the
# gateway) is delivered locally and never reaches the forward hook.
forwardChain = ''
set log_v4 { type ipv4_addr; flags dynamic; timeout 1m; size 4096; }
set log_v6 { type ipv6_addr; flags dynamic; timeout 1m; size 4096; }
@ -192,7 +205,12 @@ let
chain forward {
type filter hook forward priority filter - 10; policy accept;
${fromBridge} ether saddr @direct_mac accept
${fromBridge} meta nfproto ipv4 meta l4proto tcp ether saddr . ip daddr . tcp dport @agent_allow accept
${lib.concatStringsSep "\n " (
[
"${fromBridge} meta nfproto ipv4 meta l4proto tcp ether saddr . ip daddr . tcp dport @agent_allow accept"
]
++ publishedRules
)}
${fromBridge} counter
${lib.concatStringsSep "\n " (
logRules "v4" "ip" ++ logRules "v6" "ip6" ++ unmeteredRules ++ finalRules
@ -358,7 +376,8 @@ in
`reject` the traffic of the MACs in the `direct_mac` set (the unrestricted
agents and the service containers) is accepted and never logged, and so
is TCP from an agent to an address and port in the `agent_allow` set
(`connectionsFile`).
(`connectionsFile`), and a flow to a service the host publishes
(`publishedPorts`).
- `accept`: nothing; the table has no forward chain.
- `log`: each new flow is logged with the prefix `hive-egress-would-drop `,
@ -375,6 +394,25 @@ in
'';
};
options.services.hyperhive.deploy.hive-controller.egress.publishedPorts = lib.mkOption {
type = lib.types.listOf lib.types.port;
default = [ ];
description = ''
TCP ports of services the host publishes with a NAT rule, which the
bridge's agents dial at the bridge's address: a pod's `hostPort`, or the
port of a `LoadBalancer` Service that the cluster's service load
balancer binds on the node. The rule redirects the packet to the pod, so
the forward chain sees traffic from the bridge to the cluster and would
refuse it. In `log` and `reject` mode the chain accepts a flow of TCP
that the host's NAT redirected (`ct status dnat`) when the address and
port the agent dialed (`ct original`) are the bridge's address and one of
these ports, right after the `agent_allow` rule. A flow to a pod's own
address has no redirect, and one to another address of the node has
another original destination, so both stay refused. Each module that
publishes a port adds it; the default is none, which adds no rule.
'';
};
options.services.hyperhive.deploy.hive-controller.egress.connectionsFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;

View file

@ -1618,6 +1618,10 @@ in
# The pod's client `hostPort` is a DNAT rule, which the INPUT rules above
# never see: ./lib/swarm-nats-raw-rules.nix gives it the container's reach.
services.hyperhive.network.enable = lib.mkIf onK8s (lib.mkDefault true);
# An agent dials the queue at the bridge's address; the pod's `hostPort` is
# forwarded to the cluster, which the egress forward chain would refuse.
services.hyperhive.deploy.hive-controller.egress.publishedPorts = lib.mkIf onK8s [ cfg.port ];
networking.firewall.extraCommands = lib.mkIf onK8s (
import ./lib/swarm-nats-raw-rules.nix { inherit lib; } {
chain = "nixos-fw-nats";