feat(eda): capture schedule firings on a reserved source for bindings
This commit is contained in:
9 files changed
+170
-1
No files matched your search
@@ -293,6 +293,11 @@ expressions that limit which files the rule applies to.
|
||||
else runs `triage`, which classifies it and hands off.
|
||||
- **Event bindings**: a webhook source mapped onto a workflow and its inputs
|
||||
(Events page).
|
||||
- **Schedules**: each firing is captured on the reserved `schedule` source as
|
||||
a `Schedule fired` event (`schedule_id`, `name`, `scheduled_for`,
|
||||
`fired_at`). A schedule trigger is a binding on that source using the
|
||||
`schedule-fired` mapping, filtered to one `schedule_id`. A firing starts at
|
||||
most one task per binding.
|
||||
- **Playbooks**: a trigger (issue kind and labels) and actions that dispatch
|
||||
workflows.
|
||||
- **Other workflows**: `workflow.call` (a child run), `workflow.handoff` (the
|
||||
|
||||
@@ -36,6 +36,9 @@ func ValidatePath(path string) error {
|
||||
if path == "" || len(path) > MaxPathLen || path == "." || path == ".." {
|
||||
return ErrInvalidPath
|
||||
}
|
||||
if path == SchedulePath {
|
||||
return ErrReservedPath
|
||||
}
|
||||
for i := range len(path) {
|
||||
if !unreserved(path[i]) {
|
||||
return ErrInvalidPath
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package source
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
// SchedulePath is the source a schedule's firing is captured on. It is
|
||||
// reserved: no endpoint receives on it and no operator source may take it, so
|
||||
// only the scheduler writes its captures.
|
||||
const SchedulePath = "schedule"
|
||||
|
||||
// ScheduleFiredType and ScheduleFiredMapping name the built-in event type and
|
||||
// mapping that identify and map every capture on SchedulePath. A schedule
|
||||
// trigger is a binding on SchedulePath using ScheduleFiredMapping, filtered to
|
||||
// one schedule_id.
|
||||
const (
|
||||
ScheduleFiredType = "schedule-fired"
|
||||
ScheduleFiredMapping = "schedule-fired"
|
||||
)
|
||||
|
||||
// ScheduleFired is one firing of a schedule: the payload of its capture.
|
||||
type ScheduleFired struct {
|
||||
ScheduleID string `json:"schedule_id"`
|
||||
Name string `json:"name"`
|
||||
ScheduledFor time.Time `json:"scheduled_for"`
|
||||
FiredAt time.Time `json:"fired_at"`
|
||||
}
|
||||
|
||||
// Delivery names the firing of one slot. A retried firing of the slot shares
|
||||
// it, so a binding starts at most one task per slot.
|
||||
func (f ScheduleFired) Delivery() string {
|
||||
return f.ScheduleID + "@" + f.ScheduledFor.UTC().Format(time.RFC3339Nano)
|
||||
}
|
||||
|
||||
// Body is the capture payload.
|
||||
func (f ScheduleFired) Body() ([]byte, error) {
|
||||
f.ScheduledFor, f.FiredAt = f.ScheduledFor.UTC(), f.FiredAt.UTC()
|
||||
return json.Marshal(f)
|
||||
}
|
||||
@@ -31,6 +31,8 @@ var (
|
||||
ErrInvalidPath = errors.New("source: path must be 1-128 URL-safe characters (A-Z a-z 0-9 - . _ ~), not a dot segment")
|
||||
// ErrInvalidHeader refuses a delivery header that is not an HTTP field name.
|
||||
ErrInvalidHeader = errors.New("source: delivery header must be an HTTP header name")
|
||||
// ErrReservedPath refuses a path the scheduler captures on.
|
||||
ErrReservedPath = errors.New("source: path is reserved")
|
||||
// ErrSigningTransition refuses an approval with no pending request.
|
||||
ErrSigningTransition = errors.New("source: signing transition rejected")
|
||||
)
|
||||
|
||||
@@ -80,6 +80,12 @@ func (rc *Receiver) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
path := r.PathValue("source")
|
||||
// Only the scheduler captures on the schedule source; an outside post
|
||||
// there would pose as a schedule firing.
|
||||
if path == source.SchedulePath {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
// Rate limit by remote address; the source segment is attacker-chosen.
|
||||
if rc.Limiter != nil && !rc.Limiter.Allow(remoteAddrHost(r)) {
|
||||
|
||||
@@ -1,8 +1,16 @@
|
||||
package captureintake
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/source"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
)
|
||||
|
||||
func TestFormPayload(t *testing.T) {
|
||||
@@ -25,3 +33,37 @@ func TestFormPayload(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type recordingCaptures struct{ sources []string }
|
||||
|
||||
func (c *recordingCaptures) InsertCapture(_ context.Context, e storecontract.CapturedEvent, _ time.Duration, _ int) (string, error) {
|
||||
c.sources = append(c.sources, e.Source)
|
||||
return "id", nil
|
||||
}
|
||||
|
||||
func (c *recordingCaptures) ListCaptures(context.Context, int) ([]storecontract.CapturedEvent, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// An outside post must never land on the schedule source, where it would
|
||||
// pose as a schedule firing.
|
||||
func TestReceiverReservesScheduleSource(t *testing.T) {
|
||||
tests := []struct {
|
||||
path string
|
||||
status int
|
||||
stored bool
|
||||
}{
|
||||
{source.SchedulePath, http.StatusNotFound, false},
|
||||
{"hooks", http.StatusAccepted, true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
captures := &recordingCaptures{}
|
||||
mux := http.NewServeMux()
|
||||
(&Receiver{Captures: captures}).Register(mux)
|
||||
rec := httptest.NewRecorder()
|
||||
mux.ServeHTTP(rec, httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/"+tt.path, strings.NewReader(`{"schedule_id":"x"}`)))
|
||||
if rec.Code != tt.status || (len(captures.sources) == 1) != tt.stored {
|
||||
t.Errorf("%s: status %d stored %v, want %d %v", tt.path, rec.Code, captures.sources, tt.status, tt.stored)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
-- A schedule's firing is captured on the reserved "schedule" source. This
|
||||
-- built-in event type identifies every capture there, and the built-in mapping
|
||||
-- hands a binding the firing's fields, so a schedule trigger is an ordinary
|
||||
-- binding on that source filtered to one schedule_id.
|
||||
|
||||
-- +goose Up
|
||||
INSERT INTO event_types (id, source, name, rule, schema, org_id, workspace_id)
|
||||
VALUES ('schedule-fired', 'schedule', 'Schedule fired', '{"headers":[],"payload":[]}',
|
||||
'{"schedule_id":"string","name":"string","scheduled_for":"string","fired_at":"string"}',
|
||||
'org-sys', 'default')
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
INSERT INTO mappings (id, name, source_hint, event_type, fields, org_id, workspace_id)
|
||||
VALUES ('schedule-fired', 'Schedule fired', 'schedule', 'schedule-fired',
|
||||
'[{"name":"schedule_id","path":"schedule_id","type":"string","required":true},'
|
||||
'{"name":"name","path":"name","type":"string","required":false},'
|
||||
'{"name":"scheduled_for","path":"scheduled_for","type":"string","required":true},'
|
||||
'{"name":"fired_at","path":"fired_at","type":"string","required":true}]',
|
||||
'org-sys', 'default')
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
-- +goose Down
|
||||
DELETE FROM mappings WHERE id = 'schedule-fired';
|
||||
DELETE FROM event_types WHERE id = 'schedule-fired';
|
||||
@@ -0,0 +1,47 @@
|
||||
// Package schedulecapture records a schedule's firing as a capture on the
|
||||
// reserved schedule source, where armed bindings pick it up like any other
|
||||
// event.
|
||||
package schedulecapture
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/source"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
)
|
||||
|
||||
// Firer captures schedule firings. Retention and MaxEvents are the capture
|
||||
// table's prune-on-write bounds, as config.CaptureConfig sets for webhooks.
|
||||
type Firer struct {
|
||||
Captures storecontract.CaptureStore
|
||||
Retention time.Duration
|
||||
MaxEvents int
|
||||
}
|
||||
|
||||
// Fire records one firing and returns its capture id. The tasks it starts are
|
||||
// the binding dispatches recorded against that id. A retried firing of the
|
||||
// same slot shares its delivery, so no binding starts a second task for it.
|
||||
func (f Firer) Fire(ctx context.Context, fired source.ScheduleFired) (string, error) {
|
||||
if f.Captures == nil {
|
||||
return "", errors.New("schedulecapture: no capture store")
|
||||
}
|
||||
body, err := fired.Body()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("schedulecapture: encode firing: %w", err)
|
||||
}
|
||||
id, err := f.Captures.InsertCapture(ctx, storecontract.CapturedEvent{
|
||||
ReceivedAt: fired.FiredAt.UTC(),
|
||||
Source: source.SchedulePath,
|
||||
ContentType: "application/json",
|
||||
Body: string(body),
|
||||
Authenticated: true,
|
||||
Delivery: fired.Delivery(),
|
||||
}, f.Retention, f.MaxEvents)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("schedulecapture: capture firing of %q: %w", fired.ScheduleID, err)
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
@@ -60,7 +60,7 @@ func (s *Server) handleSourceCreate(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
src, err := source.New(req.Path)
|
||||
if errors.Is(err, source.ErrInvalidPath) {
|
||||
if errors.Is(err, source.ErrInvalidPath) || errors.Is(err, source.ErrReservedPath) {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user