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:
@@ -43,6 +43,17 @@ func cfgRBAC(enabled bool) *config.Config {
|
||||
return &config.Config{Tenant: &config.TenantConfig{EnableRBAC: enabled}}
|
||||
}
|
||||
|
||||
// cfgRBACWithCrossTenant returns a config with both per-tenant RBAC
|
||||
// enforcement AND the cluster-wide cross-tenant access flag enabled.
|
||||
// IsCrossTenantSuperuser requires BOTH to honour the User attribute,
|
||||
// so cross-tenant superuser tests need this rather than plain cfgRBAC.
|
||||
func cfgRBACWithCrossTenant(enabled bool) *config.Config {
|
||||
return &config.Config{Tenant: &config.TenantConfig{
|
||||
EnableRBAC: enabled,
|
||||
EnableCrossTenantAccess: true,
|
||||
}}
|
||||
}
|
||||
|
||||
// ---------- RequireRole ----------
|
||||
|
||||
func TestRequireRole_AllowsAtMin(t *testing.T) {
|
||||
@@ -111,7 +122,7 @@ func TestRequireRole_CrossTenantSuperuserBypass(t *testing.T) {
|
||||
c.Next()
|
||||
})
|
||||
router.GET("/protected",
|
||||
RequireRole(types.TenantRoleOwner, cfgRBAC(true)),
|
||||
RequireRole(types.TenantRoleOwner, cfgRBACWithCrossTenant(true)),
|
||||
func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{}) },
|
||||
)
|
||||
req := httptest.NewRequest(http.MethodGet, "/protected", nil)
|
||||
@@ -264,7 +275,7 @@ func TestRequireOwnershipOrRole_CrossTenantSuperuserBypass(t *testing.T) {
|
||||
c.Next()
|
||||
})
|
||||
router.GET("/protected",
|
||||
RequireOwnershipOrRole(types.TenantRoleOwner, lookup, cfgRBAC(true)),
|
||||
RequireOwnershipOrRole(types.TenantRoleOwner, lookup, cfgRBACWithCrossTenant(true)),
|
||||
func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{}) },
|
||||
)
|
||||
req := httptest.NewRequest(http.MethodGet, "/protected", nil)
|
||||
|
||||
Reference in New Issue
Block a user