Skip to content

Commit c182f91

Browse files
committed
Update index-sarif-results-in-elasticsearch.py
1 parent 8806b10 commit c182f91

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

scripts/es-sarif/index-sarif-results-in-elasticsearch.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
Environment Variables:
2121
ES_LOCAL_URL - Elasticsearch host URL (default: http://localhost:9200)
2222
ES_LOCAL_API_KEY - API key for authentication (optional, enables API key auth)
23+
ES_LOCAL_USERNAME - Username for basic authentication (optional)
24+
ES_LOCAL_PASSWORD - Password for basic authentication (optional)
2325
2426
Requirements:
2527
- Python 3.11+
@@ -197,15 +199,22 @@ def replace_var(match):
197199
}
198200

199201

200-
def create_elasticsearch_client(host, api_key=None):
201-
"""Create Elasticsearch client with optional API key authentication."""
202+
def create_elasticsearch_client(host, api_key=None, username=None, password=None):
203+
"""Create Elasticsearch client with optional API key or basic authentication."""
202204
if api_key and api_key.strip():
203205
return Elasticsearch(
204206
hosts=[host],
205207
api_key=api_key.strip(),
206208
verify_certs=False, # For local development
207209
ssl_show_warn=False,
208210
)
211+
elif username and password:
212+
return Elasticsearch(
213+
hosts=[host],
214+
basic_auth=(username, password),
215+
verify_certs=False, # For local development
216+
ssl_show_warn=False,
217+
)
209218
else:
210219
return Elasticsearch(hosts=[host])
211220

@@ -411,11 +420,11 @@ def sarif_results_generator(sarif_files, index_name):
411420
)
412421

413422

414-
def index_sarif_files(sarif_files, index_name, host, api_key=None):
423+
def index_sarif_files(sarif_files, index_name, host, api_key=None, username=None, password=None):
415424
"""
416425
Connect to Elasticsearch and bulk index all SARIF results.
417426
"""
418-
es_client = create_elasticsearch_client(host, api_key)
427+
es_client = create_elasticsearch_client(host, api_key, username, password)
419428

420429
# Validate connection
421430
if not validate_elasticsearch_connection(es_client, host):
@@ -477,6 +486,8 @@ def main():
477486
print("Environment Variables:")
478487
print(" ES_LOCAL_URL - Elasticsearch host URL (default: http://localhost:9200)")
479488
print(" ES_LOCAL_API_KEY - API key for authentication (optional)")
489+
print(" ES_LOCAL_USERNAME - Username for basic authentication (optional)")
490+
print(" ES_LOCAL_PASSWORD - Password for basic authentication (optional)")
480491
print()
481492
print("Example:")
482493
print(f" python {sys.argv[0]} sarif-files.txt sarif_results_2024")
@@ -496,6 +507,8 @@ def main():
496507
# Get configuration from environment variables
497508
elastic_host = os.getenv("ES_LOCAL_URL", DEFAULT_ELASTIC_HOST)
498509
elastic_api_key = os.getenv("ES_LOCAL_API_KEY")
510+
elastic_username = os.getenv("ES_LOCAL_USERNAME")
511+
elastic_password = os.getenv("ES_LOCAL_PASSWORD")
499512

500513
# Handle variable substitution in ES_LOCAL_URL if needed
501514
if elastic_host and "${ES_LOCAL_PORT}" in elastic_host:
@@ -505,13 +518,26 @@ def main():
505518
# Treat empty string or literal "None" as None for API key
506519
if elastic_api_key == "" or elastic_api_key == "None":
507520
elastic_api_key = None
508-
521+
522+
# Treat empty strings as None for username/password
523+
if elastic_username == "" or elastic_username == "None":
524+
elastic_username = None
525+
if elastic_password == "" or elastic_password == "None":
526+
elastic_password = None
527+
528+
# Determine authentication method
529+
auth_method = "None"
530+
if elastic_api_key:
531+
auth_method = "API Key"
532+
elif elastic_username and elastic_password:
533+
auth_method = "Basic Auth (Username/Password)"
534+
509535
print(f"SARIF Files Elasticsearch Indexer")
510536
print(f"==================================")
511537
print(f"SARIF files list: {sarif_files_list}")
512538
print(f"Elasticsearch index: {index_name}")
513539
print(f"Elasticsearch host: {elastic_host}")
514-
print(f"Authentication: {'API Key' if elastic_api_key else 'None (HTTP Basic)'}")
540+
print(f"Authentication: {auth_method}")
515541
print()
516542

517543
# Read and validate SARIF files list
@@ -521,7 +547,7 @@ def main():
521547
sys.exit(1)
522548

523549
# Index the files
524-
if index_sarif_files(sarif_files, index_name, elastic_host, elastic_api_key):
550+
if index_sarif_files(sarif_files, index_name, elastic_host, elastic_api_key, elastic_username, elastic_password):
525551
print(f"\n✓ Successfully created and populated index '{index_name}'")
526552
print(f"You can now query the index using Elasticsearch APIs or Kibana.")
527553
sys.exit(0)

0 commit comments

Comments
 (0)