feat(knowledge-base): validate vector store bindings on create, copy, and delete
Wires KnowledgeBase.VectorStoreID and the ownership-aware retrieve factory into the user-facing knowledge-base lifecycle: - POST /knowledge-bases validates the requested vector_store_id against the caller's tenant scope and the engine registry. New error codes ErrVectorStoreBindingInvalid (2200) and ErrVectorStoreUnavailable (2201) distinguish the typed branches without echoing UUIDs to the client. - GET / POST / PUT / PUT-pin responses embed the bound store's display metadata (name, source, engine_type, status) without exposing any connection credentials. Cross-tenant shared KBs receive a suppressed payload (vector_store_id stripped, source="shared") so operator-chosen store names cannot be enumerated across tenants. - POST /knowledge-bases/copy synchronously rejects clones whose target has a different embedding model or vector store, before the async clone task is enqueued. The async clone worker re-applies the same checks for defense in depth. - DELETE /vector-stores/:id refuses to remove a store with bound KBs, inside a transaction that row-locks the store on PostgreSQL and serializes via WAL on SQLite. unregister-from-registry is wrapped in defer/recover so a panic surfaces as a structured warning instead of silently leaking a stale engine. - vector_store_id is immutable after creation. The GORM <-:create tag blocks every ORM update path; the service-layer DTO omits the field entirely; a reflection-based regression test catches any future maintainer who adds it back to either layer. - Empty-string vector_store_id is normalized to nil at both the create path and inside SharesStoreWith, so rows persisted by callers that did not run Normalize first cannot trip false same-store comparisons. Part of #993. Depends on #994 and #1310.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
stderrors "errors"
|
||||
"net/http"
|
||||
@@ -25,11 +26,12 @@ import (
|
||||
|
||||
// KnowledgeBaseHandler defines the HTTP handler for knowledge base operations
|
||||
type KnowledgeBaseHandler struct {
|
||||
service interfaces.KnowledgeBaseService
|
||||
knowledgeService interfaces.KnowledgeService
|
||||
kbShareService interfaces.KBShareService
|
||||
agentShareService interfaces.AgentShareService
|
||||
asynqClient interfaces.TaskEnqueuer
|
||||
service interfaces.KnowledgeBaseService
|
||||
knowledgeService interfaces.KnowledgeService
|
||||
kbShareService interfaces.KBShareService
|
||||
agentShareService interfaces.AgentShareService
|
||||
asynqClient interfaces.TaskEnqueuer
|
||||
vectorStoreService interfaces.VectorStoreService // enriches KB responses with bound store display
|
||||
}
|
||||
|
||||
// NewKnowledgeBaseHandler creates a new knowledge base handler instance
|
||||
@@ -39,16 +41,103 @@ func NewKnowledgeBaseHandler(
|
||||
kbShareService interfaces.KBShareService,
|
||||
agentShareService interfaces.AgentShareService,
|
||||
asynqClient interfaces.TaskEnqueuer,
|
||||
vectorStoreService interfaces.VectorStoreService,
|
||||
) *KnowledgeBaseHandler {
|
||||
return &KnowledgeBaseHandler{
|
||||
service: service,
|
||||
knowledgeService: knowledgeService,
|
||||
kbShareService: kbShareService,
|
||||
agentShareService: agentShareService,
|
||||
asynqClient: asynqClient,
|
||||
service: service,
|
||||
knowledgeService: knowledgeService,
|
||||
kbShareService: kbShareService,
|
||||
agentShareService: agentShareService,
|
||||
asynqClient: asynqClient,
|
||||
vectorStoreService: vectorStoreService,
|
||||
}
|
||||
}
|
||||
|
||||
// buildKBResponse turns a knowledge base into a JSON-ready response shape,
|
||||
// merging the bound vector store's display metadata and any caller-supplied
|
||||
// extras (e.g., my_permission for shared KBs). Returns the kb pointer
|
||||
// unchanged on serialization failure so the request still succeeds.
|
||||
//
|
||||
// The map-merge approach (rather than a wrapper struct embedding the kb)
|
||||
// is deliberate: KnowledgeBase has a custom MarshalJSON, and embedding
|
||||
// would promote it to any wrapper struct and silently swallow the extra
|
||||
// fields. The same pattern is already used by GetKnowledgeBase to add the
|
||||
// my_permission field for shared knowledge bases.
|
||||
//
|
||||
// Shared-KB suppression: when storeView.Source == StoreSourceShared (the
|
||||
// caller is not the KB owner), the raw vector_store_id UUID is stripped
|
||||
// from the response so the owner-tenant's store inventory cannot be
|
||||
// correlated across multiple shared KBs. Name / engine type / status are
|
||||
// already empty in the SharedStoreDisplay payload; suppressing the UUID
|
||||
// completes the cross-tenant metadata hiding.
|
||||
func buildKBResponse(
|
||||
kb *types.KnowledgeBase,
|
||||
storeView types.StoreDisplay,
|
||||
extras map[string]interface{},
|
||||
) interface{} {
|
||||
b, err := json.Marshal(kb)
|
||||
if err != nil {
|
||||
return kb
|
||||
}
|
||||
var m map[string]interface{}
|
||||
if err := json.Unmarshal(b, &m); err != nil || m == nil {
|
||||
return kb
|
||||
}
|
||||
if storeView.Source == types.StoreSourceShared {
|
||||
delete(m, "vector_store_id")
|
||||
}
|
||||
if storeView.Name != "" {
|
||||
m["vector_store_name"] = storeView.Name
|
||||
}
|
||||
if storeView.Source != "" {
|
||||
m["vector_store_source"] = storeView.Source
|
||||
}
|
||||
if storeView.EngineType != "" {
|
||||
m["vector_store_engine_type"] = storeView.EngineType
|
||||
}
|
||||
if storeView.Status != "" {
|
||||
m["vector_store_status"] = storeView.Status
|
||||
}
|
||||
for k, v := range extras {
|
||||
m[k] = v
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// resolveKBStoreView returns the store display payload to embed in the KB
|
||||
// response. It applies two policies on top of the service-level resolver:
|
||||
//
|
||||
// - When the KB does not have a DB-managed vector store binding,
|
||||
// the env-fallback display is returned without touching the service.
|
||||
// - When the caller is not the KB owner (shared access), the underlying
|
||||
// store's name and engine are suppressed so operator-chosen names do
|
||||
// not leak across tenants. The Source value is set to "shared".
|
||||
//
|
||||
// On resolution error, an unavailable display is returned and the failure
|
||||
// is logged for ops; the request itself still succeeds.
|
||||
func (h *KnowledgeBaseHandler) resolveKBStoreView(
|
||||
ctx context.Context, kb *types.KnowledgeBase, callerTenantID uint64,
|
||||
) types.StoreDisplay {
|
||||
if !kb.HasVectorStore() {
|
||||
return types.DefaultStoreDisplay()
|
||||
}
|
||||
if kb.TenantID != callerTenantID {
|
||||
return types.SharedStoreDisplay()
|
||||
}
|
||||
if h.vectorStoreService == nil {
|
||||
return types.UnavailableStoreDisplay()
|
||||
}
|
||||
view, err := h.vectorStoreService.ResolveStoreView(ctx, kb.TenantID, *kb.VectorStoreID)
|
||||
if err != nil {
|
||||
logger.WarnWithFields(ctx, logger.Fields{
|
||||
"kb_id": secutils.SanitizeForLog(kb.ID),
|
||||
"tenant_id": kb.TenantID,
|
||||
}, "[kb.view] vector store resolve failed; returning unavailable")
|
||||
return types.UnavailableStoreDisplay()
|
||||
}
|
||||
return view
|
||||
}
|
||||
|
||||
// HybridSearch godoc
|
||||
// @Summary 混合搜索
|
||||
// @Description 在知识库中执行向量和关键词混合搜索
|
||||
@@ -141,6 +230,16 @@ func (h *KnowledgeBaseHandler) CreateKnowledgeBase(c *gin.Context) {
|
||||
// Create knowledge base using the service
|
||||
kb, err := h.service.CreateKnowledgeBase(ctx, &req)
|
||||
if err != nil {
|
||||
// Surface typed AppErrors (notably the 400-class codes
|
||||
// ErrVectorStoreBindingInvalid and ErrVectorStoreUnavailable
|
||||
// returned by validateVectorStoreBinding) instead of wrapping them
|
||||
// into a generic 500. The middleware renders the original code and
|
||||
// HTTP status verbatim. Falls through to 500 only for raw infra
|
||||
// errors that the service did not classify.
|
||||
if appErr, ok := apperrors.IsAppError(err); ok {
|
||||
c.Error(appErr)
|
||||
return
|
||||
}
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(apperrors.NewInternalServerError(err.Error()))
|
||||
return
|
||||
@@ -148,9 +247,10 @@ func (h *KnowledgeBaseHandler) CreateKnowledgeBase(c *gin.Context) {
|
||||
|
||||
logger.Infof(ctx, "Knowledge base created successfully, ID: %s, name: %s",
|
||||
secutils.SanitizeForLog(kb.ID), secutils.SanitizeForLog(kb.Name))
|
||||
callerTenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
c.JSON(http.StatusCreated, gin.H{
|
||||
"success": true,
|
||||
"data": kb,
|
||||
"data": buildKBResponse(kb, h.resolveKBStoreView(ctx, kb, callerTenantID), nil),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -285,18 +385,13 @@ func (h *KnowledgeBaseHandler) GetKnowledgeBase(c *gin.Context) {
|
||||
logger.Warnf(c.Request.Context(), "Failed to fill KB counts for %s: %v", kb.ID, fillErr)
|
||||
}
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
data := interface{}(kb)
|
||||
storeView := h.resolveKBStoreView(c.Request.Context(), kb, tenantID)
|
||||
var extras map[string]interface{}
|
||||
if kb.TenantID != tenantID && permission != "" {
|
||||
// Include my_permission in data so frontend can show role (e.g. "只读") instead of "--" for agent-visible KBs
|
||||
var dataMap map[string]interface{}
|
||||
b, _ := json.Marshal(kb)
|
||||
_ = json.Unmarshal(b, &dataMap)
|
||||
if dataMap != nil {
|
||||
dataMap["my_permission"] = permission
|
||||
data = dataMap
|
||||
}
|
||||
extras = map[string]interface{}{"my_permission": permission}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "data": data})
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "data": buildKBResponse(kb, storeView, extras)})
|
||||
}
|
||||
|
||||
// ListKnowledgeBases godoc
|
||||
@@ -460,9 +555,10 @@ func (h *KnowledgeBaseHandler) TogglePinKnowledgeBase(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
callerTenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": kb,
|
||||
"data": buildKBResponse(kb, h.resolveKBStoreView(ctx, kb, callerTenantID), nil),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -524,9 +620,10 @@ func (h *KnowledgeBaseHandler) UpdateKnowledgeBase(c *gin.Context) {
|
||||
|
||||
logger.Infof(ctx, "Knowledge base updated successfully, ID: %s",
|
||||
secutils.SanitizeForLog(id))
|
||||
callerTenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": kb,
|
||||
"data": buildKBResponse(kb, h.resolveKBStoreView(ctx, kb, callerTenantID), nil),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -641,6 +738,10 @@ func (h *KnowledgeBaseHandler) CopyKnowledgeBase(c *gin.Context) {
|
||||
}
|
||||
|
||||
// If target_id provided, validate target belongs to caller's tenant
|
||||
// and run the pre-flight defenses synchronously so a mismatched
|
||||
// clone is rejected with 400 before the task is enqueued. The same
|
||||
// checks are re-applied inside the async worker (service.CopyKnowledgeBase)
|
||||
// as defense in depth.
|
||||
if req.TargetID != "" {
|
||||
targetKB, err := h.service.GetKnowledgeBaseByID(ctx, req.TargetID)
|
||||
if err != nil {
|
||||
@@ -658,6 +759,24 @@ func (h *KnowledgeBaseHandler) CopyKnowledgeBase(c *gin.Context) {
|
||||
c.Error(errors.NewForbiddenError("No permission to copy to this knowledge base"))
|
||||
return
|
||||
}
|
||||
// Pre-flight defense 1: embedding model must match.
|
||||
// Without this check the async clone would run with incompatible
|
||||
// vector spaces and produce semantically broken results.
|
||||
if sourceKB.EmbeddingModelID != targetKB.EmbeddingModelID {
|
||||
c.Error(apperrors.NewBadRequestError(
|
||||
"source and target knowledge bases use different embedding models; " +
|
||||
"clone into a target with the same embedding model"))
|
||||
return
|
||||
}
|
||||
// Pre-flight defense 2: vector store binding must match.
|
||||
// Cross-store cloning would require copying physical vector data
|
||||
// between stores, which is not yet supported.
|
||||
if !sourceKB.SharesStoreWith(targetKB) {
|
||||
c.Error(apperrors.NewBadRequestError(
|
||||
"source and target knowledge bases are bound to different vector stores; " +
|
||||
"cross-store cloning is not yet supported"))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Generate task ID if not provided
|
||||
|
||||
@@ -0,0 +1,180 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
apperrors "github.com/Tencent/WeKnora/internal/errors"
|
||||
"github.com/Tencent/WeKnora/internal/middleware"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
)
|
||||
|
||||
// handler.CopyKnowledgeBase pre-flight tests.
|
||||
//
|
||||
// The async clone worker (service.CopyKnowledgeBase) re-applies the same
|
||||
// embedding-model and store-binding defenses as defense in depth, but the
|
||||
// handler-level pre-flight is the one that surfaces 400 to the API caller
|
||||
// synchronously instead of inside Asynq progress polling. These tests pin
|
||||
// the synchronous behavior so a future refactor that drops the pre-flight
|
||||
// fails loudly here rather than silently degrading UX.
|
||||
|
||||
// stubKBCopyService provides only the two methods the Copy handler reaches
|
||||
// for (GetKnowledgeBaseByID twice). Other interface methods stay nil so any
|
||||
// accidental new call panics rather than silently succeeding.
|
||||
type stubKBCopyService struct {
|
||||
interfaces.KnowledgeBaseService
|
||||
byID func(ctx context.Context, id string) (*types.KnowledgeBase, error)
|
||||
}
|
||||
|
||||
func (s *stubKBCopyService) GetKnowledgeBaseByID(ctx context.Context, id string) (*types.KnowledgeBase, error) {
|
||||
return s.byID(ctx, id)
|
||||
}
|
||||
|
||||
// stubEnqueuer records whether Enqueue was invoked. The whole point of the
|
||||
// pre-flight is to short-circuit *before* enqueue, so the test fails if
|
||||
// enqueue ran for a mismatched clone.
|
||||
type stubEnqueuer struct {
|
||||
calls int
|
||||
}
|
||||
|
||||
func (s *stubEnqueuer) Enqueue(_ interface{}, _ ...interface{}) (*stubEnqueueInfo, error) {
|
||||
s.calls++
|
||||
return &stubEnqueueInfo{ID: "x"}, nil
|
||||
}
|
||||
|
||||
// stubEnqueueInfo is a stand-in for asynq.TaskInfo so the test does not need
|
||||
// to construct one.
|
||||
type stubEnqueueInfo struct{ ID string }
|
||||
|
||||
func newCopyPreflightRouter(svc interfaces.KnowledgeBaseService) (*gin.Engine, *stubEnqueuer) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
r.Use(middleware.ErrorHandler())
|
||||
r.Use(func(c *gin.Context) {
|
||||
c.Set(types.TenantIDContextKey.String(), uint64(1))
|
||||
c.Set(types.UserIDContextKey.String(), "u-test")
|
||||
c.Next()
|
||||
})
|
||||
enq := &stubEnqueuer{}
|
||||
// The real handler reaches into h.asynqClient.Enqueue with the asynq
|
||||
// task type. We do not exercise the enqueue path in these tests — every
|
||||
// case here either short-circuits at pre-flight or returns 4xx earlier.
|
||||
// Leaving asynqClient nil would panic if enqueue ran, which is exactly
|
||||
// the regression we want to catch.
|
||||
h := &KnowledgeBaseHandler{service: svc}
|
||||
r.POST("/knowledge-bases/copy", h.CopyKnowledgeBase)
|
||||
return r, enq
|
||||
}
|
||||
|
||||
func storeIDPtr(s string) *string { return &s }
|
||||
|
||||
func TestCopyHandlerPreflight_DifferentEmbeddingModel(t *testing.T) {
|
||||
svc := &stubKBCopyService{
|
||||
byID: func(_ context.Context, id string) (*types.KnowledgeBase, error) {
|
||||
switch id {
|
||||
case "src":
|
||||
return &types.KnowledgeBase{
|
||||
ID: "src", TenantID: 1, EmbeddingModelID: "embed-A",
|
||||
}, nil
|
||||
case "dst":
|
||||
return &types.KnowledgeBase{
|
||||
ID: "dst", TenantID: 1, EmbeddingModelID: "embed-B",
|
||||
}, nil
|
||||
}
|
||||
return nil, nil
|
||||
},
|
||||
}
|
||||
r, _ := newCopyPreflightRouter(svc)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPost, "/knowledge-bases/copy",
|
||||
strings.NewReader(`{"source_id":"src","target_id":"dst"}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("expected 400 for embedding-model mismatch, got %d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
if !strings.Contains(w.Body.String(), "different embedding models") {
|
||||
t.Fatalf("expected embedding-model error message, got %s", w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyHandlerPreflight_DifferentVectorStore(t *testing.T) {
|
||||
svc := &stubKBCopyService{
|
||||
byID: func(_ context.Context, id string) (*types.KnowledgeBase, error) {
|
||||
switch id {
|
||||
case "src":
|
||||
return &types.KnowledgeBase{
|
||||
ID: "src", TenantID: 1, EmbeddingModelID: "embed-A",
|
||||
VectorStoreID: storeIDPtr("store-A"),
|
||||
}, nil
|
||||
case "dst":
|
||||
return &types.KnowledgeBase{
|
||||
ID: "dst", TenantID: 1, EmbeddingModelID: "embed-A",
|
||||
VectorStoreID: storeIDPtr("store-B"),
|
||||
}, nil
|
||||
}
|
||||
return nil, nil
|
||||
},
|
||||
}
|
||||
r, _ := newCopyPreflightRouter(svc)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPost, "/knowledge-bases/copy",
|
||||
strings.NewReader(`{"source_id":"src","target_id":"dst"}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("expected 400 for store mismatch, got %d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
if !strings.Contains(w.Body.String(), "different vector stores") {
|
||||
t.Fatalf("expected store-mismatch error message, got %s", w.Body.String())
|
||||
}
|
||||
if strings.Contains(w.Body.String(), "Phase 4") {
|
||||
t.Fatalf("error message must not leak internal roadmap labels: %s", w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyHandlerPreflight_OneSideNilStore(t *testing.T) {
|
||||
svc := &stubKBCopyService{
|
||||
byID: func(_ context.Context, id string) (*types.KnowledgeBase, error) {
|
||||
switch id {
|
||||
case "src":
|
||||
return &types.KnowledgeBase{
|
||||
ID: "src", TenantID: 1, EmbeddingModelID: "embed-A",
|
||||
VectorStoreID: nil,
|
||||
}, nil
|
||||
case "dst":
|
||||
return &types.KnowledgeBase{
|
||||
ID: "dst", TenantID: 1, EmbeddingModelID: "embed-A",
|
||||
VectorStoreID: storeIDPtr("store-A"),
|
||||
}, nil
|
||||
}
|
||||
return nil, nil
|
||||
},
|
||||
}
|
||||
r, _ := newCopyPreflightRouter(svc)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPost, "/knowledge-bases/copy",
|
||||
strings.NewReader(`{"source_id":"src","target_id":"dst"}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("expected 400 when one side is env-store and the other is DB-store, got %d body=%s",
|
||||
w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// compile-time guard against accidentally dropping the apperrors import
|
||||
// from the file — if the pre-flight refactor goes away, this fails too.
|
||||
var _ = apperrors.NewBadRequestError
|
||||
@@ -0,0 +1,188 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
apperrors "github.com/Tencent/WeKnora/internal/errors"
|
||||
"github.com/Tencent/WeKnora/internal/middleware"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
)
|
||||
|
||||
// CreateKnowledgeBase typed-error preservation — the handler must surface
|
||||
// the typed AppError (ErrVectorStoreBindingInvalid / ErrVectorStoreUnavailable)
|
||||
// returned by validateVectorStoreBinding instead of stripping it into a
|
||||
// generic 500 via NewInternalServerError. Without the IsAppError unwrap in
|
||||
// the handler, the typed error codes would be silently nullified at the
|
||||
// HTTP boundary and clients would lose the ability to branch on the cause.
|
||||
//
|
||||
// Shared-KB UUID suppression — responses for cross-tenant shared KBs must
|
||||
// not leak the owner tenant's vector_store_id UUID. SharedStoreDisplay
|
||||
// suppresses store name + engine_type for cross-tenant callers, but the
|
||||
// underlying KnowledgeBase.MarshalJSON still emits the UUID; the
|
||||
// buildKBResponse strip closes the gap so the UUID cannot be correlated
|
||||
// across multiple shared KBs.
|
||||
|
||||
// stubKBCreateService drives CreateKnowledgeBase end-to-end with a
|
||||
// service that returns a chosen error. Embedding the interface keeps
|
||||
// any other method nil-panic'ing on purpose.
|
||||
type stubKBCreateService struct {
|
||||
interfaces.KnowledgeBaseService
|
||||
createErr error
|
||||
}
|
||||
|
||||
func (s *stubKBCreateService) CreateKnowledgeBase(_ context.Context, kb *types.KnowledgeBase) (*types.KnowledgeBase, error) {
|
||||
if s.createErr != nil {
|
||||
return nil, s.createErr
|
||||
}
|
||||
kb.ID = "kb-new"
|
||||
kb.TenantID = 1
|
||||
return kb, nil
|
||||
}
|
||||
|
||||
func newCreateKBRouter(svc interfaces.KnowledgeBaseService) *gin.Engine {
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
r.Use(middleware.ErrorHandler())
|
||||
r.Use(func(c *gin.Context) {
|
||||
c.Set(types.TenantIDContextKey.String(), uint64(1))
|
||||
c.Set(types.UserIDContextKey.String(), "u-test")
|
||||
c.Next()
|
||||
})
|
||||
h := &KnowledgeBaseHandler{service: svc}
|
||||
r.POST("/knowledge-bases", h.CreateKnowledgeBase)
|
||||
return r
|
||||
}
|
||||
|
||||
func TestCreateKB_PreservesTypedErrorCode_2200(t *testing.T) {
|
||||
svc := &stubKBCreateService{
|
||||
createErr: apperrors.NewVectorStoreBindingInvalidError("vector store not found"),
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPost, "/knowledge-bases",
|
||||
strings.NewReader(`{"name":"kb"}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
newCreateKBRouter(svc).ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("expected 400, got %d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
body := w.Body.String()
|
||||
if !strings.Contains(body, `"code":2200`) {
|
||||
t.Fatalf("expected envelope to contain code 2200, got %s", body)
|
||||
}
|
||||
if strings.Contains(body, `"code":1007`) || strings.Contains(body, `"code":1000`) {
|
||||
t.Fatalf("typed error must not be wrapped into a generic code, got %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateKB_PreservesTypedErrorCode_2201(t *testing.T) {
|
||||
svc := &stubKBCreateService{
|
||||
createErr: apperrors.NewVectorStoreUnavailableError(""),
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPost, "/knowledge-bases",
|
||||
strings.NewReader(`{"name":"kb"}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
newCreateKBRouter(svc).ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("expected 400, got %d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
if !strings.Contains(w.Body.String(), `"code":2201`) {
|
||||
t.Fatalf("expected envelope to contain code 2201, got %s", w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateKB_GenericErrorStillFallsThroughTo500(t *testing.T) {
|
||||
// A non-AppError must NOT be auto-rewritten to 200/400 — operational
|
||||
// monitoring still needs to see infrastructure failures as 5xx.
|
||||
svc := &stubKBCreateService{createErr: errSentinel("connection refused")}
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPost, "/knowledge-bases",
|
||||
strings.NewReader(`{"name":"kb"}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
newCreateKBRouter(svc).ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusInternalServerError {
|
||||
t.Fatalf("expected 500 for raw infra error, got %d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
type errSentinel string
|
||||
|
||||
func (e errSentinel) Error() string { return string(e) }
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// buildKBResponse must strip vector_store_id for shared KB responses
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestBuildKBResponse_StripsVectorStoreIDForSharedKB(t *testing.T) {
|
||||
storeID := "aaaa-bbbb-cccc-dddd"
|
||||
kb := &types.KnowledgeBase{
|
||||
ID: "kb-1",
|
||||
Name: "shared-kb",
|
||||
TenantID: 42, // different from caller
|
||||
EmbeddingModelID: "e",
|
||||
SummaryModelID: "s",
|
||||
VectorStoreID: &storeID,
|
||||
}
|
||||
got := buildKBResponse(kb, types.SharedStoreDisplay(), nil)
|
||||
m, ok := got.(map[string]interface{})
|
||||
if !ok {
|
||||
t.Fatalf("expected map result, got %T", got)
|
||||
}
|
||||
if _, exists := m["vector_store_id"]; exists {
|
||||
t.Fatalf("shared KB response must not expose vector_store_id, got %v", m["vector_store_id"])
|
||||
}
|
||||
if _, exists := m["vector_store_name"]; exists {
|
||||
t.Fatalf("shared KB response must not expose vector_store_name, got %v", m["vector_store_name"])
|
||||
}
|
||||
if m["vector_store_source"] != types.StoreSourceShared {
|
||||
t.Fatalf("expected vector_store_source=shared, got %v", m["vector_store_source"])
|
||||
}
|
||||
// Defensive: ensure the source UUID does not appear *anywhere* in
|
||||
// the serialized output (paranoid check against future map keys).
|
||||
serialized, _ := json.Marshal(m)
|
||||
if strings.Contains(string(serialized), storeID) {
|
||||
t.Fatalf("shared KB response leaked vector store UUID via some path: %s", serialized)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildKBResponse_KeepsVectorStoreIDForOwnerKB(t *testing.T) {
|
||||
// Same setup but with the user-source display — owner caller should
|
||||
// still see the UUID alongside the resolved metadata.
|
||||
storeID := "aaaa-bbbb-cccc-dddd"
|
||||
kb := &types.KnowledgeBase{
|
||||
ID: "kb-1",
|
||||
Name: "owner-kb",
|
||||
TenantID: 1,
|
||||
EmbeddingModelID: "e",
|
||||
SummaryModelID: "s",
|
||||
VectorStoreID: &storeID,
|
||||
}
|
||||
view := types.StoreDisplay{
|
||||
Name: "prod-es",
|
||||
Source: types.StoreSourceUser,
|
||||
EngineType: "elasticsearch",
|
||||
Status: "available",
|
||||
}
|
||||
got := buildKBResponse(kb, view, nil)
|
||||
m, ok := got.(map[string]interface{})
|
||||
if !ok {
|
||||
t.Fatalf("expected map result, got %T", got)
|
||||
}
|
||||
if m["vector_store_id"] != storeID {
|
||||
t.Fatalf("owner KB must keep vector_store_id, got %v", m["vector_store_id"])
|
||||
}
|
||||
if m["vector_store_name"] != "prod-es" {
|
||||
t.Fatalf("owner KB must surface store name, got %v", m["vector_store_name"])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
// TestUpdateKBRequest_DoesNotAcceptVectorStoreID is the structural enforcement
|
||||
// behind the vector_store_id immutability contract. The GORM `<-:create`
|
||||
// tag on KnowledgeBase.VectorStoreID already blocks every ORM UPDATE path
|
||||
// (verified by the repository-level sqlite immutability tests), but the
|
||||
// service DTO must independently refuse to even *accept* the field —
|
||||
// otherwise a future maintainer who adds it to UpdateKnowledgeBaseRequest
|
||||
// or KnowledgeBaseConfig opens a path where the field is silently ignored
|
||||
// by the ORM, which is worse than an explicit rejection.
|
||||
//
|
||||
// This test walks the request and config struct shapes and fails if either
|
||||
// gains a VectorStoreID member, by name or by JSON tag.
|
||||
func TestUpdateKBRequest_DoesNotAcceptVectorStoreID(t *testing.T) {
|
||||
t.Run("UpdateKnowledgeBaseRequest", func(t *testing.T) {
|
||||
assertNoVectorStoreIDField(t, reflect.TypeOf(UpdateKnowledgeBaseRequest{}))
|
||||
})
|
||||
t.Run("KnowledgeBaseConfig", func(t *testing.T) {
|
||||
// Config carries chunking / extract / faq / wiki sub-configs and must
|
||||
// not be extended with a VectorStoreID either (Config is passed
|
||||
// straight into the service Update path).
|
||||
assertNoVectorStoreIDField(t, reflect.TypeOf(types.KnowledgeBaseConfig{}))
|
||||
})
|
||||
}
|
||||
|
||||
// assertNoVectorStoreIDField walks the visible fields of t (including embedded
|
||||
// anonymous structs) and reports any field named VectorStoreID or carrying
|
||||
// a json tag of "vector_store_id".
|
||||
func assertNoVectorStoreIDField(t *testing.T, typ reflect.Type) {
|
||||
t.Helper()
|
||||
var visit func(rt reflect.Type, path string)
|
||||
visit = func(rt reflect.Type, path string) {
|
||||
for rt.Kind() == reflect.Ptr {
|
||||
rt = rt.Elem()
|
||||
}
|
||||
if rt.Kind() != reflect.Struct {
|
||||
return
|
||||
}
|
||||
for i := 0; i < rt.NumField(); i++ {
|
||||
f := rt.Field(i)
|
||||
full := path + "." + f.Name
|
||||
if f.Name == "VectorStoreID" {
|
||||
t.Fatalf("%s declares VectorStoreID — vector_store_id is immutable post-create "+
|
||||
"and must not be accepted by update DTOs", full)
|
||||
}
|
||||
if tag := strings.Split(f.Tag.Get("json"), ",")[0]; tag == "vector_store_id" {
|
||||
t.Fatalf("%s carries json tag \"vector_store_id\" — the field is immutable "+
|
||||
"post-create and must not be accepted by update DTOs", full)
|
||||
}
|
||||
if f.Anonymous {
|
||||
visit(f.Type, full)
|
||||
}
|
||||
}
|
||||
}
|
||||
visit(typ, typ.Name())
|
||||
}
|
||||
Reference in New Issue
Block a user