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:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-22 00:49:35 +01:00
1 parent 7b063d2cd8
commit 15932db98a
2 files changed
+280

No files matched your search

+197
View File
@@ -0,0 +1,197 @@
// Package authz decides what a member of an organization may do.
//
// Permissions are granted through roles. A role assignment applies either to
// the whole organization or to a single resource such as one status page.
// The organization owner holds every permission implicitly.
package authz
import (
"slices"
"github.com/google/uuid"
)
// Permission names one capability. The string form is stored in the database
// and exposed through the API, so existing values must never change.
type Permission string
// Permissions. A *.write or *.manage permission implies the matching read.
const (
OrgManage Permission = "org.manage"
MembersRead Permission = "members.read"
MembersManage Permission = "members.manage"
RolesManage Permission = "roles.manage"
MonitorsRead Permission = "monitors.read"
MonitorsWrite Permission = "monitors.write"
StatusPagesRead Permission = "statuspages.read"
StatusPagesWrite Permission = "statuspages.write"
IncidentsRead Permission = "incidents.read"
IncidentsWrite Permission = "incidents.write"
NotificationsRead Permission = "notifications.read"
NotificationsWrite Permission = "notifications.write"
)
// All lists every permission in display order.
var All = []Permission{
OrgManage,
MembersRead, MembersManage, RolesManage,
MonitorsRead, MonitorsWrite,
StatusPagesRead, StatusPagesWrite,
IncidentsRead, IncidentsWrite,
NotificationsRead, NotificationsWrite,
}
var implies = map[Permission]Permission{
MembersManage: MembersRead,
RolesManage: MembersRead,
MonitorsWrite: MonitorsRead,
StatusPagesWrite: StatusPagesRead,
IncidentsWrite: IncidentsRead,
NotificationsWrite: NotificationsRead,
}
// Set is an unordered set of permissions.
type Set map[Permission]struct{}
// NewSet returns a set of ps including the permissions they imply. Unknown
// strings are dropped, so a permission removed in a later release simply
// stops granting anything.
func NewSet[T ~string](ps ...T) Set {
s := Set{}
for _, p := range ps {
perm := Permission(p)
if !slices.Contains(All, perm) {
continue
}
s[perm] = struct{}{}
if r, ok := implies[perm]; ok {
s[r] = struct{}{}
}
}
return s
}
// Has reports whether p is in s.
func (s Set) Has(p Permission) bool {
_, ok := s[p]
return ok
}
// Covers reports whether every permission in other is also in s.
func (s Set) Covers(other Set) bool {
for p := range other {
if !s.Has(p) {
return false
}
}
return true
}
// Union returns the permissions in either set.
func (s Set) Union(other Set) Set {
out := make(Set, len(s)+len(other))
for p := range s {
out[p] = struct{}{}
}
for p := range other {
out[p] = struct{}{}
}
return out
}
// Intersect returns the permissions in both sets.
func (s Set) Intersect(other Set) Set {
out := Set{}
for p := range s {
if other.Has(p) {
out[p] = struct{}{}
}
}
return out
}
// Strings returns the permissions in the order of All.
func (s Set) Strings() []string {
out := make([]string, 0, len(s))
for _, p := range All {
if s.Has(p) {
out = append(out, string(p))
}
}
return out
}
// ResourceType names what a scoped assignment applies to.
type ResourceType string
// Resource types that assignments can be scoped to.
const (
MonitorGroup ResourceType = "monitor_group"
StatusPage ResourceType = "status_page"
)
// Resource identifies one scoped object.
type Resource struct {
Type ResourceType
ID uuid.UUID
}
// Valid reports whether r names a known resource type.
func (r Resource) Valid() bool {
return (r.Type == MonitorGroup || r.Type == StatusPage) && r.ID != uuid.Nil
}
// Grants are the effective permissions of one user in one organization.
type Grants struct {
Owner bool
Org Set
Scoped map[Resource]Set
}
// Can reports whether the user holds p organization-wide or on any of rs.
func (g *Grants) Can(p Permission, rs ...Resource) bool {
if g.Owner || g.Org.Has(p) {
return true
}
for _, r := range rs {
if g.Scoped[r].Has(p) {
return true
}
}
return false
}
// CanGrant reports whether the user may hand out perms, organization-wide
// when scope is nil or on that resource otherwise. Nobody can grant a
// permission they do not hold themselves, which keeps delegated
// administrators from escalating their own rights through a helper account.
func (g *Grants) CanGrant(perms Set, scope *Resource) bool {
if g.Owner {
return true
}
held := g.Org
if scope != nil {
held = held.Union(g.Scoped[*scope])
}
return held.Covers(perms)
}
// Restrict returns the grants limited to perms, as used for API tokens whose
// permissions must never exceed their owner's current ones.
func (g *Grants) Restrict(perms Set) *Grants {
out := &Grants{Org: perms.Intersect(g.all()), Scoped: map[Resource]Set{}}
if g.Owner {
out.Org = perms
}
for r, s := range g.Scoped {
out.Scoped[r] = s.Intersect(perms)
}
return out
}
func (g *Grants) all() Set {
if g.Org == nil {
return Set{}
}
return g.Org
}
+83
View File
@@ -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)
}
}