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
|
# 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 asyncio
import base64
import functools
import gzip
import os
import re
import ssl
import sys
import warnings
from typing import Optional, TypedDict, Union
from .._compat import warn_stacklevel
from .._exceptions import ConnectionError, ConnectionTimeout, SecurityWarning, TlsError
from .._models import ApiResponseMeta, HttpHeaders, NodeConfig
from ..client_utils import DEFAULT, DefaultType, client_meta_version
from ._base import (
BUILTIN_EXCEPTIONS,
DEFAULT_CA_CERTS,
RERAISE_EXCEPTIONS,
NodeApiResponse,
ssl_context_from_node_config,
)
from ._base_async import BaseAsyncNode
try:
import aiohttp
import aiohttp.client_exceptions as aiohttp_exceptions
_AIOHTTP_AVAILABLE = True
_AIOHTTP_META_VERSION = client_meta_version(aiohttp.__version__)
_version_parts = []
for _version_part in aiohttp.__version__.split(".")[:3]:
try:
_version_parts.append(int(re.search(r"^([0-9]+)", _version_part).group(1))) # type: ignore[union-attr]
except (AttributeError, ValueError):
break
_AIOHTTP_SEMVER_VERSION = tuple(_version_parts)
# See aio-libs/aiohttp#1769 and #5012
_AIOHTTP_FIXED_HEAD_BUG = _AIOHTTP_SEMVER_VERSION >= (3, 7, 0)
class RequestKwarg(TypedDict, total=False):
ssl: aiohttp.Fingerprint
except ImportError: # pragma: nocover
_AIOHTTP_AVAILABLE = False
_AIOHTTP_META_VERSION = ""
_AIOHTTP_FIXED_HEAD_BUG = False
# Avoid aiohttp enabled_cleanup_closed warning: https://github.com/aio-libs/aiohttp/pull/9726
_NEEDS_CLEANUP_CLOSED_313 = (3, 13, 0) <= sys.version_info < (3, 13, 1)
_NEEDS_CLEANUP_CLOSED = _NEEDS_CLEANUP_CLOSED_313 or sys.version_info < (3, 12, 7)
class AiohttpHttpNode(BaseAsyncNode):
"""Default asynchronous node class using the ``aiohttp`` library via HTTP.
Supports asyncio.
"""
_CLIENT_META_HTTP_CLIENT = ("ai", _AIOHTTP_META_VERSION)
def __init__(self, config: NodeConfig):
if not _AIOHTTP_AVAILABLE: # pragma: nocover
raise ValueError("You must have 'aiohttp' installed to use AiohttpHttpNode")
super().__init__(config)
self._ssl_assert_fingerprint = config.ssl_assert_fingerprint
ssl_context: Optional[ssl.SSLContext] = None
if config.scheme == "https":
if config.ssl_context is not None:
ssl_context = ssl_context_from_node_config(config)
else:
ssl_context = ssl_context_from_node_config(config)
ca_certs = (
DEFAULT_CA_CERTS if config.ca_certs is None else config.ca_certs
)
if config.verify_certs:
if not ca_certs:
raise ValueError(
"Root certificates are missing for certificate "
"validation. Either pass them in using the ca_certs parameter or "
"install certifi to use it automatically."
)
else:
if config.ssl_show_warn:
warnings.warn(
f"Connecting to {self.base_url!r} using TLS with verify_certs=False is insecure",
stacklevel=warn_stacklevel(),
category=SecurityWarning,
)
if ca_certs is not None:
if os.path.isfile(ca_certs):
ssl_context.load_verify_locations(cafile=ca_certs)
elif os.path.isdir(ca_certs):
ssl_context.load_verify_locations(capath=ca_certs)
else:
raise ValueError("ca_certs parameter is not a path")
# Use client_cert and client_key variables for SSL certificate configuration.
if config.client_cert and not os.path.isfile(config.client_cert):
raise ValueError("client_cert is not a path to a file")
if config.client_key and not os.path.isfile(config.client_key):
raise ValueError("client_key is not a path to a file")
if config.client_cert and config.client_key:
ssl_context.load_cert_chain(config.client_cert, config.client_key)
elif config.client_cert:
ssl_context.load_cert_chain(config.client_cert)
self._loop: asyncio.AbstractEventLoop = None # type: ignore[assignment]
self.session: Optional[aiohttp.ClientSession] = None
# Parameters for creating an aiohttp.ClientSession later.
self._connections_per_node = config.connections_per_node
self._ssl_context = ssl_context
async def perform_request( # type: ignore[override]
self,
method: str,
target: str,
body: Optional[bytes] = None,
headers: Optional[HttpHeaders] = None,
request_timeout: Union[DefaultType, Optional[float]] = DEFAULT,
) -> NodeApiResponse:
if self.session is None:
self._create_aiohttp_session()
assert self.session is not None
url = self.base_url + target
is_head = False
# There is a bug in aiohttp<3.7 that disables the re-use
# of the connection in the pool when method=HEAD.
# See: aio-libs/aiohttp#1769
if method == "HEAD" and not _AIOHTTP_FIXED_HEAD_BUG:
method = "GET"
is_head = True
# total=0 means no timeout for aiohttp
resolved_timeout: Optional[float] = (
self.config.request_timeout
if request_timeout is DEFAULT
else request_timeout
)
aiohttp_timeout = aiohttp.ClientTimeout(
total=resolved_timeout if resolved_timeout is not None else 0
)
request_headers = self._headers.copy()
if headers:
request_headers.update(headers)
body_to_send: Optional[bytes]
if body:
if self._http_compress:
body_to_send = gzip.compress(body)
request_headers["content-encoding"] = "gzip"
else:
body_to_send = body
else:
body_to_send = None
kwargs: RequestKwarg = {}
if self._ssl_assert_fingerprint:
kwargs["ssl"] = aiohttp_fingerprint(self._ssl_assert_fingerprint)
try:
start = self._loop.time()
async with self.session.request(
method,
url,
data=body_to_send,
headers=request_headers,
timeout=aiohttp_timeout,
**kwargs,
) as response:
if is_head: # We actually called 'GET' so throw away the data.
await response.release()
raw_data = b""
else:
raw_data = await response.read()
duration = self._loop.time() - start
# We want to reraise a cancellation or recursion error.
except RERAISE_EXCEPTIONS:
raise
except Exception as e:
err: Exception
if isinstance(
e, (asyncio.TimeoutError, aiohttp_exceptions.ServerTimeoutError)
):
err = ConnectionTimeout(
"Connection timed out during request", errors=(e,)
)
elif isinstance(e, (ssl.SSLError, aiohttp_exceptions.ClientSSLError)):
err = TlsError(str(e), errors=(e,))
elif isinstance(e, BUILTIN_EXCEPTIONS):
raise
else:
err = ConnectionError(str(e), errors=(e,))
self._log_request(
method="HEAD" if is_head else method,
target=target,
headers=request_headers,
body=body,
exception=err,
)
raise err from None
meta = ApiResponseMeta(
node=self.config,
duration=duration,
http_version="1.1",
status=response.status,
headers=HttpHeaders(response.headers),
)
self._log_request(
method="HEAD" if is_head else method,
target=target,
headers=request_headers,
body=body,
meta=meta,
response=raw_data,
)
return NodeApiResponse(
meta,
raw_data,
)
async def close(self) -> None: # type: ignore[override]
if self.session:
await self.session.close()
self.session = None
def _create_aiohttp_session(self) -> None:
"""Creates an aiohttp.ClientSession(). This is delayed until
the first call to perform_request() so that AsyncTransport has
a chance to set AiohttpHttpNode.loop
"""
if self._loop is None:
self._loop = asyncio.get_running_loop()
self.session = aiohttp.ClientSession(
headers=self.headers,
skip_auto_headers=("accept", "accept-encoding", "user-agent"),
auto_decompress=True,
loop=self._loop,
cookie_jar=aiohttp.DummyCookieJar(),
connector=aiohttp.TCPConnector(
limit_per_host=self._connections_per_node,
use_dns_cache=True,
enable_cleanup_closed=_NEEDS_CLEANUP_CLOSED,
ssl=self._ssl_context or False,
),
)
@functools.lru_cache(maxsize=64, typed=True)
def aiohttp_fingerprint(ssl_assert_fingerprint: str) -> "aiohttp.Fingerprint":
"""Changes 'ssl_assert_fingerprint' into a configured 'aiohttp.Fingerprint' instance.
Uses a cache to prevent creating tons of objects needlessly.
"""
return aiohttp.Fingerprint(
base64.b16decode(ssl_assert_fingerprint.replace(":", ""), casefold=True)
)
|