Skip to content

resolver

resolver

DNS resolution with timestamp-based caching.

Resolves allowlist domains via dig (getent fallback) and caches the IPs so containers do not block on DNS at every start.

Two cache layers:

  • per-container file (cache_path): the view the nft ruleset reads. Scoped to one container, so a fresh container never reuses another's.
  • host cache (host_cache_dir): shared across containers, keyed by the allowlist's content hash. The first task resolves; the rest read.

Domains resolve concurrently with a per-lookup timeout, so a batch costs about one lookup and one dead domain cannot stall startup. A batch that does run says so on stderr, before and after: the launcher is waiting on it, and a silent wait reads as a hang.

Every tier but dnsmasq-live uses this module at launch. On that tier domains resolve on-demand at runtime via --nftset; this module then handles raw IPs only.

logger = logging.getLogger(__name__) module-attribute

RESOLVE_TIMEOUT = 2 module-attribute

Per-subprocess DNS budget in seconds.

Best-effort: an answer slower than this counts as no answer. The old 10s budget let one dead domain (times the getent retry) stall a start by ~20s.

MAX_RESOLVE_WORKERS = 16 module-attribute

Upper bound on concurrent resolver subprocesses per batch.

DnsResolver(*, runner, host_cache_dir=None)

Stateless DNS resolver — all persistence lives in the cache files.

Depends on a CommandRunner for lookup-tool (dig/drill) and getent subprocess calls and a host-level cache directory shared across containers.

Inject the command runner and the shared cache location.

Parameters:

Name Type Description Default
runner CommandRunner

Command runner used for all DNS subprocess calls.

required
host_cache_dir Path | None

Cross-container cache directory; None selects dns_cache_dir.

None
Source code in src/terok_shield/dns/resolver.py
def __init__(self, *, runner: CommandRunner, host_cache_dir: Path | None = None) -> None:
    """Inject the command runner and the shared cache location.

    Args:
        runner: Command runner used for all DNS subprocess calls.
        host_cache_dir: Cross-container cache directory; ``None`` selects
            [`dns_cache_dir`][terok_shield.paths.dns_cache_dir].
    """
    self._runner = runner
    self._host_cache_dir = host_cache_dir or dns_cache_dir()

resolve_and_cache(entries, cache_path, *, force=False, source_mtime=0.0)

Resolve profile entries and cache the result.

Reads the per-container file first, then the shared host cache (materializing a hit into the per-container file), and only resolves when both miss.

Parameters:

Name Type Description Default
entries list[str]

Domain names and/or raw IPs from composed profiles.

required
cache_path Path

Per-container file the nft ruleset reads.

required
force bool

Re-resolve even when a cache is younger than the one-hour freshness window.

False
source_mtime float

mtime of the authored policy; a per-container cache older than it is re-resolved even within the freshness window, so an edited allowlist takes effect on the next task start. The host cache ignores this — its content-hash key already makes it edit-aware.

0.0

Returns:

Type Description
list[str]

Resolved IPv4/IPv6 addresses combined with raw IPs/CIDRs.

Source code in src/terok_shield/dns/resolver.py
def resolve_and_cache(
    self,
    entries: list[str],
    cache_path: Path,
    *,
    force: bool = False,
    source_mtime: float = 0.0,
) -> list[str]:
    """Resolve profile entries and cache the result.

    Reads the per-container file first, then the shared host cache
    (materializing a hit into the per-container file), and only resolves
    when both miss.

    Args:
        entries: Domain names and/or raw IPs from composed profiles.
        cache_path: Per-container file the nft ruleset reads.
        force: Re-resolve even when a cache is younger than the one-hour
            freshness window.
        source_mtime: mtime of the authored policy; a per-container cache
            older than it is re-resolved even within the freshness window,
            so an edited allowlist takes effect on the next task start. The
            host cache ignores this — its content-hash key already makes it
            edit-aware.

    Returns:
        Resolved IPv4/IPv6 addresses combined with raw IPs/CIDRs.
    """
    max_age = 0 if force else _CACHE_MAX_AGE
    if self._cache_fresh(cache_path, max_age, source_mtime):
        return self._read_cache(cache_path)

    host_path = self._host_cache_path(entries)
    if self._cache_fresh(host_path, max_age):
        ips = self._read_cache(host_path)
        self._write_cache(cache_path, ips)
        return ips

    domains, raw_ips = self._split_entries(entries)
    resolved = self._resolve_announced(domains)
    all_ips = raw_ips + resolved

    if domains and not resolved and self._cache_current(cache_path, source_mtime):
        # Not one name answered (a DNS outage, a broken resolver) while the
        # cache holds an answer for this very policy that merely aged out:
        # keep it — stale addresses beat none.  Left untouched, the cache
        # stays stale, so the next resolve tries again.  (The deny caches
        # need more than this — a name that fails *alone* must keep its
        # addresses too — so they resolve per name, through
        # ``resolve_names_and_cache`` with ``keep_unanswered``.)
        logger.warning("no name in %s resolved — keeping the previous answer", cache_path.name)
        return self._read_cache(cache_path)
    self._write_cache(cache_path, all_ips)
    # An all-domains-failed resolve (DNS outage, broken resolver) is fine
    # for one container but must not poison every task on the host for
    # max_age: share only when at least one domain resolved (or there were
    # none to resolve).
    if resolved or not domains:
        self._write_cache(host_path, all_ips)
    return all_ips

