mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-28 22:06:31 +00:00
fix(tor_ports): forward only the ports a role names under ports.onion
The public-port sweep never forwarded anything. It read tor.enabled from meta/services.yml on disk, where 104 of 105 roles declare it as the Jinja string "{{ 'svc-net-tor' in group_names }}", which _as_bool answers False for. Running HEAD's collect_public_ports the way HEAD's run() calls it, against the real roles directory with every role deployed, returns []. Only svc-net-tor reads True on disk, and it declares no public ports. The node onion therefore carried port 80 and port 22 and nothing else.
Reviving the sweep by reading the merged view instead would have been worse than leaving it dead: it publishes 16 ports across 7 roles, and declared is not published. openldap's ldaps 636 sits next to network.public false, Mailu's implicit-TLS ports are dropped by container_ports whenever TLS is off (which an onion deployment always is), and svc-net-tor's own SOCKS port would become an open proxy. So the source and the selection rule change together: read the variant-merged applications view, and forward only the categories a service names under ports.onion, resolving the number from ports.local when declared there and ports.public otherwise, which is the precedence container_ports publishes by.
collect_public_ports had no consumer outside this module and its own test. UDP_ONLY_CATEGORIES becomes public so the accompanying lint imports the list rather than restating it, and gains stun_turn and stun_turn_tls: those speak TCP, but the allocation TURN hands back is a UDP relay address an onion client can never receive on.
No role declares ports.onion yet, so the rendered HiddenServicePort set is unchanged by this commit alone.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
9a68f0eaaf
commit
d7efffc31a
2 files changed
+223
-65
No files matched your search
@@ -9,7 +9,7 @@ from ansible.errors import AnsibleError
|
||||
from plugins.lookup.tor_ports import (
|
||||
LookupModule,
|
||||
collect_exposed_ports,
|
||||
collect_public_ports,
|
||||
collect_onion_ports,
|
||||
)
|
||||
from utils.cache import _reset_cache_for_tests
|
||||
from utils.cache.yaml import dump_yaml_str
|
||||
@@ -33,7 +33,15 @@ class TestTorPortsLookup(unittest.TestCase):
|
||||
_write_services(
|
||||
self.roles_dir,
|
||||
"web-app-gitea",
|
||||
{"gitea": {"ports": {"public": {"ssh": 2201}, "local": {"http": 8002}}}},
|
||||
{
|
||||
"gitea": {
|
||||
"ports": {
|
||||
"public": {"ssh": 2201},
|
||||
"local": {"http": 8002},
|
||||
"onion": {"ssh": True},
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
_write_services(
|
||||
self.roles_dir,
|
||||
@@ -68,40 +76,159 @@ class TestTorPortsLookup(unittest.TestCase):
|
||||
_reset_cache_for_tests()
|
||||
self._tmpdir.cleanup()
|
||||
|
||||
def test_collects_public_single_ints_sorted(self) -> None:
|
||||
ports = collect_public_ports(
|
||||
["web-app-gitea", "svc-db-openldap"], self.roles_dir
|
||||
@staticmethod
|
||||
def _apps(**roles: dict) -> dict:
|
||||
"""The variant-merged applications view for the given roles.
|
||||
|
||||
Every entity keeps its own ``ports``; the top-level ``tor:`` block of
|
||||
``meta/services.yml`` merges in as one more entity alongside them.
|
||||
"""
|
||||
return {role: {"services": dict(entities)} for role, entities in roles.items()}
|
||||
|
||||
def test_only_named_categories_are_forwarded_sorted(self) -> None:
|
||||
apps = self._apps(
|
||||
**{
|
||||
"web-app-gitea": {
|
||||
"gitea": {
|
||||
"ports": {"public": {"ssh": 2201}, "onion": {"ssh": True}}
|
||||
}
|
||||
},
|
||||
"web-app-mailu": {
|
||||
"mailu": {
|
||||
"ports": {
|
||||
"public": {"smtp": 25, "smtps": 465, "imap": 143},
|
||||
"onion": {"smtp": True},
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
)
|
||||
self.assertEqual(ports, [636, 2201])
|
||||
|
||||
def test_relay_ranges_excluded(self) -> None:
|
||||
ports = collect_public_ports(["web-svc-coturn"], self.roles_dir)
|
||||
self.assertEqual(ports, [3481, 5351])
|
||||
ports = collect_onion_ports(apps, ["web-app-gitea", "web-app-mailu"])
|
||||
|
||||
def test_media_category_excluded(self) -> None:
|
||||
_write_services(
|
||||
self.roles_dir,
|
||||
"web-app-jitsi",
|
||||
{"jitsi": {"ports": {"public": {"media": 10000}, "local": {"http": 8084}}}},
|
||||
self.assertEqual(ports, [25, 2201])
|
||||
|
||||
def test_local_wins_over_public_for_the_same_category(self) -> None:
|
||||
"""``container_ports`` publishes the local port when the category has
|
||||
one, so the forward must target that and not the public alias."""
|
||||
apps = self._apps(
|
||||
**{
|
||||
"web-app-x": {
|
||||
"x": {
|
||||
"ports": {
|
||||
"local": {"http": 8080},
|
||||
"public": {"http": 80},
|
||||
"onion": {"http": True},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
self.assertEqual(collect_public_ports(["web-app-jitsi"], self.roles_dir), [])
|
||||
|
||||
def test_roles_without_public_ports_yield_nothing(self) -> None:
|
||||
self.assertEqual(collect_public_ports(["web-app-hugo"], self.roles_dir), [])
|
||||
self.assertEqual(collect_onion_ports(apps, ["web-app-x"]), [8080])
|
||||
|
||||
def test_tor_disabled_role_excluded(self) -> None:
|
||||
_write_services(
|
||||
self.roles_dir,
|
||||
"web-app-optout",
|
||||
{
|
||||
"optout": {"ports": {"public": {"ssh": 2299}}},
|
||||
"tor": {"enabled": False},
|
||||
},
|
||||
def test_a_falsy_or_unrendered_flag_forwards_nothing(self) -> None:
|
||||
"""The input must be the rendered view: template text left in the flag
|
||||
is not quietly read as enabled."""
|
||||
apps = self._apps(
|
||||
**{
|
||||
"web-app-a": {
|
||||
"a": {"ports": {"public": {"ssh": 22}, "onion": {"ssh": False}}}
|
||||
},
|
||||
"web-app-b": {
|
||||
"b": {
|
||||
"ports": {
|
||||
"public": {"ssh": 23},
|
||||
"onion": {"ssh": "{{ 'svc-net-tor' in group_names }}"},
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
)
|
||||
self.assertEqual(collect_public_ports(["web-app-optout"], self.roles_dir), [])
|
||||
|
||||
def test_unknown_role_ignored(self) -> None:
|
||||
self.assertEqual(collect_public_ports(["web-app-nope"], self.roles_dir), [])
|
||||
self.assertEqual(collect_onion_ports(apps, ["web-app-a", "web-app-b"]), [])
|
||||
|
||||
def test_udp_only_categories_are_never_forwarded(self) -> None:
|
||||
apps = self._apps(
|
||||
**{
|
||||
"web-svc-coturn": {
|
||||
"coturn": {
|
||||
"ports": {
|
||||
"public": {
|
||||
"media": 10000,
|
||||
"stun_turn": 3481,
|
||||
"stun_turn_tls": 5351,
|
||||
"ssh": 2299,
|
||||
"relay": {"start": 20000, "end": 39999},
|
||||
},
|
||||
"onion": {
|
||||
"media": True,
|
||||
"relay": True,
|
||||
"stun_turn": True,
|
||||
"stun_turn_tls": True,
|
||||
"ssh": True,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
self.assertEqual(collect_onion_ports(apps, ["web-svc-coturn"]), [2299])
|
||||
|
||||
def test_a_category_with_no_declared_port_is_skipped(self) -> None:
|
||||
apps = self._apps(
|
||||
**{
|
||||
"web-app-x": {
|
||||
"x": {"ports": {"public": {"ssh": 2201}, "onion": {"typo": True}}}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
self.assertEqual(collect_onion_ports(apps, ["web-app-x"]), [])
|
||||
|
||||
def test_a_role_without_an_onion_block_forwards_nothing(self) -> None:
|
||||
apps = self._apps(
|
||||
**{"web-app-hugo": {"hugo": {"ports": {"local": {"http": 8008}}}}}
|
||||
)
|
||||
|
||||
self.assertEqual(collect_onion_ports(apps, ["web-app-hugo"]), [])
|
||||
|
||||
def test_the_port_flag_is_the_gate_not_the_tor_flag(self) -> None:
|
||||
"""``services.tor.enabled`` is the dependency edge onto svc-net-tor, not
|
||||
the port forward; ``ports.onion`` alone decides what gets a
|
||||
HiddenServicePort."""
|
||||
apps = self._apps(
|
||||
**{
|
||||
"web-app-gitea": {
|
||||
"tor": {"enabled": False},
|
||||
"gitea": {
|
||||
"ports": {"public": {"ssh": 2201}, "onion": {"ssh": True}}
|
||||
},
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
self.assertEqual(collect_onion_ports(apps, ["web-app-gitea"]), [2201])
|
||||
|
||||
def test_role_outside_the_deploy_is_ignored(self) -> None:
|
||||
apps = self._apps(
|
||||
**{
|
||||
"web-app-gitea": {
|
||||
"gitea": {
|
||||
"ports": {"public": {"ssh": 2201}, "onion": {"ssh": True}}
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
self.assertEqual(collect_onion_ports(apps, ["web-app-nope"]), [])
|
||||
|
||||
def test_collect_onion_ports_ignores_non_mappings(self) -> None:
|
||||
self.assertEqual(collect_onion_ports({}, ["a"]), [])
|
||||
self.assertEqual(collect_onion_ports(None, ["a"]), [])
|
||||
apps = {"a": {"services": {"s": {"ports": {"onion": True}}}}}
|
||||
self.assertEqual(collect_onion_ports(apps, ["a"]), [])
|
||||
|
||||
def test_collect_exposed_ports_gated_and_deployed(self) -> None:
|
||||
apps = {
|
||||
@@ -151,6 +278,14 @@ class TestTorPortsLookup(unittest.TestCase):
|
||||
)[0]
|
||||
self.assertEqual(result, [{"onion_port": 2201, "target": "127.0.0.1:2201"}])
|
||||
|
||||
def test_run_forwards_nothing_without_an_onion_opt_in(self) -> None:
|
||||
result = self.lookup.run(
|
||||
[],
|
||||
variables={"group_names": ["svc-db-openldap", "web-svc-coturn"]},
|
||||
roles_dir=str(self.roles_dir),
|
||||
)[0]
|
||||
self.assertEqual(result, [])
|
||||
|
||||
def test_empty_group_names(self) -> None:
|
||||
result = self.lookup.run([], variables={}, roles_dir=str(self.roles_dir))[0]
|
||||
self.assertEqual(result, [])
|
||||
|
||||
Reference in new issue
Block a user