Files
archie/internal/infrastructure/configuration/defaults_test.go
T
sam 101c133239 feat(capture): store unbound webhook events, retention-bounded
Adds POST /webhooks/capture/{source}: an unauthenticated endpoint (mounted
alongside /healthz, bypassing dashboard token auth by design) that persists
any inbound webhook archie has no binding for yet, so it can be inspected
and mapped later (t2db.2/t2db.3).

docs/prds/event-capture-storage.md decides SQLite over NATS JetStream for
storage: NATS is not unconditionally available until 7d5u.2 ships embedded
support, and capture's single-writer/single-reader access pattern doesn't
need JetStream's fan-out/replay machinery anyway.

internal/store/captures.go adds a captured_events table with prune-on-write
(retention age + row-count cap, both configurable). The HTTP handler applies
webhookguard redaction before persisting, and enforces two more independent
disk-bound mechanics before that: a per-request body-size cap and a
per-remote-address rate limit. New [capture] config section with documented
defaults; all fields optional, capture is on by default.

Adversarial review (manual, after the usual fresh-subagent pass hit a
session API limit mid-run) caught and fixed three real defects before this
landed, each with a regression test confirmed to fail against the pre-fix
code:
- the body-size cap silently no-op'd when CaptureMaxBodyBytes was left
  unset, the one bound that limits a single payload's size rather than row
  count or write rate
- the rate limiter was keyed by the request's own source path segment --
  unregistered and attacker-chosen by design for this endpoint, violating
  webhookguard.RateLimiter's documented bounded-key-space assumption and
  letting a sender bypass the limit by rotating the segment; rekeyed to
  remote address
- the retention prune compared time.RFC3339Nano strings, whose trimmed
  variable-width fractional seconds break lexicographic ordering within the
  same second; switched to fixed-width RFC3339

Closes archie-core-t2db.1.
2026-08-22 04:51:25 +10:00

59 lines
1.9 KiB
Go

package configuration
import (
"testing"
"time"
"github.com/samcharles93/archie-core/internal/config"
)
func TestApplyCaptureDefaultsFillsZeroValues(t *testing.T) {
cfg := &config.Config{}
applyCaptureDefaults(cfg)
if got, want := cfg.Capture.Retention.Std(), defaultCaptureRetention; got != want {
t.Errorf("Capture.Retention = %v, want %v", got, want)
}
if cfg.Capture.MaxEvents != defaultCaptureMaxEvents {
t.Errorf("Capture.MaxEvents = %d, want %d", cfg.Capture.MaxEvents, defaultCaptureMaxEvents)
}
if cfg.Capture.MaxBodyBytes != defaultCaptureMaxBodyBytes {
t.Errorf("Capture.MaxBodyBytes = %d, want %d", cfg.Capture.MaxBodyBytes, defaultCaptureMaxBodyBytes)
}
if cfg.Capture.RatePerSecond != defaultCaptureRatePerSecond {
t.Errorf("Capture.RatePerSecond = %v, want %v", cfg.Capture.RatePerSecond, defaultCaptureRatePerSecond)
}
if cfg.Capture.RateBurst != defaultCaptureRateBurst {
t.Errorf("Capture.RateBurst = %d, want %d", cfg.Capture.RateBurst, defaultCaptureRateBurst)
}
}
func TestApplyCaptureDefaultsLeavesExplicitValuesAlone(t *testing.T) {
cfg := &config.Config{
Capture: config.CaptureConfig{
Retention: config.Duration(24 * time.Hour),
MaxEvents: 42,
MaxBodyBytes: 1024,
RatePerSecond: 2.5,
RateBurst: 9,
},
}
applyCaptureDefaults(cfg)
if cfg.Capture.Retention.Std() != 24*time.Hour {
t.Errorf("Capture.Retention = %v, want unchanged 24h", cfg.Capture.Retention.Std())
}
if cfg.Capture.MaxEvents != 42 {
t.Errorf("Capture.MaxEvents = %d, want unchanged 42", cfg.Capture.MaxEvents)
}
if cfg.Capture.MaxBodyBytes != 1024 {
t.Errorf("Capture.MaxBodyBytes = %d, want unchanged 1024", cfg.Capture.MaxBodyBytes)
}
if cfg.Capture.RatePerSecond != 2.5 {
t.Errorf("Capture.RatePerSecond = %v, want unchanged 2.5", cfg.Capture.RatePerSecond)
}
if cfg.Capture.RateBurst != 9 {
t.Errorf("Capture.RateBurst = %d, want unchanged 9", cfg.Capture.RateBurst)
}
}