Files
archie/modules/events/events.go
T
sam f24c2b4455 feat(connectors): separate packages from scoped installations (#2027)
Packages are accepted once by the instance. Installations configure them at the instance, an organisation or a person; bindings and credentials resolve the person's, then their organisation's, then the instance's. Decisions, approval requests and held chat actions record the installation and scope that acted.

The dashboard lists packages separately from installations, shows every installation's scope, and creates, edits and disables installations at their scope.
2026-11-24 03:05:27 +11:00

826 lines
30 KiB
Go

// Package events is how Archie hears: sources deliver payloads, a connector
// turns each verified payload into typed events, and every event is stored
// before it is acknowledged. A source whose connector holds its own connection
// (a chat long poll) streams its events instead, resuming from a stored
// cursor. A dispatcher then claims each new event, settles an approval it
// answers, links a resolve to what it resolves, and starts a run for every
// trigger it matches.
package events
import (
"context"
"crypto/rand"
"encoding/hex"
"encoding/json/jsontext"
json "encoding/json/v2"
"errors"
"fmt"
"log/slog"
"net"
"net/http"
"net/netip"
"regexp"
"slices"
"strings"
"sync"
"time"
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
"github.com/samcharles93/archie-core/internal/platform/httpjson"
"github.com/samcharles93/archie-core/modules/approvals"
"github.com/samcharles93/archie-core/modules/connectors"
"github.com/samcharles93/archie-core/modules/events/internal/mapping"
"github.com/samcharles93/archie-core/modules/events/internal/match"
"github.com/samcharles93/archie-core/modules/events/internal/store"
"github.com/samcharles93/archie-core/modules/governance"
"github.com/samcharles93/archie-core/modules/runs"
)
var validName = regexp.MustCompile(`^[a-z][a-z0-9-]{0,62}$`)
// Connectors finds the running connector a source reads with, and says what a
// connector declared when it last loaded.
type Connectors interface {
Get(ctx context.Context, name string) (*connectors.Connector, bool)
Capabilities(ctx context.Context, connector string) (connectors.Capabilities, bool, error)
}
// Runs starts runs for matched triggers, and says whether a trigger could
// start its workflow.
type Runs interface {
Start(ctx context.Context, req runs.StartRequest) (uuid.UUID, error)
CheckTrigger(ctx context.Context, orgID, workflow string) error
}
// Deliveries recognises the echo of a message Archie sent. It returns the run
// that sent the message the ids name on channel, which is nil when the message
// belonged to no run.
type Deliveries interface {
SentBy(ctx context.Context, channel string, ids ...string) (run uuid.UUID, found bool, err error)
// Send writes text to one chat on a channel.
Send(ctx context.Context, channel, chatID, text, key string) error
}
// Answers settles the approval requests answer events name.
type Answers interface {
Settle(ctx context.Context, id uuid.UUID, who governance.Subject, via string, approved bool) (bool, error)
}
// Links binds the channel accounts that account.link events name to the
// accounts that issued their codes.
type Links interface {
CompleteLink(ctx context.Context, issuer, subject, name, code string) (governance.Identity, error)
// LinkingURL is the dashboard page where a person links a channel account.
LinkingURL(ctx context.Context) (string, error)
}
// The events a channel emits. An answer carries the approval request and the
// person's decision; a link carries the code the person sent to the channel.
// Both name the sender as "<connector>:<channel user id>" in by, and may name
// the chat to reply to in chat_id.
const (
answerType = "approval.answer"
linkType = "account.link"
)
// listenRetry is how long a listener waits after its stream breaks.
const listenRetry = 5 * time.Second
// Module hears events for one organisation.
type Module struct {
orgID string
store *store.Store
reader sourceReader
inserter eventInserter
types typeReader
delivery governance.DeliveryAuthorizer
proxies func(context.Context) ([]netip.Prefix, error)
sealer Sealer
parse func(ctx context.Context, connector string, body []byte, headers map[string]string) ([]connectors.Event, error)
connectors Connectors
runs Runs
answers Answers
links Links
deliveries Deliveries
log *slog.Logger
poll time.Duration
}
// NewModule returns the events module for orgID.
func NewModule(db *sqlx.DB, log *slog.Logger, orgID string, sealer Sealer, c Connectors, r Runs, a Answers, l Links, d Deliveries) *Module {
st := store.New(db)
m := &Module{orgID: orgID, store: st, reader: st, inserter: st, types: st, sealer: sealer, connectors: c, runs: r, answers: a, links: l, deliveries: d, log: log, poll: time.Second}
m.parse = m.parseWith
return m
}
// sourceReader is the store method that intake uses to look up a source.
type sourceReader interface {
Source(ctx context.Context, orgID, name string) (store.Source, error)
}
// eventInserter stores an event, and reports false when its source event id
// was stored before.
type eventInserter interface {
Insert(ctx context.Context, e store.Event) (isNew bool, err error)
}
// UseDeliveryPolicy gives intake the policy that decides, per source and
// sender address, who may deliver. Until it is set every delivery is refused.
func (m *Module) UseDeliveryPolicy(d governance.DeliveryAuthorizer) { m.delivery = d }
// Routes adds the webhook intake. It stays outside the API because each
// source verifies its own deliveries.
func (m *Module) Routes(mux *http.ServeMux) {
mux.HandleFunc("POST /hooks/{source}", m.intake)
}
// intake verifies a delivery, checks the source's delivery policy, parses it
// with the source's connector and stores each event before responding. A
// redelivered event violates a uniqueness constraint, is counted as a
// duplicate and is not dispatched again.
func (m *Module) intake(w http.ResponseWriter, r *http.Request) {
d, status, err := m.accept(r)
if err != nil {
httpjson.Error(w, status, err.Error())
return
}
stored, duplicates, err := m.save(r.Context(), d.admission, d.events, d.body, d.arrival)
if err != nil {
httpjson.Error(w, http.StatusServiceUnavailable, err.Error())
return
}
httpjson.Write(w, http.StatusAccepted, map[string]int{"stored": stored, "duplicates": duplicates})
}
// delivery is a webhook delivery that passed verification and was parsed.
type delivery struct {
src store.Source
// admission is set after the source's delivery policy allows the sender.
admission admission
events []connectors.Event
body []byte
arrival arrival
}
// accept verifies a delivery against its source's scheme, then passes it to
// the connector for parsing. A delivery that fails verification is rejected
// with 401 before the connector sees its body, and the rejection is logged.
// An unknown source and a failed verification get the same response, so a
// caller cannot use it to discover source names.
func (m *Module) accept(r *http.Request) (d delivery, status int, err error) {
ctx := r.Context()
src, err := m.reader.Source(ctx, m.orgID, r.PathValue("source"))
if errors.Is(err, store.ErrNotFound) {
return d, http.StatusUnauthorized, errUnknown
}
if err != nil {
return d, http.StatusServiceUnavailable, errors.New("events store unavailable")
}
d.src = src
// A token is checked before the body is read. A signature covers the body,
// so it is checked on the raw bytes as soon as they are read. A source that
// checks nothing takes deliveries only once a person approved that.
switch src.Verification {
case store.Token:
if err := checkToken(src, r); err != nil {
return m.refuse(ctx, d, err)
}
d.arrival.verification = store.TokenVerified
case store.HMAC:
d.arrival.verification = store.Signed
case store.None:
if !src.UnsignedApproved {
return m.refuse(ctx, d, fmt.Errorf("%w: the source isn't approved for unsigned deliveries", errUnverified))
}
d.arrival.verification = store.Unsigned
default:
return m.refuse(ctx, d, fmt.Errorf("unknown verification %q", src.Verification))
}
d.body, err = httpjson.Body(r)
if err != nil {
return d, http.StatusRequestEntityTooLarge, err
}
if src.Verification == store.HMAC {
if err := m.verifyHMAC(src, r.Header, d.body); err != nil {
return m.refuse(ctx, d, err)
}
}
addr := m.clientAddr(ctx, r)
d.admission, status, err = m.admit(ctx, src, addr)
if err != nil {
return d, status, err
}
d.events, err = m.parse(ctx, src.Connector, d.body, map[string]string{"Content-Type": r.Header.Get("Content-Type")})
if errors.Is(err, errConnectorDown) {
return d, http.StatusServiceUnavailable, err
}
if err != nil {
return d, http.StatusBadRequest, err
}
d.arrival.addr = addr
d.arrival.headers = r.Header
return d, 0, nil
}
var (
errUnknown = errors.New("unknown source or failed verification")
errConnectorDown = errors.New("connector isn't running")
)
// refuse logs why a delivery was rejected and chooses the status: 401 when
// verification failed, 503 when verification could not run.
func (m *Module) refuse(ctx context.Context, d delivery, err error) (delivery, int, error) {
m.log.WarnContext(ctx, "refused webhook delivery", "source", d.src.Name, "verification", d.src.Verification, "reason", err.Error())
if errors.Is(err, errUnverified) {
return d, http.StatusUnauthorized, errUnknown
}
return d, http.StatusServiceUnavailable, errors.New("couldn't verify the delivery")
}
var (
errDeliveryDenied = errors.New("delivery not permitted")
errDeliveryUnavailable = errors.New("delivery policy unavailable")
)
// admission records that the source's delivery policy allowed a sender. Only
// admit creates one and save requires one, so no intake path can store events
// without evaluating the policy.
type admission struct {
src store.Source
}
// admit evaluates the source's delivery policy against the sender's address.
// The address is empty for a source without a remote sender, such as a
// connector's listener stream. Verified deliveries are evaluated too. If the
// policy chain returns no decision, admit returns 503 so that a webhook sender
// retries, and nothing is stored.
func (m *Module) admit(ctx context.Context, src store.Source, addr string) (admission, int, error) {
if m.delivery == nil {
return admission{}, http.StatusServiceUnavailable, errDeliveryUnavailable
}
// Delivery policies belong to the operating organisation, whatever the
// source's own org id says.
d := m.delivery.AuthorizeDelivery(ctx, governance.DefaultOrgID, src.Name, addr)
switch {
case d.Allowed:
return admission{src: src}, 0, nil
case errors.Is(d.Err, governance.ErrChainUnavailable):
m.log.WarnContext(ctx, "delivery policy unavailable", "source", src.Name)
return admission{}, http.StatusServiceUnavailable, errDeliveryUnavailable
}
m.log.WarnContext(ctx, "refused delivery", "source", src.Name, "reason", "delivery policy", "level", d.Level, "policies", d.Policies)
return admission{}, http.StatusForbidden, errDeliveryDenied
}
// clientAddr returns the request's client address: the connection's peer
// address, or the forwarded client address when the peer is a trusted proxy.
func (m *Module) clientAddr(ctx context.Context, r *http.Request) string {
peer := r.RemoteAddr
if host, _, err := net.SplitHostPort(peer); err == nil {
peer = host
}
return forwardedClient(peer, r.Header.Values("X-Forwarded-For"), m.trustedProxies(ctx))
}
// forwardedClient returns the client address from X-Forwarded-For. The header
// lists the client first, followed by each proxy that relayed the request.
// Only entries appended by trusted proxies are reliable, because a client can
// set any value at the left end, so the list is read from right to left and
// the first address that is not a trusted proxy is returned. The peer address
// is returned when the peer is not trusted or the header can't be parsed.
func forwardedClient(peer string, forwarded []string, trusted []netip.Prefix) string {
if len(trusted) == 0 || !isTrusted(peer, trusted) {
return peer
}
hops := strings.Split(strings.Join(forwarded, ","), ",")
for i, hop := range slices.Backward(hops) {
hop := strings.TrimSpace(hop)
addr, err := netip.ParseAddr(hop)
if err != nil {
return peer
}
if i == 0 || !isTrusted(hop, trusted) {
return addr.Unmap().String()
}
}
return peer
}
// isTrusted reports whether addr is inside any of the trusted prefixes.
func isTrusted(addr string, trusted []netip.Prefix) bool {
a, err := netip.ParseAddr(addr)
if err != nil {
return false
}
a = a.Unmap()
return slices.ContainsFunc(trusted, func(p netip.Prefix) bool { return p.Contains(a) })
}
// trustedProxies returns the configured trusted proxy prefixes.
func (m *Module) trustedProxies(ctx context.Context) []netip.Prefix {
if m.proxies == nil {
return nil
}
prefixes, err := m.proxies(ctx)
if err != nil {
m.log.WarnContext(ctx, "read trusted proxies", "error", err)
return nil
}
return prefixes
}
// UseTrustedProxies sets the function that reads the trusted reverse proxies.
// If it is not set, X-Forwarded-For is ignored.
func (m *Module) UseTrustedProxies(read func(context.Context) ([]netip.Prefix, error)) {
m.proxies = read
}
// parseWith has a running connector parse a delivery.
func (m *Module) parseWith(ctx context.Context, connector string, body []byte, headers map[string]string) ([]connectors.Event, error) {
conn, ok := m.connectors.Get(ctx, connector)
if !ok {
return nil, fmt.Errorf("connector %s: %w", connector, errConnectorDown)
}
return conn.Parse(ctx, body, headers)
}
// save stores each event with the payload it arrived in, recording at arrival
// whether it echoes a delivery Archie sent, so those never reach grouping.
func (m *Module) save(ctx context.Context, adm admission, parsed []connectors.Event, body []byte, a arrival) (stored, duplicates int, err error) {
src := adm.src
for i, e := range parsed {
run, own, err := m.deliveries.SentBy(ctx, src.Connector, e.SourceEventID, e.LinkKey)
if err != nil {
return stored, duplicates, fmt.Errorf("check delivery provenance: %w", err)
}
row := newRow(m.orgID, src, e, body, own, run)
if err := m.decorate(ctx, &row, src, a, i); err != nil {
return stored, duplicates, err
}
isNew, err := m.inserter.Insert(ctx, row)
if err != nil {
return stored, duplicates, err
}
if isNew {
stored++
} else {
duplicates++
}
}
return stored, duplicates, nil
}
// Work listens to every listening source and dispatches stored events until
// ctx ends.
func (m *Module) Work(ctx context.Context) {
var listeners sync.WaitGroup
defer listeners.Wait()
listening := map[string]bool{}
nextCheck, nextTick := time.Time{}, time.Time{}
for ctx.Err() == nil {
if now := time.Now(); now.After(nextTick) {
if err := m.tick(ctx, now); err != nil && ctx.Err() == nil {
m.log.ErrorContext(ctx, "fire schedules", "error", err)
}
nextTick = now.Add(scheduleCheck)
}
if time.Now().After(nextCheck) {
m.startListeners(ctx, &listeners, listening)
nextCheck = time.Now().Add(listenRetry)
}
found, err := m.store.Dispatch(ctx, m.dispatch)
if err != nil {
m.log.ErrorContext(ctx, "dispatch event", "error", err)
}
if found && err == nil {
continue
}
select {
case <-ctx.Done():
case <-time.After(m.poll):
}
}
}
// startListeners starts one listener for each source whose connector listens
// and has none yet. A listener lives until ctx ends.
func (m *Module) startListeners(ctx context.Context, wg *sync.WaitGroup, listening map[string]bool) {
sources, err := m.store.Sources(ctx, m.orgID)
if err != nil {
m.log.ErrorContext(ctx, "list sources", "error", err)
return
}
for _, src := range sources {
conn, ok := m.connectors.Get(ctx, src.Connector)
if listening[src.Name] || !ok || !conn.Listens() {
continue
}
listening[src.Name] = true
wg.Go(func() { m.listen(ctx, src) })
}
}
// listen streams one source's events, storing each batch before its cursor,
// so a restart fetches again anything not yet stored and dedupe drops what
// was. A broken stream is opened again from the stored cursor.
func (m *Module) listen(ctx context.Context, src store.Source) {
cursor := src.Cursor
for ctx.Err() == nil {
conn, ok := m.connectors.Get(ctx, src.Connector)
var err error
if !ok {
err = fmt.Errorf("connector %q isn't running", src.Connector)
} else {
err = conn.Listen(ctx, cursor, func(events []connectors.Event, next string) error {
if err := m.listenBatch(ctx, src, events, next); err != nil {
return err
}
cursor = next
return nil
})
}
if ctx.Err() != nil {
return
}
m.log.WarnContext(ctx, "listening stopped", "source", src.Name, "error", err)
select {
case <-ctx.Done():
case <-time.After(listenRetry):
}
}
}
// listenBatch stores one streamed batch, then moves the source's cursor past it.
func (m *Module) listenBatch(ctx context.Context, src store.Source, events []connectors.Event, next string) error {
if err := m.storeListened(ctx, src, events); err != nil {
return err
}
return m.store.SetCursor(ctx, m.orgID, src.Name, next)
}
// storeListened stores a batch streamed by a connector listener if the source's
// delivery policy allows it. A listener has no remote sender, so the policy is
// evaluated with an empty address: rules on the source apply, and a rule that
// allows only specific addresses denies every listener. A denied batch is
// dropped without an error, because evaluating it again would return the same
// decision.
func (m *Module) storeListened(ctx context.Context, src store.Source, events []connectors.Event) error {
adm, _, err := m.admit(ctx, src, "")
if errors.Is(err, errDeliveryDenied) {
return nil
}
if err != nil {
return err
}
_, _, err = m.save(ctx, adm, events, received(events), arrival{})
return err
}
// answer settles the approval request an answer event names. An answer that
// can't settle it is logged and done with: the request's own record already
// says why. A channel account no account has linked is told how to link it.
func (m *Module) answer(ctx context.Context, e store.Event) error {
var a struct {
RequestID uuid.UUID `json:"request_id"`
Approved bool `json:"approved"`
By string `json:"by"`
ChatID string `json:"chat_id"`
}
if err := json.Unmarshal(e.Fields, &a); err != nil {
m.log.WarnContext(ctx, "unreadable approval answer", "event", e.ID, "error", err)
return nil
}
src, who, ok := m.sender(ctx, e, a.By)
if !ok {
return nil
}
settled, err := m.answers.Settle(ctx, a.RequestID, who, e.Source, a.Approved)
if errors.Is(err, approvals.ErrUnlinked) {
m.replyUnlinked(ctx, e, src, a.ChatID)
return nil
}
if err != nil {
return fmt.Errorf("settle approval %s: %w", a.RequestID, err)
}
if !settled {
m.log.InfoContext(ctx, "approval answer changed nothing", "event", e.ID, "request", a.RequestID)
}
return nil
}
// link binds the channel account that sent a link code to the account that
// issued the code, and tells the sender how it went. A code that doesn't work
// is not retried.
func (m *Module) link(ctx context.Context, e store.Event) error {
var l struct {
By string `json:"by"`
Code string `json:"code"`
Name string `json:"name"`
ChatID string `json:"chat_id"`
}
if err := json.Unmarshal(e.Fields, &l); err != nil {
m.log.WarnContext(ctx, "unreadable link request", "event", e.ID, "error", err)
return nil
}
src, who, ok := m.sender(ctx, e, l.By)
if !ok {
return nil
}
account, err := m.links.CompleteLink(ctx, who.Issuer, who.Subject, l.Name, l.Code)
switch {
case errors.Is(err, governance.ErrLinkCode):
m.reply(ctx, e, src, l.ChatID, "That link code didn't work. Start again from your account page in the dashboard.")
case errors.Is(err, governance.ErrLinkTaken):
m.reply(ctx, e, src, l.ChatID, "That "+src.Connector+" account is already linked to another account.")
case err != nil:
return fmt.Errorf("link %s account: %w", src.Connector, err)
default:
m.log.InfoContext(ctx, "channel account linked", "event", e.ID, "connector", src.Connector, "account", account.ID)
m.reply(ctx, e, src, l.ChatID, "Linked to "+account.DisplayName+".")
}
return nil
}
// sender reads who sent a channel event. The connector comes from the event's
// source, so a connector can only speak for its own accounts: by must name the
// source's connector.
func (m *Module) sender(ctx context.Context, e store.Event, by string) (store.Source, governance.Subject, bool) {
src, err := m.reader.Source(ctx, e.OrgID, e.Source)
if err != nil {
m.log.WarnContext(ctx, "channel event from an unknown source", "event", e.ID, "source", e.Source, "error", err)
return store.Source{}, governance.Subject{}, false
}
prefix, subject, found := strings.Cut(by, ":")
if !found || prefix != src.Connector || subject == "" {
m.log.WarnContext(ctx, "channel event names a sender outside its connector", "event", e.ID, "source", e.Source)
return store.Source{}, governance.Subject{}, false
}
return src, governance.Subject{Issuer: src.Connector, Subject: subject}, true
}
// replyUnlinked tells an unlinked channel account where to link it.
func (m *Module) replyUnlinked(ctx context.Context, e store.Event, src store.Source, chatID string) {
url, err := m.links.LinkingURL(ctx)
if err != nil {
m.log.WarnContext(ctx, "read the linking URL", "event", e.ID, "error", err)
return
}
m.reply(ctx, e, src, chatID, "That "+src.Connector+" account isn't linked to an account, so your answer wasn't counted. Link it at "+url+" and answer again.")
}
// reply sends text to the chat an event came from. A reply that fails is
// logged and dropped: the event itself is already handled.
func (m *Module) reply(ctx context.Context, e store.Event, src store.Source, chatID, text string) {
if chatID == "" {
return
}
if err := m.deliveries.Send(ctx, src.Connector, chatID, text, "reply:"+e.ID.String()); err != nil {
m.log.WarnContext(ctx, "reply to a channel event", "event", e.ID, "error", err)
}
}
// dispatch links a resolve to the event it resolves and starts no work for
// it. Any other event starts one run per matching trigger, keyed so a retried
// dispatch never starts a second run.
func (m *Module) dispatch(tx *sqlx.Tx, e store.Event) (bool, error) {
ctx := context.Background()
if e.Own {
// Archie's own message coming back starts nothing and proposes nothing.
return false, nil
}
switch e.Type {
case answerType:
return false, m.answer(ctx, e)
case linkType:
if err := m.link(ctx, e); err != nil {
return false, err
}
return false, m.redactHandledLink(ctx, tx, e)
}
linked, err := store.Link(ctx, tx, e)
if err != nil || linked {
return false, err
}
triggers, err := store.Triggers(ctx, tx, e.OrgID, e.Source, e.Type, e.EventTypeID)
if err != nil {
return false, err
}
matched, refused := false, false
for _, t := range triggers {
outcome, err := m.fire(ctx, tx, e, t)
if err != nil {
return false, err
}
switch outcome {
case fired:
matched = true
case noRun:
refused = true
}
}
if !matched && !refused {
return false, m.noteUnmatched(ctx, tx, e)
}
return matched, nil
}
// outcome is the result of evaluating a trigger against an event: notMatched
// (its filter didn't match), noRun (it matched but started no run, and the
// dispatch records why) or fired (a run started).
type outcome int
const (
notMatched outcome = iota
noRun
fired
)
// fire starts the run trigger t asks for when e matches its filter, and
// records the dispatch.
func (m *Module) fire(ctx context.Context, tx *sqlx.Tx, e store.Event, t store.Trigger) (outcome, error) {
var filter map[string]string
if err := json.Unmarshal(t.Filter, &filter); err != nil {
return notMatched, fmt.Errorf("trigger %q filter: %w", t.Name, err)
}
if !match.Fields(e.Fields, filter) {
return notMatched, nil
}
input, mapped, why, err := m.inputFor(ctx, tx, e, t)
if err != nil {
return noRun, err
}
if why == nil {
var runID uuid.UUID
runID, why, err = m.startRun(ctx, e, t, input)
if err != nil {
return noRun, err
}
if why == nil {
return fired, m.recordStarted(ctx, tx, e, t, runID, mapped)
}
}
return noRun, recordNoRun(ctx, tx, e, t, why)
}
// recordStarted records the run trigger t started for e, and counts the match
// against the trigger's mapping when one was applied.
func (m *Module) recordStarted(ctx context.Context, tx *sqlx.Tx, e store.Event, t store.Trigger, runID uuid.UUID, mapped bool) error {
if mapped {
if err := store.CountMatch(ctx, tx, t.MappingID.UUID); err != nil {
return err
}
}
if err := store.RecordDispatch(ctx, tx, e.ID, t.Name, runID); err != nil {
return err
}
m.log.InfoContext(ctx, "trigger started run", "event", e.ID, "trigger", t.Name, "run", runID)
return nil
}
// recordNoRun stores why trigger t matched e but started no run.
func recordNoRun(ctx context.Context, tx *sqlx.Tx, e store.Event, t store.Trigger, why *noRunReason) error {
reason, err := json.Marshal(why)
if err != nil {
return fmt.Errorf("encode no-run reason: %w", err)
}
return store.RecordNoRun(ctx, tx, e.ID, t.Name, why.outcome, reason)
}
// noteUnmatched files an event no trigger took into a group for review.
func (m *Module) noteUnmatched(ctx context.Context, tx *sqlx.Tx, e store.Event) error {
created, err := store.NoteUnmatched(ctx, tx, e)
if err != nil {
return err
}
if created {
m.log.InfoContext(ctx, "unmatched events started a group", "source", e.Source, "type", e.Type)
}
return nil
}
// startRun starts the run trigger t asks for, as the workload principal, so
// the start is decided by the same policy chain as any other caller. A policy
// refusal or a disabled workflow is final: it returns the reason no run started
// and no error. A policy chain that can't decide returns an error instead,
// which rolls the dispatch back so the event stays queued and is dispatched
// again.
func (m *Module) startRun(ctx context.Context, e store.Event, t store.Trigger, input jsontext.Value) (uuid.UUID, *noRunReason, error) {
ctx = governance.WithPrincipal(ctx, governance.WorkloadPrincipal(governance.DefaultOrgID))
runID, err := m.runs.Start(ctx, runs.StartRequest{
OrgID: e.OrgID, Workflow: t.Workflow, Key: e.ID.String() + ":" + t.Name, Input: input,
})
if errors.Is(err, governance.ErrDenied) {
m.log.WarnContext(ctx, "trigger not permitted to start its workflow", "event", e.ID, "trigger", t.Name, "error", err)
return uuid.Nil, &noRunReason{outcome: store.OutcomeDenied, Code: reasonAccessDenied, Workflow: t.Workflow, Detail: err.Error()}, nil
}
if errors.Is(err, runs.ErrDisabled) {
m.log.InfoContext(ctx, "trigger started no run: the workflow is disabled", "event", e.ID, "trigger", t.Name, "workflow", t.Workflow)
return uuid.Nil, &noRunReason{outcome: store.OutcomeDisabled, Code: reasonWorkflowDisabled, Workflow: t.Workflow}, nil
}
if err != nil {
return uuid.Nil, nil, fmt.Errorf("start run for trigger %q: %w", t.Name, err)
}
return runID, nil, nil
}
// Origin returns the event and trigger that started a run.
func (m *Module) Origin(ctx context.Context, runID uuid.UUID) (runs.Origin, bool, error) {
o, ok, err := m.store.OriginOf(ctx, m.orgID, runID)
if err != nil || !ok {
return runs.Origin{}, ok, err
}
return runs.Origin{
EventID: o.EventID, Source: o.Source, Type: o.Type, Fields: o.Fields,
ReceivedAt: o.ReceivedAt, Trigger: o.Trigger, Filter: o.Filter,
}, true, nil
}
func runInput(e store.Event) jsontext.Value {
return runInputWith(e, nil)
}
// runInputWith is the run input for e, with the values a mapping read from its
// payload under "mapped" when there are any.
func runInputWith(e store.Event, mapped map[string]any) jsontext.Value {
doc := map[string]any{"event": map[string]any{
"id": e.ID, "source": e.Source, "type": e.Type, "fields": e.Fields, "occurred_at": e.OccurredAt.Time,
}}
if mapped != nil {
doc["mapped"] = mapped
}
input, err := json.Marshal(doc)
if err != nil {
return jsontext.Value("null")
}
return input
}
// Codes for why a trigger that matched an event started no run.
const (
reasonMappingMismatch = "mapping_mismatch"
reasonAccessDenied = "access_denied"
reasonWorkflowDisabled = "workflow_disabled"
)
// noRunReason explains why a trigger matched an event but started no run. It
// is stored as the dispatch's reason and shown on the capture and the trigger.
type noRunReason struct {
// outcome is the dispatch outcome stored with this reason.
outcome string
Code string `json:"code"`
Mapping string `json:"mapping,omitempty"`
Failures []mapping.Failure `json:"failures,omitempty"`
Workflow string `json:"workflow,omitempty"`
Detail string `json:"detail,omitempty"`
}
// inputFor builds the run input for trigger t. For a trigger with a mapping,
// the mapping is applied to the event's payload. When a field the mapping
// requires is missing or has the wrong shape, no run starts and the failure is
// recorded on the event.
func (m *Module) inputFor(ctx context.Context, tx *sqlx.Tx, e store.Event, t store.Trigger) (input jsontext.Value, mapped bool, failure *noRunReason, err error) {
if !t.MappingID.Valid {
return runInput(e), false, nil, nil
}
row, err := store.MappingIn(ctx, tx, e.OrgID, t.MappingID.UUID)
if err != nil {
return nil, false, nil, err
}
return applyMapping(e, row)
}
// applyMapping applies the mapping row to the event's payload.
func applyMapping(e store.Event, row store.Mapping) (input jsontext.Value, mapped bool, failure *noRunReason, err error) {
var fields []mapping.Field
if err := json.Unmarshal(row.Fields, &fields); err != nil {
return nil, false, nil, fmt.Errorf("mapping %q fields: %w", row.Name, err)
}
values, failures := mapping.Resolve(fields, e.Body)
if len(failures) > 0 {
return nil, false, &noRunReason{outcome: store.OutcomeFailed, Code: reasonMappingMismatch, Mapping: row.Name, Failures: failures}, nil
}
return runInputWith(e, values), true, nil, nil
}
func newToken() (string, error) {
b := make([]byte, 32)
if _, err := rand.Read(b); err != nil {
return "", fmt.Errorf("generate token: %w", err)
}
return hex.EncodeToString(b), nil
}
// received renders a batch a listening connector streamed, so the stored body
// is what reached the run rather than only the fields parsed from it.
func received(events []connectors.Event) []byte {
body, err := json.Marshal(events)
if err != nil {
return nil
}
return body
}