Skip to content

Commit e3d2a18

Browse files
Jan WasGr1N
authored andcommitted
allow to override TLS hostname
1 parent 9434077 commit e3d2a18

1 file changed

Lines changed: 17 additions & 12 deletions

File tree

pynats/client.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ def __init__(
102102
tls_cacert: Optional[str] = None,
103103
tls_client_cert: Optional[str] = None,
104104
tls_client_key: Optional[str] = None,
105+
tls_hostname: Optional[str] = None,
105106
tls_verify: bool = False,
106107
socket_timeout: float = None,
107108
socket_keepalive: bool = False,
@@ -119,6 +120,7 @@ def __init__(
119120
"tls_cacert": tls_cacert,
120121
"tls_client_cert": tls_client_cert,
121122
"tls_client_key": tls_client_key,
123+
"tls_hostname": tls_hostname,
122124
"tls_verify": tls_verify,
123125
"version": pkg_resources.get_distribution("nats-python").version,
124126
"verbose": verbose,
@@ -156,22 +158,25 @@ def _connect_tls(self) -> None:
156158
raise NATSTCPConnectionRequiredError("server disabled TLS connection")
157159

158160
ctx = ssl.create_default_context(purpose=ssl.Purpose.SERVER_AUTH)
159-
if self._conn_options["tls_verify"]:
160-
if self._conn_options["tls_cacert"] is not None:
161-
ctx.load_verify_locations(cafile=str(self._conn_options["tls_cacert"]))
162-
if (
163-
self._conn_options["tls_client_cert"] is not None
164-
and self._conn_options["tls_client_key"] is not None
165-
):
166-
ctx.load_cert_chain(
167-
certfile=str(self._conn_options["tls_client_cert"]),
168-
keyfile=str(self._conn_options["tls_client_key"]),
169-
)
170-
else:
161+
if not self._conn_options["tls_verify"]:
171162
ctx.check_hostname = False
172163
ctx.verify_mode = ssl.CERT_NONE
173164

165+
if self._conn_options["tls_cacert"] is not None:
166+
ctx.load_verify_locations(cafile=str(self._conn_options["tls_cacert"]))
167+
if (
168+
self._conn_options["tls_client_cert"] is not None
169+
or self._conn_options["tls_client_key"] is not None
170+
):
171+
ctx.load_cert_chain(
172+
certfile=str(self._conn_options["tls_client_cert"]),
173+
keyfile=str(self._conn_options["tls_client_key"]),
174+
)
175+
174176
hostname = str(self._conn_options["hostname"])
177+
if self._conn_options["tls_hostname"] is not None:
178+
hostname = str(self._conn_options["tls_hostname"])
179+
175180
self._socket = ctx.wrap_socket(self._socket, server_hostname=hostname)
176181
self._socket_file = self._socket.makefile("rb")
177182

0 commit comments

Comments
 (0)