Files
computer-playbook/tests/unit/python/plugins/lookup/test_container_dns.py
T
kevinveenbirkenbachandClaude Opus 5 677a63931c Merge branch 'feature/network/multi-network-serving' into feature/store-i18n
Resolves 68 conflicted files. The governing rule was our structure with their
network semantics: the tor boolean becomes a network mode (clearnet | tor |
multi) throughout the variant layer, while this branch's architecture and mesh
axes, its extracted modules and its i18n content stay.

Where the two sides covered different ground, both are kept rather than one
picked:

- ca_injected answers a ca_client on the deployment's TLS mode OR on an SSO
  issuer that resolves self-signed. 3a3e9ed2c9 records why: an issuer is not
  an object store, so keeping only one side drops the other's coverage.
- get_merged_domains keeps the loud refusal when svc-net-tor is deployed
  without a provisioned node, now gated on their tor_serves_host; their
  node_onion_of returns an empty string there, which would have served every
  onion-exclusive app on clearnet in silence.
- sso_cookie_domains stays the single source for oauth2-proxy's cookie scope.
  Their per-network branch omitted the issuer domain on the onion arm, which
  breaks the callback leg.
- The clearnet CSP batch carries --clearnet-timeout again; their batch dict
  hardcoded zero while the role still passes the flag.
- The dashboard keeps its i18n content dir, image, version and design
  resolution; their side reduced the logo sources to the bare service
  provider values.

Their side wins where it is measurably better: the onion mirror rewrite is
gated per vhost rather than per app, the Playwright onion decision reads the
pass's own domain instead of the role's canonical one, and the matomo
reinjection and dashboard config render per network sibling.

Eight files were broken by the merge without ever carrying a conflict marker,
because git marks only the lines both sides touched:

- utils/cache/domains.py lost the onionize block that sat between two hunks
- roles/sys-front-inj-all/templates/body_filter.lua.j2 called it
- web-app-bluesky, keycloak sso_proxy: the get_entity_name filter was renamed
- trigger/__main__.py, playwright_coverage.py and its test: the deleted tor
  module
- cli/administration/deploy/ci/timings.py read Label.tor
- plugins/filter/csp_filters.py called _aligned_url with four of six arguments
- tests/unit/python/utils/cache/test_domains.py referenced a renamed class
- tests/.../sys-ctl-hlth-csp/files/test_script.py mocked a function main no
  longer calls

Verified: the variant layer (155 tests, 65 subtests), ca_injected (18),
the CI layer (175), the domains cache including the restored guard (32),
retrigger inputs (9), timings and the CSP health script (53). make
quality-high clears autoformat; its remaining findings are tracked separately.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-10-23 09:53:41 +02:00

410 lines
15 KiB
Python

