mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-08 03:38:23 +00:00
CI job 77797371504 on run 26428080957 crashed with:
tls lookup: application_id '{{ lookup('env', 'INFINITO_DOMAIN') }}'
not found in domains mapping
`DOMAIN_PRIMARY` is defined in inventories/development/default.yml:20
as `"{{ lookup('env', 'INFINITO_DOMAIN') }}"`. On Ansible 2.18+ that
string carries a `TrustedAsTemplate` tag (it is an
`_AnsibleTaggedStr`). The constructor at tasks/stages/01_constructor.yml:64
wrapped it in `[ DOMAIN_PRIMARY ] | flatten | map('string') | first`
— the explicit `str()` cast strips the trust tag. The resulting plain
string flowed into `CURRENT_PLAY_REDIRECT_DOMAINS.source` as the
literal text `{{ lookup('env', 'INFINITO_DOMAIN') }}`, was passed
through `domain: "{{ item.source }}"` in
roles/web-opt-rdr-domains/tasks/main.yml:14 (one render pass that
produces untrusted content), and finally reached `as_str(terms[0])`
in plugins/lookup/tls.py:39 unchanged. The tls plugin never templated
its arg, so `resolve_term` looked up the literal Jinja string in the
domains mapping and raised.
Two layers, both necessary:
1. plugins/lookup/tls.py:39 — defensively template `terms[0]` through
`self._templar` before string-coercion. Mirrors the idiom in
plugins/lookup/unit_name.py:33 and plugins/lookup/email.py:61. Any
future caller that hands the plugin an unresolved expression now
gets it resolved before the domains-map lookup.
2. tasks/stages/01_constructor.yml:64-65 — drop the
`[ X ] | flatten | map('string') | first` ritual for
`DOMAIN_PRIMARY` and `DOMAIN_HOMEPAGE`. Both are scalar strings in
every known inventory (group_vars/all/00_general.yml:41,43 +
inventories/development/default.yml:20); the over-defensive
wrapper was the trust-tag stripper. Direct reference preserves the
tag through `CURRENT_PLAY_REDIRECT_DOMAINS` and every downstream
consumer.
Downstream consumers audited; all transparent to `_AnsibleTaggedStr`
(it inherits from `str`):
- merge_mapping filter: content-based dict-key compare
- domain_mappings filter: takes `DOMAIN_PRIMARY` directly, no cast
- items2dict(key_name='source', value_name='source'): hashable
- generate_all_domains filter: `isinstance(item, str)` matches
- sys-stk-front-base `when: ... | length > 0`: only counts
- web-opt-rdr-domains -> tls lookup (the failing path): now templates
Coverage: new `test_term_with_unresolved_jinja_is_templated` in
tests/unit/plugins/lookup/test_tls.py verifies the lookup feeds its
first term through the templar. The 12 existing tls tests stay
green. `make test` green across all 5 targets.
Class follow-up (not addressed here): the
`| string` / `| map('string')` cast still strips `TrustedAsTemplate`
elsewhere if a future commit reintroduces it; 12 other lookup
plugins do not yet defensively template their args. A lint guard and
plugin-wide audit will come in separate PRs.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
110 lines
3.6 KiB
Python
110 lines
3.6 KiB
Python
# STRICT TLS resolver (without SAN/cert identity planning).
|
|
#
|
|
# Certificate identity planning (SAN list, cert/key paths) is moved to:
|
|
# lookup_plugins/cert.py
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from ansible.errors import AnsibleError
|
|
from ansible.plugins.lookup import LookupBase
|
|
|
|
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,
|
|
require,
|
|
resolve_enabled,
|
|
resolve_mode,
|
|
resolve_term,
|
|
uniq_preserve,
|
|
want_get,
|
|
)
|
|
|
|
|
|
class LookupModule(LookupBase):
|
|
def run(self, terms, variables: dict | None = 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(
|
|
"tls: one or two terms required: (domain|application_id[, want_path])"
|
|
)
|
|
|
|
templar = getattr(self, "_templar", None)
|
|
term = as_str(templar.template(terms[0]) if templar else terms[0])
|
|
if not term:
|
|
raise AnsibleError("tls: 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"tls: 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="tls",
|
|
)
|
|
|
|
all_domains = collect_domains_for_app(domains, app_id, err_prefix="tls")
|
|
all_domains = (
|
|
uniq_preserve([primary_domain, *all_domains])
|
|
if all_domains
|
|
else [primary_domain]
|
|
)
|
|
|
|
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="tls")
|
|
|
|
if mode not in {"off"} | AVAILABLE_FLAVORS:
|
|
raise AnsibleError(f"tls: unsupported mode '{mode}' for app '{app_id}'")
|
|
|
|
web_protocol = "https" if enabled else "http"
|
|
websocket_protocol = "wss" if enabled else "ws"
|
|
web_port = 443 if enabled else 80
|
|
base_url = f"{web_protocol}://{primary_domain}/"
|
|
|
|
resolved: dict[str, Any] = {
|
|
"application_id": app_id,
|
|
"domain": primary_domain,
|
|
"enabled": enabled,
|
|
"mode": mode,
|
|
"domains": {"primary": primary_domain, "all": all_domains},
|
|
"protocols": {"web": web_protocol, "websocket": websocket_protocol},
|
|
"ports": {"web": web_port},
|
|
"url": {"base": base_url},
|
|
}
|
|
|
|
if want:
|
|
return [want_get(resolved, want)]
|
|
return [resolved]
|