fix(svc-swarm-node): drop outside traffic to non-public routing-mesh ports

In swarm mode `docker stack deploy` ignores the host IP of a short port
mapping, so every `127.0.0.1:<port>:<port>` that container_ports renders for
a `ports.local` entry is published by the ingress routing mesh on every
node, via docker-proxy on 0.0.0.0 and [::] plus DNAT in nat PREROUTING.
Databases, LDAP and application backends answered from outside the cluster.

svc-swarm-node now loads an nftables table through svc-net-firewall. Its
prerouting filter chain (priority mangle, ahead of Docker's dstnat) drops
packets addressed to the node for any ingress-published port that no
application declares under `ports.public`. Loopback, docker0,
docker_gwbridge, the user bridges and the swarm nodes' own addresses stay
admitted. Host-side provisioning over 127.0.0.1, containers that reach a
host port through host-gateway, and node-to-node consumers such as the
registry-cache mirror keep working.

Both sets are read live on the manager (`docker service inspect` for
`.Endpoint.Ports`, `docker node inspect` for `.Status.Addr`), so ephemeral
ingress ports and nodes outside `--limit` are covered. The table is loaded
on a node before it joins, reloaded on every node after the join, and
reloaded after each stack deploy by the sys-svc-compose swarm handler.

Refs #1089

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
kevinveenbirkenbachandClaude Opus 5.5 committed 2026-10-21 20:56:29 +02:00
1 parent c2bbe814da
commit 788eabffbf
12 files changed
+400 -2

No files matched your search

@@ -0,0 +1,129 @@
import ipaddress
import unittest
from ansible.errors import AnsibleFilterError
from plugins.filter.swarm_ingress_guard import (
FilterModule,
swarm_ingress_guarded,
swarm_ingress_published,
swarm_node_addresses,
swarm_public_ports,
)
class TestSwarmIngressPublished(unittest.TestCase):
def test_keeps_ingress_entries_only(self):
lines = [
'[{"Protocol":"tcp","TargetPort":5432,"PublishedPort":5432,"PublishMode":"ingress"}]',
'[{"Protocol":"tcp","TargetPort":80,"PublishedPort":80,"PublishMode":"host"}]',
"null",
(
'[{"Protocol":"udp","TargetPort":3478,"PublishedPort":3478,"PublishMode":"ingress"},'
'{"Protocol":"tcp","TargetPort":5000,"PublishedPort":30001,"PublishMode":"ingress"}]'
),
]
self.assertEqual(
swarm_ingress_published(lines),
[
{"protocol": "tcp", "port": 5432},
{"protocol": "tcp", "port": 30001},
{"protocol": "udp", "port": 3478},
],
)
def test_a_port_two_services_publish_appears_once(self):
line = '[{"Protocol":"tcp","TargetPort":1,"PublishedPort":8080,"PublishMode":"ingress"}]'
self.assertEqual(
swarm_ingress_published([line, line]),
[{"protocol": "tcp", "port": 8080}],
)
def test_no_output_means_no_published_port(self):
self.assertEqual(swarm_ingress_published([]), [])
def test_refuses_a_line_that_is_not_json(self):
with self.assertRaises(AnsibleFilterError):
swarm_ingress_published(["Error: no such service"])
def test_refuses_an_unknown_protocol(self):
line = '[{"Protocol":"sctp","TargetPort":1,"PublishedPort":9,"PublishMode":"ingress"}]'
with self.assertRaises(AnsibleFilterError):
swarm_ingress_published([line])
class TestSwarmPublicPorts(unittest.TestCase):
def test_collects_single_ports_and_expands_ranges(self):
applications = {
"web-app-gitea": {
"services": {"gitea": {"ports": {"public": {"ssh": 2201}}}}
},
"web-svc-coturn": {
"services": {
"coturn": {
"ports": {
"public": {
"stun_turn": 3478,
"relay": {"start": 49160, "end": 49162},
}
}
}
}
},
"web-app-moodle": {
"services": {"moodle": {"ports": {"local": {"http": 8033}}}}
},
"svc-empty": {"services": None},
}
self.assertEqual(
swarm_public_ports(applications),
[2201, 3478, 49160, 49161, 49162],
)
class TestSwarmIngressGuarded(unittest.TestCase):
def test_drops_the_ports_declared_public(self):
published = [
{"protocol": "tcp", "port": 2201},
{"protocol": "tcp", "port": 5432},
{"protocol": "udp", "port": 3478},
{"protocol": "tcp", "port": 30001},
]
self.assertEqual(
swarm_ingress_guarded(published, [2201, 3478]),
[{"protocol": "tcp", "port": 5432}, {"protocol": "tcp", "port": 30001}],
)
class TestSwarmNodeAddresses(unittest.TestCase):
def test_splits_by_family_and_drops_the_unspecified_address(self):
unspecified = str(ipaddress.IPv4Address(0))
lines = ["10.0.0.7", unspecified, "fd00::2", "10.0.0.3", "", "10.0.0.7"]
self.assertEqual(
swarm_node_addresses(lines),
{"ipv4": ["10.0.0.3", "10.0.0.7"], "ipv6": ["fd00::2"]},
)
def test_no_output_means_no_peer(self):
self.assertEqual(swarm_node_addresses([]), {"ipv4": [], "ipv6": []})
def test_refuses_a_line_that_is_not_an_address(self):
with self.assertRaises(AnsibleFilterError):
swarm_node_addresses(["Error: This node is not a swarm manager."])
class TestFilterModule(unittest.TestCase):
def test_registers_the_four_filters(self):
self.assertEqual(
set(FilterModule().filters()),
{
"swarm_ingress_published",
"swarm_public_ports",
"swarm_ingress_guarded",
"swarm_node_addresses",
},
)
if __name__ == "__main__":
unittest.main()