credential_lifecycle.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. """Unified provider-credential lifecycle across every store Hermes reads.
  2. Deleting a key from ``.env`` alone leaves the stale ``credential_pool`` entry (and the
  3. ``provider_models_cache.json`` row) behind, so the provider keeps appearing in the model picker
  4. even across restarts (the pool loader is additive-only). Every surface that saves or removes a
  5. provider credential should route through :func:`save_provider_env_credential` /
  6. :func:`remove_provider_env_credential` so all stores stay consistent.
  7. """
  8. from __future__ import annotations
  9. from typing import Any, Dict, List
  10. __all__ = [
  11. "purge_env_credential_references",
  12. "remove_provider_env_credential",
  13. "save_provider_env_credential",
  14. ]
  15. def _providers_for_env_var(env_var: str) -> list[str]:
  16. """Provider ids whose registered api_key_env_vars include ``env_var``."""
  17. try:
  18. from hermes_cli.auth_plugin_providers import canonical_registry_rows
  19. rows = canonical_registry_rows()
  20. except Exception:
  21. return []
  22. hits: list[str] = []
  23. for pid, cfg in rows:
  24. try:
  25. if env_var in (cfg.api_key_env_vars or ()):
  26. hits.append(pid)
  27. except Exception:
  28. continue
  29. return hits
  30. def _for_each_provider(providers: list[str], import_path: str, *args: Any) -> None:
  31. """Best-effort ``module.fn(provider, *args)`` for every provider; failures never propagate."""
  32. try:
  33. import importlib
  34. module_name, fn_name = import_path.rsplit(".", 1)
  35. fn = getattr(importlib.import_module(module_name), fn_name)
  36. for provider in providers:
  37. fn(provider, *args)
  38. except Exception:
  39. pass
  40. def _prune_env_pool_entries(env_var: str) -> list[str]:
  41. """Drop ``credential_pool`` entries seeded from ``env:<env_var>``; return providers pruned.
  42. Spans ALL providers (shared vars like GITHUB_TOKEN seed several). Entries with any other
  43. source (OAuth, device-code, manual, borrowed-CLI) are preserved verbatim.
  44. """
  45. from hermes_cli.auth import _auth_store_lock, _load_auth_store, _save_auth_store
  46. source = f"env:{env_var}"
  47. pruned: list[str] = []
  48. with _auth_store_lock():
  49. auth_store = _load_auth_store()
  50. pool = auth_store.get("credential_pool")
  51. if not isinstance(pool, dict):
  52. return pruned
  53. for provider in list(pool.keys()):
  54. entries = pool[provider]
  55. if not isinstance(entries, list):
  56. continue
  57. kept = [e for e in entries if not (isinstance(e, dict) and e.get("source") == source)]
  58. if len(kept) == len(entries):
  59. continue
  60. pruned.append(provider)
  61. if kept:
  62. pool[provider] = kept
  63. else:
  64. del pool[provider]
  65. if pruned:
  66. _save_auth_store(auth_store)
  67. return pruned
  68. def _scrub_config_yaml_mirrors(old_value: str, new_value: str | None) -> list[str]:
  69. """Reconcile config.yaml api_key mirrors holding ``old_value``; return dotted paths touched.
  70. Value-matched on purpose: only an entry holding the SAME credential that just changed in
  71. ``.env`` is touched. ``new_value=None`` removes the field. Operates on the RAW user config
  72. so defaults are never baked into the user's file.
  73. """
  74. from hermes_cli.config_backend import config_exists
  75. if not old_value:
  76. return []
  77. from hermes_cli.config import atomic_config_replace, get_config_path, read_user_config_raw
  78. config_path = get_config_path()
  79. if not config_exists(config_path):
  80. return []
  81. try:
  82. user_config = read_user_config_raw(config_path)
  83. except Exception:
  84. return []
  85. if not user_config:
  86. return []
  87. touched: list[str] = []
  88. def _fix(section: Any, key_path: str, fields: tuple[str, ...] = ("api_key", "api")) -> None:
  89. # "api" is the legacy alias for model.api_key in older configs. In the keyed ``providers``
  90. # schema ``api`` means the base_url, not a credential, so that section passes
  91. # ``fields=("api_key",)``.
  92. if not isinstance(section, dict):
  93. return
  94. for field in fields:
  95. current = section.get(field)
  96. if isinstance(current, str) and current == old_value:
  97. if new_value:
  98. section[field] = new_value
  99. else:
  100. section.pop(field, None)
  101. touched.append(f"{key_path}.{field}")
  102. def _items(value: Any, allow_list: bool):
  103. if isinstance(value, dict):
  104. return value.items()
  105. return enumerate(value) if allow_list and isinstance(value, list) else ()
  106. _fix(user_config.get("model"), "model")
  107. for task, slot_cfg in _items(user_config.get("auxiliary"), False):
  108. _fix(slot_cfg, f"auxiliary.{task}")
  109. for name, entry in _items(user_config.get("custom_providers"), True):
  110. _fix(entry, f"custom_providers.{name}")
  111. # ``providers.<id>.api_key`` (v12+) is where dashboard/desktop write custom-endpoint
  112. # credentials. It is a real inline secret with higher precedence than the env var, so a stale
  113. # copy shadows a rotation (persistent 401 with a key the UI no longer shows) and survives a
  114. # removal that promised to clear EVERY store.
  115. for provider_id, entry in _items(user_config.get("providers"), False):
  116. _fix(entry, f"providers.{provider_id}", fields=("api_key",))
  117. if touched:
  118. atomic_config_replace(config_path, user_config)
  119. return touched
  120. def purge_env_credential_references(
  121. env_var: str, *, clear_models_cache: bool = True) -> dict[str, Any]:
  122. """Remove non-.env references to an env-var credential.
  123. Prunes env-seeded pool entries and (optionally) the affected ``provider_models_cache.json`` rows
  124. so the model picker stops advertising a provider whose key is gone.
  125. See #59761.
  126. """
  127. pruned = _prune_env_pool_entries(env_var)
  128. providers = sorted(set(pruned) | set(_providers_for_env_var(env_var)))
  129. # Make the removal sticky the same way `hermes auth remove` does: a lingering shell export (or
  130. # another live process's os.environ) would otherwise re-seed the pool entry on the next
  131. # load_pool(). The save path lifts the suppression on an explicit re-add.
  132. _for_each_provider(providers, "hermes_cli.auth.suppress_credential_source", f"env:{env_var}")
  133. if clear_models_cache and providers:
  134. # Best-effort — a cache failure must not block the credential removal itself.
  135. _for_each_provider(providers, "hermes_cli.models.clear_provider_models_cache")
  136. return {"pool_pruned": pruned, "providers": providers}
  137. def save_provider_env_credential(env_var: str, value: str) -> dict[str, Any]:
  138. """Save/update a credential in ``.env`` and reconcile every mirror.
  139. config.yaml mirrors of the PREVIOUS value are updated so a stale higher-precedence copy cannot
  140. shadow the rotation, and ``load_pool()`` runs now so the env-seeded ``credential_pool`` entry
  141. lands in ``auth.json`` (a ``.env``-only write left env-backed providers 401'ing).
  142. Suppressed ``env:<VAR>`` pool sources are re-enabled so a deliberate re-add through the UI behaves like
  143. ``hermes auth add``. See #62269.
  144. The save also forces an immediate ``load_pool()`` for every provider registered against this env var so
  145. the env-seeded ``credential_pool`` entry is materialized to ``auth.json`` right now — the live runtime
  146. reads from the pool, and before #96058 the Desktop "Save" action only touched ``.env`` while
  147. ``auth.json``'s mtime stayed unchanged, so an OpenCode Go (or any other env-backed provider) request
  148. kept 401'ing until the user ran ``hermes auth add <provider> --type api-key`` separately. This makes the
  149. Desktop save's effect on disk match what ``hermes auth add`` does.
  150. """
  151. from hermes_cli.config import load_env, require_env_writable, save_env_value
  152. # A locked key must fail here: save_env_value's refusal returns like a success, and the mirror
  153. # scrub below would still move the new value into config.yaml.
  154. require_env_writable(env_var, "set")
  155. old_value = load_env().get(env_var)
  156. save_env_value(env_var, value)
  157. config_updates: list[str] = []
  158. if value and old_value and old_value != value:
  159. config_updates = _scrub_config_yaml_mirrors(old_value, value)
  160. # A prior removal may have suppressed this env source; a fresh save is an explicit re-add.
  161. providers = _providers_for_env_var(env_var)
  162. _for_each_provider(providers, "hermes_cli.auth.unsuppress_credential_source", f"env:{env_var}")
  163. # ``load_pool`` is idempotent and additive-only for env sources, so re-running is safe even when
  164. # the pool already had this entry. Best-effort: never masks the successful .env write above.
  165. _for_each_provider(providers, "agent.credential_pool.load_pool")
  166. return {"ok": True, "key": env_var, "config_updates": config_updates}
  167. def remove_provider_env_credential(env_var: str) -> dict[str, Any]:
  168. """Remove a credential from EVERY store: ``.env`` (and process env), env-seeded
  169. ``credential_pool`` entries, model-cache rows, config.yaml mirrors of the same value."""
  170. from hermes_cli.config import load_env, remove_env_value, require_env_writable
  171. # Before the pool prune and mirror scrub: a refused remove must not strip the other stores.
  172. require_env_writable(env_var, "remove")
  173. old_value = load_env().get(env_var)
  174. removed_from_env = remove_env_value(env_var)
  175. refs = purge_env_credential_references(env_var)
  176. config_scrubbed = _scrub_config_yaml_mirrors(old_value, None) if old_value else []
  177. return {
  178. "ok": True,
  179. "key": env_var,
  180. "removed": removed_from_env,
  181. "pool_pruned": refs["pool_pruned"],
  182. "providers": refs["providers"],
  183. "config_scrubbed": config_scrubbed,
  184. "found": bool(removed_from_env or refs["pool_pruned"] or config_scrubbed)}