- 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
282 lines
10 KiB
Go
282 lines
10 KiB
Go
package governance
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"net/http"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/danielgtaylor/huma/v2"
|
|
|
|
"github.com/samcharles93/archie-core/internal/platform/httpapi"
|
|
)
|
|
|
|
// fakeDirectory records the changes a member route lets through.
|
|
type fakeDirectory struct {
|
|
members []Member
|
|
identities []Identity
|
|
changes []string
|
|
}
|
|
|
|
func (f *fakeDirectory) ListMembers(context.Context, OrgID) ([]Member, error) { return f.members, nil }
|
|
|
|
func (f *fakeDirectory) EnsureMembership(_ context.Context, m Membership) error {
|
|
f.changes = append(f.changes, "ensure "+string(m.IdentityID)+" "+string(m.Role))
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeDirectory) RemoveMembership(_ context.Context, m Membership) error {
|
|
f.changes = append(f.changes, "remove "+string(m.IdentityID))
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeDirectory) Get(_ context.Context, id IdentityID) (Identity, error) {
|
|
return Identity{ID: id, Version: 1}, nil
|
|
}
|
|
|
|
func (f *fakeDirectory) ListInstanceIdentities(context.Context) ([]Identity, error) {
|
|
return f.identities, nil
|
|
}
|
|
|
|
func (f *fakeDirectory) Apply(_ context.Context, id IdentityID, _ int64, c IdentityCommand, _ IdentityAudit) (Identity, error) {
|
|
f.changes = append(f.changes, string(c.Type)+" "+string(id))
|
|
return Identity{ID: id}, nil
|
|
}
|
|
|
|
func status(t *testing.T, err error) int {
|
|
t.Helper()
|
|
if err == nil {
|
|
return http.StatusOK
|
|
}
|
|
var se huma.StatusError
|
|
if !errors.As(err, &se) {
|
|
t.Fatalf("error %v carries no status", err)
|
|
}
|
|
return se.GetStatus()
|
|
}
|
|
|
|
func actor(id IdentityID, role Role) Principal {
|
|
return Principal{
|
|
IdentityID: id, Kind: KindUser, Org: DefaultOrgID,
|
|
Memberships: []Membership{{IdentityID: id, OrgID: DefaultOrgID, Role: role}},
|
|
}
|
|
}
|
|
|
|
func member(id IdentityID, role Role, status Lifecycle) Member {
|
|
return Member{IdentityID: id, Kind: KindUser, DisplayName: string(id), Role: role, Status: status}
|
|
}
|
|
|
|
func memberRoutes(dir *fakeDirectory) *memberAPI {
|
|
return &memberAPI{
|
|
dir: dir,
|
|
authz: func() Authorizer { return NewEngine(ShippedOrgPolicies(DefaultOrgID)) },
|
|
log: slog.New(slog.DiscardHandler),
|
|
}
|
|
}
|
|
|
|
// TestAccessWritesNeedTheirGrant guards the declared access of every route that
|
|
// changes members or policies, and the grant the shipped roles give it. A
|
|
// viewer or developer who reached a write would let them reassign roles or
|
|
// rewrite the access policies.
|
|
func TestAccessWritesNeedTheirGrant(t *testing.T) {
|
|
api := httpapi.New(http.NewServeMux(), "test", nil, nil)
|
|
(&Module{}).RegisterAccess(api)
|
|
|
|
engine := NewEngine(ShippedOrgPolicies(DefaultOrgID))
|
|
writes := 0
|
|
for _, route := range httpapi.Routes(api) {
|
|
mutates := route.Method != http.MethodGet
|
|
candidates := route.ID == "list-org-member-candidates"
|
|
action, kind, ok := route.Access.Declared()
|
|
if !ok {
|
|
t.Errorf("%s declares no access", route.ID)
|
|
continue
|
|
}
|
|
if mutates {
|
|
writes++
|
|
}
|
|
wantAction := string(ActionManageMembers)
|
|
if strings.HasPrefix(route.Path, "/api/access/policies") {
|
|
wantAction = string(ActionManagePolicies)
|
|
}
|
|
if (mutates || candidates) && action != wantAction {
|
|
t.Errorf("%s declares %q, want %q", route.ID, action, wantAction)
|
|
}
|
|
if !mutates && route.Path == "/api/access/decisions" && action != string(ActionReadAudit) {
|
|
t.Errorf("%s declares %q, want read_audit", route.ID, action)
|
|
}
|
|
resource := Resource{Kind: ResourceKind(kind), Org: DefaultOrgID}
|
|
if !engine.Authorize(t.Context(), actor("admin", RoleAdmin), Action(action), resource, AccessContext{}).Allowed {
|
|
t.Errorf("%s: an admin is refused %s %s", route.ID, action, kind)
|
|
}
|
|
if mutates || candidates || route.Path == "/api/access/decisions" {
|
|
for _, role := range []Role{RoleViewer, RoleDeveloper} {
|
|
if engine.Authorize(t.Context(), actor("lowly", role), Action(action), resource, AccessContext{}).Allowed {
|
|
t.Errorf("%s: a %s is allowed %s %s", route.ID, role, action, kind)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if writes != 7 {
|
|
t.Errorf("%d write routes registered, want 7", writes)
|
|
}
|
|
}
|
|
|
|
// TestMemberCandidates guards the listing offered when adding a member: it is
|
|
// limited to active identities outside the organisation, never reaches another
|
|
// organisation, and never offers the system identity.
|
|
func TestMemberCandidates(t *testing.T) {
|
|
identities := []Identity{
|
|
{ID: "root", Kind: KindUser, DisplayName: "Root", Lifecycle: LifecycleActive},
|
|
{ID: "newcomer", Kind: KindUser, DisplayName: "Newcomer", Lifecycle: LifecycleActive},
|
|
{ID: "scoped", Kind: KindUser, DisplayName: "Scoped", Lifecycle: LifecycleActive},
|
|
{ID: "gone", Kind: KindUser, DisplayName: "Gone", Lifecycle: LifecycleSuspended},
|
|
{ID: SystemID, Kind: KindSystem, DisplayName: "System", Lifecycle: LifecycleActive},
|
|
}
|
|
members := []Member{
|
|
member("root", RoleOwner, LifecycleActive),
|
|
{IdentityID: "scoped", Kind: KindUser, WorkspaceID: "ws", Role: RoleViewer, Status: LifecycleActive},
|
|
}
|
|
foreign := actor("outsider", RoleOwner)
|
|
foreign.Org = "other"
|
|
foreign.Memberships = []Membership{{IdentityID: "outsider", OrgID: "other", Role: RoleOwner}}
|
|
tests := []struct {
|
|
name string
|
|
by *Principal
|
|
org OrgID
|
|
want int
|
|
ids []IdentityID
|
|
}{
|
|
{"owner sees identities outside the org", new(actor("root", RoleOwner)), DefaultOrgID, http.StatusOK, []IdentityID{"newcomer", "scoped"}},
|
|
{"member of another org", &foreign, DefaultOrgID, http.StatusNotFound, nil},
|
|
{"owner of another org asks for its own org", &foreign, "other", http.StatusOK, []IdentityID{"newcomer", "scoped"}},
|
|
{"caller without a session", nil, DefaultOrgID, http.StatusUnauthorized, nil},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
dir := &fakeDirectory{members: members, identities: identities}
|
|
ctx := t.Context()
|
|
if tt.by != nil {
|
|
ctx = WithPrincipal(ctx, *tt.by)
|
|
}
|
|
reply, err := memberRoutes(dir).candidates(ctx, &orgPath{ID: string(tt.org)})
|
|
if got := status(t, err); got != tt.want {
|
|
t.Fatalf("status = %d (%v), want %d", got, err, tt.want)
|
|
}
|
|
if err != nil {
|
|
return
|
|
}
|
|
var got []IdentityID
|
|
for _, c := range reply.Body.Identities {
|
|
got = append(got, c.ID)
|
|
}
|
|
if !slices.Equal(got, tt.ids) {
|
|
t.Fatalf("candidates = %v, want %v", got, tt.ids)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestLastOwnerIsKept guards the rule that an organisation always keeps an
|
|
// active owner: removing, demoting or suspending the only one is refused before
|
|
// anything changes.
|
|
func TestLastOwnerIsKept(t *testing.T) {
|
|
sole := []Member{member("root", RoleOwner, LifecycleActive), member("dev", RoleDeveloper, LifecycleActive)}
|
|
pair := []Member{member("root", RoleOwner, LifecycleActive), member("second", RoleOwner, LifecycleActive)}
|
|
suspendedPair := []Member{member("root", RoleOwner, LifecycleActive), member("second", RoleOwner, LifecycleSuspended)}
|
|
scoped := append(slicesClone(sole), Member{IdentityID: "root", Kind: KindUser, WorkspaceID: "ws", Role: RoleOwner, Status: LifecycleActive})
|
|
|
|
tests := []struct {
|
|
name string
|
|
members []Member
|
|
act func(*memberAPI, context.Context) error
|
|
want int
|
|
changed bool
|
|
}{
|
|
{"demote the only owner", sole, setRole("root", "admin"), http.StatusConflict, false},
|
|
{"remove the only owner", sole, removeMember("root", ""), http.StatusConflict, false},
|
|
{"suspend the only owner", sole, suspendMember("root"), http.StatusConflict, false},
|
|
{"demote when the other owner is suspended", suspendedPair, setRole("root", "viewer"), http.StatusConflict, false},
|
|
{"demote one of two owners", pair, setRole("root", "admin"), http.StatusOK, true},
|
|
{"remove one of two owners", pair, removeMember("second", ""), http.StatusOK, true},
|
|
{"keep the only owner as owner", sole, setRole("root", "owner"), http.StatusOK, true},
|
|
{"remove an owner's workspace membership", scoped, removeMember("root", "ws"), http.StatusOK, true},
|
|
{"remove the only developer", sole, removeMember("dev", ""), http.StatusOK, true},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
dir := &fakeDirectory{members: tt.members}
|
|
ctx := WithPrincipal(t.Context(), actor("root", RoleOwner))
|
|
err := tt.act(memberRoutes(dir), ctx)
|
|
if got := status(t, err); got != tt.want {
|
|
t.Fatalf("status = %d (%v), want %d", got, err, tt.want)
|
|
}
|
|
if changed := len(dir.changes) > 0; changed != tt.changed {
|
|
t.Fatalf("changes = %v, want changed %v", dir.changes, tt.changed)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestOwnerRoleNeedsAnOwner guards the rule that only an owner grants the owner
|
|
// role or alters an owner: an admin who could would promote themselves.
|
|
func TestOwnerRoleNeedsAnOwner(t *testing.T) {
|
|
members := []Member{
|
|
member("root", RoleOwner, LifecycleActive), member("second", RoleOwner, LifecycleActive),
|
|
member("dev", RoleDeveloper, LifecycleActive),
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
by Principal
|
|
act func(*memberAPI, context.Context) error
|
|
want int
|
|
}{
|
|
{"admin grants owner", actor("adm", RoleAdmin), setRole("dev", "owner"), http.StatusForbidden},
|
|
{"admin demotes an owner", actor("adm", RoleAdmin), setRole("second", "viewer"), http.StatusForbidden},
|
|
{"admin removes an owner", actor("adm", RoleAdmin), removeMember("second", ""), http.StatusForbidden},
|
|
{"admin suspends an owner", actor("adm", RoleAdmin), suspendMember("second"), http.StatusForbidden},
|
|
{"admin grants developer", actor("adm", RoleAdmin), setRole("dev", "admin"), http.StatusOK},
|
|
{"owner grants owner", actor("root", RoleOwner), setRole("dev", "owner"), http.StatusOK},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
dir := &fakeDirectory{members: members}
|
|
err := tt.act(memberRoutes(dir), WithPrincipal(t.Context(), tt.by))
|
|
if got := status(t, err); got != tt.want {
|
|
t.Fatalf("status = %d (%v), want %d", got, err, tt.want)
|
|
}
|
|
if (tt.want == http.StatusOK) != (len(dir.changes) > 0) {
|
|
t.Fatalf("changes = %v with status %d", dir.changes, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func setRole(identity, role string) func(*memberAPI, context.Context) error {
|
|
return func(a *memberAPI, ctx context.Context) error {
|
|
in := &setMemberInput{ID: string(DefaultOrgID), Identity: identity}
|
|
in.Body.Role = role
|
|
_, err := a.set(ctx, in)
|
|
return err
|
|
}
|
|
}
|
|
|
|
func removeMember(identity, workspace string) func(*memberAPI, context.Context) error {
|
|
return func(a *memberAPI, ctx context.Context) error {
|
|
_, err := a.remove(ctx, &removeMemberInput{ID: string(DefaultOrgID), Identity: identity, Workspace: workspace})
|
|
return err
|
|
}
|
|
}
|
|
|
|
func suspendMember(identity string) func(*memberAPI, context.Context) error {
|
|
return func(a *memberAPI, ctx context.Context) error {
|
|
_, err := a.suspend(ctx, &memberPath{ID: string(DefaultOrgID), Identity: identity})
|
|
return err
|
|
}
|
|
}
|
|
|
|
func slicesClone(m []Member) []Member { return append([]Member(nil), m...) }
|