Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions backend/apps/db/es_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,26 @@
from common.error import SingleMessageError


def get_es_auth(conf: DatasourceConf):
username = f"{conf.username}"
password = f"{conf.password}"

credentials = f"{username}:{password}"
encoded_credentials = b64encode(credentials.encode()).decode()

return {
"Content-Type": "application/json",
"Authorization": f"Basic {encoded_credentials}"
}


def get_es_connect(conf: DatasourceConf):
es_client = Elasticsearch(
[conf.host], # ES address
basic_auth=(conf.username, conf.password),
verify_certs=False,
compatibility_mode=True
compatibility_mode=True,
headers=get_es_auth(conf)
)
return es_client

Expand Down Expand Up @@ -95,18 +109,8 @@ def get_es_data_by_http(conf: DatasourceConf, sql: str):
url = url[:-1]

host = f'{url}/_sql?format=json'
username = f"{conf.username}"
password = f"{conf.password}"

credentials = f"{username}:{password}"
encoded_credentials = b64encode(credentials.encode()).decode()

headers = {
"Content-Type": "application/json",
"Authorization": f"Basic {encoded_credentials}"
}

response = requests.post(host, data=json.dumps({"query": sql}), headers=headers, verify=False)
response = requests.post(host, data=json.dumps({"query": sql}), headers=get_es_auth(conf), verify=False)

# print(response.json())
res = response.json()
Expand Down