From 7f1aeb19276402ab8d279bf85136d0648ede889e Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 4 Oct 2026 18:55:37 +1100 Subject: [PATCH] feat(daemon): a workflow that names an identity runs as it; the dispatcher needs run on the workflow --- internal/daemon/daemon.go | 5 ++ internal/daemon/workflow_identity.go | 82 ++++++++++++++++++++++ internal/daemon/workflow_identity_test.go | 82 ++++++++++++++++++++++ internal/domain/workflow/task/interface.go | 4 ++ 4 files changed, 173 insertions(+) create mode 100644 internal/daemon/workflow_identity.go create mode 100644 internal/daemon/workflow_identity_test.go diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index b84e1f7e7..9f8d56f26 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -1278,6 +1278,11 @@ func (d *Daemon) reconcilePRs(ctx context.Context) { } func (d *Daemon) process(ctx context.Context, task *workflow.Task) { + // The workflow may name the identity the run acts as, which everything + // below is chosen by, so it is settled before anything else. + if !d.adoptWorkflowIdentity(ctx, task) { + return + } // Register first so the task can be stopped during setup. ctx, finished := d.running.begin(ctx, task.ID, task.Identity) defer finished() diff --git a/internal/daemon/workflow_identity.go b/internal/daemon/workflow_identity.go new file mode 100644 index 000000000..c753b78d7 --- /dev/null +++ b/internal/daemon/workflow_identity.go @@ -0,0 +1,82 @@ +package daemon + +import ( + "context" + + "github.com/samcharles93/archie-core/internal/domain/access" + "github.com/samcharles93/archie-core/internal/domain/identity" + "github.com/samcharles93/archie-core/internal/domain/workflow" + workflowtask "github.com/samcharles93/archie-core/internal/domain/workflow/task" + "github.com/samcharles93/archie-core/internal/taskstate" +) + +// adoptWorkflowIdentity pins the task's workflow and, when the workflow names +// an identity, makes the run act as it. The identity that dispatched the run +// (a binding's, a poll's, or a calling workflow's) needs run on the workflow. +// It runs before anything that depends on the identity, and reports whether +// processing may continue; a refusal parks the task. +func (d *Daemon) adoptWorkflowIdentity(ctx context.Context, task *workflow.Task) bool { + if err := d.pinWorkflowDefinition(ctx, task); err != nil { + d.Log.Error("pin workflow definition failed", "task", task.ID, "err", err) + d.parkRunningTask(ctx, task.ID, "pin workflow definition: "+err.Error(), pinParkClass(err)) + return false + } + iface, err := workflowtask.ParseWorkflowInterface(task.WorkflowDefinitionYAML) + if err != nil || iface.Identity == "" || iface.Identity == task.Identity { + // A malformed definition is pinTaskProfile's to report. + return true + } + runner := d.identityFor(&workflow.Task{Identity: iface.Identity}) + if runner == nil { + d.parkRunningTask(ctx, task.ID, "workflow "+task.Workflow+" runs as identity "+iface.Identity+", which is not configured", taskstate.ParkNeedsHuman) + return false + } + if !d.mayRunWorkflow(ctx, task) { + d.parkRunningTask(ctx, task.ID, "the dispatching identity may not run workflow "+task.Workflow, taskstate.ParkNeedsHuman) + return false + } + task.Identity = runner.Name + if err := d.Store.Update(ctx, task); err != nil { + d.parkRunningTask(ctx, task.ID, "persist workflow identity: "+err.Error(), taskstate.ParkTransient) + return false + } + return true +} + +// mayRunWorkflow asks the policy chain whether the task's current identity +// may run its workflow, recording a denial. No chain allows it, as every +// other check on an install that has not built one. +func (d *Daemon) mayRunWorkflow(ctx context.Context, task *workflow.Task) bool { + if d.Access == nil || d.Principals == nil { + return true + } + principal, err := d.Principals.PrincipalFor(ctx, d.dispatcherID(task)) + if err != nil { + d.Log.Warn("workflow identity: principal unavailable", "task", task.ID, "err", err) + return false + } + resource := access.Resource{Kind: access.KindWorkflow, ID: task.Workflow, Org: principal.Org} + decision := d.Access.Authorize(principal, access.ActionRun, resource, access.Context{}) + if decision.Allowed { + return true + } + if d.Denials != nil { + denial := access.Denial{ + Principal: principal.IdentityID, Org: principal.Org, Action: access.ActionRun, + Kind: resource.Kind, ResourceID: resource.ID, Level: decision.Level, Policies: decision.Policies, + } + if err := d.Denials.RecordDenial(ctx, denial); err != nil { + d.Log.Warn("workflow identity: record denial", "task", task.ID, "err", err) + } + } + return false +} + +// dispatcherID is the identity ID the task currently acts as: its configured +// identity's, else the root's. +func (d *Daemon) dispatcherID(task *workflow.Task) identity.IdentityID { + if runner := d.identityFor(task); runner != nil { + return runner.ID + } + return d.RootIdentityID +} diff --git a/internal/daemon/workflow_identity_test.go b/internal/daemon/workflow_identity_test.go new file mode 100644 index 000000000..1bcbf97d4 --- /dev/null +++ b/internal/daemon/workflow_identity_test.go @@ -0,0 +1,82 @@ +package daemon + +import ( + "context" + "log/slog" + "testing" + + "github.com/samcharles93/archie-core/internal/domain/access" + "github.com/samcharles93/archie-core/internal/domain/identity" + "github.com/samcharles93/archie-core/internal/domain/storecontract" + "github.com/samcharles93/archie-core/internal/domain/workflow" + "github.com/samcharles93/archie-core/internal/taskstate" +) + +type identityStore struct { + storecontract.TaskStore + parked bool +} + +func (*identityStore) Update(context.Context, *workflow.Task) error { return nil } + +func (s *identityStore) ParkTask(context.Context, int64, string, string, taskstate.ParkClass) error { + s.parked = true + return nil +} + +type principalOf struct{} + +func (principalOf) PrincipalFor(_ context.Context, id identity.IdentityID) (access.Principal, error) { + return access.Principal{IdentityID: id}, nil +} + +// runAllowed permits run only to the identities it lists. +type runAllowed map[identity.IdentityID]bool + +func (r runAllowed) Authorize(p access.Principal, _ access.Action, _ access.Resource, _ access.Context) access.Decision { + if r[p.IdentityID] { + return access.Allowed() + } + return access.DeniedAt(access.LevelOrg, nil) +} + +func (runAllowed) Reload(context.Context) error { return nil } + +// TestWorkflowIdentity pins that a workflow naming an identity runs as it +// only when the dispatcher may run the workflow, and that a workflow naming +// none runs as the dispatcher. +func TestWorkflowIdentity(t *testing.T) { + const named = "id: deploy\nidentity: deployer\n" + const plain = "id: deploy\n" + tests := []struct { + name string + yaml string + dispatcher string + allowed runAllowed + wantIdentity string + wantParked bool + }{ + {"allowed dispatcher runs as the workflow's identity", named, "", runAllowed{"root": true}, "deployer", false}, + {"refused dispatcher is parked", named, "", runAllowed{}, "", true}, + {"unconfigured identity is parked", "id: deploy\nidentity: ghost\n", "", runAllowed{"root": true}, "", true}, + {"no named identity runs as the dispatcher", plain, "bot", runAllowed{}, "bot", false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + store := &identityStore{} + d := &Daemon{ + Store: store, Log: slog.New(slog.DiscardHandler), RootIdentityID: "root", + Access: tt.allowed, Principals: principalOf{}, + Identities: []*IdentityRunner{{ID: "id-deployer", Name: "deployer"}, {ID: "id-bot", Name: "bot"}}, + } + task := &workflow.Task{ + ID: 1, Identity: tt.dispatcher, Workflow: "deploy", + WorkflowDefinitionYAML: tt.yaml, WorkflowDefinitionDigest: workflow.DigestDefinition(tt.yaml), + } + ok := d.adoptWorkflowIdentity(context.Background(), task) + if ok == tt.wantParked || store.parked != tt.wantParked || (ok && task.Identity != tt.wantIdentity) { + t.Fatalf("ok %v parked %v identity %q, want parked %v identity %q", ok, store.parked, task.Identity, tt.wantParked, tt.wantIdentity) + } + }) + } +} diff --git a/internal/domain/workflow/task/interface.go b/internal/domain/workflow/task/interface.go index 9a90d5ac2..2a6da5f88 100644 --- a/internal/domain/workflow/task/interface.go +++ b/internal/domain/workflow/task/interface.go @@ -56,6 +56,10 @@ type WorkflowInterface struct { // Profile names a [containers.profiles] entry. It is resolved when a task // is dispatched, not when the workflow is saved. Profile string `yaml:"profile,omitempty" json:"profile,omitempty"` + // Identity names the identity every run of this workflow acts as, + // whatever dispatched it; the dispatcher needs run on the workflow. Empty + // runs as the dispatcher. + Identity string `yaml:"identity,omitempty" json:"identity,omitempty"` // DeclaredNeeds is what the definition's needs: block declares. Read // Needs, not this, when deciding a harness requirement: a declared output // forces the captures capability and Needs resolves it.