SPB Git

Repository Public

authz.go 6.8 KB 234 lines go

1// Package authz is the single source of truth for permissions and role2// defaults. Every server-side authorization decision goes through Decide.3package authz45import "slices"67type Role string89const (10	Owner    Role = "owner"11	Admin    Role = "admin"12	Operator Role = "operator"13	Viewer   Role = "viewer"14)1516// Rank orders roles; a higher rank includes more authority.17func (r Role) Rank() int {18	switch r {19	case Owner:20		return 421	case Admin:22		return 323	case Operator:24		return 225	case Viewer:26		return 127	}28	return 029}3031func (r Role) Valid() bool { return r.Rank() > 0 }3233type Permission string3435const (36	ClusterRead        Permission = "cluster.read"37	NodesRead          Permission = "nodes.read"38	NodesPrivateRead   Permission = "nodes.private.read"39	NodesWrite         Permission = "nodes.write"40	LogsRead           Permission = "logs.read"41	ServicesRead       Permission = "services.read"42	ModelsRead         Permission = "models.read"43	JobsRead           Permission = "jobs.read"44	JobsSubmit         Permission = "jobs.submit"45	AlertsRead         Permission = "alerts.read"46	AlertsAck          Permission = "alerts.ack"47	AlertsManage       Permission = "alerts.manage"48	MaintenanceRead    Permission = "maintenance.read"49	MaintenanceWrite   Permission = "maintenance.write"50	ActionsRead        Permission = "actions.read"51	ActionsRunLow      Permission = "actions.run.low"52	ActionsRunMedium   Permission = "actions.run.medium"53	ActionsRunHigh     Permission = "actions.run.high"54	ActionsRunCritical Permission = "actions.run.critical"55	ActionsApprove     Permission = "actions.approve"56	AuditRead          Permission = "audit.read"57	AuditExport        Permission = "audit.export"58	UsersManage        Permission = "users.manage"59	InvitationsManage  Permission = "invitations.manage"60	AgentsManage       Permission = "agents.manage"61	SettingsManage     Permission = "settings.manage"62)6364// All lists every permission in a stable, documented order.65var All = []Permission{66	ClusterRead, NodesRead, NodesPrivateRead, NodesWrite, LogsRead, ServicesRead, ModelsRead,67	JobsRead, JobsSubmit, AlertsRead, AlertsAck, AlertsManage, MaintenanceRead, MaintenanceWrite,68	ActionsRead, ActionsRunLow, ActionsRunMedium, ActionsRunHigh, ActionsRunCritical, ActionsApprove,69	AuditRead, AuditExport, UsersManage, InvitationsManage, AgentsManage, SettingsManage,70}7172func Known(p Permission) bool { return slices.Contains(All, p) }7374var viewer = []Permission{ClusterRead, NodesRead, ServicesRead, ModelsRead, JobsRead, AlertsRead, MaintenanceRead, ActionsRead}75var operator = append(slices.Clone(viewer), NodesPrivateRead, LogsRead, AlertsAck, MaintenanceWrite, JobsSubmit, ActionsRunLow, ActionsRunMedium)76var admin = append(slices.Clone(operator), NodesWrite, AlertsManage, ActionsRunHigh, ActionsApprove, AuditRead, AuditExport,77	UsersManage, InvitationsManage, AgentsManage, SettingsManage)7879// RoleDefaults returns the permissions a role holds before any grant.80func RoleDefaults(r Role) []Permission {81	switch r {82	case Owner:83		return slices.Clone(All)84	case Admin:85		return slices.Clone(admin)86	case Operator:87		return slices.Clone(operator)88	case Viewer:89		return slices.Clone(viewer)90	}91	return nil92}9394type Effect string9596const (97	Allow Effect = "allow"98	Deny  Effect = "deny"99)100101// Grant is a custom policy row. Empty ResourceKind matches every resource.102type Grant struct {103	SubjectKind  string // "user" | "role"104	Subject      string // user id | role name105	Permission   Permission106	ResourceKind string107	ResourceID   string108	Effect       Effect109}110111// Subject is the authenticated principal of a request.112type Subject struct {113	UserID string114	Role   Role115}116117// Resource identifies what is being accessed; the zero value means "any".118type Resource struct {119	Kind string120	ID   string121}122123func (g Grant) appliesTo(s Subject, p Permission, r Resource) bool {124	if g.Permission != p {125		return false126	}127	switch g.SubjectKind {128	case "user":129		if g.Subject != s.UserID {130			return false131		}132	case "role":133		if g.Subject != string(s.Role) {134			return false135		}136	default:137		return false138	}139	if g.ResourceKind == "" {140		return true141	}142	// A resource-scoped grant never applies to an unscoped ("any resource") check.143	return g.ResourceKind == r.Kind && (g.ResourceID == "" || g.ResourceID == r.ID)144}145146// Decide answers whether subject may exercise permission on resource.147// Order: an applicable deny always wins; then role defaults; then an148// applicable allow. Grants can never give anyone but an owner the149// owner-only permission ActionsRunCritical or let a non-admin manage users.150func Decide(s Subject, p Permission, r Resource, grants []Grant) bool {151	if !s.Role.Valid() || !Known(p) {152		return false153	}154	allowed := slices.Contains(RoleDefaults(s.Role), p)155	for _, g := range grants {156		if !g.appliesTo(s, p, r) {157			continue158		}159		if g.Effect == Deny {160			return false161		}162		if g.Effect == Allow && grantable(s.Role, p) {163			allowed = true164		}165	}166	return allowed167}168169// DecideFacets is Decide for a resource that can be addressed in several ways170// at once (a node by its id, by its site and by each of its tags; a service171// by its id and by its node). A deny applying to any facet wins; otherwise172// the role default or an allow applying to any facet grants access. With no173// facet it is exactly Decide on the zero Resource.174func DecideFacets(s Subject, p Permission, facets []Resource, grants []Grant) bool {175	if !s.Role.Valid() || !Known(p) {176		return false177	}178	allowed := slices.Contains(RoleDefaults(s.Role), p)179	for _, g := range grants {180		applies := g.appliesTo(s, p, Resource{})181		for i := 0; !applies && i < len(facets); i++ {182			applies = g.appliesTo(s, p, facets[i])183		}184		if !applies {185			continue186		}187		if g.Effect == Deny {188			return false189		}190		if g.Effect == Allow && grantable(s.Role, p) {191			allowed = true192		}193	}194	return allowed195}196197// Grantable reports whether an allow-grant of p can have any effect for a198// subject holding role: Decide ignores the others.199func Grantable(role Role, p Permission) bool { return role.Valid() && Known(p) && grantable(role, p) }200201// grantable bounds what custom allow-grants may extend.202func grantable(role Role, p Permission) bool {203	switch p {204	case ActionsRunCritical:205		return role == Owner206	case UsersManage, InvitationsManage, AgentsManage, SettingsManage, AuditExport:207		return role.Rank() >= Admin.Rank()208	}209	return true210}211212// Effective lists the permissions a subject holds on unscoped resources.213func Effective(s Subject, grants []Grant) []Permission {214	var out []Permission215	for _, p := range All {216		if Decide(s, p, Resource{}, grants) {217			out = append(out, p)218		}219	}220	return out221}222223// CanAssignRole reports whether actor may give target the role next.224// Only an owner may create, modify or demote an owner.225func CanAssignRole(actor Role, current, next Role) bool {226	if !next.Valid() || actor.Rank() < Admin.Rank() {227		return false228	}229	if current == Owner || next == Owner {230		return actor == Owner231	}232	return true233}234
Files main
Menu

Theme