Skip to content

Commit fefdfb2

Browse files
author
Phil Varner
authored
Merge pull request #81 from stac-utils/pv/deployable-image
add env vars to configure auth and ssl for ES instance
2 parents 6ad8591 + 73041a1 commit fefdfb2

File tree

5 files changed

+55
-31
lines changed

5 files changed

+55
-31
lines changed

.github/workflows/cicd.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,13 @@ jobs:
6565
cd stac_fastapi/elasticsearch && pipenv run pytest -svvv
6666
env:
6767
ENVIRONMENT: testing
68-
ES_USER: dev
69-
ES_PASS: stac
68+
# ES_USER: dev
69+
# ES_PASS: stac
7070
ES_PORT: 9200
7171
ES_HOST: 172.17.0.1
72-
#
72+
ES_USE_SSL: false
73+
ES_VERIFY_CERTS: false
74+
7375
# - name: Run test suite against Elasticsearch 8.x
7476
# run: |
7577
# cd stac_fastapi/elasticsearch && pipenv run pytest -svvv

Makefile

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ APP_HOST ?= 0.0.0.0
33
APP_PORT ?= 8080
44
EXTERNAL_APP_PORT ?= ${APP_PORT}
55

6+
APP_PORT ?= 8080
7+
ES_HOST ?= docker.for.mac.localhost
8+
ES_PORT ?= 9200
9+
610
run_es = docker-compose \
711
run \
812
-p ${EXTERNAL_APP_PORT}:${APP_PORT} \
@@ -11,18 +15,17 @@ run_es = docker-compose \
1115
-e APP_PORT=${APP_PORT} \
1216
app-elasticsearch
1317

14-
.PHONY: image
15-
image:
18+
.PHONY: image-deploy
19+
image-deploy:
1620
docker build -f Dockerfile.deploy -t stac-fastapi-elasticsearch:latest .
1721

18-
.PHONY: run
19-
run:
22+
.PHONY: run-deploy-locally
23+
run-deploy-locally:
2024
docker run -it -p 8080:8080 \
21-
-e ENVIRONMENT=local \
22-
-e ES_HOST=docker.for.mac.localhost \
23-
-e ES_PORT=9200 \
24-
-e ES_USER=dev \
25-
-e ES_PASS=stac \
25+
-e ES_HOST=${ES_HOST} \
26+
-e ES_PORT=${ES_PORT} \
27+
-e ES_USER=${ES_USER} \
28+
-e ES_PASS=${ES_PASS} \
2629
stac-fastapi-elasticsearch:latest
2730

2831
.PHONY: image-dev

docker-compose.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ services:
1414
- RELOAD=false
1515
- ENVIRONMENT=local
1616
- WEB_CONCURRENCY=10
17-
- ES_USER=dev
18-
- ES_PASS=stac
19-
- ES_PORT=9200
2017
- ES_HOST=172.17.0.1
18+
- ES_PORT=9200
19+
# - ES_USER=dev
20+
# - ES_PASS=stac
21+
- ES_USE_SSL=false
22+
- ES_VERIFY_CERTS=false
2123
ports:
2224
- "8080:8080"
2325
volumes:
Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,57 @@
11
"""API configuration."""
22
import os
3-
from typing import Set
3+
from typing import Any, Dict, Set
44

55
from elasticsearch import AsyncElasticsearch, Elasticsearch
66

77
from stac_fastapi.types.config import ApiSettings
88

9-
DOMAIN = os.getenv("ES_HOST")
10-
PORT = os.getenv("ES_PORT")
9+
10+
def _es_config() -> Dict[str, Any]:
11+
config = {
12+
"hosts": [{"host": os.getenv("ES_HOST"), "port": os.getenv("ES_PORT")}],
13+
"headers": {"accept": "application/vnd.elasticsearch+json; compatible-with=7"},
14+
"use_ssl": True,
15+
"verify_certs": True,
16+
}
17+
18+
if (u := os.getenv("ES_USER")) and (p := os.getenv("ES_PASS")):
19+
config["http_auth"] = (u, p)
20+
21+
if (v := os.getenv("ES_USE_SSL")) and v == "false":
22+
config["use_ssl"] = False
23+
24+
if (v := os.getenv("ES_VERIFY_CERTS")) and v == "false":
25+
config["verify_certs"] = False
26+
27+
if v := os.getenv("CURL_CA_BUNDLE"):
28+
config["ca_certs"] = v
29+
30+
return config
31+
32+
33+
_forbidden_fields: Set[str] = {"type"}
1134

1235

1336
class ElasticsearchSettings(ApiSettings):
1437
"""API settings."""
1538

1639
# Fields which are defined by STAC but not included in the database model
17-
forbidden_fields: Set[str] = {"type"}
40+
forbidden_fields: Set[str] = _forbidden_fields
1841

1942
@property
2043
def create_client(self):
2144
"""Create es client."""
22-
return Elasticsearch(
23-
[{"host": str(DOMAIN), "port": str(PORT)}],
24-
headers={"accept": "application/vnd.elasticsearch+json; compatible-with=7"},
25-
)
45+
return Elasticsearch(**_es_config())
2646

2747

2848
class AsyncElasticsearchSettings(ApiSettings):
2949
"""API settings."""
3050

3151
# Fields which are defined by STAC but not included in the database model
32-
forbidden_fields: Set[str] = {"type"}
52+
forbidden_fields: Set[str] = _forbidden_fields
3353

3454
@property
3555
def create_client(self):
3656
"""Create async elasticsearch client."""
37-
return AsyncElasticsearch(
38-
[{"host": str(DOMAIN), "port": str(PORT)}],
39-
headers={"accept": "application/vnd.elasticsearch+json; compatible-with=7"},
40-
)
57+
return AsyncElasticsearch(**_es_config())

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/indexes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class IndexesClient:
1616
"""Elasticsearch client to handle index creation."""
1717

1818
session: Session = attr.ib(default=attr.Factory(Session.create_from_env))
19-
client = AsyncElasticsearchSettings().create_client
2019

2120
ES_MAPPINGS_DYNAMIC_TEMPLATES = [
2221
# Common https://github.com/radiantearth/stac-spec/blob/master/item-spec/common-metadata.md
@@ -103,12 +102,13 @@ class IndexesClient:
103102

104103
async def create_indexes(self):
105104
"""Create the index for Items and Collections."""
106-
await self.client.indices.create(
105+
client = AsyncElasticsearchSettings().create_client
106+
await client.indices.create(
107107
index=ITEMS_INDEX,
108108
mappings=self.ES_ITEMS_MAPPINGS,
109109
ignore=400, # ignore 400 already exists code
110110
)
111-
await self.client.indices.create(
111+
await client.indices.create(
112112
index=COLLECTIONS_INDEX,
113113
mappings=self.ES_COLLECTIONS_MAPPINGS,
114114
ignore=400, # ignore 400 already exists code

0 commit comments

Comments
 (0)