// 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" TokensManage Permission = "tokens.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" AuditRead Permission = "audit.read" ) // All lists every permission in display order. var All = []Permission{ OrgManage, MembersRead, MembersManage, RolesManage, TokensManage, MonitorsRead, MonitorsWrite, StatusPagesRead, StatusPagesWrite, IncidentsRead, IncidentsWrite, NotificationsRead, NotificationsWrite, AuditRead, } 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 }