Skip to content

Commit 3b01bfa

Browse files
authored
Merge pull request #19 from SamoraHunter/my-feature-branch
Add encoded API key authentication for elasticsearch on top of existing methods
2 parents ad5ebac + 0f62795 commit 3b01bfa

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

cogstack.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +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:
31+
self.elastic = elasticsearch.Elasticsearch(hosts=hosts,
32+
api_key=api_key,
33+
verify_certs=False,
34+
timeout=timeout)
35+
36+
37+
elif api:
2938
api_username, api_password = self._check_auth_details(username, password)
3039
self.elastic = elasticsearch.Elasticsearch(hosts=hosts,
3140
api_key=(api_username, api_password),
3241
verify_certs=False,
3342
timeout=timeout)
43+
3444
else:
3545
username, password = self._check_auth_details(username, password)
3646
self.elastic = elasticsearch.Elasticsearch(hosts=hosts,

credentials.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
username = None
99
password = None
1010

11+
api_key = None # Encoded api key issued by your cogstack administrator.
1112

1213
# NLM authentication
1314
# The UMLS REST API requires a UMLS account for the authentication described below.

0 commit comments

Comments
 (0)