Skip to content

Commit 05423b5

Browse files
committed
update config
1 parent 9e4fa7a commit 05423b5

File tree

1 file changed

+23
-13
lines changed
  • stac_fastapi/elasticsearch/stac_fastapi/elasticsearch

1 file changed

+23
-13
lines changed

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/config.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,44 @@
11
"""API configuration."""
22
import os
3+
import ssl
34
from typing import Any, Dict, Set
45

56
from elasticsearch import AsyncElasticsearch, Elasticsearch # type: ignore
67
from stac_fastapi.types.config import ApiSettings
78

89

910
def _es_config() -> Dict[str, Any]:
11+
# Determine the scheme (http or https)
12+
use_ssl = os.getenv("ES_USE_SSL", "true").lower() == "true"
13+
scheme = "https" if use_ssl else "http"
14+
15+
# Configure the hosts parameter with the correct scheme
16+
hosts = [f"{scheme}://{os.getenv('ES_HOST')}:{os.getenv('ES_PORT')}"]
17+
18+
# Initialize the configuration dictionary
1019
config = {
11-
"hosts": [{"host": os.getenv("ES_HOST"), "port": os.getenv("ES_PORT")}],
12-
"headers": {"accept": "application/vnd.elasticsearch+json; compatible-with=7"},
13-
"use_ssl": True,
14-
"verify_certs": True,
20+
"hosts": hosts,
21+
"headers": {"accept": "application/vnd.elasticsearch+json; compatible-with=7"}
1522
}
1623

17-
if (u := os.getenv("ES_USER")) and (p := os.getenv("ES_PASS")):
18-
config["http_auth"] = (u, p)
24+
# Explicitly exclude SSL settings when not using SSL
25+
if not use_ssl:
26+
return config
1927

20-
if (v := os.getenv("ES_USE_SSL")) and v == "false":
21-
config["use_ssl"] = False
28+
# Include SSL settings if using https
29+
config["ssl_version"] = ssl.TLSVersion.TLSv1_2
30+
config["verify_certs"] = os.getenv("ES_VERIFY_CERTS", "true").lower() != "false"
2231

23-
if (v := os.getenv("ES_VERIFY_CERTS")) and v == "false":
24-
config["verify_certs"] = False
32+
# Include CA Certificates if verifying certs
33+
if config["verify_certs"]:
34+
config["ca_certs"] = os.getenv("CURL_CA_BUNDLE", "/etc/ssl/certs/ca-certificates.crt")
2535

26-
if v := os.getenv("CURL_CA_BUNDLE"):
27-
config["ca_certs"] = v
36+
# Handle authentication
37+
if (u := os.getenv("ES_USER")) and (p := os.getenv("ES_PASS")):
38+
config["http_auth"] = (u, p)
2839

2940
return config
3041

31-
3242
_forbidden_fields: Set[str] = {"type"}
3343

3444

0 commit comments

Comments
 (0)