359 lines
14 KiB
Go
359 lines
14 KiB
Go
package daemon
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"maps"
|
|
"strings"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/access"
|
|
"github.com/samcharles93/archie-core/internal/domain/binding"
|
|
"github.com/samcharles93/archie-core/internal/domain/identity"
|
|
"github.com/samcharles93/archie-core/internal/domain/mapping"
|
|
"github.com/samcharles93/archie-core/internal/domain/org"
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
|
workflowtask "github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
|
"github.com/samcharles93/archie-core/internal/events"
|
|
)
|
|
|
|
// dispatchBindings offers each identified capture to every armed binding on
|
|
// its source.
|
|
func (d *Daemon) dispatchBindings(ctx context.Context) {
|
|
principals := d.OrgPrincipals(ctx)
|
|
seen := make(map[org.OrgID]bool)
|
|
for _, principal := range principals {
|
|
if seen[principal.Org] {
|
|
continue
|
|
}
|
|
seen[principal.Org] = true
|
|
d.dispatchOrgBindings(org.WithOrg(access.WithPrincipal(ctx, principal), principal.Org))
|
|
}
|
|
}
|
|
|
|
func (d *Daemon) dispatchOrgBindings(ctx context.Context) {
|
|
// Gather the armed bindings whose workflow is enabled first, so the
|
|
// oldest-first captures query is not held by captures that only a binding
|
|
// on a disabled workflow would take.
|
|
bindings, err := d.Bindings.ListBindings(ctx)
|
|
if err != nil {
|
|
d.Log.WarnContext(ctx, "list bindings failed", "error", err)
|
|
return
|
|
}
|
|
enablement, err := d.workflowEnablement(ctx)
|
|
if err != nil {
|
|
d.Log.WarnContext(ctx, "binding dispatch: workflow enablement unavailable", "error", err)
|
|
return
|
|
}
|
|
enabled := make([]string, 0, len(bindings))
|
|
armed := make([]binding.Binding, 0, len(bindings))
|
|
for _, b := range bindings {
|
|
if b.OrgID == org.OrgFromContext(ctx) && b.Status == binding.StatusArmed && b.Matcher.Source != "" && enablement.Enabled(b.OrgID, b.Workflow) {
|
|
enabled = append(enabled, b.ID)
|
|
armed = append(armed, b)
|
|
}
|
|
}
|
|
if len(enabled) == 0 {
|
|
return
|
|
}
|
|
|
|
captures, err := d.BindingDispatcher.ListUndispatchedCaptures(ctx, enabled, bindingDispatchBatchLimit)
|
|
if err != nil {
|
|
d.Log.WarnContext(ctx, "list undispatched captures failed", "error", err)
|
|
return
|
|
}
|
|
workflows, err := d.activeWorkflows(ctx)
|
|
if err != nil {
|
|
d.Log.WarnContext(ctx, "binding dispatch: workflow definitions unavailable", "error", err)
|
|
return
|
|
}
|
|
for _, c := range captures {
|
|
d.dispatchCapture(ctx, c, armed, workflows)
|
|
}
|
|
}
|
|
|
|
// dispatchCapture offers one capture to every armed binding that matches it.
|
|
// armed holds only the org's armed bindings on enabled workflows, so a
|
|
// disabled workflow's capture waits rather than recording a failure every pass.
|
|
func (d *Daemon) dispatchCapture(ctx context.Context, c storecontract.CapturedEvent, armed []binding.Binding, workflows workflow.WorkflowDefinitionCollection) {
|
|
for _, b := range armed {
|
|
if b.Matcher.Matches(c.Source, c.Dispatchable()) {
|
|
d.dispatchOneBinding(ctx, b, c, workflows)
|
|
}
|
|
}
|
|
}
|
|
|
|
// bindingMapping returns b's mapping, or nil after logging why it is
|
|
// unavailable. A mapping that no longer exists is a terminal outcome.
|
|
func (d *Daemon) bindingMapping(ctx context.Context, b binding.Binding, c storecontract.CapturedEvent) *mapping.Mapping {
|
|
m, err := d.Mappings.GetMapping(ctx, b.MappingID)
|
|
if err != nil {
|
|
d.Log.WarnContext(ctx, "binding dispatch: get mapping", "binding", b.ID, "mapping", b.MappingID, "error", err)
|
|
return nil
|
|
}
|
|
if m == nil {
|
|
d.recordDispatchFailure(ctx, b, c, fmt.Sprintf("mapping %q does not exist", b.MappingID), nil)
|
|
}
|
|
return m
|
|
}
|
|
|
|
// dispatchOneBinding offers one capture to one binding: the mapping must
|
|
// match the capture's event type and resolve required fields, the filter
|
|
// must admit the values, and the workflow must accept the inputs. The
|
|
// ledger claim and the task commit together, so each binding fires exactly
|
|
// once per delivery.
|
|
func (d *Daemon) dispatchOneBinding(ctx context.Context, b binding.Binding, c storecontract.CapturedEvent, workflows workflow.WorkflowDefinitionCollection) {
|
|
m := d.bindingMapping(ctx, b, c)
|
|
if m == nil || m.EventTypeID != c.EventType {
|
|
return
|
|
}
|
|
values, failures := mapping.Resolve(m.Fields, []byte(c.Body))
|
|
if hasBlockingFailure(m.Fields, failures) {
|
|
d.recordDispatchFailure(ctx, b, c, "required field failed", map[string]any{"failures": failures})
|
|
return
|
|
}
|
|
if err := d.MappingMatches.RecordMappingMatch(ctx, m.ID, c.ID); err != nil {
|
|
d.Log.WarnContext(ctx, "binding dispatch: record mapping match", "mapping", m.ID, "capture", c.ID, "error", err)
|
|
}
|
|
filter, err := binding.CompileFilter(b.Filter, m.Fields)
|
|
if err != nil {
|
|
d.recordDispatchFailure(ctx, b, c, "filter does not compile", map[string]any{"error": err.Error()})
|
|
return
|
|
}
|
|
if admitted, _ := filter.Admits(values); !admitted {
|
|
d.recordEvaluated(ctx, b, c, "filtered")
|
|
return
|
|
}
|
|
|
|
target, reason, ok := d.resolveBindingTarget(b, values, workflows)
|
|
if !ok {
|
|
d.recordDispatchFailure(ctx, b, c, reason, nil)
|
|
return
|
|
}
|
|
// The chain decides dispatch: the binding's principal may `run` this
|
|
// workflow in its workspace, and the event's signature result and address
|
|
// travel with the request. A denial is terminal; an unavailable principal
|
|
// or policy chain leaves the capture listed for the next cycle.
|
|
if !d.authorizeDispatch(ctx, b, c, target) {
|
|
return
|
|
}
|
|
d.claimAndEnqueue(ctx, b, c, target, values)
|
|
}
|
|
|
|
// claimAndEnqueue asks the State Store to claim the dispatch and enqueue its
|
|
// task as one unit. A failure leaves the delivery undispatched, so the next
|
|
// cycle offers it again; a delivery already dispatched is refused.
|
|
func (d *Daemon) claimAndEnqueue(ctx context.Context, b binding.Binding, c storecontract.CapturedEvent, target bindingTarget, values map[string]any) {
|
|
title := fmt.Sprintf("binding %s/%d from %s", b.Name, b.Version, c.Source)
|
|
body := renderBindingBody(values, c)
|
|
if target.declared {
|
|
body = fmt.Sprintf("Started by binding %q from capture %q; the workflow's inputs travel with the task.", b.Name, c.ID)
|
|
}
|
|
taskIdentity := string(identity.SystemID)
|
|
if principal, ok := access.PrincipalFromContext(ctx); ok {
|
|
taskIdentity = string(principal.IdentityID)
|
|
}
|
|
task, err := d.BindingTaskCreator.EnqueueBindingTask(ctx, target.owner, target.repo, title, body, b.Workflow, taskIdentity, b.ID, b.Version, c.ID, target.inputs)
|
|
if err != nil {
|
|
if !errors.Is(err, storecontract.ErrAlreadyDispatched) {
|
|
d.Log.WarnContext(ctx, "binding dispatch: enqueue", "binding", b.ID, "capture", c.ID, "error", err)
|
|
}
|
|
return
|
|
}
|
|
d.markBindingStart(ctx, task.ID, b, c)
|
|
}
|
|
|
|
// authorizeDispatch checks the binding's principal, its org's dispatching
|
|
// identity, may start the workflow. A denial is recorded and reported as a
|
|
// binding_dispatch_failure. The run itself acts as the workflow's declared
|
|
// identity, which adoptWorkflowIdentity checks when the task is processed.
|
|
func (d *Daemon) authorizeDispatch(ctx context.Context, b binding.Binding, c storecontract.CapturedEvent, target bindingTarget) bool {
|
|
principal, ok := access.PrincipalFromContext(ctx)
|
|
if !ok {
|
|
d.Log.WarnContext(ctx, "binding dispatch: no principal; dispatch left for the next cycle", "binding", b.ID, "capture", c.ID)
|
|
return false
|
|
}
|
|
resource := access.Resource{
|
|
Kind: access.KindWorkflow, ID: b.Workflow, Org: principal.Org, Workspace: b.WorkspaceID,
|
|
}
|
|
contextValue := access.Context{Signature: signatureOf(c), Addr: c.RemoteAddr}
|
|
decision := d.Access.Authorize(ctx, principal, access.ActionRun, resource, contextValue)
|
|
if decision.Allowed {
|
|
return true
|
|
}
|
|
if errors.Is(decision.Err, access.ErrChainUnavailable) {
|
|
// Nothing was evaluated. Recording it would mark the pair evaluated for
|
|
// good, so a State Store blip would drop the capture instead of leaving
|
|
// it for the next cycle.
|
|
d.Log.WarnContext(ctx, "binding dispatch: access chain unavailable; dispatch left for the next cycle", "binding", b.ID, "capture", c.ID, "err", decision.Err)
|
|
return false
|
|
}
|
|
denial := access.Denial{
|
|
Principal: principal.IdentityID, Org: principal.Org, Action: access.ActionRun,
|
|
Kind: resource.Kind, ResourceID: resource.ID, Level: decision.Level,
|
|
Policies: decision.Policies,
|
|
}
|
|
if err := d.Denials.RecordDenial(ctx, denial); err != nil {
|
|
d.Log.WarnContext(ctx, "binding dispatch: record denial", "binding", b.ID, "error", err)
|
|
}
|
|
d.recordDispatchFailure(ctx, b, c, "access denied", map[string]any{
|
|
"level": string(decision.Level), "policies": decision.Policies,
|
|
})
|
|
return false
|
|
}
|
|
|
|
// markBindingStart records on a task's timeline the binding and captured
|
|
// event that started it.
|
|
func (d *Daemon) markBindingStart(ctx context.Context, taskID int64, b binding.Binding, c storecontract.CapturedEvent) {
|
|
if _, err := d.Store.InsertEvent(ctx, events.Event{
|
|
TaskID: taskID,
|
|
Kind: events.KindBindingStarted,
|
|
Detail: fmt.Sprintf("started by binding %q from source %q", b.Name, c.Source),
|
|
Data: map[string]any{
|
|
"binding_id": b.ID, "binding_name": b.Name, "binding_version": b.Version,
|
|
"capture_id": c.ID, "source": c.Source, "unsigned": c.Unsigned,
|
|
},
|
|
}); err != nil {
|
|
d.Log.WarnContext(ctx, "binding dispatch: start marker", "task", taskID, "error", err)
|
|
}
|
|
}
|
|
|
|
// recordDispatchFailure records why a binding did not dispatch a capture and
|
|
// marks the pair evaluated, so the capture is not offered to it again.
|
|
func (d *Daemon) recordDispatchFailure(ctx context.Context, b binding.Binding, c storecontract.CapturedEvent, reason string, extra map[string]any) {
|
|
d.recordEvaluated(ctx, b, c, reason)
|
|
data := map[string]any{
|
|
"binding_id": b.ID,
|
|
"binding_version": b.Version,
|
|
"capture_id": c.ID,
|
|
}
|
|
maps.Copy(data, extra)
|
|
_, _ = d.Store.InsertEvent(ctx, events.Event{
|
|
Kind: "binding_dispatch_failure",
|
|
Detail: fmt.Sprintf("binding %q (capture %q): %s", b.ID, c.ID, reason),
|
|
Data: data,
|
|
})
|
|
}
|
|
|
|
// recordEvaluated writes the ledger row for an evaluation that started no
|
|
// task. Only terminal outcomes call it; transient ones leave the capture
|
|
// listed for the next cycle.
|
|
func (d *Daemon) recordEvaluated(ctx context.Context, b binding.Binding, c storecontract.CapturedEvent, reason string) {
|
|
err := d.BindingDispatcher.RecordDispatch(ctx, b.ID, int64(b.Version), c.ID, 0, reason)
|
|
if err != nil && !errors.Is(err, storecontract.ErrAlreadyDispatched) {
|
|
d.Log.WarnContext(ctx, "binding dispatch: record evaluation", "binding", b.ID, "capture", c.ID, "error", err)
|
|
}
|
|
}
|
|
|
|
// resolveBindingRepo returns the binding's pinned owner/repo, or the only
|
|
// configured repo. With zero or several repos and no pin it refuses.
|
|
func (d *Daemon) resolveBindingRepo(b binding.Binding) (owner, repo, reason string) {
|
|
if b.Owner != "" && b.Repo != "" {
|
|
return b.Owner, b.Repo, ""
|
|
}
|
|
repos := d.Cfg.Get().Repos
|
|
if len(repos) == 1 {
|
|
return repos[0].Owner, repos[0].Name, ""
|
|
}
|
|
return "", "", fmt.Sprintf("cannot pick a repository: %d are configured; pin the binding to one", len(repos))
|
|
}
|
|
|
|
// activeWorkflows returns the definitions a dispatch cycle checks bindings
|
|
// against, the same set a task is later pinned from.
|
|
func (d *Daemon) activeWorkflows(ctx context.Context) (workflow.WorkflowDefinitionCollection, error) {
|
|
collection, _, err := d.WorkflowDefinitions.WorkflowDefinitions(ctx)
|
|
return collection, err
|
|
}
|
|
|
|
// workflowEnablement returns which workflows each org has disabled; with no
|
|
// source wired every workflow is enabled.
|
|
func (d *Daemon) workflowEnablement(ctx context.Context) (workflowtask.WorkflowEnablement, error) {
|
|
return d.WorkflowEnablement.WorkflowEnablement(ctx)
|
|
}
|
|
|
|
// disabledRoutedWorkflow reports the workflow an issue's labels route to and
|
|
// whether the identity's org has that workflow disabled, matching the
|
|
// enablement filter binding dispatch applies. A read failure (org,
|
|
// definitions, enablement) is an error so the caller falls through to
|
|
// publishing rather than dropping the issue; no routable definition is not an
|
|
// error, it is reported as not disabled and the pin path parks the task.
|
|
func (d *Daemon) disabledRoutedWorkflow(ctx context.Context, labels, identityID string) (routed string, disabled bool, err error) {
|
|
orgID, err := d.identityOrg(ctx, identity.IdentityID(identityID))
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
definitions, err := d.activeWorkflows(ctx)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
available := make(map[string]struct{}, len(definitions.Definitions))
|
|
for _, definition := range definitions.Definitions {
|
|
available[definition.ID] = struct{}{}
|
|
}
|
|
routed, resolveErr := workflow.ResolveWorkflowID(&workflow.Task{Labels: labels}, available)
|
|
if resolveErr == nil {
|
|
enablement, err := d.workflowEnablement(ctx)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
return routed, !enablement.Enabled(orgID, routed), nil
|
|
}
|
|
// No routable definition is a normal outcome here, not a read failure:
|
|
// keep the publish behaviour and let the pin path park the task.
|
|
return "", false, nil
|
|
}
|
|
|
|
// 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
|
|
|
|
// signatureOf names the event's signature result as the dispatch context
|
|
// carries it: a signed event and an approved unsigned event are the two
|
|
// dispatchable shapes (storecontract.CapturedEvent.Dispatchable).
|
|
func signatureOf(c storecontract.CapturedEvent) string {
|
|
switch {
|
|
case c.Authenticated:
|
|
return "valid"
|
|
case c.Unsigned:
|
|
return "unsigned"
|
|
}
|
|
return "unverified"
|
|
}
|
|
|
|
// hasBlockingFailure reports whether any failure is on a required field.
|
|
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 storecontract.CapturedEvent) string {
|
|
if len(values) == 0 {
|
|
return fmt.Sprintf("(no fields resolved from capture %q)", 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")
|
|
}
|