Fix/workflow eda audit (#1627)
* fix(webui): accept the merge task action The action whitelist omitted merge, so the dashboard's Merge button on a pr_open task was refused with 400 unknown action. Closes: archie-core-slpoz * fix(workflow): give each parallel branch its own outputs Branches shared the task's Outputs map, so concurrent declared-output writes raced (or were lost when the map started nil). Each branch now writes a copy; join merges new outputs in branch-name order and refuses one written by two branches as a second write. Closes: archie-core-3a7w2 * fix(postgres): dispatch captures oldest first and keep pending ones ListUndispatchedCaptures ordered newest first and listed captures no binding can match (failed HMAC), so sustained intake starved older work and bad signatures held the batch. It is now oldest first and lists only dispatchable captures. Count retention no longer evicts a capture that is still pending for an armed binding. The daemon lists only sources with an enabled workflow, so captures waiting on a disabled one do not hold the oldest-first batch. Closes: archie-core-2et4b, archie-core-o0s0z * fix(ui): refuse renaming a workflow onto an existing id Saving upserts by id, so editing a workflow's id to another workflow's id removed the open one and overwrote the other. The editor now refuses that save and says why. Closes: archie-core-wpoz8 * fix(workflow): reject unknown step settings at save Step factories decoded settings with yaml.Node.Decode, which drops keys it does not know, and settings-less steps ignored settings entirely, so a misspelt key (readOnly for read_only) saved and silently did nothing. Every factory now decodes strictly and settings-less steps refuse settings; ParseDefinition runs the factories, so the save refuses them. Closes: archie-core-8b5nk * fix(postgres): keep a stored capture when its prune fails InsertCapture committed the capture, then returned the prune's error, so intake answered 500 and a sender without a delivery header retried into a duplicate capture and a duplicate dispatch. A failed prune is now logged and the stored capture's id returned. Closes: archie-core-3miuh * fix(crondelivery): fail and advance a job no runner can deliver Router.Run returned nil for a kind with no runner, so the engine reported a successful run while the schedule never advanced and the job fired on every tick. The run now fails with the refusal, which the engine reports, and the schedule advances (a one-shot is removed). The router's own refusal event is gone with it. Closes: archie-core-s6ahh * fix(taskactions): stop the runs of cancelled callees CancelExecution cancelled a caller's waited-on callees in the store, but only the caller's run was stopped, so callee containers ran to the end. CancelExecution now returns the callees it cancelled (nested included) in place of the step ids nobody read, and every cancelling action stops their runs too. Closes: archie-core-9labz * fix(workflow): treat a zero json.Number as false in when Task inputs and step results decode numbers as json.Number, which truthy did not unpack, so when: inputs.count held for a count of 0. Closes: archie-core-uv270 * fix(webui): read a workflow's runs with a workflow-filtered page The workflows page and the canvas run picker filtered the newest 100 tasks of every workflow client-side, so on a busy instance a workflow's older runs were invisible. GET /api/tasks takes a workflow filter, carried through the State Store's Tasks request to the query, and both views ask for the selected workflow's runs. Closes: archie-core-tpiw1 * fix(ui): load the restored workflow into the editor Restore Defaults saved the shipped definition but left the open draft in the editor, still marked unsaved, so a later save re-applied it. The editor now reloads the restored definition. Closes: archie-core-85e7w
This commit is contained in:
36 files changed
+592
-148
No files matched your search
@@ -9,6 +9,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -42,13 +43,14 @@ type EDA struct {
|
||||
q *postgresdb.Queries
|
||||
cipher bindingcipher.BindingCipher
|
||||
notify func(events.Event)
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
// NewEDA builds an EDA store over pool. A nil cipher keeps source secrets in
|
||||
// plaintext; the caller resolves the keyring through
|
||||
// bindingcipher.NewBindingCipher. The pool is owned by the caller, not this store.
|
||||
func NewEDA(pool *pgxpool.Pool, cipher bindingcipher.BindingCipher) *EDA {
|
||||
return &EDA{pool: pool, q: postgresdb.New(pool), cipher: cipher}
|
||||
func NewEDA(pool *pgxpool.Pool, cipher bindingcipher.BindingCipher, log *slog.Logger) *EDA {
|
||||
return &EDA{pool: pool, q: postgresdb.New(pool), cipher: cipher, log: log}
|
||||
}
|
||||
|
||||
// SetNotify wires the change-event callback. nil is a silent no-op, which is
|
||||
@@ -75,7 +77,8 @@ func newRecordID() string { return uuid.NewString() }
|
||||
|
||||
// InsertCapture stores one inbound event verbatim, tagged with the event type
|
||||
// it is identified as on arrival (empty when unidentified). retention and maxEvents are
|
||||
// applied as a prune after the insert.
|
||||
// applied as a prune after the insert; a failed prune is logged, not returned,
|
||||
// because the capture is already stored and a retry would store it again.
|
||||
func (s *EDA) InsertCapture(ctx context.Context, c storecontract.CapturedEvent, retention time.Duration, maxEvents int) (string, error) {
|
||||
received := c.ReceivedAt
|
||||
if received.IsZero() {
|
||||
@@ -108,7 +111,7 @@ func (s *EDA) InsertCapture(ctx context.Context, c storecontract.CapturedEvent,
|
||||
return "", fmt.Errorf("edastore: insert capture: %w", err)
|
||||
}
|
||||
if err := s.pruneCaptures(ctx, id, retention, maxEvents); err != nil {
|
||||
return "", err
|
||||
s.log.Warn("capture stored but not pruned", "capture", id, "err", err)
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
@@ -162,9 +165,10 @@ func (s *EDA) ListCaptures(ctx context.Context, limit int) ([]storecontract.Capt
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ListUndispatchedCaptures returns recent identified captures for the given
|
||||
// sources that some armed binding for their event type has not dispatched yet.
|
||||
// An unidentified capture is never returned, so it never dispatches.
|
||||
// ListUndispatchedCaptures returns the oldest dispatchable, identified
|
||||
// captures for the given sources that some armed binding for their event type
|
||||
// has not dispatched yet. An unidentified capture is never returned, so it
|
||||
// never dispatches.
|
||||
func (s *EDA) ListUndispatchedCaptures(ctx context.Context, sources []string, limit int) ([]storecontract.CapturedEvent, error) {
|
||||
if len(sources) == 0 || limit <= 0 {
|
||||
return nil, nil
|
||||
|
||||
@@ -162,7 +162,15 @@ WHERE c.org_id = (SELECT m.org_id FROM captures AS m WHERE m.id = $1)
|
||||
WHERE k.org_id = (SELECT m.org_id FROM captures AS m WHERE m.id = $1)
|
||||
ORDER BY k.received_at DESC
|
||||
LIMIT $2
|
||||
)
|
||||
)
|
||||
AND NOT (
|
||||
c.event_type <> '' AND (c.authenticated OR c.unsigned)
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM bindings b JOIN mappings m ON m.id = b.mapping
|
||||
WHERE b.org_id = c.org_id AND b.source = c.source AND b.status = 'armed' AND m.event_type = c.event_type
|
||||
AND NOT EXISTS (SELECT 1 FROM binding_dispatches d WHERE d.binding = b.id AND d.delivery = c.delivery)
|
||||
)
|
||||
)
|
||||
`
|
||||
|
||||
type DeleteCapturesBeyondCountParams struct {
|
||||
@@ -171,7 +179,8 @@ type DeleteCapturesBeyondCountParams struct {
|
||||
}
|
||||
|
||||
// Count retention stays within the just-inserted capture's org: one org's
|
||||
// volume must never evict another org's recent captures.
|
||||
// volume must never evict another org's recent captures. It never evicts a
|
||||
// capture ListUndispatchedCaptures would still offer: that is accepted work.
|
||||
func (q *Queries) DeleteCapturesBeyondCount(ctx context.Context, arg DeleteCapturesBeyondCountParams) error {
|
||||
_, err := q.db.Exec(ctx, deleteCapturesBeyondCount, arg.CaptureID, arg.MaxEvents)
|
||||
return err
|
||||
@@ -994,12 +1003,13 @@ SELECT c.id, c.source, c.remote_addr, c.content_type, c.headers, c.body, c.authe
|
||||
FROM captures c
|
||||
WHERE c.org_id = $1 AND c.source = ANY($2::text[])
|
||||
AND c.event_type <> ''
|
||||
AND (c.authenticated OR c.unsigned)
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM bindings b JOIN mappings m ON m.id = b.mapping
|
||||
WHERE b.org_id = c.org_id AND b.source = c.source AND b.status = 'armed' AND m.event_type = c.event_type
|
||||
AND NOT EXISTS (SELECT 1 FROM binding_dispatches d WHERE d.binding = b.id AND d.delivery = c.delivery)
|
||||
)
|
||||
ORDER BY c.received_at DESC
|
||||
ORDER BY c.received_at, c.id
|
||||
LIMIT $3
|
||||
`
|
||||
|
||||
@@ -1009,6 +1019,9 @@ type ListUndispatchedCapturesParams struct {
|
||||
EntryLimit int32
|
||||
}
|
||||
|
||||
// Oldest first, so sustained intake cannot starve accepted work. Only
|
||||
// dispatchable captures (signed, or from an approved unsigned source) are
|
||||
// listed: no binding ever matches the rest, so they would hold the batch.
|
||||
func (q *Queries) ListUndispatchedCaptures(ctx context.Context, arg ListUndispatchedCapturesParams) ([]Capture, error) {
|
||||
rows, err := q.db.Query(ctx, listUndispatchedCaptures, arg.OrgID, arg.Sources, arg.EntryLimit)
|
||||
if err != nil {
|
||||
|
||||
@@ -585,15 +585,17 @@ SELECT id, owner, repo, issue_number, title, status, workflow,
|
||||
FROM tasks
|
||||
WHERE ($1::text IS NULL OR org_id = $1)
|
||||
AND (cardinality($2::text[]) = 0 OR status = ANY($2::text[]))
|
||||
AND ($3::timestamptz IS NULL
|
||||
OR (updated_at, id) < ($3::timestamptz, $4::bigint))
|
||||
AND ($3::text = '' OR workflow = $3::text)
|
||||
AND ($4::timestamptz IS NULL
|
||||
OR (updated_at, id) < ($4::timestamptz, $5::bigint))
|
||||
ORDER BY updated_at DESC, id DESC
|
||||
LIMIT $5
|
||||
LIMIT $6
|
||||
`
|
||||
|
||||
type ListTaskSummariesParams struct {
|
||||
ScopeOrg pgtype.Text
|
||||
Statuses []string
|
||||
Workflow string
|
||||
BeforeUpdated pgtype.Timestamptz
|
||||
BeforeID int64
|
||||
PageLimit int32
|
||||
@@ -638,6 +640,7 @@ func (q *Queries) ListTaskSummaries(ctx context.Context, arg ListTaskSummariesPa
|
||||
rows, err := q.db.Query(ctx, listTaskSummaries,
|
||||
arg.ScopeOrg,
|
||||
arg.Statuses,
|
||||
arg.Workflow,
|
||||
arg.BeforeUpdated,
|
||||
arg.BeforeID,
|
||||
arg.PageLimit,
|
||||
|
||||
@@ -14,16 +14,20 @@ ORDER BY received_at DESC
|
||||
LIMIT $1;
|
||||
|
||||
-- name: ListUndispatchedCaptures :many
|
||||
-- Oldest first, so sustained intake cannot starve accepted work. Only
|
||||
-- dispatchable captures (signed, or from an approved unsigned source) are
|
||||
-- listed: no binding ever matches the rest, so they would hold the batch.
|
||||
SELECT c.id, c.source, c.remote_addr, c.content_type, c.headers, c.body, c.authenticated, c.received_at, c.unsigned, c.event_type, c.org_id, c.workspace_id, c.delivery
|
||||
FROM captures c
|
||||
WHERE c.org_id = @org_id AND c.source = ANY(@sources::text[])
|
||||
AND c.event_type <> ''
|
||||
AND (c.authenticated OR c.unsigned)
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM bindings b JOIN mappings m ON m.id = b.mapping
|
||||
WHERE b.org_id = c.org_id AND b.source = c.source AND b.status = 'armed' AND m.event_type = c.event_type
|
||||
AND NOT EXISTS (SELECT 1 FROM binding_dispatches d WHERE d.binding = b.id AND d.delivery = c.delivery)
|
||||
)
|
||||
ORDER BY c.received_at DESC
|
||||
ORDER BY c.received_at, c.id
|
||||
LIMIT @entry_limit;
|
||||
|
||||
-- name: DeleteCapturesOlderThan :exec
|
||||
@@ -31,7 +35,8 @@ DELETE FROM captures WHERE received_at < $1;
|
||||
|
||||
-- name: DeleteCapturesBeyondCount :exec
|
||||
-- Count retention stays within the just-inserted capture's org: one org's
|
||||
-- volume must never evict another org's recent captures.
|
||||
-- volume must never evict another org's recent captures. It never evicts a
|
||||
-- capture ListUndispatchedCaptures would still offer: that is accepted work.
|
||||
DELETE FROM captures AS c
|
||||
WHERE c.org_id = (SELECT m.org_id FROM captures AS m WHERE m.id = sqlc.arg(capture_id))
|
||||
AND c.id NOT IN (
|
||||
@@ -39,7 +44,15 @@ WHERE c.org_id = (SELECT m.org_id FROM captures AS m WHERE m.id = sqlc.arg(captu
|
||||
WHERE k.org_id = (SELECT m.org_id FROM captures AS m WHERE m.id = sqlc.arg(capture_id))
|
||||
ORDER BY k.received_at DESC
|
||||
LIMIT sqlc.arg(max_events)
|
||||
);
|
||||
)
|
||||
AND NOT (
|
||||
c.event_type <> '' AND (c.authenticated OR c.unsigned)
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM bindings b JOIN mappings m ON m.id = b.mapping
|
||||
WHERE b.org_id = c.org_id AND b.source = c.source AND b.status = 'armed' AND m.event_type = c.event_type
|
||||
AND NOT EXISTS (SELECT 1 FROM binding_dispatches d WHERE d.binding = b.id AND d.delivery = c.delivery)
|
||||
)
|
||||
);
|
||||
|
||||
-- name: InsertMapping :exec
|
||||
INSERT INTO mappings (id, name, source_hint, event_type, fields, org_id)
|
||||
|
||||
@@ -47,6 +47,7 @@ SELECT id, owner, repo, issue_number, title, status, workflow,
|
||||
FROM tasks
|
||||
WHERE (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org))
|
||||
AND (cardinality(@statuses::text[]) = 0 OR status = ANY(@statuses::text[]))
|
||||
AND (@workflow::text = '' OR workflow = @workflow::text)
|
||||
AND (sqlc.narg(before_updated)::timestamptz IS NULL
|
||||
OR (updated_at, id) < (sqlc.narg(before_updated)::timestamptz, @before_id::bigint))
|
||||
ORDER BY updated_at DESC, id DESC
|
||||
|
||||
@@ -282,7 +282,9 @@ func stepParentID(parentID int64) pgtype.Int8 {
|
||||
}
|
||||
|
||||
// CancelExecution cancels the current attempt's open steps and moves the
|
||||
// execution to `to`, in one transaction with events and an audit row.
|
||||
// execution to `to`, in one transaction with events and an audit row, then
|
||||
// cancels the callees it was waiting on. It returns the cancelled callees'
|
||||
// ids, nested callees included.
|
||||
func (s *Store) CancelExecution(ctx context.Context, taskID int64, reason, to string) ([]int64, error) {
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
@@ -344,25 +346,24 @@ func (s *Store) CancelExecution(ctx context.Context, taskID int64, reason, to st
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ids := make([]int64, 0, len(steps))
|
||||
for _, step := range steps {
|
||||
ids = append(ids, step.ID)
|
||||
}
|
||||
|
||||
if err := s.cancelWaitedCallees(ctx, waiting, reason, to); err != nil {
|
||||
return ids, err
|
||||
}
|
||||
return ids, nil
|
||||
return s.cancelWaitedCallees(ctx, waiting, reason, to)
|
||||
}
|
||||
|
||||
// cancelWaitedCallees cancels each callee the caller was waiting on.
|
||||
// Failures are reported; the caller's cancel stands.
|
||||
func (s *Store) cancelWaitedCallees(ctx context.Context, waiting []postgresdb.WaitingCallStepsRow, reason, to string) error {
|
||||
// cancelWaitedCallees cancels each callee the caller was waiting on and
|
||||
// returns the ids of those it cancelled, nested callees included. Failures are
|
||||
// reported; the caller's cancel stands.
|
||||
func (s *Store) cancelWaitedCallees(ctx context.Context, waiting []postgresdb.WaitingCallStepsRow, reason, to string) ([]int64, error) {
|
||||
var cancelled []int64
|
||||
var failures []error
|
||||
for _, call := range waiting {
|
||||
if _, err := s.CancelExecution(ctx, call.CalledExecutionID, reason, to); err != nil {
|
||||
// A callee is returned even when its cancel failed: stopping a run
|
||||
// that already ended does nothing.
|
||||
nested, err := s.CancelExecution(ctx, call.CalledExecutionID, reason, to)
|
||||
if err != nil {
|
||||
failures = append(failures, fmt.Errorf("cancel callee %d: %w", call.CalledExecutionID, err))
|
||||
}
|
||||
cancelled = append(cancelled, call.CalledExecutionID)
|
||||
cancelled = append(cancelled, nested...)
|
||||
}
|
||||
return errors.Join(failures...)
|
||||
return cancelled, errors.Join(failures...)
|
||||
}
|
||||
@@ -737,6 +737,7 @@ func (s *Store) TasksPage(ctx context.Context, page storecontract.TaskPage) ([]w
|
||||
params := postgresdb.ListTaskSummariesParams{
|
||||
ScopeOrg: scopeOrg(ctx),
|
||||
Statuses: append([]string{}, page.Statuses...),
|
||||
Workflow: page.Workflow,
|
||||
PageLimit: int32(min(max(page.Limit, 1), 500)), // clamped
|
||||
}
|
||||
if page.After.ID != 0 {
|
||||
|
||||
Reference in new issue
Block a user