feat(rbac): per-KB ownership for knowledge / chunks / wiki pages (#1303)
PR 1–4 of the multi-tenant RBAC series gated KB-level and Agent-level
mutations behind a "creator OR Admin+" check via OwnedKBOrAdmin /
OwnedAgentOrAdmin. The layer underneath — knowledge entries, chunks,
wiki pages — was still gated only by Contributor, which is wider than
intended: any tenant Contributor could edit/delete documents in a
private KB they didn't own. PR 2 / wiki author left explicit follow-up
breadcrumbs at the three call sites in router.go acknowledging this gap.
This PR closes those breadcrumbs by reusing the existing scaffolding:
- KnowledgeService.GetOwningKBCreatorID walks knowledge_id -> kb_id ->
KB.CreatorID, tenant-scoped through GetKnowledgeByID. The chain lives
in the service layer so the handler-side lookups stay pure adapters.
- Three new CreatorLookup closures (KBCreatorLookupFromKnowledgeID,
KBCreatorLookupFromKnowledgeIDParam, KBCreatorLookupFromKBPath) are
added in handler/rbac_lookups.go alongside the existing
KBCreatorLookup / AgentCreatorLookup. Each translates the repository
sentinels into middleware.ErrResourceNotFound and re-checks tenant
scope as defence-in-depth, mirroring KBCreatorLookup exactly.
- router/rbac.go gains three guard factories — OwnedKnowledgeKBOrAdmin,
OwnedChunkKBOrAdmin, OwnedWikiKBOrAdmin — wired through the same
rbacGuards bundle.
- Mutating routes in RegisterKnowledgeRoutes / RegisterChunkRoutes /
RegisterWikiPageRoutes switch from g.Contributor() to the new
ownership guards. Cross-KB batch operations (POST /knowledge/move,
POST /knowledge/batch-delete, PUT /knowledge/tags) stay
Contributor-gated since there's no single owning KB to check
against. KB-scoped uploads (POST /knowledge-bases/:id/knowledge/...)
reuse the existing OwnedKBOrAdmin because the URL :id is the KB id
directly. The chunks.DELETE("/by-id/:id/questions") route addresses
chunks by their own id with no knowledge id in the URL, so it stays
Contributor-gated as a documented carve-out.
No schema changes: documents inside a shared KB are KB-owned content,
not personal uploads — that's how the product is positioned and matches
the original "per-KB-ownership granularity" wording from the PR 2
breadcrumbs. Legacy/built-in KBs with empty CreatorID surface as
tenant-owned (Admin+ only), same rule as built-in agents.
Tests:
- 9 new test cases in handler/rbac_lookups_test.go covering the three
new lookups: NotFoundMapsToSentinel (each repository sentinel),
CrossTenantIsHiddenAsNotFound, OwnerMatchReturnsCreatorID,
MissingTenantContext (fail-closed), and ChunkRouteShape (param-name
regression guard).
- 5 new test cases in service/knowledge_owner_chain_test.go covering
the chain helper: knowledge-not-found, kb-not-found, nil-kb-without-
err, happy path, and legacy KB with empty CreatorID.
Build, vet, and the targeted ./internal/middleware/...
./internal/handler/... ./internal/router/...
./internal/application/service test runs are clean. Pre-existing
TestCreateStore_* / TestOssEnsureBucket_CreateFails failures on main
are unrelated and reproduce on a clean checkout.
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/Tencent/WeKnora/internal/application/service"
|
||||
"github.com/Tencent/WeKnora/internal/middleware"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@@ -105,4 +106,98 @@ func (h *CustomAgentHandler) AgentCreatorLookup(c *gin.Context) (string, error)
|
||||
var (
|
||||
_ middleware.CreatorLookup = (*KnowledgeBaseHandler)(nil).KBCreatorLookup
|
||||
_ middleware.CreatorLookup = (*CustomAgentHandler)(nil).AgentCreatorLookup
|
||||
_ middleware.CreatorLookup = (*KnowledgeHandler)(nil).KBCreatorLookupFromKnowledgeID
|
||||
_ middleware.CreatorLookup = (*ChunkHandler)(nil).KBCreatorLookupFromKnowledgeIDParam
|
||||
_ middleware.CreatorLookup = (*WikiPageHandler)(nil).KBCreatorLookupFromKBPath
|
||||
)
|
||||
|
||||
// KBCreatorLookupFromKnowledgeID resolves :id (a Knowledge ID) to the
|
||||
// CreatorID of the *owning KB*, scoped to the caller's tenant. Used by
|
||||
// per-knowledge mutating routes (PR 5, #1303) so a Contributor who owns
|
||||
// the KB can edit/delete any of its documents while a Contributor who
|
||||
// merely belongs to the tenant cannot. Built-in / legacy KBs without a
|
||||
// CreatorID surface as ("", nil) and stay Admin-gated.
|
||||
//
|
||||
// The chain (knowledge_id -> kb_id -> KB.CreatorID) lives in the
|
||||
// service layer (KnowledgeService.GetOwningKBCreatorID) so this lookup
|
||||
// stays a pure adapter — same shape as KBCreatorLookup.
|
||||
func (h *KnowledgeHandler) KBCreatorLookupFromKnowledgeID(c *gin.Context) (string, error) {
|
||||
knowledgeID := c.Param("id")
|
||||
if knowledgeID == "" {
|
||||
return "", errors.New("missing :id param for knowledge owner lookup")
|
||||
}
|
||||
return resolveKBCreatorByKnowledgeID(c, h.kgService, knowledgeID)
|
||||
}
|
||||
|
||||
// KBCreatorLookupFromKnowledgeIDParam mirrors KBCreatorLookupFromKnowledgeID
|
||||
// for chunk routes, which use :knowledge_id rather than :id. The
|
||||
// chunks.DELETE("/by-id/:id/questions") route addresses a chunk by its
|
||||
// own id (not a knowledge id), so it stays Contributor-gated and is
|
||||
// not wired through this lookup — see router.go for the carve-out.
|
||||
func (h *ChunkHandler) KBCreatorLookupFromKnowledgeIDParam(c *gin.Context) (string, error) {
|
||||
knowledgeID := c.Param("knowledge_id")
|
||||
if knowledgeID == "" {
|
||||
return "", errors.New("missing :knowledge_id param for chunk owner lookup")
|
||||
}
|
||||
return resolveKBCreatorByKnowledgeID(c, h.kgService, knowledgeID)
|
||||
}
|
||||
|
||||
// KBCreatorLookupFromKBPath resolves :kb_id (used by wiki routes) to
|
||||
// KnowledgeBase.CreatorID. No chain hop — the wiki page URL already
|
||||
// carries the owning KB id, so we go straight to the KB service. The
|
||||
// tenant defence-in-depth re-check stays the same as KBCreatorLookup:
|
||||
// repo.GetKnowledgeBaseByID is unscoped, so we explicitly compare
|
||||
// against the context tenant.
|
||||
func (h *WikiPageHandler) KBCreatorLookupFromKBPath(c *gin.Context) (string, error) {
|
||||
kbID := c.Param("kb_id")
|
||||
if kbID == "" {
|
||||
return "", errors.New("missing :kb_id param for wiki owner lookup")
|
||||
}
|
||||
ctx := c.Request.Context()
|
||||
tenantID, ok := types.TenantIDFromContext(ctx)
|
||||
if !ok {
|
||||
return "", errors.New("tenant context missing")
|
||||
}
|
||||
kb, err := h.kbService.GetKnowledgeBaseByID(ctx, kbID)
|
||||
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
|
||||
}
|
||||
|
||||
// resolveKBCreatorByKnowledgeID is the shared body for the knowledge /
|
||||
// chunk lookups. The service-layer chain helper does the tenant-scoped
|
||||
// fetch (knowledge -> KB) and returns repository sentinel errors;
|
||||
// translating those to middleware.ErrResourceNotFound is the lookup's
|
||||
// job, mirroring what KBCreatorLookup does for plain :id routes.
|
||||
func resolveKBCreatorByKnowledgeID(
|
||||
c *gin.Context,
|
||||
kgService interfaces.KnowledgeService,
|
||||
knowledgeID string,
|
||||
) (string, error) {
|
||||
ctx := c.Request.Context()
|
||||
if _, ok := types.TenantIDFromContext(ctx); !ok {
|
||||
// Same fail-closed reasoning as KBCreatorLookup: no tenant
|
||||
// context means auth didn't complete, and we'd rather have the
|
||||
// caller see a 503 than a silent fail-open.
|
||||
return "", errors.New("tenant context missing")
|
||||
}
|
||||
creatorID, err := kgService.GetOwningKBCreatorID(ctx, knowledgeID)
|
||||
if err != nil {
|
||||
if errors.Is(err, apprepo.ErrKnowledgeNotFound) ||
|
||||
errors.Is(err, apprepo.ErrKnowledgeBaseNotFound) {
|
||||
return "", middleware.ErrResourceNotFound
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
return creatorID, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user