Skip to content

Commit 035330e

Browse files
Fix OpenSearch connection error with empty port parameter (#1004)
* Fix OpenSearch connection error with empty port parameter Handle empty es_port and es_host parameters gracefully to prevent 'invalid literal for int()' error when ElasticSearch/OpenSearch connection parameters are not provided in Jenkins jobs. Changes: - Add validation to check for empty host/port before conversion - Convert port to integer once and reuse the value - Provide clearer error message when connection parameters are missing - Allow jobs to continue with es=None when ES is not configured Co-authored-by: Cursor <cursoragent@cursor.com> * Review comments --------- Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent a25bc02 commit 035330e

1 file changed

Lines changed: 30 additions & 3 deletions

File tree

cloud_governance/common/elasticsearch/elasticsearch_operations.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,19 @@ def __init__(self,
4848
'ES_TIMEOUT') else timeout
4949
self.__account = self.__environment_variables_dict.get('account')
5050
self.__server_type = self.__environment_variables_dict.get('ES_SERVER_TYPE', 'opensearch')
51+
52+
# Check if ES/OpenSearch connection parameters are provided
53+
if not self.__es_port or not self.__es_host:
54+
logger.warning(f'ElasticSearch/OpenSearch not configured (es_host={self.__es_host}, es_port={self.__es_port}). Policy will run without ES upload.')
55+
self.__es = None
56+
self.__bulk_fn = None
57+
return
58+
5159
try:
60+
es_port_int = int(self.__es_port)
5261
if self.__server_type == 'elasticsearch':
53-
scheme = 'https' if int(self.__es_port) == 443 else 'http'
54-
hosts = [{'host': self.__es_host, 'port': int(self.__es_port), 'scheme': scheme}]
62+
scheme = 'https' if es_port_int == 443 else 'http'
63+
hosts = [{'host': self.__es_host, 'port': es_port_int, 'scheme': scheme}]
5564
basic_auth = (self.__es_user, self.__es_password) if self.__es_user else None
5665
self.__es = Elasticsearch(hosts, basic_auth=basic_auth, verify_certs=False,
5766
request_timeout=self.__timeout, max_retries=2)
@@ -60,14 +69,15 @@ def __init__(self,
6069
add_host = {'host': self.__es_host, 'port': self.__es_port}
6170
if self.__es_user:
6271
add_host['http_auth'] = (self.__es_user, self.__es_password)
63-
if int(self.__es_port) == 443:
72+
if es_port_int == 443:
6473
add_host['use_ssl'] = True
6574
add_host['verify_certs'] = False
6675
self.__es = OpenSearch([add_host], timeout=self.__timeout, max_retries=2)
6776
self.__bulk_fn = opensearch_bulk
6877
except Exception as err:
6978
logger.error(f'Failed to connect to {self.__server_type} at {self.__es_host}:{self.__es_port}: {err}')
7079
self.__es = None
80+
self.__bulk_fn = None
7181

7282
def __elasticsearch_get_index_hits(self, index: str, uuid: str = '', workload: str = '', fast_check: bool = False,
7383
id: bool = False):
@@ -145,6 +155,11 @@ def upload_to_elasticsearch(self, index: str, data: dict, es_add_items: dict = N
145155
:param es_add_items:
146156
:return:
147157
"""
158+
# Check if ES is configured
159+
if not self.__es:
160+
logger.debug(f'Skipping upload to ElasticSearch - not configured')
161+
return False
162+
148163
# read json to dict
149164
json_path = ""
150165

@@ -189,6 +204,9 @@ def update_elasticsearch_index(self, index: str, id: str, metadata: dict = ''):
189204
:param metadata: The metadata for enrich that existing index according to id
190205
:return:
191206
"""
207+
if not self.__es:
208+
logger.debug(f'Skipping update to ElasticSearch - not configured')
209+
return
192210
self.__es.update(index=index, id=id, body={"doc": metadata})
193211

194212
@typechecked()
@@ -314,10 +332,14 @@ def verify_elastic_index_doc_id(self, index: str, doc_id: str):
314332
"""
315333
This method verify that document present in the index or not
316334
"""
335+
if not self.__es:
336+
return False
317337
return self.__es.exists(index=index, id=doc_id)
318338

319339
def get_es_data_by_id(self, id: str, index: str):
320340
"""This method fetch the data from the es based on the match case and match value"""
341+
if not self.__es:
342+
return {}
321343
try:
322344
es_data = self.__es.get(index=index, id=id)
323345
except Exception as err:
@@ -331,6 +353,11 @@ def upload_data_in_bulk(self, data_items: list, index: str, **kwargs):
331353
:param data_items:
332354
:return:
333355
"""
356+
# Check if ES is configured
357+
if not self.__es or not self.__bulk_fn:
358+
logger.debug(f'Skipping bulk upload to ElasticSearch - not configured')
359+
return
360+
334361
total_uploaded = 0
335362
failed_items = 0
336363
for i in range(0, len(data_items), self.DEFAULT_ES_BULK_LIMIT):

0 commit comments

Comments
 (0)