Skip to content

Commit 74ca8be

Browse files
committed
fix _ssl and has_sni
1 parent f6b17dd commit 74ca8be

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

pymongo/pool_shared.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
)
3939
from pymongo.network_layer import AsyncNetworkingInterface, NetworkingInterface, PyMongoProtocol
4040
from pymongo.pool_options import PoolOptions
41-
from pymongo.ssl_support import HAS_SNI, PYSSLError, SSLError
41+
from pymongo.ssl_support import PYSSLError, SSLError, _has_sni
4242

4343
SSLErrors = (PYSSLError, SSLError)
4444
if TYPE_CHECKING:
@@ -280,7 +280,7 @@ async def _async_configured_socket(
280280
try:
281281
# We have to pass hostname / ip address to wrap_socket
282282
# to use SSLContext.check_hostname.
283-
if HAS_SNI:
283+
if _has_sni(False):
284284
loop = asyncio.get_running_loop()
285285
ssl_sock = await loop.run_in_executor(
286286
None,
@@ -459,7 +459,7 @@ def _configured_socket(address: _Address, options: PoolOptions) -> Union[socket.
459459
try:
460460
# We have to pass hostname / ip address to wrap_socket
461461
# to use SSLContext.check_hostname.
462-
if HAS_SNI:
462+
if _has_sni(True):
463463
ssl_sock = ssl_context.wrap_socket(sock, server_hostname=host) # type: ignore[assignment, misc, unused-ignore]
464464
else:
465465
ssl_sock = ssl_context.wrap_socket(sock) # type: ignore[assignment, misc, unused-ignore]
@@ -508,7 +508,7 @@ def _configured_socket_interface(address: _Address, options: PoolOptions) -> Net
508508
try:
509509
# We have to pass hostname / ip address to wrap_socket
510510
# to use SSLContext.check_hostname.
511-
if HAS_SNI:
511+
if _has_sni(True):
512512
ssl_sock = ssl_context.wrap_socket(sock, server_hostname=host)
513513
else:
514514
ssl_sock = ssl_context.wrap_socket(sock)

pymongo/ssl_support.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@
5555
IPADDR_SAFE = True
5656

5757
if HAVE_PYSSL:
58-
# # We have to pass hostname / ip address to wrap_socket
59-
# # to use SSLContext.check_hostname.
60-
# if ssl_context.has_sni:
61-
# ...
62-
HAS_SNI = _pyssl.HAS_SNI and _ssl.HAS_SNI
6358
PYSSLError: Any = _pyssl.SSLError
6459
BLOCKING_IO_ERRORS: tuple = _pyssl.BLOCKING_IO_ERRORS + _ssl.BLOCKING_IO_ERRORS
6560
BLOCKING_IO_READ_ERROR: tuple = (
@@ -71,14 +66,18 @@
7166
_ssl.BLOCKING_IO_WRITE_ERROR,
7267
)
7368
else:
74-
HAS_SNI = _ssl.HAS_SNI
7569
PYSSLError = _ssl.SSLError
7670
BLOCKING_IO_ERRORS = _ssl.BLOCKING_IO_ERRORS
7771
BLOCKING_IO_READ_ERROR = (_ssl.BLOCKING_IO_READ_ERROR,)
7872
BLOCKING_IO_WRITE_ERROR = (_ssl.BLOCKING_IO_WRITE_ERROR,)
7973
SSLError = _ssl.SSLError
8074
BLOCKING_IO_LOOKUP_ERROR = BLOCKING_IO_READ_ERROR
8175

76+
def _has_sni(is_sync: bool) -> bool:
77+
if is_sync and HAVE_PYSSL:
78+
return _pyssl.HAS_SNI
79+
return _ssl.HAS_SNI
80+
8281
def get_ssl_context(
8382
certfile: Optional[str],
8483
passphrase: Optional[str],
@@ -92,6 +91,8 @@ def get_ssl_context(
9291
"""Create and return an SSLContext object."""
9392
if is_sync and HAVE_PYSSL:
9493
_ssl: types.ModuleType = _pyssl
94+
else:
95+
_ssl = globals()["_ssl"]
9596
verify_mode = CERT_NONE if allow_invalid_certificates else CERT_REQUIRED
9697
ctx = _ssl.SSLContext(_ssl.PROTOCOL_SSLv23)
9798
if verify_mode != CERT_NONE:
@@ -132,10 +133,12 @@ def get_ssl_context(
132133
class SSLError(Exception): # type: ignore
133134
pass
134135

135-
HAS_SNI = False
136136
IPADDR_SAFE = False
137137
BLOCKING_IO_ERRORS = ()
138138

139+
def _has_sni(is_sync: bool) -> bool: # noqa: ARG001
140+
return False
141+
139142
def get_ssl_context(*dummy): # type: ignore
140143
"""No ssl module, raise ConfigurationError."""
141144
raise ConfigurationError("The ssl module is not available")

0 commit comments

Comments
 (0)