Harden config: no credentials in code (REVIEW SEC-001, SEC-006)
SEC-006: database_url had a working default carrying a real username and password. Removed. Absence is now reported by 'poorbird check' as a problem (exit 1) and raised by db.py with a message that names the variable and the example file, instead of SQLAlchemy's "Could not parse URL from string ''" three frames down. SEC-001: enroll_secret stays environment-only and keeps its empty default so the Phase 1 commands still run on a fresh clone. The device endpoints will refuse to serve without it when they land. Both fields now reject unedited placeholders (CHANGE_ME and friends), so a copied .example cannot boot as though it were configured. Tests: 12 -> 17. New cases cover the absent default, the readable failure, and placeholder rejection for both fields. Note: 6 files unrelated to this change are not clean under 'ruff format' (the repo is wrapped at 88, pyproject sets line-length 100). Left alone to keep this diff reviewable. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
2188bed633
commit
3344a9e9b1
5 files changed
+125
-17
No files matched your search
@@ -12,9 +12,13 @@ from functools import lru_cache
|
||||
from pathlib import Path
|
||||
from typing import Literal
|
||||
|
||||
from pydantic import SecretStr
|
||||
from pydantic import SecretStr, field_validator
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
# Values that mean "nobody filled this in". The .example files ship with these,
|
||||
# and a copied-but-unedited example must not boot as if it were configured.
|
||||
_PLACEHOLDERS = frozenset({"change_me", "changeme", "todo", "xxx", "secret"})
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
model_config = SettingsConfigDict(
|
||||
@@ -27,7 +31,12 @@ class Settings(BaseSettings):
|
||||
# --- database ---------------------------------------------------------
|
||||
# Loopback TCP with scram-sha-256, deliberately not a Unix socket: a socket
|
||||
# is the one genuinely container-hostile choice (TODO.md Phase 1.2).
|
||||
database_url: str = "postgresql+psycopg://poorbird:poorbird@127.0.0.1:5432/poorbird"
|
||||
#
|
||||
# No default. A working URL in code means real credentials in code, and the
|
||||
# module docstring above already promises none (REVIEW SEC-006). Absence is
|
||||
# reported by ``poorbird check`` and raised with a readable message by db.py,
|
||||
# rather than silently connecting to whatever happens to be on localhost.
|
||||
database_url: str = ""
|
||||
db_pool_size: int = 5
|
||||
db_echo: bool = False
|
||||
|
||||
@@ -50,6 +59,10 @@ class Settings(BaseSettings):
|
||||
# Shared bootstrap secret presented by a device in X-Enroll-Secret.
|
||||
# See firmware/API.md: do NOT rotate this without shipping new firmware,
|
||||
# or you remove the last-resort recovery path for a mounted camera.
|
||||
#
|
||||
# Environment only, never a literal here (REVIEW SEC-001). Empty is allowed
|
||||
# so the Phase 1 commands still run on a fresh clone; the device endpoints
|
||||
# refuse to serve without it.
|
||||
enroll_secret: SecretStr = SecretStr("")
|
||||
|
||||
# --- worker -----------------------------------------------------------
|
||||
@@ -63,6 +76,31 @@ class Settings(BaseSettings):
|
||||
log_level: str = "INFO"
|
||||
log_format: Literal["text", "json"] = "text"
|
||||
|
||||
@field_validator("database_url")
|
||||
@classmethod
|
||||
def _reject_unedited_example(cls, v: str) -> str:
|
||||
if any(p in v.lower() for p in _PLACEHOLDERS):
|
||||
raise ValueError(
|
||||
"POORBIRD_DATABASE_URL still contains a placeholder from the "
|
||||
"example file. Put the real password in, or leave the variable "
|
||||
"unset."
|
||||
)
|
||||
return v
|
||||
|
||||
@field_validator("enroll_secret")
|
||||
@classmethod
|
||||
def _reject_placeholder_secret(cls, v: SecretStr) -> SecretStr:
|
||||
if v.get_secret_value().strip().lower() in _PLACEHOLDERS:
|
||||
raise ValueError(
|
||||
"POORBIRD_ENROLL_SECRET is a placeholder. Generate one with "
|
||||
"'openssl rand -hex 32', or leave it unset until Phase 2."
|
||||
)
|
||||
return v
|
||||
|
||||
@property
|
||||
def database_url_is_set(self) -> bool:
|
||||
return bool(self.database_url.strip())
|
||||
|
||||
@property
|
||||
def enroll_secret_is_set(self) -> bool:
|
||||
return bool(self.enroll_secret.get_secret_value())
|
||||
|
||||
Reference in new issue
Block a user