From 19f23009342f36c93ed754924a3f1b2f44cde486 Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com> Date: Tue, 10 Nov 2026 22:46:34 +0100 Subject: [PATCH] feat(authz): the catalogue of rights (RBAC step 1) Capabilities, bundles and the two position variants, versioned in code and served as GET /v1/authz/catalogue. Today's six roles are bundles that reproduce the portal surfaces, the Werkmeister permissions and the registry's own role checks; pin tests fail if any of them drifts. Nothing reads the catalogue yet, so no right changes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014aRnCGqELxoix5TfpX8Ejf --- internal/authz/catalogue.go | 298 +++++++++++++++++++++++++++++ internal/authz/catalogue_test.go | 162 ++++++++++++++++ internal/server/authz.go | 2 + internal/server/authz_catalogue.go | 16 ++ internal/server/authz_test.go | 2 + internal/server/rights_pin_test.go | 60 ++++++ internal/server/server.go | 2 + 7 files changed, 542 insertions(+) create mode 100644 internal/authz/catalogue.go create mode 100644 internal/authz/catalogue_test.go create mode 100644 internal/server/authz_catalogue.go create mode 100644 internal/server/rights_pin_test.go diff --git a/internal/authz/catalogue.go b/internal/authz/catalogue.go new file mode 100644 index 0000000..79f968d --- /dev/null +++ b/internal/authz/catalogue.go @@ -0,0 +1,298 @@ +// Package authz is the catalogue of rights of the platform (RBAC plan, +// approved 2026-11-10): capabilities, the bundles that grant them and the +// position presets of the two company variants. +// +// - A capability is what the software lets someone do, named like an API +// key scope: ":." (internal/scopes). Each +// service checks capabilities, never role names. +// - A bundle is the one sign-in role a person holds (org_roles). It grants +// a set of capabilities. A person holds exactly one bundle and may be +// given individual capabilities on top; custom bundles are an +// enterprise feature for later. +// - A position proposes a bundle and the company functions (internal/roles) +// that usually go with it, for a small company (variant S) or a larger +// one (variant L). It grants nothing itself. +// +// Step 1 of the plan: the catalogue only. Nothing reads it to decide access +// yet; the today's bundles reproduce the role checks of the registry, the +// portal and Werkmeister as they stand (pinned in tests), so moving the +// services onto it (step 2) changes nobody's rights. The new bundles are +// defined but not assignable until step 4. +package authz + +// Version names this catalogue; it changes with every change of a +// capability, bundle or position. +const Version = "2026-11-10.1" + +// Products a capability belongs to. +const ( + Portal = "portal" + Werkmeister = "werkmeister" + Registry = "registry" +) + +// Capability is one right. +type Capability struct { + Code string `json:"code"` + Product string `json:"product"` + Label string `json:"label"` + // Admin: granting it on its own needs a second person (four-eyes), and + // nobody grants a capability they do not hold. + Admin bool `json:"admin,omitempty"` +} + +// Bundle is a sign-in role. +type Bundle struct { + Code string `json:"code"` + Label string `json:"label"` + // Assignable: a person may be given it. The new bundles become + // assignable with the team and setup UI (step 4). + Assignable bool `json:"assignable"` + Capabilities []string `json:"capabilities"` +} + +// Variants of company positions. +const ( + VariantSmall = "S" // few people, each covering many functions + VariantLarge = "L" // one position per function, with deputies +) + +// Position is a preset: a bundle and the functions that usually go with it. +type Position struct { + Code string `json:"code"` + Variant string `json:"variant"` + Label string `json:"label"` + Bundle string `json:"bundle"` + Functions []string `json:"functions"` +} + +// The capabilities. Portal surfaces are "portal:.view"; what the +// portal lets one change on a surface has its own capability. +var Capabilities = []Capability{ + // ── Portal: surfaces (src/lib/session.ts surfaceRoles) ──────────────── + {Code: "portal:dashboard.view", Product: Portal, Label: "Übersicht ansehen"}, + {Code: "portal:products.view", Product: Portal, Label: "Produkte ansehen"}, + {Code: "portal:projects.view", Product: Portal, Label: "Projekte ansehen"}, + {Code: "portal:profile.view", Product: Portal, Label: "Unternehmensprofil ansehen"}, + {Code: "portal:roles.view", Product: Portal, Label: "Verantwortliche und Befugnisse ansehen"}, + {Code: "portal:security.view", Product: Portal, Label: "Sicherheitsereignis melden"}, + {Code: "portal:settings.view", Product: Portal, Label: "Organisation verwalten", Admin: true}, + {Code: "portal:users.view", Product: Portal, Label: "Team verwalten", Admin: true}, + {Code: "portal:api-keys.view", Product: Portal, Label: "API-Schlüssel verwalten", Admin: true}, + {Code: "portal:integrations.view", Product: Portal, Label: "Integrationen verwalten", Admin: true}, + {Code: "portal:billing.view", Product: Portal, Label: "Abrechnung ansehen"}, + {Code: "portal:audit.view", Product: Portal, Label: "Audit-Log ansehen"}, + {Code: "portal:support.view", Product: Portal, Label: "Hilfe und Support"}, + {Code: "portal:catalog.view", Product: Portal, Label: "Produktkatalog ansehen"}, + {Code: "portal:documents.view", Product: Portal, Label: "Dokumente ansehen"}, + {Code: "portal:store.view", Product: Portal, Label: "Dokumentenablage ansehen"}, + {Code: "portal:contracts.view", Product: Portal, Label: "Vertragsunterlagen ansehen"}, + {Code: "portal:infra.view", Product: Portal, Label: "Verwaltete Infrastruktur ansehen"}, + {Code: "portal:studio.view", Product: Portal, Label: "Studio"}, + // ── Portal: changes ───────────────────────────────────────────────────── + {Code: "portal:products.write", Product: Portal, Label: "In den Produkten arbeiten"}, + {Code: "portal:infra.write", Product: Portal, Label: "Änderungen an der verwalteten Infrastruktur beantragen", Admin: true}, + {Code: "portal:profile.edit", Product: Portal, Label: "Unternehmensprofil bearbeiten"}, + {Code: "portal:store.upload", Product: Portal, Label: "Dokumente in die Ablage hochladen"}, + {Code: "portal:projects.edit", Product: Portal, Label: "Projekte anlegen und ändern"}, + {Code: "portal:documents.chunks", Product: Portal, Label: "Gelesene Abschnitte eines Dokuments ansehen"}, + {Code: "portal:documents.reread", Product: Portal, Label: "Dokument erneut lesen lassen"}, + // ── Werkmeister (internal/rbac) ───────────────────────────────────────── + {Code: "werkmeister:process.view", Product: Werkmeister, Label: "Vorgänge ansehen"}, + {Code: "werkmeister:run.start", Product: Werkmeister, Label: "Vorgänge starten"}, + {Code: "werkmeister:task.work", Product: Werkmeister, Label: "Schritte bearbeiten"}, + {Code: "werkmeister:process.edit", Product: Werkmeister, Label: "Abläufe bearbeiten"}, + {Code: "werkmeister:process.publish", Product: Werkmeister, Label: "Abläufe veröffentlichen"}, + {Code: "werkmeister:connection.manage", Product: Werkmeister, Label: "Verbindungen verwalten", Admin: true}, + {Code: "werkmeister:documents.read", Product: Werkmeister, Label: "Gelesene Produktdokumente ansehen"}, + // ── Registry (internal/server) ────────────────────────────────────────── + {Code: "registry:members.manage", Product: Registry, Label: "Mitglieder einladen und verwalten", Admin: true}, + {Code: "registry:api-keys.manage", Product: Registry, Label: "API-Schlüssel anlegen und widerrufen", Admin: true}, + {Code: "registry:pruefwerk.settings", Product: Registry, Label: "Prüfwerk-Einstellungen ändern"}, + {Code: "registry:services.read", Product: Registry, Label: "Verwaltete Dienste ansehen"}, + {Code: "registry:notify.documents", Product: Registry, Label: "Benachrichtigung über veröffentlichte Dokumente"}, + {Code: "registry:notify.findings", Product: Registry, Label: "Benachrichtigung über neue Befunde"}, + {Code: "registry:intake.report", Product: Registry, Label: "Sicherheitsereignisse melden und eigene Meldungen sehen"}, +} + +// Capability groups the bundles share. +var ( + adminPortal = []string{"portal:dashboard.view", "portal:products.view", "portal:projects.view", "portal:profile.view", "portal:roles.view", + "portal:security.view", "portal:settings.view", "portal:users.view", "portal:api-keys.view", "portal:integrations.view", "portal:billing.view", + "portal:audit.view", "portal:support.view", "portal:catalog.view", "portal:documents.view", "portal:store.view", "portal:contracts.view", + "portal:infra.view", "portal:studio.view", "portal:products.write", "portal:infra.write", "portal:profile.edit", "portal:store.upload", + "portal:projects.edit", "portal:documents.chunks", "portal:documents.reread"} + adminWerkmeister = []string{"werkmeister:process.view", "werkmeister:run.start", "werkmeister:task.work", "werkmeister:process.edit", + "werkmeister:process.publish", "werkmeister:connection.manage", "werkmeister:documents.read"} + adminRegistry = []string{"registry:members.manage", "registry:api-keys.manage", "registry:pruefwerk.settings", "registry:services.read", + "registry:notify.documents", "registry:notify.findings", "registry:intake.report"} + // Everyone in a workspace sees the overview, who decides what, help, + // and reports security events (A1 AS-5). + member = []string{"portal:dashboard.view", "portal:roles.view", "portal:security.view", "portal:support.view", "registry:intake.report"} +) + +func join(groups ...[]string) []string { + out := []string{} + for _, g := range groups { + out = append(out, g...) + } + return out +} + +// Bundles: today's six first, as the registry, the portal and Werkmeister +// grant them on 2026-11-10; then the new ones of variant L and EXTERNAL. +var Bundles = []Bundle{ + {Code: "IT_ADMIN", Label: "IT-Administration", Assignable: true, Capabilities: join(adminPortal, adminWerkmeister, adminRegistry)}, + // OWNER has IT_ADMIN's rights. The portal does not know OWNER yet; it + // will show it like IT_ADMIN (step 2, decided 2026-11-10). + {Code: "OWNER", Label: "Inhaber des Arbeitsbereichs", Assignable: true, Capabilities: join(adminPortal, adminWerkmeister, adminRegistry)}, + {Code: "CXO", Label: "Geschäftsleitung", Assignable: true, Capabilities: join(member, []string{ + "portal:products.view", "portal:projects.view", "portal:profile.view", "portal:billing.view", "portal:audit.view", "portal:catalog.view", + "portal:documents.view", "portal:store.view", "portal:contracts.view", "portal:infra.view", "portal:studio.view", + "portal:profile.edit", "portal:store.upload", "portal:projects.edit", + "werkmeister:process.view", + "registry:services.read", "registry:notify.documents", "registry:notify.findings"})}, + // FINANCE stays a portal-only bundle (decided 2026-11-10). + {Code: "FINANCE", Label: "Finanzen", Assignable: true, Capabilities: join(member, []string{ + "portal:projects.view", "portal:profile.view", "portal:billing.view", "portal:documents.view", "portal:store.view", "portal:contracts.view", + "registry:pruefwerk.settings", "registry:notify.documents"})}, + {Code: "LEGAL", Label: "Recht", Assignable: true, Capabilities: join(member, []string{ + "portal:products.view", "portal:projects.view", "portal:profile.view", "portal:audit.view", "portal:documents.view", "portal:store.view", + "portal:contracts.view", "portal:studio.view", "portal:profile.edit", "portal:store.upload", "portal:projects.edit", + "werkmeister:process.view", + "registry:notify.documents", "registry:notify.findings"})}, + {Code: "USER", Label: "Mitarbeitende", Assignable: true, Capabilities: join(member, []string{ + "portal:products.view", "portal:projects.view", "portal:studio.view", "portal:products.write", + "werkmeister:process.view", "werkmeister:run.start", "werkmeister:task.work"})}, + + // ── New (all taken 2026-11-10; assignable with step 4) ───────────────── + {Code: "PSIRT", Label: "Produktsicherheit (PSIRT)", Capabilities: join(member, []string{ + "portal:products.view", "portal:projects.view", "portal:documents.view", "portal:studio.view", + "werkmeister:process.view", "werkmeister:run.start", "werkmeister:task.work", + "registry:notify.findings"})}, + {Code: "DEVELOPMENT", Label: "Entwicklung", Capabilities: join(member, []string{ + "portal:products.view", "portal:projects.view", "portal:studio.view", "portal:products.write", + "werkmeister:process.view", "werkmeister:run.start", "werkmeister:task.work", + "registry:notify.findings"})}, + {Code: "QUALITY", Label: "Qualität und Konformität", Capabilities: join(member, []string{ + "portal:products.view", "portal:projects.view", "portal:profile.view", "portal:documents.view", "portal:store.view", "portal:contracts.view", + "portal:audit.view", "portal:studio.view", "portal:profile.edit", "portal:projects.edit", "portal:store.upload", + "werkmeister:process.view", "werkmeister:task.work", + "registry:notify.documents"})}, + {Code: "PRIVACY", Label: "Datenschutz", Capabilities: join(member, []string{ + "portal:profile.view", "portal:documents.view", "portal:store.view", "portal:contracts.view", "portal:audit.view", "portal:studio.view", + "portal:profile.edit", + "werkmeister:process.view", + "registry:notify.documents"})}, + {Code: "PURCHASING", Label: "Einkauf und Lieferanten", Capabilities: join(member, []string{ + "portal:products.view", "portal:projects.view", "portal:documents.view", "portal:contracts.view", "portal:studio.view", + "werkmeister:process.view", "werkmeister:task.work", + "registry:notify.documents"})}, + // AUDITOR reads everything a CXO reads, changes nothing. + {Code: "AUDITOR", Label: "Revision", Capabilities: join(member, []string{ + "portal:products.view", "portal:projects.view", "portal:profile.view", "portal:billing.view", "portal:audit.view", "portal:catalog.view", + "portal:documents.view", "portal:store.view", "portal:contracts.view", "portal:infra.view", "portal:studio.view", + "werkmeister:process.view", + "registry:services.read", "registry:notify.documents"})}, + // EXTERNAL: only what the functions they hold bring (step 5); until + // Werkmeister scopes cases to functions, no Werkmeister access at all, + // since process.view would show every case of the workspace. + {Code: "EXTERNAL", Label: "Externer Dienstleister", Capabilities: []string{"portal:security.view", "portal:support.view", "registry:intake.report"}}, +} + +// Positions: the presets of the two variants. Proposals, changed per +// person when they are added. +var Positions = []Position{ + {Code: "s.geschaeftsfuehrung", Variant: VariantSmall, Label: "Geschäftsführung", Bundle: "CXO", + Functions: []string{"r1.geschaeftsleitung", "r1.freigabestelle", "r1.freigabe_produktaenderung", "r1.vorfallkontakt", "r1.meldeberechtigt", "r1.abschlussverantwortung"}}, + {Code: "s.it_technik", Variant: VariantSmall, Label: "IT und Technik", Bundle: "IT_ADMIN", + Functions: []string{"r1.produkt_cybersecurity", "r1.technischer_ansprechpartner", "r1.technischer_produktverantwortlicher", "r1.produktsicherheit", "r1.dokumentenlenkung", "r1.aenderungskoordination"}}, + {Code: "s.mitarbeitende", Variant: VariantSmall, Label: "Mitarbeitende", Bundle: "USER", Functions: []string{"r1.massnahmenausfuehrer"}}, + {Code: "s.buchhaltung", Variant: VariantSmall, Label: "Buchhaltung", Bundle: "FINANCE", Functions: []string{}}, + {Code: "s.externe_beratung", Variant: VariantSmall, Label: "Externe Beratung", Bundle: "EXTERNAL", Functions: []string{"r1.externer_cybersecurity_dienstleister", "r1.fachpruefung"}}, + + {Code: "l.geschaeftsleitung", Variant: VariantLarge, Label: "Geschäftsleitung", Bundle: "CXO", Functions: []string{"r1.geschaeftsleitung", "r1.freigabestelle", "r1.freigabe_produktaenderung"}}, + {Code: "l.it_administration", Variant: VariantLarge, Label: "IT-Administration", Bundle: "IT_ADMIN", Functions: []string{"r1.technischer_ansprechpartner"}}, + {Code: "l.produktsicherheit", Variant: VariantLarge, Label: "Produktsicherheit (PSIRT)", Bundle: "PSIRT", Functions: []string{"r1.produkt_cybersecurity", "r1.vorfallkontakt", "r1.meldeberechtigt"}}, + {Code: "l.entwicklung", Variant: VariantLarge, Label: "Entwicklung", Bundle: "DEVELOPMENT", Functions: []string{"r1.technischer_produktverantwortlicher", "r1.produktverantwortung", "r1.massnahmenausfuehrer"}}, + {Code: "l.qualitaet", Variant: VariantLarge, Label: "Qualität und Konformität", Bundle: "QUALITY", + Functions: []string{"r1.konformitaet", "r1.produktsicherheit", "r1.dokumentenlenkung", "r1.prueffunktion", "r1.abschlussverantwortung"}}, + {Code: "l.datenschutz", Variant: VariantLarge, Label: "Datenschutz", Bundle: "PRIVACY", Functions: []string{}}, + {Code: "l.einkauf", Variant: VariantLarge, Label: "Einkauf und Lieferanten", Bundle: "PURCHASING", Functions: []string{}}, + {Code: "l.recht", Variant: VariantLarge, Label: "Recht", Bundle: "LEGAL", Functions: []string{}}, + {Code: "l.revision", Variant: VariantLarge, Label: "Revision", Bundle: "AUDITOR", Functions: []string{}}, + {Code: "l.buchhaltung", Variant: VariantLarge, Label: "Buchhaltung", Bundle: "FINANCE", Functions: []string{}}, + {Code: "l.mitarbeitende", Variant: VariantLarge, Label: "Mitarbeitende", Bundle: "USER", Functions: []string{"r1.massnahmenausfuehrer"}}, + {Code: "l.extern", Variant: VariantLarge, Label: "Externer Dienstleister", Bundle: "EXTERNAL", Functions: []string{"r1.externer_cybersecurity_dienstleister", "r1.fachpruefung"}}, +} + +var ( + capIndex = map[string]Capability{} + bundleIndex = map[string]Bundle{} +) + +func init() { + for _, c := range Capabilities { + capIndex[c.Code] = c + } + for _, b := range Bundles { + bundleIndex[b.Code] = b + } +} + +// BundleByCode looks a bundle up. +func BundleByCode(code string) (Bundle, bool) { + b, ok := bundleIndex[code] + return b, ok +} + +// CapabilityByCode looks a capability up. +func CapabilityByCode(code string) (Capability, bool) { + c, ok := capIndex[code] + return c, ok +} + +// Grants reports whether bundle code grants capability cap. An unknown +// bundle grants nothing (fail closed). +func Grants(bundle, capability string) bool { + b, ok := bundleIndex[bundle] + if !ok { + return false + } + for _, c := range b.Capabilities { + if c == capability { + return true + } + } + return false +} + +// Effective is what a person may do: their bundle's capabilities and their +// individual ones that the catalogue knows; nothing else. +func Effective(bundle string, extra []string) map[string]bool { + out := map[string]bool{} + if b, ok := bundleIndex[bundle]; ok { + for _, c := range b.Capabilities { + out[c] = true + } + } + for _, c := range extra { + if _, ok := capIndex[c]; ok { + out[c] = true + } + } + return out +} + +// Catalogue is the answer of GET /v1/authz/catalogue. +type Catalogue struct { + Version string `json:"version"` + Capabilities []Capability `json:"capabilities"` + Bundles []Bundle `json:"bundles"` + Positions []Position `json:"positions"` +} + +// Current is the catalogue as served. +func Current() Catalogue { + return Catalogue{Version: Version, Capabilities: Capabilities, Bundles: Bundles, Positions: Positions} +} diff --git a/internal/authz/catalogue_test.go b/internal/authz/catalogue_test.go new file mode 100644 index 0000000..d72f13c --- /dev/null +++ b/internal/authz/catalogue_test.go @@ -0,0 +1,162 @@ +package authz + +import ( + "slices" + "testing" + + "git.breakpilot.com/platform/tenant-registry/internal/roles" +) + +// The catalogue holds together: unique codes, bundles and positions name +// what exists, positions name functions of the role dataset. +func TestTheCatalogueHoldsTogether(t *testing.T) { + seen := map[string]bool{} + for _, c := range Capabilities { + if seen[c.Code] || c.Label == "" || (c.Product != Portal && c.Product != Werkmeister && c.Product != Registry) { + t.Errorf("capability %q: duplicate, unlabelled or of no product", c.Code) + } + seen[c.Code] = true + } + bundles := map[string]bool{} + for _, b := range Bundles { + if bundles[b.Code] || b.Label == "" { + t.Errorf("bundle %q: duplicate or unlabelled", b.Code) + } + bundles[b.Code] = true + dup := map[string]bool{} + for _, c := range b.Capabilities { + if !seen[c] { + t.Errorf("bundle %s grants unknown capability %q", b.Code, c) + } + if dup[c] { + t.Errorf("bundle %s grants %q twice", b.Code, c) + } + dup[c] = true + } + } + positions := map[string]bool{} + for _, p := range Positions { + if positions[p.Code] || (p.Variant != VariantSmall && p.Variant != VariantLarge) || !bundles[p.Bundle] { + t.Errorf("position %q: duplicate, of no variant or of unknown bundle %q", p.Code, p.Bundle) + } + positions[p.Code] = true + for _, f := range p.Functions { + if _, ok := roles.FunctionByKey(f); !ok { + t.Errorf("position %s proposes unknown function %q", p.Code, f) + } + } + } + // Today's six are assignable; the new ones wait for step 4. + for _, code := range []string{"IT_ADMIN", "OWNER", "CXO", "FINANCE", "LEGAL", "USER"} { + if b, _ := BundleByCode(code); !b.Assignable { + t.Errorf("%s is not assignable", code) + } + } + for _, code := range []string{"PSIRT", "DEVELOPMENT", "QUALITY", "PRIVACY", "PURCHASING", "AUDITOR", "EXTERNAL"} { + if b, ok := BundleByCode(code); !ok || b.Assignable { + t.Errorf("%s: missing, or assignable before step 4", code) + } + } +} + +// Today's rights of the portal and Werkmeister on 2026-11-10, copied from +// their code: portal src/lib/session.ts (surfaceRoles, productWriteRoles, +// infraWriteRoles), lib/company-profile/registry.ts (PROFILE_EDITORS), +// lib/document-store.ts (STORE_UPLOADERS), lib/projects.ts +// (PROJECT_EDITORS), lib/chunks.ts (CHUNK_ROLES), app/[slug]/projects/ +// [pid]/documents/[doc] (reread: IT_ADMIN); Werkmeister internal/rbac +// (orgRoles). Step 2 moves those services onto the catalogue; until then +// this pins that the bundles say what they do. +var today = map[string][]string{ + "portal:dashboard.view": {"IT_ADMIN", "CXO", "FINANCE", "LEGAL", "USER"}, + "portal:products.view": {"IT_ADMIN", "CXO", "LEGAL", "USER"}, + "portal:projects.view": {"IT_ADMIN", "CXO", "FINANCE", "LEGAL", "USER"}, + "portal:profile.view": {"IT_ADMIN", "CXO", "FINANCE", "LEGAL"}, + "portal:roles.view": {"IT_ADMIN", "CXO", "FINANCE", "LEGAL", "USER"}, + "portal:security.view": {"IT_ADMIN", "CXO", "FINANCE", "LEGAL", "USER"}, + "portal:settings.view": {"IT_ADMIN"}, + "portal:users.view": {"IT_ADMIN"}, + "portal:api-keys.view": {"IT_ADMIN"}, + "portal:integrations.view": {"IT_ADMIN"}, + "portal:billing.view": {"IT_ADMIN", "CXO", "FINANCE"}, + "portal:audit.view": {"IT_ADMIN", "CXO", "LEGAL"}, + "portal:support.view": {"IT_ADMIN", "CXO", "FINANCE", "LEGAL", "USER"}, + "portal:catalog.view": {"IT_ADMIN", "CXO"}, + "portal:documents.view": {"IT_ADMIN", "CXO", "FINANCE", "LEGAL"}, + "portal:store.view": {"IT_ADMIN", "CXO", "FINANCE", "LEGAL"}, + "portal:contracts.view": {"IT_ADMIN", "CXO", "FINANCE", "LEGAL"}, + "portal:infra.view": {"IT_ADMIN", "CXO"}, + "portal:studio.view": {"IT_ADMIN", "CXO", "LEGAL", "USER"}, + "portal:products.write": {"IT_ADMIN", "USER"}, + "portal:infra.write": {"IT_ADMIN"}, + "portal:profile.edit": {"IT_ADMIN", "CXO", "LEGAL"}, + "portal:store.upload": {"IT_ADMIN", "CXO", "LEGAL"}, + "portal:projects.edit": {"IT_ADMIN", "CXO", "LEGAL"}, + "portal:documents.chunks": {"IT_ADMIN", "OWNER"}, + "portal:documents.reread": {"IT_ADMIN"}, + "werkmeister:process.view": {"IT_ADMIN", "OWNER", "USER", "CXO", "LEGAL"}, + "werkmeister:run.start": {"IT_ADMIN", "OWNER", "USER"}, + "werkmeister:task.work": {"IT_ADMIN", "OWNER", "USER"}, + "werkmeister:process.edit": {"IT_ADMIN", "OWNER"}, + "werkmeister:process.publish": {"IT_ADMIN", "OWNER"}, + "werkmeister:connection.manage": {"IT_ADMIN", "OWNER"}, + "werkmeister:documents.read": {"IT_ADMIN", "OWNER"}, +} + +// The one deliberate difference: the portal does not know OWNER today and +// will show it like IT_ADMIN (decided 2026-11-10), so OWNER's portal rights +// are IT_ADMIN's, not today's. +func TestTodaysBundlesReproduceThePortalAndWerkmeister(t *testing.T) { + for _, c := range Capabilities { + if c.Product == Registry { + continue // pinned against the registry's own code (internal/server) + } + want, ok := today[c.Code] + if !ok { + t.Errorf("%s: not pinned", c.Code) + continue + } + for _, b := range []string{"IT_ADMIN", "OWNER", "CXO", "FINANCE", "LEGAL", "USER"} { + has := slices.Contains(want, b) + if b == "OWNER" && c.Product == Portal { + has = slices.Contains(today[c.Code], "IT_ADMIN") + } + if Grants(b, c.Code) != has { + t.Errorf("%s %s: catalogue %v, today %v", b, c.Code, Grants(b, c.Code), has) + } + } + } +} + +// Rights are additive and fail closed. +func TestEffectiveRights(t *testing.T) { + if Grants("SOMEONE", "portal:dashboard.view") { + t.Fatal("an unknown bundle grants something") + } + e := Effective("USER", []string{"werkmeister:process.publish", "werkmeister:everything"}) + if !e["werkmeister:process.publish"] || !e["werkmeister:task.work"] || e["werkmeister:everything"] || e["portal:users.view"] { + t.Fatalf("bundle plus known extras only: %v", e) + } + if len(Effective("", nil)) != 0 { + t.Fatal("no bundle, no rights") + } + // New bundles never reach what only the administration may do. + for _, b := range []string{"PSIRT", "DEVELOPMENT", "QUALITY", "PRIVACY", "PURCHASING", "AUDITOR", "EXTERNAL"} { + for _, c := range Capabilities { + if c.Admin && Grants(b, c.Code) { + t.Errorf("%s grants the administration capability %s", b, c.Code) + } + } + } + // AUDITOR changes nothing; EXTERNAL sees no Werkmeister case until step 5. + for _, c := range []string{"portal:products.write", "portal:profile.edit", "portal:store.upload", "portal:projects.edit", "werkmeister:task.work", "werkmeister:run.start"} { + if Grants("AUDITOR", c) { + t.Errorf("AUDITOR may %s", c) + } + } + for _, c := range Capabilities { + if c.Product == Werkmeister && Grants("EXTERNAL", c.Code) { + t.Errorf("EXTERNAL may %s", c.Code) + } + } +} diff --git a/internal/server/authz.go b/internal/server/authz.go index 566d148..141b13c 100644 --- a/internal/server/authz.go +++ b/internal/server/authz.go @@ -85,6 +85,8 @@ func routeGroup(method, path string) string { return "entitlements" case strings.HasPrefix(path, "/v1/catalog"): return "catalog" + case strings.HasPrefix(path, "/v1/authz/"): + return "authz" case strings.HasPrefix(path, "/v1/api-keys"): return "api-keys" case strings.HasPrefix(path, "/v1/audit"): diff --git a/internal/server/authz_catalogue.go b/internal/server/authz_catalogue.go new file mode 100644 index 0000000..050e5de --- /dev/null +++ b/internal/server/authz_catalogue.go @@ -0,0 +1,16 @@ +package server + +import ( + "net/http" + + "git.breakpilot.com/platform/tenant-registry/internal/authz" +) + +// getAuthzCatalogue — GET /v1/authz/catalogue: the catalogue of rights +// (capabilities, bundles, position presets; RBAC plan step 1). Read-only +// and the same for everyone; services read it to expand a person's bundle +// (step 2). Route group "authz": a service client needs that grant in +// AUTHZ_RULES. Nothing decides access by it yet. +func (s *Server) getAuthzCatalogue(w http.ResponseWriter, r *http.Request) { + writeJSON(w, http.StatusOK, authz.Current()) +} diff --git a/internal/server/authz_test.go b/internal/server/authz_test.go index 781cc0f..7a0a583 100644 --- a/internal/server/authz_test.go +++ b/internal/server/authz_test.go @@ -20,6 +20,8 @@ func TestAuthzRules(t *testing.T) { {"pruefwerk-svc", "GET", "/v1/audit", false}, {"root", "DELETE", "/v1/api-keys/x", true}, {"unknown", "GET", "/v1/catalog", false}, + {"portal-svc", "GET", "/v1/authz/catalogue", false}, + {"root", "GET", "/v1/authz/catalogue", true}, } for _, c := range cases { if got := a.Allowed(c.azp, routeGroup(c.method, c.path)); got != c.want { diff --git a/internal/server/rights_pin_test.go b/internal/server/rights_pin_test.go new file mode 100644 index 0000000..026a836 --- /dev/null +++ b/internal/server/rights_pin_test.go @@ -0,0 +1,60 @@ +package server + +import ( + "net/http/httptest" + "slices" + "strings" + "testing" + + "git.breakpilot.com/platform/tenant-registry/internal/authz" + "git.breakpilot.com/platform/tenant-registry/internal/mail" +) + +// RBAC step 1: the catalogue's bundles grant exactly what the registry's +// role checks grant today, read from the live variables, so the handlers +// can move onto capabilities (step 2) without changing anybody's rights. +func TestTheBundlesReproduceTheRegistrysRoleChecks(t *testing.T) { + // The roles a member can be given are today's assignable bundles. + assignable := []string{} + for _, b := range authz.Bundles { + if b.Assignable { + assignable = append(assignable, b.Code) + } + } + if !slices.Equal(assignable, teamRoles) { + t.Fatalf("assignable bundles %v, team roles %v", assignable, teamRoles) + } + for _, role := range teamRoles { + pin := map[string]bool{ + "registry:members.manage": isAdminRole([]string{role}), + "registry:api-keys.manage": isAdminRole([]string{role}), + "registry:pruefwerk.settings": pruefwerkSettingsRole[role], + "registry:services.read": serviceReadRole[role], + "registry:notify.documents": slices.Contains(notifyRoles[mail.KindDocumentPublished], role), + "registry:notify.findings": slices.Contains(notifyRoles[mail.KindFindingsNew], role), + "registry:intake.report": true, // every member reports (A1 AS-5) + } + for capability, want := range pin { + if got := authz.Grants(role, capability); got != want { + t.Errorf("%s %s: catalogue %v, registry %v", role, capability, got, want) + } + } + } + for _, c := range authz.Capabilities { + if c.Product == authz.Registry { + if _, ok := map[string]bool{"registry:members.manage": true, "registry:api-keys.manage": true, "registry:pruefwerk.settings": true, + "registry:services.read": true, "registry:notify.documents": true, "registry:notify.findings": true, "registry:intake.report": true}[c.Code]; !ok { + t.Errorf("%s: not pinned against a registry check", c.Code) + } + } + } +} + +// GET /v1/authz/catalogue serves the catalogue as it is in code. +func TestTheCatalogueIsServed(t *testing.T) { + rec := httptest.NewRecorder() + (&Server{}).getAuthzCatalogue(rec, nil) + if rec.Code != 200 || !strings.Contains(rec.Body.String(), `"version":"`+authz.Version+`"`) || !strings.Contains(rec.Body.String(), `"code":"PSIRT"`) { + t.Fatalf("%d %s", rec.Code, rec.Body.String()) + } +} diff --git a/internal/server/server.go b/internal/server/server.go index 5edba43..65aa82b 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -178,6 +178,8 @@ func NewRouter(s *Server) http.Handler { // catalog mux.HandleFunc("GET /v1/catalog", s.getCatalog) + // The catalogue of rights (RBAC step 1): read-only, group "authz". + mux.HandleFunc("GET /v1/authz/catalogue", s.getAuthzCatalogue) mux.HandleFunc("POST /v1/catalog/request", s.catalogRequest) mux.HandleFunc("POST /v1/catalog/trial-request", s.catalogTrialRequest)