157 lines
7.0 KiB
Go
157 lines
7.0 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 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.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.ParkNeedsHuman, 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.ParkNeedsHuman, 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 !d.mayRunWorkflow(ctx, task, d.publisherIdentity(task.Identity)) {
|
|
d.parkBlockedTask(ctx, task, "the identity that started it may not run workflow "+task.Workflow, taskstate.ParkNeedsHuman, taskstate.BlockerPolicyDenied)
|
|
return false
|
|
}
|
|
if !d.mayRunWorkflow(ctx, task, runsAs) {
|
|
d.parkBlockedTask(ctx, task, "identity "+iface.Identity+" may not run workflow "+task.Workflow, taskstate.ParkNeedsHuman, taskstate.BlockerPolicyDenied)
|
|
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(ctx context.Context, task *workflow.Task, ref string) (identity.IdentityID, string, error) {
|
|
var id identity.IdentityID
|
|
var name string
|
|
if identity.IdentityID(ref) == d.RootIdentityID || identity.StableID(ref) == d.RootIdentityID {
|
|
id = d.RootIdentityID
|
|
} else {
|
|
id, name = identity.IdentityID(ref), ref
|
|
}
|
|
if d.IdentityRepository == nil {
|
|
if id == d.RootIdentityID {
|
|
return id, name, nil
|
|
}
|
|
return "", "", pinFailure{taskstate.ParkTransient, fmt.Errorf("identity authority unavailable for %s", ref)}
|
|
}
|
|
value, err := d.databaseExecutionIdentity(ctx, task, id)
|
|
if err != nil {
|
|
return "", "", err
|
|
}
|
|
|
|
if name != "" {
|
|
name = string(value.ID)
|
|
}
|
|
return value.ID, name, 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.ParkNeedsHuman
|
|
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.ParkNeedsHuman, 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.ParkNeedsHuman, 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.ParkNeedsHuman, 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.
|
|
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, 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
|
|
}
|
|
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
|
|
}
|