feat(authz): add permission sets, scoped grants and escalation checks
Refs #6, #7 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
7b063d2cd8
commit
15932db98a
2 files changed
+280
No files matched your search
@@ -0,0 +1,83 @@
|
||||
package authz
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestNewSetAddsImpliedAndDropsUnknown(t *testing.T) {
|
||||
got := NewSet("monitors.write", "no.such", "members.manage").Strings()
|
||||
want := []string{"members.read", "members.manage", "monitors.read", "monitors.write"}
|
||||
if !slices.Equal(got, want) {
|
||||
t.Errorf("NewSet(...).Strings() = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCan(t *testing.T) {
|
||||
page := Resource{Type: StatusPage, ID: uuid.New()}
|
||||
other := Resource{Type: StatusPage, ID: uuid.New()}
|
||||
g := &Grants{
|
||||
Org: NewSet(MonitorsRead),
|
||||
Scoped: map[Resource]Set{page: NewSet(StatusPagesWrite)},
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
p Permission
|
||||
rs []Resource
|
||||
want bool
|
||||
}{
|
||||
{"org-wide permission", MonitorsRead, nil, true},
|
||||
{"missing permission", MonitorsWrite, nil, false},
|
||||
{"scoped permission on its resource", StatusPagesWrite, []Resource{page}, true},
|
||||
{"scoped permission implies read", StatusPagesRead, []Resource{page}, true},
|
||||
{"scoped permission on other resource", StatusPagesWrite, []Resource{other}, false},
|
||||
{"scoped permission without resource", StatusPagesWrite, nil, false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if got := g.Can(tt.p, tt.rs...); got != tt.want {
|
||||
t.Errorf("%s: Can(%s, %v) = %t, want %t", tt.name, tt.p, tt.rs, got, tt.want)
|
||||
}
|
||||
}
|
||||
if !(&Grants{Owner: true}).Can(OrgManage) {
|
||||
t.Error("owner Can(org.manage) = false, want true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCanGrant(t *testing.T) {
|
||||
page := Resource{Type: StatusPage, ID: uuid.New()}
|
||||
admin := &Grants{
|
||||
Org: NewSet(MembersManage, MonitorsWrite),
|
||||
Scoped: map[Resource]Set{page: NewSet(StatusPagesWrite)},
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
perms Set
|
||||
scope *Resource
|
||||
want bool
|
||||
}{
|
||||
{"subset org-wide", NewSet(MonitorsRead), nil, true},
|
||||
{"equal org-wide", NewSet(MembersManage, MonitorsWrite), nil, true},
|
||||
{"escalation org-wide", NewSet(OrgManage), nil, false},
|
||||
{"scoped right org-wide", NewSet(StatusPagesWrite), nil, false},
|
||||
{"scoped right on its resource", NewSet(StatusPagesWrite), &page, true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if got := admin.CanGrant(tt.perms, tt.scope); got != tt.want {
|
||||
t.Errorf("%s: CanGrant(%v) = %t, want %t", tt.name, tt.perms.Strings(), got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRestrict(t *testing.T) {
|
||||
user := &Grants{Org: NewSet(MonitorsWrite)}
|
||||
tok := user.Restrict(NewSet(MonitorsWrite, OrgManage))
|
||||
if !tok.Can(MonitorsWrite) || tok.Can(OrgManage) {
|
||||
t.Errorf("Restrict() org = %v, want monitors only", tok.Org.Strings())
|
||||
}
|
||||
owner := (&Grants{Owner: true}).Restrict(NewSet(MonitorsRead))
|
||||
if owner.Owner || !owner.Can(MonitorsRead) || owner.Can(MonitorsWrite) {
|
||||
t.Errorf("owner Restrict() = %+v, want only monitors.read", owner)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user