One Rollen- und Befugnisdatensatz per company (docgen#1 plan 13781 §3.2
no. 2, owner addendum 13791): functions, who holds them, deputies with
their basis, the deciding function per decision type, immediate
authority, standard releases, named editors and the contact path for
critical security events.
- internal/roles: the catalogue in code (version r1-2026-10-26.1), the
union of A1 §2.3 and A4 §4 = 21 functions, 14 customer decision types;
only the three releases carry an owner default (management), the rest
must be set by the company.
- Rules: a deputy is wirksam only with a documented basis, within its
validity and, where it covers management authority, with management's
authorization (B7 wording: what management records it has authorized);
another release body for R1-D02 updates only with the R1-D02 provision
(B8); management and document owners write, the OWNER sign-in role may
only record the first management holder; recorded_*/authorized_* are
stamped from the token, never from the body; no self-authorization.
- Routes: GET/PUT /v1/tenants/{id}/role-dataset, GET …/revisions/{rev},
GET …/resolve. Writes need the user's token; user tokens on these
routes form route group role-dataset.user, so the portal's sign-in
client needs no tenants grant.
- Migration 0024: role_datasets and role_dataset_revisions, versioned as
the company profile (expected_revision, 409 on conflict).
- Audit role_dataset.saved with revision and changed entry ids only.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014aRnCGqELxoix5TfpX8Ejf
163 lines
4.8 KiB
Go
163 lines
4.8 KiB
Go
package server
|
|
|
|
import (
|
|
"log/slog"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"git.breakpilot.com/platform/tenant-registry/internal/authn"
|
|
)
|
|
|
|
// Per-client authorization (PLATFORM_ARCHITECTURE §5c): each Keycloak service
|
|
// client (JWT azp) may only call the route groups it is mapped to.
|
|
//
|
|
// AUTHZ_RULES = "portal-svc-stage:tenants,catalog,api-keys,audit,users;pruefwerk-svc-stage:entitlements,tenants.read,api-keys.verify,audit.write;erp-svc:erp"
|
|
// A group of "*" allows everything. With AUTHZ_RULES unset every
|
|
// authenticated client may call every route (dev; logged at startup).
|
|
type Authz struct {
|
|
rules map[string]map[string]bool // azp → group → allowed
|
|
}
|
|
|
|
// ParseAuthzRules parses the AUTHZ_RULES syntax. Empty ⇒ nil (allow all).
|
|
func ParseAuthzRules(raw string) *Authz {
|
|
raw = strings.TrimSpace(raw)
|
|
if raw == "" {
|
|
return nil
|
|
}
|
|
a := &Authz{rules: map[string]map[string]bool{}}
|
|
for _, entry := range strings.Split(raw, ";") {
|
|
entry = strings.TrimSpace(entry)
|
|
if entry == "" {
|
|
continue
|
|
}
|
|
azp, groups, ok := strings.Cut(entry, ":")
|
|
if !ok {
|
|
continue
|
|
}
|
|
set := map[string]bool{}
|
|
for _, g := range strings.Split(groups, ",") {
|
|
if g = strings.TrimSpace(g); g != "" {
|
|
set[g] = true
|
|
}
|
|
}
|
|
a.rules[strings.TrimSpace(azp)] = set
|
|
}
|
|
return a
|
|
}
|
|
|
|
// Allowed reports whether client azp may call the given route group. A group
|
|
// "x.read" is also satisfied by the wider "x" grant.
|
|
func (a *Authz) Allowed(azp, group string) bool {
|
|
if a == nil {
|
|
return true
|
|
}
|
|
set, ok := a.rules[azp]
|
|
if !ok {
|
|
return false
|
|
}
|
|
if set["*"] || set[group] {
|
|
return true
|
|
}
|
|
if base, _, hasSub := strings.Cut(group, "."); hasSub && set[base] {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// routeGroup classifies a request. Keep in sync with NewRouter.
|
|
func routeGroup(method, path string) string {
|
|
switch {
|
|
case strings.HasPrefix(path, "/v1/admin/"):
|
|
return "admin"
|
|
case strings.HasPrefix(path, "/v1/notifications"):
|
|
return "notify"
|
|
case strings.HasPrefix(path, "/v1/internal/entitlements/"):
|
|
return "entitlements"
|
|
case strings.HasPrefix(path, "/v1/internal/api-keys/verify"):
|
|
return "api-keys.verify"
|
|
case strings.HasPrefix(path, "/v1/internal/keycloak/"):
|
|
return "keycloak"
|
|
case strings.HasPrefix(path, "/v1/internal/erp/"):
|
|
return "erp"
|
|
case strings.HasPrefix(path, "/v1/entitlements"):
|
|
return "entitlements"
|
|
case strings.HasPrefix(path, "/v1/catalog"):
|
|
return "catalog"
|
|
case strings.HasPrefix(path, "/v1/api-keys"):
|
|
return "api-keys"
|
|
case strings.HasPrefix(path, "/v1/audit"):
|
|
if method == http.MethodGet {
|
|
return "audit.read"
|
|
}
|
|
return "audit.write"
|
|
case strings.HasPrefix(path, "/v1/users/"):
|
|
return "users"
|
|
case strings.HasPrefix(path, "/v1/usage"):
|
|
if method == http.MethodGet {
|
|
return "usage.read"
|
|
}
|
|
return "usage"
|
|
case strings.HasPrefix(path, "/v1/subscriptions"):
|
|
return "tenants.read"
|
|
case strings.HasPrefix(path, "/v1/licences"):
|
|
return "licences"
|
|
case strings.HasPrefix(path, "/v1/trial-offers"):
|
|
return "tenants.write"
|
|
case strings.HasPrefix(path, "/v1/tenants"):
|
|
if method == http.MethodGet {
|
|
return "tenants.read"
|
|
}
|
|
return "tenants.write"
|
|
}
|
|
return "other"
|
|
}
|
|
|
|
// requireAuthz sits behind requireAuth. Without a principal (AUTH_ENABLED=false)
|
|
// or without rules it passes through.
|
|
func (s *Server) requireAuthz(next http.Handler) http.Handler {
|
|
if s.Authz == nil {
|
|
return next
|
|
}
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
p, ok := authn.PrincipalFrom(r.Context())
|
|
if !ok {
|
|
next.ServeHTTP(w, r) // auth disabled: nothing to authorize against
|
|
return
|
|
}
|
|
group := routeGroup(r.Method, r.URL.Path)
|
|
// A user token on the key routes can only reach the integration
|
|
// path (apikeys_integration.go), which checks tenant and role
|
|
// itself; plain "api-keys" (any tenant) stays a service grant.
|
|
if group == "api-keys" && p.IsUser() {
|
|
group = "api-keys.integration"
|
|
}
|
|
// A user token on the role dataset (R1 S0) reaches only these
|
|
// routes: the handlers check tenant and function themselves, so the
|
|
// portal's sign-in client needs no tenants grant.
|
|
if p.IsUser() && isRoleDatasetPath(r.URL.Path) {
|
|
group = "role-dataset.user"
|
|
}
|
|
if !s.Authz.Allowed(p.ClientID, group) {
|
|
s.Log.Warn("client not authorized for route group", "azp", p.ClientID, "group", group, "path", r.URL.Path)
|
|
writeError(w, http.StatusForbidden, "FORBIDDEN_CLIENT", "client "+p.ClientID+" may not call "+group)
|
|
return
|
|
}
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
// LogAuthz emits the startup line so an open registry is never silent.
|
|
func LogAuthz(log *slog.Logger, a *Authz) {
|
|
if a == nil {
|
|
log.Warn("AUTHZ_RULES not set — every authenticated client may call every route (dev only)")
|
|
return
|
|
}
|
|
for azp, groups := range a.rules {
|
|
keys := make([]string, 0, len(groups))
|
|
for g := range groups {
|
|
keys = append(keys, g)
|
|
}
|
|
log.Info("authz rule", "azp", azp, "groups", keys)
|
|
}
|
|
}
|