resolve_names_and_cache(names, cache_path, *, force=False, source_mtime=0.0, keep_unanswered=False, kind='override')

Resolve each of names on its own and cache which addresses came from which name.

The name-keyed sibling of resolve_and_cache, for a tier whose entries need more than a pool of addresses: the t10 override, where every entry carries its own expiry and an address must be timed to the expiry of the name it came from, and the two t20 deny caches. The same freshness rules hold (one hour, and never older than source_mtime), and a resolve in which not one name answered keeps a cache that merely aged out. No host-wide cache: such names are few, and a deny must never take a partial answer from another container.

keep_unanswered: a name that answers nothing this time keeps the addresses it had, where an answering name takes its new ones. For the deny caches, one lookup timing out must not drop a vault host's addresses out of the t20 set at the next rebuild — in the DOWN posture, and wherever nothing sinkholes the name, that would open the host — so a stale answer beats none. For the override cache, a lookup timing out must not end a break-glass early. An address that an answering name no longer gives is dropped either way. kind names the names in the stderr announcement.

Returns:

Type Description
dict[str, list[str]]

Each name with addresses, after keep_unanswered; a name left

dict[str, list[str]]

with none is absent.

Source code in src/terok_shield/dns/resolver.py
def resolve_names_and_cache(
    self,
    names: list[str],
    cache_path: Path,
    *,
    force: bool = False,
    source_mtime: float = 0.0,
    keep_unanswered: bool = False,
    kind: str = "override",
) -> dict[str, list[str]]:
    """Resolve each of *names* on its own and cache which addresses came from which name.

    The name-keyed sibling of
    [`resolve_and_cache`][terok_shield.dns.resolver.DnsResolver.resolve_and_cache],
    for a tier whose entries need more than a pool of addresses: the t10
    override, where every entry carries its own expiry and an address must
    be timed to the expiry of the name it came from, and the two t20 deny
    caches.  The same freshness rules hold (one hour, and never older than
    *source_mtime*), and a resolve in which not one name answered keeps a
    cache that merely aged out.  No host-wide cache: such names are few,
    and a deny must never take a partial answer from another container.

    *keep_unanswered*: a name that answers nothing this time keeps the
    addresses it had, where an answering name takes its new ones.  For
    the deny caches, one lookup timing out must not drop a vault host's
    addresses out of the t20 set at the next rebuild — in the DOWN
    posture, and wherever nothing sinkholes the name, that would open the
    host — so a stale answer beats none.  For the override cache, a
    lookup timing out must not end a break-glass early.  An address that
    an answering name no longer gives is dropped either way.
    *kind* names the names in the stderr announcement.

    Returns:
        Each name with addresses, after *keep_unanswered*; a name left
        with none is absent.
    """
    resolved = self.resolve_names(
        names,
        cache_path,
        force=force,
        source_mtime=source_mtime,
        keep_unanswered=keep_unanswered,
        kind=kind,
    )
    if resolved is None:
        cached = read_name_resolution(cache_path) or {}
        return {name: cached[name] for name in names if name in cached}
    self._write_text(cache_path, render_name_resolution(resolved))
    return resolved

resolve_names(names, cache_path, *, force=False, source_mtime=0.0, keep_unanswered=False, kind='override')

What resolve_names_and_cache would cache — without writing it.

The same rules, with cache_path only read: for a caller that may persist an answer only once something else has taken it. A live override grant resolves its names, swaps the running t10 set, and only then records the grant and caches what it seeded — a grant refused for want of an answer must leave the cache as it was.

Returns:

Type Description
dict[str, list[str]] | None

Each name with addresses, as the cache would hold them after

dict[str, list[str]] | None

