2082b63951
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).
111 lines
4.4 KiB
Go
111 lines
4.4 KiB
Go
package router
|
|
|
|
import (
|
|
"github.com/Tencent/WeKnora/internal/config"
|
|
"github.com/Tencent/WeKnora/internal/handler"
|
|
"github.com/Tencent/WeKnora/internal/middleware"
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// rbacGuards is the centralised role-matrix bundle for tenant-level RBAC
|
|
// (issue #1303 PR 2). NewRouter constructs it once and threads it into
|
|
// each Register* function that registers gated routes.
|
|
//
|
|
// Each method returns a fresh gin.HandlerFunc; routes call the method
|
|
// and inline the guard, so a glance at a route line tells you what
|
|
// authority it requires:
|
|
//
|
|
// kb.PUT("/:id", g.OwnedKBOrAdmin(), handler.UpdateKnowledgeBase)
|
|
//
|
|
// All guards honour cfg.Tenant.EnableRBAC: when the flag is off they log
|
|
// the would-be rejection and let the request through, preserving today's
|
|
// "anyone in the tenant can edit anything" behaviour during the rollout
|
|
// window. When the flag flips to true, the same code paths start
|
|
// rejecting unauthorised callers.
|
|
type rbacGuards struct {
|
|
cfg *config.Config
|
|
|
|
// Lookup closures resolve a request's :id into the resource's creator
|
|
// user ID. Captured up front so the handler-level methods don't have
|
|
// to be exported into every Register* function as well.
|
|
kbCreator middleware.CreatorLookup
|
|
agentCreator middleware.CreatorLookup
|
|
}
|
|
|
|
// newRBACGuards wires the guards from the live configuration and the
|
|
// already-built handlers. Called once from NewRouter.
|
|
func newRBACGuards(cfg *config.Config, kbHandler *handler.KnowledgeBaseHandler, agentHandler *handler.CustomAgentHandler) *rbacGuards {
|
|
g := &rbacGuards{cfg: cfg}
|
|
if kbHandler != nil {
|
|
g.kbCreator = kbHandler.KBCreatorLookup
|
|
}
|
|
if agentHandler != nil {
|
|
g.agentCreator = agentHandler.AgentCreatorLookup
|
|
}
|
|
return g
|
|
}
|
|
|
|
// Role-only guards — pure RequireRole convenience wrappers, named after
|
|
// the matrix entries so route lines stay readable.
|
|
|
|
func (g *rbacGuards) Viewer() gin.HandlerFunc {
|
|
return middleware.RequireRole(types.TenantRoleViewer, g.cfg)
|
|
}
|
|
|
|
func (g *rbacGuards) Contributor() gin.HandlerFunc {
|
|
return middleware.RequireRole(types.TenantRoleContributor, g.cfg)
|
|
}
|
|
|
|
func (g *rbacGuards) Admin() gin.HandlerFunc {
|
|
return middleware.RequireRole(types.TenantRoleAdmin, g.cfg)
|
|
}
|
|
|
|
func (g *rbacGuards) Owner() gin.HandlerFunc {
|
|
return middleware.RequireRole(types.TenantRoleOwner, g.cfg)
|
|
}
|
|
|
|
// Ownership-or-role guards. Required role here is the privilege level
|
|
// that bypasses the ownership check; Contributors ALWAYS pass when they
|
|
// own the resource.
|
|
|
|
// OwnedKBOrAdmin: KB mutations (update/delete/pin/copy). The original
|
|
// creator may proceed; otherwise Admin+ is required. Contributors who
|
|
// did not create the KB get 403 (when enforcement is on).
|
|
func (g *rbacGuards) OwnedKBOrAdmin() gin.HandlerFunc {
|
|
return middleware.RequireOwnershipOrRole(types.TenantRoleAdmin, g.kbCreator, g.cfg)
|
|
}
|
|
|
|
// OwnedAgentOrAdmin: same shape as OwnedKBOrAdmin but for CustomAgent.
|
|
// Built-in agents (IsBuiltin=true) are tenant-owned; their creator
|
|
// lookup returns "" and only Admin+ may mutate them.
|
|
func (g *rbacGuards) OwnedAgentOrAdmin() gin.HandlerFunc {
|
|
return middleware.RequireOwnershipOrRole(types.TenantRoleAdmin, g.agentCreator, g.cfg)
|
|
}
|
|
|
|
// Tenant-access guards. Distinct from the role guards above: these
|
|
// answer the orthogonal question "may this caller touch this tenant
|
|
// at all", before role membership inside the tenant is even
|
|
// considered. Both delegate to middleware/access.go which centralises
|
|
// the cross-tenant rules so the router stays declarative.
|
|
|
|
// CrossTenant gates a route on the caller being an org-level
|
|
// superuser (CanAccessAllTenants AND EnableCrossTenantAccess). Used by
|
|
// /tenants/all, /tenants/search, POST /tenants, GET /tenants — the
|
|
// endpoints that operate across tenants. Replaces the if-blocks that
|
|
// used to live inside ListAllTenants/SearchTenants/CreateTenant.
|
|
func (g *rbacGuards) CrossTenant() gin.HandlerFunc {
|
|
return middleware.RequireCrossTenantAccess(g.cfg)
|
|
}
|
|
|
|
// PathTenantMatch enforces that the URL :id matches the caller's
|
|
// active tenant context (cross-tenant superusers bypass). Routes apply
|
|
// it at the /tenants/:id group level so every per-tenant endpoint —
|
|
// GetTenant / UpdateTenant / DeleteTenant / ResetAPIKey / member
|
|
// management / leave — shares the same check. Replaces the
|
|
// authorizeTenantAccess helper that used to live inside the tenant
|
|
// handler.
|
|
func (g *rbacGuards) PathTenantMatch() gin.HandlerFunc {
|
|
return middleware.RequirePathTenantMatch(g.cfg)
|
|
}
|