|
3 | 3 | import copy |
4 | 4 | import struct |
5 | 5 | import time |
| 6 | +import urllib |
6 | 7 | from dateutil.relativedelta import relativedelta |
7 | 8 |
|
8 | 9 | from datashader.utils import lnglat_to_meters |
9 | 10 | from elasticsearch import Elasticsearch |
10 | 11 | from elasticsearch_dsl import AttrDict, Search |
11 | 12 |
|
| 13 | +import elastic_transport |
12 | 14 | import pynumeral |
13 | 15 | import yaml |
14 | 16 |
|
@@ -45,15 +47,27 @@ def scan(search, use_scroll=False, size=10000): |
45 | 47 | else: |
46 | 48 | _search = None |
47 | 49 |
|
| 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 | + return node_configs |
48 | 63 |
|
49 | 64 | def verify_datashader_indices(elasticsearch_hosts: str): |
50 | 65 | """Verify the ES indices exist |
51 | 66 |
|
52 | 67 | :param elasticsearch_hosts: |
53 | 68 | """ |
54 | 69 | es = Elasticsearch( |
55 | | - elasticsearch_hosts.split(","), |
56 | | - verify_certs=False, |
| 70 | + hosts_url_to_nodeconfig(elasticsearch_hosts), |
57 | 71 | timeout=120 |
58 | 72 | ) |
59 | 73 |
|
@@ -182,8 +196,7 @@ def get_search_base( |
182 | 196 | x_opaque_id = params.get("x-opaque-id") |
183 | 197 | # Connect to Elasticsearch |
184 | 198 | es = Elasticsearch( |
185 | | - elastic_hosts.split(","), |
186 | | - verify_certs=False, |
| 199 | + hosts_url_to_nodeconfig(elastic_hosts), |
187 | 200 | timeout=900, |
188 | 201 | headers=get_es_headers(headers, user, x_opaque_id), |
189 | 202 | ) |
|
0 commit comments