Files
opensearch-pyd/opensearchpy/connection/http_urllib3.py
T

330 lines
12 KiB
Python
Raw Normal View History

# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
#
# Modifications Copyright OpenSearch Contributors. See
# GitHub history for details.
#
# 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.
2020-04-23 11:22:08 -05:00
import ssl
import time
2015-01-29 23:46:01 +01:00
import warnings
2023-11-06 13:08:19 -05:00
from typing import Any, Callable, Collection, Mapping, Optional, Union
2023-11-06 13:08:19 -05:00
import urllib3
from urllib3.exceptions import ReadTimeoutError
2023-11-06 13:08:19 -05:00
from urllib3.exceptions import SSLError as UrllibSSLError
from urllib3.util.retry import Retry
from opensearchpy.metrics import Metrics, MetricsNone
from ..compat import reraise_exceptions, urlencode
2020-03-09 11:51:35 -05:00
from ..exceptions import (
ConnectionError,
ConnectionTimeout,
ImproperlyConfigured,
2020-03-09 11:51:35 -05:00
SSLError,
)
from .base import Connection
2020-03-09 11:51:35 -05:00
# sentinel value for `verify_certs` and `ssl_show_warn`.
# This is used to detect if a user is passing in a value
# for SSL kwargs if also using an SSLContext.
VERIFY_CERTS_DEFAULT = object()
SSL_SHOW_WARN_DEFAULT = object()
2023-11-06 13:08:19 -05:00
def create_ssl_context(**kwargs: Any) -> Any:
"""
A helper function around creating an SSL context
https://docs.python.org/3/library/ssl.html#context-creation
Accepts kwargs in the same manner as `create_default_context`.
"""
ctx = ssl.create_default_context(**kwargs)
return ctx
class Urllib3HttpConnection(Connection):
"""
Default connection class using the `urllib3` library and the http protocol.
:arg host: hostname of the node (default: localhost)
:arg port: port to use (integer, default: 9200)
2021-08-13 15:51:50 +05:30
:arg url_prefix: optional url prefix for opensearch
:arg timeout: default timeout in seconds (float, default: 10)
:arg http_auth: optional http auth information as either ':' separated
string or a tuple
:arg use_ssl: use ssl for the connection if `True`
:arg verify_certs: whether to verify SSL certificates
2019-03-28 20:20:16 +01:00
:arg ssl_show_warn: show warning when verify certs is disabled
:arg ca_certs: optional path to CA bundle.
See https://urllib3.readthedocs.io/en/latest/security.html#using-certifi-with-urllib3
for instructions how to get default set
:arg client_cert: path to the file containing the private key and the
certificate, or cert only if using client_key
:arg client_key: path to the file containing the private key if using
separate cert and key files (client_cert will contain only the cert)
:arg ssl_version: version of the SSL protocol to use. Choices are:
SSLv23 (default) SSLv2 SSLv3 TLSv1 (see ``PROTOCOL_*`` constants in the
``ssl`` module for exact options for your environment).
:arg ssl_assert_hostname: use hostname verification if not `False`
:arg ssl_assert_fingerprint: verify the supplied certificate fingerprint if not `None`
:arg pool_maxsize: the number of connections which will be kept open to this
2016-10-17 17:25:50 +02:00
host. See https://urllib3.readthedocs.io/en/1.4/pools.html#api for more
information.
2016-07-12 18:13:11 +02:00
:arg headers: any custom http headers to be add to requests
2018-03-13 12:34:25 -04:00
:arg http_compress: Use gzip compression
2020-03-11 16:33:15 -05:00
:arg opaque_id: Send this value in the 'X-Opaque-Id' HTTP header
For tracing all requests made by this transport.
:arg metrics: metrics is an instance of a subclass of the
:class:`~opensearchpy.Metrics` class, used for collecting
and reporting metrics related to the client's operations;
"""
2019-03-29 09:25:23 -06:00
def __init__(
self,
2023-11-06 13:08:19 -05:00
host: str = "localhost",
port: Optional[int] = None,
http_auth: Any = None,
use_ssl: bool = False,
verify_certs: Any = VERIFY_CERTS_DEFAULT,
ssl_show_warn: Any = SSL_SHOW_WARN_DEFAULT,
ca_certs: Any = None,
client_cert: Any = None,
client_key: Any = None,
ssl_version: Any = None,
ssl_assert_hostname: Any = None,
ssl_assert_fingerprint: Any = None,
pool_maxsize: Any = None,
headers: Any = None,
ssl_context: Any = None,
http_compress: Any = None,
opaque_id: Any = None,
metrics: Metrics = MetricsNone(),
2023-11-06 13:08:19 -05:00
**kwargs: Any
) -> None:
self.metrics = metrics
# Initialize headers before calling super().__init__().
self.headers = urllib3.make_headers(keep_alive=True)
super(Urllib3HttpConnection, self).__init__(
host=host,
port=port,
use_ssl=use_ssl,
headers=headers,
http_compress=http_compress,
2020-03-11 16:33:15 -05:00
opaque_id=opaque_id,
**kwargs
)
self.http_auth = http_auth
if self.http_auth is not None:
2023-11-06 13:08:19 -05:00
if isinstance(self.http_auth, Callable): # type: ignore
pass
elif isinstance(self.http_auth, (tuple, list)):
self.headers.update(
urllib3.make_headers(basic_auth=":".join(http_auth))
)
else:
self.headers.update(urllib3.make_headers(basic_auth=http_auth))
2023-11-06 13:08:19 -05:00
pool_class: Any = urllib3.HTTPConnectionPool
kw = {}
# if providing an SSL context, raise error if any other SSL related flag is used
2019-03-29 09:25:23 -06:00
if ssl_context and (
(verify_certs is not VERIFY_CERTS_DEFAULT)
or (ssl_show_warn is not SSL_SHOW_WARN_DEFAULT)
2019-03-29 09:25:23 -06:00
or ca_certs
or client_cert
or client_key
or ssl_version
):
warnings.warn(
"When using `ssl_context`, all other SSL related kwargs are ignored"
)
# if ssl_context provided use SSL by default
if ssl_context and self.use_ssl:
pool_class = urllib3.HTTPSConnectionPool
2019-03-29 09:25:23 -06:00
kw.update(
{
"assert_fingerprint": ssl_assert_fingerprint,
"ssl_context": ssl_context,
}
)
elif self.use_ssl:
pool_class = urllib3.HTTPSConnectionPool
2019-03-29 09:25:23 -06:00
kw.update(
{
"ssl_version": ssl_version,
"assert_hostname": ssl_assert_hostname,
"assert_fingerprint": ssl_assert_fingerprint,
}
)
# Convert all sentinel values to their actual default
# values if not using an SSLContext.
if verify_certs is VERIFY_CERTS_DEFAULT:
verify_certs = True
if ssl_show_warn is SSL_SHOW_WARN_DEFAULT:
ssl_show_warn = True
ca_certs = self.default_ca_certs() if ca_certs is None else ca_certs
if verify_certs:
if not ca_certs:
2019-03-29 09:25:23 -06:00
raise ImproperlyConfigured(
"Root certificates are missing for certificate "
"validation. Either pass them in using the ca_certs parameter or "
2019-03-29 09:25:23 -06:00
"install certifi to use it automatically."
)
2019-03-29 09:25:23 -06:00
kw.update(
{
"cert_reqs": "CERT_REQUIRED",
"ca_certs": ca_certs,
"cert_file": client_cert,
"key_file": client_key,
}
)
else:
kw["cert_reqs"] = "CERT_NONE"
2019-03-28 20:20:16 +01:00
if ssl_show_warn:
warnings.warn(
2019-03-29 09:25:23 -06:00
"Connecting to %s using SSL with verify_certs=False is insecure."
% self.host
2019-03-29 09:25:23 -06:00
)
if not ssl_show_warn:
urllib3.disable_warnings()
if pool_maxsize and isinstance(pool_maxsize, int):
kw["maxsize"] = pool_maxsize
2024-01-02 19:33:24 +01:00
self._urllib3_pool_factory = lambda: pool_class(
self.hostname, port=self.port, timeout=self.timeout, **kw
2019-03-29 09:25:23 -06:00
)
2024-01-02 19:33:24 +01:00
self._create_urllib3_pool()
def _create_urllib3_pool(self) -> None:
self.pool = self._urllib3_pool_factory() # type: ignore
2019-03-29 09:25:23 -06:00
def perform_request(
2023-11-06 13:08:19 -05:00
self,
method: str,
url: str,
params: Optional[Mapping[str, Any]] = None,
body: Optional[bytes] = None,
timeout: Optional[Union[int, float]] = None,
ignore: Collection[int] = (),
headers: Optional[Mapping[str, str]] = None,
) -> Any:
2024-01-02 19:33:24 +01:00
if self.pool is None:
self._create_urllib3_pool()
assert self.pool is not None
url = self.url_prefix + url
if params:
2019-03-29 09:25:23 -06:00
url = "%s?%s" % (url, urlencode(params))
full_url = self.host + url
start = time.time()
orig_body = body
try:
kw = {}
if timeout:
2019-03-29 09:25:23 -06:00
kw["timeout"] = timeout
2014-10-03 17:32:11 +02:00
2015-02-26 14:14:33 -08:00
# in python2 we need to make sure the url and method are not
# unicode. Otherwise the body will be decoded into unicode too and
# that will fail (#133, #201).
if not isinstance(url, str):
2019-03-29 09:25:23 -06:00
url = url.encode("utf-8")
2015-02-26 14:14:33 -08:00
if not isinstance(method, str):
2019-03-29 09:25:23 -06:00
method = method.encode("utf-8")
2014-10-03 17:32:11 +02:00
request_headers = self.headers.copy()
request_headers.update(headers or ())
2018-03-13 12:34:25 -04:00
if self.http_compress and body:
body = self._gzip_compress(body)
request_headers["content-encoding"] = "gzip"
2018-05-18 13:52:37 -06:00
if self.http_auth is not None:
2023-11-06 13:08:19 -05:00
if isinstance(self.http_auth, Callable): # type: ignore
request_headers.update(self.http_auth(method, full_url, body))
self.metrics.request_start()
2019-03-29 09:25:23 -06:00
response = self.pool.urlopen(
method, url, body, retries=Retry(False), headers=request_headers, **kw
)
duration = time.time() - start
raw_data = response.data.decode("utf-8", "surrogatepass")
except reraise_exceptions:
raise
except Exception as e:
2019-03-29 09:25:23 -06:00
self.log_request_fail(
method, full_url, url, orig_body, time.time() - start, exception=e
2019-03-29 09:25:23 -06:00
)
2016-12-15 16:46:05 +11:00
if isinstance(e, UrllibSSLError):
2019-03-29 09:25:23 -06:00
raise SSLError("N/A", str(e), e)
2016-12-15 16:46:05 +11:00
if isinstance(e, ReadTimeoutError):
2019-03-29 09:25:23 -06:00
raise ConnectionTimeout("TIMEOUT", str(e), e)
raise ConnectionError("N/A", str(e), e)
finally:
self.metrics.request_end()
# raise warnings if any from the 'Warnings' header.
warning_headers = response.headers.get_all("warning", ())
self._raise_warnings(warning_headers)
2016-12-15 16:46:05 +11:00
# raise errors based on http status codes, let the client handle those if needed
if not (200 <= response.status < 300) and response.status not in ignore:
2019-03-29 09:25:23 -06:00
self.log_request_fail(
method, full_url, url, orig_body, duration, response.status, raw_data
2019-03-29 09:25:23 -06:00
)
self._raise_error(
response.status,
raw_data,
self.get_response_headers(response).get("content-type"),
)
2019-03-29 09:25:23 -06:00
self.log_request_success(
method, full_url, url, orig_body, response.status, raw_data, duration
2019-03-29 09:25:23 -06:00
)
return response.status, response.headers, raw_data
2023-11-06 13:08:19 -05:00
def get_response_headers(self, response: Any) -> Any:
return {header.lower(): value for header, value in response.headers.items()}
2023-11-06 13:08:19 -05:00
def close(self) -> None:
"""
Explicitly closes connection
"""
2024-01-02 19:33:24 +01:00
if self.pool:
self.pool.close()
self.pool = None