Skip to content

Commit 0f62795

Browse files
committed
Add support for both Elasticsearch authentication methods:
- Maintain backward compatibility with ES 7.17 API key auth (username/password tuple) - Add support for ES 8.17 string-based API key - Update docstring to clarify version requirements for each auth method - Add optional api_key parameter to constructor with default=None
1 parent a5eb8bd commit 0f62795

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

cogstack.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,27 @@ class CogStack(object):
2020
hosts (List[str]): A list of Elasticsearch host URLs.
2121
username (str, optional): The username to use when connecting to Elasticsearch. If not provided, the user will be prompted to enter a username.
2222
password (str, optional): The password to use when connecting to Elasticsearch. If not provided, the user will be prompted to enter a password.
23-
api (bool, optional): A boolean value indicating whether to use API keys or basic authentication to connect to Elasticsearch. Defaults to False (i.e., use basic authentication).
23+
api (bool, optional): A boolean value indicating whether to use API keys or basic authentication to connect to Elasticsearch. Defaults to False (i.e., use basic authentication). Elasticsearch 7.17.
24+
api_key (str, optional): The API key to use when connecting to Elasticsearch.
25+
When provided along with `api=True`, this takes precedence over username/password. Only available when using Elasticsearch 8.17.
2426
"""
2527
def __init__(self, hosts: List, username: Optional[str] = None, password: Optional[str] = None,
26-
api: bool = False, timeout: Optional[int]=60):
28+
api: bool = False, timeout: Optional[int]=60, api_key: Optional[str] = None):
2729

28-
if api:
30+
if api_key and api:
2931
self.elastic = elasticsearch.Elasticsearch(hosts=hosts,
3032
api_key=api_key,
3133
verify_certs=False,
3234
timeout=timeout)
35+
36+
37+
elif api:
38+
api_username, api_password = self._check_auth_details(username, password)
39+
self.elastic = elasticsearch.Elasticsearch(hosts=hosts,
40+
api_key=(api_username, api_password),
41+
verify_certs=False,
42+
timeout=timeout)
43+
3344
else:
3445
username, password = self._check_auth_details(username, password)
3546
self.elastic = elasticsearch.Elasticsearch(hosts=hosts,

0 commit comments

Comments
 (0)