Files
computer-playbook/plugins/lookup/cert.py
T
kevinveenbirkenbachandClaude Opus 4.7 9c35909752 refactor(utils/cache): split data.py into base/applications/users/domains
The 934-line `utils/cache/data.py` monolith mixed cross-cutting helpers
(constants, yaml-loaders, deep-merge, signatures, render machinery)
with three independent domain caches (applications, users, domains).
Per the KISS/SPOT/DRY guidelines, split it along domain boundaries so
each file has a single, well-scoped responsibility:

  utils/cache/__init__.py     — package; owns _reset_cache_for_tests()
                                orchestrator
  utils/cache/base.py         — cross-cutting helpers: constants,
                                yaml-loaders, _deep_merge, cache-key +
                                content-fingerprint signatures, the
                                cross-domain re-entry guard, and the
                                templar-render machinery (the only
                                ansible-coupled symbol, lazy-imports
                                its dependency)
  utils/cache/applications.py — variants + defaults +
                                get_merged_applications. Strictly
                                ansible-free at import time so the
                                GitHub Actions runner-host CLI path
                                (cli.deploy.development.init →
                                plan_dev_inventory_matrix →
                                get_variants) keeps working without
                                ansible installed
  utils/cache/users.py        — user defs, token store hydration,
                                alias materialization,
                                get_user_defaults, get_merged_users
  utils/cache/domains.py      — get_merged_domains, derived from the
                                merged applications view
  utils/cache/data.py         — DELETED. Façade is gone; the package
                                exports nothing the domain modules
                                don't already

Backward-compat: the facade was deleted (per operator request) and all
36+ consumers — 18 plugins, 2 CLI entry points, 9 test files, 4 doc
files, plus a role-internal lookup_plugin — were rewired to import
directly from the owning domain module. `_reset_cache_for_tests` is
now exposed at `from utils.cache import _reset_cache_for_tests`.

Tests added (per-module, focused):
  tests/unit/utils/cache/test_base.py            (31 cases)
  tests/unit/utils/cache/test_applications.py    ( 9 cases)
  tests/unit/utils/cache/test_users_module.py    (21 cases; suffix
                                                  disambiguates from
                                                  the existing
                                                  tests/unit/plugins/
                                                  lookup/test_users.py)
  tests/unit/utils/cache/test_domains.py         ( 6 cases)

The ansible-block invariants from CI runs 24934007615 and 24935979190
are now pinned via subprocess isolation: each ansible-less import test
spawns a fresh `python3 -c '…'` with a meta_path filter denying every
`ansible*` import and asserts the import + invocation succeed in that
clean process. The earlier in-process sys.modules-manipulation shims
fragmented module identity and broke under Python 3.13 inside the
infinito container; subprocess isolation has neither problem.

Doc-style audit (per docs/contributing/documentation.md):
  - Removed em-dash from utils/cache/README.md (split into two
    sentences)
  - Updated stale `utils.runtime_data` reference in
    docs/contributing/design/matrix-deploy.md
  - Removed two SPOT self-declarations
    (docs/agents/files/group_vars/applications.md and
    docs/contributing/artefact/files/plugins/lookup/users.md) per
    the documentation rule that SPOT status is conferred by
    cross-links, not by the page asserting it about itself

Empirical proof:
  - All four domain modules import without ansible (subprocess
    isolation tests)
  - get_variants(roles_dir=ROLES_DIR) returns 99 apps in the
    ansible-blocked subprocess
  - 142 cache tests + 1085 wider-suite tests pass; the only
    remaining failures (test_host_vars free-function fixture bug,
    test_runtime_performance timing flake) are pre-existing and
    unrelated to this refactor (verified via `git stash`).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-26 00:27:57 +02:00

239 lines
7.5 KiB
Python