import sys
import unittest
from typing import ClassVar
from . import PROJECT_ROOT
def _ensure_repo_root_on_syspath():
if str(PROJECT_ROOT) not in sys.path:
sys.path.insert(0, str(PROJECT_ROOT))
_ensure_repo_root_on_syspath()
from ansible.errors import AnsibleError # noqa: E402
from plugins.lookup.container_dns import resolve_container_dns # noqa: E402
from utils.templating.ansible import ( # noqa: E402
TrustedAsTemplate,
_trust_as_template,
)
_POOLS = [{"base": "10.208.0.0/12", "size": 24}]
_PUBLIC = ["192.0.2.53", "198.51.100.53"]
_GATEWAY = "10.208.0.1"
_BRIDGE = {"docker0": {"ipv4": {"address": "172.17.0.1"}}}
_CLEARNET = {"internet": {"dns": "172.30.0.53"}}
def _vars(**overrides):
"""Return play variables for the lookup.
Args:
overrides: values replacing the defaults.
The pools and the public resolvers are group_vars that exist on every host,
so a fixture without them tests a shape production never has.
"""
variables = {
"ansible_facts": _BRIDGE,
"group_names": ["svc-net-tor"],
"networks": _CLEARNET,
"DEPLOYMENT_MODE": "compose",
"IS_COMPOSE_MODE": True,
"IS_SWARM_MODE": False,
"NETWORK_DOCKER_ADDRESS_POOLS": _POOLS,
"NETWORK_PUBLIC_DNS_RESOLVERS": _PUBLIC,
}
variables.update(overrides)
return variables
class TestResolveContainerDns(unittest.TestCase):
"""The bridge resolver must come first on a Tor node so .onion resolves,
with the clearnet resolver kept as fallback."""
def test_tor_node_prefers_the_bridge_resolver(self):
self.assertEqual(resolve_container_dns(_vars()), [_GATEWAY, "172.30.0.53"])
def test_without_tor_only_clearnet(self):
self.assertEqual(resolve_container_dns(_vars(group_names=[])), ["172.30.0.53"])
def test_the_bridge_follows_ownership_not_the_deploy_mode(self):
self.assertEqual(
resolve_container_dns(_vars(DEPLOYMENT_MODE="swarm")),
[_GATEWAY, "172.30.0.53"],
)
def test_a_tor_node_without_clearnet_still_gets_a_second_resolver(self):
self.assertEqual(
resolve_container_dns(_vars(DEPLOYMENT_MODE="swarm", networks={})),
[_GATEWAY, _PUBLIC[0]],
)
def test_a_non_tor_node_yields_nothing_without_clearnet(self):
self.assertEqual(resolve_container_dns(_vars(group_names=[], networks={})), [])
def test_a_bridge_equal_to_clearnet_is_not_emitted_twice(self):
self.assertEqual(
resolve_container_dns(_vars(networks={"internet": {"dns": _GATEWAY}})),
[_GATEWAY, _PUBLIC[0]],
)
def test_the_declaration_answers_where_the_fact_is_absent(self):
self.assertEqual(
resolve_container_dns(_vars(ansible_facts={}, group_names=[])),
["172.30.0.53"],
)
def test_a_pool_less_host_has_no_bridge_to_offer(self):
self.assertEqual(
resolve_container_dns(_vars(networks={}, NETWORK_DOCKER_ADDRESS_POOLS=[])),
[],
)
def test_missing_keys_do_not_raise(self):
self.assertEqual(resolve_container_dns({}), [])
class TestALoopbackResolverIsNotAClaim(unittest.TestCase):
"""Reading resolv.conf cannot tell who binds the bridge.
A systemd-resolved stub on ``127.0.0.53`` and a node dnsmasq that also
listens on the bridge look identical from the host's own resolv.conf, and
only the second is reachable from a container. Treating loopback as proof
handed ordinary hosts a bridge address nothing served, and their image
builds resolved nothing while the daemon itself resolved fine.
"""
@staticmethod
def _facts(nameservers):
return {**_BRIDGE, "dns": {"nameservers": nameservers}}
def _resolve(self, nameservers, **overrides):
"""Return the resolver list for a host with these nameservers.
Args:
nameservers: what the host's own resolv.conf lists.
overrides: further variables, e.g. the bridge declaration.
"""
return resolve_container_dns(
_vars(group_names=[], ansible_facts=self._facts(nameservers), **overrides)
)
def test_a_loopback_stub_alone_earns_no_bridge(self):
for nameservers in (["127.0.0.1"], ["127.0.0.53"], ["::1"]):
with self.subTest(nameservers=nameservers):
self.assertEqual(["172.30.0.53"], self._resolve(nameservers))
def test_the_declaration_is_what_earns_it(self):
self.assertEqual(
[_GATEWAY, "172.30.0.53"],
self._resolve(["127.0.0.1"], NETWORK_CONTAINER_BRIDGE_RESOLVER=True),
)
def test_a_routable_host_resolver_needs_no_bridge(self):
self.assertEqual(["172.30.0.53"], self._resolve(["172.30.0.53"]))
def test_an_empty_nameserver_list_earns_no_bridge(self):
self.assertEqual(["172.30.0.53"], self._resolve([]))
class _Templar:
"""Renders one expression, the way a play's templar would.
Ansible 2.19+ renders only strings carrying ``TrustedAsTemplate``, and
``str()`` drops that tag. A stub that renders any string passes while
production returns the expression untouched, so this one refuses an
untagged string exactly as the real templar does.
"""
def __init__(self, mapping):
self.mapping = mapping
def template(self, text):
if TrustedAsTemplate is not None and not TrustedAsTemplate.is_tagged_on(text):
return text
for name, value in self.mapping.items():
if str(text).strip() == "{{ " + name + " | first }}":
return value[0]
return text
class TestTheBridgeSurvivesTheFirstRun(unittest.TestCase):
"""The run that writes daemon.json is the one with no docker0 fact.
Facts are gathered at play start and nothing re-gathers them after the
daemon is installed, so a fresh node renders its daemon config while
``ansible_facts.docker0`` is still absent. Deriving the address from the
fact alone emitted the clearnet resolver on its own, and the inner
containers then asked a public resolver for a domain only the node dnsmasq
knows: ``server can't find <domain>: NXDOMAIN``.
"""
POOLS: ClassVar[list] = [{"base": "10.208.0.0/12", "size": 24}]
def facts(self, **extra):
"""Return facts for a host that resolves only through loopback.
Args:
extra: additional fact keys, e.g. a ``docker0`` entry.
"""
return {"dns": {"nameservers": ["127.0.0.1"]}, **extra}
def test_the_bridge_is_known_before_docker_ever_ran(self):
self.assertEqual(
resolve_container_dns(
_vars(
ansible_facts=self.facts(),
group_names=[],
networks={"internet": {"dns": "192.0.2.1"}},
NETWORK_DOCKER_ADDRESS_POOLS=self.POOLS,
NETWORK_CONTAINER_BRIDGE_RESOLVER=True,
)
),
["10.208.0.1", "192.0.2.1"],
)
def test_the_pass_after_docker_renders_the_same_bytes(self):
"""A differing render restarts the runtime under a live stack."""
before = resolve_container_dns(
_vars(
ansible_facts=self.facts(),
group_names=[],
networks={"internet": {"dns": "192.0.2.1"}},
NETWORK_DOCKER_ADDRESS_POOLS=self.POOLS,
)
)
after = resolve_container_dns(
_vars(
ansible_facts=self.facts(docker0={"ipv4": {"address": "10.208.0.1"}}),
group_names=[],
networks={"internet": {"dns": "192.0.2.1"}},
NETWORK_DOCKER_ADDRESS_POOLS=self.POOLS,
)
)
self.assertEqual(before, after)
def test_a_stale_docker0_fact_never_reaches_the_daemon(self):
"""Docker's default bridge, read before the pools moved it.
Facts are gathered before the daemon config is applied, so ``docker0``
still holds ``172.17.0.1`` while the pools are about to put the bridge
at ``10.208.0.1``. A swarm node shipped exactly that mismatch: its
daemon.json named the address its live interface no longer had, and
every lookup inside a container went to a dead resolver.
"""
self.assertEqual(
resolve_container_dns(
_vars(
ansible_facts=self.facts(
docker0={"ipv4": {"address": "172.17.0.1"}}
),
group_names=[],
networks={"internet": {"dns": "192.0.2.1"}},
NETWORK_DOCKER_ADDRESS_POOLS=self.POOLS,
NETWORK_CONTAINER_BRIDGE_RESOLVER=True,
)
),
["10.208.0.1", "192.0.2.1"],
)
class TestTheBridgeNeverGoesOutAlone(unittest.TestCase):
"""No host shape may end up with the bridge as its only resolver.
Whether anything answers on the bridge is not knowable from the host's own
resolv.conf: a systemd-resolved stub and a node dnsmasq both read as
loopback-only and only the second binds the bridge. A list holding just
that address therefore hands the daemon one possibly-dead nameserver, and
every container and image build loses DNS - measured as an apk install
failing on ``DNS: transient error`` with ``"dns": ["10.208.0.1"]``.
The assertion runs over a cross-product because the regression slipped
through a suite that checked each shape on its own.
"""
NAMESERVERS: ClassVar[list] = [
["127.0.0.1"],
["127.0.0.53"],
["::1"],
["172.30.0.53"],
[],
]
def test_every_host_shape_keeps_a_second_resolver(self):
for nameservers in self.NAMESERVERS:
for tor in ([], ["svc-net-tor"]):
for clearnet in ("", "192.0.2.1"):
for docker0 in (
{},
{"docker0": {"ipv4": {"address": "172.17.0.1"}}},
):
with self.subTest(
nameservers=nameservers, tor=bool(tor), clearnet=clearnet
):
result = resolve_container_dns(
_vars(
group_names=tor,
networks={"internet": {"dns": clearnet}},
ansible_facts={
"dns": {"nameservers": nameservers},
**docker0,
},
)
)
self.assertNotEqual([_GATEWAY], result)
def test_the_fallback_is_the_declared_resolver_not_a_literal(self):
"""A hardcoded address here would bypass the project's own SPOT."""
result = resolve_container_dns(
_vars(group_names=["svc-net-tor"], networks={"internet": {"dns": ""}})
)
self.assertEqual([_GATEWAY, _PUBLIC[0]], result)
def test_an_empty_resolver_declaration_fails_loudly(self):
with self.assertRaises(AnsibleError):
resolve_container_dns(
_vars(
group_names=["svc-net-tor"],
networks={"internet": {"dns": ""}},
NETWORK_PUBLIC_DNS_RESOLVERS=[],
)
)
class TestTheTrustTagIsStillWhereBothSidesLookForIt(unittest.TestCase):
"""Without this the whole class below can pass while production is broken.
``_trust_as_template`` and the stub both key off the same private ansible
module. Were it to move, the tagging degrades to a no-op and the templar
stops rendering, while the stub degrades to rendering anything and keeps
the tests green. This asserts the shared premise instead.
"""
def test_the_tag_class_is_importable(self):
self.assertIsNotNone(
TrustedAsTemplate,
"ansible no longer exposes TrustedAsTemplate where "
"utils.templating.ansible looks for it; every lookup that "
"re-tags before templating silently stopped rendering",
)
def test_tagging_is_observable(self):
tagged = _trust_as_template("{{ x }}")
self.assertTrue(TrustedAsTemplate.is_tagged_on(tagged))
self.assertFalse(TrustedAsTemplate.is_tagged_on(str(tagged)))
class TestInventoryExpressionIsRendered(unittest.TestCase):
"""The field arrives through `-e @inventory.yml` and is read raw.
While it held a literal address nobody noticed that nothing rendered it.
An expression there reached the daemon as its own source text, and docker
refused the whole config: ParseAddr("{{ ... }}"): unable to parse IP.
"""
EXPRESSION = "{{ NETWORK_PUBLIC_DNS_RESOLVERS | first }}"
def facts(self, nameservers):
return {"dns": {"nameservers": nameservers}, "docker0": {"ipv4": {}}}
def test_an_expression_is_resolved_against_the_play(self):
templar = _Templar({"NETWORK_PUBLIC_DNS_RESOLVERS": ["192.0.2.1", "192.0.2.2"]})
result = resolve_container_dns(
{
"networks": {"internet": {"dns": self.EXPRESSION}},
"group_names": [],
"ansible_facts": self.facts(["192.0.2.9"]),
},
templar,
)
self.assertEqual(["192.0.2.1"], result)
def test_no_resolver_ever_carries_jinja(self):
"""What reaches daemon.json must be an address, not its source text."""
templar = _Templar({"NETWORK_PUBLIC_DNS_RESOLVERS": ["192.0.2.1"]})
for nameservers in ([], ["127.0.0.1"], ["192.0.2.9"]):
with self.subTest(nameservers=nameservers):
for resolver in resolve_container_dns(
{
"networks": {"internet": {"dns": self.EXPRESSION}},
"group_names": [],
"ansible_facts": self.facts(nameservers),
},
templar,
):
self.assertNotIn("{{", resolver)
def test_a_literal_survives_without_a_templar(self):
"""Outside a play there is nothing to render against, and no need."""
self.assertEqual(
["172.30.0.53"],
resolve_container_dns(
{
"networks": {"internet": {"dns": "172.30.0.53"}},
"group_names": [],
"ansible_facts": self.facts(["192.0.2.9"]),
}
),
)
def test_an_unresolvable_expression_aborts_instead_of_reaching_docker(self):
"""This is the shape that reached CI: no templar, so nothing rendered."""
with self.assertRaises(AnsibleError) as caught:
resolve_container_dns(
{
"networks": {"internet": {"dns": self.EXPRESSION}},
"group_names": [],
"ansible_facts": self.facts(["192.0.2.9"]),
}
)
self.assertIn("daemon.json", str(caught.exception))
def test_a_templar_that_cannot_resolve_it_aborts_too(self):
with self.assertRaises(AnsibleError):
resolve_container_dns(
{
"networks": {"internet": {"dns": self.EXPRESSION}},
"group_names": [],
"ansible_facts": self.facts(["192.0.2.9"]),
},
_Templar({}),
)
if __name__ == "__main__":
unittest.main()