feat(runs,events,ui): identify workflows by immutable UUID (#2125)
This commit is contained in:
46 files changed
+1284
-512
No files matched your search
@@ -128,7 +128,11 @@ func (s *startRecorder) Start(ctx context.Context, _ runs.StartRequest) (uuid.UU
|
||||
return uuid.New(), s.err
|
||||
}
|
||||
|
||||
func (*startRecorder) CheckTrigger(context.Context, string, string) error { return nil }
|
||||
func (*startRecorder) CheckTrigger(context.Context, string, uuid.UUID) error { return nil }
|
||||
|
||||
func (*startRecorder) WorkflowName(context.Context, string, uuid.UUID) (string, error) {
|
||||
return "triage", nil
|
||||
}
|
||||
|
||||
// TestTriggersStartRunsAsTheWorkload guards that dispatching an event never
|
||||
// starts a run without a principal: the workload principal acts, a policy
|
||||
@@ -152,7 +156,7 @@ func TestTriggersStartRunsAsTheWorkload(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
starts := &startRecorder{err: tt.err}
|
||||
m := &Module{runs: starts, log: slog.New(slog.DiscardHandler)}
|
||||
_, why, err := m.startRun(t.Context(), store.Event{ID: uuid.New(), OrgID: "default"}, store.Trigger{Name: "t", Workflow: "triage"}, nil)
|
||||
_, why, err := m.startRun(t.Context(), store.Event{ID: uuid.New(), OrgID: "default"}, store.Trigger{Name: "t", WorkflowID: uuid.New()}, nil)
|
||||
var outcome string
|
||||
if why != nil {
|
||||
outcome = why.outcome
|
||||
|
||||
+23
-16
@@ -241,12 +241,12 @@ func (m *Module) setHMAC(src *store.Source, set SourceSettings, stored *store.So
|
||||
type putTriggerInput struct {
|
||||
Name string `path:"name" doc:"Lowercase letters, digits and hyphens"`
|
||||
Body struct {
|
||||
SourceID string `json:"source_id,omitzero" format:"uuid" doc:"The source whose events fire it; none when every is set"`
|
||||
EventType string `json:"event_type,omitzero" doc:"The event type that fires it; fixed when every is set"`
|
||||
Filter map[string]string `json:"filter,omitzero" doc:"Event fields that must match exactly"`
|
||||
Workflow string `json:"workflow" doc:"The workflow each match runs"`
|
||||
Disabled bool `json:"disabled,omitzero"`
|
||||
Every string `json:"every,omitzero" doc:"Fire on an interval instead of on events, such as 1h" example:"1h"`
|
||||
SourceID string `json:"source_id,omitzero" format:"uuid" doc:"The source whose events fire it; none when every is set"`
|
||||
EventType string `json:"event_type,omitzero" doc:"The event type that fires it; fixed when every is set"`
|
||||
Filter map[string]string `json:"filter,omitzero" doc:"Event fields that must match exactly"`
|
||||
WorkflowID string `json:"workflow_id" format:"uuid" doc:"The workflow each match runs"`
|
||||
Disabled bool `json:"disabled,omitzero"`
|
||||
Every string `json:"every,omitzero" doc:"Fire on an interval instead of on events, such as 1h" example:"1h"`
|
||||
// EventTypeID restricts the trigger to events identified as that event
|
||||
// type, and MappingID reads their payload for the run.
|
||||
EventTypeID string `json:"event_type_id,omitzero" format:"uuid" doc:"Fire only for events identified as this event type of the source"`
|
||||
@@ -283,14 +283,18 @@ func (m *Module) putTrigger(ctx context.Context, in *putTriggerInput) (*httpapi.
|
||||
// this trigger to its own.
|
||||
req.Filter = map[string]string{"trigger": in.Name}
|
||||
}
|
||||
if !validName.MatchString(in.Name) || (req.SourceID == "" && !every.Valid) || (req.EventType == "" && req.EventTypeID == "") || req.Workflow == "" {
|
||||
if !validName.MatchString(in.Name) || (req.SourceID == "" && !every.Valid) || (req.EventType == "" && req.EventTypeID == "") || req.WorkflowID == "" {
|
||||
return nil, huma.Error400BadRequest("a trigger needs a lowercase name, a workflow, and either a source and event type or an every interval")
|
||||
}
|
||||
sourceID, err := m.triggerSource(ctx, req.SourceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
typeID, mappingID, err := m.checkTargets(ctx, sourceID, req.EventType, req.Workflow, req.EventTypeID, req.MappingID)
|
||||
workflow, err := uuid.Parse(req.WorkflowID)
|
||||
if err != nil {
|
||||
return nil, huma.Error400BadRequest("a workflow id is a UUID", &huma.ErrorDetail{Location: "body.workflow_id", Message: "a workflow id is a UUID"})
|
||||
}
|
||||
typeID, mappingID, err := m.checkTargets(ctx, sourceID, req.EventType, workflow, req.EventTypeID, req.MappingID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -304,7 +308,7 @@ func (m *Module) putTrigger(ctx context.Context, in *putTriggerInput) (*httpapi.
|
||||
}
|
||||
if err := m.store.UpsertTrigger(ctx, store.Trigger{
|
||||
OrgID: m.orgID, Name: in.Name, SourceID: sourceID, EventType: req.EventType,
|
||||
Filter: filter, Workflow: req.Workflow, Enabled: !req.Disabled, Approved: approved,
|
||||
Filter: filter, WorkflowID: workflow, Enabled: !req.Disabled, Approved: approved,
|
||||
EventTypeID: typeID, MappingID: mappingID, ScheduleEverySeconds: every,
|
||||
}); err != nil {
|
||||
return nil, huma.Error500InternalServerError("couldn't save the trigger", err)
|
||||
@@ -337,13 +341,13 @@ func (m *Module) triggerSource(ctx context.Context, raw string) (uuid.NullUUID,
|
||||
|
||||
// checkTargets refuses a trigger whose workflow can't be started or whose
|
||||
// event type, mapping or connector event type doesn't exist.
|
||||
func (m *Module) checkTargets(ctx context.Context, sourceID uuid.NullUUID, eventType, workflow, eventTypeID, mappingID string) (typeID, mapID uuid.NullUUID, err error) {
|
||||
func (m *Module) checkTargets(ctx context.Context, sourceID uuid.NullUUID, eventType string, workflow uuid.UUID, eventTypeID, mappingID string) (typeID, mapID uuid.NullUUID, err error) {
|
||||
if typeID, mapID, err = m.checkBinding(ctx, sourceID, eventTypeID, mappingID); err != nil {
|
||||
return typeID, mapID, err
|
||||
}
|
||||
if err := m.runs.CheckTrigger(ctx, m.orgID, workflow); err != nil {
|
||||
if refused, ok := errors.AsType[*runs.InvalidError](err); ok {
|
||||
return typeID, mapID, huma.Error400BadRequest(refused.Problems[0].Message, &huma.ErrorDetail{Location: "body.workflow", Message: refused.Problems[0].Message})
|
||||
return typeID, mapID, huma.Error400BadRequest(refused.Problems[0].Message, &huma.ErrorDetail{Location: "body.workflow_id", Message: refused.Problems[0].Message})
|
||||
}
|
||||
return typeID, mapID, huma.Error500InternalServerError("couldn't check the workflow", err)
|
||||
}
|
||||
@@ -489,7 +493,6 @@ type proposedTrigger struct {
|
||||
Source string `json:"source" doc:"The source's label"`
|
||||
EventType string `json:"event_type"`
|
||||
Filter jsontext.Value `json:"filter"`
|
||||
Workflow string `json:"workflow"`
|
||||
}
|
||||
|
||||
func (m *Module) groups(ctx context.Context, _ *struct{}) (*httpapi.Reply[[]groupView], error) {
|
||||
@@ -583,9 +586,11 @@ type triggerView struct {
|
||||
Source string `json:"source,omitempty"`
|
||||
EventType string `json:"event_type"`
|
||||
Filter map[string]string `json:"filter"`
|
||||
Workflow string `json:"workflow"`
|
||||
Enabled bool `json:"enabled"`
|
||||
Every string `json:"every,omitempty"`
|
||||
// WorkflowID is the workflow the trigger starts, and Workflow its name.
|
||||
WorkflowID uuid.UUID `json:"workflow_id"`
|
||||
Workflow string `json:"workflow"`
|
||||
Enabled bool `json:"enabled"`
|
||||
Every string `json:"every,omitempty"`
|
||||
// Status is pending_approval, armed or paused.
|
||||
Status string `json:"status" enum:"pending_approval,armed,paused"`
|
||||
EventTypeID string `json:"event_type_id,omitempty"`
|
||||
@@ -608,7 +613,7 @@ func statusOf(t store.Trigger) string {
|
||||
// viewOfTrigger is the API's form of a stored trigger. Unsigned is set by the
|
||||
// caller, which knows the sources.
|
||||
func viewOfTrigger(t store.Trigger) (triggerView, error) {
|
||||
view := triggerView{Name: t.Name, EventType: t.EventType, Workflow: t.Workflow, Enabled: t.Enabled, Filter: map[string]string{}, Status: statusOf(t)}
|
||||
view := triggerView{Name: t.Name, EventType: t.EventType, WorkflowID: t.WorkflowID, Enabled: t.Enabled, Filter: map[string]string{}, Status: statusOf(t)}
|
||||
if len(t.Filter) > 0 {
|
||||
if err := json.Unmarshal(t.Filter, &view.Filter); err != nil {
|
||||
return view, fmt.Errorf("filter of %s: %w", t.Name, err)
|
||||
@@ -635,6 +640,7 @@ func (m *Module) viewOfTrigger(ctx context.Context, t store.Trigger) (triggerVie
|
||||
if err != nil {
|
||||
return view, err
|
||||
}
|
||||
view.Workflow = m.workflowName(ctx, t.WorkflowID)
|
||||
if t.SourceID.Valid {
|
||||
if src, err := m.store.Source(ctx, m.orgID, t.SourceID.UUID); err == nil {
|
||||
view.Source, view.Unsigned = src.Label, src.Verification == store.None
|
||||
@@ -670,6 +676,7 @@ func (m *Module) triggers(ctx context.Context, _ *struct{}) (*httpapi.Reply[[]tr
|
||||
return nil, huma.Error500InternalServerError("couldn't read the filter of " + t.Name)
|
||||
}
|
||||
view.Source, view.Unsigned = src.Label, src.Verification == store.None
|
||||
view.Workflow = m.workflowName(ctx, t.WorkflowID)
|
||||
out = append(out, view)
|
||||
}
|
||||
return httpapi.OK(out), nil
|
||||
|
||||
@@ -80,15 +80,16 @@ type TriggerSpec struct {
|
||||
SourceID uuid.UUID
|
||||
EventType string
|
||||
Filter map[string]string
|
||||
Workflow string
|
||||
// WorkflowID is the workflow the trigger starts.
|
||||
WorkflowID uuid.UUID
|
||||
}
|
||||
|
||||
// SaveTrigger creates or replaces an event trigger.
|
||||
func (m *Module) SaveTrigger(ctx context.Context, t TriggerSpec) error {
|
||||
if !validName.MatchString(t.Name) || t.SourceID == uuid.Nil || t.EventType == "" || t.Workflow == "" {
|
||||
if !validName.MatchString(t.Name) || t.SourceID == uuid.Nil || t.EventType == "" || t.WorkflowID == uuid.Nil {
|
||||
return fmt.Errorf("a trigger needs a lowercase name, a source, an event type and a workflow")
|
||||
}
|
||||
if err := m.runs.CheckTrigger(ctx, m.orgID, t.Workflow); err != nil {
|
||||
if err := m.runs.CheckTrigger(ctx, m.orgID, t.WorkflowID); err != nil {
|
||||
return err
|
||||
}
|
||||
filter, err := json.Marshal(t.Filter)
|
||||
@@ -97,6 +98,6 @@ func (m *Module) SaveTrigger(ctx context.Context, t TriggerSpec) error {
|
||||
}
|
||||
return m.store.UpsertTrigger(ctx, store.Trigger{
|
||||
OrgID: m.orgID, Name: t.Name, SourceID: uuid.NullUUID{UUID: t.SourceID, Valid: true}, EventType: t.EventType,
|
||||
Filter: filter, Workflow: t.Workflow, Enabled: true, Approved: true,
|
||||
Filter: filter, WorkflowID: t.WorkflowID, Enabled: true, Approved: true,
|
||||
})
|
||||
}
|
||||
@@ -51,7 +51,8 @@ type Connectors interface {
|
||||
// start its workflow.
|
||||
type Runs interface {
|
||||
Start(ctx context.Context, req runs.StartRequest) (uuid.UUID, error)
|
||||
CheckTrigger(ctx context.Context, orgID, workflow string) error
|
||||
CheckTrigger(ctx context.Context, orgID string, workflow uuid.UUID) error
|
||||
WorkflowName(ctx context.Context, orgID string, workflow uuid.UUID) (string, error)
|
||||
}
|
||||
|
||||
// Deliveries recognises the echo of a message Archie sent. It returns the run
|
||||
@@ -716,15 +717,15 @@ func (m *Module) noteUnmatched(ctx context.Context, tx *sqlx.Tx, e store.Event)
|
||||
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,
|
||||
OrgID: e.OrgID, WorkflowID: t.WorkflowID, 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
|
||||
return uuid.Nil, &noRunReason{outcome: store.OutcomeDenied, Code: reasonAccessDenied, WorkflowID: t.WorkflowID, Workflow: m.workflowName(ctx, t.WorkflowID), 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
|
||||
m.log.InfoContext(ctx, "trigger started no run: the workflow is disabled", "event", e.ID, "trigger", t.Name, "workflow", t.WorkflowID)
|
||||
return uuid.Nil, &noRunReason{outcome: store.OutcomeDisabled, Code: reasonWorkflowDisabled, WorkflowID: t.WorkflowID, Workflow: m.workflowName(ctx, t.WorkflowID)}, nil
|
||||
}
|
||||
if err != nil {
|
||||
return uuid.Nil, nil, fmt.Errorf("start run for trigger %q: %w", t.Name, err)
|
||||
@@ -780,8 +781,21 @@ type noRunReason struct {
|
||||
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"`
|
||||
// WorkflowID is the workflow the trigger starts, and Workflow its name
|
||||
// when the dispatch was refused.
|
||||
WorkflowID uuid.UUID `json:"workflow_id,omitzero"`
|
||||
Workflow string `json:"workflow,omitempty"`
|
||||
Detail string `json:"detail,omitempty"`
|
||||
}
|
||||
|
||||
// workflowName returns the name of a workflow, or its id when the name can't
|
||||
// be read.
|
||||
func (m *Module) workflowName(ctx context.Context, id uuid.UUID) string {
|
||||
name, err := m.runs.WorkflowName(ctx, m.orgID, id)
|
||||
if err != nil {
|
||||
return id.String()
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
// inputFor builds the run input for trigger t. For a trigger with a mapping,
|
||||
|
||||
@@ -400,7 +400,7 @@ func (s *Store) SetTriggerState(ctx context.Context, orgID, name string, approve
|
||||
func (s *Store) Trigger(ctx context.Context, orgID, name string) (Trigger, error) {
|
||||
var t Trigger
|
||||
err := s.db.GetContext(ctx, &t, `
|
||||
SELECT org_id, name, source, event_type, filter, workflow, enabled, approved, event_type_id, mapping_id,
|
||||
SELECT org_id, name, source_id, event_type, filter, workflow_id, enabled, approved, event_type_id, mapping_id,
|
||||
schedule_every_s, schedule_since
|
||||
FROM events.triggers WHERE org_id = $1 AND name = $2`, orgID, name)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
|
||||
@@ -127,11 +127,11 @@ type Trigger struct {
|
||||
OrgID string `db:"org_id"`
|
||||
Name string `db:"name"`
|
||||
// SourceID is null for a scheduled trigger, which has no source.
|
||||
SourceID uuid.NullUUID `db:"source_id"`
|
||||
EventType string `db:"event_type"`
|
||||
Filter jsontext.Value `db:"filter"`
|
||||
Workflow string `db:"workflow"`
|
||||
Enabled bool `db:"enabled"`
|
||||
SourceID uuid.NullUUID `db:"source_id"`
|
||||
EventType string `db:"event_type"`
|
||||
Filter jsontext.Value `db:"filter"`
|
||||
WorkflowID uuid.UUID `db:"workflow_id"`
|
||||
Enabled bool `db:"enabled"`
|
||||
// ScheduleEverySeconds is the interval a scheduled trigger fires on; it
|
||||
// is null for a trigger that fires on events.
|
||||
ScheduleEverySeconds sql.NullInt64 `db:"schedule_every_s"`
|
||||
@@ -344,7 +344,7 @@ func SetEventPayload(ctx context.Context, tx *sqlx.Tx, e Event) error {
|
||||
func Triggers(ctx context.Context, tx *sqlx.Tx, orgID string, sourceID uuid.NullUUID, eventType string, typeID uuid.NullUUID) ([]Trigger, error) {
|
||||
var out []Trigger
|
||||
if err := tx.SelectContext(ctx, &out, `
|
||||
SELECT org_id, name, source_id, event_type, filter, workflow, enabled, approved, event_type_id, mapping_id
|
||||
SELECT org_id, name, source_id, event_type, filter, workflow_id, enabled, approved, event_type_id, mapping_id
|
||||
FROM events.triggers
|
||||
WHERE org_id = $1 AND source_id IS NOT DISTINCT FROM $2 AND (event_type = $3 OR event_type = '') AND enabled AND approved
|
||||
AND (event_type_id IS NULL OR event_type_id = $4)
|
||||
@@ -395,14 +395,14 @@ func RecordNoRun(ctx context.Context, tx *sqlx.Tx, eventID uuid.UUID, trigger, o
|
||||
// UpsertTrigger creates or replaces a trigger.
|
||||
func (s *Store) UpsertTrigger(ctx context.Context, t Trigger) error {
|
||||
if _, err := s.db.NamedExecContext(ctx, `
|
||||
INSERT INTO events.triggers (org_id, name, source_id, event_type, filter, workflow, enabled, approved,
|
||||
INSERT INTO events.triggers (org_id, name, source_id, event_type, filter, workflow_id, enabled, approved,
|
||||
event_type_id, mapping_id, schedule_every_s, schedule_since)
|
||||
VALUES (:org_id, :name, :source_id, :event_type, :filter, :workflow, :enabled, :approved,
|
||||
VALUES (:org_id, :name, :source_id, :event_type, :filter, :workflow_id, :enabled, :approved,
|
||||
:event_type_id, :mapping_id,
|
||||
CAST(:schedule_every_s AS integer), CASE WHEN CAST(:schedule_every_s AS integer) IS NULL THEN NULL ELSE now() END)
|
||||
ON CONFLICT (org_id, name) DO UPDATE
|
||||
SET source_id = EXCLUDED.source_id, event_type = EXCLUDED.event_type, filter = EXCLUDED.filter,
|
||||
workflow = EXCLUDED.workflow, enabled = EXCLUDED.enabled, approved = EXCLUDED.approved,
|
||||
workflow_id = EXCLUDED.workflow_id, enabled = EXCLUDED.enabled, approved = EXCLUDED.approved,
|
||||
event_type_id = EXCLUDED.event_type_id, mapping_id = EXCLUDED.mapping_id,
|
||||
schedule_since = CASE
|
||||
WHEN EXCLUDED.schedule_every_s IS NULL THEN NULL
|
||||
@@ -419,7 +419,7 @@ func (s *Store) UpsertTrigger(ctx context.Context, t Trigger) error {
|
||||
func (s *Store) ScheduledTriggers(ctx context.Context, orgID string) ([]Trigger, error) {
|
||||
var out []Trigger
|
||||
if err := s.db.SelectContext(ctx, &out, `
|
||||
SELECT org_id, name, source_id, event_type, filter, workflow, enabled, approved, event_type_id, mapping_id,
|
||||
SELECT org_id, name, source_id, event_type, filter, workflow_id, enabled, approved, event_type_id, mapping_id,
|
||||
schedule_every_s, schedule_since
|
||||
FROM events.triggers
|
||||
WHERE org_id = $1 AND enabled AND approved AND schedule_every_s IS NOT NULL ORDER BY name`, orgID); err != nil {
|
||||
@@ -503,7 +503,7 @@ func (s *Store) RunsForEvent(ctx context.Context, orgID string, id uuid.UUID) ([
|
||||
func (s *Store) AllTriggers(ctx context.Context, orgID string) ([]Trigger, error) {
|
||||
var out []Trigger
|
||||
if err := s.db.SelectContext(ctx, &out, `
|
||||
SELECT org_id, name, source_id, event_type, filter, workflow, enabled, approved, event_type_id, mapping_id,
|
||||
SELECT org_id, name, source_id, event_type, filter, workflow_id, enabled, approved, event_type_id, mapping_id,
|
||||
schedule_every_s, schedule_since
|
||||
FROM events.triggers WHERE org_id = $1 ORDER BY name`, orgID); err != nil {
|
||||
return nil, fmt.Errorf("list triggers: %w", err)
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
package events
|
||||
|
||||
import "context"
|
||||
import (
|
||||
"context"
|
||||
|
||||
// WorkflowTriggers names the triggers that start workflow.
|
||||
func (m *Module) WorkflowTriggers(ctx context.Context, workflow string) ([]string, error) {
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// WorkflowTriggers returns the names of the triggers that start the workflow.
|
||||
func (m *Module) WorkflowTriggers(ctx context.Context, workflow uuid.UUID) ([]string, error) {
|
||||
triggers, err := m.store.AllTriggers(ctx, m.orgID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var names []string
|
||||
for _, t := range triggers {
|
||||
if t.Workflow == workflow {
|
||||
if t.WorkflowID == workflow {
|
||||
names = append(names, t.Name)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user