Skip to content

Commit 379c0c2

Browse files
committed
Fix use of SSLContext with sniffing
1 parent e34e8b9 commit 379c0c2

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

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: 8 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,17 @@ 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("https", "localhost", 80, path_prefix="/a", ssl_context=context),
514+
NodeConfig("https", "localhost", 81, path_prefix="/b", ssl_context=context),
512515
],
513516
sniff_on_start=True,
514-
sniff_callback=lambda *_: [],
517+
sniff_callback=lambda *_: [
518+
NodeConfig("https", "localhost", 80, path_prefix="/a")
519+
],
515520
)
516521

517522
assert len(w) == 1

tests/test_transport.py

Lines changed: 5 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,15 @@ 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("https", "localhost", 80, path_prefix="/a", ssl_context=context),
545+
NodeConfig("https", "localhost", 81, path_prefix="/b", ssl_context=context),
544546
],
545547
sniff_on_start=True,
546548
sniff_callback=lambda *_: [
547-
NodeConfig("http", "localhost", 80, path_prefix="/a")
549+
NodeConfig("https", "localhost", 80, path_prefix="/a")
548550
],
549551
)
550552

0 commit comments

Comments
 (0)