Drop the bot_user-derived root identity and the empty-identity sentinel. System polls forge connections and owns instance-level work; a polled task stores the polling identity, task_queued and work_request_submitted carry task id, actor and principal, and an empty task identity is refused. Migration 0072 moves empty tasks.identity, the identity-grants root key and the old root's connection permits to System so a re-poll dedups.
159 lines
7.5 KiB
Go
159 lines
7.5 KiB
Go
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.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.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.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
|
|
// authorized as and the name the task carries while it runs.
|
|
func (d *Daemon) declaredIdentity(ctx context.Context, task *workflow.Task, ref string) (identity.IdentityID, string, error) {
|
|
if d.IdentityRepository == nil {
|
|
return "", "", pinFailure{taskstate.ParkTransient, fmt.Errorf("identity authority unavailable for %s", ref)}
|
|
}
|
|
value, err := d.databaseExecutionIdentity(ctx, task, identity.IdentityID(ref))
|
|
if err != nil {
|
|
return "", "", err
|
|
}
|
|
return value.ID, string(value.ID), nil
|
|
}
|
|
|
|
func (d *Daemon) databaseExecutionIdentity(ctx context.Context, task *workflow.Task, id identity.IdentityID) (identity.Identity, error) {
|
|
value, err := d.IdentityRepository.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)}}
|
|
}
|
|
if d.AgentAssignments == nil {
|
|
return identity.Identity{}, pinFailure{taskstate.ParkTransient, fmt.Errorf("agent assignment authority unavailable")}
|
|
}
|
|
owner := org.OrgID(task.OrgOrDefault())
|
|
if d.Principals == nil {
|
|
return identity.Identity{}, pinFailure{taskstate.ParkTransient, fmt.Errorf("principal authority unavailable")}
|
|
}
|
|
principal, err := d.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 := d.AgentAssignments.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 (d *Daemon) mayRunWorkflow(ctx context.Context, task *workflow.Task, id identity.IdentityID) (bool, error) {
|
|
if d.Access == nil || d.Principals == nil {
|
|
return true, nil
|
|
}
|
|
principal, err := d.principalIn(ctx, id, task.Org)
|
|
if err != nil {
|
|
d.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 := d.Access.Authorize(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}
|
|
}
|
|
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.WarnContext(ctx, "workflow identity: record denial", "task", task.ID, "err", err)
|
|
}
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
// parkRefusedRun parks a task whose workflow identity check did not permit it.
|
|
// A denial is the operator's to fix, but a chain that could not be read is a
|
|
// store that will answer again, so it parks transient rather than demanding an
|
|
// operator for a blip.
|
|
func (d *Daemon) parkRefusedRun(ctx context.Context, task *workflow.Task, reason string, err error) {
|
|
class, blocker := taskstate.ParkNeedsDecision, taskstate.BlockerPolicyDenied
|
|
if err != nil {
|
|
class, blocker = pinParkClass(err), parkBlocker(err)
|
|
}
|
|
d.parkBlockedTask(ctx, task, reason, class, blocker)
|
|
}
|