Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make SSLContext async friendly #1448

Merged
merged 2 commits into from
Nov 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions pyoverkiz/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,23 @@ async def refresh_listener(invocation: Mapping[str, Any]) -> None:
# pylint: disable=too-many-instance-attributes, too-many-branches


def _create_local_ssl_context() -> ssl.SSLContext:
"""Create SSL context.

This method is not async-friendly and should be called from a thread
because it will load certificates from disk and do other blocking I/O.
"""
return ssl.create_default_context(
cafile=os.path.dirname(os.path.realpath(__file__)) + "/overkiz-root-ca-2048.crt"
)


# The default SSLContext objects are created at import time
# since they do blocking I/O to load certificates from disk,
# and imports should always be done before the event loop starts or in a thread.
SSL_CONTEXT_LOCAL_API = _create_local_ssl_context()


class OverkizClient:
"""Interface class for the Overkiz API"""

Expand Down Expand Up @@ -153,11 +170,7 @@ def __init__(
if verify_ssl:
# To avoid security issues while authentication to local API, we add the following authority to
# our HTTPS client trust store: https://ca.overkiz.com/overkiz-root-ca-2048.crt
self._ssl = ssl.create_default_context(
cafile=os.path.dirname(os.path.realpath(__file__))
+ "/overkiz-root-ca-2048.crt"
)

self._ssl = SSL_CONTEXT_LOCAL_API
else:
self.api_type = APIType.CLOUD

Expand Down