From d6fcce59ef093acc4705f978d835ac1d76815c28 Mon Sep 17 00:00:00 2001 From: Graham Hukill Date: Tue, 23 Jan 2024 09:48:17 -0500 Subject: [PATCH] Support 'opensearch' as local host variation Why these changes are being introduced: Previously, only the string 'localhost' would trigger the code path to setup an Opensearch connection to a locally running instance. This proved problematic when attempting to have a docker container (e.g. this TIM project) connect to another docker container (e.g. an Opensearch instance) because 'localhost' was pointing at the calling TIM container and could not see the other. By allowing the string 'opensearch' to also trigger the code path for a local connection, setting the host to 'opensearch', this allows using docker networks and/or container names where one container can make requests to opensearch on the host 'opensearch'. How this addresses that need: * both 'localhost' and 'opensearch' trigger a non SSL, non authenticated opensearch connection * this TIM project running as a container can access an Opensearch client on the 'opensearch' host Side effects of this change: * None; the simple string 'opensearch' would not resolve to an actual, AWS deployed Opensearch instance Relevant ticket(s): * None --- tests/test_opensearch.py | 5 +++++ tim/opensearch.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/test_opensearch.py b/tests/test_opensearch.py index 46d0b22..16bcb1c 100644 --- a/tests/test_opensearch.py +++ b/tests/test_opensearch.py @@ -25,6 +25,11 @@ def test_configure_opensearch_client_for_localhost(): assert str(result) == "" +def test_configure_opensearch_client_for_local_opensearch_host(): + result = tim_os.configure_opensearch_client("opensearch") + assert str(result) == "" + + @mock.patch("boto3.session.Session") def test_configure_opensearch_client_for_aws(mocked_boto3_session): # noqa result = tim_os.configure_opensearch_client("fake-dev.us-east-1.es.amazonaws.com") diff --git a/tim/opensearch.py b/tim/opensearch.py index 06db54b..28857cf 100644 --- a/tim/opensearch.py +++ b/tim/opensearch.py @@ -34,7 +34,7 @@ def configure_opensearch_client(url: str) -> OpenSearch: Includes the appropriate AWS credentials configuration if the URL is not localhost. """ logger.info("OpenSearch request configurations: %s", REQUEST_CONFIG) - if url == "localhost": + if url in ["localhost", "opensearch"]: return OpenSearch( hosts=[{"host": url, "port": "9200"}], http_auth=("admin", "admin"),