Skip to content

Commit af34992

Browse files
authored
Fix use of SSLContext with sniffing (#199)
* Fix use of SSLContext with sniffing * Fix lint * Ignore enabled_cleanup_closed warning It's not triggered for normal code. * Remove warning filter
1 parent 6167370 commit af34992

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

elastic_transport/_node/_urllib3_chain_certs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def _validate_conn(self, conn: HTTPSConnection) -> None: # type: ignore[overrid
108108
if sys.version_info >= (3, 13):
109109
fingerprints = [
110110
hash_func(cert).digest()
111-
for cert in conn.sock.get_verified_chain()
111+
for cert in conn.sock.get_verified_chain() # type: ignore
112112
]
113113
else:
114114
# 'get_verified_chain()' and 'Certificate.public_bytes()' are private APIs

elastic_transport/_transport.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -540,13 +540,13 @@ def validate_sniffing_options(
540540

541541
def warn_if_varying_node_config_options(node_configs: List[NodeConfig]) -> None:
542542
"""Function which detects situations when sniffing may produce incorrect configs"""
543-
exempt_attrs = {"host", "port", "connections_per_node", "_extras"}
543+
exempt_attrs = {"host", "port", "connections_per_node", "_extras", "ssl_context"}
544544
match_attr_dict = None
545545
for node_config in node_configs:
546546
attr_dict = {
547-
k: v
548-
for k, v in dataclasses.asdict(node_config).items()
549-
if k not in exempt_attrs
547+
field.name: getattr(node_config, field.name)
548+
for field in dataclasses.fields(node_config)
549+
if field.name not in exempt_attrs
550550
}
551551
if match_attr_dict is None:
552552
match_attr_dict = attr_dict

tests/async_/test_async_transport.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import asyncio
1919
import random
2020
import re
21+
import ssl
2122
import sys
2223
import time
2324
import warnings
@@ -505,13 +506,21 @@ async def test_error_sniffing_callback_without_sniffing_enabled():
505506
@pytest.mark.asyncio
506507
async def test_heterogeneous_node_config_warning_with_sniffing():
507508
with warnings.catch_warnings(record=True) as w:
509+
# SSLContext objects cannot be compared and are thus ignored
510+
context = ssl.create_default_context()
508511
AsyncTransport(
509512
[
510-
NodeConfig("http", "localhost", 80, path_prefix="/a"),
511-
NodeConfig("http", "localhost", 81, path_prefix="/b"),
513+
NodeConfig(
514+
"https", "localhost", 80, path_prefix="/a", ssl_context=context
515+
),
516+
NodeConfig(
517+
"https", "localhost", 81, path_prefix="/b", ssl_context=context
518+
),
512519
],
513520
sniff_on_start=True,
514-
sniff_callback=lambda *_: [],
521+
sniff_callback=lambda *_: [
522+
NodeConfig("https", "localhost", 80, path_prefix="/a")
523+
],
515524
)
516525

517526
assert len(w) == 1

tests/test_transport.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import random
1919
import re
20+
import ssl
2021
import threading
2122
import time
2223
import warnings
@@ -537,14 +538,19 @@ def test_error_sniffing_callback_without_sniffing_enabled():
537538

538539
def test_heterogeneous_node_config_warning_with_sniffing():
539540
with warnings.catch_warnings(record=True) as w:
541+
context = ssl.create_default_context()
540542
Transport(
541543
[
542-
NodeConfig("http", "localhost", 80, path_prefix="/a"),
543-
NodeConfig("http", "localhost", 81, path_prefix="/b"),
544+
NodeConfig(
545+
"https", "localhost", 80, path_prefix="/a", ssl_context=context
546+
),
547+
NodeConfig(
548+
"https", "localhost", 81, path_prefix="/b", ssl_context=context
549+
),
544550
],
545551
sniff_on_start=True,
546552
sniff_callback=lambda *_: [
547-
NodeConfig("http", "localhost", 80, path_prefix="/a")
553+
NodeConfig("https", "localhost", 80, path_prefix="/a")
548554
],
549555
)
550556

0 commit comments

Comments
 (0)