Files
archie/internal/domain/binding/binding.go
T
sam 684ec1263a feat(eda): sources own signing, with UUIDv7 paths and approved unsigned option
A source is the capture endpoint: a UUIDv7 path by default or a unique
URL-safe custom path, signed by default with an Archie-generated secret
(encrypted via the bindings cipher), or unsigned after an approval.
Intake verifies X-Hub-Signature-256/X-Signature-256 against the source
secret before redaction; an unauthenticated event on a signed source is
captured but never dispatched, an event on an approved unsigned source
dispatches and is marked unsigned. The binding secret is retired; migration
0006 derives sources from existing binding and capture source strings,
moving each binding's secret onto its source. SourceStore crosses the
State Store gRPC contract. The dashboard gains a sources panel and flags
unsigned in the capture inspector, binding list and task timeline.
2026-09-24 14:26:21 +10:00

93 lines
3.6 KiB
Go

// 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: a binding is created
// pending_approval, armed is the only state that evaluates against incoming
// events, any edit drops armed back to pending_approval, and only an
// explicit Approve call moves pending_approval -> armed.
type Status string
const (
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. Signing belongs to the source the matcher names, not to the
// binding (internal/domain/source).
type Binding struct {
ID string `json:"id"`
Name string `json:"name"`
Matcher Matcher `json:"matcher"`
MappingID string `json:"mapping_id"`
Workflow string `json:"workflow"`
// Owner and Repo pin a binding to a specific configured repo, so a
// multi-repo deployment can dispatch correctly. Both empty means "no
// pin" -- resolveBindingRepo falls back to the single-configured-repo
// behaviour that predates this field. Setting only one is invalid
// (Validate rejects it): a pin is a complete owner/repo pair or not a
// pin at all, never a half-guess.
Owner string `json:"owner,omitempty"`
Repo string `json:"repo,omitempty"`
Version int `json:"version"`
Status Status `json:"status"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// Validate checks a binding is well-formed before it is persisted: a
// non-empty Name, Matcher.Source, MappingID and Workflow, and a complete
// owner/repo pin or none. 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 strings.TrimSpace(b.MappingID) == "" {
return fmt.Errorf("binding: mapping_id is required")
}
if strings.TrimSpace(b.Workflow) == "" {
return fmt.Errorf("binding: workflow is required")
}
if (b.Owner == "") != (b.Repo == "") {
return fmt.Errorf("binding: owner and repo must both be set or both be empty")
}
return nil
}
// Matches reports whether a captured event would trigger this binding. An
// event that is not dispatchable (no valid signature, and not from an
// approved unsigned source) never matches, however well it would otherwise.
// Matches is pure and has no I/O.
func (m Matcher) Matches(source string, dispatchable bool) bool {
return dispatchable && m.Source == source
}