Rate-limit report ingestion per host #12

Merged
patrick merged 2 commits from feat/ingest-rate-limit into main 2026-10-30 01:16:32 +00:00
Owner

Summary

/api/v1/report had no rate limit. A stuck agent or a leaked token could write
reports as fast as it could connect. Retention is by age only, so the table could
grow without bound within the 30 days, and the host page (last 50 reports) would
be filled with duplicates.

Reports arriving sooner than report_min_interval_seconds (default 30, 0
disables) after the same host's previous report are now refused with
429 {"error": "reporting too often"} and a Retry-After header, and are not
stored.

  • The check and insert share one BEGIN IMMEDIATE transaction in add_report,
    so concurrent requests can't both pass.
  • Per host, and only after token auth. Invalid payloads (400) don't count.
  • Worst case with a misbehaving token: ~2,880 rows/day/host instead of unbounded.
  • add_report now returns the seconds to wait (0 on success); the new parameter
    defaults to 0, so existing callers are unchanged.

Agent impact

None, no redeploy. curl treats 429 as transient and honors Retry-After, so a
manual systemctl start kernelwatch-agent twice in a row pauses up to 30s and
then succeeds (checked against a stub with curl 8.23; the journal gets a one-line
"returned error: 429"). Older curl (Debian 12, Ubuntu 22.04) uses the fixed 10s
retry delay x3, about 30s, so a quick double run there may fail once.

Testing

make test: 112 tests pass (4 new): refusal and Retry-After, per-host
independence, resuming after the interval, bad payloads not counting, 0
disabling, and config validation.

🤖 Generated with Claude Code

## Summary `/api/v1/report` had no rate limit. A stuck agent or a leaked token could write reports as fast as it could connect. Retention is by age only, so the table could grow without bound within the 30 days, and the host page (last 50 reports) would be filled with duplicates. Reports arriving sooner than `report_min_interval_seconds` (default 30, `0` disables) after the same host's previous report are now refused with `429 {"error": "reporting too often"}` and a `Retry-After` header, and are not stored. - The check and insert share one `BEGIN IMMEDIATE` transaction in `add_report`, so concurrent requests can't both pass. - Per host, and only after token auth. Invalid payloads (400) don't count. - Worst case with a misbehaving token: ~2,880 rows/day/host instead of unbounded. - `add_report` now returns the seconds to wait (0 on success); the new parameter defaults to 0, so existing callers are unchanged. ## Agent impact None, no redeploy. curl treats 429 as transient and honors `Retry-After`, so a manual `systemctl start kernelwatch-agent` twice in a row pauses up to 30s and then succeeds (checked against a stub with curl 8.23; the journal gets a one-line "returned error: 429"). Older curl (Debian 12, Ubuntu 22.04) uses the fixed 10s retry delay x3, about 30s, so a quick double run there may fail once. ## Testing `make test`: 112 tests pass (4 new): refusal and `Retry-After`, per-host independence, resuming after the interval, bad payloads not counting, `0` disabling, and config validation. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
patrick added 2 commits 2026-10-30 01:10:10 +00:00
Reports arriving sooner than report_min_interval_seconds (default 30) after
the host's previous one are refused with 429 and a Retry-After header. The
check shares the insert's transaction so concurrent requests can't bypass it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Document report rate limit
CI / tests (Python 3.10) (pull_request) Successful in 21s
CI / tests (Python 3.11) (pull_request) Successful in 19s
CI / tests (Python 3.12) (pull_request) Successful in 19s
CI / tests (Python 3.13) (pull_request) Successful in 19s
CI / lint (pull_request) Successful in 3s
51732066d0
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
patrick merged commit 313961d70b into main 2026-10-30 01:16:32 +00:00
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: patrick/kernel-automation#12