package daemon import ( "context" "errors" "fmt" "slices" "github.com/samcharles93/archie-core/internal/domain/access" "github.com/samcharles93/archie-core/internal/domain/identity" "github.com/samcharles93/archie-core/internal/domain/org" "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 System'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.ErrorContext(ctx, "pin workflow definition failed", "task", task.ID, "err", err) d.parkBlockedTask(ctx, task, "pin workflow definition: "+err.Error(), pinParkClass(err), parkBlocker(err)) return false } iface, err := workflowtask.ParseWorkflowInterface(task.WorkflowDefinitionYAML) if err != nil { d.parkBlockedTask(ctx, task, "workflow "+task.Workflow+": "+err.Error(), taskstate.ParkNeedsDecision, taskstate.BlockerWorkflowIdentity) return false } if iface.Identity == "" { d.parkBlockedTask(ctx, task, "workflow "+task.Workflow+" declares no identity to run as; set one in its settings", taskstate.ParkNeedsDecision, taskstate.BlockerWorkflowIdentity) return false } runsAs, runnerName, err := d.Identity.declaredIdentity(ctx, task, iface.Identity) if err != nil { d.parkBlockedTask(ctx, task, "workflow "+task.Workflow+": "+err.Error(), pinParkClass(err), parkBlocker(err)) return false } if allowed, err := d.Identity.mayRunWorkflow(ctx, task, identity.IdentityID(task.Identity)); !allowed { d.parkRefusedRun(ctx, task, "the identity that started it may not run workflow "+task.Workflow, err) return false } if allowed, err := d.Identity.mayRunWorkflow(ctx, task, runsAs); !allowed { d.parkRefusedRun(ctx, task, "identity "+iface.Identity+" may not run workflow "+task.Workflow, err) 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 // authorised as and the name the task carries while it runs. func (g *IdentityGate) declaredIdentity(ctx context.Context, task *workflow.Task, ref string) (identity.IdentityID, string, error) { value, err := g.databaseExecutionIdentity(ctx, task, identity.IdentityID(ref)) if err != nil { return "", "", err } return value.ID, string(value.ID), nil } func (g *IdentityGate) databaseExecutionIdentity(ctx context.Context, task *workflow.Task, id identity.IdentityID) (identity.Identity, error) { value, err := g.identities.Get(ctx, id) if err != nil { failure := pinFailure{taskstate.ParkTransient, fmt.Errorf("resolve execution identity %s: %w", id, err)} if errors.Is(err, identity.ErrNotFound) { failure.class = taskstate.ParkNeedsDecision return identity.Identity{}, blockedPark{taskstate.BlockerAgentAssignment, failure} } return identity.Identity{}, failure } if !value.Kind.Agent() || value.Lifecycle != identity.LifecycleActive { return identity.Identity{}, blockedPark{taskstate.BlockerAgentAssignment, pinFailure{taskstate.ParkNeedsDecision, fmt.Errorf("execution identity %s must be an active bot or service account", id)}} } owner := org.OrgID(task.OrgOrDefault()) principal, err := g.principals.PrincipalFor(ctx, value.ID) if err != nil { return identity.Identity{}, pinFailure{taskstate.ParkTransient, fmt.Errorf("resolve execution principal: %w", err)} } if principal.Org != owner { return identity.Identity{}, blockedPark{taskstate.BlockerAgentAssignment, pinFailure{taskstate.ParkNeedsDecision, fmt.Errorf("execution identity belongs to another organisation")}} } assignments, err := g.assignments.ListOrgAgents(access.WithPrincipal(ctx, principal), owner) if err != nil { return identity.Identity{}, pinFailure{taskstate.ParkTransient, fmt.Errorf("resolve execution identity assignment: %w", err)} } if !slices.ContainsFunc(assignments, func(a org.AgentAssignment) bool { return a.IdentityID == value.ID && a.OrgID == owner }) { return identity.Identity{}, blockedPark{taskstate.BlockerAgentAssignment, pinFailure{taskstate.ParkNeedsDecision, fmt.Errorf("execution identity %s is not assigned to this organisation", id)}} } return value, nil } // 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. The error reports a chain that could not // be consulted, which is not a denial: nothing was evaluated. func (g *IdentityGate) mayRunWorkflow(ctx context.Context, task *workflow.Task, id identity.IdentityID) (bool, error) { principal, err := g.principalIn(ctx, id, task.Org) if err != nil { g.log.WarnContext(ctx, "workflow identity: principal unavailable", "task", task.ID, "identity", id, "err", err) return false, nil } resource := access.Resource{Kind: access.KindWorkflow, ID: task.Workflow, Org: task.Org, Workspace: task.Workspace} if resource.Org == "" { resource.Org = principal.Org } decision := g.authorizer.Authorize(ctx, principal, access.ActionRun, resource, access.Context{Signature: task.BindingSignature, Addr: task.BindingAddr}) if decision.Allowed { return true, nil } if errors.Is(decision.Err, access.ErrChainUnavailable) { // Nothing was evaluated, so there is no denial to record and no // operator to involve: the chain will answer once the store does. return false, pinFailure{taskstate.ParkTransient, decision.Err} } 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 := g.denials.RecordDenial(ctx, denial); err != nil { g.log.WarnContext(ctx, "workflow identity: record denial", "task", task.ID, "err", err) } return false, nil }