fix(rbac): plug ungated mutating routes; log RBAC state at startup

Sweep over the route table found four classes of mutating endpoints
that had no role guard:

- POST /tenants — creating a brand-new tenant. Now g.CrossTenant():
  only org-level superusers may create tenants, matching /tenants/all
  and /tenants/search.
- POST /initialization/initialize/:kbId, PUT /initialization/config/:kbId
  — these change a KB's embedding/parser/storage configuration, which
  is at least as sensitive as PUT /knowledge-bases/:id. New
  KBCreatorLookupFromKbIDParam (sister of KBCreatorLookup but reading
  :kbId) lets us reuse the OwnedKBOrAdmin matrix; routes now use the
  new g.OwnedKBOrAdminFromKbIDParam() guard.
- POST /initialization/{ollama,remote,embedding,rerank,asr,multimodal,
  extract}/* — system-level model probes / downloads / extraction
  pipelines. These trigger LLM calls and outbound network fanout
  with tenant credentials; gated to Admin+. The corresponding GETs
  (status / list / progress) drop to Viewer+ since they're read-only
  observability.
- POST /system/{parser-engines,docreader,storage-engine}/* — same
  pattern: GETs that report state are Viewer+, the *-check /
  /reconnect actions are Admin+.

POST /evaluation/ runs the eval harness which fans out LLM calls
across the tenant — gated to Admin+.

Operational improvement: log the resolved tenant RBAC state at
LoadConfig completion. air's hot-reload only rebuilds the binary on
Go-source changes; it does NOT re-source .env, so flipping
WEKNORA_TENANT_ENABLE_RBAC=true while the dev loop is already
running has no effect until the parent dev-app process restarts.
This was a real diagnostic loop in PR review — printing the value at
startup makes the "I edited .env but the gates still aren't firing"
trap obvious from the first console line.

The Printf includes both the resolved cfg.Tenant.EnableRBAC value
and the raw env var, so a mismatch between "what the .env file
says" and "what this process actually inherited" is one glance.
This commit is contained in:
wizardchen
2026-05-14 22:44:31 +08:00
committed by lyingbug
parent b9a1ce7617
commit 0a44077498
4 changed files with 117 additions and 33 deletions
+13
View File
@@ -31,6 +31,9 @@ type rbacGuards struct {
// to be exported into every Register* function as well.
kbCreator middleware.CreatorLookup
agentCreator middleware.CreatorLookup
// kbCreatorFromKbIDParam reads :kbId (not :id) for the
// /initialization/* routes whose KB is addressed by :kbId.
kbCreatorFromKbIDParam middleware.CreatorLookup
// Per-KB-ownership lookups for knowledge / chunk / wiki page routes
// (PR 5, #1303). They walk the URL param back to KB.CreatorID so a
// Contributor who owns the KB can edit/delete its sub-resources
@@ -54,6 +57,7 @@ func newRBACGuards(
g := &rbacGuards{cfg: cfg}
if kbHandler != nil {
g.kbCreator = kbHandler.KBCreatorLookup
g.kbCreatorFromKbIDParam = kbHandler.KBCreatorLookupFromKbIDParam
}
if agentHandler != nil {
g.agentCreator = agentHandler.AgentCreatorLookup
@@ -100,6 +104,15 @@ func (g *rbacGuards) OwnedKBOrAdmin() gin.HandlerFunc {
return middleware.RequireOwnershipOrRole(types.TenantRoleAdmin, g.kbCreator, g.cfg)
}
// OwnedKBOrAdminFromKbIDParam is the same matrix as OwnedKBOrAdmin but
// addresses the KB via :kbId (used by /initialization/* routes). KB
// configuration changes — picking the embedding/parser/storage
// engine, materialising indexes — are at least as sensitive as
// updating the KB itself, so they share the "creator OR Admin+" rule.
func (g *rbacGuards) OwnedKBOrAdminFromKbIDParam() gin.HandlerFunc {
return middleware.RequireOwnershipOrRole(types.TenantRoleAdmin, g.kbCreatorFromKbIDParam, 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.