Skip to content

Commit

Permalink
get_client kwargs handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jensens committed Nov 28, 2023
1 parent a1f0114 commit 7d50a26
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

2.0.0rc2 (2023-xx-xx)
--------------------

- Fix: Default address for ElasticSearch needs ``https://`` prefix [jensens]
- Feature: set env ``INDEX_VERIFY_CERTS=1`` to pass as ``verify_certs`` kwarg in OpenSearch client [jensens]

2.0.0rc1 (2023-11-27)
--------------------

Expand Down
13 changes: 9 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,25 @@ Environment

Environment variables are:

INDEX_OPENSEARCH
Whether to use OpenSearch or ElasticSearch.

Default: 1

INDEX_SERVER
The URL of the ElasticSearch or OpenSearch server.

Default: localhost:9200

INDEX_USE_SSL
Whether to use a secure connection or not.
Whether to use a secure TLS connection or not.

Default: 0

INDEX_OPENSEARCH
Whether to use OpenSearch or ElasticSearch.
INDEX_VERIFY_CERTS
Whether to verify TLS certificates on secure connection or not.

Default: 1
Default: 0

INDEX_LOGIN
Username for the ElasticSearch 8+ or OpenSearch server.
Expand Down
27 changes: 15 additions & 12 deletions src/collective/elastic/ingest/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def get_client(index_server_baseurl: str = ""):
raw_addr = index_server_baseurl or os.environ.get("INDEX_SERVER", "")
addresses = [x for x in raw_addr.split(",") if x.strip()]
if not addresses:
addresses.append("127.0.0.1:9200")
addresses.append("127.0.0.1:9200" if OPENSEARCH else "https://localhost:9200")

# TODO: more auth options (cert, bearer token, api-key, etc)
auth = (
Expand All @@ -36,17 +36,20 @@ def get_client(index_server_baseurl: str = ""):

if OPENSEARCH:
logger.info(f"Use OpenSearch client at {addresses}")
hosts = []
for address in addresses:
host, port = address.rsplit(":", 1)
hosts.append({"host": host, "port": port})
use_ssl = bool(int(os.environ.get("INDEX_USE_SSL", "0")))
client = OpenSearch(
hosts=hosts,
http_auth=auth,
use_ssl=use_ssl,
verify_certs=False,
)
kwargs = {
"hosts": [
dict(zip(("host", "port"), address.rsplit(":", 1)))
for address in addresses
]
}
kwargs["use_ssl"] = bool(int(os.environ.get("INDEX_USE_SSL", "0")))
if kwargs["use_ssl"]:
kwargs["verify_certs"] = bool(
int(os.environ.get("INDEX_VERIFY_CERTS", "0"))
)
kwargs["http_auth"] = auth
logger.info(f"OpenSearch client kwargs: {kwargs}")
client = OpenSearch(**kwargs)
info = client.info()
logger.info(f"OpenSearch client info: {info}")
else:
Expand Down

0 comments on commit 7d50a26

Please sign in to comment.