Source code for s365b.core.config

"""Process configuration read from environment variables.

Only settings needed to *start* the process live here (database, paths, keys,
module allow-list). Everything an admin changes at runtime (schedules,
notification recipients, module on/off) is stored in the database.

All variables use the ``S365B_`` prefix, e.g. ``S365B_DATABASE_URL``.
Values ending in ``_FILE`` point to files, which is how Docker secrets are
passed in.
"""

from __future__ import annotations

from functools import lru_cache
from pathlib import Path
from typing import Annotated, Literal

from pydantic import Field, SecretStr, field_validator
from pydantic_settings import BaseSettings, NoDecode, SettingsConfigDict


[docs] class Settings(BaseSettings): """Settings for every Mail Safehouse process. Attributes: database_url: PostgreSQL URL in SQLAlchemy form, e.g. ``postgresql+psycopg://s365b:pw@postgres:5432/s365b``. master_key_file: File holding the master key that encrypts secrets stored in the database (certificate private keys, SMTP passwords). Generate with ``s365b generate-master-key``. repository_path: Default location of the filesystem backup repository. modules: Allow-list of module IDs this instance may load. Empty means every installed module is allowed. Modules outside the list are never imported, whatever the UI says. base_url: Public URL of the web UI (``https://host[:port]``), used for links in emails, the Entra sign-in callback and secure cookies. timezone: IANA time zone that backup schedules are written in. web_dir: Built web UI (``frontend/dist``) to serve at ``/``. Unset or missing means API only. log_level: Minimum log level. log_format: ``json`` for containers, ``console`` for development. """
[docs] model_config = SettingsConfigDict(env_prefix="S365B_", env_file=".env", extra="ignore")
[docs] database_url: SecretStr = SecretStr("postgresql+psycopg://s365b:s365b@localhost:5432/s365b")
[docs] master_key_file: Path = Path("/run/secrets/master_key")
[docs] repository_path: Path = Path("/repo")
[docs] modules: Annotated[list[str], NoDecode] = Field(default_factory=list)
[docs] base_url: str = "https://localhost"
[docs] timezone: str = "UTC"
[docs] web_dir: Path | None = None
[docs] log_level: Literal["DEBUG", "INFO", "WARNING", "ERROR"] = "INFO"
[docs] log_format: Literal["json", "console"] = "json"
@field_validator("modules", mode="before") @classmethod def _split_modules(cls, value: object) -> object: """Accept a comma-separated string such as ``entra,exchange``.""" if isinstance(value, str): return [m.strip() for m in value.split(",") if m.strip()] return value
[docs] def libpq_url(self) -> str: """Return the database URL in plain libpq form (for psycopg/procrastinate). Returns: The URL without SQLAlchemy's ``+driver`` suffix. """ url = self.database_url.get_secret_value() scheme, sep, rest = url.partition("://") return f"{scheme.split('+', 1)[0]}{sep}{rest}"
@lru_cache(maxsize=1)
[docs] def get_settings() -> Settings: """Return the process-wide settings, read once from the environment. Returns: The cached :class:`Settings` instance. """ return Settings()