From 97490ef7eda1df9a477bfc66c2718f385e71467d Mon Sep 17 00:00:00 2001 From: wizardchen Date: Wed, 15 Jul 2026 10:50:41 +0800 Subject: [PATCH] feat(api-keys): enhance manage_kbs capability for KB lifecycle management Update the manage_kbs capability to cover the full lifecycle of knowledge bases, including create, copy, duplicate, update, and delete operations. Adjust related API routes and tests to ensure proper access control and policy requirements for knowledge base management. --- internal/router/rbac.go | 5 +++-- internal/router/router.go | 7 +++++-- .../router_api_key_capabilities_test.go | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/internal/router/rbac.go b/internal/router/rbac.go index 18f906a5..bc099148 100644 --- a/internal/router/rbac.go +++ b/internal/router/rbac.go @@ -258,8 +258,9 @@ func apiKeyIngest(base middleware.APIKeyRoutePolicy) middleware.APIKeyRoutePolic } // apiKeyManageKnowledgeBases layers the "manage_kbs" capability on top of a -// base policy so a scoped key can manage existing KB metadata/config within -// its KB allow-list. +// base policy so a scoped key can manage the KB lifecycle (create/copy/ +// duplicate/update/delete + config). Existing-KB operations stay bounded by +// the key's allow-list downstream; create has no source to bound. func apiKeyManageKnowledgeBases(base middleware.APIKeyRoutePolicy) middleware.APIKeyRoutePolicy { return base.WithCapability(types.APIKeyCapabilityManageKnowledgeBases) } diff --git a/internal/router/router.go b/internal/router/router.go index 2a0a1d44..2dc85df4 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -458,8 +458,11 @@ func RegisterKnowledgeBaseRoutes(r *gin.RouterGroup, handler *handler.KnowledgeB // 创建知识库副本 — 产出新 KB,与 create 同档:JWT Contributor+,API key 需 manage_kbs 或 full-access; // 且对源 KB 有 read 权限(KBAccessRead 会对限定 key 兜住源 KB)。只创建新的 KB 设置记录,不复制内容/索引/分享。 kbManagement.POST("/:id/duplicate", g.Contributor(), g.KBAccessRead("id"), handler.DuplicateKnowledgeBase) - // 获取知识库复制进度 — Viewer+;只读,retrieve OR full-access 均可轮询(租户自查)。 - kb.GET("/copy/progress/:task_id", g.Viewer(), handler.GetKBCloneProgress) + // 获取知识库复制进度 — Viewer+;只读。manage_kbs(发起 copy 的 key)或 + // retrieve 均可轮询;任务按租户隔离(requireTaskProgressTenant),key 只能 + // 查本租户任务。 + kb.With(apiKeyRetrieve(apiKeyManageKnowledgeBases(apiKeyFullAccess()))). + GET("/copy/progress/:task_id", g.Viewer(), handler.GetKBCloneProgress) // 获取可移动目标知识库列表 — Viewer+ 且对 KB 有 read 权限 kb.GET("/:id/move-targets", g.Viewer(), g.KBAccessRead("id"), handler.ListMoveTargets) } diff --git a/internal/router/router_api_key_capabilities_test.go b/internal/router/router_api_key_capabilities_test.go index 250aba53..e4b34524 100644 --- a/internal/router/router_api_key_capabilities_test.go +++ b/internal/router/router_api_key_capabilities_test.go @@ -429,6 +429,25 @@ func TestChunkerPreviewRouteRequiresRetrieveOrIngestCapability(t *testing.T) { } } +func TestKBCloneProgressRouteRequiresRetrieveOrManageKbsCapability(t *testing.T) { + gin.SetMode(gin.TestMode) + g := &rbacGuards{} + v1 := gin.New().Group("/api/v1") + + RegisterKnowledgeBaseRoutes(v1, &handler.KnowledgeBaseHandler{}, g) + + policy := mustLookupAPIKeyPolicy(t, g, http.MethodGet, "/api/v1/knowledge-bases/copy/progress/:task_id") + if !policy.RequireFullAccess { + t.Fatal("policy should require full access without a matching capability") + } + if !policyHasCapability(policy, types.APIKeyCapabilityRetrieve) { + t.Fatalf("policy capabilities = %#v, want retrieve", policy.Capabilities) + } + if !policyHasCapability(policy, types.APIKeyCapabilityManageKnowledgeBases) { + t.Fatalf("policy capabilities = %#v, want manage_kbs", policy.Capabilities) + } +} + func TestFAQImportProgressRouteRequiresRetrieveOrIngestCapability(t *testing.T) { gin.SetMode(gin.TestMode) g := &rbacGuards{}