onepassword.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. """1Password (`op` CLI) secret source.
  2. Users map env-var names to ``op://vault/item/field`` references in
  3. ``secrets.onepassword.env``; each is resolved with one ``op read -- <ref>``
  4. call using whatever auth the user's ``op`` already has (``OP_SERVICE_ACCOUNT_TOKEN``
  5. headless, ``OP_SESSION_*`` interactive) — Hermes never authenticates on the
  6. user's behalf, and failures never block startup. Complete pulls are cached
  7. in-process and under ``<hermes_home>/op_cache.json`` (values only; auth
  8. material is fingerprinted, never stored).
  9. """
  10. from __future__ import annotations
  11. import logging
  12. import os
  13. import shutil
  14. import subprocess # tests monkeypatch ``op.subprocess.run``; production goes through run_cli
  15. import time
  16. from pathlib import Path
  17. from typing import Dict, List, Optional, Tuple
  18. from agent.secret_sources._cache import CachedFetch, SecretCache, fingerprint as _fingerprint
  19. from agent.secret_sources.base import (
  20. ErrorKind, FetchResult, SecretSource, classify_cli_error, coerce_float,
  21. get_source_environment, is_valid_env_name, run_cli,
  22. )
  23. logger = logging.getLogger(__name__)
  24. _OP_RUN_TIMEOUT = 30
  25. # `op` itself reads OP_SERVICE_ACCOUNT_TOKEN; `service_account_token_env` lets
  26. # the user source it from another name, and _op_child_env normalizes it back.
  27. _DEFAULT_TOKEN_ENV = "OP_SERVICE_ACCOUNT_TOKEN"
  28. # Minimal allowlisted child env (never the full post-dotenv os.environ, which
  29. # holds every provider credential). OP_SESSION_* and the token are added
  30. # dynamically in _op_child_env().
  31. _OP_ENV_ALLOWLIST = (
  32. "PATH", "HOME", "USERPROFILE", "APPDATA", "LOCALAPPDATA", "SystemRoot",
  33. "TMPDIR", "TMP", "TEMP", "XDG_CONFIG_HOME", "XDG_RUNTIME_DIR", "OP_CONFIG_DIR",
  34. "OP_ACCOUNT", "OP_CONNECT_HOST", "OP_CONNECT_TOKEN",
  35. # Lets a user skip op's desktop-app integration probe (which can hang with
  36. # no timeout on a wedged desktop container) and go straight to token auth.
  37. "OP_LOAD_DESKTOP_APP_SETTINGS",
  38. )
  39. # L1 key folds in str(home_path) so a HERMES_HOME switch inside one long-lived
  40. # process (the gateway) can't return another profile's secrets. The disk key
  41. # omits home because the file already lives in that home.
  42. _CacheKey = tuple[str, str, str, str] # (auth_fp, account, home, refs_fp)
  43. _DISK_CACHE_BASENAME = "op_cache.json"
  44. def _disk_key_str(cache_key: _CacheKey) -> str:
  45. auth_fp, account, _home, refs_fp = cache_key
  46. return f"{auth_fp}|{account}|{refs_fp}"
  47. _STORE: SecretCache[_CacheKey] = SecretCache(_DISK_CACHE_BASENAME, key_serializer=_disk_key_str)
  48. _CACHE = _STORE.memory # tests flush L1 directly
  49. _MISSING_BINARY_HINT = (
  50. "Install the 1Password CLI (https://developer.1password.com/docs/cli/get-started/) "
  51. "or set secrets.onepassword.binary_path."
  52. )
  53. # First matching rule wins.
  54. _OP_ERROR_RULES = (
  55. (ErrorKind.TIMEOUT, ("timed out",)),
  56. (ErrorKind.BINARY_MISSING, ("not found on path", "not an executable", "failed to invoke")),
  57. (ErrorKind.AUTH_FAILED, ("unauthorized", "not signed in", "session expired",
  58. "authentication", "401", "403")),
  59. (ErrorKind.EMPTY_VALUE, ("empty value",)),
  60. (ErrorKind.NETWORK, ("network", "connection", "resolve host", "dns")),
  61. )
  62. def _classify_op_error(message: str) -> ErrorKind:
  63. return classify_cli_error(message, _OP_ERROR_RULES)
  64. def _validate_references(references: Optional[dict[str, str]]) -> tuple[dict[str, str], list[str]]:
  65. """``(valid_refs, warnings)``: keep valid env names bound to stripped ``op://`` strings."""
  66. valid: dict[str, str] = {}
  67. warnings: list[str] = []
  68. for name, ref in (references or {}).items():
  69. if not is_valid_env_name(name):
  70. warnings.append(f"Skipping {name!r}: not a valid env-var name")
  71. elif not isinstance(ref, str):
  72. warnings.append(f"Skipping {name!r}: reference is not a string")
  73. elif not ref.strip().startswith("op://"):
  74. warnings.append(f"Skipping {name!r}: {ref!r} is not an op:// secret reference")
  75. else:
  76. valid[name] = ref.strip()
  77. return valid, warnings
  78. def _auth_fingerprint(token_env: str) -> str:
  79. """SHA-256 prefix over everything `op` would authenticate with (token, account,
  80. Connect host/token, ``OP_SESSION_*``), so a new identity never sees old cached values."""
  81. source_env = get_source_environment()
  82. parts: list[str] = [f"{label}={source_env.get(var, '')}" for label, var in (
  83. ("token", token_env), ("account", "OP_ACCOUNT"),
  84. ("connect_host", "OP_CONNECT_HOST"), ("connect_token", "OP_CONNECT_TOKEN"))]
  85. parts += [f"{key}={source_env[key]}" for key in sorted(source_env) if key.startswith("OP_SESSION_")]
  86. return _fingerprint("\n".join(parts))
  87. def _refs_fingerprint(references: dict[str, str]) -> str:
  88. return _fingerprint("\n".join(f"{name}={references[name]}" for name in sorted(references)))
  89. def find_op(binary_path: str = "") -> Optional[Path]:
  90. """Resolve a usable ``op`` binary, or None. A pinned ``binary_path`` is used
  91. verbatim — pinned-but-missing returns None rather than falling back to PATH."""
  92. found = binary_path or shutil.which("op")
  93. if not found or (binary_path and not os.access(binary_path, os.X_OK)):
  94. return None
  95. return Path(found)
  96. def _scrub(text: str) -> str:
  97. """Full ECMA-48 ANSI strip (so a control sequence can't hide text after a redaction marker) + trim."""
  98. from tools.ansi_strip import strip_ansi
  99. return strip_ansi(text).replace("\x1b", "").strip()
  100. def _op_child_env(token_value: str) -> dict[str, str]:
  101. source_env = get_source_environment()
  102. env = {k: source_env[k] for k in _OP_ENV_ALLOWLIST if k in source_env}
  103. env.update((k, v) for k, v in source_env.items() if k.startswith("OP_SESSION_"))
  104. if token_value:
  105. env["OP_SERVICE_ACCOUNT_TOKEN"] = token_value
  106. env["NO_COLOR"] = "1"
  107. return env
  108. def _run_op_read(op: Path, reference: str, *, account: str = "", token_value: str = "") -> str:
  109. """Resolve one ``op://`` reference; raises ``RuntimeError`` on any failure, including
  110. an exit-0 empty value (applying it would clobber a good credential with ``""``)."""
  111. cmd: list[str] = [str(op), "read"]
  112. if account:
  113. cmd += ["--account", account]
  114. cmd += ["--", reference] # `--` so a reference can never parse as an op flag
  115. proc = run_cli(cmd, env=_op_child_env(token_value), timeout=_OP_RUN_TIMEOUT, label="op",
  116. timeout_message=f"op read timed out after {_OP_RUN_TIMEOUT}s for {reference!r}", stdin=None)
  117. if proc.returncode != 0:
  118. err = _scrub(proc.stderr or "")[:200]
  119. if err:
  120. raise RuntimeError(f"op read failed for {reference!r}: {err}")
  121. raise RuntimeError(f"op read exited {proc.returncode} for {reference!r}")
  122. # Strip only op's trailing newline so intentional edge spaces survive.
  123. value = (proc.stdout or "").rstrip("\r\n")
  124. if not value.strip():
  125. raise RuntimeError(f"op read returned an empty value for {reference!r}")
  126. return value
  127. def fetch_onepassword_secrets(
  128. *, references: dict[str, str], account: str = "", token_env: str = _DEFAULT_TOKEN_ENV,
  129. binary: Optional[Path] = None, binary_path: str = "", use_cache: bool = True,
  130. cache_ttl_seconds: float = 300, home_path: Optional[Path] = None,
  131. ) -> tuple[dict[str, str], list[str]]:
  132. """Resolve ``references`` (name → ``op://…``) to ``(secrets, warnings)``.
  133. Raises ``RuntimeError`` only when no ``op`` binary is available; per-ref
  134. failures become warnings. Only a complete, error-free pull is cached, so a
  135. transient auth failure isn't frozen in for the whole TTL window.
  136. """
  137. valid, warnings = _validate_references(references)
  138. if not valid:
  139. return {}, warnings
  140. token_value = get_source_environment().get(token_env, "").strip()
  141. cache_key: _CacheKey = (_auth_fingerprint(token_env), account or "",
  142. str(home_path) if home_path is not None else "", _refs_fingerprint(valid))
  143. if use_cache:
  144. cached = _STORE.lookup(cache_key, cache_ttl_seconds, home_path)
  145. if cached is not None:
  146. return dict(cached.secrets), warnings
  147. op = binary or find_op(binary_path)
  148. if op is None:
  149. raise RuntimeError("op CLI not found. Install the 1Password CLI "
  150. "(https://developer.1password.com/docs/cli/get-started/) or set "
  151. "secrets.onepassword.binary_path to its absolute location.")
  152. secrets: dict[str, str] = {}
  153. read_errors = 0
  154. for name in sorted(valid):
  155. try:
  156. secrets[name] = _run_op_read(op, valid[name], account=account, token_value=token_value)
  157. except RuntimeError as exc:
  158. warnings.append(str(exc))
  159. read_errors += 1
  160. if use_cache and not read_errors and secrets:
  161. _STORE.store(cache_key, CachedFetch(secrets=dict(secrets), fetched_at=time.time()),
  162. cache_ttl_seconds, home_path)
  163. return secrets, warnings
  164. def _missing_binary_error(binary_path: str) -> str:
  165. if binary_path:
  166. return f"secrets.onepassword.binary_path ({binary_path!r}) is not an executable op binary."
  167. return ("secrets.onepassword.enabled is true but the op CLI was not found on PATH. Install it "
  168. "(https://developer.1password.com/docs/cli/get-started/) or set secrets.onepassword.binary_path.")
  169. def apply_onepassword_secrets(
  170. *, enabled: bool, env: Optional[dict[str, str]] = None, account: str = "",
  171. service_account_token_env: str = _DEFAULT_TOKEN_ENV, binary_path: str = "",
  172. override_existing: bool = True, cache_ttl_seconds: float = 300, home_path: Optional[Path] = None,
  173. ) -> FetchResult:
  174. """Resolve configured ``op://`` references and set them on ``os.environ``
  175. (``hermes secrets onepassword sync --apply``). Never raises. Refs already
  176. satisfied by the env (when ``override_existing`` is false) and the token var
  177. are skipped *before* fetching, so ``op`` never runs for a discarded value."""
  178. result = FetchResult()
  179. if not enabled:
  180. return result
  181. valid, warnings = _validate_references(env)
  182. result.warnings.extend(warnings)
  183. def _guarded(name: str) -> bool:
  184. """True when ``name`` must not be applied (token var or env already set)."""
  185. return name == service_account_token_env or (not override_existing and bool(os.environ.get(name)))
  186. result.skipped.extend(n for n in valid if _guarded(n))
  187. refs_to_fetch = {n: ref for n, ref in valid.items() if not _guarded(n)}
  188. if not refs_to_fetch:
  189. return result
  190. binary = find_op(binary_path)
  191. result.binary_path = binary
  192. if binary is None:
  193. result.error = _missing_binary_error(binary_path)
  194. return result
  195. try:
  196. secrets, fetch_warnings = fetch_onepassword_secrets(
  197. references=refs_to_fetch, account=account, token_env=service_account_token_env,
  198. binary=binary, cache_ttl_seconds=cache_ttl_seconds, home_path=home_path)
  199. except RuntimeError as exc:
  200. result.error = str(exc)
  201. return result
  202. result.secrets = secrets
  203. result.warnings.extend(fetch_warnings)
  204. for name, value in secrets.items():
  205. if _guarded(name): # defensive re-check: keys should already be ⊆ refs_to_fetch
  206. if name not in result.skipped:
  207. result.skipped.append(name)
  208. continue
  209. os.environ[name] = value
  210. result.applied.append(name)
  211. return result
  212. class OnePasswordSource(SecretSource):
  213. """1Password as a registered **mapped** source (explicit per-var bindings, so
  214. its claims outrank bulk sources on contested vars)."""
  215. name = "onepassword"
  216. label = "1Password"
  217. shape = "mapped"
  218. scheme = "op"
  219. token_env_key = "service_account_token_env"
  220. default_token_env = _DEFAULT_TOKEN_ENV
  221. # override_existing defaults True: an explicit VAR→op:// binding is the
  222. # strongest user intent; a stale .env line must not silently defeat it.
  223. override_existing_default = True
  224. _AUTH_HINT = ("Run `hermes secrets onepassword token` to paste a fresh service-account token "
  225. "({token_env}), or `op signin` for an interactive session.")
  226. remediation_hints = {ErrorKind.AUTH_FAILED: _AUTH_HINT, ErrorKind.AUTH_EXPIRED: _AUTH_HINT,
  227. ErrorKind.BINARY_MISSING: _MISSING_BINARY_HINT}
  228. def config_schema(self) -> dict:
  229. return {
  230. "enabled": {"description": "Master switch", "default": False},
  231. "env": {"description": "Map of ENV_VAR -> op://vault/item/field reference", "default": {}},
  232. "account": {"description": "op --account shorthand (empty = default account)", "default": ""},
  233. "service_account_token_env": {"description": "Env var holding the service-account token "
  234. "(unset = desktop/interactive session)",
  235. "default": _DEFAULT_TOKEN_ENV},
  236. "binary_path": {"description": "Pin the op binary (empty = resolve via PATH)", "default": ""},
  237. "cache_ttl_seconds": {"description": "Disk+memory cache TTL; 0 disables", "default": 300},
  238. "override_existing": {"description": "Resolved values overwrite .env/shell values", "default": True},
  239. }
  240. def fetch(self, cfg: dict, home_path: Path) -> FetchResult:
  241. cfg = cfg if isinstance(cfg, dict) else {}
  242. result = FetchResult()
  243. env_map = cfg.get("env")
  244. valid, warnings = _validate_references(env_map if isinstance(env_map, dict) else None)
  245. result.warnings.extend(warnings)
  246. if not valid:
  247. if not warnings:
  248. result.fail("secrets.onepassword.enabled is true but the env: map is "
  249. "empty. Add ENV_VAR: op://vault/item/field entries.", ErrorKind.NOT_CONFIGURED)
  250. return result
  251. binary_path = str(cfg.get("binary_path") or "")
  252. binary = find_op(binary_path)
  253. result.binary_path = binary
  254. if binary is None:
  255. return result.fail(_missing_binary_error(binary_path), ErrorKind.BINARY_MISSING)
  256. try:
  257. secrets, fetch_warnings = fetch_onepassword_secrets(
  258. references=valid, account=str(cfg.get("account") or ""), token_env=self.token_env(cfg),
  259. binary=binary, cache_ttl_seconds=coerce_float(cfg.get("cache_ttl_seconds", 300), 300.0),
  260. home_path=home_path)
  261. except RuntimeError as exc:
  262. return result.fail(str(exc), _classify_op_error(str(exc)))
  263. result.secrets = secrets
  264. result.warnings.extend(fetch_warnings)
  265. return result
  266. def clear_caches(home_path: Optional[Path] = None) -> None:
  267. """Drop in-process AND disk caches (after a token rotation, so the next
  268. startup resolves fresh instead of serving values cached under the old token)."""
  269. _STORE.clear(home_path)
  270. _reset_cache_for_tests = clear_caches