command_token_source.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. """Mint a provider API key by running a command (``key_cmd``).
  2. Enterprise gateways (SSO/OIDC brokers, cloud IAM, auth proxies) issue SHORT-LIVED bearers; a key
  3. copied into ``.env`` goes stale within the hour. ``key_cmd`` names a command that PRINTS a token
  4. (the ``apiKeyHelper`` / ``gcloud auth print-access-token`` idiom). Both wire clients accept a
  5. callable API key and invoke it per request; the token is cached until shortly before expiry.
  6. Output contract: ONLY the token on stdout, bare or as JSON with an ``access_token`` field
  7. (``expires_in`` / ISO ``expiry`` honoured). Precedence: explicit ``--api-key`` wins (one-off
  8. recovery escape hatch); otherwise ``key_cmd`` beats a static ``api_key`` / ``key_env``.
  9. """
  10. from __future__ import annotations
  11. import json
  12. import logging
  13. import subprocess
  14. import threading
  15. import time
  16. from typing import Optional
  17. from agent.credential_source import KEY_CMD
  18. logger = logging.getLogger(__name__)
  19. # Treat a token as spent slightly before expiry so a request can't be signed with one that dies in
  20. # flight (60s = usual OAuth cache leeway).
  21. _TOKEN_REFRESH_LEEWAY_SECONDS = 60.0
  22. # Helpers answer from a local cache in milliseconds; this long means hung.
  23. _MINT_TIMEOUT_SECONDS = 15
  24. # No advertised expiry: nothing in the request path re-mints on 401 (the SDK retries 429/5xx only), so
  25. # a process-lifetime cache would 401 forever once the token died. Re-mint on a bounded window instead.
  26. _NO_TTL_REFRESH_SECONDS = 900.0
  27. class CommandTokenError(RuntimeError):
  28. """A ``key_cmd`` failed to produce a usable token."""
  29. def materialize_probe_api_key(api_key: object) -> str:
  30. """Best-effort probe credential; never send a callable's repr or log mint errors."""
  31. try:
  32. token = api_key() if callable(api_key) else api_key
  33. except Exception:
  34. return ""
  35. return token.strip() if isinstance(token, str) else ""
  36. def _mint(command: str, label: str) -> tuple[str, Optional[float]]:
  37. """Run *command*, returning ``(token, ttl_seconds_or_None)``. The helper runs FOR the profile whose
  38. provider is being minted: it gets that profile's own env (secrets + HERMES_HOME), never the multiplexer's
  39. launch environ — an ``op read`` / ``vault kv get`` helper must sign in as the served profile."""
  40. from tools.environments.local import served_profile_child_env
  41. try:
  42. completed = subprocess.run(
  43. command, stdin=subprocess.DEVNULL, shell=True, capture_output=True, text=True, errors="replace", timeout=_MINT_TIMEOUT_SECONDS,
  44. env=served_profile_child_env(inherit_credentials=True),
  45. check=False,
  46. )
  47. except subprocess.TimeoutExpired as exc:
  48. raise CommandTokenError(
  49. f"key_cmd for provider {label!r} timed out after {_MINT_TIMEOUT_SECONDS}s"
  50. ) from exc
  51. except OSError as exc:
  52. raise CommandTokenError(f"key_cmd for provider {label!r} could not be executed: {exc}") from exc
  53. if completed.returncode != 0:
  54. # NEVER include stdout/stderr (may hold a token) or the command string (may embed
  55. # `--client-secret=…`); name the provider instead.
  56. raise CommandTokenError(
  57. f"key_cmd for provider {label!r} exited {completed.returncode}. "
  58. f"Run that provider's key_cmd manually to see why "
  59. f"(e.g. `databricks auth login` if its OAuth session expired)."
  60. )
  61. stdout = completed.stdout or ""
  62. if not stdout.strip():
  63. raise CommandTokenError(f"key_cmd for provider {label!r} produced no output")
  64. # JSON payload — the shape `databricks auth token --output json` prints.
  65. if stdout.lstrip().startswith("{"):
  66. try:
  67. payload = json.loads(stdout)
  68. except json.JSONDecodeError:
  69. payload = None
  70. if isinstance(payload, dict):
  71. token = str(payload.get("access_token") or "").strip()
  72. if not token:
  73. raise CommandTokenError(
  74. f"key_cmd for provider {label!r} returned JSON without an 'access_token' field"
  75. )
  76. ttl = payload.get("expires_in")
  77. if isinstance(ttl, (int, float)) and ttl > 0:
  78. return token, float(ttl)
  79. # CLI helpers often print an absolute ISO 8601 deadline instead of OAuth's relative
  80. # lifetime; honour it or the token 401s once past. Lazy import: hermes_cli.auth imports agent.*.
  81. from hermes_cli.auth import _parse_iso_timestamp
  82. for field in ("expiry", "expiresOn"):
  83. deadline = _parse_iso_timestamp(payload.get(field))
  84. remaining = deadline - time.time() if deadline is not None else 0
  85. if remaining > 0:
  86. return token, remaining
  87. return token, None
  88. # Bare token: stdout carries the token and nothing else. Do NOT keep one line of several — that
  89. # turns a misconfigured helper (banner, warning) into a corrupt-key 401 far harder to diagnose.
  90. token = stdout.strip()
  91. if "\n" in token:
  92. raise CommandTokenError(
  93. f"key_cmd for provider {label!r} printed multiple lines; it must "
  94. "print only the token (or JSON with an 'access_token' field)"
  95. )
  96. return token, None
  97. class CommandTokenSource:
  98. """Callable returning a bearer token, cached until shortly before expiry."""
  99. hermes_credential_source = KEY_CMD # display label source; read without calling (agent.credential_source)
  100. def __init__(self, command: str, label: str = "custom") -> None:
  101. self._command = command
  102. self._label = label or "custom"
  103. self._lock = threading.Lock()
  104. self._token = ""
  105. self._expires_at: float = 0.0
  106. @property
  107. def cache_identity(self) -> str:
  108. """Stable catalog identity; token rotation must not mint on cache reads."""
  109. return f"cmd:{self._command}"
  110. def __call__(self) -> str:
  111. with self._lock:
  112. if self._token and time.monotonic() < self._expires_at:
  113. return self._token
  114. token, ttl = _mint(self._command, self._label)
  115. self._token = token
  116. self._expires_at = time.monotonic() + (
  117. max(ttl - _TOKEN_REFRESH_LEEWAY_SECONDS, 5.0) if ttl else _NO_TTL_REFRESH_SECONDS
  118. )
  119. logger.debug(
  120. "Minted key_cmd token for provider %s (ttl=%s)",
  121. self._label, f"{int(ttl)}s" if ttl else "unknown",
  122. )
  123. return token
  124. def build_command_token_provider(key_cmd: str, provider_label: str = "custom") -> Optional[CommandTokenSource]:
  125. """A per-request token provider for *key_cmd*, or ``None`` when unset."""
  126. command = str(key_cmd or "").strip()
  127. return CommandTokenSource(command, provider_label) if command else None