From 55b19c7d18ddd8adbe337d65ddd8cf2dec0caae7 Mon Sep 17 00:00:00 2001 From: OpenBot Date: Fri, 16 Oct 2026 11:12:00 +0800 Subject: [PATCH] feat(nats): TLS support (tls_ca/tls_insecure config) --- src/agenthub/channels/_nats_sync.py | 3 +++ src/agenthub/channels/nats.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/agenthub/channels/_nats_sync.py b/src/agenthub/channels/_nats_sync.py index 54afd90..9e0d430 100644 --- a/src/agenthub/channels/_nats_sync.py +++ b/src/agenthub/channels/_nats_sync.py @@ -54,6 +54,7 @@ class SyncNATS: username: str = "", password: str = "", connect_timeout: float = 10.0, + tls: Any | None = None, on_connect: ConnectCallback | None = None, on_disconnect: ConnectCallback | None = None, ) -> None: @@ -61,6 +62,7 @@ class SyncNATS: self._username = username self._password = password self._connect_timeout = connect_timeout + self._tls = tls self._on_connect = on_connect self._on_disconnect = on_disconnect @@ -119,6 +121,7 @@ class SyncNATS: user=self._username or None, password=self._password or None, connect_timeout=int(self._connect_timeout), + tls=self._tls, reconnect_time_wait=1, max_reconnect_attempts=-1, disconnected_cb=_disc, diff --git a/src/agenthub/channels/nats.py b/src/agenthub/channels/nats.py index e789c08..890721e 100644 --- a/src/agenthub/channels/nats.py +++ b/src/agenthub/channels/nats.py @@ -18,6 +18,8 @@ Config (dict): username / password: str = "" in_subject: str = "agenthub.in.>" # subscription pattern out_subject: str = "agenthub.out" # replies published to out_subject. + tls_ca: str = "" # CA cert path (enable TLS if set) + tls_insecure: bool = False # skip cert verification (dev only) allowed_users: list[str] = [] Requires the ``nats`` extra (nats-py). Reuses octop-gateway's SyncNATS @@ -63,6 +65,8 @@ class NatsChannel: self.password = config.get("password", "") self.in_subject = config.get("in_subject", "agenthub.in.>") self.out_subject = (config.get("out_subject") or "agenthub.out").rstrip(".") + self.tls_ca = config.get("tls_ca", "") + self.tls_insecure = bool(config.get("tls_insecure", False)) self.allowed_users = set(config.get("allowed_users") or []) self._nats_factory = nats_factory # injectable for tests self._nc: Any = None @@ -76,6 +80,18 @@ class NatsChannel: def start(self) -> None: from ._nats_sync import SyncNATS # vendored sync wrapper + kwargs: dict[str, Any] = {} + if self.tls_ca or self.tls_insecure: + import ssl + + ctx = ssl.create_default_context( + cafile=self.tls_ca or None + ) + if self.tls_insecure: + ctx.check_hostname = False + ctx.verify_mode = ssl.CERT_NONE + kwargs["tls"] = ctx + factory = self._nats_factory or SyncNATS self._nc = factory( self.servers, @@ -83,6 +99,7 @@ class NatsChannel: password=self.password, on_connect=lambda: self._logger.info("[nats] connected"), on_disconnect=lambda: self._logger.warning("[nats] disconnected; reconnecting"), + **kwargs, ) self._sub_id = self._nc.subscribe(self.in_subject, self._on_nats_msg) self._logger.info(f"[nats] subscribed {self.in_subject} (out: {self.out_subject}.*)")