Files
archie/modules/runs/access_test.go
T
sam 098d8cc328 feat(runs): authorize runs, deliveries and intake as the workload principal (#2078)
Trigger dispatch, run start, every run claim, every delivery attempt and
webhook intake now ask the policy chain. Background work acts as the
workload principal; a refusal is final and recorded on the run or the
delivery, and an undecidable chain leaves the work queued for another
attempt. Intake applies the per-source delivery policy after source
verification.
2026-11-22 02:15:05 +11:00

134 lines
5.0 KiB
Go

package runs
import (
"context"
"encoding/json/jsontext"
"errors"
"log/slog"
"slices"
"strings"
"testing"
"time"
"github.com/google/uuid"
"github.com/samcharles93/archie-core/modules/governance"
"github.com/samcharles93/archie-core/modules/runs/internal/store"
)
// asked is one decision the engine requested.
type asked struct {
principal governance.IdentityID
action governance.Action
kind governance.ResourceKind
id string
}
// recordingAccess answers every Authorize with one fixed decision and records
// what was asked.
type recordingAccess struct {
decision governance.Decision
calls []asked
}
func (r *recordingAccess) Authorize(_ context.Context, p governance.Principal, a governance.Action, res governance.Resource, _ governance.AccessContext) governance.Decision {
r.calls = append(r.calls, asked{p.IdentityID, a, res.Kind, res.ID})
return r.decision
}
// queueingStore holds the runs the engine queues.
type queueingStore struct {
fakeWork
queued []store.NewRun
}
func (q *queueingStore) Start(_ context.Context, n store.NewRun) (uuid.UUID, error) {
q.queued = append(q.queued, n)
return uuid.New(), nil
}
// claimsOne hands out one queued run to the worker, as the store does after a
// restart or a retry.
type claimsOne struct {
*queueingStore
run store.Run
}
func (c claimsOne) Claim(context.Context, string, uuid.UUID, time.Duration) (store.Run, bool, error) {
return c.run, true, nil
}
// TestRunsAreAuthorizedAtStartAndAtEveryClaim guards that a run started by a
// trigger and the same run claimed again after a restart or a retry are decided
// by the same live check, as the workload principal, and that a refusal stops
// the work. A policy chain that can't decide starts nothing, finishes nothing
// and leaves the run for another attempt. Without it a revoked grant would
// keep running queued work.
func TestRunsAreAuthorizedAtStartAndAtEveryClaim(t *testing.T) {
workload := asked{governance.WorkloadID, governance.ActionRun, governance.KindWorkflow, "triage"}
tests := []struct {
name string
decision governance.Decision
noPrincipal bool
claim bool
wantCalls []asked
wantQueued int
wantRan bool
wantFinished []string
wantErr error
}{
{name: "a trigger starts its run", decision: governance.Allowed(), wantCalls: []asked{workload}, wantQueued: 1},
{name: "a start with no principal", decision: governance.Allowed(), noPrincipal: true, wantErr: governance.ErrDenied},
{name: "a revoked start", decision: governance.DeniedAt(governance.LevelOrg, []string{"org-workload"}), wantCalls: []asked{workload}, wantErr: governance.ErrDenied},
{name: "a start the chain can't decide", decision: governance.Unavailable(), wantCalls: []asked{workload}, wantErr: governance.ErrChainUnavailable},
{name: "a claim after a restart", decision: governance.Allowed(), claim: true, wantCalls: []asked{workload}, wantRan: true, wantFinished: []string{store.StateSucceeded}},
{name: "a claim after revocation", decision: governance.DeniedAt(governance.LevelOrg, []string{"org-workload"}), claim: true, wantCalls: []asked{workload}, wantFinished: []string{store.StateFailed}},
{name: "a claim the chain can't decide", decision: governance.Unavailable(), claim: true, wantCalls: []asked{workload}, wantErr: governance.ErrChainUnavailable},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
work := &queueingStore{}
access := &recordingAccess{decision: tt.decision}
engine := newEngine(nil, work, slog.New(slog.DiscardHandler), Options{})
engine.queue = work
engine.Use(Deps{Access: access})
ran := false
useWorkflow(engine, func(*Context, jsontext.Value) (jsontext.Value, error) {
ran = true
return jsontext.Value(`"done"`), nil
})
var err error
if tt.claim {
engine.work = claimsOne{work, store.Run{ID: uuid.New(), OrgID: "org", Workflow: "triage", Version: 1, Input: jsontext.Value(`null`)}}
_, err = engine.claimAndExecute(t.Context())
} else {
ctx := t.Context()
if !tt.noPrincipal {
ctx = governance.WithPrincipal(ctx, governance.WorkloadPrincipal(governance.DefaultOrgID))
}
_, err = engine.Start(ctx, StartRequest{OrgID: "org", Workflow: "triage", Key: "event:trigger"})
}
if !errors.Is(err, tt.wantErr) {
t.Fatalf("error = %v, want %v", err, tt.wantErr)
}
if !slices.Equal(access.calls, tt.wantCalls) {
t.Fatalf("decisions asked = %v, want %v", access.calls, tt.wantCalls)
}
if len(work.queued) != tt.wantQueued {
t.Fatalf("runs queued = %d, want %d", len(work.queued), tt.wantQueued)
}
if ran != tt.wantRan {
t.Fatalf("workflow ran = %v, want %v", ran, tt.wantRan)
}
if !slices.Equal(work.finished, tt.wantFinished) {
t.Fatalf("finished = %v, want %v", work.finished, tt.wantFinished)
}
if slices.Contains(tt.wantFinished, store.StateFailed) && !strings.Contains(work.errors[0], "not permitted") {
t.Fatalf("the run's trail says %q, want the refusal", work.errors[0])
}
})
}
}