Skip to content

Commit 9787251

Browse files
author
Sean Sullivan
committed
fix issue where elastic doesn't parse username correctly
1 parent bf00330 commit 9787251

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

elastic_datashader/elastic.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import copy
44
import struct
55
import time
6+
import urllib
67
from dateutil.relativedelta import relativedelta
78

89
from datashader.utils import lnglat_to_meters
910
from elasticsearch import Elasticsearch
1011
from elasticsearch_dsl import AttrDict, Search
1112

13+
import elastic_transport
1214
import pynumeral
1315
import yaml
1416

@@ -45,15 +47,28 @@ def scan(search, use_scroll=False, size=10000):
4547
else:
4648
_search = None
4749

50+
def hosts_url_to_nodeconfig(elasticsearch_hosts: str):
51+
node_configs = []
52+
for host in elasticsearch_hosts.split(","):
53+
nodeconfig = elastic_transport.client_utils.url_to_node_config(host)
54+
nodeconfig.verify_certs = False
55+
# check if host has username and password and override the basic auth due to elastic bug
56+
# https://github.com/elastic/elastic-transport-python/issues/141
57+
parsed_url = urllib.parse.urlparse(host)
58+
if parsed_url.username and parsed_url.password:
59+
nodeconfig.headers = nodeconfig.headers.copy()
60+
nodeconfig.headers["authorization"] = elastic_transport.client_utils.basic_auth_to_header((parsed_url.username, parsed_url.password))
61+
node_configs.append(nodeconfig)
62+
print(node_configs)
63+
return node_configs
4864

4965
def verify_datashader_indices(elasticsearch_hosts: str):
5066
"""Verify the ES indices exist
5167
5268
:param elasticsearch_hosts:
5369
"""
5470
es = Elasticsearch(
55-
elasticsearch_hosts.split(","),
56-
verify_certs=False,
71+
hosts_url_to_nodeconfig(elasticsearch_hosts),
5772
timeout=120
5873
)
5974

@@ -182,8 +197,7 @@ def get_search_base(
182197
x_opaque_id = params.get("x-opaque-id")
183198
# Connect to Elasticsearch
184199
es = Elasticsearch(
185-
elastic_hosts.split(","),
186-
verify_certs=False,
200+
hosts_url_to_nodeconfig(elastic_hosts),
187201
timeout=900,
188202
headers=get_es_headers(headers, user, x_opaque_id),
189203
)

0 commit comments

Comments
 (0)