_shared.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. """Cross-adapter helpers shared by gateway/platforms/* and plugins/platforms/*.
  2. Kept dependency-light (stdlib + ``agent.secret_scope``) so every adapter can
  3. import it at module top level without cycles.
  4. """
  5. from __future__ import annotations
  6. import contextlib
  7. import json
  8. import logging
  9. import os
  10. from typing import Any, Callable, Iterable, Optional
  11. # Profile-scoped secret reader for multiplexing support (PR #50094)
  12. from agent.secret_scope import UnscopedSecretError as _UnscopedSecretError
  13. from agent.secret_scope import get_secret as _scoped_get_secret
  14. logger = logging.getLogger(__name__)
  15. def get_scoped_secret(name: str, default: Any = None, *, external_fallback: bool = False) -> Any:
  16. """Scope-aware credential read with the default-profile startup fallback.
  17. An installed profile secret scope is authoritative: a scoped miss returns
  18. ``default`` (never borrow another profile's value from ``os.environ``).
  19. The DEFAULT profile constructs and sends *unscoped* under multiplexing,
  20. where a bare ``get_secret`` raises ``UnscopedSecretError``; there
  21. ``os.environ`` is that profile's own value, so fall back to it.
  22. ``external_fallback`` adds one rung for startup gates that run before any
  23. scope exists: an unscoped miss consults a one-shot build of the profile's
  24. own secret scope, so externally managed credentials (Bitwarden etc., only
  25. ``BWS_ACCESS_TOKEN`` in ``.env``) are visible to ``check_requirements``.
  26. """
  27. try:
  28. val = _scoped_get_secret(name, None)
  29. except _UnscopedSecretError:
  30. val = os.getenv(name)
  31. if val is None and external_fallback and _current_scope() is None:
  32. val = _unscoped_profile_secrets().get(name)
  33. return val if val is not None else default
  34. def _current_scope():
  35. from agent.secret_scope import current_secret_scope
  36. return current_secret_scope()
  37. _UNSCOPED_PROFILE_SECRETS: Optional[dict] = None
  38. def _unscoped_profile_secrets() -> dict:
  39. """Process-cached profile secret mapping (external resolvers are slow); failures degrade to {}."""
  40. global _UNSCOPED_PROFILE_SECRETS
  41. if _UNSCOPED_PROFILE_SECRETS is None:
  42. try:
  43. from agent.secret_scope import build_profile_secret_scope
  44. from hermes_constants import get_hermes_home
  45. _UNSCOPED_PROFILE_SECRETS = dict(build_profile_secret_scope(get_hermes_home()))
  46. except Exception:
  47. logger.warning(
  48. "Could not build the profile secret scope; externally managed credentials will not be "
  49. "visible to the startup gate (#95216)", exc_info=True)
  50. _UNSCOPED_PROFILE_SECRETS = {}
  51. return _UNSCOPED_PROFILE_SECRETS
  52. def platform_gate_env(name: str, default: str = "") -> str:
  53. """Allow/deny gate env read with per-profile isolation, always stripped.
  54. With a profile secret scope installed AND multiplexing active, a scoped miss returns ``default``
  55. instead of falling through to ``os.environ``, which may hold ANOTHER profile's first-writer
  56. bridged value (the YAML→env bridges are first-writer-wins; allowlist leak, #72348).
  57. Single-profile deployments behave exactly like ``os.getenv``.
  58. """
  59. if not name:
  60. return default
  61. with contextlib.suppress(Exception):
  62. from agent.secret_scope import current_secret_scope, is_multiplex_active
  63. scope = current_secret_scope()
  64. if scope is not None and is_multiplex_active():
  65. val = scope.get(name)
  66. return default if val is None else str(val).strip()
  67. return (os.getenv(name) or default).strip()
  68. def decode_json_list_literal(raw):
  69. """Decode a JSON-encoded allowlist written by ``hermes config set``.
  70. String-typed defaults keep list literals verbatim on write (``allowed_chats`` is
  71. declared as ``""``), so the config can hold ``'["-100","-200"]'`` as a string.
  72. Malformed JSON passes through unchanged and keeps the legacy comma-split path.
  73. """
  74. if isinstance(raw, str) and raw.lstrip()[:1] == "[":
  75. try:
  76. loaded = json.loads(raw)
  77. except ValueError:
  78. return raw
  79. if isinstance(loaded, list):
  80. return loaded
  81. return raw
  82. def extra_or_secret(extra: Optional[dict], key: str, env: str, default: Any = "",
  83. *, blank_is_unset: bool = True) -> Any:
  84. """The ONE per-profile setting reader: explicit env ``env`` → the profile's YAML
  85. ``config.extra[key]`` → ``default``.
  86. The env rung is the owning profile's, read through ``get_scoped_secret``: a secondary
  87. multiplex profile sees its own ``.env`` and a miss falls to ITS YAML, never to the launch
  88. process's ``os.environ`` (which holds the default profile's bridged values); single-profile
  89. and default-profile installs read ``os.environ`` there, keeping the documented env-over-YAML
  90. contract (an explicit ``DISCORD_ALLOW_MENTION_EVERYONE=false`` beats ``everyone: true``,
  91. #108440; ``TELEGRAM_REACTIONS=true`` beats the stock ``reactions: false``, #109032). A blank
  92. env value is unset. An explicit ``False``/``0`` in YAML is a real value (``require_mention:
  93. false`` must not fall to ``default``). A blank YAML string is unset by default; readers whose
  94. YAML key means "clear it" (``allowed_channels: "" `` = no whitelist) pass
  95. ``blank_is_unset=False`` so only a missing/``None`` key falls through.
  96. """
  97. if env:
  98. env_value = get_scoped_secret(env, None)
  99. if env_value is not None and str(env_value).strip():
  100. return env_value
  101. value = (extra or {}).get(key)
  102. if value is None or (blank_is_unset and isinstance(value, str) and not value.strip()):
  103. return default
  104. return value
  105. def profile_scoped() -> bool:
  106. """True when running inside a multiplexed secondary profile's scope.
  107. Secondary-profile adapters are constructed/connected inside
  108. ``_profile_runtime_scope`` (secret scope installed + multiplex active).
  109. The DEFAULT profile under multiplexing runs unscoped and keeps the legacy
  110. ``os.environ`` precedence, so YAML->env bridges must skip only when True.
  111. """
  112. try:
  113. from agent.secret_scope import current_secret_scope, is_multiplex_active
  114. return bool(is_multiplex_active() and current_secret_scope() is not None)
  115. except Exception:
  116. return False
  117. # --------------------------------------------------------------------------- YAML → env config bridge
  118. # (apply_yaml_config_fn, #25443)
  119. # ---------------------------------------------------------------------------
  120. def yaml_env_setter() -> Callable[[str, Any], None]:
  121. """``set_env(name, value)`` for ``apply_yaml_config_fn`` hooks: writes ``os.environ[name]`` only
  122. when the var is unset (explicit env wins over YAML) and NEVER while a multiplexed secondary
  123. profile's scope is active — the gateway loads every secondary's config inside
  124. ``_profile_runtime_scope``, so a write there would pin that profile's policy process-wide and the
  125. default profile's adapters would read it as their own (first-writer-wins poisoning, #80099).
  126. Hooks seed the same values into the returned ``extra`` so each profile's adapter reads its own.
  127. Lists are comma-joined; ``None`` is skipped.
  128. """
  129. skip = profile_scoped()
  130. def set_env(name: str, value: Any) -> None:
  131. if value is None or skip or os.getenv(name):
  132. return
  133. os.environ[name] = ",".join(str(v) for v in value) if isinstance(value, list) else str(value)
  134. return set_env
  135. def send_error(message: Any) -> dict:
  136. """Standalone-sender failure envelope with vendor exception text redacted (the same helper
  137. ``send_message`` uses), so a token or signed URL in an httpx/aiohttp error never reaches the
  138. model transcript."""
  139. from tools.send_message_senders import _error
  140. return _error(str(message))
  141. # kind -> (applies-when predicate over (cfg, key), env encoder). "lower"/"json" bridge whenever the key
  142. # is present (YAML ``none`` still writes "none"); "str" skips null/blank, "csv" skips null and leaves
  143. # lists to the setter's comma join.
  144. _YAML_KINDS: dict[str, tuple[Callable[[dict, str], bool], Callable[[Any], Any]]] = {
  145. "lower": (lambda cfg, key: key in cfg, lambda v: str(v).lower()),
  146. "str": (lambda cfg, key: cfg.get(key) not in (None, ""), str),
  147. "csv": (lambda cfg, key: cfg.get(key) is not None, lambda v: v),
  148. "json": (lambda cfg, key: key in cfg, json.dumps),
  149. }
  150. def apply_yaml_bridge(cfg: dict, spec: Iterable[tuple[str, str, str]]) -> dict | None:
  151. """Table-driven ``apply_yaml_config_fn`` body: for each ``(yaml_key, ENV_VAR, kind)`` row seed the
  152. original YAML value into the returned ``extra`` and bridge it to env through ``yaml_env_setter``
  153. (env wins; skipped under a secondary profile's scope). ``None`` when nothing matched.
  154. """
  155. set_env = yaml_env_setter()
  156. seeded: dict = {}
  157. for key, env, kind in spec:
  158. applies, encode = _YAML_KINDS[kind]
  159. if applies(cfg, key):
  160. seeded[key] = cfg[key]
  161. set_env(env, encode(cfg[key]))
  162. return seeded or None
  163. # --------------------------------------------------------------------------- env → extra seeding
  164. # (env_enablement_fn)
  165. # ---------------------------------------------------------------------------
  166. def seed_extra_from_env(spec: Iterable[tuple[str, str, Callable[[str], Any] | None]], *,
  167. home_env: str | None = None, home_default: str = "") -> dict:
  168. """Table-driven ``env_enablement_fn`` body: for each ``(ENV_VAR, extra_key, conv)`` row read the
  169. profile-scoped value, skip it when blank or when ``conv`` raises ``ValueError``, else seed
  170. ``conv(value)`` (``None`` conv keeps the stripped string).
  171. ``home_env`` seeds ``home_channel`` as ``{"chat_id", "name"}`` from ``<home_env>`` (else
  172. ``home_default``) with ``<home_env>_NAME`` naming it — the core hook lifts that dict into a
  173. ``HomeChannel``. The name falls back to the literal ``"Home"``, the same rule the built-in
  174. platforms use.
  175. """
  176. seed: dict = {}
  177. for env, key, conv in spec:
  178. raw = str(get_scoped_secret(env, "") or "").strip()
  179. if not raw:
  180. continue
  181. with contextlib.suppress(ValueError):
  182. seed[key] = conv(raw) if conv else raw
  183. if home_env:
  184. home = str(get_scoped_secret(home_env, "") or "").strip() or home_default
  185. if home:
  186. seed["home_channel"] = {"chat_id": home, "name": get_scoped_secret(f"{home_env}_NAME", "Home")}
  187. return seed
  188. def env_is_connected(*names: str) -> Callable[[Any], bool]:
  189. """``is_connected`` for platforms whose only configuration is env: True when every ``names`` var is
  190. non-blank. Resolves ``hermes_cli.gateway.get_env_value`` at call time (scope-aware, .env-backed) so
  191. setup-status tests that patch it see the same value."""
  192. def is_connected(config: Any) -> bool:
  193. import hermes_cli.gateway as gateway_mod
  194. return all((gateway_mod.get_env_value(name) or "").strip() for name in names)
  195. return is_connected
  196. def config_connected_extra(*keys: str) -> Callable[[Any], bool]:
  197. """``PlatformEntry.config_connected_fn``: connected when the merged PlatformConfig carries a
  198. token/api_key or every ``extra`` key (no env fallback; that is ``is_connected``'s job)."""
  199. def config_connected(config: Any) -> bool:
  200. return bool(config.token or config.api_key) or all(config.extra.get(k) for k in keys)
  201. return config_connected
  202. def coerce_port(value: Any, default: int) -> int:
  203. """``int(value)`` or ``default`` when unparseable (incl. non-finite YAML like ``.inf``)."""
  204. try:
  205. return int(value)
  206. except (TypeError, ValueError, OverflowError):
  207. return default