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:
parent
e29a8e71f3
commit
e7ccc41652
6 changed files with 121 additions and 9 deletions
|
|
@ -228,7 +228,11 @@ holds the container back until the pod has gone.
|
|||
read 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
|
||||
has that reach.
|
||||
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`
|
||||
([the egress chain](../networking/network.md#agent-bridge-egress-log)): agents
|
||||
reach the pod through the published port and no other way.
|
||||
- **The exporter's port.** The exporter's port has no `hostPort`: a ClusterIP
|
||||
Service, `swarm-nats-metrics`, fronts it, and the gateway's `/metrics`
|
||||
location on the host dials the Service's name through nginx's `resolver` (the
|
||||
|
|
|
|||
|
|
@ -757,6 +757,18 @@ also refuses the flow.
|
|||
`reject` doesn't stop an agent from reaching them when they run on another
|
||||
host. The forge and matrix gateways need no line: the hive's resolver answers
|
||||
their names with the bridge address, which the forward chain never sees.
|
||||
- **Published services.** After the `agent_allow` rule the chain accepts TCP
|
||||
that the host's own NAT redirected (`ct status dnat`) when the flow's
|
||||
original destination (`ct original`) is the bridge's address at a port in
|
||||
`services.hyperhive.deploy.hive-controller.egress.publishedPorts`, in both
|
||||
modes, so such a flow leaves no count and no line. A module that publishes a
|
||||
port adds it: the queue in the cluster adds its client port, because the
|
||||
pod's `hostPort` rewrites the destination to the pod and the packet then
|
||||
leaves the bridge for the cluster. An agent dials the queue's name, which the
|
||||
hive's resolver answers with the bridge's address. A flow to the pod's own
|
||||
address has no redirect, and one to the node's address has another original
|
||||
destination, so both stay refused. The set is empty, and the chain has no
|
||||
such rule, while no module publishes a port.
|
||||
- **What `reject` refuses.** Everything from the bridge to the uplink that the
|
||||
chain didn't exempt or allow, whether a flow is new or not: TCP on any port
|
||||
but the 80 and 443 that squid takes, UDP (DNS to an outside resolver, QUIC
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
|
|
|||
|
|
@ -2025,6 +2025,39 @@ let
|
|||
&& has "-t raw -A nixos-fw-nats -p tcp --dport 4222 -m addrtype --dst-type LOCAL -j DROP" onCluster.networking.firewall.extraCommands
|
||||
&& has "-t raw -F nixos-fw-nats" onCluster.networking.firewall.extraStopCommands;
|
||||
}
|
||||
{
|
||||
# The host with the queue on it is the control: it publishes no port, and
|
||||
# its chain has no rule of the kind.
|
||||
name = "the egress forward chain accepts the flows the host publishes for the agents, from the bridge's address at the queue's port, and none on a host without the queue in the cluster";
|
||||
ok =
|
||||
let
|
||||
rules =
|
||||
c:
|
||||
lib.filter (l: l != "") (
|
||||
map lib.trim (
|
||||
lib.splitString "\n" (
|
||||
lib.last (
|
||||
lib.splitString "chain forward" (
|
||||
lib.head (lib.splitString "table bridge hive_egress {" c.services.hyperhive.network.egress.ruleset)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
idx = c: pat: lib.lists.findFirstIndex (lib.hasInfix pat) 1000 (rules c);
|
||||
br = onCluster.services.hyperhive.network.bridgeName;
|
||||
published = ''iifname "${br}" oifname != "${br}" meta nfproto ipv4 meta l4proto tcp ct status dnat ct original ip daddr 10.42.0.1 ct original proto-dst { 4222 } accept'';
|
||||
in
|
||||
onCluster.services.hyperhive.deploy.hive-controller.egress.publishedPorts == [ 4222 ]
|
||||
&& withNode.services.hyperhive.deploy.hive-controller.egress.publishedPorts == [ ]
|
||||
&& lib.elem published (rules onCluster)
|
||||
&& !(lib.any (lib.hasInfix "ct original") (rules withNode))
|
||||
# After the named SSH connections and before the counter and the log:
|
||||
# a published flow is neither counted nor logged.
|
||||
&& idx onCluster "@agent_allow accept" < idx onCluster "ct original"
|
||||
&& idx onCluster "ct original" < idx onCluster "counter"
|
||||
&& idx onCluster "@direct_mac accept" < idx onCluster "ct original";
|
||||
}
|
||||
{
|
||||
# The file names the runtime and the host units of every service and no
|
||||
# state: nothing is copied. The units of the poller (timer and service)
|
||||
|
|
|
|||
|
|
@ -12,8 +12,9 @@
|
|||
# - the pod is Ready by its exec probe on the server's monitor, holds the leaf
|
||||
# and the callout config as init containers, and publishes the client port
|
||||
# on every address, with no `hostPort` on the exporter;
|
||||
# - a client on the hive's bridge reaches the queue by name through the
|
||||
# published port, and the responder in the pod admits the router's token;
|
||||
# - with the egress intercept on, a client on the hive's bridge reaches the
|
||||
# queue by name through the published port and not by the pod's address or
|
||||
# the node's, and the responder in the pod admits the router's token;
|
||||
# - the port is as closed to a host outside the node as the container's is, and
|
||||
# open with `openFirewall`, where a second node dials it by name and verifies
|
||||
# the name. The exporter answers the host only, by its Service's name;
|
||||
|
|
@ -163,6 +164,12 @@ pkgs.testers.runNixOSTest {
|
|||
deploy.k8s.enable = true;
|
||||
network.enable = true;
|
||||
network.bridgeIp = bridgeIp;
|
||||
# The egress intercept is on, which makes the forward chain refuse an
|
||||
# agent's traffic to anything but the bridge, as on a hive controller host.
|
||||
deploy.hive-controller.egress.intercept = {
|
||||
enable = true;
|
||||
refuseEch = false;
|
||||
};
|
||||
deploy.bao.enable = true;
|
||||
deploy.bao.seal = "shamir";
|
||||
deploy.bao.bootstrapTokenFile = "/var/lib/swarm-bao-bootstrap/grant.token";
|
||||
|
|
@ -475,6 +482,13 @@ pkgs.testers.runNixOSTest {
|
|||
return status == 0, out.strip()
|
||||
|
||||
|
||||
def probe_from_agent() -> tuple[bool, str]:
|
||||
"""`probe` from the netns of the client on the bridge, at the bridge's
|
||||
address, by the name an agent dials."""
|
||||
status, out = machine.execute(f"ip netns exec agent python3 /etc/natstls.py {BRIDGE} {NAME} {SERVICES_ROOT} 2>&1")
|
||||
return status == 0, out.strip()
|
||||
|
||||
|
||||
def reachable(node: QemuMachine, address: str, port: int) -> bool:
|
||||
return node.execute(f"nc -z -w 4 {address} {port}")[0] == 0
|
||||
|
||||
|
|
@ -674,8 +688,15 @@ pkgs.testers.runNixOSTest {
|
|||
# back when the container starts.
|
||||
assert int(machine.succeed(f"find {HOST_STORE} ! -uid {HOST_UID} | wc -l")) > 0
|
||||
|
||||
with subtest("a client on the hive's bridge reaches the pod through the published port, and a host outside the node does not"):
|
||||
with subtest("a client on the hive's bridge reaches the pod through the published port, behind the egress chain that refuses it everything else of the cluster, and a host outside the node does not"):
|
||||
assert machine.execute(f"ip netns exec agent nc -z -w 4 {BRIDGE} {PORT}")[0] == 0
|
||||
assert probe_from_agent()[0], probe_from_agent()
|
||||
# The same pod by its own address, and the queue by the node's address,
|
||||
# are no flow the host published for the agent: both are refused.
|
||||
pod_ip = kube(machine, f"get pod {pod_name} -o jsonpath='{{.status.podIP}}'").strip()
|
||||
assert pod_ip.startswith("10.200."), pod_ip
|
||||
assert machine.execute(f"ip netns exec agent nc -z -w 4 {pod_ip} {PORT}")[0] != 0
|
||||
assert machine.execute(f"ip netns exec agent nc -z -w 4 {NODE} {PORT}")[0] != 0
|
||||
assert machine.execute(f"ip netns exec agent nc -z -w 4 {BRIDGE} {METRICS}")[0] != 0
|
||||
assert reachable(outside, NODE, CONTROL)
|
||||
assert not reachable(outside, NODE, PORT), "the pod's port is open to a host outside the node"
|
||||
|
|
|
|||
Loading…
Reference in a new issue