Skip to content

Commit

Permalink
Support 'opensearch' as local host variation
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ghukill committed Jan 23, 2024
1 parent 74a2377 commit d6fcce5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
5 changes: 5 additions & 0 deletions tests/test_opensearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ def test_configure_opensearch_client_for_localhost():
assert str(result) == "<OpenSearch([{'host': 'localhost', 'port': '9200'}])>"


def test_configure_opensearch_client_for_local_opensearch_host():
result = tim_os.configure_opensearch_client("opensearch")
assert str(result) == "<OpenSearch([{'host': 'opensearch', 'port': '9200'}])>"


@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")
Expand Down
2 changes: 1 addition & 1 deletion tim/opensearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit d6fcce5

Please sign in to comment.