package governance import ( "context" "errors" "log/slog" "testing" "github.com/samcharles93/archie-core/internal/platform/httpapi" ) // TestShippedPoliciesLetTheWorkloadWork guards that a fresh install runs its // workflows: the workload principal may run workflows, deliver messages and // read, and may do nothing that changes configuration or reads secrets. Members // reach their own account, conversations and notes whatever their role. func TestShippedPoliciesLetTheWorkloadWork(t *testing.T) { engine := NewEngine(ShippedOrgPolicies(DefaultOrgID)) member := func(role Role) Principal { return Principal{IdentityID: "alice", Org: DefaultOrgID, Memberships: []Membership{{IdentityID: "alice", OrgID: DefaultOrgID, Role: role}}} } tests := []struct { name string who Principal action Action kind ResourceKind want bool }{ {"workload runs a workflow", WorkloadPrincipal(DefaultOrgID), ActionRun, KindWorkflow, true}, {"workload delivers", WorkloadPrincipal(DefaultOrgID), ActionDeliver, KindDelivery, true}, {"workload reads", WorkloadPrincipal(DefaultOrgID), ActionRead, KindRun, true}, {"workload does not change a workflow", WorkloadPrincipal(DefaultOrgID), ActionUpdate, KindWorkflow, false}, {"workload does not read secrets", WorkloadPrincipal(DefaultOrgID), ActionReadSecret, KindSecret, false}, {"workload does not manage policies", WorkloadPrincipal(DefaultOrgID), ActionManagePolicies, KindPolicy, false}, {"a viewer changes their account", member(RoleViewer), ActionUpdate, KindAccount, true}, {"a viewer sends a chat turn", member(RoleViewer), ActionCreate, KindChatSession, true}, {"a viewer keeps a note", member(RoleViewer), ActionCreate, KindNote, true}, {"a viewer does not update a workflow", member(RoleViewer), ActionUpdate, KindWorkflow, false}, {"a viewer does not publish a shared note", member(RoleViewer), ActionCreate, KindMemory, false}, {"a developer runs a workflow", member(RoleDeveloper), ActionRun, KindWorkflow, true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { err := Require(t.Context(), engine, tt.who, tt.action, Resource{Kind: tt.kind, Org: tt.who.Org}) if (err == nil) != tt.want { t.Fatalf("Require = %v, want allowed %v", err, tt.want) } if err != nil && !errors.Is(err, ErrDenied) { t.Fatalf("a refusal is %v, want %v", err, ErrDenied) } }) } } type denialLog struct{ recorded []Denial } func (d *denialLog) RecordDenial(_ context.Context, denial Denial) error { d.recorded = append(d.recorded, denial) return nil } // TestEnforcementFailsClosedAndRecordsRefusals guards the single seam every // entry point shares: a chain that is not loaded refuses with // ErrChainUnavailable, as does one with no authorizer at all, a refusal is // recorded as a denial, and an undecidable request is not. func TestEnforcementFailsClosedAndRecordsRefusals(t *testing.T) { failing := func(context.Context) (int64, error) { return 0, errors.New("database down") } nothing := func(context.Context) ([]Policy, error) { return nil, nil } pending := NewPending(failing, nothing, slog.New(slog.DiscardHandler)) loaded := NewEngine(ShippedOrgPolicies(DefaultOrgID)) admin := SharedTokenOwner() viewer := Principal{IdentityID: "bob", Org: DefaultOrgID, Memberships: []Membership{{IdentityID: "bob", OrgID: DefaultOrgID, Role: RoleViewer}}} resource := Resource{Kind: KindWorkflow, ID: "triage", Org: DefaultOrgID} tests := []struct { name string authorizer Authorizer who Principal wantErr error wantDenials int wantHTTPDeny error }{ {name: "a chain that is not loaded", authorizer: pending, who: admin, wantErr: ErrChainUnavailable, wantHTTPDeny: httpapi.ErrUnavailable}, {name: "no authorizer", authorizer: nil, who: admin, wantErr: ErrChainUnavailable, wantHTTPDeny: httpapi.ErrUnavailable}, {name: "a refusal", authorizer: loaded, who: viewer, wantErr: ErrDenied, wantDenials: 1, wantHTTPDeny: httpapi.ErrDenied}, {name: "a grant", authorizer: loaded, who: admin}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { denials := &denialLog{} var a Authorizer if tt.authorizer != nil { a = Recording(tt.authorizer, denials, slog.New(slog.DiscardHandler)) } if err := Require(t.Context(), a, tt.who, ActionRun, resource); !errors.Is(err, tt.wantErr) { t.Fatalf("Require = %v, want %v", err, tt.wantErr) } if len(denials.recorded) != tt.wantDenials { t.Fatalf("denials recorded = %d, want %d", len(denials.recorded), tt.wantDenials) } m := &Module{} if a != nil { m.UseAuthorizer(a) } ctx := WithPrincipal(t.Context(), tt.who) if err := m.Allow(ctx, string(ActionRun), string(KindWorkflow)); !errors.Is(err, tt.wantHTTPDeny) { t.Fatalf("Allow = %v, want %v", err, tt.wantHTTPDeny) } }) } }