Every attempt checks that the identity that started the task and the identity the workflow declares may both run it, recording a denial for either. A workflow that declares no identity is refused rather than running as its starter or the root. A workflow.call callee is started by its caller's declared identity instead of inheriting the caller's row. The workflow settings panel marks the identity as required and offers the agent identities.
101 lines
4.3 KiB
Go
101 lines
4.3 KiB
Go
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 makes the run act as the
|
|
// identity the workflow declares. Two checks guard it, on every attempt: the
|
|
// stored identity that started the task (a binding's principal, a chat user,
|
|
// a poller, or a calling workflow's declared identity) must be permitted run
|
|
// on the workflow, and so must the identity the run acts as. A workflow that
|
|
// declares no identity is refused: it neither inherits its starter's nor
|
|
// falls back to the root's. 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 {
|
|
d.parkRunningTask(ctx, task.ID, "workflow "+task.Workflow+": "+err.Error(), taskstate.ParkNeedsHuman)
|
|
return false
|
|
}
|
|
if iface.Identity == "" {
|
|
d.parkRunningTask(ctx, task.ID, "workflow "+task.Workflow+" declares no identity to run as; set one in its settings", taskstate.ParkNeedsHuman)
|
|
return false
|
|
}
|
|
runsAs, runnerName, ok := d.declaredIdentity(iface.Identity)
|
|
if !ok {
|
|
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.publisherIdentity(task.Identity)) {
|
|
d.parkRunningTask(ctx, task.ID, "the identity that started it may not run workflow "+task.Workflow, taskstate.ParkNeedsHuman)
|
|
return false
|
|
}
|
|
if !d.mayRunWorkflow(ctx, task, runsAs) {
|
|
d.parkRunningTask(ctx, task.ID, "identity "+iface.Identity+" may not run workflow "+task.Workflow, taskstate.ParkNeedsHuman)
|
|
return false
|
|
}
|
|
// In memory only: the stored identity stays the starter's, so every
|
|
// attempt re-applies both checks.
|
|
task.Identity = runnerName
|
|
return true
|
|
}
|
|
|
|
// declaredIdentity resolves a workflow's declared identity to the ID it is
|
|
// authorized as and the name the task carries while it runs: a configured
|
|
// identity's name, or empty for the root identity of a single-identity
|
|
// install.
|
|
func (d *Daemon) declaredIdentity(ref string) (identity.IdentityID, string, bool) {
|
|
if runner := d.identityFor(&workflow.Task{Identity: ref}); runner != nil {
|
|
return runner.ID, runner.Name, true
|
|
}
|
|
if identity.IdentityID(ref) == d.RootIdentityID || identity.StableID(ref) == d.RootIdentityID {
|
|
return d.RootIdentityID, "", true
|
|
}
|
|
return "", "", false
|
|
}
|
|
|
|
// mayRunWorkflow asks the policy chain whether id may run the task's
|
|
// 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, id identity.IdentityID) bool {
|
|
if d.Access == nil || d.Principals == nil {
|
|
return true
|
|
}
|
|
principal, err := d.Principals.PrincipalFor(ctx, id)
|
|
if err != nil {
|
|
d.Log.Warn("workflow identity: principal unavailable", "task", task.ID, "identity", id, "err", err)
|
|
return false
|
|
}
|
|
resource := access.Resource{Kind: access.KindWorkflow, ID: task.Workflow, Org: task.Org}
|
|
if resource.Org == "" {
|
|
resource.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
|
|
}
|