Skip to content

Commit 2c767a6

Browse files
committed
fix: fetch postgres creds on each app startup
resolves #236
1 parent 64e1b1f commit 2c767a6

3 files changed

Lines changed: 44 additions & 30 deletions

File tree

lib/stac-api/runtime/src/stac_api/handler.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,22 @@
2020
logger = logging.getLogger(__name__)
2121

2222

23-
logger.info("fetching pgstac secret")
24-
secret = get_secret_dict(secret_arn_env_var="PGSTAC_SECRET_ARN")
25-
postgres_settings = PostgresSettings(
26-
pghost=secret["host"],
27-
pgdatabase=secret["dbname"],
28-
pguser=secret["username"],
29-
pgpassword=secret["password"],
30-
pgport=int(secret["port"]),
31-
)
32-
3323
_connection_initialized = False
3424

3525

26+
def _build_postgres_settings() -> PostgresSettings:
27+
"""Fetch credentials from Secrets Manager and build PostgresSettings."""
28+
logger.info("fetching pgstac secret")
29+
secret = get_secret_dict(secret_arn_env_var="PGSTAC_SECRET_ARN")
30+
return PostgresSettings(
31+
pghost=secret["host"],
32+
pgdatabase=secret["dbname"],
33+
pguser=secret["username"],
34+
pgpassword=secret["password"],
35+
pgport=int(secret["port"]),
36+
)
37+
38+
3639
@register_before_snapshot
3740
def on_snapshot():
3841
"""
@@ -90,6 +93,7 @@ def on_snap_restore():
9093
app.state.writepool = None
9194

9295
# Create fresh connection pool
96+
postgres_settings = _build_postgres_settings()
9397
loop.run_until_complete(
9498
connect_to_db(
9599
app,
@@ -111,6 +115,7 @@ def on_snap_restore():
111115
async def startup_event():
112116
"""Connect to database on startup."""
113117
logger.info("Setting up DB connection...")
118+
postgres_settings = _build_postgres_settings()
114119
await connect_to_db(
115120
app,
116121
postgres_settings=postgres_settings,
@@ -145,7 +150,7 @@ async def shutdown_event():
145150
loop.run_until_complete(
146151
connect_to_db(
147152
app,
148-
postgres_settings=postgres_settings,
153+
postgres_settings=_build_postgres_settings(),
149154
add_write_connection_pool=with_transactions,
150155
)
151156
)

lib/tipg-api/runtime/src/tipg_api/handler.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,24 @@
1717
)
1818
from utils import get_secret_dict
1919

20-
secret = get_secret_dict(secret_arn_env_var="PGSTAC_SECRET_ARN")
21-
postgres_settings = PostgresSettings(
22-
postgres_host=secret["host"],
23-
postgres_dbname=secret["dbname"],
24-
postgres_user=secret["username"],
25-
postgres_pass=secret["password"],
26-
postgres_port=int(secret["port"]),
27-
)
2820
db_settings = DatabaseSettings()
2921
custom_sql_settings = CustomSQLSettings()
3022

3123
_connection_initialized = False
3224

3325

26+
def _build_postgres_settings() -> PostgresSettings:
27+
"""Fetch credentials from Secrets Manager and build PostgresSettings."""
28+
secret = get_secret_dict(secret_arn_env_var="PGSTAC_SECRET_ARN")
29+
return PostgresSettings(
30+
postgres_host=secret["host"],
31+
postgres_dbname=secret["dbname"],
32+
postgres_user=secret["username"],
33+
postgres_pass=secret["password"],
34+
postgres_port=int(secret["port"]),
35+
)
36+
37+
3438
@register_before_snapshot
3539
def on_snapshot():
3640
"""
@@ -74,6 +78,7 @@ def on_snap_restore():
7478
app.state.pool = None
7579

7680
# Create fresh connection pool
81+
postgres_settings = _build_postgres_settings()
7782
loop.run_until_complete(
7883
connect_to_db(
7984
app,
@@ -103,6 +108,7 @@ def on_snap_restore():
103108
@app.on_event("startup")
104109
async def startup_event() -> None:
105110
"""Connect to database on startup."""
111+
postgres_settings = _build_postgres_settings()
106112
await connect_to_db(
107113
app,
108114
schemas=db_settings.schemas,

lib/titiler-pgstac-api/runtime/src/titiler_pgstac_api/handler.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,21 @@
1212
from titiler.pgstac.settings import PostgresSettings
1313
from utils import get_secret_dict
1414

15-
secret = get_secret_dict(secret_arn_env_var="PGSTAC_SECRET_ARN")
16-
postgres_settings = PostgresSettings(
17-
pghost=secret["host"],
18-
pgdatabase=secret["dbname"],
19-
pguser=secret["username"],
20-
pgpassword=secret["password"],
21-
pgport=int(secret["port"]),
22-
)
23-
2415
_connection_initialized = False
2516

2617

18+
def _build_postgres_settings() -> PostgresSettings:
19+
"""Fetch credentials from Secrets Manager and build PostgresSettings."""
20+
secret = get_secret_dict(secret_arn_env_var="PGSTAC_SECRET_ARN")
21+
return PostgresSettings(
22+
pghost=secret["host"],
23+
pgdatabase=secret["dbname"],
24+
pguser=secret["username"],
25+
pgpassword=secret["password"],
26+
pgport=int(secret["port"]),
27+
)
28+
29+
2730
@register_before_snapshot
2831
def on_snapshot():
2932
"""
@@ -67,7 +70,7 @@ def on_snap_restore():
6770
app.state.dbpool = None
6871

6972
# Create fresh connection pool
70-
loop.run_until_complete(connect_to_db(app, settings=postgres_settings))
73+
loop.run_until_complete(connect_to_db(app, settings=_build_postgres_settings()))
7174

7275
_connection_initialized = True
7376

@@ -81,7 +84,7 @@ def on_snap_restore():
8184
@app.on_event("startup")
8285
async def startup_event() -> None:
8386
"""Connect to database on startup."""
84-
await connect_to_db(app, settings=postgres_settings)
87+
await connect_to_db(app, settings=_build_postgres_settings())
8588

8689

8790
handler = Mangum(app, lifespan="off")

0 commit comments

Comments
 (0)