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
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user