Skip to content

Commit 28c3daf

Browse files
authored
Deprecate the 'http_auth' parameter
1 parent a1e1830 commit 28c3daf

File tree

5 files changed

+115
-2
lines changed

5 files changed

+115
-2
lines changed

elasticsearch/_async/client/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ def __init__(
168168
] = None,
169169
sniffer_timeout=DEFAULT,
170170
sniff_on_connection_fail=DEFAULT,
171+
http_auth=DEFAULT,
171172
# Internal use only
172173
_transport: Optional[AsyncTransport] = None,
173174
) -> None:
@@ -346,6 +347,7 @@ def __init__(
346347
self._headers["x-opaque-id"] = opaque_id
347348
self._headers = resolve_auth_headers(
348349
self._headers,
350+
http_auth=http_auth,
349351
api_key=api_key,
350352
basic_auth=basic_auth,
351353
bearer_auth=bearer_auth,

elasticsearch/_async/client/_base.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353

5454
def resolve_auth_headers(
5555
headers: Optional[Mapping[str, str]],
56+
http_auth: Union[DefaultType, None, Tuple[str, str], str] = DEFAULT,
5657
api_key: Union[DefaultType, None, Tuple[str, str], str] = DEFAULT,
5758
basic_auth: Union[DefaultType, None, Tuple[str, str], str] = DEFAULT,
5859
bearer_auth: Union[DefaultType, None, str] = DEFAULT,
@@ -63,8 +64,33 @@ def resolve_auth_headers(
6364
elif not isinstance(headers, HttpHeaders):
6465
headers = HttpHeaders(headers)
6566

66-
resolved_api_key = resolve_default(api_key, None)
67+
resolved_http_auth = resolve_default(http_auth, None)
6768
resolved_basic_auth = resolve_default(basic_auth, None)
69+
if resolved_http_auth is not None:
70+
if resolved_basic_auth is not None:
71+
raise ValueError(
72+
"Can't specify both 'http_auth' and 'basic_auth', "
73+
"instead only specify 'basic_auth'"
74+
)
75+
if isinstance(http_auth, str) or (
76+
isinstance(resolved_http_auth, (list, tuple))
77+
and all(isinstance(x, str) for x in resolved_http_auth)
78+
):
79+
resolved_basic_auth = resolved_http_auth
80+
else:
81+
raise TypeError(
82+
"The deprecated 'http_auth' parameter must be either 'Tuple[str, str]' or 'str'. "
83+
"Use either the 'basic_auth' parameter instead"
84+
)
85+
86+
warnings.warn(
87+
"The 'http_auth' parameter is deprecated. "
88+
"Use 'basic_auth' or 'bearer_auth' parameters instead",
89+
category=DeprecationWarning,
90+
stacklevel=warn_stacklevel(),
91+
)
92+
93+
resolved_api_key = resolve_default(api_key, None)
6894
resolved_bearer_auth = resolve_default(bearer_auth, None)
6995
if resolved_api_key or resolved_basic_auth or resolved_bearer_auth:
7096
if (

elasticsearch/_sync/client/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ def __init__(
168168
] = None,
169169
sniffer_timeout=DEFAULT,
170170
sniff_on_connection_fail=DEFAULT,
171+
http_auth=DEFAULT,
171172
# Internal use only
172173
_transport: Optional[Transport] = None,
173174
) -> None:
@@ -346,6 +347,7 @@ def __init__(
346347
self._headers["x-opaque-id"] = opaque_id
347348
self._headers = resolve_auth_headers(
348349
self._headers,
350+
http_auth=http_auth,
349351
api_key=api_key,
350352
basic_auth=basic_auth,
351353
bearer_auth=bearer_auth,

elasticsearch/_sync/client/_base.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353

5454
def resolve_auth_headers(
5555
headers: Optional[Mapping[str, str]],
56+
http_auth: Union[DefaultType, None, Tuple[str, str], str] = DEFAULT,
5657
api_key: Union[DefaultType, None, Tuple[str, str], str] = DEFAULT,
5758
basic_auth: Union[DefaultType, None, Tuple[str, str], str] = DEFAULT,
5859
bearer_auth: Union[DefaultType, None, str] = DEFAULT,
@@ -63,8 +64,33 @@ def resolve_auth_headers(
6364
elif not isinstance(headers, HttpHeaders):
6465
headers = HttpHeaders(headers)
6566

66-
resolved_api_key = resolve_default(api_key, None)
67+
resolved_http_auth = resolve_default(http_auth, None)
6768
resolved_basic_auth = resolve_default(basic_auth, None)
69+
if resolved_http_auth is not None:
70+
if resolved_basic_auth is not None:
71+
raise ValueError(
72+
"Can't specify both 'http_auth' and 'basic_auth', "
73+
"instead only specify 'basic_auth'"
74+
)
75+
if isinstance(http_auth, str) or (
76+
isinstance(resolved_http_auth, (list, tuple))
77+
and all(isinstance(x, str) for x in resolved_http_auth)
78+
):
79+
resolved_basic_auth = resolved_http_auth
80+
else:
81+
raise TypeError(
82+
"The deprecated 'http_auth' parameter must be either 'Tuple[str, str]' or 'str'. "
83+
"Use either the 'basic_auth' parameter instead"
84+
)
85+
86+
warnings.warn(
87+
"The 'http_auth' parameter is deprecated. "
88+
"Use 'basic_auth' or 'bearer_auth' parameters instead",
89+
category=DeprecationWarning,
90+
stacklevel=warn_stacklevel(),
91+
)
92+
93+
resolved_api_key = resolve_default(api_key, None)
6894
resolved_bearer_auth = resolve_default(bearer_auth, None)
6995
if resolved_api_key or resolved_basic_auth or resolved_bearer_auth:
7096
if (

test_elasticsearch/test_client/test_deprecated_options.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import warnings
1919

20+
import pytest
21+
2022
from elasticsearch import Elasticsearch
2123

2224

@@ -30,6 +32,17 @@ def test_sniff_on_connection_fail():
3032
"The 'sniff_on_connection_fail' parameter is deprecated in favor of 'sniff_on_node_failure'"
3133
)
3234

35+
with pytest.raises(ValueError) as e:
36+
Elasticsearch(
37+
"http://localhost:9200",
38+
sniff_on_connection_fail=True,
39+
sniff_on_node_failure=True,
40+
)
41+
assert (
42+
str(e.value)
43+
== "Can't specify both 'sniff_on_connection_fail' and 'sniff_on_node_failure', instead only specify 'sniff_on_node_failure'"
44+
)
45+
3346

3447
def test_sniffer_timeout():
3548
with warnings.catch_warnings(record=True) as w:
@@ -41,6 +54,15 @@ def test_sniffer_timeout():
4154
"The 'sniffer_timeout' parameter is deprecated in favor of 'min_delay_between_sniffing'"
4255
)
4356

57+
with pytest.raises(ValueError) as e:
58+
Elasticsearch(
59+
"http://localhost:9200", sniffer_timeout=1, min_delay_between_sniffing=1
60+
)
61+
assert (
62+
str(e.value)
63+
== "Can't specify both 'sniffer_timeout' and 'min_delay_between_sniffing', instead only specify 'min_delay_between_sniffing'"
64+
)
65+
4466

4567
def test_randomize_hosts():
4668
with warnings.catch_warnings(record=True) as w:
@@ -50,3 +72,38 @@ def test_randomize_hosts():
5072
assert str(w[0].message) == (
5173
"The 'randomize_hosts' parameter is deprecated in favor of 'randomize_nodes_in_pool'"
5274
)
75+
76+
with pytest.raises(ValueError) as e:
77+
Elasticsearch(
78+
"http://localhost:9200", randomize_hosts=True, randomize_nodes_in_pool=True
79+
)
80+
assert (
81+
str(e.value)
82+
== "Can't specify both 'randomize_hosts' and 'randomize_nodes_in_pool', instead only specify 'randomize_nodes_in_pool'"
83+
)
84+
85+
86+
def test_http_auth():
87+
with warnings.catch_warnings(record=True) as w:
88+
client = Elasticsearch(
89+
"http://localhost:9200", http_auth=("username", "password")
90+
)
91+
92+
assert len(w) == 1
93+
assert w[0].category == DeprecationWarning
94+
assert (
95+
str(w[0].message)
96+
== "The 'http_auth' parameter is deprecated. Use 'basic_auth' or 'bearer_auth' parameters instead"
97+
)
98+
assert client._headers["Authorization"] == "Basic dXNlcm5hbWU6cGFzc3dvcmQ="
99+
100+
with pytest.raises(ValueError) as e:
101+
Elasticsearch(
102+
"http://localhost:9200",
103+
http_auth=("username", "password"),
104+
basic_auth=("username", "password"),
105+
)
106+
assert (
107+
str(e.value)
108+
== "Can't specify both 'http_auth' and 'basic_auth', instead only specify 'basic_auth'"
109+
)

0 commit comments

Comments
 (0)