Skip to content

Commit f691222

Browse files
committed
Map PyOpenSSL SysCallErrors to standard Python ConnectionErrors
1 parent 4a8dc43 commit f691222

File tree

4 files changed

+155
-7
lines changed

4 files changed

+155
-7
lines changed

.flake8

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ per-file-ignores =
138138
cheroot/test/test_core.py: C815, DAR101, DAR201, DAR401, I003, I004, N805, N806, S101, WPS110, WPS111, WPS114, WPS121, WPS202, WPS204, WPS226, WPS229, WPS324, WPS421, WPS422, WPS432, WPS602
139139
cheroot/test/test_dispatch.py: DAR101, DAR201, S101, WPS111, WPS121, WPS422, WPS430
140140
cheroot/test/test_ssl.py: C818, DAR101, DAR201, DAR301, DAR401, E800, I001, I003, I004, I005, S101, S309, S404, S603, WPS100, WPS110, WPS111, WPS114, WPS121, WPS130, WPS201, WPS202, WPS204, WPS210, WPS211, WPS218, WPS219, WPS222, WPS226, WPS231, WPS235, WPS301, WPS324, WPS335, WPS336, WPS408, WPS420, WPS421, WPS422, WPS432, WPS441, WPS509, WPS608
141+
cheroot/test/test_ssl_pyopenssl.py: DAR101, DAR201, WPS226
141142
cheroot/test/test_server.py: DAR101, DAR201, DAR301, I001, I003, I004, I005, S101, WPS110, WPS111, WPS118, WPS121, WPS122, WPS130, WPS201, WPS202, WPS210, WPS218, WPS226, WPS229, WPS324, WPS421, WPS422, WPS430, WPS432, WPS473, WPS509, WPS608
142143
cheroot/test/test_conn.py: DAR101, DAR201, DAR301, DAR401, E800, I001, I003, I004, I005, N802, N805, RST304, S101, S310, WPS100, WPS110, WPS111, WPS114, WPS115, WPS120, WPS121, WPS122, WPS201, WPS202, WPS204, WPS210, WPS211, WPS213, WPS214, WPS218, WPS219, WPS226, WPS231, WPS301, WPS402, WPS420, WPS421, WPS422, WPS429, WPS430, WPS432, WPS435, WPS447, WPS504, WPS509, WPS602
143144
cheroot/test/webtest.py: DAR101, DAR201, DAR401, I001, I003, I004, N802, RST303, RST304, S101, S104, WPS100, WPS110, WPS111, WPS115, WPS120, WPS121, WPS122, WPS201, WPS202, WPS204, WPS210, WPS211, WPS213, WPS214, WPS220, WPS221, WPS223, WPS229, WPS230, WPS231, WPS236, WPS237, WPS301, WPS338, WPS414, WPS420, WPS421, WPS422, WPS430, WPS432, WPS501, WPS505, WPS601

