- rename governance.access_denials table, indexes and API types via migration 00061 - expose /api/access/decisions and decisions payload; keep audit auth unchanged - rename UI store, page and route from denials to decisions
112 lines
4.8 KiB
Go
112 lines
4.8 KiB
Go
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 decisionLog struct{ recorded []DecisionRecord }
|
|
|
|
func (d *decisionLog) RecordDecision(_ context.Context, rec DecisionRecord) error {
|
|
d.recorded = append(d.recorded, rec)
|
|
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, every allowed or
|
|
// refused request is recorded, and an undecidable request is not.
|
|
func TestEnforcementFailsClosedAndRecordsDecisions(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
|
|
wantDecisions 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, wantDecisions: 1, wantHTTPDeny: httpapi.ErrDenied},
|
|
{name: "a grant", authorizer: loaded, who: admin, wantDecisions: 1},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
log := &decisionLog{}
|
|
var a Authorizer
|
|
if tt.authorizer != nil {
|
|
a = Recording(tt.authorizer, log, 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(log.recorded) != tt.wantDecisions {
|
|
t.Fatalf("decisions recorded = %d, want %d", len(log.recorded), tt.wantDecisions)
|
|
}
|
|
|
|
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)
|
|
}
|
|
})
|
|
}
|
|
}
|