From 4dba35deead7edc2d6d2c80e1ba93362a6c9af0e Mon Sep 17 00:00:00 2001 From: Michael Oliver Date: Wed, 19 Jul 2023 14:02:04 +0100 Subject: [PATCH] Allow passing `trust_env` to `aiohttp.ClientSession` (#438) Fixes #368 Signed-off-by: Michael Oliver --- CHANGELOG.md | 2 ++ USER_GUIDE.md | 31 +++++++++++++++++++ opensearchpy/_async/http_aiohttp.py | 3 ++ opensearchpy/_async/http_aiohttp.pyi | 1 + .../test_async/test_connection.py | 14 +++++++++ 5 files changed, 51 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ad64bac..a85497c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Enhanced YAML test runner to use OpenSearch rest-api-spec YAML tests ([#414](https://github.com/opensearch-project/opensearch-py/pull/414) - Added `Search#collapse` ([#409](https://github.com/opensearch-project/opensearch-py/issues/409)) - Added support for the ISM API ([#398](https://github.com/opensearch-project/opensearch-py/pull/398)) +- Added `trust_env` to `AIOHttpConnection` ([#398](https://github.com/opensearch-project/opensearch-py/pull/438)) + ### Changed - Upgrading pytest-asyncio to latest version - 0.21.0 ([#339](https://github.com/opensearch-project/opensearch-py/pull/339)) - Fixed flaky CI tests by replacing httpbin with a simple http_server ([#395](https://github.com/opensearch-project/opensearch-py/pull/395)) diff --git a/USER_GUIDE.md b/USER_GUIDE.md index 568a6c46..39fac299 100644 --- a/USER_GUIDE.md +++ b/USER_GUIDE.md @@ -35,6 +35,7 @@ - [Pre-requisites to use `AWSV4SignerAuth`](#pre-requisites-to-use-awsv4signerauth) - [Using IAM authentication with an async client](#using-iam-authentication-with-an-async-client) - [Using Kerberos](#using-kerberos) + - [Using environment settings for proxy configuration](#using-environment-settings-for-proxy-configuration) # User guide of OpenSearch Python client @@ -686,3 +687,33 @@ client = OpenSearch( health = client.cluster.health() ``` + +## Using environment settings for proxy configuration + +Tell connection to get proxy information from `HTTP_PROXY` / `HTTPS_PROXY` environment variables or `~/.netrc` file if present. + +```python +from opensearchpy import OpenSearch, RequestsHttpConnection + + +OpenSearch( + hosts=["htps://..."], + use_ssl=True, + verify_certs=True, + connection_class=RequestsHttpConnection, + trust_env=True, +) +``` + + +```python +from opensearchpy import AsyncOpenSearch, AIOHttpConnection + +client = AsyncOpenSearch( + hosts=["htps://..."], + use_ssl=True, + verify_certs=True, + connection_class=AIOHttpConnection, + trust_env=True, +) +``` diff --git a/opensearchpy/_async/http_aiohttp.py b/opensearchpy/_async/http_aiohttp.py index f630a3bd..cc426164 100644 --- a/opensearchpy/_async/http_aiohttp.py +++ b/opensearchpy/_async/http_aiohttp.py @@ -91,6 +91,7 @@ def __init__( http_compress=None, opaque_id=None, loop=None, + trust_env=False, **kwargs ): """ @@ -219,6 +220,7 @@ def __init__( self._limit = maxsize self._http_auth = http_auth self._ssl_context = ssl_context + self._trust_env = trust_env async def perform_request( self, method, url, params=None, body=None, timeout=None, ignore=(), headers=None @@ -367,6 +369,7 @@ async def _create_aiohttp_session(self): connector=aiohttp.TCPConnector( limit=self._limit, use_dns_cache=True, ssl=self._ssl_context ), + trust_env=self._trust_env, ) diff --git a/opensearchpy/_async/http_aiohttp.pyi b/opensearchpy/_async/http_aiohttp.pyi index f148830c..4dea4317 100644 --- a/opensearchpy/_async/http_aiohttp.pyi +++ b/opensearchpy/_async/http_aiohttp.pyi @@ -66,5 +66,6 @@ class AIOHttpConnection(AsyncConnection): http_compress: Optional[bool] = ..., opaque_id: Optional[str] = ..., loop: Any = ..., + trust_env: bool = ..., **kwargs: Any ) -> None: ... diff --git a/test_opensearchpy/test_async/test_connection.py b/test_opensearchpy/test_async/test_connection.py index 323237d2..147a6a3a 100644 --- a/test_opensearchpy/test_async/test_connection.py +++ b/test_opensearchpy/test_async/test_connection.py @@ -274,6 +274,20 @@ async def test_uses_no_ca_certs(self, load_verify_locations): AIOHttpConnection(use_ssl=True, verify_certs=False) load_verify_locations.assert_not_called() + async def test_trust_env(self): + con = AIOHttpConnection(trust_env=True) + await con._create_aiohttp_session() + + assert con._trust_env is True + assert con.session.trust_env is True + + async def test_trust_env_default_value_is_false(self): + con = AIOHttpConnection() + await con._create_aiohttp_session() + + assert con._trust_env is False + assert con.session.trust_env is False + @patch("opensearchpy.connection.base.logger") async def test_uncompressed_body_logged(self, logger): con = await self._get_mock_connection(connection_params={"http_compress": True})