Skip to content

Commit

Permalink
Allowing providing a custom default ssl context function
Browse files Browse the repository at this point in the history
  • Loading branch information
loopj committed Dec 24, 2024
1 parent 377ba1b commit 9d0f6aa
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/aiovantage/connection.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
"""Wrapper for an asyncio connection to a Vantage controller."""

import asyncio
from collections.abc import Callable
from ssl import CERT_NONE, SSLContext, create_default_context
from typing import ClassVar

from .errors import ClientConnectionError, ClientTimeoutError


def _get_default_context() -> SSLContext:
"""Create a default SSL context."""
# We don't have a local issuer certificate to check against, and we'll be
# connecting to an IP address so we can't check the hostname
ctx = create_default_context()
ctx.check_hostname = False
ctx.verify_mode = CERT_NONE
return ctx


class BaseConnection:
"""Wrapper for an asyncio connection to a Vantage controller."""

ssl_context_factory: ClassVar[Callable[[], SSLContext]] = _get_default_context

default_port: int
default_ssl_port: int
buffer_limit: int = 2**16
Expand All @@ -30,11 +44,7 @@ def __init__(
# Set up the SSL context
self._ssl: SSLContext | None
if ssl is True:
# We don't have a local issuer certificate to check against, and we'll be
# connecting to an IP address so we can't check the hostname
self._ssl = create_default_context()
self._ssl.check_hostname = False
self._ssl.verify_mode = CERT_NONE
self._ssl = self.ssl_context_factory()
elif isinstance(ssl, SSLContext):
self._ssl = ssl
else:
Expand Down

0 comments on commit 9d0f6aa

Please sign in to comment.