mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-12-02 07:47:58 +00:00
feat(network): serve roles on clearnet and onion from one registry
A node runs in NETWORK_MODE clearnet, tor or multi; empty derives multi when svc-net-tor is deployed. meta/networks.yml reachability narrows a role (modes, single_mode, each justified by a network-reachability nocheck), and utils/networks/reachability.py is the one registry every network decision goes through. services.tor keeps only the dependency bond; exclusive and primary are gone and Matrix is single_mode. Every canonical domain gains one sibling per effective network, clearnet first. sys-stk-front-proxy renders one vhost per sibling, and TLS, CSP, injected snippets, redirects, the web health probe and the CSP crawler are decided per vhost. Cross-app URLs follow the network of the vhost being rendered, the SSO issuer stays single, clearnet vhosts of multi roles send Onion-Location, and the shared location forwards X-Forwarded-Host, which lets Wazuh derive its OIDC redirect per request again. The to_onion_url filter is removed; its two consumers render per vhost instead. Measured before commit: on a clearnet node the domain map of all 108 apps and every CSP header are identical to HEAD; on a multi node every role keeps its clearnet domains first and only gains onion siblings, and no CSP source crosses networks except the SSO issuer. Refs: docs/requirements/037-multi-network-serving.md Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
42e66e2080
commit
66c8df6729
73 files changed
+1986
-1135
No files matched your search
+111
-150
@@ -209,147 +209,123 @@ class TestImportableWithoutAnsible(unittest.TestCase):
|
||||
self.assertIn("OK", result.stdout)
|
||||
|
||||
|
||||
class TestOnionDomainInjection(unittest.TestCase):
|
||||
class TestNetworkSiblingInjection(unittest.TestCase):
|
||||
ONION = "abc123def456ghij789klmno000pqrstuvwx111yz222abc333def444gh.onion"
|
||||
|
||||
def _apps(self, **tor):
|
||||
return {
|
||||
"web-app-x": {
|
||||
"services": {
|
||||
"tor": {
|
||||
"enabled": True,
|
||||
"exclusive": False,
|
||||
"primary": False,
|
||||
**tor,
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
def _apps(self, enabled=True, **reachability):
|
||||
config = {"services": {"tor": {"enabled": enabled}}}
|
||||
if reachability:
|
||||
config["networks"] = {"reachability": reachability}
|
||||
return {"web-app-x": config}
|
||||
|
||||
def test_dual_stack_appends_onion(self):
|
||||
merged = {"web-app-x": ["x.infinito.test"]}
|
||||
out = cache_domains._inject_onion_domains(
|
||||
merged, self._apps(), "infinito.test", self.ONION
|
||||
)
|
||||
self.assertEqual(out["web-app-x"], ["x.infinito.test", f"x.{self.ONION}"])
|
||||
def _inject(self, apps, node_mode, *, domains=None, deployed=("web-app-x",)):
|
||||
return cache_domains._inject_network_siblings(
|
||||
{"web-app-x": domains if domains is not None else ["x.infinito.test"]},
|
||||
apps,
|
||||
"infinito.test",
|
||||
self.ONION,
|
||||
node_mode,
|
||||
deployed=list(deployed),
|
||||
)["web-app-x"]
|
||||
|
||||
def test_exclusive_replaces_clearnet(self):
|
||||
merged = {"web-app-x": ["x.infinito.test"]}
|
||||
out = cache_domains._inject_onion_domains(
|
||||
merged, self._apps(exclusive=True), "infinito.test", self.ONION
|
||||
def test_multi_appends_onion_after_clearnet(self):
|
||||
self.assertEqual(
|
||||
self._inject(self._apps(), "multi"),
|
||||
["x.infinito.test", f"x.{self.ONION}"],
|
||||
)
|
||||
self.assertEqual(out["web-app-x"], [f"x.{self.ONION}"])
|
||||
|
||||
def test_primary_puts_onion_first(self):
|
||||
merged = {"web-app-x": ["x.infinito.test"]}
|
||||
out = cache_domains._inject_onion_domains(
|
||||
merged, self._apps(primary=True), "infinito.test", self.ONION
|
||||
)
|
||||
self.assertEqual(out["web-app-x"], [f"x.{self.ONION}", "x.infinito.test"])
|
||||
def test_tor_mode_replaces_clearnet(self):
|
||||
self.assertEqual(self._inject(self._apps(), "tor"), [f"x.{self.ONION}"])
|
||||
|
||||
def test_disabled_app_untouched(self):
|
||||
merged = {"web-app-x": ["x.infinito.test"]}
|
||||
apps = {"web-app-x": {"services": {"tor": {"enabled": False}}}}
|
||||
out = cache_domains._inject_onion_domains(
|
||||
merged, apps, "infinito.test", self.ONION
|
||||
def test_clearnet_mode_is_untouched(self):
|
||||
self.assertEqual(
|
||||
self._inject(self._apps(enabled=False), "clearnet"), ["x.infinito.test"]
|
||||
)
|
||||
self.assertEqual(out["web-app-x"], ["x.infinito.test"])
|
||||
|
||||
def test_exclusive_primary_default_from_provider(self):
|
||||
"""A consumer that omits exclusive/primary inherits the svc-net-tor
|
||||
provider defaults (exclusive: true -> onion only)."""
|
||||
merged = {"web-app-x": ["x.infinito.test"]}
|
||||
apps = {
|
||||
"web-app-x": {"services": {"tor": {"enabled": True, "shared": True}}},
|
||||
"svc-net-tor": {
|
||||
"services": {
|
||||
"tor": {
|
||||
"enabled": True,
|
||||
"shared": True,
|
||||
"exclusive": True,
|
||||
"primary": True,
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
out = cache_domains._inject_onion_domains(
|
||||
merged, apps, "infinito.test", self.ONION
|
||||
)
|
||||
self.assertEqual(out["web-app-x"], [f"x.{self.ONION}"])
|
||||
def test_clearnet_mode_never_renders_the_tor_flag(self):
|
||||
apps = self._apps(enabled="{{ 'svc-net-tor' in group_names }}")
|
||||
self.assertEqual(self._inject(apps, "clearnet"), ["x.infinito.test"])
|
||||
|
||||
def test_consumer_override_beats_provider_default(self):
|
||||
"""A consumer that pins exclusive: false overrides the provider's
|
||||
exclusive: true and stays dual-stack."""
|
||||
merged = {"web-app-x": ["x.infinito.test"]}
|
||||
apps = {
|
||||
"web-app-x": {
|
||||
"services": {
|
||||
"tor": {"enabled": True, "shared": True, "exclusive": False}
|
||||
}
|
||||
},
|
||||
"svc-net-tor": {
|
||||
"services": {
|
||||
"tor": {
|
||||
"enabled": True,
|
||||
"shared": True,
|
||||
"exclusive": True,
|
||||
"primary": False,
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
out = cache_domains._inject_onion_domains(
|
||||
merged, apps, "infinito.test", self.ONION
|
||||
)
|
||||
self.assertEqual(out["web-app-x"], ["x.infinito.test", f"x.{self.ONION}"])
|
||||
def test_tor_only_role_on_clearnet_node_fails_loudly(self):
|
||||
with self.assertRaisesRegex(ValueError, "web-app-x"):
|
||||
self._inject(self._apps(modes=["tor"]), "clearnet")
|
||||
|
||||
def test_consumer_exclusive_false_inherits_provider_primary_dual(self):
|
||||
"""A consumer pinning only exclusive: false inherits the provider's
|
||||
primary: true and stays dual-stack, onion-first."""
|
||||
merged = {"web-app-x": ["x.infinito.test"]}
|
||||
apps = {
|
||||
"web-app-x": {
|
||||
"services": {
|
||||
"tor": {"enabled": True, "shared": True, "exclusive": False}
|
||||
}
|
||||
},
|
||||
"svc-net-tor": {
|
||||
"services": {
|
||||
"tor": {
|
||||
"enabled": True,
|
||||
"shared": True,
|
||||
"exclusive": True,
|
||||
"primary": True,
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
out = cache_domains._inject_onion_domains(
|
||||
merged, apps, "infinito.test", self.ONION
|
||||
def test_single_mode_on_multi_serves_tor(self):
|
||||
self.assertEqual(
|
||||
self._inject(self._apps(single_mode=True), "multi"), [f"x.{self.ONION}"]
|
||||
)
|
||||
|
||||
def test_clearnet_only_role_on_multi_stays_clearnet(self):
|
||||
self.assertEqual(
|
||||
self._inject(self._apps(modes=["clearnet"]), "multi"), ["x.infinito.test"]
|
||||
)
|
||||
|
||||
def test_disabled_tor_bond_on_multi_stays_clearnet(self):
|
||||
self.assertEqual(
|
||||
self._inject(self._apps(enabled=False), "multi"), ["x.infinito.test"]
|
||||
)
|
||||
|
||||
def test_role_without_tor_bond_is_exempt_on_tor_node(self):
|
||||
apps = {"web-app-x": {"services": {}}}
|
||||
self.assertEqual(self._inject(apps, "tor"), ["x.infinito.test"])
|
||||
|
||||
def test_deployed_mismatch_fails_loudly(self):
|
||||
with self.assertRaisesRegex(ValueError, "web-app-x"):
|
||||
self._inject(self._apps(modes=["clearnet"]), "tor")
|
||||
|
||||
def test_undeployed_mismatch_keeps_domains(self):
|
||||
self.assertEqual(
|
||||
self._inject(self._apps(modes=["clearnet"]), "tor", deployed=()),
|
||||
["x.infinito.test"],
|
||||
)
|
||||
self.assertEqual(out["web-app-x"], [f"x.{self.ONION}", "x.infinito.test"])
|
||||
|
||||
def test_bare_primary_domain_maps_to_node_onion(self):
|
||||
merged = {"web-app-x": ["infinito.test"]}
|
||||
out = cache_domains._inject_onion_domains(
|
||||
merged, self._apps(), "infinito.test", self.ONION
|
||||
self.assertEqual(
|
||||
self._inject(self._apps(), "multi", domains=["infinito.test"]),
|
||||
["infinito.test", self.ONION],
|
||||
)
|
||||
|
||||
def test_named_canonicals_gain_onion_keys_after_clearnet(self):
|
||||
self.assertEqual(
|
||||
self._inject(self._apps(), "multi", domains={"api": "api.infinito.test"}),
|
||||
{"api": "api.infinito.test", "api_onion": f"api.{self.ONION}"},
|
||||
)
|
||||
|
||||
def test_named_canonicals_in_tor_mode_swap_values(self):
|
||||
self.assertEqual(
|
||||
self._inject(self._apps(), "tor", domains={"api": "api.infinito.test"}),
|
||||
{"api": f"api.{self.ONION}"},
|
||||
)
|
||||
self.assertEqual(out["web-app-x"], ["infinito.test", self.ONION])
|
||||
|
||||
|
||||
class TestOnionFlagsAreRendered(unittest.TestCase):
|
||||
"""`services.tor.*` reaches this map as Jinja in 99 of the 105 roles that
|
||||
declare it, and as its own source text whenever the applications render is
|
||||
still in flight. Reading that text as a boolean answers False and drops the
|
||||
onion domains for every app, which is what froze a clearnet asset URL into
|
||||
the applications payload of run 34922994823.
|
||||
class TestNodeNetworkMode(unittest.TestCase):
|
||||
def test_node_onion_requires_the_provider_on_the_host(self):
|
||||
apps = {"svc-net-tor": {"services": {"tor": {"node": "n.onion"}}}}
|
||||
self.assertEqual(cache_domains.node_onion_of(apps, {"group_names": []}), "")
|
||||
self.assertEqual(
|
||||
cache_domains.node_onion_of(apps, {"group_names": ["svc-net-tor"]}),
|
||||
"n.onion",
|
||||
)
|
||||
|
||||
def test_empty_mode_derives_from_the_node_onion(self):
|
||||
self.assertEqual(cache_domains.node_network_mode({}, "n.onion"), "multi")
|
||||
self.assertEqual(cache_domains.node_network_mode({}, ""), "clearnet")
|
||||
|
||||
def test_declared_tor_mode_without_provider_fails(self):
|
||||
with self.assertRaises(ValueError):
|
||||
cache_domains.node_network_mode({"NETWORK_MODE": "tor"}, "")
|
||||
|
||||
|
||||
class TestTorFlagsAreRendered(unittest.TestCase):
|
||||
"""`services.tor.enabled` reaches this map as Jinja in almost every role
|
||||
that declares it, and as its own source text whenever the applications
|
||||
render is still in flight. Reading that text as a boolean answers False and
|
||||
drops the onion domains for every app.
|
||||
"""
|
||||
|
||||
ONION = TestOnionDomainInjection.ONION
|
||||
ONION = TestNetworkSiblingInjection.ONION
|
||||
JINJA_ON = "{{ 'svc-net-tor' in group_names }}"
|
||||
|
||||
_apps = TestOnionDomainInjection._apps
|
||||
|
||||
def _templar(self, **variables):
|
||||
from ansible.parsing.dataloader import DataLoader
|
||||
from ansible.template import Templar
|
||||
@@ -358,49 +334,34 @@ class TestOnionFlagsAreRendered(unittest.TestCase):
|
||||
templar.available_variables = dict(variables)
|
||||
return templar
|
||||
|
||||
def _inject(self, apps, **variables):
|
||||
return cache_domains._inject_onion_domains(
|
||||
def _inject(self, templar, **variables):
|
||||
return cache_domains._inject_network_siblings(
|
||||
{"web-app-x": ["x.infinito.test"]},
|
||||
apps,
|
||||
{"web-app-x": {"services": {"tor": {"enabled": self.JINJA_ON}}}},
|
||||
"infinito.test",
|
||||
self.ONION,
|
||||
templar=self._templar(**variables),
|
||||
"multi",
|
||||
templar=templar,
|
||||
variables=dict(variables),
|
||||
)
|
||||
)["web-app-x"]
|
||||
|
||||
def test_a_templated_enabled_flag_still_injects_the_onion(self):
|
||||
out = self._inject(
|
||||
self._apps(enabled=self.JINJA_ON),
|
||||
group_names=["svc-net-tor", "web-app-x"],
|
||||
variables = {"group_names": ["svc-net-tor", "web-app-x"]}
|
||||
self.assertEqual(
|
||||
self._inject(self._templar(**variables), **variables),
|
||||
["x.infinito.test", f"x.{self.ONION}"],
|
||||
)
|
||||
|
||||
self.assertEqual(out["web-app-x"], ["x.infinito.test", f"x.{self.ONION}"])
|
||||
|
||||
def test_a_templated_flag_resolving_false_leaves_the_clearnet_domain(self):
|
||||
out = self._inject(self._apps(enabled=self.JINJA_ON), group_names=["web-app-x"])
|
||||
|
||||
self.assertEqual(out["web-app-x"], ["x.infinito.test"])
|
||||
|
||||
def test_templated_exclusive_replaces_the_clearnet_domain(self):
|
||||
out = self._inject(
|
||||
self._apps(enabled=self.JINJA_ON, exclusive=self.JINJA_ON),
|
||||
group_names=["svc-net-tor", "web-app-x"],
|
||||
variables = {"group_names": ["web-app-x"]}
|
||||
self.assertEqual(
|
||||
self._inject(self._templar(**variables), **variables),
|
||||
["x.infinito.test"],
|
||||
)
|
||||
|
||||
self.assertEqual(out["web-app-x"], [f"x.{self.ONION}"])
|
||||
|
||||
def test_a_flag_that_cannot_be_rendered_is_an_error(self):
|
||||
"""Without a templar the text cannot resolve, and answering False
|
||||
would drop the onion domains without saying so."""
|
||||
with self.assertRaisesRegex(ValueError, "services.tor.enabled"):
|
||||
cache_domains._inject_onion_domains(
|
||||
{"web-app-x": ["x.infinito.test"]},
|
||||
self._apps(enabled=self.JINJA_ON),
|
||||
"infinito.test",
|
||||
self.ONION,
|
||||
templar=None,
|
||||
variables={"group_names": ["svc-net-tor"]},
|
||||
)
|
||||
self._inject(None, group_names=["svc-net-tor"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -133,38 +133,6 @@ class TestTlsCommon(unittest.TestCase):
|
||||
"x", domains=self.domains, forced_mode="invalid", err_prefix="t"
|
||||
)
|
||||
|
||||
def test_resolve_term_clearnet_domain_swapped_to_onion_keeps_clearnet(self):
|
||||
domains = {"svc-prx-openresty": ["abc.onion"]}
|
||||
applications = {
|
||||
"svc-prx-openresty": {"domains": {"canonical": ["infinito.test"]}}
|
||||
}
|
||||
app_id, primary = resolve_term(
|
||||
"infinito.test",
|
||||
domains=domains,
|
||||
applications=applications,
|
||||
forced_mode="auto",
|
||||
err_prefix="t",
|
||||
)
|
||||
self.assertEqual(app_id, "svc-prx-openresty")
|
||||
self.assertEqual(primary, "infinito.test")
|
||||
self.assertTrue(resolve_enabled({}, True, primary_domain=primary))
|
||||
|
||||
def test_resolve_term_alias_without_onion_swap_keeps_canonical(self):
|
||||
domains = {"web-app-x": ["x.example"]}
|
||||
applications = {
|
||||
"web-app-x": {
|
||||
"domains": {"canonical": ["x.example"], "aliases": ["old.example"]}
|
||||
}
|
||||
}
|
||||
_, primary = resolve_term(
|
||||
"old.example",
|
||||
domains=domains,
|
||||
applications=applications,
|
||||
forced_mode="auto",
|
||||
err_prefix="t",
|
||||
)
|
||||
self.assertEqual(primary, "x.example")
|
||||
|
||||
def test_resolve_enabled_and_mode(self):
|
||||
app = {}
|
||||
self.assertTrue(resolve_enabled(app, True))
|
||||
@@ -319,6 +287,81 @@ class TestTlsCommon(unittest.TestCase):
|
||||
"cdn.example",
|
||||
)
|
||||
|
||||
def test_align_domain_to_consumer_onion_consumer_gets_onion_sibling(self):
|
||||
domains = {
|
||||
"web-svc-cdn": ["cdn.example", "cdn.abc.onion"],
|
||||
"web-app-dashboard": ["dash.abc.onion"],
|
||||
}
|
||||
self.assertEqual(
|
||||
align_domain_to_consumer(
|
||||
domains, "web-svc-cdn", "cdn.example", consumer="web-app-dashboard"
|
||||
),
|
||||
"cdn.abc.onion",
|
||||
)
|
||||
|
||||
def test_align_domain_to_consumer_follows_the_rendered_vhost(self):
|
||||
domains = {
|
||||
"web-svc-cdn": ["cdn.example", "cdn.abc.onion"],
|
||||
"web-app-wazuh": ["wazuh.example", "wazuh.abc.onion"],
|
||||
}
|
||||
variables = {"application_id": "web-app-wazuh", "domain": "wazuh.abc.onion"}
|
||||
self.assertEqual(
|
||||
align_domain_to_consumer(
|
||||
domains, "web-svc-cdn", "cdn.example", variables=variables
|
||||
),
|
||||
"cdn.abc.onion",
|
||||
)
|
||||
|
||||
def test_align_domain_to_consumer_ignores_a_foreign_vhost(self):
|
||||
domains = {
|
||||
"web-svc-cdn": ["cdn.example", "cdn.abc.onion"],
|
||||
"web-app-wazuh": ["wazuh.example", "wazuh.abc.onion"],
|
||||
}
|
||||
variables = {"application_id": "web-app-wazuh", "domain": "other.abc.onion"}
|
||||
self.assertEqual(
|
||||
align_domain_to_consumer(
|
||||
domains, "web-svc-cdn", "cdn.example", variables=variables
|
||||
),
|
||||
"cdn.example",
|
||||
)
|
||||
|
||||
def test_align_domain_to_consumer_renders_a_templated_vhost(self):
|
||||
class _FakeTemplar:
|
||||
def template(self, value):
|
||||
return {"{{ front_proxy_domain }}": "wazuh.abc.onion"}.get(value, value)
|
||||
|
||||
domains = {
|
||||
"web-svc-cdn": ["cdn.example", "cdn.abc.onion"],
|
||||
"web-app-wazuh": ["wazuh.example", "wazuh.abc.onion"],
|
||||
}
|
||||
variables = {
|
||||
"application_id": "web-app-wazuh",
|
||||
"domain": "{{ front_proxy_domain }}",
|
||||
}
|
||||
self.assertEqual(
|
||||
align_domain_to_consumer(
|
||||
domains,
|
||||
"web-svc-cdn",
|
||||
"cdn.example",
|
||||
variables=variables,
|
||||
templar=_FakeTemplar(),
|
||||
),
|
||||
"cdn.abc.onion",
|
||||
)
|
||||
|
||||
def test_align_domain_to_consumer_never_moves_the_sso_issuer(self):
|
||||
domains = {
|
||||
"web-app-keycloak": ["auth.example", "auth.abc.onion"],
|
||||
"web-app-wazuh": ["wazuh.example", "wazuh.abc.onion"],
|
||||
}
|
||||
variables = {"application_id": "web-app-wazuh", "domain": "wazuh.abc.onion"}
|
||||
self.assertEqual(
|
||||
align_domain_to_consumer(
|
||||
domains, "web-app-keycloak", "auth.example", variables=variables
|
||||
),
|
||||
"auth.example",
|
||||
)
|
||||
|
||||
def test_align_domain_to_consumer_dict_target_picks_clearnet_value(self):
|
||||
domains = {
|
||||
"web-app-matrix": {
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
"""Contract of the network registry and the per-role reachability resolution."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
|
||||
from utils.networks.reachability import (
|
||||
CLEARNET,
|
||||
MULTI,
|
||||
TOR,
|
||||
allowed_networks,
|
||||
effective_networks,
|
||||
is_network,
|
||||
network,
|
||||
network_of,
|
||||
resolve_node_mode,
|
||||
role_reachability,
|
||||
sibling_domain,
|
||||
)
|
||||
|
||||
ONION = "abcdefghijklmnopqrstuvwxyz234567abcdefghijklmnopqrstuvw.onion"
|
||||
|
||||
|
||||
def _cfg(**reachability) -> dict:
|
||||
return {"networks": {"reachability": reachability}} if reachability else {}
|
||||
|
||||
|
||||
class TestNetworkOf(unittest.TestCase):
|
||||
def test_onion_suffix_is_tor(self) -> None:
|
||||
self.assertEqual(network_of(f"wazuh.{ONION}"), TOR)
|
||||
|
||||
def test_trailing_dot_and_case_are_ignored(self) -> None:
|
||||
self.assertEqual(network_of(f"Wazuh.{ONION.upper()}."), TOR)
|
||||
|
||||
def test_everything_else_is_clearnet(self) -> None:
|
||||
self.assertEqual(network_of("wazuh.infinito.test"), CLEARNET)
|
||||
|
||||
def test_is_network(self) -> None:
|
||||
self.assertTrue(is_network(ONION, TOR))
|
||||
self.assertFalse(is_network("infinito.test", TOR))
|
||||
|
||||
def test_reserved_network_is_refused(self) -> None:
|
||||
with self.assertRaises(ValueError):
|
||||
network("handshake")
|
||||
|
||||
def test_unknown_network_is_refused(self) -> None:
|
||||
with self.assertRaises(ValueError):
|
||||
network("i2p")
|
||||
|
||||
|
||||
class TestSiblingDomain(unittest.TestCase):
|
||||
def test_clearnet_subdomain_to_tor(self) -> None:
|
||||
self.assertEqual(
|
||||
sibling_domain("wazuh.infinito.test", TOR, "infinito.test", ONION),
|
||||
f"wazuh.{ONION}",
|
||||
)
|
||||
|
||||
def test_apex_to_tor_is_the_node_onion(self) -> None:
|
||||
self.assertEqual(
|
||||
sibling_domain("infinito.test", TOR, "infinito.test", ONION), ONION
|
||||
)
|
||||
|
||||
def test_tor_subdomain_to_clearnet(self) -> None:
|
||||
self.assertEqual(
|
||||
sibling_domain(f"wazuh.{ONION}", CLEARNET, "infinito.test", ONION),
|
||||
"wazuh.infinito.test",
|
||||
)
|
||||
|
||||
def test_same_network_returns_itself(self) -> None:
|
||||
self.assertEqual(
|
||||
sibling_domain("wazuh.infinito.test", CLEARNET, "infinito.test", ONION),
|
||||
"wazuh.infinito.test",
|
||||
)
|
||||
|
||||
def test_foreign_domain_has_no_sibling(self) -> None:
|
||||
self.assertIsNone(sibling_domain("example.org", TOR, "infinito.test", ONION))
|
||||
|
||||
def test_no_node_address_has_no_sibling(self) -> None:
|
||||
self.assertIsNone(
|
||||
sibling_domain("wazuh.infinito.test", TOR, "infinito.test", "")
|
||||
)
|
||||
|
||||
|
||||
class TestResolveNodeMode(unittest.TestCase):
|
||||
def test_empty_derives_multi_with_tor(self) -> None:
|
||||
self.assertEqual(resolve_node_mode("", tor_provided=True), MULTI)
|
||||
|
||||
def test_empty_derives_clearnet_without_tor(self) -> None:
|
||||
self.assertEqual(resolve_node_mode(None, tor_provided=False), CLEARNET)
|
||||
|
||||
def test_explicit_tor(self) -> None:
|
||||
self.assertEqual(resolve_node_mode("tor", tor_provided=True), TOR)
|
||||
|
||||
def test_tor_mode_without_provider_fails(self) -> None:
|
||||
with self.assertRaises(ValueError):
|
||||
resolve_node_mode("multi", tor_provided=False)
|
||||
|
||||
def test_unknown_mode_fails(self) -> None:
|
||||
with self.assertRaises(ValueError):
|
||||
resolve_node_mode("dual", tor_provided=True)
|
||||
|
||||
|
||||
class TestRoleReachability(unittest.TestCase):
|
||||
def test_missing_means_every_network(self) -> None:
|
||||
self.assertEqual(role_reachability({}), ((), False))
|
||||
|
||||
def test_empty_list_means_every_network(self) -> None:
|
||||
self.assertEqual(role_reachability(_cfg(modes=[])), ((), False))
|
||||
|
||||
def test_multi_is_not_a_list_value(self) -> None:
|
||||
with self.assertRaises(ValueError):
|
||||
role_reachability(_cfg(modes=[CLEARNET, MULTI]))
|
||||
|
||||
def test_single_mode_must_be_boolean(self) -> None:
|
||||
with self.assertRaises(TypeError):
|
||||
role_reachability(_cfg(single_mode="yes"))
|
||||
|
||||
def test_tor_disabled_role_loses_tor(self) -> None:
|
||||
self.assertEqual(allowed_networks({}, tor_enabled=False), ((CLEARNET,), False))
|
||||
|
||||
|
||||
class TestEffectiveNetworks(unittest.TestCase):
|
||||
"""Every combination of node mode, ``modes`` and ``single_mode``."""
|
||||
|
||||
CASES = (
|
||||
(CLEARNET, (), False, (CLEARNET,)),
|
||||
(TOR, (), False, (TOR,)),
|
||||
(MULTI, (), False, (CLEARNET, TOR)),
|
||||
(CLEARNET, (), True, (CLEARNET,)),
|
||||
(TOR, (), True, (TOR,)),
|
||||
(MULTI, (), True, (TOR,)),
|
||||
(CLEARNET, (CLEARNET,), False, (CLEARNET,)),
|
||||
(MULTI, (CLEARNET,), False, (CLEARNET,)),
|
||||
(MULTI, (CLEARNET,), True, (CLEARNET,)),
|
||||
(TOR, (TOR,), False, (TOR,)),
|
||||
(MULTI, (TOR,), False, (TOR,)),
|
||||
(MULTI, (CLEARNET, TOR), False, (CLEARNET, TOR)),
|
||||
(MULTI, (CLEARNET, TOR), True, (TOR,)),
|
||||
)
|
||||
|
||||
FAILURES = (
|
||||
(TOR, (CLEARNET,), False),
|
||||
(TOR, (CLEARNET,), True),
|
||||
(CLEARNET, (TOR,), False),
|
||||
(CLEARNET, (TOR,), True),
|
||||
)
|
||||
|
||||
def test_served_networks(self) -> None:
|
||||
for node_mode, modes, single, expected in self.CASES:
|
||||
allowed, parsed_single = allowed_networks(
|
||||
_cfg(modes=list(modes), single_mode=single), tor_enabled=True
|
||||
)
|
||||
with self.subTest(node=node_mode, modes=modes, single=single):
|
||||
self.assertEqual(
|
||||
effective_networks(node_mode, allowed, parsed_single), expected
|
||||
)
|
||||
|
||||
def test_mismatch_fails_loudly(self) -> None:
|
||||
for node_mode, modes, single in self.FAILURES:
|
||||
allowed, parsed_single = allowed_networks(
|
||||
_cfg(modes=list(modes), single_mode=single), tor_enabled=True
|
||||
)
|
||||
with (
|
||||
self.subTest(node=node_mode, modes=modes, single=single),
|
||||
self.assertRaisesRegex(ValueError, "web-app-x"),
|
||||
):
|
||||
effective_networks(node_mode, allowed, parsed_single, app="web-app-x")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user