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
+34
View File
@@ -62,6 +62,39 @@ func (h *KnowledgeBaseHandler) KBCreatorLookup(c *gin.Context) (string, error) {
return kb.CreatorID, nil
}
// KBCreatorLookupFromKbIDParam is the same lookup as KBCreatorLookup
// but reads `:kbId` instead of `:id`. Used by the /initialization
// routes (POST /initialization/initialize/:kbId, PUT
// /initialization/config/:kbId), which are KB-scoped mutating ops:
// changing a KB's embedding/parser/storage configuration is at least
// as sensitive as updating the KB itself, so it must follow the same
// "creator OR Admin+" matrix.
func (h *KnowledgeBaseHandler) KBCreatorLookupFromKbIDParam(c *gin.Context) (string, error) {
id := c.Param("kbId")
if id == "" {
return "", errors.New("missing :kbId param for KB creator lookup")
}
ctx := c.Request.Context()
tenantID, ok := types.TenantIDFromContext(ctx)
if !ok {
return "", errors.New("tenant context missing")
}
kb, err := h.service.GetKnowledgeBaseByID(ctx, id)
if err != nil {
if errors.Is(err, apprepo.ErrKnowledgeBaseNotFound) {
return "", middleware.ErrResourceNotFound
}
return "", err
}
if kb == nil {
return "", middleware.ErrResourceNotFound
}
if kb.TenantID != tenantID {
return "", middleware.ErrResourceNotFound
}
return kb.CreatorID, nil
}
// AgentCreatorLookup resolves :id -> CustomAgent.CreatedBy. Built-in
// agents (IsBuiltin == true) are tenant-owned across the board: they
// belong to the tenant rather than to any one user, so we return
@@ -105,6 +138,7 @@ func (h *CustomAgentHandler) AgentCreatorLookup(c *gin.Context) (string, error)
// so route wiring stays type-safe even if a signature drifts.
var (
_ middleware.CreatorLookup = (*KnowledgeBaseHandler)(nil).KBCreatorLookup
_ middleware.CreatorLookup = (*KnowledgeBaseHandler)(nil).KBCreatorLookupFromKbIDParam
_ middleware.CreatorLookup = (*CustomAgentHandler)(nil).AgentCreatorLookup
_ middleware.CreatorLookup = (*KnowledgeHandler)(nil).KBCreatorLookupFromKnowledgeID
_ middleware.CreatorLookup = (*ChunkHandler)(nil).KBCreatorLookupFromKnowledgeIDParam