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
+18
View File
@@ -477,6 +477,24 @@ func LoadConfig() (*Config, error) {
return nil, err
}
// Surface RBAC enforcement state at startup. air's hot-reload only
// rebuilds the binary on Go-source changes; it does NOT re-source
// .env, so a `WEKNORA_TENANT_ENABLE_RBAC=true` flip while the dev
// loop is already running silently has no effect until the dev
// script restarts. Logging this once at startup makes the
// "I edited .env but the gates still aren't firing" trap obvious
// from the first console line. Printf rather than logger because
// LoadConfig runs before the logger sink is wired in the dig graph.
rbacOn := cfg.Tenant != nil && cfg.Tenant.EnableRBAC
xtAccess := cfg.Tenant != nil && cfg.Tenant.EnableCrossTenantAccess
fmt.Printf(
"[config] tenant RBAC enforcement: enable_rbac=%v cross_tenant_access=%v "+
"(env: WEKNORA_TENANT_ENABLE_RBAC=%q WEKNORA_TENANT_ENABLE_CROSS_TENANT_ACCESS=%q)\n",
rbacOn, xtAccess,
os.Getenv("WEKNORA_TENANT_ENABLE_RBAC"),
os.Getenv("WEKNORA_TENANT_ENABLE_CROSS_TENANT_ACCESS"),
)
return &cfg, nil
}