9e3c92ca95
Build on PR 1's tenant_members schema by enforcing the role matrix on
every authenticated route and at agent run time. Enforcement is gated
behind cfg.Tenant.EnableRBAC so the change ships dormant — the same
code paths log the would-be rejection during the rollout window and
start blocking once operators flip the flag.
Middleware (internal/middleware/rbac.go):
- RequireRole(min, cfg): role-only gate using TenantRole.HasPermission.
- RequireOwnershipOrRole(min, lookup, cfg): allows the original creator
through even when their role is below min, otherwise falls back to
the role check. Lookup errors fail closed; an empty creator_id is
treated as tenant-owned and requires the role.
- Both honour cfg.Tenant.EnableRBAC for the rollout window.
Creator lookups (internal/handler/rbac_lookups.go):
- KBCreatorLookup resolves :id -> KnowledgeBase.CreatorID.
- AgentCreatorLookup resolves :id -> CustomAgent.CreatedBy; built-in
agents (IsBuiltin=true) return ("", nil) so they remain Admin+ only.
- Compile-time guards keep the methods aligned with CreatorLookup.
Route guards (internal/router/rbac.go + router.go):
- rbacGuards bundles the matrix into Viewer/Contributor/Admin/Owner
helpers plus OwnedKBOrAdmin / OwnedAgentOrAdmin. Each Register*
function takes *rbacGuards so route lines remain readable.
- KB / Agent / Knowledge / Chunk / Tenant / Model / MCPService /
VectorStore / DataSource / IMChannel routes annotated per the PR 1
design doc matrix. Tenant-wide infra (models, vector stores, MCP,
data sources, IM channels) reads as Viewer+ and writes as Admin+;
per-resource mutations on KB / Agent route through the
ownership-or-Admin guard.
Resource ownership (services):
- CreateKnowledgeBase / CopyKnowledgeBase populate CreatorID from the
authenticated user, so the OwnedKBOrAdmin guard has a creator to
match.
- CreateAgent / CopyAgent populate CreatedBy similarly. Built-in
agents stay tenant-owned.
Agent run-time gate (internal/handler/session/qa.go):
- AgentQA refuses to launch the agent engine for Viewers when the
target agent has RunnableByViewer=false. Same EnableRBAC gating as
the middleware so it's a no-op until enforcement is turned on.
Tests:
- 12 unit tests cover RequireRole / RequireOwnershipOrRole, including
the fail-open and fail-closed branches and the lookup-error path.
85 lines
3.2 KiB
Go
85 lines
3.2 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)
|
|
}
|