Fix elasticsearch authentication when initializing (#98)

This commit is contained in:
Théo Mathieu 2021-12-05 08:55:06 +01:00 committed by GitHub
parent 8a3d992aaf
commit e352a6a1e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 5 deletions

View File

@ -5,7 +5,7 @@ Apply elasticsearch mappings to given hosts.
import argparse import argparse
import json import json
from pathlib import Path from pathlib import Path
from typing import Optional, Sequence from typing import Optional, Sequence, Tuple
from elasticsearch import Elasticsearch from elasticsearch import Elasticsearch
@ -13,7 +13,7 @@ HERE = Path(__file__).resolve().parent
def apply_mappings_to_cluster( def apply_mappings_to_cluster(
hosts: Sequence, key: Optional[str] = None, es_args: dict = None hosts: Sequence, key: Optional[str] = None, es_args: dict = None, http_auth: Tuple = None
): ):
"""Hosts maybe a sequence of strings or dicts in the form {"host": <host>, "port": <port>}""" """Hosts maybe a sequence of strings or dicts in the form {"host": <host>, "port": <port>}"""
@ -30,7 +30,7 @@ def apply_mappings_to_cluster(
else: else:
files = p.glob("**/*.json") files = p.glob("**/*.json")
es = Elasticsearch(hosts=hosts, **(es_args or {})) es = Elasticsearch(hosts=hosts, http_auth=http_auth, **(es_args or {}))
return [_send_template(f) for f in files] return [_send_template(f) for f in files]

View File

@ -82,7 +82,16 @@ def check_elastic_empty() -> bool:
es_logger.addFilter(log_filter) es_logger.addFilter(log_filter)
for retry in range(max_retries): for retry in range(max_retries):
try: try:
es = Elasticsearch(hosts=cluster_conf.get("hosts")) hosts = cluster_conf.get("hosts", None)
http_auth = (
es_factory.get_credentials("events")
if cluster_conf.get("secure", True)
else None
)
args = cluster_conf.get("args", {})
es = Elasticsearch(
hosts=hosts, http_auth=http_auth, **args
)
return not es.indices.get_template(name="events*") return not es.indices.get_template(name="events*")
except exceptions.NotFoundError as ex: except exceptions.NotFoundError as ex:
log.error(ex) log.error(ex)
@ -109,5 +118,10 @@ def init_es_data():
log.info(f"Applying mappings to ES host: {hosts_config}") log.info(f"Applying mappings to ES host: {hosts_config}")
args = cluster_conf.get("args", {}) args = cluster_conf.get("args", {})
res = apply_mappings_to_cluster(hosts_config, name, es_args=args) http_auth = (
es_factory.get_credentials(name)
if cluster_conf.get("secure", True)
else None
)
res = apply_mappings_to_cluster(hosts_config, name, es_args=args, http_auth=http_auth)
log.info(res) log.info(res)