# lookup_plugins/cert.py
#
# Certificate planning lookup for Infinito.Nexus:
# - Computes certificate file paths (cert/key/ca)
# - Computes effective SAN list (domains.san)
# - Supports self-signed scope: "app" | "global"
#
# See utils/tls_common.py for shared resolution logic.
from __future__ import annotations
import os
from typing import Any, Dict, Optional, List
from ansible.errors import AnsibleError
from ansible.plugins.lookup import LookupBase
from utils.jinja_strict import render_strict
from utils.cache.applications import get_merged_applications
from utils.cache.domains import get_merged_domains
from utils.tls_common import (
AVAILABLE_FLAVORS,
as_str,
collect_domains_for_app,
override_san_list,
require,
resolve_enabled,
resolve_le_name,
resolve_mode,
resolve_term,
uniq_preserve,
want_get,
)
LE_FULLCHAIN = "fullchain.pem"
LE_PRIVKEY = "privkey.pem"
def _join(*parts: Any) -> str:
cleaned = [str(p).strip() for p in parts if str(p).strip()]
return os.path.join(*cleaned) if cleaned else ""
def _require_current_play_domains_all_strict(variables: dict) -> List[str]:
"""
STRICT FORMAT REQUIREMENT
CURRENT_PLAY_DOMAINS_ALL MUST:
- exist
- be list[str]
- be non-empty
- contain only non-empty strings
No fallback. No coercion. No dict support.
"""
value = variables.get("CURRENT_PLAY_DOMAINS_ALL")
if not isinstance(value, list):
raise AnsibleError(
"cert(strict): CURRENT_PLAY_DOMAINS_ALL must be of type list[str]. "
f"Got {type(value).__name__}."
)
cleaned: List[str] = []
for item in value:
if not isinstance(item, str):
raise AnsibleError(
"cert(strict): CURRENT_PLAY_DOMAINS_ALL must contain only strings."
)
item = item.strip()
if not item:
raise AnsibleError(
"cert(strict): CURRENT_PLAY_DOMAINS_ALL must not contain empty strings."
)
cleaned.append(item)
if not cleaned:
raise AnsibleError("cert(strict): CURRENT_PLAY_DOMAINS_ALL must not be empty.")
return cleaned
class LookupModule(LookupBase):
def run(self, terms, variables: Optional[dict] = None, **kwargs):
variables = variables or {}
# New API: want-path is the 2nd positional term.
# Legacy 'want=' kwarg is ignored (no error) to keep tasks noise-free.
if not terms or len(terms) not in (1, 2):
raise AnsibleError(
"cert: one or two terms required: (domain|application_id[, want_path])"
)
term = as_str(terms[0])
if not term:
raise AnsibleError("cert: term is empty")
want = as_str(terms[1]).strip() if len(terms) == 2 else ""
domains = get_merged_domains(
variables=variables,
roles_dir=kwargs.get("roles_dir"),
templar=getattr(self, "_templar", None),
)
applications = get_merged_applications(
variables=variables,
roles_dir=kwargs.get("roles_dir"),
templar=getattr(self, "_templar", None),
)
enabled_default = require(variables, "TLS_ENABLED", (bool, int))
mode_default = as_str(require(variables, "TLS_MODE", str))
if mode_default not in AVAILABLE_FLAVORS:
raise AnsibleError(
f"cert: TLS_MODE must be one of {sorted(AVAILABLE_FLAVORS)}, got '{mode_default}'"
)
forced_mode = as_str(kwargs.get("mode", "auto")).lower()
app_id, primary_domain = resolve_term(
term,
domains=domains,
applications=applications,
forced_mode=forced_mode,
err_prefix="cert",
)
app = applications.get(app_id, {})
if not isinstance(app, dict):
app = {}
enabled = resolve_enabled(app, bool(enabled_default))
mode = resolve_mode(app, enabled, mode_default, err_prefix="cert")
cert_file = ""
key_file = ""
ca_file = ""
san_domains: List[str] = []
cert_id = ""
scope = "app"
if mode == "off":
pass
elif mode == "letsencrypt":
le_live_raw = require(variables, "LETSENCRYPT_LIVE_PATH", str)
le_live = render_strict(
le_live_raw,
variables=variables,
var_name="LETSENCRYPT_LIVE_PATH",
err_prefix="cert",
)
le_name = resolve_le_name(app, primary_domain)
cert_id = le_name
cert_file = _join(le_live, le_name, LE_FULLCHAIN)
key_file = _join(le_live, le_name, LE_PRIVKEY)
all_domains = collect_domains_for_app(domains, app_id, err_prefix="cert")
all_domains = (
uniq_preserve([primary_domain] + all_domains)
if all_domains
else [primary_domain]
)
san_override = override_san_list(app)
if san_override is None:
san_domains = all_domains[:]
else:
san_domains = uniq_preserve([primary_domain] + san_override)
elif mode == "self_signed":
ss_base_raw = require(variables, "TLS_SELFSIGNED_BASE_PATH", str)
ss_base = render_strict(
ss_base_raw,
variables=variables,
var_name="TLS_SELFSIGNED_BASE_PATH",
err_prefix="cert",
)
ss_scope = as_str(variables.get("TLS_SELFSIGNED_SCOPE")).lower()
if ss_scope not in {"app", "global"}:
raise AnsibleError(
"cert: TLS_SELFSIGNED_SCOPE must be 'app' or 'global'"
)
scope = ss_scope
if ss_scope == "global":
cert_id = "global"
cert_file = _join(ss_base, cert_id, LE_FULLCHAIN)
key_file = _join(ss_base, cert_id, LE_PRIVKEY)
# STRICT: list[str] only
san_domains = _require_current_play_domains_all_strict(variables)
# Ensure primary domain is always included
if primary_domain:
san_domains = uniq_preserve([primary_domain] + san_domains)
else:
cert_id = app_id
cert_file = _join(ss_base, app_id, primary_domain, LE_FULLCHAIN)
key_file = _join(ss_base, app_id, primary_domain, LE_PRIVKEY)
all_domains = collect_domains_for_app(
domains, app_id, err_prefix="cert"
)
all_domains = (
uniq_preserve([primary_domain] + all_domains)
if all_domains
else [primary_domain]
)
san_override = override_san_list(app)
if san_override is None:
san_domains = all_domains[:]
else:
san_domains = uniq_preserve([primary_domain] + san_override)
else:
raise AnsibleError(f"cert: unsupported mode '{mode}'")
resolved: Dict[str, Any] = {
"application_id": app_id,
"domain": primary_domain,
"enabled": enabled,
"mode": mode,
"scope": scope,
"cert_id": cert_id,
"domains": {"san": san_domains},
"files": {"cert": cert_file, "key": key_file, "ca": ca_file},
}
if want:
return [want_get(resolved, want)]
return [resolved]