refactor(rbac): centralise tenant-access guards in middleware
Tenant-access decisions previously lived in four places with subtly different shapes: canAccessTenant in middleware/auth.go, an unexported isCrossTenantSuperuser in middleware/rbac.go, authorizeTenantAccess + two opening if-blocks in handler/tenant.go, and resolveTenantIDFromPath in handler/tenant_member.go. Each variant had its own logging, its own treatment of the cluster-wide EnableCrossTenantAccess flag, and its own subtle difference in what "URL :id matches active tenant" meant. Collapse all four into middleware/access.go: - IsCrossTenantSuperuser(ctx, cfg): exported, flag-aware. Both the per-user CanAccessAllTenants attribute AND cfg.Tenant.EnableCrossTenantAccess must be true; either alone rejects, so flipping the cluster flag off operationally revokes every superuser without a token re-issue. - IsTenantAccessible(ctx, user, target, memberService, cfg): home tenant + superuser path + active tenant_members row. Replaces the old superuser-only canAccessTenant; the X-Tenant-ID gate now also honours ordinary multi-tenant memberships. - RequireCrossTenantAccess(cfg): route guard for /tenants/all and /tenants/search, replacing the 12-line if-blocks at the top of ListAllTenants and SearchTenants. - RequirePathTenantMatch(cfg): route guard for /tenants/:id/..., replacing authorizeTenantAccess (4 callers in tenant.go) and resolveTenantIDFromPath's superuser carve-out (5 endpoints in tenant_member.go). The router declares it once on the /tenants/:id group so the rule lives next to the route definitions. router/rbac.go grows two factory methods (CrossTenant, PathTenantMatch) so RegisterTenantRoutes stays declarative and reads the same as the existing g.Viewer() / g.Owner() / g.OwnedKBOrAdmin() guards. The /tenants/:id endpoints now group under one PathTenantMatch() middleware instead of each handler re-deriving the check. Handler cleanups follow naturally: tenant.go drops authorizeTenantAccess and the cross-tenant if-blocks; tenant_member.go drops the cfg field and shrinks resolveTenantIDFromPath to a thin parser. NewTenantMemberHandler loses its *config.Config argument; container/container.go's dig wiring picks up the simpler signature automatically. middleware/access_test.go covers all four exports with truth-table cases (flag/attribute/membership combinations, lookup errors, missing context). tenant_member_test.go's existing handler-level cross-tenant assertions still pass because memberTestRouter now mounts RequirePathTenantMatch the same way the production router does. Net -41 lines across 8 changed files + 2 new files; no behaviour change other than the X-Tenant-ID gate accepting active memberships (which was the original PR 4 goal that motivated this refactor).
This commit is contained in:
@@ -73,7 +73,7 @@ func RequireRole(min types.TenantRole, cfg *config.Config) gin.HandlerFunc {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
if isCrossTenantSuperuser(ctx) {
|
||||
if IsCrossTenantSuperuser(ctx, cfg) {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
@@ -132,7 +132,7 @@ func RequireOwnershipOrRole(min types.TenantRole, lookup CreatorLookup, cfg *con
|
||||
}
|
||||
|
||||
// 2. Cross-tenant superuser bypass — same reasoning as RequireRole.
|
||||
if isCrossTenantSuperuser(ctx) {
|
||||
if IsCrossTenantSuperuser(ctx, cfg) {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
@@ -197,19 +197,10 @@ func rbacEnforcementEnabled(cfg *config.Config) bool {
|
||||
return cfg != nil && cfg.Tenant != nil && cfg.Tenant.EnableRBAC
|
||||
}
|
||||
|
||||
// isCrossTenantSuperuser reports whether the caller is an org-level
|
||||
// superuser whose CanAccessAllTenants flag was honoured by the auth
|
||||
// middleware (cfg.Tenant.EnableCrossTenantAccess must also be on for
|
||||
// the flag to mean anything operationally). Such callers bypass tenant
|
||||
// role gates because their access is already governed by a separate
|
||||
// org-level authorisation check.
|
||||
func isCrossTenantSuperuser(ctx context.Context) bool {
|
||||
u, ok := ctx.Value(types.UserContextKey).(*types.User)
|
||||
if !ok || u == nil {
|
||||
return false
|
||||
}
|
||||
return u.CanAccessAllTenants
|
||||
}
|
||||
// isCrossTenantSuperuser was moved to access.go (renamed to
|
||||
// IsCrossTenantSuperuser, exported, and made flag-aware) so the same
|
||||
// helper backs the X-Tenant-ID gate in auth.go and the RequireRole /
|
||||
// RequireOwnershipOrRole guards above.
|
||||
|
||||
// warnOnNilConfig emits a one-shot startup warning when a guard is
|
||||
// constructed with a nil-or-incomplete config. nil cfg makes
|
||||
|
||||
Reference in New Issue
Block a user