Skip to content

Open-source personal finance manager. Self-hosted, privacy-first.

feat: support reading config secrets from file-based sources (#392)

Pedro Magalhães793385d70660
diff --git a/backend/app/core/config.py b/backend/app/core/config.py
index e7b9d866..21b0dd27 100644
--- a/backend/app/core/config.py
+++ b/backend/app/core/config.py
@@ -1,8 +1,16 @@
from functools import lru_cache
+from os import getenv
+from pathlib import Path
from urllib.parse import urlparse
from pydantic_settings import BaseSettings, SettingsConfigDict
+# Use the same environment variable that systemd uses: https://systemd.io/CREDENTIALS/
+# If not defined, defaults to docker secrets defaults (https://docs.docker.com/compose/how-tos/use-secrets/)
+CREDENTIALS_DIRECTORY: list[Path] = [
+ Path(p) for p in getenv("CREDENTIALS_DIRECTORY", "/run/secrets").split(":") if p
+]
+
class Settings(BaseSettings):
# App
@@ -85,7 +93,9 @@ class Settings(BaseSettings):
# OIDC login (works with Authentik, Pocket ID, and other standard OIDC providers)
oidc_enabled: bool = False
oidc_provider_name: str = "OIDC"
- oidc_discovery_url: str = "" # e.g. https://auth.example.com/application/o/securo/.well-known/openid-configuration
+ oidc_discovery_url: str = (
+ "" # e.g. https://auth.example.com/application/o/securo/.well-known/openid-configuration
+ )
oidc_client_id: str = ""
oidc_client_secret: str = ""
oidc_redirect_uri: str = "" # defaults to {FRONTEND_URL}/api/auth/oidc/callback
@@ -115,7 +125,7 @@ class Settings(BaseSettings):
# external dependency on the Brazilian government endpoint).
tesouro_direto_enabled: bool = True
- model_config = SettingsConfigDict(env_file=".env")
+ model_config = SettingsConfigDict(env_file=".env", secrets_dir=CREDENTIALS_DIRECTORY)
@lru_cache
diff --git a/backend/pyproject.toml b/backend/pyproject.toml
index b111aed9..365a8317 100644
--- a/backend/pyproject.toml
+++ b/backend/pyproject.toml
@@ -10,7 +10,7 @@ dependencies = [
"alembic>=1.13.0",
"asyncpg>=0.29.0",
"pydantic>=2.5.0",
- "pydantic-settings>=2.1.0",
+ "pydantic-settings>=2.5.0",
"python-jose[cryptography]>=3.3.0",
"cryptography==46.0.7",
"passlib[bcrypt]>=1.7.4",
diff --git a/backend/tests/test_config.py b/backend/tests/test_config.py
new file mode 100644
index 00000000..33bcbc51
--- /dev/null
+++ b/backend/tests/test_config.py
@@ -0,0 +1,70 @@
+from pathlib import Path
+import pytest
+
+from app.core.config import Settings
+
+
+def write(dir: Path, name: str, value: str) -> None:
+ (dir / name).write_text(value)
+
+
+def secrets(tmp_path: Path):
+ d = tmp_path / "secrets"
+ d.mkdir()
+ return d
+
+
+def test_oidc_secret_reads_from_secrets_dir(secrets: Path):
+ write(secrets, "oidc_client_secret", "file-secret")
+
+ settings = Settings(_secrets_dir=str(secrets))
+ assert settings.oidc_client_secret == "file-secret"
+
+
+def test_oidc_secret_strips_whitespace(secrets: Path):
+ write(secrets, "oidc_client_secret", " stripped-value \n\n")
+
+ settings = Settings(_secrets_dir=str(secrets))
+ assert settings.oidc_client_secret == "stripped-value"
+
+
+def test_oidc_secret_inline_when_no_file(secrets: Path):
+ settings = Settings(
+ oidc_client_secret="inline-secret",
+ _secrets_dir=str(secrets),
+ )
+
+ assert settings.oidc_client_secret == "inline-secret"
+
+
+def test_oidc_secret_default_when_no_file(secrets: Path):
+ settings = Settings(_secrets_dir=str(secrets))
+
+ assert settings.oidc_client_secret == ""
+
+
+def test_multiple_secrets_dirs_merge(tmp_path: Path):
+ d1, d2 = tmp_path / "d1", tmp_path / "d2"
+ d1.mkdir()
+ d2.mkdir()
+
+ secrets = {
+ d1: {
+ "oidc_client_secret": "from-d1",
+ "oidc_provider_name": "Provider D1",
+ },
+ d2: {
+ "oidc_client_secret": "from-d2",
+ "oidc_client_id": "from-d2",
+ },
+ }
+ for directory, values in secrets.items():
+ for name, value in values.items():
+ write(directory, name, value)
+
+ expectedSecrets = {**secrets[d1], **secrets[d2]}
+ settings = Settings(_secrets_dir=[str(d1), str(d2)])
+
+ for key, expectedValue in expectedSecrets.items():
+ assert getattr(settings, key) == expectedValue