cheroot/ssl/pyopenssl.py

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
pyopenssl
5151
"""
5252

53+
import errno
54+
import os
5355
import socket
5456
import sys
5557
import threading
@@ -77,6 +79,45 @@
7779
from . import Adapter
7880

7981

82+
@contextlib.contextmanager
83+
def _morph_syscall_to_connection_error(method_name, /):
84+
"""
85+
Handle :exc:`OpenSSL.SSL.SysCallError` in a wrapped method.
86+
87+
This context manager catches and re-raises SSL system call errors
88+
with appropriate exception types.
89+
90+
Yields:
91+
None: Execution continues within the context block.
92+
""" # noqa: DAR301
93+
try:
94+
yield
95+
except SSL.SysCallError as ssl_syscall_err:
96+
connection_error_map = {
97+
errno.EBADF: ConnectionError, # socket is gone?
98+
errno.ECONNABORTED: ConnectionAbortedError,
99+
errno.ECONNREFUSED: ConnectionRefusedError,
100+
errno.ECONNRESET: ConnectionResetError,
101+
errno.ENOTCONN: ConnectionError,
102+
errno.EPIPE: BrokenPipeError,
103+
errno.ESHUTDOWN: BrokenPipeError,
104+
}
105+
error_code = ssl_syscall_err.args[0] if ssl_syscall_err.args else None
106+
error_msg = (
107+
os.strerror(error_code)
108+
if error_code is not None
109+
else repr(ssl_syscall_err)
110+
)
111+
conn_err_cls = connection_error_map.get(
112+
error_code,
113+
ConnectionError,
114+
)
115+
raise conn_err_cls(
116+
error_code,
117+
f'Error in calling {method_name!s} on PyOpenSSL connection: {error_msg!s}',
118+
) from ssl_syscall_err
119+
120+
80121
class SSLFileobjectMixin:
81122
"""Base mixin for a TLS socket stream."""
82123

@@ -224,14 +265,12 @@ def lock_decorator(method):
224265
"""Create a proxy method for a new class."""
225266

226267
def proxy_wrapper(self, *args):
227-
self._lock.acquire()
228-
try:
229-
new_args = (
230-
args[:] if method not in proxy_methods_no_args else []
231-
)
268+
new_args = (
269+
args[:] if method not in proxy_methods_no_args else []
270+
)
271+
# translate any SysCallError to ConnectionError
272+
with _morph_syscall_to_connection_error(method), self._lock:
232273
return getattr(self._ssl_conn, method)(*new_args)
233-
finally:
234-
self._lock.release()
235274

236275
return proxy_wrapper
237276

cheroot/test/test_ssl_pyopenssl.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
"""Tests for :py:mod: `cheroot.ssl.pyopenssl` :py:class:`SSLConnection` wrapper."""
2+
3+
import errno
4+
5+
import pytest
6+
7+
from OpenSSL import SSL
8+
9+
from cheroot.ssl.pyopenssl import SSLConnection
10+
11+
12+
@pytest.fixture
13+
def mock_ssl_context(mocker):
14+
"""Fixture providing a mock instance of :py:class:`SSL.Context`."""
15+
mock_context = mocker.Mock(spec=SSL.Context)
16+
17+
# Add a mock _context attribute to simulate SSL.Context behavior
18+
mock_context._context = mocker.Mock()
19+
return mock_context
20+
21+
22+
@pytest.mark.parametrize(
23+
(
24+
'tested_method_name',
25+
'simulated_errno',
26+
'expected_exception',
27+
),
28+
(
29+
pytest.param('close', errno.EBADF, ConnectionError, id='close-EBADF'),
30+
pytest.param(
31+
'close',
32+
errno.ECONNABORTED,
33+
ConnectionAbortedError,
34+
id='close-ECONNABORTED',
35+
),
36+
pytest.param(
37+
'send',
38+
errno.EPIPE,
39+
BrokenPipeError,
40+
id='send-EPIPE',
41+
), # Expanded coverage
42+
pytest.param(
43+
'shutdown',
44+
errno.EPIPE,
45+
BrokenPipeError,
46+
id='shutdown-EPIPE',
47+
),
48+
pytest.param(
49+
'shutdown',
50+
errno.ECONNRESET,
51+
ConnectionResetError,
52+
id='shutdown-ECONNRESET',
53+
),
54+
pytest.param(
55+
'close',
56+
errno.ENOTCONN,
57+
ConnectionError,
58+
id='close-ENOTCONN',
59+
),
60+
pytest.param('close', errno.EPIPE, BrokenPipeError, id='close-EPIPE'),
61+
pytest.param(
62+
'close',
63+
errno.ESHUTDOWN,
64+
BrokenPipeError,
65+
id='close-ESHUTDOWN',
66+
),
67+
),
68+
)
69+
def test_close_morphs_syscall_error_correctly(
70+
mocker,
71+
mock_ssl_context,
72+
tested_method_name,
73+
simulated_errno,
74+
expected_exception,
75+
):
76+
"""Check ``SSLConnection.close()`` morphs ``SysCallError`` to ``ConnectionError``."""
77+
# Prevents the real OpenSSL.SSL.Connection.__init__ from running
78+
mocker.patch('OpenSSL.SSL.Connection')
79+
80+
# Create SSLConnection object. The patched SSL.Connection() call returns
81+
# a mock that is stored internally as conn._ssl_conn.
82+
conn = SSLConnection(mock_ssl_context)
83+
84+
# Define specific OpenSSL error based on the parameter
85+
simulated_error = SSL.SysCallError(
86+
simulated_errno,
87+
'Simulated connection error',
88+
)
89+
90+
# Dynamically retrieve the method on the underlying mock
91+
underlying_method = getattr(conn._ssl_conn, tested_method_name)
92+
93+
# Patch the method to raise the simulated error
94+
underlying_method.side_effect = simulated_error
95+
96+
expected_match = (
97+
f'.*Error in calling {tested_method_name} on PyOpenSSL connection.*'
98+
)
99+
100+
# Assert the expected exception is raised based on the parameter
101+
with pytest.raises(expected_exception, match=expected_match) as excinfo:
102+
getattr(conn, tested_method_name)()
103+
104+
# Assert the original SysCallError is included in the new exception's cause
105+
assert excinfo.value.__cause__ is simulated_error
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
OpenSSL system call errors are now intercepted and re-raised as standard Python :exc:`ConnectionErrors`.
2+
3+
-- by :user:`julianz-`

0 commit comments

Comments
 (0)