Skip to content

Commit

Permalink
ADD: support for elasticsearch http basic auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzay-G committed Aug 2, 2021
1 parent e600b29 commit 5a7f5f8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
8 changes: 4 additions & 4 deletions archivy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
"Search is enabled but engine option is invalid or absent. Archivy will try to guess preferred search engine."
)
app.config["SEARCH_CONF"]["engine"] = "none"
es = elasticsearch.Elasticsearch(app.config["SEARCH_CONF"]["url"])
try:
es.cluster.health()

es = get_elastic_client(error_if_invalid=False)
if es:
app.config["SEARCH_CONF"]["engine"] = "elasticsearch"
except elasticsearch.exceptions.ConnectionError:
else:
if which("rg"):
app.config["SEARCH_CONF"]["engine"] = "ripgrep"
engine = app.config["SEARCH_CONF"]["engine"]
Expand Down
27 changes: 23 additions & 4 deletions archivy/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,33 @@ def test_es_connection(es):
)


def get_elastic_client():
def get_elastic_client(error_if_invalid=True):
"""Returns the elasticsearch client you can use to search and insert / delete data"""
if (
not current_app.config["SEARCH_CONF"]["enabled"]
or current_app.config["SEARCH_CONF"]["engine"] != "elasticsearch"
):
) and error_if_invalid:
return None

es = Elasticsearch(current_app.config["SEARCH_CONF"]["url"])
test_es_connection(es)
auth_setup = (
current_app.config["SEARCH_CONF"]["es_user"]
and current_app.config["SEARCH_CONF"]["es_password"]
)
if auth_setup:
es = Elasticsearch(
current_app.config["SEARCH_CONF"]["url"],
http_auth=(
current_app.config["SEARCH_CONF"]["es_user"],
current_app.config["SEARCH_CONF"]["es_password"],
),
)
else:
es = Elasticsearch(current_app.config["SEARCH_CONF"]["url"])
if error_if_invalid:
test_es_connection(es)
else:
try:
es.cluster.health()
except elasticsearch.exceptions.ConnectionError:
return False
return es

0 comments on commit 5a7f5f8

Please sign in to comment.