In-process token buckets, no Redis and no slowapi - for the same reason the job queue has no Redis (SPEC C8). This is one uvicorn process serving a handful of cameras on a LAN, and slowapi's default storage is in-process anyway, so a dependency would buy convenience rather than capability. That assumption is named in the module: poorbird serve runs a single process, and if the deployment ever gains workers each gets its own buckets and the effective limit multiplies. Move the state to PostgreSQL at that point rather than widening the numbers. Buckets rather than fixed windows, because a camera uploads three to five frames back to back when the radar fires and then goes quiet. A window would reject the tail of a legitimate burst that straddled a boundary. Authenticated endpoints key on the device, so one wedged camera cannot spend another's allowance. /enroll keys on the caller address, since there is no device yet and it is the one place a shared secret could be ground at. This required a Caddyfile change, not a cosmetic one: the :8080 camera block proxied without X-Real-IP, so every camera reached the app as 127.0.0.1 and they would all have shared one enrollment bucket - one noisy device locking the rest out. The header is trusted only because uvicorn binds loopback. Limits are deliberately loose, each derived from what the firmware actually does and noted in the code so the number can be re-derived rather than guessed at. A throttled camera stops delivering photographs, which on a private LAN is worse than the flooding the limit prevents. Throttling answers 429, never 401: the firmware clears its token on a 401, and being busy is not a credential problem. Retry-After gives it something better than a guess. The key table is LRU-bounded. It is keyed partly on caller address, and an unbounded map keyed by something a caller influences is itself a way to exhaust memory. Off by default in tests, which fire far more requests than any camera would. tests/test_ratelimit.py turns it back on and proves the buckets are wired to the routes. Also fixes the same teardown-in-test-body flaw in the new file, found by re-running a deliberate mutation and noticing a leaked device row. Teardown is in fixtures; the mutation now fails its one test and leaves nothing behind. Tests: 158 -> 175. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
54 lines
2.5 KiB
Bash
54 lines
2.5 KiB
Bash
# poorBird configuration.
|
|
#
|
|
# Install to /etc/poorbird/poorbird.env, owned by root:poorbird, mode 0640.
|
|
# systemd loads it via EnvironmentFile. This same file becomes a compose .env
|
|
# unchanged if containers ever happen (TODO.md Phase 1.2).
|
|
#
|
|
# Note: systemd's EnvironmentFile does NOT do shell quoting or expansion.
|
|
# Write VALUES literally, with no surrounding quotes.
|
|
|
|
# --- database ---------------------------------------------------------------
|
|
# Loopback TCP with scram-sha-256, deliberately not a Unix socket.
|
|
# Required: the app has no built-in default and refuses to start without it.
|
|
# The literal CHANGE_ME below is rejected at startup, so you cannot copy this
|
|
# file and forget to edit it.
|
|
POORBIRD_DATABASE_URL=postgresql+psycopg://poorbird:CHANGE_ME@127.0.0.1:5432/poorbird
|
|
|
|
# --- http -------------------------------------------------------------------
|
|
# Bind to loopback only; Caddy is the only thing that talks to it.
|
|
# Caddy itself takes :8080 for the camera and :443 for humans, so uvicorn sits
|
|
# behind on 8090. See deploy/Caddyfile.
|
|
POORBIRD_API_HOST=127.0.0.1
|
|
POORBIRD_API_PORT=8090
|
|
|
|
# --- storage ----------------------------------------------------------------
|
|
# Read by the app AND referenced by the Caddyfile: the auth-checked image
|
|
# handoff needs both to agree on this path (SPEC 3.3).
|
|
POORBIRD_IMAGE_ROOT=/srv/poorbird/images
|
|
POORBIRD_THUMB_ROOT=/srv/poorbird/thumbs
|
|
POORBIRD_STATE_DIR=/var/lib/poorbird
|
|
|
|
# --- devices ----------------------------------------------------------------
|
|
# Shared bootstrap secret the firmware presents in X-Enroll-Secret.
|
|
# Generate with: openssl rand -hex 32
|
|
#
|
|
# WARNING (firmware/API.md): this value is compiled into the firmware image as
|
|
# the last-resort recovery path for a camera that cannot be reached over USB.
|
|
# Rotating it here WITHOUT shipping new firmware removes that escape hatch.
|
|
POORBIRD_ENROLL_SECRET=
|
|
|
|
# --- rate limiting ------------------------------------------------------------
|
|
# In-process token buckets. Sized for ONE uvicorn process: if you ever run the
|
|
# API with workers, each gets its own buckets and the effective limit
|
|
# multiplies. Set false only for local development.
|
|
POORBIRD_RATE_LIMIT_ENABLED=true
|
|
|
|
# --- worker -----------------------------------------------------------------
|
|
POORBIRD_WORKER_POLL_INTERVAL_S=2
|
|
POORBIRD_JOB_MAX_ATTEMPTS=3
|
|
|
|
# --- logging ----------------------------------------------------------------
|
|
POORBIRD_LOG_LEVEL=INFO
|
|
# text for journalctl by eye, json once you are shipping logs somewhere.
|
|
POORBIRD_LOG_FORMAT=text
|