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.
This commit is contained in:
1 parent
3d74f0136e
commit
101c133239
17 files changed
+1066
-3
No files matched your search
@@ -39,6 +39,15 @@ const (
|
||||
// above trims it further. Generous enough for a documentation page,
|
||||
// small enough that a large download cannot occupy the daemon.
|
||||
defaultWebFetchMaxBytes = 2_000_000
|
||||
|
||||
// Capture defaults, per docs/prds/event-capture-storage.md's disk-bound
|
||||
// mechanics: retention + row cap + per-request size cap + per-source
|
||||
// rate limit, composed.
|
||||
defaultCaptureRetention = 7 * 24 * time.Hour
|
||||
defaultCaptureMaxEvents = 5000
|
||||
defaultCaptureMaxBodyBytes = 256 * 1024
|
||||
defaultCaptureRatePerSecond = 1.0
|
||||
defaultCaptureRateBurst = 5
|
||||
)
|
||||
|
||||
// applyDefaults fills in every absent value. It never reports an error and
|
||||
@@ -58,6 +67,27 @@ func (l *Loader) applyDefaults(cfg *config.Config) {
|
||||
applyIdentityDefaults(cfg)
|
||||
applyContainerDefaults(cfg)
|
||||
applyToolPolicyDefaults(cfg)
|
||||
applyCaptureDefaults(cfg)
|
||||
}
|
||||
|
||||
// applyCaptureDefaults fills the webhook capture endpoint's settings. See
|
||||
// [config.CaptureConfig].
|
||||
func applyCaptureDefaults(cfg *config.Config) {
|
||||
if cfg.Capture.Retention == 0 {
|
||||
cfg.Capture.Retention = config.Duration(defaultCaptureRetention)
|
||||
}
|
||||
if cfg.Capture.MaxEvents == 0 {
|
||||
cfg.Capture.MaxEvents = defaultCaptureMaxEvents
|
||||
}
|
||||
if cfg.Capture.MaxBodyBytes == 0 {
|
||||
cfg.Capture.MaxBodyBytes = defaultCaptureMaxBodyBytes
|
||||
}
|
||||
if cfg.Capture.RatePerSecond == 0 {
|
||||
cfg.Capture.RatePerSecond = defaultCaptureRatePerSecond
|
||||
}
|
||||
if cfg.Capture.RateBurst == 0 {
|
||||
cfg.Capture.RateBurst = defaultCaptureRateBurst
|
||||
}
|
||||
}
|
||||
|
||||
// applyToolPolicyDefaults fills the tool result limits.
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -75,7 +75,35 @@ func validate(cfg *config.Config) error {
|
||||
if err := validatePollInterval(cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
return validateContainers(cfg)
|
||||
if err := validateContainers(cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
return validateCapture(cfg)
|
||||
}
|
||||
|
||||
// validateCapture rejects negative capture settings. applyDefaults fills
|
||||
// every zero value with a positive default, so this only fires on an
|
||||
// explicit negative value -- which would make time.Duration/AddDate math
|
||||
// and the token-bucket rate limiter behave nonsensically (e.g. a negative
|
||||
// retention window would prune every row on every insert, including the one
|
||||
// just written).
|
||||
func validateCapture(cfg *config.Config) error {
|
||||
if cfg.Capture.Retention < 0 {
|
||||
return fmt.Errorf("%w: capture.retention must not be negative", ErrInvalidInput)
|
||||
}
|
||||
if cfg.Capture.MaxEvents < 0 {
|
||||
return fmt.Errorf("%w: capture.max_events must not be negative", ErrInvalidInput)
|
||||
}
|
||||
if cfg.Capture.MaxBodyBytes < 0 {
|
||||
return fmt.Errorf("%w: capture.max_body_bytes must not be negative", ErrInvalidInput)
|
||||
}
|
||||
if cfg.Capture.RatePerSecond < 0 {
|
||||
return fmt.Errorf("%w: capture.rate_per_second must not be negative", ErrInvalidInput)
|
||||
}
|
||||
if cfg.Capture.RateBurst < 0 {
|
||||
return fmt.Errorf("%w: capture.rate_burst must not be negative", ErrInvalidInput)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// validatePollInterval rejects a non-positive poll interval.
|
||||
|
||||
@@ -139,6 +139,36 @@ func TestValidate_RejectsTheSameProblemsAsLoaderLoad(t *testing.T) {
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "negative capture.retention",
|
||||
mutate: func(cfg *config.Config) { cfg.Capture.Retention = config.Duration(-time.Hour) },
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "negative capture.max_events",
|
||||
mutate: func(cfg *config.Config) { cfg.Capture.MaxEvents = -1 },
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "negative capture.max_body_bytes",
|
||||
mutate: func(cfg *config.Config) { cfg.Capture.MaxBodyBytes = -1 },
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "negative capture.rate_per_second",
|
||||
mutate: func(cfg *config.Config) { cfg.Capture.RatePerSecond = -1 },
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "negative capture.rate_burst",
|
||||
mutate: func(cfg *config.Config) { cfg.Capture.RateBurst = -1 },
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "zero capture fields are valid (defaults fill them)",
|
||||
mutate: func(cfg *config.Config) {},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
|
||||
Reference in new issue
Block a user