Skip to content

Commit 74387d0

Browse files
authored
Remove deprecated http_auth parameter (elastic#2839)
1 parent ed13098 commit 74387d0

File tree

13 files changed

+8
-265
lines changed

13 files changed

+8
-265
lines changed

elasticsearch/_async/client/__init__.py

-21
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@
8888
_rewrite_parameters,
8989
_stability_warning,
9090
client_node_configs,
91-
is_requests_http_auth,
92-
is_requests_node_class,
9391
)
9492
from .watcher import WatcherClient
9593
from .xpack import XPackClient
@@ -191,7 +189,6 @@ def __init__(
191189
] = None,
192190
sniffer_timeout: t.Union[DefaultType, None, float] = DEFAULT,
193191
sniff_on_connection_fail: t.Union[DefaultType, bool] = DEFAULT,
194-
http_auth: t.Union[DefaultType, t.Any] = DEFAULT,
195192
maxsize: t.Union[DefaultType, int] = DEFAULT,
196193
# Internal use only
197194
_transport: t.Optional[AsyncTransport] = None,
@@ -320,26 +317,9 @@ def __init__(
320317
sniff_callback = default_sniff_callback
321318

322319
if _transport is None:
323-
requests_session_auth = None
324-
if http_auth is not None and http_auth is not DEFAULT:
325-
if is_requests_http_auth(http_auth):
326-
# If we're using custom requests authentication
327-
# then we need to alert the user that they also
328-
# need to use 'node_class=requests'.
329-
if not is_requests_node_class(node_class):
330-
raise ValueError(
331-
"Using a custom 'requests.auth.AuthBase' class for "
332-
"'http_auth' must be used with node_class='requests'"
333-
)
334-
335-
# Reset 'http_auth' to DEFAULT so it's not consumed below.
336-
requests_session_auth = http_auth
337-
http_auth = DEFAULT
338-
339320
node_configs = client_node_configs(
340321
hosts,
341322
cloud_id=cloud_id,
342-
requests_session_auth=requests_session_auth,
343323
connections_per_node=connections_per_node,
344324
http_compress=http_compress,
345325
verify_certs=verify_certs,
@@ -426,7 +406,6 @@ def __init__(
426406
self._headers["x-opaque-id"] = opaque_id
427407
self._headers = resolve_auth_headers(
428408
self._headers,
429-
http_auth=http_auth,
430409
api_key=api_key,
431410
basic_auth=basic_auth,
432411
bearer_auth=bearer_auth,

elasticsearch/_async/client/_base.py

-26
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868

6969
def resolve_auth_headers(
7070
headers: Optional[Mapping[str, str]],
71-
http_auth: Union[DefaultType, None, Tuple[str, str], str] = DEFAULT,
7271
api_key: Union[DefaultType, None, Tuple[str, str], str] = DEFAULT,
7372
basic_auth: Union[DefaultType, None, Tuple[str, str], str] = DEFAULT,
7473
bearer_auth: Union[DefaultType, None, str] = DEFAULT,
@@ -78,32 +77,7 @@ def resolve_auth_headers(
7877
elif not isinstance(headers, HttpHeaders):
7978
headers = HttpHeaders(headers)
8079

81-
resolved_http_auth = http_auth if http_auth is not DEFAULT else None
8280
resolved_basic_auth = basic_auth if basic_auth is not DEFAULT else None
83-
if resolved_http_auth is not None:
84-
if resolved_basic_auth is not None:
85-
raise ValueError(
86-
"Can't specify both 'http_auth' and 'basic_auth', "
87-
"instead only specify 'basic_auth'"
88-
)
89-
if isinstance(http_auth, str) or (
90-
isinstance(resolved_http_auth, (list, tuple))
91-
and all(isinstance(x, str) for x in resolved_http_auth)
92-
):
93-
resolved_basic_auth = resolved_http_auth
94-
else:
95-
raise TypeError(
96-
"The deprecated 'http_auth' parameter must be either 'Tuple[str, str]' or 'str'. "
97-
"Use either the 'basic_auth' parameter instead"
98-
)
99-
100-
warnings.warn(
101-
"The 'http_auth' parameter is deprecated. "
102-
"Use 'basic_auth' or 'bearer_auth' parameters instead",
103-
category=DeprecationWarning,
104-
stacklevel=warn_stacklevel(),
105-
)
106-
10781
resolved_api_key = api_key if api_key is not DEFAULT else None
10882
resolved_bearer_auth = bearer_auth if bearer_auth is not DEFAULT else None
10983
if resolved_api_key or resolved_basic_auth or resolved_bearer_auth:

elasticsearch/_async/client/utils.py

-4
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
_rewrite_parameters,
2828
_stability_warning,
2929
client_node_configs,
30-
is_requests_http_auth,
31-
is_requests_node_class,
3230
)
3331

3432
__all__ = [
@@ -43,6 +41,4 @@
4341
"client_node_configs",
4442
"_rewrite_parameters",
4543
"_stability_warning",
46-
"is_requests_http_auth",
47-
"is_requests_node_class",
4844
]

elasticsearch/_async/helpers.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -419,12 +419,9 @@ def pop_transport_kwargs(kw: MutableMapping[str, Any]) -> MutableMapping[str, An
419419
# Grab options that should be propagated to every
420420
# API call within this helper instead of just 'search()'
421421
transport_kwargs = {}
422-
for key in ("headers", "api_key", "http_auth", "basic_auth", "bearer_auth"):
422+
for key in ("headers", "api_key", "basic_auth", "bearer_auth"):
423423
try:
424-
value = kw.pop(key)
425-
if key == "http_auth":
426-
key = "basic_auth"
427-
transport_kwargs[key] = value
424+
transport_kwargs[key] = kw.pop(key)
428425
except KeyError:
429426
pass
430427
return transport_kwargs

elasticsearch/_sync/client/__init__.py

-21
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@
8888
_rewrite_parameters,
8989
_stability_warning,
9090
client_node_configs,
91-
is_requests_http_auth,
92-
is_requests_node_class,
9391
)
9492
from .watcher import WatcherClient
9593
from .xpack import XPackClient
@@ -191,7 +189,6 @@ def __init__(
191189
] = None,
192190
sniffer_timeout: t.Union[DefaultType, None, float] = DEFAULT,
193191
sniff_on_connection_fail: t.Union[DefaultType, bool] = DEFAULT,
194-
http_auth: t.Union[DefaultType, t.Any] = DEFAULT,
195192
maxsize: t.Union[DefaultType, int] = DEFAULT,
196193
# Internal use only
197194
_transport: t.Optional[Transport] = None,
@@ -320,26 +317,9 @@ def __init__(
320317
sniff_callback = default_sniff_callback
321318

322319
if _transport is None:
323-
requests_session_auth = None
324-
if http_auth is not None and http_auth is not DEFAULT:
325-
if is_requests_http_auth(http_auth):
326-
# If we're using custom requests authentication
327-
# then we need to alert the user that they also
328-
# need to use 'node_class=requests'.
329-
if not is_requests_node_class(node_class):
330-
raise ValueError(
331-
"Using a custom 'requests.auth.AuthBase' class for "
332-
"'http_auth' must be used with node_class='requests'"
333-
)
334-
335-
# Reset 'http_auth' to DEFAULT so it's not consumed below.
336-
requests_session_auth = http_auth
337-
http_auth = DEFAULT
338-
339320
node_configs = client_node_configs(
340321
hosts,
341322
cloud_id=cloud_id,
342-
requests_session_auth=requests_session_auth,
343323
connections_per_node=connections_per_node,
344324
http_compress=http_compress,
345325
verify_certs=verify_certs,
@@ -426,7 +406,6 @@ def __init__(
426406
self._headers["x-opaque-id"] = opaque_id
427407
self._headers = resolve_auth_headers(
428408
self._headers,
429-
http_auth=http_auth,
430409
api_key=api_key,
431410
basic_auth=basic_auth,
432411
bearer_auth=bearer_auth,

elasticsearch/_sync/client/_base.py

-26
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868

6969
def resolve_auth_headers(
7070
headers: Optional[Mapping[str, str]],
71-
http_auth: Union[DefaultType, None, Tuple[str, str], str] = DEFAULT,
7271
api_key: Union[DefaultType, None, Tuple[str, str], str] = DEFAULT,
7372
basic_auth: Union[DefaultType, None, Tuple[str, str], str] = DEFAULT,
7473
bearer_auth: Union[DefaultType, None, str] = DEFAULT,
@@ -78,32 +77,7 @@ def resolve_auth_headers(
7877
elif not isinstance(headers, HttpHeaders):
7978
headers = HttpHeaders(headers)
8079

81-
resolved_http_auth = http_auth if http_auth is not DEFAULT else None
8280
resolved_basic_auth = basic_auth if basic_auth is not DEFAULT else None
83-
if resolved_http_auth is not None:
84-
if resolved_basic_auth is not None:
85-
raise ValueError(
86-
"Can't specify both 'http_auth' and 'basic_auth', "
87-
"instead only specify 'basic_auth'"
88-
)
89-
if isinstance(http_auth, str) or (
90-
isinstance(resolved_http_auth, (list, tuple))
91-
and all(isinstance(x, str) for x in resolved_http_auth)
92-
):
93-
resolved_basic_auth = resolved_http_auth
94-
else:
95-
raise TypeError(
96-
"The deprecated 'http_auth' parameter must be either 'Tuple[str, str]' or 'str'. "
97-
"Use either the 'basic_auth' parameter instead"
98-
)
99-
100-
warnings.warn(
101-
"The 'http_auth' parameter is deprecated. "
102-
"Use 'basic_auth' or 'bearer_auth' parameters instead",
103-
category=DeprecationWarning,
104-
stacklevel=warn_stacklevel(),
105-
)
106-
10781
resolved_api_key = api_key if api_key is not DEFAULT else None
10882
resolved_bearer_auth = bearer_auth if bearer_auth is not DEFAULT else None
10983
if resolved_api_key or resolved_basic_auth or resolved_bearer_auth:

elasticsearch/_sync/client/utils.py

-35
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
# under the License.
1717

1818
import base64
19-
import inspect
2019
import urllib.parse
2120
import warnings
2221
from datetime import date, datetime
@@ -44,7 +43,6 @@
4443
AsyncTransport,
4544
HttpHeaders,
4645
NodeConfig,
47-
RequestsHttpNode,
4846
SniffOptions,
4947
Transport,
5048
)
@@ -92,7 +90,6 @@ class Stability(Enum):
9290

9391
_TRANSPORT_OPTIONS = {
9492
"api_key",
95-
"http_auth",
9693
"request_timeout",
9794
"opaque_id",
9895
"headers",
@@ -105,7 +102,6 @@ class Stability(Enum):
105102
def client_node_configs(
106103
hosts: Optional[_TYPE_HOSTS],
107104
cloud_id: Optional[str],
108-
requests_session_auth: Optional[Any] = None,
109105
**kwargs: Any,
110106
) -> List[NodeConfig]:
111107
if cloud_id is not None:
@@ -126,12 +122,6 @@ def client_node_configs(
126122
headers.setdefault("user-agent", USER_AGENT)
127123
node_options["headers"] = headers
128124

129-
# If a custom Requests AuthBase is passed we set that via '_extras'.
130-
if requests_session_auth is not None:
131-
node_options.setdefault("_extras", {})[
132-
"requests.session.auth"
133-
] = requests_session_auth
134-
135125
def apply_node_options(node_config: NodeConfig) -> NodeConfig:
136126
"""Needs special handling of headers since .replace() wipes out existing headers"""
137127
nonlocal node_options
@@ -451,28 +441,3 @@ def wrapped(*args: Any, **kwargs: Any) -> Any:
451441
return wrapped # type: ignore[return-value]
452442

453443
return wrapper
454-
455-
456-
def is_requests_http_auth(http_auth: Any) -> bool:
457-
"""Detect if an http_auth value is a custom Requests auth object"""
458-
try:
459-
from requests.auth import AuthBase
460-
461-
return isinstance(http_auth, AuthBase)
462-
except ImportError:
463-
pass
464-
return False
465-
466-
467-
def is_requests_node_class(node_class: Any) -> bool:
468-
"""Detect if 'RequestsHttpNode' would be used given the setting of 'node_class'"""
469-
return (
470-
node_class is not None
471-
and node_class is not DEFAULT
472-
and (
473-
node_class == "requests"
474-
or (
475-
inspect.isclass(node_class) and issubclass(node_class, RequestsHttpNode)
476-
)
477-
)
478-
)

elasticsearch/helpers/actions.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -698,16 +698,12 @@ def pop_transport_kwargs(kw: MutableMapping[str, Any]) -> Dict[str, Any]:
698698
for key in (
699699
"headers",
700700
"api_key",
701-
"http_auth",
702701
"basic_auth",
703702
"bearer_auth",
704703
"opaque_id",
705704
):
706705
try:
707-
value = kw.pop(key)
708-
if key == "http_auth":
709-
key = "basic_auth"
710-
transport_kwargs[key] = value
706+
transport_kwargs[key] = kw.pop(key)
711707
except KeyError:
712708
pass
713709
return transport_kwargs

test_elasticsearch/test_async/test_server/test_helpers.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,8 @@ async def test_clear_scroll(self, async_client, scan_teardown):
741741
"kwargs",
742742
[
743743
{"api_key": ("name", "value")},
744-
{"http_auth": ("username", "password")},
744+
{"basic_auth": ("username", "password")},
745+
{"bearer_auth": "token"},
745746
{"headers": {"custom", "header"}},
746747
],
747748
)
@@ -790,9 +791,6 @@ async def test_scan_auth_kwargs_forwarded(
790791

791792
assert data == [{"search_data": 1}]
792793

793-
if "http_auth" in kwargs:
794-
kwargs = {"basic_auth": kwargs.pop("http_auth")}
795-
796794
assert options.call_args_list == [
797795
call(request_timeout=None, **kwargs),
798796
call(ignore_status=404),

test_elasticsearch/test_client/test_deprecated_options.py

-26
Original file line numberDiff line numberDiff line change
@@ -100,32 +100,6 @@ def test_randomize_hosts():
100100
)
101101

102102

103-
def test_http_auth():
104-
with warnings.catch_warnings(record=True) as w:
105-
client = Elasticsearch(
106-
"http://localhost:9200", http_auth=("username", "password")
107-
)
108-
109-
assert len(w) == 1
110-
assert w[0].category == DeprecationWarning
111-
assert (
112-
str(w[0].message)
113-
== "The 'http_auth' parameter is deprecated. Use 'basic_auth' or 'bearer_auth' parameters instead"
114-
)
115-
assert client._headers["Authorization"] == "Basic dXNlcm5hbWU6cGFzc3dvcmQ="
116-
117-
with pytest.raises(ValueError) as e:
118-
Elasticsearch(
119-
"http://localhost:9200",
120-
http_auth=("username", "password"),
121-
basic_auth=("username", "password"),
122-
)
123-
assert (
124-
str(e.value)
125-
== "Can't specify both 'http_auth' and 'basic_auth', instead only specify 'basic_auth'"
126-
)
127-
128-
129103
def test_serializer_and_serializers():
130104
with pytest.raises(ValueError) as e:
131105
Elasticsearch(

0 commit comments

Comments
 (0)