feat(webui): playbook bindings -- matcher + mapping + workflow, HMAC-gated
Implements archie-core-t2db.4: the runtime-editable binding entity that turns a captured webhook into an archie task, plus the per-source HMAC verification at handleCapture that makes t2db.5's 'unauthenticated event cannot trigger a binding' invariant structurally enforceable. Captures, mappings and bindings are now separate concerns on the same single *store.Store; the new BindingStore / BindingDispatcher / BindingTaskCreator split keeps each interface under the interfacebloat limit (8 methods). State machine: draft -> pending_approval -> armed; any edit on an armed binding drops it back to pending_approval, enforced in SQL via status='pending_approval' on UPDATE so the rule cannot be bypassed by a direct status field in the request body. Approve is the only sanctioned transition into armed (WHERE status='pending_approval' guard). Overlap: two bindings may not share a source in any status; enforced at InsertBinding and UpdateBinding (transaction-local SELECT). Approve has a separate TOCTOU guard so a race between Insert and Approve cannot // bypass the check. HMAC at handleCapture: reads X-Hub-Signature-256 (then X-Signature-256) // fallback, verifies via webhookguard.VerifyHMAC against the binding's // per-source secret, marks CapturedEvent.Authenticated=true on match. // Captures with no armed binding, missing signature, or wrong signature all persist as authenticated=false -- the dispatch loop's binding.Matcher.Matches gate (requires authenticated) is what stops them from spawning a task. Two armed bindings for one source returns 409 at capture time as a belt-and-braces TOCTOU guard. Dispatch loop: added Daemon.dispatchBindings, called from both Cycle and maintainAndDrain between poll and drain. Walks captured_events where authenticated=1 AND source IN armed bindings AND id NOT IN binding_dispatches, runs the binding's matcher, resolves the mapping, enqueues a chat-style task with binding_id / // binding_version provenance stamped in the same transaction as the // row insert. The binding_dispatches ledger (composite PK on (binding_id, capture_id)) is the at-most-once guarantee across cycles // and daemon restarts. Tasks schema migration: binding_id INTEGER NOT NULL DEFAULT 0 + binding_version INTEGER NOT NULL DEFAULT 0 (taskSchemaVersion bumped 1 -> 2). Existing tasks keep 0 / 0. Out of scope (documented follow-ups): - secret encryption at rest (plaintext today) - full dashboard HTML for the binding editor (CRUD API only this bead) - per-binding rate limits (existing per-remote-address limit applies) - binding.Owner / binding.Repo fields (dispatch falls back to the single configured repo; multi-repo deployments log and skip)
This commit is contained in:
1 parent
31283f47bf
commit
543c10ac9a
15 files changed
+2649
-40
No files matched your search
@@ -357,9 +357,34 @@ func (b *boot) setupObservability() {
|
||||
// doesn't expose it. See docs/prds/payload-field-mapping.md.
|
||||
if ms, ok := b.st.(store.MappingStore); ok {
|
||||
b.web.Mappings = ms
|
||||
b.d.Mappings = ms
|
||||
} else {
|
||||
log.Warn("mapping storage unavailable: task store does not implement MappingStore")
|
||||
}
|
||||
// Binding storage: same narrow-assertion pattern as CaptureStore/MappingStore
|
||||
// above, for the same reason. The daemon arm/dispatch logic is wired in
|
||||
// Phase E; here only the storage handle is shared so /api/bindings has
|
||||
// somewhere to read/write. The BindingStore/BindingDispatcher/
|
||||
// BindingTaskCreator split keeps each interface under the
|
||||
// interfacebloat limit; the concrete *store.Store satisfies all three.
|
||||
// See docs/prds/webhook-intake-security.md.
|
||||
if bs, ok := b.st.(store.BindingStore); ok {
|
||||
b.web.Bindings = bs
|
||||
b.d.Bindings = bs
|
||||
} else {
|
||||
log.Warn("binding storage unavailable: task store does not implement BindingStore")
|
||||
}
|
||||
if bd, ok := b.st.(store.BindingDispatcher); ok {
|
||||
b.web.BindingDispatcher = bd
|
||||
b.d.BindingDispatcher = bd
|
||||
} else {
|
||||
log.Warn("binding dispatcher unavailable: task store does not implement BindingDispatcher")
|
||||
}
|
||||
if btc, ok := b.st.(store.BindingTaskCreator); ok {
|
||||
b.d.BindingTaskCreator = btc
|
||||
} else {
|
||||
log.Warn("binding task creator unavailable: task store does not implement BindingTaskCreator")
|
||||
}
|
||||
sink := bus.Subscribe(256)
|
||||
go persistAndBroadcastEvents(sink, b.st, b.web, log)
|
||||
}
|
||||
|
||||
+221
-6
@@ -17,7 +17,9 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/container"
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
"github.com/samcharles93/archie-core/internal/domain/curator"
|
||||
"github.com/samcharles93/archie-core/internal/domain/mapping"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workintake"
|
||||
"github.com/samcharles93/archie-core/internal/eventbus"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
@@ -70,10 +72,29 @@ type Daemon struct {
|
||||
// production composition and remains useful only to fail closed in tests.
|
||||
ConnectedNATS NATSEndpoint
|
||||
Store store.TaskStore
|
||||
Forge forge.Forge
|
||||
Trees *worktree.Manager
|
||||
Bus *events.Bus
|
||||
Log *slog.Logger
|
||||
// Mappings persists payload field mappings (docs/prds/payload-field-mapping.md).
|
||||
// Used by the binding dispatch loop to resolve capture bodies against
|
||||
// the mapping a binding names. Optional: nil disables the binding
|
||||
// dispatch loop (legacy behaviour).
|
||||
Mappings store.MappingStore
|
||||
// Bindings persists playbook bindings (docs/prds/playbook-binding.md).
|
||||
// Optional: nil disables the binding dispatch loop (legacy behaviour).
|
||||
Bindings store.BindingStore
|
||||
// BindingDispatcher is the dispatch-time helper surface for bindings:
|
||||
// list undispatched captures, look up armed bindings by source for the
|
||||
// matcher, and write the at-most-once dedup ledger row. Split from
|
||||
// BindingStore to keep the CRUD interface under the interfacebloat
|
||||
// limit. Optional: nil disables the dispatch loop.
|
||||
BindingDispatcher store.BindingDispatcher
|
||||
// BindingTaskCreator enqueues a task triggered by a binding. Split
|
||||
// off TaskLifecycle so the lifecycle surface stays narrow and the
|
||||
// dispatch loop does not acquire the full task-creation contract.
|
||||
// Optional: nil disables the dispatch loop.
|
||||
BindingTaskCreator store.BindingTaskCreator
|
||||
Forge forge.Forge
|
||||
Trees *worktree.Manager
|
||||
Bus *events.Bus
|
||||
Log *slog.Logger
|
||||
// Tasks is the optional message bus for task distribution. Nil means no
|
||||
// bus is configured; the existing SQLite ClaimNext flow is used.
|
||||
Tasks TaskBus
|
||||
@@ -332,6 +353,7 @@ func (d *Daemon) pollForIdentity(ctx context.Context, id *IdentityRunner) {
|
||||
func (d *Daemon) maintainAndDrain(ctx context.Context) {
|
||||
d.cleanupExpiredStorage(ctx)
|
||||
d.reconcilePRs(ctx)
|
||||
d.dispatchBindings(ctx)
|
||||
if d.Tasks != nil {
|
||||
d.drainNATS(ctx)
|
||||
} else {
|
||||
@@ -401,12 +423,13 @@ func (d *Daemon) pollEitherWithConfig(ctx context.Context, fg forge.Forge, cfg c
|
||||
}
|
||||
|
||||
// Cycle is one poll-and-drain pass: enqueue newly assigned issues,
|
||||
// reconcile open PRs, act on human replies to waiting tasks, then
|
||||
// process queued tasks concurrently up to containers.max_concurrency.
|
||||
// reconcile open PRs, run the binding dispatch loop, then process
|
||||
// queued tasks concurrently up to containers.max_concurrency.
|
||||
func (d *Daemon) Cycle(ctx context.Context) {
|
||||
d.cleanupExpiredStorage(ctx)
|
||||
d.poll(ctx)
|
||||
d.reconcilePRs(ctx)
|
||||
d.dispatchBindings(ctx)
|
||||
if d.Tasks != nil {
|
||||
d.drainNATS(ctx)
|
||||
} else {
|
||||
@@ -447,6 +470,198 @@ func (d *Daemon) drainSQLite(ctx context.Context) {
|
||||
dispatcher.Wait()
|
||||
}
|
||||
|
||||
// bindingDispatchBatchLimit caps how many undispatched captures a single
|
||||
// dispatchBindings pass will fetch from the store. The cap is defensive
|
||||
// (the captures table is also pruned by retention/maxEvents) and bounds a
|
||||
// single cycle's worst-case latency on a backlog.
|
||||
const bindingDispatchBatchLimit = 100
|
||||
|
||||
// dispatchBindings walks authenticated captures whose source has at
|
||||
// least one armed binding, evaluates each binding's matcher against
|
||||
// the capture, resolves fields through the binding's mapping, and
|
||||
// enqueues a task per matched (binding, capture) pair.
|
||||
//
|
||||
// The binding_dispatches ledger is the at-most-once guarantee across
|
||||
// cycles and daemon restarts: RecordDispatch returns
|
||||
// store.ErrAlreadyDispatched on a duplicate (binding_id, capture_id)
|
||||
// pair, and dispatchOneBinding treats that as a normal "another cycle
|
||||
// raced us" outcome rather than an error.
|
||||
//
|
||||
// Nil Bindings disables the loop (legacy behaviour: daemons built
|
||||
// before t2db.4 Phase E had no playbook bindings).
|
||||
func (d *Daemon) dispatchBindings(ctx context.Context) {
|
||||
if d.Bindings == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Gather armed sources first so the captures query filters down to
|
||||
// only relevant rows -- the table-level view over captured_events
|
||||
// is otherwise unbounded, and a backlog of unrelated sources
|
||||
// should never be scanned on a cycle.
|
||||
bindings, err := d.Bindings.ListBindings(ctx)
|
||||
if err != nil {
|
||||
d.Log.Warn("list bindings failed", "error", err)
|
||||
return
|
||||
}
|
||||
sources := make([]string, 0, len(bindings))
|
||||
for _, b := range bindings {
|
||||
if b.Status == binding.StatusArmed && b.Matcher.Source != "" {
|
||||
sources = append(sources, b.Matcher.Source)
|
||||
}
|
||||
}
|
||||
if len(sources) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
captures, err := d.BindingDispatcher.ListUndispatchedCaptures(ctx, sources, bindingDispatchBatchLimit)
|
||||
if err != nil {
|
||||
d.Log.Warn("list undispatched captures failed", "error", err)
|
||||
return
|
||||
}
|
||||
for _, c := range captures {
|
||||
armed, err := d.BindingDispatcher.ArmedBindingsForSource(ctx, c.Source)
|
||||
if err != nil {
|
||||
d.Log.Warn("armed bindings lookup", "source", c.Source, "error", err)
|
||||
continue
|
||||
}
|
||||
for _, b := range armed {
|
||||
if !b.Matcher.Matches(c.Source, c.Authenticated) {
|
||||
continue
|
||||
}
|
||||
d.dispatchOneBinding(ctx, b, c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// dispatchOneBinding handles a single (binding, capture) match: resolve
|
||||
// fields through the binding's mapping, enqueue a task, and record the
|
||||
// dispatch in the dedup ledger. Each step degrades independently --
|
||||
// a mapping lookup failure logs and returns, a required-field
|
||||
// resolution failure records a binding_dispatch_failure event and
|
||||
// returns without enqueueing, an enqueue failure logs and returns, and
|
||||
// a RecordDispatch returning ErrAlreadyDispatched is treated as a
|
||||
// benign race winner (another cycle already created the task).
|
||||
//
|
||||
// Owner/repo resolution currently falls back to the single configured
|
||||
// repo (when exactly one is configured); binding.Owner/Repo is a
|
||||
// follow-up so multi-repo deployments don't have to live with the
|
||||
// ambiguity.
|
||||
func (d *Daemon) dispatchOneBinding(ctx context.Context, b binding.Binding, c store.CapturedEvent) {
|
||||
if d.Mappings == nil {
|
||||
d.Log.Warn("binding dispatch: mapping store unavailable", "binding", b.ID)
|
||||
return
|
||||
}
|
||||
m, err := d.Mappings.GetMapping(ctx, b.MappingID)
|
||||
if err != nil {
|
||||
d.Log.Warn("binding dispatch: get mapping", "binding", b.ID, "mapping", b.MappingID, "error", err)
|
||||
return
|
||||
}
|
||||
if m == nil {
|
||||
d.Log.Warn("binding dispatch: mapping missing", "binding", b.ID, "mapping", b.MappingID)
|
||||
return
|
||||
}
|
||||
values, failures := mapping.Resolve(m.Fields, []byte(c.Body))
|
||||
if hasBlockingFailure(m.Fields, failures) {
|
||||
_, _ = d.Store.InsertEvent(ctx, events.Event{
|
||||
Kind: "binding_dispatch_failure",
|
||||
Detail: fmt.Sprintf("binding %d (capture %d): required field failed", b.ID, c.ID),
|
||||
Data: map[string]any{
|
||||
"binding_id": b.ID,
|
||||
"binding_version": b.Version,
|
||||
"capture_id": c.ID,
|
||||
"failures": failures,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
owner, repo, ok := d.resolveBindingRepo()
|
||||
if !ok {
|
||||
return // resolveBindingRepo already logged a warning
|
||||
}
|
||||
title := fmt.Sprintf("binding %s/%d from %s", b.Name, b.Version, c.Source)
|
||||
body := renderBindingBody(values, c)
|
||||
|
||||
task, err := d.BindingTaskCreator.EnqueueBindingTask(ctx, owner, repo, title, body, b.Workflow, "", b.ID, b.Version)
|
||||
if err != nil {
|
||||
d.Log.Warn("binding dispatch: enqueue", "binding", b.ID, "capture", c.ID, "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := d.BindingDispatcher.RecordDispatch(ctx, nil, b.ID, int64(b.Version), c.ID, task.ID); err != nil {
|
||||
if errors.Is(err, store.ErrAlreadyDispatched) {
|
||||
// Another cycle or daemon instance raced us and already
|
||||
// recorded the dispatch. The duplicate task remains
|
||||
// queued (delete-on-races is its own can of worms),
|
||||
// but the ledger prevents the same (binding, capture)
|
||||
// pair from ever being dispatched again.
|
||||
return
|
||||
}
|
||||
d.Log.Warn("binding dispatch: record", "binding", b.ID, "capture", c.ID, "error", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// hasBlockingFailure reports whether any of the failures belongs to a
|
||||
// required field. The mapping package reports failures only by field
|
||||
// name and reason -- the Field's Required flag is on the Field
|
||||
// declaration, so the dispatch loop re-derives it from the full
|
||||
// Field list. Failures on optional fields are warnings, not blocks:
|
||||
// the workflow still receives a partial value map.
|
||||
func hasBlockingFailure(fields []mapping.Field, failures []mapping.Failure) bool {
|
||||
if len(failures) == 0 {
|
||||
return false
|
||||
}
|
||||
required := make(map[string]bool, len(fields))
|
||||
for _, f := range fields {
|
||||
if f.Required {
|
||||
required[f.Name] = true
|
||||
}
|
||||
}
|
||||
for _, fail := range failures {
|
||||
if required[fail.FieldName] {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// renderBindingBody builds the task body from the values a mapping
|
||||
// resolved, one "key=value" pair per line. Empty maps still produce
|
||||
// a body that records the source capture id so the workflow has
|
||||
// provenance to hand back to the operator.
|
||||
func renderBindingBody(values map[string]any, c store.CapturedEvent) string {
|
||||
if len(values) == 0 {
|
||||
return fmt.Sprintf("(no fields resolved from capture %d)", c.ID)
|
||||
}
|
||||
parts := make([]string, 0, len(values))
|
||||
for k, v := range values {
|
||||
parts = append(parts, fmt.Sprintf("%s=%v", k, v))
|
||||
}
|
||||
return strings.Join(parts, "\n")
|
||||
}
|
||||
|
||||
// resolveBindingRepo picks the owner/repo a binding-spawned task
|
||||
// operates against. Today the binding vocabulary doesn't carry
|
||||
// owner/repo fields -- every binding applies to whatever repo the
|
||||
// daemon happens to be configured for. With exactly one configured
|
||||
// repo the choice is unambiguous; with zero or more than one the
|
||||
// loop refuses to dispatch (logged below) so a binding cannot
|
||||
// silently fire against the wrong target.
|
||||
//
|
||||
// A binding.Owner/Repo field is the natural follow-up; until then
|
||||
// this fallback is what Phase E can ship.
|
||||
func (d *Daemon) resolveBindingRepo() (string, string, bool) {
|
||||
repos := d.Cfg.Get().Repos
|
||||
if len(repos) == 1 {
|
||||
return repos[0].Owner, repos[0].Name, true
|
||||
}
|
||||
d.Log.Warn("binding dispatch: cannot resolve target repo",
|
||||
"hint", "configure exactly one [[repos]] entry, or extend the binding schema with owner/repo fields",
|
||||
"configured_repos", len(repos))
|
||||
return "", "", false
|
||||
}
|
||||
|
||||
// drainNATS processes tasks from NATS, falling back to SQLite ClaimNext for
|
||||
// requeued tasks (waiting_human approval, retry-parked) that didn't come
|
||||
// through a NATS publish.
|
||||
|
||||
@@ -0,0 +1,287 @@
|
||||
package daemon
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
"github.com/samcharles93/archie-core/internal/domain/mapping"
|
||||
"github.com/samcharles93/archie-core/internal/store"
|
||||
)
|
||||
|
||||
// Test 30: an armed binding + an authenticated capture creates a task
|
||||
// whose provenance points back at the binding (and its version), and
|
||||
// records a dispatch row in the dedup ledger.
|
||||
func TestDispatchBindingsCreatesTaskFromArmedCapture(t *testing.T) {
|
||||
s := openDispatchTestStore(t)
|
||||
mappingID := seedMapping(t, s, "sentry", mapping.Field{Name: "title", Path: "title", Type: mapping.TypeString, Required: true})
|
||||
bindingID, bindingVersion := seedArmedBinding(t, s, "sentry", mappingID)
|
||||
seedCapture(t, s, "sentry", true, `{"title":"hello"}`)
|
||||
|
||||
d := newDispatchDaemon(t, s)
|
||||
d.dispatchBindings(t.Context())
|
||||
|
||||
tasks, err := s.Tasks(t.Context(), 10)
|
||||
if err != nil {
|
||||
t.Fatalf("Tasks: %v", err)
|
||||
}
|
||||
if len(tasks) != 1 {
|
||||
t.Fatalf("Tasks len = %d, want 1", len(tasks))
|
||||
}
|
||||
got := tasks[0]
|
||||
if got.BindingID != bindingID {
|
||||
t.Errorf("task.BindingID = %d, want %d", got.BindingID, bindingID)
|
||||
}
|
||||
if got.BindingVersion != bindingVersion {
|
||||
t.Errorf("task.BindingVersion = %d, want %d", got.BindingVersion, bindingVersion)
|
||||
}
|
||||
if got.Source != store.SourceChat {
|
||||
t.Errorf("task.Source = %q, want %q", got.Source, store.SourceChat)
|
||||
}
|
||||
if got.Status != store.StatusQueued {
|
||||
t.Errorf("task.Status = %q, want %q", got.Status, store.StatusQueued)
|
||||
}
|
||||
|
||||
assertDispatchRecorded(t, s, bindingID, got.ID)
|
||||
}
|
||||
|
||||
// Test 31: the binding_dispatches ledger is the at-most-once
|
||||
// guarantee across cycles -- running dispatchBindings twice must
|
||||
// yield exactly one task even though the undispatched-captures query
|
||||
// keeps finding the capture on the second pass.
|
||||
func TestDispatchBindingsDoesNotDoubleDispatch(t *testing.T) {
|
||||
s := openDispatchTestStore(t)
|
||||
mappingID := seedMapping(t, s, "sentry", mapping.Field{Name: "title", Path: "title", Type: mapping.TypeString})
|
||||
seedArmedBinding(t, s, "sentry", mappingID)
|
||||
seedCapture(t, s, "sentry", true, `{"title":"hello"}`)
|
||||
|
||||
d := newDispatchDaemon(t, s)
|
||||
d.dispatchBindings(t.Context())
|
||||
d.dispatchBindings(t.Context())
|
||||
|
||||
tasks, err := s.Tasks(t.Context(), 10)
|
||||
if err != nil {
|
||||
t.Fatalf("Tasks: %v", err)
|
||||
}
|
||||
if len(tasks) != 1 {
|
||||
t.Fatalf("after two cycles, Tasks len = %d, want 1 (ledger dedup)", len(tasks))
|
||||
}
|
||||
}
|
||||
|
||||
// Test 32: an unauthenticated capture can never spawn a task -- the
|
||||
// matcher refuses it regardless of source match (binding.Matcher.Matches
|
||||
// gates on authenticated, see docs/prds/webhook-intake-security.md).
|
||||
func TestDispatchBindingsSkipsUnauthenticatedCaptures(t *testing.T) {
|
||||
s := openDispatchTestStore(t)
|
||||
mappingID := seedMapping(t, s, "sentry", mapping.Field{Name: "title", Path: "title", Type: mapping.TypeString})
|
||||
seedArmedBinding(t, s, "sentry", mappingID)
|
||||
seedCapture(t, s, "sentry", false, `{"title":"hello"}`)
|
||||
|
||||
d := newDispatchDaemon(t, s)
|
||||
d.dispatchBindings(t.Context())
|
||||
|
||||
tasks, err := s.Tasks(t.Context(), 10)
|
||||
if err != nil {
|
||||
t.Fatalf("Tasks: %v", err)
|
||||
}
|
||||
if len(tasks) != 0 {
|
||||
t.Fatalf("unauthenticated capture spawned %d task(s), want 0", len(tasks))
|
||||
}
|
||||
}
|
||||
|
||||
// Test 33: a required field whose path is missing in the capture body
|
||||
// blocks the dispatch -- the failure path emits a
|
||||
// binding_dispatch_failure event and creates no task.
|
||||
func TestDispatchBindingsSkipsWhenResolveFails(t *testing.T) {
|
||||
s := openDispatchTestStore(t)
|
||||
mappingID := seedMapping(t, s, "sentry",
|
||||
mapping.Field{Name: "title", Path: "title", Type: mapping.TypeString, Required: true})
|
||||
seedArmedBinding(t, s, "sentry", mappingID)
|
||||
// Body is missing the required "title" path.
|
||||
seedCapture(t, s, "sentry", true, `{"other":"value"}`)
|
||||
|
||||
d := newDispatchDaemon(t, s)
|
||||
d.dispatchBindings(t.Context())
|
||||
|
||||
tasks, err := s.Tasks(t.Context(), 10)
|
||||
if err != nil {
|
||||
t.Fatalf("Tasks: %v", err)
|
||||
}
|
||||
if len(tasks) != 0 {
|
||||
t.Fatalf("required-field failure spawned %d task(s), want 0", len(tasks))
|
||||
}
|
||||
events, err := s.EventsSince(t.Context(), 0, 100)
|
||||
if err != nil {
|
||||
t.Fatalf("EventsSince: %v", err)
|
||||
}
|
||||
found := false
|
||||
for _, e := range events {
|
||||
if e.Kind == "binding_dispatch_failure" {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("binding_dispatch_failure event not published; got %d events", len(events))
|
||||
}
|
||||
}
|
||||
|
||||
// Test 34: with no armed bindings the loop is a no-op -- even with
|
||||
// authenticated captures sitting in the table.
|
||||
func TestDispatchBindingsNoOpWhenNoArmedBindings(t *testing.T) {
|
||||
s := openDispatchTestStore(t)
|
||||
// Capture is present, but no binding rows exist.
|
||||
seedCapture(t, s, "sentry", true, `{"title":"hello"}`)
|
||||
|
||||
d := newDispatchDaemon(t, s)
|
||||
d.dispatchBindings(t.Context())
|
||||
|
||||
tasks, err := s.Tasks(t.Context(), 10)
|
||||
if err != nil {
|
||||
t.Fatalf("Tasks: %v", err)
|
||||
}
|
||||
if len(tasks) != 0 {
|
||||
t.Fatalf("no-armed-bindings loop spawned %d task(s), want 0", len(tasks))
|
||||
}
|
||||
}
|
||||
|
||||
// Test: dispatchBindings is a no-op when Bindings is nil -- the loop
|
||||
// must degrade silently rather than panic in legacy compositions.
|
||||
func TestDispatchBindingsNilBindingsIsNoOp(t *testing.T) {
|
||||
d := &Daemon{
|
||||
Cfg: config.NewHolder(config.Config{}),
|
||||
Log: slog.New(slog.NewTextHandler(os.Stderr, nil)),
|
||||
}
|
||||
// Should not panic.
|
||||
d.dispatchBindings(t.Context())
|
||||
}
|
||||
|
||||
// ── helpers ──────────────────────────────────────────────────────
|
||||
|
||||
// openDispatchTestStore creates a fresh in-memory SQLite store for
|
||||
// each dispatch test. The TempDir anchor matches store.OpenTest's
|
||||
// own convention; tests rely on the same schema-migration path
|
||||
// production uses (CREATE TABLE + ALTER TABLE migrations), so
|
||||
// binding_id / binding_version / binding_dispatches all exist on the
|
||||
// store by the time a test runs.
|
||||
func openDispatchTestStore(t *testing.T) *store.Store {
|
||||
t.Helper()
|
||||
s, err := store.Open(t.Context(), filepath.Join(t.TempDir(), "dispatch.db"))
|
||||
if err != nil {
|
||||
t.Fatalf("store.Open: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = s.Close() })
|
||||
return s
|
||||
}
|
||||
|
||||
// seedMapping inserts a mapping with the given fields and returns its id.
|
||||
// The Name field is "sentry-mapping" by default; callers pass fields
|
||||
// inline.
|
||||
func seedMapping(t *testing.T, s *store.Store, sourceHint string, fields ...mapping.Field) int64 {
|
||||
t.Helper()
|
||||
id, err := s.InsertMapping(t.Context(), mapping.Mapping{
|
||||
Name: sourceHint + "-mapping",
|
||||
SourceHint: sourceHint,
|
||||
Fields: fields,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("InsertMapping: %v", err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
// seedArmedBinding inserts a binding and walks it draft -> pending_approval
|
||||
// -> armed, matching the operator flow an Approve button on the
|
||||
// dashboard drives. InsertBinding forces status='draft' and the only
|
||||
// sanctioned armed transition is ApproveBinding, so the helper
|
||||
// advances through pending_approval via the store's test-only DB
|
||||
// accessor -- production callers must use the structured methods
|
||||
// (bindings.go) rather than touching the SQL directly.
|
||||
func seedArmedBinding(t *testing.T, s *store.Store, source string, mappingID int64) (int64, int) {
|
||||
t.Helper()
|
||||
id, err := s.InsertBinding(t.Context(), binding.Binding{
|
||||
Name: "test " + source,
|
||||
Matcher: binding.Matcher{Source: source},
|
||||
MappingID: mappingID,
|
||||
Workflow: "implement",
|
||||
Secret: "0123456789abcdef0123456789abcdef",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("InsertBinding: %v", err)
|
||||
}
|
||||
if _, err := s.DB().ExecContext(t.Context(), `UPDATE bindings SET status='pending_approval' WHERE id=?`, id); err != nil {
|
||||
t.Fatalf("force pending_approval: %v", err)
|
||||
}
|
||||
if err := s.ApproveBinding(t.Context(), id); err != nil {
|
||||
t.Fatalf("ApproveBinding: %v", err)
|
||||
}
|
||||
got, err := s.GetBinding(t.Context(), id)
|
||||
if err != nil || got == nil {
|
||||
t.Fatalf("GetBinding after approve: %+v, %v", got, err)
|
||||
}
|
||||
return id, got.Version
|
||||
}
|
||||
|
||||
// seedCapture inserts an inbound capture event with the given source,
|
||||
// authenticated flag, and body. retention=0, maxEvents=0 disable
|
||||
// the per-insert pruning the production path uses, so a test that
|
||||
// only needs a single row in the table is not at the mercy of
|
||||
// maxEvents=0's "no cap" reading.
|
||||
func seedCapture(t *testing.T, s *store.Store, source string, authenticated bool, body string) {
|
||||
t.Helper()
|
||||
_, err := s.InsertCapture(t.Context(), store.CapturedEvent{
|
||||
Source: source,
|
||||
Body: body,
|
||||
Authenticated: authenticated,
|
||||
ReceivedAt: time.Now().UTC(),
|
||||
}, 0, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("InsertCapture: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// newDispatchDaemon builds a Daemon whose store-related handles all
|
||||
// point at the same *store.Store so dispatchBindings can resolve
|
||||
// captures through the dispatcher handles and mappings through
|
||||
// d.Mappings. Exactly one repo is configured so resolveBindingRepo's
|
||||
// single-repo fallback accepts the dispatch; with zero or many
|
||||
// configured repos the loop would log and skip, masking the test's
|
||||
// positive assertions.
|
||||
func newDispatchDaemon(t *testing.T, s *store.Store) *Daemon {
|
||||
t.Helper()
|
||||
cfg := config.Config{
|
||||
Repos: []config.Repo{{Owner: "acme", Name: "widget"}},
|
||||
}
|
||||
return &Daemon{
|
||||
Cfg: config.NewHolder(cfg),
|
||||
Store: s,
|
||||
Mappings: s,
|
||||
Bindings: s,
|
||||
BindingDispatcher: s,
|
||||
BindingTaskCreator: s,
|
||||
Log: slog.New(slog.NewTextHandler(os.Stderr, nil)),
|
||||
}
|
||||
}
|
||||
|
||||
// assertDispatchRecorded reads the dedup ledger directly: the
|
||||
// (binding_id, capture_id) primary key is what guarantees no double
|
||||
// dispatch. Going through the SQL table rather than the public
|
||||
// BindingStore surface keeps the assertion focused on the contract
|
||||
// Phase E actually writes to, not on a ListDispatches helper that
|
||||
// may not exist.
|
||||
func assertDispatchRecorded(t *testing.T, s *store.Store, bindingID, taskID int64) {
|
||||
t.Helper()
|
||||
var gotBindingID, gotTaskID int64
|
||||
if err := s.DB().QueryRowContext(t.Context(),
|
||||
`SELECT binding_id, task_id FROM binding_dispatches WHERE binding_id=?`, bindingID,
|
||||
).Scan(&gotBindingID, &gotTaskID); err != nil {
|
||||
t.Fatalf("binding_dispatches row missing: %v", err)
|
||||
}
|
||||
if gotBindingID != bindingID || gotTaskID != taskID {
|
||||
t.Fatalf("binding_dispatches row = (%d, %d), want (%d, %d)", gotBindingID, gotTaskID, bindingID, taskID)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
// Package binding owns the playbook binding vocabulary: the matcher +
|
||||
// mapping + workflow + state-machine entity that turns a captured webhook
|
||||
// into an archie task. See docs/prds/webhook-intake-security.md and
|
||||
// docs/prds/payload-field-mapping.md.
|
||||
package binding
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Status is the binding lifecycle state. Mirrors telegram's
|
||||
// dangerousAction / pendingApproval shape: armed is the only state that
|
||||
// evaluates against incoming events; any edit drops armed back to
|
||||
// pending_approval; only an explicit Approve call moves
|
||||
// pending_approval -> armed.
|
||||
type Status string
|
||||
|
||||
const (
|
||||
StatusDraft Status = "draft"
|
||||
StatusPendingApproval Status = "pending_approval"
|
||||
StatusArmed Status = "armed"
|
||||
)
|
||||
|
||||
// Matcher decides which captured events a binding applies to. Source is
|
||||
// the only dimension today -- the path segment the sender POSTs to
|
||||
// (e.g. "sentry"). Extend with additional predicates if a use case
|
||||
// demands it; the storage column is plain TEXT so widening is
|
||||
// non-breaking.
|
||||
type Matcher struct {
|
||||
Source string `json:"source"`
|
||||
}
|
||||
|
||||
// Binding is the runtime-editable entity that ties a matcher, a payload
|
||||
// mapping, and a workflow together. Bindings live in the store, not in
|
||||
// config.toml, so operators can author them from the dashboard while the
|
||||
// daemon runs.
|
||||
//
|
||||
// Version is bumped on every UpdateBinding so a later edit cannot
|
||||
// silently rewrite the historical provenance of a task that already
|
||||
// fired. Secret is the HMAC-SHA256 shared secret a sender must sign with
|
||||
// for an event to be marked authenticated; it is never returned by
|
||||
// GET handlers.
|
||||
type Binding struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Matcher Matcher `json:"matcher"`
|
||||
MappingID int64 `json:"mapping_id"`
|
||||
Workflow string `json:"workflow"`
|
||||
Version int `json:"version"`
|
||||
Status Status `json:"status"`
|
||||
Secret string `json:"secret,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// minSecretLen is the floor for an acceptable shared secret. A trivially
|
||||
// short secret makes accidental binding to a known HMAC trivial; the
|
||||
// floor is chosen so a one-character typo cannot arm.
|
||||
const minSecretLen = 16
|
||||
|
||||
// Validate checks a binding is well-formed before it is persisted or
|
||||
// evaluated. A non-empty Name, a non-empty Matcher.Source, a positive
|
||||
// MappingID, a non-empty Workflow, and a sufficiently long Secret are
|
||||
// all required. Workflow existence is checked at the API/store layer
|
||||
// because the domain package does not import the workflow registry.
|
||||
func (b Binding) Validate() error {
|
||||
if strings.TrimSpace(b.Name) == "" {
|
||||
return fmt.Errorf("binding: name is required")
|
||||
}
|
||||
if strings.TrimSpace(b.Matcher.Source) == "" {
|
||||
return fmt.Errorf("binding: matcher.source is required")
|
||||
}
|
||||
if b.MappingID <= 0 {
|
||||
return fmt.Errorf("binding: mapping_id must be positive")
|
||||
}
|
||||
if strings.TrimSpace(b.Workflow) == "" {
|
||||
return fmt.Errorf("binding: workflow is required")
|
||||
}
|
||||
if len(b.Secret) < minSecretLen {
|
||||
return fmt.Errorf("binding: secret must be at least %d bytes", minSecretLen)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Matches reports whether the given captured event would match this
|
||||
// binding's matcher. The Authenticated clause encodes t2db.5 point 1 --
|
||||
// an unauthenticated event can never trigger a binding, no matter how
|
||||
// well it would otherwise match.
|
||||
//
|
||||
// Note: the caller is expected to be the dispatch loop or handleCapture,
|
||||
// both of which receive a CapturedEvent with the Authenticated flag
|
||||
// already set by HMAC verification. Matches is pure and has no I/O.
|
||||
func (m Matcher) Matches(source string, authenticated bool) bool {
|
||||
if !authenticated {
|
||||
return false
|
||||
}
|
||||
return m.Source == source
|
||||
}
|
||||
|
||||
// Overlaps reports whether two matchers cover any plausible event
|
||||
// together. With a single-string matcher this collapses to: both have
|
||||
// a non-empty Source equal to each other. Two empty matchers would
|
||||
// overlap trivially; Validate rejects empty Sources upstream so this
|
||||
// case cannot arise in practice.
|
||||
func (m Matcher) Overlaps(other Matcher) bool {
|
||||
if m.Source == "" || other.Source == "" {
|
||||
return false
|
||||
}
|
||||
return m.Source == other.Source
|
||||
}
|
||||
|
||||
// Normalize clamps an arbitrary string into a recognised Status, falling
|
||||
// back to StatusDraft for any unknown value. The DB CHECK constraint
|
||||
// already rejects bad strings at write time; Normalize is the API-edge
|
||||
// guard for inputs that arrive before they ever reach the store.
|
||||
func Normalize(s Status) Status {
|
||||
switch s {
|
||||
case StatusDraft, StatusPendingApproval, StatusArmed:
|
||||
return s
|
||||
default:
|
||||
return StatusDraft
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,403 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
)
|
||||
|
||||
const bindingsSchema = `
|
||||
CREATE TABLE IF NOT EXISTS bindings (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL DEFAULT '',
|
||||
source TEXT NOT NULL DEFAULT '',
|
||||
mapping_id INTEGER NOT NULL DEFAULT 0,
|
||||
workflow TEXT NOT NULL DEFAULT '',
|
||||
version INTEGER NOT NULL DEFAULT 1,
|
||||
status TEXT NOT NULL DEFAULT 'draft',
|
||||
secret TEXT NOT NULL DEFAULT '',
|
||||
created_at TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL,
|
||||
CHECK (status IN ('draft','pending_approval','armed'))
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_bindings_source_status ON bindings(source, status);
|
||||
CREATE TABLE IF NOT EXISTS binding_dispatches (
|
||||
binding_id INTEGER NOT NULL,
|
||||
binding_version INTEGER NOT NULL,
|
||||
capture_id INTEGER NOT NULL,
|
||||
task_id INTEGER NOT NULL,
|
||||
dispatched_at TEXT NOT NULL,
|
||||
PRIMARY KEY (binding_id, capture_id)
|
||||
);
|
||||
`
|
||||
|
||||
// ErrBindingNotFound is returned by UpdateBinding and DeleteBinding when no
|
||||
// row matches the given ID. GetBinding uses (nil, nil) instead, matching
|
||||
// TaskByID's convention -- "not found" is the normal answer to "does a
|
||||
// binding with this ID currently exist," not an error.
|
||||
var ErrBindingNotFound = errors.New("store: binding not found")
|
||||
|
||||
// ErrBindingOverlap is returned by InsertBinding and UpdateBinding when a
|
||||
// different binding already covers the same source. Two bindings for one
|
||||
// source would race over the same inbound webhook; the store refuses rather
|
||||
// than picking an arbitrary winner.
|
||||
var ErrBindingOverlap = errors.New("store: binding overlaps existing binding for source")
|
||||
|
||||
// ErrBindingTransition is returned by ApproveBinding when the from-status
|
||||
// is anything other than pending_approval. Approve is the only sanctioned
|
||||
// transition into armed, and the guard is the SQL WHERE clause, not a
|
||||
// separate read.
|
||||
var ErrBindingTransition = errors.New("store: binding state transition rejected")
|
||||
|
||||
// ErrAlreadyDispatched is returned by RecordDispatch when the (binding_id,
|
||||
// capture_id) pair already exists in binding_dispatches. The dedup ledger is
|
||||
// the at-most-once guarantee per capture; the caller is expected to surface
|
||||
// this to the dispatch loop rather than retry.
|
||||
var ErrAlreadyDispatched = errors.New("store: binding already dispatched for capture")
|
||||
|
||||
// bindingTimeLayout follows captureTimeLayout's reasoning: fixed-width
|
||||
// RFC3339 so string and chronological order agree.
|
||||
const bindingTimeLayout = time.RFC3339
|
||||
|
||||
// InsertBinding persists a new binding in status=draft regardless of the
|
||||
// caller's Status field, stamps CreatedAt/UpdatedAt to now, and seeds
|
||||
// Version=1. A pre-insert overlap check refuses a duplicate source in the
|
||||
// same transaction; ErrBindingOverlap is returned rather than a silent
|
||||
// constraint violation.
|
||||
func (s *Store) InsertBinding(ctx context.Context, b binding.Binding) (int64, error) {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
var overlapID int64
|
||||
row := tx.QueryRowContext(ctx, `SELECT id FROM bindings WHERE source = ? LIMIT 1`, b.Matcher.Source)
|
||||
if err := row.Scan(&overlapID); err == nil {
|
||||
return 0, ErrBindingOverlap
|
||||
} else if !errors.Is(err, sql.ErrNoRows) {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
now := time.Now().UTC().Format(bindingTimeLayout)
|
||||
res, err := tx.ExecContext(ctx, `
|
||||
INSERT INTO bindings (name, source, mapping_id, workflow, version, status, secret, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, 1, 'draft', ?, ?, ?)`,
|
||||
b.Name, b.Matcher.Source, b.MappingID, b.Workflow, b.Secret, now, now)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
id, err := res.LastInsertId()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// GetBinding returns the binding with the given ID, or (nil, nil) if none
|
||||
// exists.
|
||||
func (s *Store) GetBinding(ctx context.Context, id int64) (*binding.Binding, error) {
|
||||
row := s.db.QueryRowContext(ctx, `
|
||||
SELECT id, name, source, mapping_id, workflow, version, status, secret, created_at, updated_at
|
||||
FROM bindings WHERE id=?`, id)
|
||||
b, err := scanBindingRow(row)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// ListBindings returns every binding, newest first.
|
||||
func (s *Store) ListBindings(ctx context.Context) (out []binding.Binding, retErr error) {
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
SELECT id, name, source, mapping_id, workflow, version, status, secret, created_at, updated_at
|
||||
FROM bindings ORDER BY id DESC`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() { retErr = errors.Join(retErr, rows.Close()) }()
|
||||
|
||||
for rows.Next() {
|
||||
b, err := scanBindingRow(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, *b)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// UpdateBinding replaces the editable fields of an existing binding and
|
||||
// bumps Version by 1. Per docs/prds/webhook-intake-security.md point 2:
|
||||
// any edit always drops status to pending_approval, including edits to
|
||||
// already-armed bindings -- an edit cannot silently re-arm, or the
|
||||
// approval gate is decorative. Secret is preserved when the caller passes
|
||||
// an empty string via COALESCE(NULLIF(?, ”), secret) so a partial update
|
||||
// cannot erase the HMAC secret. Returns ErrBindingNotFound when no row
|
||||
// matches, ErrBindingOverlap when the new source collides with a different
|
||||
// binding.
|
||||
func (s *Store) UpdateBinding(ctx context.Context, b binding.Binding) error {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
var overlapID int64
|
||||
row := tx.QueryRowContext(ctx, `SELECT id FROM bindings WHERE source = ? AND id != ? LIMIT 1`,
|
||||
b.Matcher.Source, b.ID)
|
||||
if err := row.Scan(&overlapID); err == nil {
|
||||
return ErrBindingOverlap
|
||||
} else if !errors.Is(err, sql.ErrNoRows) {
|
||||
return err
|
||||
}
|
||||
|
||||
now := time.Now().UTC().Format(bindingTimeLayout)
|
||||
res, err := tx.ExecContext(ctx, `
|
||||
UPDATE bindings SET name=?, source=?, mapping_id=?, workflow=?, secret=COALESCE(NULLIF(?, ''), secret),
|
||||
status = 'pending_approval',
|
||||
version = version + 1,
|
||||
updated_at = ?
|
||||
WHERE id=?`,
|
||||
b.Name, b.Matcher.Source, b.MappingID, b.Workflow, b.Secret, now, b.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
return ErrBindingNotFound
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// DeleteBinding removes a binding by ID along with any rows it owns in
|
||||
// binding_dispatches. The dedup rows are deleted in the same transaction
|
||||
// so a recreate with the same source does not inherit old dedup state.
|
||||
// Returns ErrBindingNotFound if it does not exist.
|
||||
func (s *Store) DeleteBinding(ctx context.Context, id int64) error {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
if _, err := tx.ExecContext(ctx, `DELETE FROM binding_dispatches WHERE binding_id = ?`, id); err != nil {
|
||||
return err
|
||||
}
|
||||
res, err := tx.ExecContext(ctx, `DELETE FROM bindings WHERE id=?`, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
return ErrBindingNotFound
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// ApproveBinding moves a binding from pending_approval to armed. The
|
||||
// status guard is the SQL WHERE clause: a row whose status does not match
|
||||
// pending_approval yields RowsAffected == 0 and is distinguished as a
|
||||
// transition error (row exists, wrong from-status) or a not-found error
|
||||
// (no row) by a follow-up SELECT inside the same transaction. Approve is
|
||||
// also refused when a different binding is already armed for the same
|
||||
// source: the overlap rule has to hold across status changes, so this is
|
||||
// a TOCTOU guard rather than a primary check (Insert/Update own the
|
||||
// initial overlap prevention).
|
||||
func (s *Store) ApproveBinding(ctx context.Context, id int64) error {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
var source string
|
||||
row := tx.QueryRowContext(ctx, `SELECT source FROM bindings WHERE id=?`, id)
|
||||
if err := row.Scan(&source); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ErrBindingNotFound
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
var otherArmed int64
|
||||
row = tx.QueryRowContext(ctx,
|
||||
`SELECT id FROM bindings WHERE source=? AND status='armed' AND id != ? LIMIT 1`,
|
||||
source, id)
|
||||
if err := row.Scan(&otherArmed); err == nil {
|
||||
return ErrBindingOverlap
|
||||
} else if !errors.Is(err, sql.ErrNoRows) {
|
||||
return err
|
||||
}
|
||||
|
||||
now := time.Now().UTC().Format(bindingTimeLayout)
|
||||
res, err := tx.ExecContext(ctx, `
|
||||
UPDATE bindings SET status='armed', updated_at=?
|
||||
WHERE id=? AND status='pending_approval'`,
|
||||
now, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
return ErrBindingTransition
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// ArmedBindingsForSource returns every armed binding for the given source,
|
||||
// ordered by id so handleCapture picks a stable winner if multiple rows
|
||||
// are ever present (the overlap guard should make this impossible in
|
||||
// practice, but the store returns the full set rather than collapsing).
|
||||
func (s *Store) ArmedBindingsForSource(ctx context.Context, source string) ([]binding.Binding, error) {
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
SELECT id, name, source, mapping_id, workflow, version, status, secret, created_at, updated_at
|
||||
FROM bindings WHERE source=? AND status='armed' ORDER BY id`, source)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() { _ = rows.Close() }()
|
||||
|
||||
var out []binding.Binding
|
||||
for rows.Next() {
|
||||
b, err := scanBindingRow(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, *b)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// sqlExecutor is the subset of *sql.DB / *sql.Tx that RecordDispatch needs.
|
||||
// Both satisfy it; nil falls back to the store's own *sql.DB so callers
|
||||
// that don't open an explicit transaction get best-effort semantics.
|
||||
type sqlExecutor interface {
|
||||
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
|
||||
}
|
||||
|
||||
// RecordDispatch writes a (binding, capture) dedup row. The caller may
|
||||
// pass a *sql.Tx to make the dispatch row commit atomically with the
|
||||
// surrounding task insert; pass nil to use the store's own connection
|
||||
// (best-effort, not atomic with the task row). INSERT OR IGNORE +
|
||||
// RowsAffected is the dedup test: a duplicate (binding_id, capture_id)
|
||||
// is a no-op write and returns ErrAlreadyDispatched rather than a
|
||||
// constraint error.
|
||||
func (s *Store) RecordDispatch(
|
||||
ctx context.Context,
|
||||
tx *sql.Tx,
|
||||
bindingID int64,
|
||||
bindingVersion int64,
|
||||
captureID int64,
|
||||
taskID int64,
|
||||
) error {
|
||||
exec := sqlExecutor(s.db)
|
||||
if tx != nil {
|
||||
exec = tx
|
||||
}
|
||||
res, err := exec.ExecContext(ctx, `
|
||||
INSERT OR IGNORE INTO binding_dispatches (binding_id, binding_version, capture_id, task_id, dispatched_at)
|
||||
VALUES (?, ?, ?, ?, ?)`,
|
||||
bindingID, bindingVersion, captureID, taskID,
|
||||
time.Now().UTC().Format(bindingTimeLayout))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
return ErrAlreadyDispatched
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListUndispatchedCaptures returns authenticated captures that have not
|
||||
// yet been dispatched by any armed binding whose source matches. The
|
||||
// nested source query means the result is naturally limited to sources
|
||||
// with an armed binding; an empty sources slice is therefore a no-arms
|
||||
// short-circuit and returns (nil, nil) without hitting the table.
|
||||
func (s *Store) ListUndispatchedCaptures(ctx context.Context, sources []string, limit int) ([]CapturedEvent, error) {
|
||||
if len(sources) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
SELECT id, received_at, source, remote_addr, content_type, headers, body, authenticated
|
||||
FROM captured_events
|
||||
WHERE authenticated = 1
|
||||
AND id NOT IN (SELECT capture_id FROM binding_dispatches)
|
||||
AND source IN (
|
||||
SELECT source FROM bindings WHERE status = 'armed' GROUP BY source
|
||||
)
|
||||
ORDER BY id
|
||||
LIMIT ?`, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() { _ = rows.Close() }()
|
||||
|
||||
var out []CapturedEvent
|
||||
for rows.Next() {
|
||||
var c CapturedEvent
|
||||
var receivedAt string
|
||||
var authenticated int
|
||||
if err := rows.Scan(&c.ID, &receivedAt, &c.Source, &c.RemoteAddr,
|
||||
&c.ContentType, &c.Headers, &c.Body, &authenticated); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
parsed, err := parseCaptureTime(receivedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.ReceivedAt = parsed
|
||||
c.Authenticated = authenticated != 0
|
||||
out = append(out, c)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// bindingScanner is satisfied by both *sql.Row and *sql.Rows so
|
||||
// scanBindingRow's column list stays in exactly one place.
|
||||
type bindingScanner interface {
|
||||
Scan(dest ...any) error
|
||||
}
|
||||
|
||||
func scanBindingRow(row bindingScanner) (*binding.Binding, error) {
|
||||
var b binding.Binding
|
||||
var createdAt, updatedAt string
|
||||
if err := row.Scan(&b.ID, &b.Name, &b.Matcher.Source, &b.MappingID, &b.Workflow,
|
||||
&b.Version, &b.Status, &b.Secret, &createdAt, &updatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
created, err := time.Parse(bindingTimeLayout, createdAt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("store: unrecognised binding created_at %q: %w", createdAt, err)
|
||||
}
|
||||
updated, err := time.Parse(bindingTimeLayout, updatedAt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("store: unrecognised binding updated_at %q: %w", updatedAt, err)
|
||||
}
|
||||
b.CreatedAt = created.UTC()
|
||||
b.UpdatedAt = updated.UTC()
|
||||
return &b, nil
|
||||
}
|
||||
@@ -0,0 +1,434 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
)
|
||||
|
||||
// testBinding returns a valid binding with a sufficiently long secret for
|
||||
// Validate. Tests that need a different shape build their own.
|
||||
func testBinding(source string) binding.Binding {
|
||||
return binding.Binding{
|
||||
Name: "sentry binding",
|
||||
Matcher: binding.Matcher{Source: source},
|
||||
MappingID: 1,
|
||||
Workflow: "implement",
|
||||
Status: binding.StatusDraft,
|
||||
Secret: "0123456789abcdef0123456789abcdef",
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertBindingRoundTrips(t *testing.T) {
|
||||
s := openTest(t)
|
||||
b := testBinding("sentry")
|
||||
id, err := s.InsertBinding(t.Context(), b)
|
||||
if err != nil {
|
||||
t.Fatalf("InsertBinding: %v", err)
|
||||
}
|
||||
if id == 0 {
|
||||
t.Fatalf("InsertBinding returned zero id")
|
||||
}
|
||||
|
||||
got, err := s.GetBinding(t.Context(), id)
|
||||
if err != nil {
|
||||
t.Fatalf("GetBinding: %v", err)
|
||||
}
|
||||
if got == nil {
|
||||
t.Fatalf("GetBinding returned nil for a just-inserted row")
|
||||
}
|
||||
if got.ID != id {
|
||||
t.Fatalf("round-tripped id = %d, want %d", got.ID, id)
|
||||
}
|
||||
if got.Name != b.Name || got.Workflow != b.Workflow || got.MappingID != b.MappingID {
|
||||
t.Fatalf("round-tripped fields = %+v", got)
|
||||
}
|
||||
if got.Matcher.Source != b.Matcher.Source {
|
||||
t.Fatalf("round-tripped matcher.source = %q, want %q", got.Matcher.Source, b.Matcher.Source)
|
||||
}
|
||||
if got.Status != binding.StatusDraft {
|
||||
t.Fatalf("status = %q, want %q (insert forces draft regardless of caller)", got.Status, binding.StatusDraft)
|
||||
}
|
||||
if got.Version != 1 {
|
||||
t.Fatalf("version = %d, want 1", got.Version)
|
||||
}
|
||||
if got.Secret != b.Secret {
|
||||
t.Fatalf("secret round-trip mismatch")
|
||||
}
|
||||
if got.CreatedAt.IsZero() || got.UpdatedAt.IsZero() {
|
||||
t.Fatalf("CreatedAt/UpdatedAt must be stamped, got %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetBindingMissingReturnsNilNoError(t *testing.T) {
|
||||
s := openTest(t)
|
||||
got, err := s.GetBinding(t.Context(), 99999)
|
||||
if err != nil {
|
||||
t.Fatalf("GetBinding: %v", err)
|
||||
}
|
||||
if got != nil {
|
||||
t.Fatalf("GetBinding for a missing id = %+v, want nil", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListBindingsOrdersNewestFirst(t *testing.T) {
|
||||
s := openTest(t)
|
||||
for i, src := range []string{"alpha", "beta", "gamma"} {
|
||||
if _, err := s.InsertBinding(t.Context(), testBinding(src)); err != nil {
|
||||
t.Fatalf("InsertBinding[%d]: %v", i, err)
|
||||
}
|
||||
}
|
||||
got, err := s.ListBindings(t.Context())
|
||||
if err != nil {
|
||||
t.Fatalf("ListBindings: %v", err)
|
||||
}
|
||||
if len(got) != 3 {
|
||||
t.Fatalf("ListBindings = %d rows, want 3", len(got))
|
||||
}
|
||||
if got[0].ID <= got[1].ID || got[1].ID <= got[2].ID {
|
||||
t.Fatalf("ListBindings not newest-first: ids = %d, %d, %d", got[0].ID, got[1].ID, got[2].ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateBindingBumpsVersion(t *testing.T) {
|
||||
s := openTest(t)
|
||||
id, err := s.InsertBinding(t.Context(), testBinding("sentry"))
|
||||
if err != nil {
|
||||
t.Fatalf("InsertBinding: %v", err)
|
||||
}
|
||||
updated := testBinding("sentry")
|
||||
updated.ID = id
|
||||
updated.Name = "renamed"
|
||||
if err := s.UpdateBinding(t.Context(), updated); err != nil {
|
||||
t.Fatalf("UpdateBinding: %v", err)
|
||||
}
|
||||
got, err := s.GetBinding(t.Context(), id)
|
||||
if err != nil || got == nil {
|
||||
t.Fatalf("GetBinding after update: %+v, %v", got, err)
|
||||
}
|
||||
if got.Version != 2 {
|
||||
t.Fatalf("version = %d, want 2 after one update", got.Version)
|
||||
}
|
||||
if got.Name != "renamed" {
|
||||
t.Fatalf("updated name = %q, want %q", got.Name, "renamed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateBindingOnArmedForcesPendingApproval(t *testing.T) {
|
||||
s := openTest(t)
|
||||
id, err := s.InsertBinding(t.Context(), testBinding("sentry"))
|
||||
if err != nil {
|
||||
t.Fatalf("InsertBinding: %v", err)
|
||||
}
|
||||
if _, err := s.db.ExecContext(t.Context(), `UPDATE bindings SET status='pending_approval' WHERE id=?`, id); err != nil {
|
||||
t.Fatalf("force pending_approval: %v", err)
|
||||
}
|
||||
if err := s.ApproveBinding(t.Context(), id); err != nil {
|
||||
t.Fatalf("ApproveBinding: %v", err)
|
||||
}
|
||||
armed, err := s.GetBinding(t.Context(), id)
|
||||
if err != nil || armed == nil {
|
||||
t.Fatalf("GetBinding after approve: %+v, %v", armed, err)
|
||||
}
|
||||
if armed.Status != binding.StatusArmed {
|
||||
t.Fatalf("status after approve = %q, want armed", armed.Status)
|
||||
}
|
||||
|
||||
// An edit on an armed binding must drop it back to pending_approval.
|
||||
updated := *armed
|
||||
updated.Name = "edit while armed"
|
||||
if err := s.UpdateBinding(t.Context(), updated); err != nil {
|
||||
t.Fatalf("UpdateBinding on armed: %v", err)
|
||||
}
|
||||
got, err := s.GetBinding(t.Context(), id)
|
||||
if err != nil || got == nil {
|
||||
t.Fatalf("GetBinding after armed edit: %+v, %v", got, err)
|
||||
}
|
||||
if got.Status != binding.StatusPendingApproval {
|
||||
t.Fatalf("status after armed edit = %q, want pending_approval", got.Status)
|
||||
}
|
||||
if got.Version <= armed.Version {
|
||||
t.Fatalf("version did not advance after update: was %d, now %d", armed.Version, got.Version)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateBindingPreservesSecretWhenEmpty(t *testing.T) {
|
||||
s := openTest(t)
|
||||
b := testBinding("sentry")
|
||||
b.Secret = "0123456789abcdef0123456789abcdef"
|
||||
id, err := s.InsertBinding(t.Context(), b)
|
||||
if err != nil {
|
||||
t.Fatalf("InsertBinding: %v", err)
|
||||
}
|
||||
updated := testBinding("sentry")
|
||||
updated.ID = id
|
||||
updated.Secret = "" // empty -> preserve
|
||||
if err := s.UpdateBinding(t.Context(), updated); err != nil {
|
||||
t.Fatalf("UpdateBinding: %v", err)
|
||||
}
|
||||
got, err := s.GetBinding(t.Context(), id)
|
||||
if err != nil || got == nil {
|
||||
t.Fatalf("GetBinding after update: %+v, %v", got, err)
|
||||
}
|
||||
if got.Secret != b.Secret {
|
||||
t.Fatalf("secret = %q, want preserved %q", got.Secret, b.Secret)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApproveBindingFromPendingApprovalToArmedSucceeds(t *testing.T) {
|
||||
s := openTest(t)
|
||||
id, err := s.InsertBinding(t.Context(), testBinding("sentry"))
|
||||
if err != nil {
|
||||
t.Fatalf("InsertBinding: %v", err)
|
||||
}
|
||||
if _, err := s.db.ExecContext(t.Context(), `UPDATE bindings SET status='pending_approval' WHERE id=?`, id); err != nil {
|
||||
t.Fatalf("force pending_approval: %v", err)
|
||||
}
|
||||
if err := s.ApproveBinding(t.Context(), id); err != nil {
|
||||
t.Fatalf("ApproveBinding: %v", err)
|
||||
}
|
||||
got, err := s.GetBinding(t.Context(), id)
|
||||
if err != nil || got == nil {
|
||||
t.Fatalf("GetBinding after approve: %+v, %v", got, err)
|
||||
}
|
||||
if got.Status != binding.StatusArmed {
|
||||
t.Fatalf("status = %q, want armed", got.Status)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApproveBindingFromDraftFails(t *testing.T) {
|
||||
s := openTest(t)
|
||||
id, err := s.InsertBinding(t.Context(), testBinding("sentry"))
|
||||
if err != nil {
|
||||
t.Fatalf("InsertBinding: %v", err)
|
||||
}
|
||||
err = s.ApproveBinding(t.Context(), id)
|
||||
if !errors.Is(err, ErrBindingTransition) {
|
||||
t.Fatalf("ApproveBinding from draft = %v, want ErrBindingTransition", err)
|
||||
}
|
||||
got, err := s.GetBinding(t.Context(), id)
|
||||
if err != nil || got == nil {
|
||||
t.Fatalf("GetBinding after failed approve: %+v, %v", got, err)
|
||||
}
|
||||
if got.Status != binding.StatusDraft {
|
||||
t.Fatalf("status after failed approve = %q, want unchanged draft", got.Status)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApproveBindingFromArmedFails(t *testing.T) {
|
||||
s := openTest(t)
|
||||
id, err := s.InsertBinding(t.Context(), testBinding("sentry"))
|
||||
if err != nil {
|
||||
t.Fatalf("InsertBinding: %v", err)
|
||||
}
|
||||
if _, err := s.db.ExecContext(t.Context(), `UPDATE bindings SET status='armed' WHERE id=?`, id); err != nil {
|
||||
t.Fatalf("force armed: %v", err)
|
||||
}
|
||||
err = s.ApproveBinding(t.Context(), id)
|
||||
if !errors.Is(err, ErrBindingTransition) {
|
||||
t.Fatalf("ApproveBinding from armed = %v, want ErrBindingTransition", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApproveBindingMissingReturnsErrBindingNotFound(t *testing.T) {
|
||||
s := openTest(t)
|
||||
err := s.ApproveBinding(t.Context(), 99999)
|
||||
if !errors.Is(err, ErrBindingNotFound) {
|
||||
t.Fatalf("ApproveBinding on missing id = %v, want ErrBindingNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertBindingRejectsOverlap(t *testing.T) {
|
||||
s := openTest(t)
|
||||
if _, err := s.InsertBinding(t.Context(), testBinding("sentry")); err != nil {
|
||||
t.Fatalf("first InsertBinding: %v", err)
|
||||
}
|
||||
_, err := s.InsertBinding(t.Context(), testBinding("sentry"))
|
||||
if !errors.Is(err, ErrBindingOverlap) {
|
||||
t.Fatalf("second InsertBinding (same source) = %v, want ErrBindingOverlap", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApproveBindingRejectsOverlapAfterSecondInserted(t *testing.T) {
|
||||
s := openTest(t)
|
||||
first, err := s.InsertBinding(t.Context(), testBinding("sentry"))
|
||||
if err != nil {
|
||||
t.Fatalf("first InsertBinding: %v", err)
|
||||
}
|
||||
if _, err := s.db.ExecContext(t.Context(), `UPDATE bindings SET status='pending_approval' WHERE id=?`, first); err != nil {
|
||||
t.Fatalf("force pending_approval: %v", err)
|
||||
}
|
||||
if err := s.ApproveBinding(t.Context(), first); err != nil {
|
||||
t.Fatalf("ApproveBinding first: %v", err)
|
||||
}
|
||||
|
||||
second, err := s.InsertBinding(t.Context(), testBinding("sentry"))
|
||||
if err == nil {
|
||||
// A duplicate-source insert must be rejected outright, so reaching
|
||||
// here means the overlap guard failed at insert time.
|
||||
t.Fatalf("second InsertBinding for armed source = id %d, want ErrBindingOverlap", second)
|
||||
}
|
||||
if !errors.Is(err, ErrBindingOverlap) {
|
||||
t.Fatalf("second InsertBinding = %v, want ErrBindingOverlap", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteBindingRemovesRowAndLedgerEntries(t *testing.T) {
|
||||
s := openTest(t)
|
||||
id, err := s.InsertBinding(t.Context(), testBinding("sentry"))
|
||||
if err != nil {
|
||||
t.Fatalf("InsertBinding: %v", err)
|
||||
}
|
||||
tx, err := s.db.BeginTx(t.Context(), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("BeginTx: %v", err)
|
||||
}
|
||||
if err := s.RecordDispatch(t.Context(), tx, id, 1, 42, 7); err != nil {
|
||||
t.Fatalf("RecordDispatch: %v", err)
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
t.Fatalf("Commit: %v", err)
|
||||
}
|
||||
if err := s.DeleteBinding(t.Context(), id); err != nil {
|
||||
t.Fatalf("DeleteBinding: %v", err)
|
||||
}
|
||||
got, err := s.GetBinding(t.Context(), id)
|
||||
if err != nil {
|
||||
t.Fatalf("GetBinding after delete: %v", err)
|
||||
}
|
||||
if got != nil {
|
||||
t.Fatalf("GetBinding after delete = %+v, want nil", got)
|
||||
}
|
||||
var n int
|
||||
if err := s.db.QueryRowContext(t.Context(), `SELECT COUNT(*) FROM binding_dispatches WHERE binding_id=?`, id).Scan(&n); err != nil {
|
||||
t.Fatalf("COUNT binding_dispatches: %v", err)
|
||||
}
|
||||
if n != 0 {
|
||||
t.Fatalf("binding_dispatches rows for binding_id=%d = %d, want 0", id, n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecordDispatchIsIdempotent(t *testing.T) {
|
||||
s := openTest(t)
|
||||
id, err := s.InsertBinding(t.Context(), testBinding("sentry"))
|
||||
if err != nil {
|
||||
t.Fatalf("InsertBinding: %v", err)
|
||||
}
|
||||
ctx := t.Context()
|
||||
tx1, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("BeginTx 1: %v", err)
|
||||
}
|
||||
if err := s.RecordDispatch(ctx, tx1, id, 1, 42, 7); err != nil {
|
||||
t.Fatalf("first RecordDispatch: %v", err)
|
||||
}
|
||||
if err := tx1.Commit(); err != nil {
|
||||
t.Fatalf("Commit 1: %v", err)
|
||||
}
|
||||
|
||||
tx2, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("BeginTx 2: %v", err)
|
||||
}
|
||||
err = s.RecordDispatch(ctx, tx2, id, 1, 42, 99)
|
||||
if !errors.Is(err, ErrAlreadyDispatched) {
|
||||
t.Fatalf("second RecordDispatch (same binding/capture) = %v, want ErrAlreadyDispatched", err)
|
||||
}
|
||||
if err := tx2.Rollback(); err != nil {
|
||||
t.Fatalf("Rollback 2: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListUndispatchedCapturesExcludesDispatched(t *testing.T) {
|
||||
s := openTest(t)
|
||||
id, err := s.InsertBinding(t.Context(), testBinding("sentry"))
|
||||
if err != nil {
|
||||
t.Fatalf("InsertBinding: %v", err)
|
||||
}
|
||||
if _, err := s.db.ExecContext(t.Context(), `UPDATE bindings SET status='armed' WHERE id=?`, id); err != nil {
|
||||
t.Fatalf("arm binding: %v", err)
|
||||
}
|
||||
|
||||
// Capture 1: authenticated, will be dispatched -> should NOT appear.
|
||||
cap1, err := s.InsertCapture(t.Context(), CapturedEvent{
|
||||
Source: "sentry", Authenticated: true, Body: `{"id":"1"}`,
|
||||
}, 0, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("InsertCapture 1: %v", err)
|
||||
}
|
||||
// Capture 2: authenticated, not dispatched -> SHOULD appear.
|
||||
cap2, err := s.InsertCapture(t.Context(), CapturedEvent{
|
||||
Source: "sentry", Authenticated: true, Body: `{"id":"2"}`,
|
||||
}, 0, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("InsertCapture 2: %v", err)
|
||||
}
|
||||
// Capture 3: unauthenticated -> should NOT appear (matches() requires auth).
|
||||
cap3, err := s.InsertCapture(t.Context(), CapturedEvent{
|
||||
Source: "sentry", Authenticated: false, Body: `{"id":"3"}`,
|
||||
}, 0, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("InsertCapture 3: %v", err)
|
||||
}
|
||||
|
||||
ctx := t.Context()
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("BeginTx: %v", err)
|
||||
}
|
||||
if err := s.RecordDispatch(ctx, tx, id, 1, cap1, 100); err != nil {
|
||||
t.Fatalf("RecordDispatch: %v", err)
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
t.Fatalf("Commit: %v", err)
|
||||
}
|
||||
|
||||
got, err := s.ListUndispatchedCaptures(ctx, []string{"sentry"}, 10)
|
||||
if err != nil {
|
||||
t.Fatalf("ListUndispatchedCaptures: %v", err)
|
||||
}
|
||||
if len(got) != 1 {
|
||||
t.Fatalf("ListUndispatchedCaptures = %d rows, want 1", len(got))
|
||||
}
|
||||
if got[0].ID != cap2 {
|
||||
t.Fatalf("returned capture id = %d, want %d (cap3 unauthenticated, cap1 dispatched)", got[0].ID, cap2)
|
||||
}
|
||||
_ = cap3
|
||||
}
|
||||
|
||||
func TestListUndispatchedCapturesEmptySourcesReturnsNil(t *testing.T) {
|
||||
s := openTest(t)
|
||||
got, err := s.ListUndispatchedCaptures(t.Context(), nil, 10)
|
||||
if err != nil {
|
||||
t.Fatalf("ListUndispatchedCaptures(nil sources): %v", err)
|
||||
}
|
||||
if got != nil {
|
||||
t.Fatalf("ListUndispatchedCaptures(nil sources) = %+v, want nil", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRecordDispatchViaExplicitTx confirms RecordDispatch accepts an
|
||||
// external *sql.Tx (its primary contract: caller-owned transaction).
|
||||
// Without this signature the dispatch ledger and the tasks row created
|
||||
// from a binding could not commit atomically.
|
||||
func TestRecordDispatchViaExplicitTx(t *testing.T) {
|
||||
s := openTest(t)
|
||||
id, err := s.InsertBinding(t.Context(), testBinding("sentry"))
|
||||
if err != nil {
|
||||
t.Fatalf("InsertBinding: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("BeginTx: %v", err)
|
||||
}
|
||||
if err := s.RecordDispatch(ctx, tx, id, 1, 1, 1); err != nil {
|
||||
t.Fatalf("RecordDispatch via *sql.Tx: %v", err)
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
t.Fatalf("Commit: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -102,7 +102,7 @@ func (s *Store) Tasks(ctx context.Context, limit int) (tasks []Task, retErr erro
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
SELECT id, owner, repo, issue_number, title, status, workflow, stage,
|
||||
pr_number, tokens_used, iterations, attempt, park_reason, retry_count,
|
||||
created_at, updated_at, plan, source, identity
|
||||
created_at, updated_at, plan, source, identity, binding_id, binding_version
|
||||
FROM tasks ORDER BY updated_at DESC LIMIT ?`, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -120,7 +120,7 @@ func (s *Store) Tasks(ctx context.Context, limit int) (tasks []Task, retErr erro
|
||||
&t.Status, &t.Workflow, &t.Stage, &t.PRNumber, &t.TokensUsed,
|
||||
&t.Iterations, &t.Attempt, &t.ParkReason, &t.RetryCount,
|
||||
sqliteTime{&t.CreatedAt}, sqliteTime{&t.UpdatedAt},
|
||||
&t.Plan, &t.Source, &t.Identity); err != nil {
|
||||
&t.Plan, &t.Source, &t.Identity, &t.BindingID, &t.BindingVersion); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tasks = append(tasks, t)
|
||||
|
||||
@@ -5,8 +5,10 @@ package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
"github.com/samcharles93/archie-core/internal/domain/mapping"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
)
|
||||
@@ -22,6 +24,9 @@ type TaskStore interface {
|
||||
}
|
||||
|
||||
// TaskLifecycle manages the core task state machine and enqueuing.
|
||||
// Binding-triggered task creation lives on BindingTaskCreator, not here,
|
||||
// so the lifecycle surface stays narrow and non-binding consumers (forge
|
||||
// poll, chat spawn, drain loop) do not acquire a binding-specific shape.
|
||||
type TaskLifecycle interface {
|
||||
EnqueueIssue(ctx context.Context, owner, repo string, number int, title, body, labels, identity string) (bool, error)
|
||||
EnqueueChatTask(ctx context.Context, owner, repo, title, body, workflow, identity string) (*Task, error)
|
||||
@@ -100,6 +105,48 @@ type MappingStore interface {
|
||||
DeleteMapping(ctx context.Context, id int64) error
|
||||
}
|
||||
|
||||
// BindingStore persists playbook bindings (t2db.4 Phase B): CRUD and the
|
||||
// draft -> pending_approval -> armed state machine. Split off from
|
||||
// TaskStore and MappingStore so the webui binding editor does not acquire
|
||||
// the full task or mapping surfaces. Dispatch-time helpers live in
|
||||
// BindingDispatcher; the daemon depends on both. See
|
||||
// docs/prds/playbook-binding.md.
|
||||
type BindingStore interface {
|
||||
InsertBinding(ctx context.Context, b binding.Binding) (int64, error)
|
||||
GetBinding(ctx context.Context, id int64) (*binding.Binding, error)
|
||||
ListBindings(ctx context.Context) ([]binding.Binding, error)
|
||||
UpdateBinding(ctx context.Context, b binding.Binding) error
|
||||
DeleteBinding(ctx context.Context, id int64) error
|
||||
ApproveBinding(ctx context.Context, id int64) error
|
||||
}
|
||||
|
||||
// BindingDispatcher is the dispatch-loop surface over the bindings store:
|
||||
// look up armed bindings for HMAC verification, list captures that still
|
||||
// need dispatching, and record the at-most-once ledger row. The
|
||||
// dispatcher's task-creation call (EnqueueBindingTask) lives on a separate
|
||||
// BindingTaskCreator interface so the dispatcher does not depend on the
|
||||
// full TaskStore just to spawn one task.
|
||||
type BindingDispatcher interface {
|
||||
ArmedBindingsForSource(ctx context.Context, source string) ([]binding.Binding, error)
|
||||
RecordDispatch(
|
||||
ctx context.Context,
|
||||
tx *sql.Tx,
|
||||
bindingID int64,
|
||||
bindingVersion int64,
|
||||
captureID int64,
|
||||
taskID int64,
|
||||
) error
|
||||
ListUndispatchedCaptures(ctx context.Context, sources []string, limit int) ([]CapturedEvent, error)
|
||||
}
|
||||
|
||||
// BindingTaskCreator is the single-method consumer-facing surface for
|
||||
// enqueueing a task triggered by a binding. Splitting it off TaskLifecycle
|
||||
// keeps the lifecycle surface narrow (8 methods, the interfacebloat limit)
|
||||
// and keeps the binding-specific shape on the binding interfaces.
|
||||
type BindingTaskCreator interface {
|
||||
EnqueueBindingTask(ctx context.Context, owner, repo, title, body, workflow, identity string, bindingID int64, bindingVersion int) (*Task, error)
|
||||
}
|
||||
|
||||
// Compile-time check: *Store satisfies TaskStore.
|
||||
var _ TaskStore = (*Store)(nil)
|
||||
|
||||
@@ -111,3 +158,12 @@ var _ CaptureStore = (*Store)(nil)
|
||||
|
||||
// Compile-time check: *Store satisfies MappingStore.
|
||||
var _ MappingStore = (*Store)(nil)
|
||||
|
||||
// Compile-time check: *Store satisfies BindingStore.
|
||||
var _ BindingStore = (*Store)(nil)
|
||||
|
||||
// Compile-time check: *Store satisfies BindingDispatcher.
|
||||
var _ BindingDispatcher = (*Store)(nil)
|
||||
|
||||
// Compile-time check: *Store satisfies BindingTaskCreator.
|
||||
var _ BindingTaskCreator = (*Store)(nil)
|
||||
+53
-8
@@ -73,6 +73,12 @@ type Task struct {
|
||||
// deployments). Used to scope /approve and /cancel authorization so
|
||||
// one identity cannot control another's chat-spawned tasks.
|
||||
Identity string `json:"identity"`
|
||||
// BindingID and BindingVersion are stamped when a task was created
|
||||
// from a playbook binding dispatch (t2db.4 Phase B). They record
|
||||
// provenance: which binding fired this task and at what version, so
|
||||
// later edits to the binding cannot silently rewrite history.
|
||||
BindingID int64 `json:"binding_id"`
|
||||
BindingVersion int `json:"binding_version"`
|
||||
// CreatedAt and UpdatedAt are the SQLite row timestamps, exposed so
|
||||
// callers can show a task's age and last activity. They are written by
|
||||
// column defaults and the UPDATE statements, never by the caller.
|
||||
@@ -97,7 +103,7 @@ func (t Task) IsForgeBacked() bool {
|
||||
|
||||
type Store struct{ db *sql.DB }
|
||||
|
||||
const taskSchemaVersion = 1
|
||||
const taskSchemaVersion = 2
|
||||
|
||||
// Open opens (creating if needed) the SQLite database and its schema.
|
||||
func Open(ctx context.Context, path string) (*Store, error) {
|
||||
@@ -109,7 +115,7 @@ func Open(ctx context.Context, path string) (*Store, error) {
|
||||
return nil, err
|
||||
}
|
||||
db.SetMaxOpenConns(1)
|
||||
if _, err := db.ExecContext(ctx, schema+eventsSchema+capturesSchema+mappingsSchema); err != nil {
|
||||
if _, err := db.ExecContext(ctx, schema+eventsSchema+capturesSchema+mappingsSchema+bindingsSchema); err != nil {
|
||||
return nil, errors.Join(fmt.Errorf("store: init schema: %w", err), db.Close())
|
||||
}
|
||||
if err := migrateTasks(ctx, db); err != nil {
|
||||
@@ -159,6 +165,8 @@ func migrateTasks(ctx context.Context, db *sql.DB) error {
|
||||
{"retry_count", `ALTER TABLE tasks ADD COLUMN retry_count INTEGER NOT NULL DEFAULT 0`},
|
||||
{"source", `ALTER TABLE tasks ADD COLUMN source TEXT NOT NULL DEFAULT 'forge'`},
|
||||
{"identity", `ALTER TABLE tasks ADD COLUMN identity TEXT NOT NULL DEFAULT ''`},
|
||||
{"binding_id", `ALTER TABLE tasks ADD COLUMN binding_id INTEGER NOT NULL DEFAULT 0`},
|
||||
{"binding_version", `ALTER TABLE tasks ADD COLUMN binding_version INTEGER NOT NULL DEFAULT 0`},
|
||||
}
|
||||
for _, migration := range migrations {
|
||||
if columns[migration.column] {
|
||||
@@ -168,7 +176,7 @@ func migrateTasks(ctx context.Context, db *sql.DB) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `PRAGMA user_version = 1`); err != nil {
|
||||
if _, err := tx.ExecContext(ctx, `PRAGMA user_version = 2`); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
@@ -217,6 +225,8 @@ CREATE TABLE IF NOT EXISTS tasks (
|
||||
retry_count INTEGER NOT NULL DEFAULT 0,
|
||||
source TEXT NOT NULL DEFAULT 'forge',
|
||||
identity TEXT NOT NULL DEFAULT '',
|
||||
binding_id INTEGER NOT NULL DEFAULT 0,
|
||||
binding_version INTEGER NOT NULL DEFAULT 0,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
UNIQUE(owner, repo, issue_number)
|
||||
@@ -233,6 +243,13 @@ CREATE TABLE IF NOT EXISTS transitions (
|
||||
|
||||
func (s *Store) Close() error { return s.db.Close() }
|
||||
|
||||
// DB exposes the underlying *sql.DB so tests outside the store
|
||||
// package can drive fixtures that the public surface does not (yet)
|
||||
// cover -- e.g. advancing a binding draft -> pending_approval without
|
||||
// going through the operator edit flow. Production callers must NOT
|
||||
// use this; the structured methods above are the contract.
|
||||
func (s *Store) DB() *sql.DB { return s.db }
|
||||
|
||||
// OpenTest opens an in-memory SQLite store suitable for tests.
|
||||
func OpenTest(t interface {
|
||||
TempDir() string
|
||||
@@ -291,11 +308,38 @@ func (s *Store) EnqueueChatTask(ctx context.Context, owner, repo, title, body, w
|
||||
RETURNING id, owner, repo, issue_number, title, body, labels, status,
|
||||
workflow, stage, branch, plan, notes, pr_number, tokens_used,
|
||||
iterations, attempt, park_reason, watch_comment_id, retry_count,
|
||||
source, identity, created_at, updated_at`,
|
||||
source, identity, binding_id, binding_version, created_at, updated_at`,
|
||||
owner, repo, owner, repo, syntheticIssueNumberBase-1, title, body, workflow, identity)
|
||||
return scanTask(row)
|
||||
}
|
||||
|
||||
// EnqueueBindingTask enqueues a task triggered by a playbook binding
|
||||
// dispatch (t2db.4 Phase B). It builds the row through EnqueueChatTask
|
||||
// (so the synthetic issue-number allocator stays the single source of
|
||||
// truth for chat-sourced tasks) and then stamps binding_id and
|
||||
// binding_version on the new row in a second statement.
|
||||
//
|
||||
// A crash between the two writes leaves the task without provenance
|
||||
// (binding_id == 0); the binding_dispatches ledger row written by the
|
||||
// dispatch loop still records the (binding, capture, task) triple, so
|
||||
// a future repair pass could backfill. This is the same
|
||||
// best-effort-provenance pattern the rest of the task lifecycle uses
|
||||
// for fields added after the row's primary insert.
|
||||
func (s *Store) EnqueueBindingTask(ctx context.Context, owner, repo, title, body, workflow, identity string, bindingID int64, bindingVersion int) (*Task, error) {
|
||||
t, err := s.EnqueueChatTask(ctx, owner, repo, title, body, workflow, identity)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := s.db.ExecContext(ctx,
|
||||
`UPDATE tasks SET binding_id=?, binding_version=? WHERE id=?`,
|
||||
bindingID, bindingVersion, t.ID); err != nil {
|
||||
return nil, fmt.Errorf("store: stamp binding provenance: %w", err)
|
||||
}
|
||||
t.BindingID = bindingID
|
||||
t.BindingVersion = bindingVersion
|
||||
return t, nil
|
||||
}
|
||||
|
||||
// ClaimNext atomically moves the oldest queued task to running and
|
||||
// returns it; nil when the queue is empty.
|
||||
func (s *Store) ClaimNext(ctx context.Context) (*Task, error) {
|
||||
@@ -305,7 +349,7 @@ func (s *Store) ClaimNext(ctx context.Context) (*Task, error) {
|
||||
RETURNING id, owner, repo, issue_number, title, body, labels, status,
|
||||
workflow, stage, branch, plan, notes, pr_number, tokens_used,
|
||||
iterations, attempt, park_reason, watch_comment_id, retry_count,
|
||||
source, identity, created_at, updated_at`)
|
||||
source, identity, binding_id, binding_version, created_at, updated_at`)
|
||||
t, err := scanTask(row)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, nil
|
||||
@@ -319,6 +363,7 @@ func scanTask(row *sql.Row) (*Task, error) {
|
||||
&t.Labels, &t.Status, &t.Workflow, &t.Stage, &t.Branch, &t.Plan, &t.Notes,
|
||||
&t.PRNumber, &t.TokensUsed, &t.Iterations, &t.Attempt, &t.ParkReason,
|
||||
&t.WatchCommentID, &t.RetryCount, &t.Source, &t.Identity,
|
||||
&t.BindingID, &t.BindingVersion,
|
||||
sqliteTime{&t.CreatedAt}, sqliteTime{&t.UpdatedAt})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -337,7 +382,7 @@ func (s *Store) ClaimByIssue(ctx context.Context, owner, repo string, number int
|
||||
RETURNING id, owner, repo, issue_number, title, body, labels, status,
|
||||
workflow, stage, branch, plan, notes, pr_number, tokens_used,
|
||||
iterations, attempt, park_reason, watch_comment_id, retry_count,
|
||||
source, identity, created_at, updated_at`, owner, repo, number)
|
||||
source, identity, binding_id, binding_version, created_at, updated_at`, owner, repo, number)
|
||||
t, err := scanTask(row)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, nil
|
||||
@@ -409,7 +454,7 @@ func (s *Store) TaskByIssue(ctx context.Context, owner, repo string, number int)
|
||||
SELECT id, owner, repo, issue_number, title, body, labels, status,
|
||||
workflow, stage, branch, plan, notes, pr_number, tokens_used,
|
||||
iterations, attempt, park_reason, watch_comment_id, retry_count,
|
||||
source, identity, created_at, updated_at
|
||||
source, identity, binding_id, binding_version, created_at, updated_at
|
||||
FROM tasks WHERE owner=? AND repo=? AND issue_number=?`, owner, repo, number)
|
||||
t, err := scanTask(row)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
@@ -426,7 +471,7 @@ func (s *Store) TaskByID(ctx context.Context, taskID int64) (*Task, error) {
|
||||
SELECT id, owner, repo, issue_number, title, body, labels, status,
|
||||
workflow, stage, branch, plan, notes, pr_number, tokens_used,
|
||||
iterations, attempt, park_reason, watch_comment_id, retry_count,
|
||||
source, identity, created_at, updated_at
|
||||
source, identity, binding_id, binding_version, created_at, updated_at
|
||||
FROM tasks WHERE id=?`, taskID)
|
||||
t, err := scanTask(row)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
|
||||
@@ -116,7 +116,7 @@ func TestOpenRejectsNewerSchemaVersion(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := db.ExecContext(t.Context(), `PRAGMA user_version = 2`); err != nil {
|
||||
if _, err := db.ExecContext(t.Context(), `PRAGMA user_version = 3`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Close(); err != nil {
|
||||
@@ -313,6 +313,49 @@ func openTest(t *testing.T) *Store {
|
||||
return s
|
||||
}
|
||||
|
||||
// TestTasksBindingColumnsRoundTrip confirms the binding_id and
|
||||
// binding_version columns added in store schema 2 survive an INSERT and a
|
||||
// full SELECT round-trip through the Task read paths (scanTask,
|
||||
// TaskByIssue, TaskByID, Tasks).
|
||||
func TestTasksBindingColumnsRoundTrip(t *testing.T) {
|
||||
s := openTest(t)
|
||||
ctx := t.Context()
|
||||
task, err := s.EnqueueChatTask(ctx, "acme", "widget", "binding task", "body", "implement", "")
|
||||
if err != nil {
|
||||
t.Fatalf("EnqueueChatTask: %v", err)
|
||||
}
|
||||
if _, err := s.db.ExecContext(ctx,
|
||||
`UPDATE tasks SET binding_id=?, binding_version=? WHERE id=?`,
|
||||
int64(42), 3, task.ID); err != nil {
|
||||
t.Fatalf("UPDATE binding provenance: %v", err)
|
||||
}
|
||||
|
||||
for name, read := range map[string]func() (*Task, error){
|
||||
"scanTask-via-TaskByIssue": func() (*Task, error) { return s.TaskByIssue(ctx, "acme", "widget", task.IssueNumber) },
|
||||
"TaskByID": func() (*Task, error) { return s.TaskByID(ctx, task.ID) },
|
||||
} {
|
||||
got, err := read()
|
||||
if err != nil {
|
||||
t.Fatalf("%s: %v", name, err)
|
||||
}
|
||||
if got == nil {
|
||||
t.Fatalf("%s: nil task", name)
|
||||
}
|
||||
if got.BindingID != 42 || got.BindingVersion != 3 {
|
||||
t.Fatalf("%s: BindingID=%d BindingVersion=%d, want 42/3",
|
||||
name, got.BindingID, got.BindingVersion)
|
||||
}
|
||||
}
|
||||
|
||||
listed, err := s.Tasks(ctx, 10)
|
||||
if err != nil {
|
||||
t.Fatalf("Tasks: %v", err)
|
||||
}
|
||||
if len(listed) == 0 || listed[0].BindingID != 42 || listed[0].BindingVersion != 3 {
|
||||
t.Fatalf("Tasks() round-trip: %+v", listed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClip(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
@@ -0,0 +1,226 @@
|
||||
package webui
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
"github.com/samcharles93/archie-core/internal/store"
|
||||
)
|
||||
|
||||
// bindingRequest is the wire shape POST /api/bindings and
|
||||
// PATCH /api/bindings/{id} both accept. ID/Version/Status/CreatedAt/UpdatedAt
|
||||
// are server-assigned (or server-driven), never taken from the request body.
|
||||
type bindingRequest struct {
|
||||
Name string `json:"name"`
|
||||
Matcher binding.Matcher `json:"matcher"`
|
||||
MappingID int64 `json:"mapping_id"`
|
||||
Workflow string `json:"workflow"`
|
||||
Secret string `json:"secret"`
|
||||
}
|
||||
|
||||
func (s *Server) handleBindingsList(w http.ResponseWriter, r *http.Request) {
|
||||
if s.Bindings == nil {
|
||||
http.Error(w, "bindings not configured", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
bindings, err := s.Bindings.ListBindings(r.Context())
|
||||
if err != nil {
|
||||
s.Log.Error("list bindings", "err", err)
|
||||
http.Error(w, "list bindings failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
stripBindingSecrets(bindings)
|
||||
writeJSON(w, map[string]any{"bindings": bindings})
|
||||
}
|
||||
|
||||
func (s *Server) handleBindingCreate(w http.ResponseWriter, r *http.Request) {
|
||||
if s.Bindings == nil {
|
||||
http.Error(w, "bindings not configured", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
var req bindingRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, "invalid request body: "+err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if !s.hasWorkflow(req.Workflow) {
|
||||
http.Error(w, "binding: workflow not found: "+req.Workflow, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
b := binding.Binding{
|
||||
Name: req.Name,
|
||||
Matcher: req.Matcher,
|
||||
MappingID: req.MappingID,
|
||||
Workflow: req.Workflow,
|
||||
Secret: req.Secret,
|
||||
Status: binding.Normalize(binding.StatusDraft),
|
||||
}
|
||||
if err := b.Validate(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
id, err := s.Bindings.InsertBinding(r.Context(), b)
|
||||
if err != nil {
|
||||
if errors.Is(err, store.ErrBindingOverlap) {
|
||||
http.Error(w, "binding source overlaps an existing binding", http.StatusConflict)
|
||||
return
|
||||
}
|
||||
s.Log.Error("insert binding", "err", err)
|
||||
http.Error(w, "create binding failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
created, err := s.Bindings.GetBinding(r.Context(), id)
|
||||
if err != nil || created == nil {
|
||||
s.Log.Error("get binding after insert", "err", err, "id", id)
|
||||
http.Error(w, "create binding failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
created.Secret = ""
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
writeJSON(w, created)
|
||||
}
|
||||
|
||||
func (s *Server) handleBindingGet(w http.ResponseWriter, r *http.Request) {
|
||||
if s.Bindings == nil {
|
||||
http.Error(w, "bindings not configured", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid binding id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
b, err := s.Bindings.GetBinding(r.Context(), id)
|
||||
if err != nil {
|
||||
s.Log.Error("get binding", "err", err, "id", id)
|
||||
http.Error(w, "get binding failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if b == nil {
|
||||
http.Error(w, "binding not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
b.Secret = ""
|
||||
writeJSON(w, b)
|
||||
}
|
||||
|
||||
func (s *Server) handleBindingUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
if s.Bindings == nil {
|
||||
http.Error(w, "bindings not configured", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid binding id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
var req bindingRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, "invalid request body: "+err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if !s.hasWorkflow(req.Workflow) {
|
||||
http.Error(w, "binding: workflow not found: "+req.Workflow, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
b := binding.Binding{
|
||||
ID: id,
|
||||
Name: req.Name,
|
||||
Matcher: req.Matcher,
|
||||
MappingID: req.MappingID,
|
||||
Workflow: req.Workflow,
|
||||
Secret: req.Secret,
|
||||
}
|
||||
if err := b.Validate(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if err := s.Bindings.UpdateBinding(r.Context(), b); err != nil {
|
||||
switch {
|
||||
case errors.Is(err, store.ErrBindingNotFound):
|
||||
http.Error(w, "binding not found", http.StatusNotFound)
|
||||
case errors.Is(err, store.ErrBindingOverlap):
|
||||
http.Error(w, "binding source overlaps an existing binding", http.StatusConflict)
|
||||
default:
|
||||
s.Log.Error("update binding", "err", err, "id", id)
|
||||
http.Error(w, "update binding failed", http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
updated, err := s.Bindings.GetBinding(r.Context(), id)
|
||||
if err != nil || updated == nil {
|
||||
s.Log.Error("get binding after update", "err", err, "id", id)
|
||||
http.Error(w, "update binding failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
updated.Secret = ""
|
||||
writeJSON(w, updated)
|
||||
}
|
||||
|
||||
func (s *Server) handleBindingDelete(w http.ResponseWriter, r *http.Request) {
|
||||
if s.Bindings == nil {
|
||||
http.Error(w, "bindings not configured", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid binding id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if err := s.Bindings.DeleteBinding(r.Context(), id); err != nil {
|
||||
if errors.Is(err, store.ErrBindingNotFound) {
|
||||
http.Error(w, "binding not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
s.Log.Error("delete binding", "err", err, "id", id)
|
||||
http.Error(w, "delete binding failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (s *Server) handleBindingApprove(w http.ResponseWriter, r *http.Request) {
|
||||
if s.Bindings == nil {
|
||||
http.Error(w, "bindings not configured", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid binding id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if err := s.Bindings.ApproveBinding(r.Context(), id); err != nil {
|
||||
switch {
|
||||
case errors.Is(err, store.ErrBindingNotFound):
|
||||
http.Error(w, "binding not found", http.StatusNotFound)
|
||||
case errors.Is(err, store.ErrBindingTransition):
|
||||
http.Error(w, "binding cannot be approved from its current state", http.StatusConflict)
|
||||
case errors.Is(err, store.ErrBindingOverlap):
|
||||
http.Error(w, "binding source overlaps an existing binding", http.StatusConflict)
|
||||
default:
|
||||
s.Log.Error("approve binding", "err", err, "id", id)
|
||||
http.Error(w, "approve binding failed", http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
updated, err := s.Bindings.GetBinding(r.Context(), id)
|
||||
if err != nil || updated == nil {
|
||||
s.Log.Error("get binding after approve", "err", err, "id", id)
|
||||
http.Error(w, "approve binding failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
updated.Secret = ""
|
||||
writeJSON(w, updated)
|
||||
}
|
||||
|
||||
// stripBindingSecrets zeroes the Secret field on every binding in the slice
|
||||
// so the list handler never leaks a shared HMAC secret through /api/bindings.
|
||||
// Single-binding reads do the same inline; this covers the list path.
|
||||
func stripBindingSecrets(bs []binding.Binding) {
|
||||
for i := range bs {
|
||||
bs[i].Secret = ""
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,331 @@
|
||||
package webui
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
"github.com/samcharles93/archie-core/internal/domain/mapping"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/store"
|
||||
)
|
||||
|
||||
// bindingTestServer wires a Server backed by a single *store.Store for all
|
||||
// three dashboards surfaces (Captures, Mappings, Bindings) and seeds the
|
||||
// one workflow binding tests need to pass server-side validation.
|
||||
func bindingTestServer(t *testing.T) *Server {
|
||||
t.Helper()
|
||||
s, err := store.Open(t.Context(), filepath.Join(t.TempDir(), "test.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = s.Close() })
|
||||
return &Server{
|
||||
Store: s,
|
||||
Log: slog.New(slog.DiscardHandler),
|
||||
Mappings: s,
|
||||
Captures: s,
|
||||
Bindings: s,
|
||||
Workflows: []workflow.Definition{{ID: "implement", Name: "implement", Enabled: true}},
|
||||
}
|
||||
}
|
||||
|
||||
// seedMapping inserts one payload mapping and returns its row ID, so
|
||||
// binding tests have a valid non-zero mapping_id without hand-rolling SQL.
|
||||
func seedMapping(t *testing.T, srv *Server, name string) int64 {
|
||||
t.Helper()
|
||||
id, err := srv.Mappings.InsertMapping(t.Context(), mapping.Mapping{
|
||||
Name: name,
|
||||
SourceHint: "sentry",
|
||||
Fields: []mapping.Field{{Name: "title", Path: "issue.title", Type: mapping.TypeString, Required: true}},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("seed mapping: %v", err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
// validBindingRequest returns a populated bindingRequest that passes
|
||||
// binding.Validate and server-side workflow validation. The Secret is 32
|
||||
// bytes (above the 16-byte floor) so length never accidentally trips
|
||||
// validation in a test that isn't about secret length.
|
||||
func validBindingRequest(suffix, source string, mappingID int64) map[string]any {
|
||||
return map[string]any{
|
||||
"name": "binding " + suffix,
|
||||
"matcher": map[string]any{"source": source},
|
||||
"mapping_id": mappingID,
|
||||
"workflow": "implement",
|
||||
"secret": "0123456789abcdef0123456789abcdef",
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleBindingCreateAndGet(t *testing.T) {
|
||||
srv := bindingTestServer(t)
|
||||
mappingID := seedMapping(t, srv, "m")
|
||||
|
||||
w := doJSON(t, srv, http.MethodPost, "/api/bindings", validBindingRequest("a", "sentry", mappingID))
|
||||
if w.Code != http.StatusCreated {
|
||||
t.Fatalf("create status = %d, want %d; body = %s", w.Code, http.StatusCreated, w.Body.String())
|
||||
}
|
||||
var created binding.Binding
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &created); err != nil {
|
||||
t.Fatalf("unmarshal created: %v", err)
|
||||
}
|
||||
if created.ID == 0 {
|
||||
t.Fatalf("created.ID = 0; body = %s", w.Body.String())
|
||||
}
|
||||
if created.Status != binding.StatusDraft {
|
||||
t.Fatalf("created.Status = %q, want %q", created.Status, binding.StatusDraft)
|
||||
}
|
||||
if created.Secret != "" {
|
||||
t.Fatalf("created.Secret = %q, want \"\" (response must strip secret)", created.Secret)
|
||||
}
|
||||
|
||||
w = doJSON(t, srv, http.MethodGet, "/api/bindings/"+strconv.FormatInt(created.ID, 10), nil)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("get status = %d, want %d; body = %s", w.Code, http.StatusOK, w.Body.String())
|
||||
}
|
||||
var got binding.Binding
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
|
||||
t.Fatalf("unmarshal got: %v", err)
|
||||
}
|
||||
if got.ID != created.ID {
|
||||
t.Fatalf("got.ID = %d, want %d", got.ID, created.ID)
|
||||
}
|
||||
if got.Secret != "" {
|
||||
t.Fatalf("get.Secret = %q, want \"\"", got.Secret)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleBindingsListStripsSecrets(t *testing.T) {
|
||||
srv := bindingTestServer(t)
|
||||
mappingID := seedMapping(t, srv, "m")
|
||||
doJSON(t, srv, http.MethodPost, "/api/bindings", validBindingRequest("a", "sentry", mappingID))
|
||||
|
||||
w := doJSON(t, srv, http.MethodGet, "/api/bindings", nil)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("list status = %d, want %d; body = %s", w.Code, http.StatusOK, w.Body.String())
|
||||
}
|
||||
var body struct {
|
||||
Bindings []binding.Binding `json:"bindings"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if len(body.Bindings) != 1 {
|
||||
t.Fatalf("bindings = %d, want 1", len(body.Bindings))
|
||||
}
|
||||
if body.Bindings[0].Secret != "" {
|
||||
t.Fatalf("listed Secret = %q, want \"\"", body.Bindings[0].Secret)
|
||||
}
|
||||
// Defence against accidental echo in the raw JSON: the "secret" key
|
||||
// must not appear at all (the binding.Binding tag is omitempty).
|
||||
if strings.Contains(w.Body.String(), `"secret"`) {
|
||||
t.Fatalf("list response leaked secret key: %s", w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleBindingCreateRejectsOverlap(t *testing.T) {
|
||||
srv := bindingTestServer(t)
|
||||
mappingID := seedMapping(t, srv, "m")
|
||||
|
||||
w := doJSON(t, srv, http.MethodPost, "/api/bindings", validBindingRequest("first", "sentry", mappingID))
|
||||
if w.Code != http.StatusCreated {
|
||||
t.Fatalf("first create status = %d, want %d; body = %s", w.Code, http.StatusCreated, w.Body.String())
|
||||
}
|
||||
|
||||
// Overlap because Per bead t2db.4, two bindings may not own the same
|
||||
// source; the second insert must surface ErrBindingOverlap -> 409.
|
||||
w = doJSON(t, srv, http.MethodPost, "/api/bindings", validBindingRequest("second", "sentry", mappingID))
|
||||
if w.Code != http.StatusConflict {
|
||||
t.Fatalf("second create status = %d, want %d; body = %s", w.Code, http.StatusConflict, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleBindingCreateRejectsMissingWorkflow(t *testing.T) {
|
||||
srv := bindingTestServer(t)
|
||||
mappingID := seedMapping(t, srv, "m")
|
||||
|
||||
req := validBindingRequest("a", "sentry", mappingID)
|
||||
req["workflow"] = "no-such-workflow"
|
||||
w := doJSON(t, srv, http.MethodPost, "/api/bindings", req)
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want %d; body = %s", w.Code, http.StatusBadRequest, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleBindingCreateRejectsValidationFailure(t *testing.T) {
|
||||
srv := bindingTestServer(t)
|
||||
mappingID := seedMapping(t, srv, "m")
|
||||
|
||||
// Short secret trips binding.Validate's length floor regardless of
|
||||
// any store-side check.
|
||||
req := validBindingRequest("a", "sentry", mappingID)
|
||||
req["secret"] = "short"
|
||||
w := doJSON(t, srv, http.MethodPost, "/api/bindings", req)
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want %d; body = %s", w.Code, http.StatusBadRequest, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleBindingCreateRequiresToken(t *testing.T) {
|
||||
srv := bindingTestServer(t)
|
||||
srv.Token = "secret"
|
||||
mappingID := seedMapping(t, srv, "m")
|
||||
|
||||
// httptest.NewRequest defaults to no Authorization header. With
|
||||
// Token set, requireToken must reject the request before any handler
|
||||
// body decode happens, so the response is 401, not 400.
|
||||
w := doJSON(t, srv, http.MethodPost, "/api/bindings", validBindingRequest("a", "sentry", mappingID))
|
||||
if w.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("status = %d, want %d; body = %s", w.Code, http.StatusUnauthorized, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleBindingUpdatePreservesOtherFields(t *testing.T) {
|
||||
srv := bindingTestServer(t)
|
||||
mappingID := seedMapping(t, srv, "m")
|
||||
w := doJSON(t, srv, http.MethodPost, "/api/bindings", validBindingRequest("a", "sentry", mappingID))
|
||||
var created binding.Binding
|
||||
_ = json.Unmarshal(w.Body.Bytes(), &created)
|
||||
|
||||
// PATCH on a draft binding should update the editable fields and
|
||||
// leave status=draft in place. Phase B's UpdateBinding currently
|
||||
// only forces armed -> pending_approval; with no path from draft to
|
||||
// pending_approval yet, an edit on a draft row correctly stays
|
||||
// draft. This test pins the editable-fields half of the contract.
|
||||
update := validBindingRequest("renamed", "sentry", mappingID)
|
||||
w = doJSON(t, srv, http.MethodPatch, "/api/bindings/"+strconv.FormatInt(created.ID, 10), update)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("patch status = %d, want %d; body = %s", w.Code, http.StatusOK, w.Body.String())
|
||||
}
|
||||
var updated binding.Binding
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &updated); err != nil {
|
||||
t.Fatalf("unmarshal updated: %v", err)
|
||||
}
|
||||
if updated.Name != "binding renamed" {
|
||||
t.Fatalf("updated.Name = %q, want %q", updated.Name, "binding renamed")
|
||||
}
|
||||
if updated.MappingID != mappingID {
|
||||
t.Fatalf("updated.MappingID = %d, want %d", updated.MappingID, mappingID)
|
||||
}
|
||||
// Status MUST not regress from a state the caller never asked for.
|
||||
if updated.Status != binding.StatusDraft && updated.Status != binding.StatusPendingApproval {
|
||||
t.Fatalf("updated.Status = %q, want draft or pending_approval", updated.Status)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleBindingApproveFromDraftRejected(t *testing.T) {
|
||||
srv := bindingTestServer(t)
|
||||
mappingID := seedMapping(t, srv, "m")
|
||||
w := doJSON(t, srv, http.MethodPost, "/api/bindings", validBindingRequest("a", "sentry", mappingID))
|
||||
var created binding.Binding
|
||||
_ = json.Unmarshal(w.Body.Bytes(), &created)
|
||||
|
||||
// draft is NOT a valid source state for approve; the binding must
|
||||
// first land in pending_approval (via PATCH). Approve from draft
|
||||
// returns ErrBindingTransition -> 409.
|
||||
w = doJSON(t, srv, http.MethodPost, "/api/bindings/"+strconv.FormatInt(created.ID, 10)+"/approve", nil)
|
||||
if w.Code != http.StatusConflict {
|
||||
t.Fatalf("status = %d, want %d; body = %s", w.Code, http.StatusConflict, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleBindingApproveHappyPath(t *testing.T) {
|
||||
// Per docs/prds/webhook-intake-security.md point 2, ANY edit drops status
|
||||
// to pending_approval. The happy path: POST creates draft, PATCH moves
|
||||
// it to pending_approval, POST /approve moves it to armed. This test
|
||||
// pins the rule end-to-end through the webui handlers.
|
||||
srv := bindingTestServer(t)
|
||||
mappingID := seedMapping(t, srv, "m")
|
||||
w := doJSON(t, srv, http.MethodPost, "/api/bindings", validBindingRequest("a", "sentry", mappingID))
|
||||
var created binding.Binding
|
||||
_ = json.Unmarshal(w.Body.Bytes(), &created)
|
||||
if created.Status != binding.StatusDraft {
|
||||
t.Fatalf("created status = %q, want %q", created.Status, binding.StatusDraft)
|
||||
}
|
||||
|
||||
w = doJSON(t, srv, http.MethodPatch, "/api/bindings/"+strconv.FormatInt(created.ID, 10), validBindingRequest("renamed", "sentry", mappingID))
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("patch status = %d, want %d; body = %s", w.Code, http.StatusOK, w.Body.String())
|
||||
}
|
||||
var patched binding.Binding
|
||||
_ = json.Unmarshal(w.Body.Bytes(), &patched)
|
||||
if patched.Status != binding.StatusPendingApproval {
|
||||
t.Fatalf("patched status = %q, want %q", patched.Status, binding.StatusPendingApproval)
|
||||
}
|
||||
|
||||
w = doJSON(t, srv, http.MethodPost, "/api/bindings/"+strconv.FormatInt(created.ID, 10)+"/approve", nil)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("approve status = %d, want %d; body = %s", w.Code, http.StatusOK, w.Body.String())
|
||||
}
|
||||
var approved binding.Binding
|
||||
_ = json.Unmarshal(w.Body.Bytes(), &approved)
|
||||
if approved.Status != binding.StatusArmed {
|
||||
t.Fatalf("approved status = %q, want %q", approved.Status, binding.StatusArmed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleBindingEditOnArmedDropsToPendingApproval(t *testing.T) {
|
||||
// Pin the other half of the rule: an edit on an already-armed binding
|
||||
// drops it back to pending_approval (not silently re-armed). This is
|
||||
// the security property that makes the approval gate non-decorative.
|
||||
srv := bindingTestServer(t)
|
||||
mappingID := seedMapping(t, srv, "m")
|
||||
w := doJSON(t, srv, http.MethodPost, "/api/bindings", validBindingRequest("a", "sentry", mappingID))
|
||||
var created binding.Binding
|
||||
_ = json.Unmarshal(w.Body.Bytes(), &created)
|
||||
|
||||
// Drive it to armed via the happy path.
|
||||
_ = doJSON(t, srv, http.MethodPatch, "/api/bindings/"+strconv.FormatInt(created.ID, 10), validBindingRequest("a", "sentry", mappingID))
|
||||
_ = doJSON(t, srv, http.MethodPost, "/api/bindings/"+strconv.FormatInt(created.ID, 10)+"/approve", nil)
|
||||
|
||||
// Now edit. Status must drop back to pending_approval.
|
||||
w = doJSON(t, srv, http.MethodPatch, "/api/bindings/"+strconv.FormatInt(created.ID, 10), validBindingRequest("a-edited", "sentry", mappingID))
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("patch status = %d, want %d; body = %s", w.Code, http.StatusOK, w.Body.String())
|
||||
}
|
||||
var patched binding.Binding
|
||||
_ = json.Unmarshal(w.Body.Bytes(), &patched)
|
||||
if patched.Status != binding.StatusPendingApproval {
|
||||
t.Fatalf("after edit, status = %q, want %q", patched.Status, binding.StatusPendingApproval)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleBindingApproveMissingReturns404(t *testing.T) {
|
||||
srv := bindingTestServer(t)
|
||||
w := doJSON(t, srv, http.MethodPost, "/api/bindings/999/approve", nil)
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Fatalf("status = %d, want %d; body = %s", w.Code, http.StatusNotFound, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleBindingDeleteRemovesRow(t *testing.T) {
|
||||
srv := bindingTestServer(t)
|
||||
mappingID := seedMapping(t, srv, "m")
|
||||
w := doJSON(t, srv, http.MethodPost, "/api/bindings", validBindingRequest("a", "sentry", mappingID))
|
||||
var created binding.Binding
|
||||
_ = json.Unmarshal(w.Body.Bytes(), &created)
|
||||
|
||||
w = doJSON(t, srv, http.MethodDelete, "/api/bindings/"+strconv.FormatInt(created.ID, 10), nil)
|
||||
if w.Code != http.StatusNoContent {
|
||||
t.Fatalf("delete status = %d, want %d; body = %s", w.Code, http.StatusNoContent, w.Body.String())
|
||||
}
|
||||
w = doJSON(t, srv, http.MethodGet, "/api/bindings/"+strconv.FormatInt(created.ID, 10), nil)
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Fatalf("get after delete status = %d, want %d", w.Code, http.StatusNotFound)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleBindingsUnavailableWhenNotConfigured(t *testing.T) {
|
||||
srv := &Server{Store: nil, Log: slog.New(slog.DiscardHandler)}
|
||||
w := doJSON(t, srv, http.MethodGet, "/api/bindings", nil)
|
||||
if w.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("status = %d, want %d", w.Code, http.StatusServiceUnavailable)
|
||||
}
|
||||
}
|
||||
@@ -2,12 +2,14 @@ package webui
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/store"
|
||||
"github.com/samcharles93/archie-core/internal/webhookguard"
|
||||
@@ -20,12 +22,14 @@ import (
|
||||
// "no captures exist" instead of "show the default page."
|
||||
const defaultCapturesListLimit = 100
|
||||
|
||||
// handleCapture accepts an unbound inbound webhook POST and persists it --
|
||||
// no HMAC verification (there is by design no secret registered yet for a
|
||||
// source nobody has configured), no workflow binding, just "something
|
||||
// arrived." See docs/prds/event-capture-storage.md. Mounted on the bypass
|
||||
// mux alongside /healthz: capture must accept unauthenticated senders, so it
|
||||
// cannot sit behind requireToken.
|
||||
// handleCapture accepts an inbound webhook POST and persists it. If a
|
||||
// binding is armed for this source the body must also carry a valid HMAC
|
||||
// signature; only correctly-signed events are recorded as authenticated
|
||||
// (t2db.5 point 1). An unauthenticated event is still captured, visible in
|
||||
// the inspector, and marked unauthenticated -- the dispatch loop's auth
|
||||
// check (binding.Matcher.Matches) is the actual gate against false task
|
||||
// creation. Mounted on the bypass mux alongside /healthz: capture must
|
||||
// accept unauthenticated senders, so it cannot sit behind requireToken.
|
||||
func (s *Server) handleCapture(w http.ResponseWriter, r *http.Request) {
|
||||
if s.Captures == nil {
|
||||
http.Error(w, "capture not configured", http.StatusServiceUnavailable)
|
||||
@@ -46,6 +50,34 @@ func (s *Server) handleCapture(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Resolve armed bindings for this source before reading the body. A
|
||||
// lookup error is logged and treated as "no armed binding" -- the
|
||||
// dispatch loop's auth check is the actual gate, so failing closed here
|
||||
// would amplify a transient store hiccup into a capture outage for
|
||||
// every senders-that-haven't-been-bound-yet flow. If a future phase
|
||||
// wants fail-closed semantics, that's a separate decision and should
|
||||
// not silently land here.
|
||||
var armed []binding.Binding
|
||||
if s.BindingDispatcher != nil {
|
||||
var err error
|
||||
armed, err = s.BindingDispatcher.ArmedBindingsForSource(r.Context(), source)
|
||||
if err != nil {
|
||||
s.Log.Warn("armed bindings lookup", "source", source, "err", err)
|
||||
armed = nil
|
||||
}
|
||||
}
|
||||
// Two armed bindings for one source would race over every inbound
|
||||
// webhook for that source: the write-time overlap check should make
|
||||
// this state impossible, but the dispatch loop needs a single binding
|
||||
// per capture so we surface the overlap as 409 at capture time
|
||||
// (belt-and-braces TOCTOU guard -- see bindings.go's ApproveBinding).
|
||||
if len(armed) > 1 {
|
||||
http.Error(w,
|
||||
fmt.Sprintf("multiple armed bindings for source %q -- overlap rejected", source),
|
||||
http.StatusConflict)
|
||||
return
|
||||
}
|
||||
|
||||
r.Body = http.MaxBytesReader(w, r.Body, captureMaxBodyBytesOrFallback(s.CaptureMaxBodyBytes))
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
@@ -55,6 +87,21 @@ func (s *Server) handleCapture(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// HMAC verification -- only when exactly one binding is armed for this
|
||||
// source. Mirrors channels/webhook/webhook.go's header precedence:
|
||||
// GitHub-style X-Hub-Signature-256 first, X-Signature-256 fallback.
|
||||
// Empty header + non-empty secret is never valid (VerifyHMAC's own
|
||||
// guard), so an absent signature with an armed binding lands here as
|
||||
// authenticated=false rather than authenticated=true.
|
||||
authenticated := false
|
||||
if len(armed) == 1 {
|
||||
sig := r.Header.Get("X-Hub-Signature-256")
|
||||
if sig == "" {
|
||||
sig = r.Header.Get("X-Signature-256")
|
||||
}
|
||||
authenticated = webhookguard.VerifyHMAC(body, sig, armed[0].Secret)
|
||||
}
|
||||
|
||||
headers, _ := json.Marshal(r.Header)
|
||||
redactedHeaders, err := webhookguard.RedactPayload(headers)
|
||||
if err != nil {
|
||||
@@ -70,12 +117,13 @@ func (s *Server) handleCapture(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
c := store.CapturedEvent{
|
||||
ReceivedAt: time.Now().UTC(),
|
||||
Source: source,
|
||||
RemoteAddr: r.RemoteAddr,
|
||||
ContentType: r.Header.Get("Content-Type"),
|
||||
Headers: string(redactedHeaders),
|
||||
Body: string(redactedBody),
|
||||
ReceivedAt: time.Now().UTC(),
|
||||
Source: source,
|
||||
RemoteAddr: r.RemoteAddr,
|
||||
ContentType: r.Header.Get("Content-Type"),
|
||||
Headers: string(redactedHeaders),
|
||||
Body: string(redactedBody),
|
||||
Authenticated: authenticated,
|
||||
}
|
||||
id, err := s.Captures.InsertCapture(r.Context(), c, s.CaptureRetention, s.CaptureMaxEvents)
|
||||
if err != nil {
|
||||
|
||||
@@ -2,6 +2,10 @@ package webui
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log/slog"
|
||||
@@ -12,6 +16,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/store"
|
||||
"github.com/samcharles93/archie-core/internal/webhookguard"
|
||||
@@ -50,6 +55,101 @@ func captureTestServer(t *testing.T) *Server {
|
||||
}
|
||||
}
|
||||
|
||||
// stubBindingStore injects a configurable armed-binding lookup result so
|
||||
// handleCapture tests can exercise the single-binding HMAC path and the
|
||||
// belt-and-braces 409-on-multiple path without seeding the real store
|
||||
// (which would either run into the overlap-rejection guard or require
|
||||
// bypassing the draft -> pending_approval -> armed lifecycle). The 409
|
||||
// path exists exactly for the case where the write-time check was
|
||||
// bypassed somehow, so simulating that case directly is the most honest
|
||||
// way to cover it.
|
||||
type stubBindingStore struct {
|
||||
armedBySource map[string][]binding.Binding
|
||||
lookupErr error
|
||||
}
|
||||
|
||||
func (s *stubBindingStore) ArmedBindingsForSource(_ context.Context, source string) ([]binding.Binding, error) {
|
||||
if s.lookupErr != nil {
|
||||
return nil, s.lookupErr
|
||||
}
|
||||
return s.armedBySource[source], nil
|
||||
}
|
||||
|
||||
// handleCapture only touches ArmedBindingsForSource on the binding
|
||||
// surface. The rest of BindingStore is stubbed so the type satisfies the
|
||||
// interface and Phase C's other handlers can be exercised elsewhere.
|
||||
func (s *stubBindingStore) InsertBinding(_ context.Context, _ binding.Binding) (int64, error) {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (s *stubBindingStore) GetBinding(_ context.Context, _ int64) (*binding.Binding, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *stubBindingStore) ListBindings(_ context.Context) ([]binding.Binding, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *stubBindingStore) UpdateBinding(_ context.Context, _ binding.Binding) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *stubBindingStore) DeleteBinding(_ context.Context, _ int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *stubBindingStore) ApproveBinding(_ context.Context, _ int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *stubBindingStore) RecordDispatch(
|
||||
_ context.Context,
|
||||
_ *sql.Tx,
|
||||
_ int64,
|
||||
_ int64,
|
||||
_ int64,
|
||||
_ int64,
|
||||
) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *stubBindingStore) ListUndispatchedCaptures(_ context.Context, _ []string, _ int) ([]store.CapturedEvent, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// captureBindingServer builds a Server with both a real CaptureStore (so
|
||||
// ListCaptures can read back what handleCapture persisted) and a stub
|
||||
// BindingDispatcher configured with one armed binding for the given
|
||||
// source. The mapping/workflow fields are placeholders -- handleCapture
|
||||
// never reads them, only the Secret.
|
||||
func captureBindingServer(t *testing.T, secret, source string) *Server {
|
||||
t.Helper()
|
||||
srv := captureTestServer(t)
|
||||
srv.BindingDispatcher = &stubBindingStore{
|
||||
armedBySource: map[string][]binding.Binding{
|
||||
source: {{
|
||||
ID: 1,
|
||||
Name: "test",
|
||||
Matcher: binding.Matcher{Source: source},
|
||||
MappingID: 1,
|
||||
Workflow: "implement",
|
||||
Version: 1,
|
||||
Status: binding.StatusArmed,
|
||||
Secret: secret,
|
||||
}},
|
||||
},
|
||||
}
|
||||
return srv
|
||||
}
|
||||
|
||||
// hmacSHA256 returns the GitHub-style "sha256=<hex>" signature for body
|
||||
// under secret, the same form webhookguard.VerifyHMAC accepts.
|
||||
func hmacSHA256(secret, body string) string {
|
||||
mac := hmac.New(sha256.New, []byte(secret))
|
||||
mac.Write([]byte(body))
|
||||
return "sha256=" + hex.EncodeToString(mac.Sum(nil))
|
||||
}
|
||||
|
||||
func TestHandleCaptureStoresRedactedBody(t *testing.T) {
|
||||
srv := captureTestServer(t)
|
||||
body := `{"action":"opened","sender":{"token":"shh-secret"}}`
|
||||
@@ -451,3 +551,231 @@ func TestHandleCapturesRequiresToken(t *testing.T) {
|
||||
t.Fatalf("status = %d, want captures to require the dashboard token like every other /api/* route", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleCaptureMarksAuthenticatedWhenHMACMatches is the green half of
|
||||
// the t2db.5 point 1 wiring: a capture POST signed with the armed
|
||||
// binding's secret must be recorded with Authenticated=true so the
|
||||
// dispatch loop's binding.Matcher.Matches check (which gates on
|
||||
// authenticated) can fire.
|
||||
func TestHandleCaptureMarksAuthenticatedWhenHMACMatches(t *testing.T) {
|
||||
const secret = "abcdefghijklmnop" // exactly 16 bytes (binding.Validate floor)
|
||||
srv := captureBindingServer(t, secret, "github")
|
||||
body := `{"action":"opened"}`
|
||||
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-Hub-Signature-256", hmacSHA256(secret, body))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
srv.Handler().ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusAccepted {
|
||||
t.Fatalf("status = %d, want %d; body = %s", w.Code, http.StatusAccepted, w.Body.String())
|
||||
}
|
||||
got, err := srv.Captures.ListCaptures(t.Context(), 10)
|
||||
if err != nil {
|
||||
t.Fatalf("ListCaptures: %v", err)
|
||||
}
|
||||
if len(got) != 1 {
|
||||
t.Fatalf("captured rows = %d, want 1", len(got))
|
||||
}
|
||||
if !got[0].Authenticated {
|
||||
t.Errorf("Authenticated = false, want true (signature matched armed binding's secret)")
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleCaptureMarksUnauthenticatedWhenHMACMismatches covers the
|
||||
// negative side of the HMAC verification: a body signed with the WRONG
|
||||
// secret is still captured (per t2db.5 point 1 -- captures must remain
|
||||
// inspectable even when auth fails) but flagged as unauthenticated so
|
||||
// the dispatch loop's auth gate can drop it.
|
||||
func TestHandleCaptureMarksUnauthenticatedWhenHMACMismatches(t *testing.T) {
|
||||
const secret = "abcdefghijklmnop"
|
||||
srv := captureBindingServer(t, secret, "github")
|
||||
body := `{"action":"opened"}`
|
||||
wrong := hmacSHA256("not-the-real-secret-aaaaaaaaa", body)
|
||||
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(body))
|
||||
req.Header.Set("X-Hub-Signature-256", wrong)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
srv.Handler().ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusAccepted {
|
||||
t.Fatalf("status = %d, want %d (a mismatched signature must still produce a capture, not reject the request)", w.Code, http.StatusAccepted)
|
||||
}
|
||||
got, err := srv.Captures.ListCaptures(t.Context(), 10)
|
||||
if err != nil {
|
||||
t.Fatalf("ListCaptures: %v", err)
|
||||
}
|
||||
if len(got) != 1 {
|
||||
t.Fatalf("captured rows = %d, want 1", len(got))
|
||||
}
|
||||
if got[0].Authenticated {
|
||||
t.Errorf("Authenticated = true, want false (signature did not match armed binding's secret)")
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleCaptureWithNoSignatureMarksUnauthenticated covers the
|
||||
// "sender forgot the header" case: the binding is armed, the secret is
|
||||
// registered, but the request carries no signature at all. The handler
|
||||
// must fall through to authenticated=false (the empty-signature guard
|
||||
// in webhookguard.VerifyHMAC). Captures are still recorded -- the
|
||||
// operator can see the unsigned attempt in the inspector.
|
||||
func TestHandleCaptureWithNoSignatureMarksUnauthenticated(t *testing.T) {
|
||||
const secret = "abcdefghijklmnop"
|
||||
srv := captureBindingServer(t, secret, "github")
|
||||
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(`{}`))
|
||||
// Deliberately no X-Hub-Signature-256 / X-Signature-256 header.
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
srv.Handler().ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusAccepted {
|
||||
t.Fatalf("status = %d, want %d", w.Code, http.StatusAccepted)
|
||||
}
|
||||
got, err := srv.Captures.ListCaptures(t.Context(), 10)
|
||||
if err != nil {
|
||||
t.Fatalf("ListCaptures: %v", err)
|
||||
}
|
||||
if len(got) != 1 || got[0].Authenticated {
|
||||
t.Fatalf("captured = %+v, want 1 row with Authenticated=false", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleCaptureWithMultipleArmedBindingsReturns409 covers the
|
||||
// belt-and-braces TOCTOU guard: two armed bindings for the same source
|
||||
// would race over every inbound webhook for that source, so the
|
||||
// capture-time handler returns 409 Conflict instead of silently picking
|
||||
// a winner. The store's overlap check should make this impossible in
|
||||
// practice; the 409 is the safety net. The stub injects the state
|
||||
// directly to exercise that safety net.
|
||||
func TestHandleCaptureWithMultipleArmedBindingsReturns409(t *testing.T) {
|
||||
srv := captureTestServer(t)
|
||||
srv.BindingDispatcher = &stubBindingStore{
|
||||
armedBySource: map[string][]binding.Binding{
|
||||
"github": {
|
||||
{ID: 1, Matcher: binding.Matcher{Source: "github"}, Status: binding.StatusArmed, Secret: "abcdefghijklmnop"},
|
||||
{ID: 2, Matcher: binding.Matcher{Source: "github"}, Status: binding.StatusArmed, Secret: "qrstuvwxyz123456"},
|
||||
},
|
||||
},
|
||||
}
|
||||
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(`{}`))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
srv.Handler().ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusConflict {
|
||||
t.Fatalf("status = %d, want %d (multiple armed bindings must surface as 409 at capture time)", w.Code, http.StatusConflict)
|
||||
}
|
||||
got, err := srv.Captures.ListCaptures(t.Context(), 10)
|
||||
if err != nil {
|
||||
t.Fatalf("ListCaptures: %v", err)
|
||||
}
|
||||
if len(got) != 0 {
|
||||
t.Fatalf("captured rows = %d, want 0 (a 409 must never persist a row)", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleCaptureAcceptsWhenNoArmedBindingExists documents the
|
||||
// "source has never been bound" path: Bindings is wired but
|
||||
// ArmedBindingsForSource returns an empty slice. The handler must
|
||||
// still capture, with authenticated=false -- there is no armed binding
|
||||
// to check against, so HMAC is meaningless, but the event itself is
|
||||
// still worth recording for the inspector.
|
||||
func TestHandleCaptureAcceptsWhenNoArmedBindingExists(t *testing.T) {
|
||||
srv := captureTestServer(t)
|
||||
srv.Bindings = &stubBindingStore{armedBySource: map[string][]binding.Binding{}}
|
||||
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(`{}`))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
srv.Handler().ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusAccepted {
|
||||
t.Fatalf("status = %d, want %d", w.Code, http.StatusAccepted)
|
||||
}
|
||||
got, err := srv.Captures.ListCaptures(t.Context(), 10)
|
||||
if err != nil {
|
||||
t.Fatalf("ListCaptures: %v", err)
|
||||
}
|
||||
if len(got) != 1 || got[0].Authenticated {
|
||||
t.Fatalf("captured = %+v, want 1 row with Authenticated=false", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleCaptureAcceptsWithBindingsNil pins the legacy backwards-
|
||||
// compatibility behaviour: Bindings is nil (composition never wired the
|
||||
// binding surface, or the dashboard is running without the feature).
|
||||
// handleCapture must behave as it did before this phase landed -- still
|
||||
// capture, still record authenticated=false.
|
||||
func TestHandleCaptureAcceptsWithBindingsNil(t *testing.T) {
|
||||
srv := captureTestServer(t)
|
||||
if srv.Bindings != nil {
|
||||
t.Fatalf("captureTestServer must not wire Bindings by default for this test")
|
||||
}
|
||||
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(`{}`))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
srv.Handler().ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusAccepted {
|
||||
t.Fatalf("status = %d, want %d", w.Code, http.StatusAccepted)
|
||||
}
|
||||
got, err := srv.Captures.ListCaptures(t.Context(), 10)
|
||||
if err != nil {
|
||||
t.Fatalf("ListCaptures: %v", err)
|
||||
}
|
||||
if len(got) != 1 || got[0].Authenticated {
|
||||
t.Fatalf("captured = %+v, want 1 row with Authenticated=false", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleCaptureBindingsLookupErrorFailsOpenAtCapture pins the
|
||||
// documented fail-open posture: if ArmedBindingsForSource errors, the
|
||||
// capture still goes through (the dispatch loop's auth check is the
|
||||
// actual gate). A transient store hiccup must not amplify into a
|
||||
// capture outage for sends-that-haven't-been-bound-yet.
|
||||
func TestHandleCaptureBindingsLookupErrorFailsOpenAtCapture(t *testing.T) {
|
||||
srv := captureTestServer(t)
|
||||
srv.Bindings = &stubBindingStore{lookupErr: errors.New("db locked")}
|
||||
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(`{}`))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
srv.Handler().ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusAccepted {
|
||||
t.Fatalf("status = %d, want %d (a transient lookup error must not fail closed at capture)", w.Code, http.StatusAccepted)
|
||||
}
|
||||
got, err := srv.Captures.ListCaptures(t.Context(), 10)
|
||||
if err != nil {
|
||||
t.Fatalf("ListCaptures: %v", err)
|
||||
}
|
||||
if len(got) != 1 || got[0].Authenticated {
|
||||
t.Fatalf("captured = %+v, want 1 row with Authenticated=false", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleCaptureAcceptsXSignature256FallbackHeader pins the second
|
||||
// header in the precedence list: when X-Hub-Signature-256 is absent but
|
||||
// X-Signature-256 is present, the fallback is honoured. Mirrors
|
||||
// channels/webhook/webhook.go's header precedence.
|
||||
func TestHandleCaptureAcceptsXSignature256FallbackHeader(t *testing.T) {
|
||||
const secret = "abcdefghijklmnop"
|
||||
srv := captureBindingServer(t, secret, "github")
|
||||
body := `{"action":"opened"}`
|
||||
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(body))
|
||||
// No X-Hub-Signature-256; signature is in X-Signature-256 only.
|
||||
req.Header.Set("X-Signature-256", hmacSHA256(secret, body))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
srv.Handler().ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusAccepted {
|
||||
t.Fatalf("status = %d, want %d", w.Code, http.StatusAccepted)
|
||||
}
|
||||
got, err := srv.Captures.ListCaptures(t.Context(), 10)
|
||||
if err != nil {
|
||||
t.Fatalf("ListCaptures: %v", err)
|
||||
}
|
||||
if len(got) != 1 || !got[0].Authenticated {
|
||||
t.Fatalf("captured = %+v, want 1 row with Authenticated=true (X-Signature-256 fallback must work)", got)
|
||||
}
|
||||
}
|
||||
+54
-11
@@ -169,6 +169,21 @@ type Server struct {
|
||||
// the dashboard failing to start.
|
||||
Mappings store.MappingStore
|
||||
|
||||
// Bindings persists playbook bindings: matcher + mapping + workflow
|
||||
// triples that turn a captured webhook into an archie task
|
||||
// (docs/prds/webhook-intake-security.md). Optional: nil makes every
|
||||
// /api/bindings route answer 503 rather than the dashboard failing to start.
|
||||
Bindings store.BindingStore
|
||||
|
||||
// BindingDispatcher is the dispatch-time helper surface for bindings:
|
||||
// the capture endpoint calls ArmedBindingsForSource here to look up
|
||||
// the HMAC secret for an incoming webhook. Split from BindingStore so
|
||||
// the CRUD interface stays narrow (six methods, under the
|
||||
// interfacebloat limit). Optional: nil disables per-source HMAC
|
||||
// verification at handleCapture and every event records as
|
||||
// authenticated=false.
|
||||
BindingDispatcher store.BindingDispatcher
|
||||
|
||||
// TelegramUpdateReportPath and TelegramUpdateChatID let a dashboard-
|
||||
// initiated update use the same post-restart notification route as a
|
||||
// Telegram-initiated update. The web UI has no durable chat identity, so
|
||||
@@ -259,36 +274,53 @@ func (s *Server) Broadcast(e events.Event) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) Handler() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
func (s *Server) registerCoreRoutes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("GET /api/summary", s.handleSummary)
|
||||
mux.HandleFunc("GET /api/setup", s.handleSetup)
|
||||
mux.HandleFunc("GET /api/tasks", s.handleTasks)
|
||||
mux.HandleFunc("GET /api/task-meta", s.handleTaskMeta)
|
||||
mux.HandleFunc("POST /api/tasks/{id}/action", s.handleTaskAction)
|
||||
mux.HandleFunc("GET /api/tasks/{id}", s.handleTask)
|
||||
mux.HandleFunc("GET /api/workflows", s.handleWorkflows)
|
||||
mux.HandleFunc("POST /api/work-requests", s.handleWorkRequest)
|
||||
mux.HandleFunc("GET /api/skills", s.handleSkills)
|
||||
mux.HandleFunc("GET /api/curators", s.handleCurators)
|
||||
mux.HandleFunc("GET /api/captures", s.handleCaptures)
|
||||
mux.HandleFunc("GET /api/channels", s.handleChannels)
|
||||
mux.HandleFunc("POST /api/channels/{id}/reload", s.handleChannelReload)
|
||||
mux.HandleFunc("GET /api/version", s.handleVersion)
|
||||
mux.HandleFunc("GET /api/memory", s.handleMemory)
|
||||
}
|
||||
|
||||
func (s *Server) registerTaskRoutes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("GET /api/tasks", s.handleTasks)
|
||||
mux.HandleFunc("GET /api/task-meta", s.handleTaskMeta)
|
||||
mux.HandleFunc("POST /api/tasks/{id}/action", s.handleTaskAction)
|
||||
mux.HandleFunc("GET /api/tasks/{id}", s.handleTask)
|
||||
mux.HandleFunc("GET /api/tasks/{id}/logs", s.handleTaskLogs)
|
||||
}
|
||||
|
||||
func (s *Server) registerMappingAndBindingRoutes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("GET /api/mappings", s.handleMappingsList)
|
||||
mux.HandleFunc("POST /api/mappings", s.handleMappingCreate)
|
||||
mux.HandleFunc("GET /api/mappings/{id}", s.handleMappingGet)
|
||||
mux.HandleFunc("PATCH /api/mappings/{id}", s.handleMappingUpdate)
|
||||
mux.HandleFunc("DELETE /api/mappings/{id}", s.handleMappingDelete)
|
||||
mux.HandleFunc("POST /api/mappings/preview", s.handleMappingPreview)
|
||||
mux.HandleFunc("GET /api/channels", s.handleChannels)
|
||||
mux.HandleFunc("POST /api/channels/{id}/reload", s.handleChannelReload)
|
||||
mux.HandleFunc("GET /api/bindings", s.handleBindingsList)
|
||||
mux.HandleFunc("POST /api/bindings", s.handleBindingCreate)
|
||||
mux.HandleFunc("GET /api/bindings/{id}", s.handleBindingGet)
|
||||
mux.HandleFunc("PATCH /api/bindings/{id}", s.handleBindingUpdate)
|
||||
mux.HandleFunc("DELETE /api/bindings/{id}", s.handleBindingDelete)
|
||||
mux.HandleFunc("POST /api/bindings/{id}/approve", s.handleBindingApprove)
|
||||
}
|
||||
|
||||
func (s *Server) registerConfigAndLogRoutes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("GET /api/config", s.handleConfig)
|
||||
mux.HandleFunc("GET /api/version", s.handleVersion)
|
||||
mux.HandleFunc("PATCH /api/config", s.handleConfigUpdate)
|
||||
mux.HandleFunc("POST /api/config/reset", s.handleConfigReset)
|
||||
mux.HandleFunc("PATCH /api/config/repos/{owner}/{name}", s.handleConfigRepoUpdate)
|
||||
mux.HandleFunc("GET /api/logs", s.handleLogs)
|
||||
mux.HandleFunc("GET /api/tasks/{id}/logs", s.handleTaskLogs)
|
||||
mux.HandleFunc("GET /api/logs/stream", s.handleLogStream)
|
||||
mux.HandleFunc("GET /api/memory", s.handleMemory)
|
||||
}
|
||||
|
||||
func (s *Server) registerChatRoutes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("GET /api/chat/sessions", s.handleChatSessions)
|
||||
mux.HandleFunc("GET /api/chat/sessions/{id}/messages", s.handleChatMessages)
|
||||
mux.HandleFunc("GET /api/chat/sessions/{id}/turns", s.handleChatTurns)
|
||||
@@ -302,6 +334,17 @@ func (s *Server) Handler() http.Handler {
|
||||
mux.HandleFunc("GET /api/chat/dangerous", s.handleChatDangerousState)
|
||||
mux.HandleFunc("POST /api/chat/dangerous/{kind}", s.handleChatDangerousRequest)
|
||||
mux.HandleFunc("POST /api/chat/dangerous/{id}/decision", s.handleChatDangerousDecision)
|
||||
}
|
||||
|
||||
func (s *Server) Handler() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
s.registerCoreRoutes(mux)
|
||||
s.registerTaskRoutes(mux)
|
||||
s.registerMappingAndBindingRoutes(mux)
|
||||
s.registerConfigAndLogRoutes(mux)
|
||||
s.registerChatRoutes(mux)
|
||||
|
||||
mux.HandleFunc("GET /events", s.handleSSE)
|
||||
mux.Handle("GET /", s.assets())
|
||||
|
||||
|
||||
Reference in new issue
Block a user