Files
archie/modules/governance/access_api_test.go
T

92 lines
3.7 KiB
Go

package governance
import (
"context"
"log/slog"
"net/http"
"slices"
"strings"
"testing"
)
// fakePolicies applies the same validation and lockout rule as the module's
// store-backed methods, against policies held in memory.
type fakePolicies struct {
stored []Policy
puts []Policy
org []Principal
}
func (f *fakePolicies) ListPolicies(context.Context) ([]Policy, error) { return f.stored, nil }
func (f *fakePolicies) PutPolicy(ctx context.Context, p Policy) (int64, error) {
err := CheckPolicyChange(ctx, f.stored, func(cur []Policy) []Policy {
return append(slices.DeleteFunc(cur, func(c Policy) bool { return samePolicy(c, p) }), p)
}, []Org{{ID: DefaultOrgID}}, func(context.Context, OrgID) ([]Principal, error) { return f.org, nil })
if err != nil {
return 0, err
}
f.puts = append(f.puts, p)
return 1, nil
}
func (f *fakePolicies) DeletePolicy(context.Context, Policy) error { return nil }
func policyRoutes(f *fakePolicies) *accessAPI {
return &accessAPI{policies: f, log: slog.New(slog.DiscardHandler)}
}
// TestPolicyEdits guards what a policy edit may do: an invalid policy is
// refused with the Cedar error and never stored, a change that leaves no owner
// able to manage policies is refused, and a policy lands in the caller's own
// organisation, with instance policies reserved for the instance owner.
func TestPolicyEdits(t *testing.T) {
owner := actor("root", RoleOwner)
otherOrgOwner := Principal{
IdentityID: "pat", Kind: KindUser, Org: "acme",
Memberships: []Membership{{IdentityID: "pat", OrgID: "acme", Role: RoleOwner}},
}
withOwner := func() *fakePolicies {
return &fakePolicies{stored: ShippedOrgPolicies(DefaultOrgID), org: []Principal{owner}}
}
policy := func(level Level, id, text string) Policy {
return Policy{ID: id, Level: level, OrgID: "elsewhere", Text: text}
}
const harmless = `permit(principal, action == Archie::Action::"read", resource);`
tests := []struct {
name string
by Principal
policy Policy
want int
wantError string
stored bool
wantOrg OrgID
}{
{name: "a valid policy", by: owner, policy: policy(LevelOrg, "p", harmless), want: http.StatusOK, stored: true, wantOrg: DefaultOrgID},
{name: "text that does not compile", by: owner, policy: policy(LevelOrg, "p", `permit(principal, action == Archie::Action::"nope", resource);`), want: http.StatusBadRequest, wantError: "nope"},
{name: "no policy text", by: owner, policy: policy(LevelOrg, "p", " "), want: http.StatusBadRequest, wantError: "no text"},
{name: "a forbid that locks owners out", by: owner, policy: policy(LevelOrg, "lock", `forbid(principal, action == Archie::Action::"manage_policies", resource);`), want: http.StatusConflict},
{name: "an instance policy by an instance owner", by: owner, policy: policy(LevelInstance, "i", `permit(principal, action, resource);`), want: http.StatusOK, stored: true},
{name: "an instance policy by another organisation's owner", by: otherOrgOwner, policy: policy(LevelInstance, "i", harmless), want: http.StatusForbidden},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := withOwner()
_, err := policyRoutes(f).putPolicy(WithPrincipal(t.Context(), tt.by), &policyInput{Body: tt.policy})
if got := status(t, err); got != tt.want {
t.Fatalf("status = %d (%v), want %d", got, err, tt.want)
}
if tt.wantError != "" && !strings.Contains(err.Error(), tt.wantError) {
t.Fatalf("error %q does not carry the Cedar error %q", err, tt.wantError)
}
if stored := len(f.puts) > 0; stored != tt.stored {
t.Fatalf("stored = %v, want %v", stored, tt.stored)
}
if tt.wantOrg != "" && f.puts[0].OrgID != tt.wantOrg {
t.Fatalf("policy stored for org %q, want %q", f.puts[0].OrgID, tt.wantOrg)
}
})
}
}