From e675d0abdad81f26e1805c3c993d46285894ae30 Mon Sep 17 00:00:00 2001 From: dcmeglio <21957250+dcmeglio@users.noreply.github.com> Date: Mon, 7 Oct 2024 21:15:25 -0400 Subject: [PATCH] fix: Fix blocking SSL calls (#43) * Add ability to pass an SSL Context. * Change the TLS method calls to pass a context * Fixed a bug not passing the tls version. * Revert version number change. --- src/pyeconet/api.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/pyeconet/api.py b/src/pyeconet/api.py index e05f440..ec3403b 100644 --- a/src/pyeconet/api.py +++ b/src/pyeconet/api.py @@ -33,6 +33,14 @@ ApiType = TypeVar("ApiType", bound="EcoNetApiInterface") +def _create_ssl_context() -> ssl.SSLContext: + """Create a SSL context for the MQTT connection.""" + context = ssl.SSLContext(ssl.PROTOCOL_TLS) + context.load_default_certs() + return context + +_SSL_CONTEXT = _create_ssl_context() + class EcoNetApiInterface: """ @@ -97,14 +105,10 @@ def subscribe(self): self._user_token, password=CLEAR_BLADE_SYSTEM_KEY ) self._mqtt_client.enable_logger() - self._mqtt_client.tls_set( - ca_certs=None, - certfile=None, - keyfile=None, - cert_reqs=ssl.CERT_REQUIRED, - tls_version=ssl.PROTOCOL_TLS, - ciphers=None, - ) + + self._mqtt_client.tls_set_context(_SSL_CONTEXT) + self._mqtt_client.tls_insecure_set(False) + self._mqtt_client.on_connect = self._on_connect self._mqtt_client.on_message = self._on_message self._mqtt_client.on_disconnect = self._on_disconnect