1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
|
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import pytest
from elastic_transport import OpenTelemetrySpan
from elastic_transport.client_utils import DEFAULT
from elasticsearch import AsyncElasticsearch, Elasticsearch, JsonSerializer
from elasticsearch._sync.client.utils import USER_AGENT
from test_elasticsearch.test_cases import (
DummyAsyncTransport,
DummyTransport,
DummyTransportTestCase,
)
EXPECTED_SERIALIZERS = {
"application/vnd.mapbox-vector-tile",
"application/x-ndjson",
"application/json",
"text/*",
"application/vnd.elasticsearch+json",
"application/vnd.elasticsearch+x-ndjson",
}
try:
import pyarrow as pa
EXPECTED_SERIALIZERS.add("application/vnd.apache.arrow.stream")
except ImportError:
pa = None
class TestOptions(DummyTransportTestCase):
def assert_called_with_headers(self, client, method, target, headers):
calls = client.transport.calls
assert (method, target) in calls
called_headers = calls[(method, target)][-1]["headers"].copy()
for header in (
"accept",
"content-type",
): # Common HTTP headers that we're not testing.
called_headers.pop(header, None)
assert headers == called_headers
@pytest.mark.parametrize(
["options", "headers"],
[
(
{"headers": {"authorization": "custom method"}},
{"Authorization": "custom method"},
),
({"api_key": "key"}, {"Authorization": "ApiKey key"}),
({"api_key": ("id", "value")}, {"Authorization": "ApiKey aWQ6dmFsdWU="}),
(
{"basic_auth": ("username", "password")},
{"Authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ="},
),
({"basic_auth": "encoded"}, {"Authorization": "Basic encoded"}),
({"bearer_auth": "bear"}, {"Authorization": "Bearer bear"}),
(
{"opaque_id": "test-id"},
{"X-Opaque-Id": "test-id"},
),
(
{
"opaque_id": "opaq-id",
"headers": {"custom": "key"},
"api_key": ("id", "val"),
},
{
"custom": "key",
"authorization": "ApiKey aWQ6dmFs",
"x-opaque-id": "opaq-id",
},
),
],
)
def test_options_to_headers(self, options, headers):
# Tests that authentication works identically from the constructor
# or from the client.options() API.
client = self.client.options(**options)
client.indices.exists(index="test")
self.assert_called_with_headers(client, "HEAD", "/test", headers=headers)
client = Elasticsearch(
"http://localhost:9200", transport_class=DummyTransport, **options
)
client.indices.exists(index="test")
self.assert_called_with_headers(client, "HEAD", "/test", headers=headers)
client = Elasticsearch(
"http://localhost:9200",
transport_class=DummyTransport,
headers={"Authorization": "not it"},
)
client = self.client.options(**options)
client.indices.exists(index="test")
self.assert_called_with_headers(client, "HEAD", "/test", headers=headers)
@pytest.mark.parametrize("api_key", [None, "api-key", ("api", "key")])
@pytest.mark.parametrize("bearer_auth", [None, "bearer"])
@pytest.mark.parametrize("basic_auth", [None, "user:pass", ("user", "pass")])
@pytest.mark.parametrize(
"headers", [None, {"Authorization": "value"}, {"authorization": "value"}]
)
def test_options_auth_conflicts(self, api_key, bearer_auth, basic_auth, headers):
if sum(x is not None for x in (api_key, bearer_auth, basic_auth, headers)) < 2:
pytest.skip("Skip the cases where 1 or fewer options are unset")
kwargs = {
k: v
for k, v in {
"api_key": api_key,
"bearer_auth": bearer_auth,
"basic_auth": basic_auth,
"headers": headers,
}.items()
if v is not None
}
with pytest.raises(ValueError) as e:
self.client.options(**kwargs)
assert str(e.value) in (
"Can only set one of 'api_key', 'basic_auth', and 'bearer_auth'",
"Can't set 'Authorization' HTTP header with other authentication options",
)
def test_options_passed_to_perform_request(self):
# Default transport options are 'DEFAULT' to rely on 'elastic_transport' defaults.
client = Elasticsearch(
"http://localhost:9200",
transport_class=DummyTransport,
)
client.indices.get(index="test")
calls = client.transport.calls
call = calls[("GET", "/test")][0]
assert call.pop("request_timeout") is DEFAULT
assert call.pop("max_retries") is DEFAULT
assert call.pop("retry_on_timeout") is DEFAULT
assert call.pop("retry_on_status") is DEFAULT
assert call.pop("client_meta") is DEFAULT
assert isinstance(call.pop("otel_span"), OpenTelemetrySpan)
assert call == {
"headers": {
"accept": "application/vnd.elasticsearch+json; compatible-with=9",
},
"body": None,
}
# Can be overwritten with .options()
client.options(
request_timeout=1,
max_retries=2,
retry_on_timeout=False,
retry_on_status=(404,),
).indices.get(index="test")
calls = client.transport.calls
call = calls[("GET", "/test")][1]
assert call.pop("client_meta") is DEFAULT
assert isinstance(call.pop("otel_span"), OpenTelemetrySpan)
assert call == {
"headers": {
"accept": "application/vnd.elasticsearch+json; compatible-with=9",
},
"body": None,
"request_timeout": 1,
"max_retries": 2,
"retry_on_status": (404,),
"retry_on_timeout": False,
}
# Can be overwritten on constructor
client = Elasticsearch(
"http://localhost:9200",
transport_class=DummyTransport,
request_timeout=1,
max_retries=2,
retry_on_timeout=False,
retry_on_status=(404,),
)
client.indices.get(index="test")
calls = client.transport.calls
call = calls[("GET", "/test")][0]
assert call.pop("client_meta") is DEFAULT
assert isinstance(call.pop("otel_span"), OpenTelemetrySpan)
assert call == {
"headers": {
"accept": "application/vnd.elasticsearch+json; compatible-with=9",
},
"body": None,
"request_timeout": 1,
"max_retries": 2,
"retry_on_status": (404,),
"retry_on_timeout": False,
}
@pytest.mark.asyncio
async def test_options_passed_to_async_perform_request(self):
# Default transport options are 'DEFAULT' to rely on 'elastic_transport' defaults.
client = AsyncElasticsearch(
"http://localhost:9200",
transport_class=DummyAsyncTransport,
)
await client.indices.get(index="test")
calls = client.transport.calls
call = calls[("GET", "/test")][0]
assert call.pop("request_timeout") is DEFAULT
assert call.pop("max_retries") is DEFAULT
assert call.pop("retry_on_timeout") is DEFAULT
assert call.pop("retry_on_status") is DEFAULT
assert call.pop("client_meta") is DEFAULT
assert isinstance(call.pop("otel_span"), OpenTelemetrySpan)
assert call == {
"headers": {
"accept": "application/vnd.elasticsearch+json; compatible-with=9",
},
"body": None,
}
# Can be overwritten with .options()
await client.options(
request_timeout=1,
max_retries=2,
retry_on_timeout=False,
retry_on_status=(404,),
).indices.get(index="test")
calls = client.transport.calls
call = calls[("GET", "/test")][1]
assert call.pop("client_meta") is DEFAULT
assert isinstance(call.pop("otel_span"), OpenTelemetrySpan)
assert call == {
"headers": {
"accept": "application/vnd.elasticsearch+json; compatible-with=9",
},
"body": None,
"request_timeout": 1,
"max_retries": 2,
"retry_on_status": (404,),
"retry_on_timeout": False,
}
# Can be overwritten on constructor
client = AsyncElasticsearch(
"http://localhost:9200",
transport_class=DummyAsyncTransport,
request_timeout=1,
max_retries=2,
retry_on_timeout=False,
retry_on_status=(404,),
)
await client.indices.get(index="test")
calls = client.transport.calls
call = calls[("GET", "/test")][0]
assert call.pop("client_meta") is DEFAULT
assert isinstance(call.pop("otel_span"), OpenTelemetrySpan)
assert call == {
"headers": {
"accept": "application/vnd.elasticsearch+json; compatible-with=9",
},
"body": None,
"request_timeout": 1,
"max_retries": 2,
"retry_on_status": (404,),
"retry_on_timeout": False,
}
def test_default_node_configs(self):
client = Elasticsearch(
"http://localhost:9200",
transport_class=DummyTransport,
headers={"key": "val"},
basic_auth=("username", "password"),
)
assert client._headers == {
"key": "val",
"authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
}
assert len(client.transport.hosts) == 1
node_config = client.transport.hosts[0]
assert node_config.scheme == "http"
assert node_config.host == "localhost"
assert node_config.port == 9200
assert node_config.path_prefix == ""
assert node_config.headers == {"user-agent": USER_AGENT}
def test_http_headers_overrides(self):
client = Elasticsearch(
"http://localhost:9200",
transport_class=DummyTransport,
headers={"key": "val"},
)
calls = client.transport.calls
client.indices.get(index="1")
call = calls[("GET", "/1")][0]
assert call["headers"] == {
"key": "val",
"accept": "application/vnd.elasticsearch+json; compatible-with=9",
}
client.options(headers={"key1": "val"}).indices.get(index="2")
call = calls[("GET", "/2")][0]
assert call["headers"] == {
"key": "val",
"key1": "val",
"accept": "application/vnd.elasticsearch+json; compatible-with=9",
}
client.options(headers={"key": "val2"}).indices.get(index="3")
call = calls[("GET", "/3")][0]
assert call["headers"] == {
"key": "val2",
"accept": "application/vnd.elasticsearch+json; compatible-with=9",
}
client = Elasticsearch(
"http://username:password@localhost:9200",
transport_class=DummyTransport,
headers={"key": "val"},
)
calls = client.transport.calls
node_config = client.transport.hosts[0]
assert node_config.headers == {
"authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
"user-agent": USER_AGENT,
}
assert client._headers == {"key": "val"}
def test_user_agent_override(self):
client = Elasticsearch(
"http://localhost:9200",
transport_class=DummyTransport,
)
calls = client.transport.calls
client.options(headers={"user-agent": "custom1"}).indices.get(index="1")
call = calls[("GET", "/1")][0]
assert call["headers"] == {
"user-agent": "custom1",
"accept": "application/vnd.elasticsearch+json; compatible-with=9",
}
client.indices.get(index="2", headers={"user-agent": "custom2"})
call = calls[("GET", "/2")][0]
assert call["headers"] == {
"user-agent": "custom2",
"accept": "application/vnd.elasticsearch+json; compatible-with=9",
}
client = Elasticsearch(
"http://localhost:9200",
transport_class=DummyTransport,
headers={"User-Agent": "custom3"},
)
calls = client.transport.calls
client.indices.get(index="1")
call = calls[("GET", "/1")][0]
assert call["headers"] == {
"user-agent": "custom3",
"accept": "application/vnd.elasticsearch+json; compatible-with=9",
}
client.indices.get(index="2", headers={"user-agent": "custom4"})
call = calls[("GET", "/2")][0]
assert call["headers"] == {
"user-agent": "custom4",
"accept": "application/vnd.elasticsearch+json; compatible-with=9",
}
def test_options_timeout_parameters(self):
client = Elasticsearch(
"http://localhost:9200",
transport_class=DummyTransport,
request_timeout=1,
max_retries=2,
retry_on_status=(404,),
retry_on_timeout=True,
)
# timeout parameters are preserved with .options()
client.options().indices.get(index="test")
calls = client.transport.calls
call = calls[("GET", "/test")][0]
assert call.pop("client_meta") is DEFAULT
assert isinstance(call.pop("otel_span"), OpenTelemetrySpan)
assert call == {
"headers": {
"accept": "application/vnd.elasticsearch+json; compatible-with=9",
},
"body": None,
"request_timeout": 1,
"max_retries": 2,
"retry_on_status": (404,),
"retry_on_timeout": True,
}
client = Elasticsearch(
"http://localhost:9200",
transport_class=DummyTransport,
request_timeout=1,
max_retries=2,
retry_on_status=(404,),
retry_on_timeout=True,
)
client.options(
request_timeout=2,
max_retries=3,
retry_on_status=(400,),
retry_on_timeout=False,
).indices.get(index="test")
calls = client.transport.calls
call = calls[("GET", "/test")][0]
assert call.pop("client_meta") is DEFAULT
assert isinstance(call.pop("otel_span"), OpenTelemetrySpan)
assert call == {
"headers": {
"accept": "application/vnd.elasticsearch+json; compatible-with=9",
},
"body": None,
"request_timeout": 2,
"max_retries": 3,
"retry_on_status": (400,),
"retry_on_timeout": False,
}
client = Elasticsearch(
"http://localhost:9200",
transport_class=DummyTransport,
)
client.options().indices.get(index="test")
calls = client.transport.calls
call = calls[("GET", "/test")][0]
assert call.pop("request_timeout") is DEFAULT
assert call.pop("max_retries") is DEFAULT
assert call.pop("retry_on_timeout") is DEFAULT
assert call.pop("retry_on_status") is DEFAULT
assert call.pop("client_meta") is DEFAULT
assert isinstance(call.pop("otel_span"), OpenTelemetrySpan)
assert call == {
"headers": {
"accept": "application/vnd.elasticsearch+json; compatible-with=9",
},
"body": None,
}
client = Elasticsearch(
"http://localhost:9200",
transport_class=DummyTransport,
)
client.options(
request_timeout=1,
max_retries=2,
retry_on_status=(404,),
retry_on_timeout=True,
).indices.get(index="test")
calls = client.transport.calls
call = calls[("GET", "/test")][0]
assert call.pop("client_meta") is DEFAULT
assert isinstance(call.pop("otel_span"), OpenTelemetrySpan)
assert call == {
"headers": {
"accept": "application/vnd.elasticsearch+json; compatible-with=9",
},
"body": None,
"request_timeout": 1,
"max_retries": 2,
"retry_on_status": (404,),
"retry_on_timeout": True,
}
def test_serializer_and_serializers(self):
with pytest.raises(ValueError) as e:
Elasticsearch(
"http://localhost:9200",
serializer=JsonSerializer(),
serializers={"application/json": JsonSerializer()},
)
assert str(e.value) == (
"Can't specify both 'serializer' and 'serializers' parameters together. "
"Instead only specify one of the other."
)
class CustomSerializer(JsonSerializer):
pass
client = Elasticsearch("http://localhost:9200", serializer=CustomSerializer())
assert isinstance(
client.transport.serializers.get_serializer("application/json"),
CustomSerializer,
)
assert (
set(client.transport.serializers.serializers.keys()) == EXPECTED_SERIALIZERS
)
client = Elasticsearch(
"http://localhost:9200",
serializers={
"application/json": CustomSerializer(),
"application/cbor": CustomSerializer(),
},
)
assert isinstance(
client.transport.serializers.get_serializer("application/json"),
CustomSerializer,
)
expected = EXPECTED_SERIALIZERS | {"application/cbor"}
assert set(client.transport.serializers.serializers.keys()) == expected
|