Files
computer-playbook/plugins/lookup/network_handover.py
T
kevinveenbirkenbachandClaude Opus 5.5 4c6f9d5cd7 refactor(networks): decide a handover sibling in one lookup and proxy it as the canonical host
The handover condition lived as the same inline Jinja in basic.conf.j2,
ws_generic.conf.j2 and Baserow's proxy.conf.j2. Move it into
lookup('network_handover', application_id, domain), which also refuses a
non-boolean flag, and use it at all three places.

A handover sibling still proxies its health, MCP and SSO endpoints to the
backend, and those requests carried the onion Host, which single-URL apps
reject: pretix answered /healthz/ready on the onion with 400 Unknown host
and failed the Prometheus blackbox probe. forwarded.conf.j2 now sends the
canonical domain as Host and X-Forwarded-Host on a handover sibling, so the
onion probe keeps measuring the backend through the onion vhost.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-18 15:19:57 +02:00

48 lines
1.8 KiB
Python

"""Whether a vhost is a network sibling its application hands over to clearnet."""
from __future__ import annotations
from typing import Any
from ansible.errors import AnsibleError
from ansible.plugins.loader import lookup_loader
from ansible.plugins.lookup import LookupBase
from utils.networks.reachability import network_of
class LookupModule(LookupBase):
"""
Usage:
{{ lookup('network_handover', application_id, domain) }}
Returns True when ``application_id`` opts into
``networks.reachability.handover`` and ``domain`` lies in another network
than the application's canonical domain, so its vhost only redirects there.
"""
def run(self, terms, variables: dict[str, Any] | None = None, **kwargs):
if not terms or len(terms) != 2 or not all(str(t or "").strip() for t in terms):
raise AnsibleError(
"lookup('network_handover', application_id, domain) expects two terms"
)
variables = variables or getattr(self._templar, "available_variables", {}) or {}
templar = getattr(self, "_templar", None)
application_id, domain = (str(term).strip() for term in terms)
def _run(name: str, *args: Any) -> Any:
return lookup_loader.get(name, loader=self._loader, templar=templar).run(
list(args), variables=variables
)[0]
handover = _run(
"config", application_id, "networks.reachability.handover", False
)
if not isinstance(handover, bool):
raise AnsibleError(
f"network_handover: {application_id}: networks.reachability.handover must be a boolean"
)
if not handover:
return [False]
return [network_of(domain) != network_of(str(_run("domain", application_id)))]