refactor(api-keys): simplify authorization to a single-role gate

Redesign tenant API key authorization around one dimension (role) plus a
central default-deny gate, replacing the previous role + KB-scoped flag model.

- Add APIKeyGate as the single authority for API-key authorization: routes
  declare one policy (MinRole) via apiKeyGroup/apiKeyRoute helpers; undeclared
  routes are denied by default. JWT principals short-circuit the gate and the
  JWT-centric RBAC guards short-circuit for API-key principals.
- Roles: viewer/contributor/admin are knowledge-base data-plane only (KB scope
  is a pure downstream data filter); owner is a full-access tenant tier that
  unlocks tenant-level infrastructure routes and is required for every method.
- Drop the KBScoped policy flag and the KB-restricted-vs-tenant branching.
- Security hardening: never disclose HMAC secret (return presence only), abort
  writes on API-key encryption failure, deny KB-restricted keys on /files, and
  backfill legacy key hashes at startup instead of O(N) plaintext scans.
- Frontend: two-level create dialog (access type -> permission level), rename
  role labels to reflect actual scope, sync en/zh/ko/ru locales.
- Fix startup self-check false-positive on trailing-slash routes and normalize
  gin paths before comparison; add regression tests.
This commit is contained in:
wizardchen
2026-07-06 17:34:52 +08:00
committed by lyingbug
parent fedb6c3f7e
commit d2ffc1b973
26 changed files with 1045 additions and 500 deletions
+25
View File
@@ -68,6 +68,13 @@ func RequireRole(min types.TenantRole, cfg *config.Config) gin.HandlerFunc {
warnOnNilConfig(cfg)
return func(c *gin.Context) {
ctx := c.Request.Context()
// API-key principals are authorized solely by the APIKeyGate
// (role + KB scope + default-deny). The JWT role ladder does not
// apply to a machine principal, so short-circuit here.
if _, ok := types.TenantAPIKeyScopeFromContext(ctx); ok {
c.Next()
return
}
role := types.TenantRoleFromContext(ctx)
if role.HasPermission(min) {
c.Next()
@@ -121,6 +128,15 @@ func RequireSystemAdmin(cfg *config.Config) gin.HandlerFunc {
warnOnNilConfig(cfg)
return func(c *gin.Context) {
ctx := c.Request.Context()
// API-key principals never gain system-admin authority, but the
// APIKeyGate is the single place that decides which routes they may
// reach. System routes are simply not declared there, so an API key
// is already rejected by default-deny before this guard runs. Short-
// circuit to avoid a misleading "system administrator required" 403.
if _, ok := types.TenantAPIKeyScopeFromContext(ctx); ok {
c.Next()
return
}
if types.IsSystemAdminFromContext(ctx) {
c.Next()
return
@@ -169,6 +185,15 @@ func RequireOwnershipOrRole(min types.TenantRole, lookup CreatorLookup, cfg *con
warnOnNilConfig(cfg)
return func(c *gin.Context) {
ctx := c.Request.Context()
// API-key principals are authorized solely by the APIKeyGate.
// Ownership ("creator OR Admin+") is a human concept that cannot
// apply to a machine principal (its synthetic system-user never
// matches creator_id), so short-circuit here. KB-scope for API
// keys is still enforced by the KBAccess guards + handler checks.
if _, ok := types.TenantAPIKeyScopeFromContext(ctx); ok {
c.Next()
return
}
role := types.TenantRoleFromContext(ctx)
// 1. Fast path: role meets the bar.