Skip to content

Commit 73f26bf

Browse files
committed
feat: normalize database password environment variable and enhance config security
1 parent cbd7449 commit 73f26bf

1 file changed

Lines changed: 9 additions & 10 deletions

File tree

core/pygeoapi.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -288,20 +288,18 @@ def _pygeoapi_db_settings() -> tuple[str, str, str, str, str]:
288288
"PYGEOAPI_POSTGRES_USER or POSTGRES_USER must be set and non-empty "
289289
"to generate the pygeoapi configuration."
290290
)
291-
if (
292-
os.environ.get("PYGEOAPI_POSTGRES_PASSWORD") is None
293-
and os.environ.get("POSTGRES_PASSWORD") is None
294-
):
291+
password_value = os.environ.get("PYGEOAPI_POSTGRES_PASSWORD")
292+
if password_value is None:
293+
password_value = os.environ.get("POSTGRES_PASSWORD")
294+
if password_value is None:
295295
raise RuntimeError(
296296
"PYGEOAPI_POSTGRES_PASSWORD or POSTGRES_PASSWORD must be set to "
297297
"generate the pygeoapi configuration."
298298
)
299-
password_env_var = (
300-
"PYGEOAPI_POSTGRES_PASSWORD"
301-
if os.environ.get("PYGEOAPI_POSTGRES_PASSWORD") is not None
302-
else "POSTGRES_PASSWORD"
303-
)
304-
return host, port, dbname, user, f"${{{password_env_var}}}"
299+
# Normalize to a dedicated runtime env var used by generated pygeoapi config.
300+
runtime_password_env_var = "PYGEOAPI_DB_AUTH_TOKEN"
301+
os.environ[runtime_password_env_var] = password_value
302+
return host, port, dbname, user, f"${{{runtime_password_env_var}}}"
305303

306304

307305
def _write_config(path: Path) -> None:
@@ -332,6 +330,7 @@ def _write_config(path: Path) -> None:
332330
# * Do not expose it in logs, error messages, or diagnostics.
333331
# * Ensure filesystem permissions restrict access appropriately.
334332
path.write_text(config, encoding="utf-8")
333+
path.chmod(0o600)
335334

336335

337336
def _generate_openapi(config_path: Path, openapi_path: Path) -> None:

0 commit comments

Comments
 (0)