keep_unanswered — or None when the cache stands as it is:

dict[str, list[str]] | None

fresh, or kept through a resolve in which not one name answered.

Source code in src/terok_shield/dns/resolver.py
def resolve_names(
    self,
    names: list[str],
    cache_path: Path,
    *,
    force: bool = False,
    source_mtime: float = 0.0,
    keep_unanswered: bool = False,
    kind: str = "override",
) -> dict[str, list[str]] | None:
    """What [`resolve_names_and_cache`][terok_shield.dns.resolver.DnsResolver.resolve_names_and_cache] would cache — without writing it.

    The same rules, with *cache_path* only read: for a caller that may
    persist an answer only once something else has taken it.  A live
    override grant resolves its names, swaps the running t10 set, and
    only then records the grant and caches what it seeded — a grant
    refused for want of an answer must leave the cache as it was.

    Returns:
        Each name with addresses, as the cache would hold them after
        *keep_unanswered* — or ``None`` when the cache stands as it is:
        fresh, or kept through a resolve in which not one name answered.
    """
    max_age = 0 if force else _CACHE_MAX_AGE
    cached = read_name_resolution(cache_path)
    if cached is not None and self._cache_fresh(cache_path, max_age, source_mtime):
        return None
    answers = self._resolve_each_announced(names, kind)
    # A cache in the flat format answers no name, yet still seeds its tier:
    # a total failure leaves it standing too, never an empty cache.
    if names and not any(answers.values()) and self._cache_current(cache_path, source_mtime):
        logger.warning("no name in %s resolved — keeping the previous answer", cache_path.name)
        return None
    if keep_unanswered and cached:
        for name in (n for n in names if not answers.get(n) and cached.get(n)):
            logger.warning(
                "%s answered nothing — %s keeps its previous addresses", name, cache_path.name
            )
            answers[name] = cached[name]
    return {name: ips for name, ips in answers.items() if ips}

resolve_domains(domains, *, quiet=False)

Resolve domain names to IPs (A + AAAA), best-effort and concurrent.

Probes for a lookup tool once, then resolves every domain on a small thread pool — a batch costs about its slowest single lookup. Unresolvable domains are skipped with a warning (a typo or a DNS failure), logged always and printed to stderr unless quiet: a background check — the security-tier refresh before a live allow — must not crowd out the one line its own caller prints there. Results are deduplicated in first-seen (input) order.

Source code in src/terok_shield/dns/resolver.py
def resolve_domains(self, domains: list[str], *, quiet: bool = False) -> list[str]:
    """Resolve domain names to IPs (A + AAAA), best-effort and concurrent.

    Probes for a lookup tool once, then resolves every domain on a small thread
    pool — a batch costs about its slowest single lookup. Unresolvable
    domains are skipped with a warning (a typo or a DNS failure), logged
    always and printed to stderr unless *quiet*: a background check — the
    security-tier refresh before a live allow — must not crowd out the
    one line its own caller prints there.  Results are deduplicated in
    first-seen (input) order.
    """
    per_domain = self.resolve_each(domains, quiet=quiet)
    return list(dict.fromkeys(ip for ips in per_domain.values() for ip in ips))

resolve_each(domains, *, quiet=False)

Resolve every domain on its own: the addresses per name, in input order.

The per-name form of resolve_domains, with the same lookup-tool probe, thread pool and warnings; a name that resolved to nothing maps to an empty list.

Source code in src/terok_shield/dns/resolver.py
def resolve_each(self, domains: list[str], *, quiet: bool = False) -> dict[str, list[str]]:
    """Resolve every domain on its own: the addresses per name, in input order.

    The per-name form of [`resolve_domains`][terok_shield.dns.resolver.DnsResolver.resolve_domains],
    with the same lookup-tool probe, thread pool and warnings; a name that
    resolved to nothing maps to an empty list.
    """
    if not domains:
        return {}
    if self._runner.has("dig") or self._runner.has("drill"):
        resolve = self._resolve_via_lookup
    else:
        logger.warning("neither dig nor drill found — using getent for DNS resolution")
        resolve = self._resolve_via_getent
    with ThreadPoolExecutor(max_workers=min(len(domains), MAX_RESOLVE_WORKERS)) as pool:
        per_domain = list(pool.map(resolve, domains))
    for domain in (d for d, ips in zip(domains, per_domain, strict=True) if not ips):
        message = f"No address for {domain}."
        logger.warning(message)
        if not quiet:
            print(message, file=sys.stderr)
    return dict(zip(domains, per_domain, strict=True))