package daemon import ( "context" "log/slog" "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/taskstate" ) // IdentityGate answers who an identity is, which org it acts for, whether it // may act, and whether it may run a workflow. It is the only part of the // daemon that reads the identity repository, agent assignments and the policy // chain for those questions. type IdentityGate struct { identities identity.Repository assignments org.AgentAssignments authorizer access.Authorizer principals access.PrincipalSource denials access.DenialRecorder orgs func(context.Context) []access.Principal workspaces access.WorkspaceLister log *slog.Logger } // NewIdentityGate builds the gate from the State Store surfaces it reads. // workspaces lets a principal select an org it is a member of; it is the // State Store's own WorkspaceLister, or nil when the store has none. func NewIdentityGate( identities identity.Repository, assignments org.AgentAssignments, authorizer access.Authorizer, principals access.PrincipalSource, denials access.DenialRecorder, orgs func(context.Context) []access.Principal, workspaces access.WorkspaceLister, log *slog.Logger, ) *IdentityGate { return &IdentityGate{ identities: identities, assignments: assignments, authorizer: authorizer, principals: principals, denials: denials, orgs: orgs, workspaces: workspaces, log: log, } } // orgResolver is the identity-to-org surface the identity checks read through. type orgResolver interface { identityOrg(ctx context.Context, id identity.IdentityID) (org.OrgID, error) identityMayAct(ctx context.Context, name string) (bool, string, taskstate.ParkClass) identityActive(ctx context.Context, id identity.IdentityID) bool } var _ orgResolver = (*IdentityGate)(nil) // Authorize evaluates the policy chain for principal's action on resource. func (g *IdentityGate) Authorize(ctx context.Context, principal access.Principal, action access.Action, resource access.Resource, c access.Context) access.Decision { return g.authorizer.Authorize(ctx, principal, action, resource, c) } // RecordDenial stores a refused action so the operator can see it. func (g *IdentityGate) RecordDenial(ctx context.Context, denial access.Denial) error { return g.denials.RecordDenial(ctx, denial) } // OrgPrincipals is one acting principal per organisation, system org first. func (g *IdentityGate) OrgPrincipals(ctx context.Context) []access.Principal { return g.orgs(ctx) } // Principals is the source that resolves an identity to its principal, for the // RPC callers that authenticate requests. func (g *IdentityGate) Principals() access.PrincipalSource { return g.principals } // Lookup reads an identity's stored record. func (g *IdentityGate) Lookup(ctx context.Context, id identity.IdentityID) (identity.Identity, error) { return g.identities.Get(ctx, id) } // identityOrg resolves an identity's org. A store error is returned. func (g *IdentityGate) identityOrg(ctx context.Context, id identity.IdentityID) (org.OrgID, error) { principal, err := g.principals.PrincipalFor(ctx, id) if err != nil { return "", err } return principal.Org, nil } func (g *IdentityGate) identityActive(ctx context.Context, id identity.IdentityID) bool { if id == "" { g.log.ErrorContext(ctx, "identity lifecycle check failed", "err", "identity ID is empty") return false } value, err := g.identities.Get(ctx, id) if err != nil { g.log.ErrorContext(ctx, "identity lifecycle check failed", "identity", id, "err", err) return false } return value.Lifecycle == identity.LifecycleActive } // identityMayAct reports whether a task's identity may act, and the park // class if not. An empty, unknown or inactive identity never falls back to // the System forge. func (g *IdentityGate) identityMayAct(ctx context.Context, name string) (bool, string, taskstate.ParkClass) { if name == "" { return false, "task has no identity", taskstate.ParkNeedsDecision } value, err := g.identities.Get(ctx, identity.IdentityID(name)) if err != nil { return false, "execution identity unavailable: " + err.Error(), taskstate.ParkTransient } if value.Lifecycle != identity.LifecycleActive { return false, "identity may not act", taskstate.ParkNeedsDecision } return true, "", "" }