webhook.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. """hermes webhook — manage dynamic webhook subscriptions from the CLI."""
  2. import hashlib
  3. import hmac
  4. import json
  5. import re
  6. import secrets
  7. import threading
  8. import time
  9. import urllib.request
  10. from contextlib import contextmanager
  11. from pathlib import Path
  12. from typing import Any, Callable, Dict
  13. from hermes_constants import display_hermes_home
  14. from utils import atomic_json_write
  15. from hermes_cli.config import cfg_get
  16. _SUBSCRIPTIONS_FILENAME = "webhook_subscriptions.json"
  17. _SUBSCRIPTIONS_FILE_MODE = 0o600
  18. _SUBSCRIPTIONS_LOCK_FILENAME = ".webhook_subscriptions.lock"
  19. _SUBSCRIPTIONS_THREAD_LOCK = threading.Lock()
  20. _MISSING_SUBSCRIPTION = object()
  21. class SubscriptionMutationConflict(RuntimeError):
  22. """The route changed after a caller took the snapshot it intended to replace."""
  23. def _subscriptions_path() -> Path:
  24. from hermes_constants import get_hermes_home
  25. return get_hermes_home() / _SUBSCRIPTIONS_FILENAME
  26. def _subscriptions_lock_path() -> Path:
  27. return _subscriptions_path().with_name(_SUBSCRIPTIONS_LOCK_FILENAME)
  28. @contextmanager
  29. def _subscriptions_lock():
  30. """Serialize subscription snapshots and mutations across threads and processes."""
  31. from hermes_cli.active_sessions import _FileLock
  32. with _SUBSCRIPTIONS_THREAD_LOCK:
  33. with _FileLock(_subscriptions_lock_path()):
  34. yield
  35. def _load_subscriptions_unlocked() -> dict[str, dict]:
  36. """Read one complete store snapshot; unreadable or malformed files read as empty."""
  37. try:
  38. raw = _subscriptions_path().read_bytes()
  39. except OSError:
  40. return {}
  41. try:
  42. data = json.loads(raw.decode("utf-8-sig"))
  43. except ValueError:
  44. return {}
  45. return data if isinstance(data, dict) else {}
  46. def _load_subscriptions() -> dict[str, dict]:
  47. """Lock-free read: writers publish by atomic rename, so a reader always sees one whole
  48. snapshot, and read-only homes (no lock file can be created) keep working."""
  49. return _load_subscriptions_unlocked()
  50. def _save_subscriptions_unlocked(subs: dict[str, dict]) -> None:
  51. # The file holds per-route HMAC secrets: atomic_json_write fchmods the temp file 0o600 BEFORE the
  52. # rename (no umask window) and re-asserts the mode on the destination afterwards.
  53. atomic_json_write(_subscriptions_path(), subs, mode=_SUBSCRIPTIONS_FILE_MODE)
  54. def _mutate_subscriptions(mutate: Callable[[dict[str, dict]], Any]) -> Any:
  55. """Lock, re-read, mutate and atomically publish one complete store snapshot."""
  56. with _subscriptions_lock():
  57. subscriptions = _load_subscriptions_unlocked()
  58. result = mutate(subscriptions)
  59. _save_subscriptions_unlocked(subscriptions)
  60. return result
  61. def _replace_subscription(name: str, route: dict, expected: object) -> dict:
  62. """CAS one route so a stale update cannot recreate a removed or disabled record.
  63. A general update is not an enable operation: an explicit ``enabled: False`` on the
  64. replaced record survives (only the dashboard's dedicated enabled endpoint lifts it).
  65. Returns the route as published.
  66. """
  67. def replace(subscriptions: dict[str, dict]) -> dict:
  68. current = subscriptions.get(name, _MISSING_SUBSCRIPTION)
  69. expected_missing = expected is _MISSING_SUBSCRIPTION
  70. if (current is _MISSING_SUBSCRIPTION) != expected_missing or (
  71. not expected_missing and current != expected
  72. ):
  73. raise SubscriptionMutationConflict(
  74. f"Webhook subscription '{name}' changed concurrently; retry the operation."
  75. )
  76. published = route
  77. if isinstance(current, dict) and current.get("enabled") is False:
  78. published = {**route, "enabled": False}
  79. subscriptions[name] = published
  80. return published
  81. return _mutate_subscriptions(replace)
  82. def _get_webhook_config() -> dict:
  83. """Load webhook platform config. Returns {} if not configured."""
  84. try:
  85. from hermes_cli.config import load_config
  86. cfg = load_config()
  87. return cfg_get(cfg, "platforms", "webhook", default={})
  88. except Exception:
  89. return {}
  90. def _is_webhook_enabled() -> bool:
  91. return bool(_get_webhook_config().get("enabled"))
  92. def _get_webhook_base_url() -> str:
  93. wh = _get_webhook_config().get("extra", {})
  94. host = wh.get("host")
  95. display_host = "localhost" if not host or host in {"0.0.0.0", "::"} else host
  96. if ":" in display_host and not display_host.startswith("["):
  97. display_host = f"[{display_host}]"
  98. return f"http://{display_host}:{wh.get('port', 8644)}"
  99. def _route_url(name: str, route: dict) -> str:
  100. profile = route.get("profile", "default")
  101. prefix = f"/p/{profile}" if profile != "default" else ""
  102. return f"{_get_webhook_base_url()}{prefix}/webhooks/{name}"
  103. def _setup_hint() -> str:
  104. _dhh = display_hermes_home()
  105. return f"""
  106. Webhook platform is not enabled. To set it up:
  107. 1. Run the gateway setup wizard:
  108. hermes gateway setup
  109. 2. Or manually add to {_dhh}/config.yaml:
  110. platforms:
  111. webhook:
  112. enabled: true
  113. extra:
  114. port: 8644
  115. secret: "your-global-hmac-secret"
  116. 3. Or set environment variables in {_dhh}/.env:
  117. WEBHOOK_ENABLED=true
  118. WEBHOOK_PORT=8644
  119. WEBHOOK_SECRET=your-global-secret
  120. Then start the gateway: hermes gateway run
  121. """
  122. def webhook_command(args):
  123. """Entry point for 'hermes webhook' subcommand."""
  124. sub = getattr(args, "webhook_action", None)
  125. if not sub:
  126. print("Usage: hermes webhook {subscribe|list|remove|test}")
  127. print("Run 'hermes webhook --help' for details.")
  128. return
  129. if not _is_webhook_enabled():
  130. print(_setup_hint())
  131. return 1
  132. handler = _ACTIONS.get(sub)
  133. if handler is not None:
  134. # A refusal returns 1 so scripts can tell no route was written (#139332).
  135. return handler(args)
  136. def _cmd_subscribe(args):
  137. name = args.name.strip().lower().replace(" ", "-")
  138. if not re.match(r'^[a-z0-9][a-z0-9_-]*$', name):
  139. print(f"Error: Invalid name '{name}'. Use lowercase alphanumeric with hyphens/underscores.")
  140. return 1
  141. subs = _load_subscriptions()
  142. expected = subs.get(name, _MISSING_SUBSCRIPTION)
  143. is_update = expected is not _MISSING_SUBSCRIPTION
  144. existing = expected if isinstance(expected, dict) else {}
  145. profile_arg = getattr(args, "route_profile", None)
  146. if profile_arg is None:
  147. profile = existing.get("profile", "default")
  148. else:
  149. from hermes_cli.profiles import normalize_profile_name, profile_exists, validate_profile_name
  150. try:
  151. profile = normalize_profile_name(profile_arg)
  152. validate_profile_name(profile)
  153. except ValueError as exc:
  154. print(f"Error: {exc}")
  155. return 1
  156. if not profile_exists(profile):
  157. print(f"Error: Profile '{profile}' does not exist.")
  158. return 1
  159. existing_profile = existing.get("profile", "default")
  160. profile_changed = is_update and profile != existing_profile
  161. if profile_changed and args.secret and args.secret == existing.get("secret"):
  162. print(
  163. "Error: Rebinding a webhook route requires a new HMAC secret. "
  164. "Omit --secret to generate one automatically, or provide a different secret."
  165. )
  166. return 1
  167. secret = args.secret or (None if profile_changed else existing.get("secret")) or secrets.token_urlsafe(32)
  168. events = [e.strip() for e in args.events.split(",")] if args.events else []
  169. route = {
  170. "description": args.description or f"Agent-created subscription: {name}",
  171. "events": events,
  172. "secret": secret,
  173. "prompt": args.prompt or "",
  174. "skills": [s.strip() for s in args.skills.split(",")] if args.skills else [],
  175. "deliver": args.deliver or "log",
  176. "profile": profile,
  177. "created_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())}
  178. if getattr(args, "deliver_only", False):
  179. if getattr(args, "cron_job", ""):
  180. print(
  181. "Error: --deliver-only and --cron-job are mutually exclusive. "
  182. "--deliver-only pushes the rendered template as a message; "
  183. "--cron-job fires an existing cron job (which handles its own "
  184. "delivery)."
  185. )
  186. return 1
  187. if route["deliver"] == "log":
  188. print(
  189. "Error: --deliver-only requires --deliver to be a real target "
  190. "(telegram, discord, slack, github_comment, etc.) — not 'log'.")
  191. return 1
  192. route["deliver_only"] = True
  193. if getattr(args, "mirror_to_session", False):
  194. route["mirror_to_session"] = True
  195. cron_job = (getattr(args, "cron_job", "") or "").strip()
  196. if cron_job:
  197. # Validate the reference up-front so a typo surfaces here, not on the first inbound event.
  198. from cron.jobs import AmbiguousJobReference
  199. try:
  200. job = _resolve_route_cron_job(profile, cron_job)
  201. except AmbiguousJobReference as e:
  202. print(f"Error: {e}")
  203. return 1
  204. if job is None:
  205. print(f"Error: no cron job matches '{cron_job}'. List jobs with: hermes cron list")
  206. return 1
  207. route["cron_job"] = job["id"]
  208. script = (getattr(args, "script", "") or "").strip()
  209. if script:
  210. route["script"] = script
  211. if args.deliver_chat_id:
  212. route["deliver_extra"] = {"chat_id": args.deliver_chat_id}
  213. try:
  214. route = _replace_subscription(name, route, expected)
  215. except SubscriptionMutationConflict as exc:
  216. print(f"Error: {exc}")
  217. return 1
  218. print(f"\n {'Updated' if is_update else 'Created'} webhook subscription: {name}")
  219. print(f" URL: {_route_url(name, route)}")
  220. print(f" Profile: {profile}")
  221. print(f" Secret: {secret}")
  222. print(f" Events: {', '.join(events) or '(all)'}")
  223. print(f" Deliver: {route['deliver']}")
  224. if route.get("deliver_only"):
  225. print(" Mode: direct delivery (no agent, zero LLM cost)")
  226. if route.get("mirror_to_session"):
  227. print(" Replies: each delivery is mirrored into the target chat's session")
  228. if route.get("cron_job"):
  229. print(f" Mode: cron-job trigger — fires job '{route['cron_job']}' on each event")
  230. if route.get("prompt"):
  231. prompt_preview = route["prompt"][:80] + ("..." if len(route["prompt"]) > 80 else "")
  232. print(f" {'Message' if route.get('deliver_only') else 'Prompt'}: {prompt_preview}")
  233. if route.get("script"):
  234. print(f" Script: {route['script']}")
  235. print("\n Configure your service to POST to the URL above.")
  236. print(" Use the secret for HMAC-SHA256 signature validation.")
  237. print(" The gateway must be running to receive events (hermes gateway run).\n")
  238. def _resolve_route_cron_job(profile: str, reference: str):
  239. """Match runtime routing without changing the caller's subscription/config home."""
  240. from cron.jobs import resolve_job_ref, use_cron_store
  241. from hermes_cli.profiles import get_profile_dir
  242. # Bare/default routes execute in the ambient home, including standalone installs.
  243. if profile == "default":
  244. return resolve_job_ref(reference)
  245. with use_cron_store(get_profile_dir(profile)):
  246. return resolve_job_ref(reference)
  247. def _cmd_list(args):
  248. subs = _load_subscriptions()
  249. if not subs:
  250. print(" No dynamic webhook subscriptions.")
  251. print(" Create one with: hermes webhook subscribe <name>")
  252. return
  253. print(f"\n {len(subs)} webhook subscription(s):\n")
  254. for name, route in subs.items():
  255. events = ", ".join(route.get("events", [])) or "(all)"
  256. deliver = route.get("deliver", "log")
  257. if route.get("deliver_only"):
  258. deliver = f"{deliver} (direct — no agent)"
  259. if route.get("cron_job"):
  260. deliver = f"cron job '{route['cron_job']}'"
  261. desc = route.get("description", "")
  262. print(f" ◆ {name}")
  263. if desc:
  264. print(f" {desc}")
  265. profile = route.get("profile", "default")
  266. print(f" URL: {_route_url(name, route)}")
  267. print(f" Profile: {profile}")
  268. print(f" Events: {events}")
  269. print(f" Deliver: {deliver}")
  270. if route.get("script"):
  271. print(f" Script: {route['script']}")
  272. print()
  273. def _cmd_remove(args):
  274. name = args.name.strip().lower()
  275. def remove(subscriptions: dict[str, dict]) -> None:
  276. if name not in subscriptions:
  277. raise KeyError(name)
  278. del subscriptions[name]
  279. try:
  280. _mutate_subscriptions(remove)
  281. except KeyError:
  282. print(f" No subscription named '{name}'.")
  283. print(" Note: Static routes from config.yaml cannot be removed here.")
  284. return
  285. print(f" Removed webhook subscription: {name}")
  286. def _cmd_test(args):
  287. """Send a test POST to a webhook route."""
  288. name = args.name.strip().lower()
  289. subs = _load_subscriptions()
  290. if name not in subs:
  291. print(f" No subscription named '{name}'.")
  292. return
  293. secret = subs[name].get("secret", "")
  294. url = _route_url(name, subs[name])
  295. payload = args.payload or '{"test": true, "event_type": "test", "message": "Hello from hermes webhook test"}'
  296. sig = "sha256=" + hmac.new(secret.encode(), payload.encode(), hashlib.sha256).hexdigest()
  297. print(f" Sending test POST to {url}")
  298. try:
  299. req = urllib.request.Request(
  300. url,
  301. data=payload.encode(),
  302. headers={"Content-Type": "application/json", "X-Hub-Signature-256": sig, "X-GitHub-Event": "test"},
  303. method="POST")
  304. with urllib.request.urlopen(req, timeout=10) as resp:
  305. body = resp.read().decode()
  306. print(f" Response ({resp.status}): {body}")
  307. except Exception as e:
  308. print(f" Error: {e}")
  309. print(" Is the gateway running? (hermes gateway run)")
  310. _ACTIONS = {
  311. "subscribe": _cmd_subscribe, "add": _cmd_subscribe,
  312. "list": _cmd_list, "ls": _cmd_list,
  313. "remove": _cmd_remove, "rm": _cmd_remove,
  314. "test": _cmd_test}