|
16 | 16 | from __future__ import annotations |
17 | 17 |
|
18 | 18 | import asyncio |
19 | | -import functools |
20 | 19 | import socket |
21 | 20 | import ssl |
22 | 21 | import sys |
|
25 | 24 | Any, |
26 | 25 | NoReturn, |
27 | 26 | Optional, |
28 | | - Union, |
29 | 27 | ) |
30 | 28 |
|
31 | 29 | from pymongo import _csot |
|
47 | 45 |
|
48 | 46 | SSLErrors = (PYSSLError, SSLError) |
49 | 47 | if TYPE_CHECKING: |
50 | | - from pymongo.pyopenssl_context import _sslConn |
51 | 48 | from pymongo.typings import _Address |
52 | 49 |
|
53 | 50 | try: |
@@ -274,64 +271,10 @@ async def _async_create_connection(address: _Address, options: PoolOptions) -> s |
274 | 271 | raise OSError("getaddrinfo failed") |
275 | 272 |
|
276 | 273 |
|
277 | | -async def _async_configured_socket( |
278 | | - address: _Address, options: PoolOptions |
279 | | -) -> Union[socket.socket, _sslConn]: |
280 | | - """Given (host, port) and PoolOptions, return a raw configured socket. |
281 | | -
|
282 | | - Can raise socket.error, ConnectionFailure, or _CertificateError. |
283 | | -
|
284 | | - Sets socket's SSL and timeout options. |
285 | | - """ |
286 | | - sock = await _async_create_connection(address, options) |
287 | | - ssl_context = options._ssl_context |
288 | | - |
289 | | - if ssl_context is None: |
290 | | - sock.settimeout(options.socket_timeout) |
291 | | - return sock |
292 | | - |
293 | | - host = address[0] |
294 | | - try: |
295 | | - # We have to pass hostname / ip address to wrap_socket |
296 | | - # to use SSLContext.check_hostname. |
297 | | - if _has_sni(False): |
298 | | - loop = asyncio.get_running_loop() |
299 | | - ssl_sock = await loop.run_in_executor( |
300 | | - None, |
301 | | - functools.partial(ssl_context.wrap_socket, sock, server_hostname=host), # type: ignore[assignment, misc, unused-ignore] |
302 | | - ) |
303 | | - else: |
304 | | - loop = asyncio.get_running_loop() |
305 | | - ssl_sock = await loop.run_in_executor(None, ssl_context.wrap_socket, sock) # type: ignore[assignment, misc, unused-ignore] |
306 | | - except _CertificateError: |
307 | | - sock.close() |
308 | | - # Raise _CertificateError directly like we do after match_hostname |
309 | | - # below. |
310 | | - raise |
311 | | - except (OSError, *SSLErrors) as exc: |
312 | | - sock.close() |
313 | | - # We raise AutoReconnect for transient and permanent SSL handshake |
314 | | - # failures alike. Permanent handshake failures, like protocol |
315 | | - # mismatch, will be turned into ServerSelectionTimeoutErrors later. |
316 | | - details = _get_timeout_details(options) |
317 | | - _raise_connection_failure(address, exc, "SSL handshake failed: ", timeout_details=details) |
318 | | - if ( |
319 | | - ssl_context.verify_mode |
320 | | - and not ssl_context.check_hostname |
321 | | - and not options.tls_allow_invalid_hostnames |
322 | | - ): |
323 | | - try: |
324 | | - ssl.match_hostname(ssl_sock.getpeercert(), hostname=host) # type:ignore[attr-defined, unused-ignore] |
325 | | - except _CertificateError: |
326 | | - ssl_sock.close() |
327 | | - raise |
328 | | - |
329 | | - ssl_sock.settimeout(options.socket_timeout) |
330 | | - return ssl_sock |
331 | | - |
332 | | - |
333 | 274 | async def _configured_protocol_interface( |
334 | | - address: _Address, options: PoolOptions, protocol_kls: PyMongoBaseProtocol = PyMongoProtocol |
| 275 | + address: _Address, |
| 276 | + options: PoolOptions, |
| 277 | + protocol_kls: type[PyMongoBaseProtocol] = PyMongoProtocol, |
335 | 278 | ) -> AsyncNetworkingInterface: |
336 | 279 | """Given (host, port) and PoolOptions, return a configured AsyncNetworkingInterface. |
337 | 280 |
|
@@ -455,55 +398,6 @@ def _create_connection(address: _Address, options: PoolOptions) -> socket.socket |
455 | 398 | raise OSError("getaddrinfo failed") |
456 | 399 |
|
457 | 400 |
|
458 | | -def _configured_socket(address: _Address, options: PoolOptions) -> Union[socket.socket, _sslConn]: |
459 | | - """Given (host, port) and PoolOptions, return a raw configured socket. |
460 | | -
|
461 | | - Can raise socket.error, ConnectionFailure, or _CertificateError. |
462 | | -
|
463 | | - Sets socket's SSL and timeout options. |
464 | | - """ |
465 | | - sock = _create_connection(address, options) |
466 | | - ssl_context = options._ssl_context |
467 | | - |
468 | | - if ssl_context is None: |
469 | | - sock.settimeout(options.socket_timeout) |
470 | | - return sock |
471 | | - |
472 | | - host = address[0] |
473 | | - try: |
474 | | - # We have to pass hostname / ip address to wrap_socket |
475 | | - # to use SSLContext.check_hostname. |
476 | | - if _has_sni(True): |
477 | | - ssl_sock = ssl_context.wrap_socket(sock, server_hostname=host) # type: ignore[assignment, misc, unused-ignore] |
478 | | - else: |
479 | | - ssl_sock = ssl_context.wrap_socket(sock) # type: ignore[assignment, misc, unused-ignore] |
480 | | - except _CertificateError: |
481 | | - sock.close() |
482 | | - # Raise _CertificateError directly like we do after match_hostname |
483 | | - # below. |
484 | | - raise |
485 | | - except (OSError, *SSLErrors) as exc: |
486 | | - sock.close() |
487 | | - # We raise AutoReconnect for transient and permanent SSL handshake |
488 | | - # failures alike. Permanent handshake failures, like protocol |
489 | | - # mismatch, will be turned into ServerSelectionTimeoutErrors later. |
490 | | - details = _get_timeout_details(options) |
491 | | - _raise_connection_failure(address, exc, "SSL handshake failed: ", timeout_details=details) |
492 | | - if ( |
493 | | - ssl_context.verify_mode |
494 | | - and not ssl_context.check_hostname |
495 | | - and not options.tls_allow_invalid_hostnames |
496 | | - ): |
497 | | - try: |
498 | | - ssl.match_hostname(ssl_sock.getpeercert(), hostname=host) # type:ignore[attr-defined, unused-ignore] |
499 | | - except _CertificateError: |
500 | | - ssl_sock.close() |
501 | | - raise |
502 | | - |
503 | | - ssl_sock.settimeout(options.socket_timeout) |
504 | | - return ssl_sock |
505 | | - |
506 | | - |
507 | 401 | def _configured_socket_interface( |
508 | 402 | address: _Address, options: PoolOptions, *args: Any |
509 | 403 | ) -> NetworkingInterface: |
|
0 commit comments