From 9fa9e7c29d8eb727825b14f0b0ca23907969b91a Mon Sep 17 00:00:00 2001 From: Mohamed Arbi Date: Wed, 7 Oct 2026 16:59:56 +0100 Subject: [PATCH] fix: use consistent qdrant env settings (#3960) * fix qdrant env settings * fix test formatting --- .../application/service/vectorstore_test.go | 22 +++++++ internal/container/container.go | 30 ++------- internal/handler/vectorstore_env_test.go | 52 +++++++++++++++ internal/types/vectorstore.go | 21 +++++- internal/types/vectorstore_test.go | 66 +++++++++++++++++++ 5 files changed, 165 insertions(+), 26 deletions(-) create mode 100644 internal/handler/vectorstore_env_test.go diff --git a/internal/application/service/vectorstore_test.go b/internal/application/service/vectorstore_test.go index 86f43962..d2991efe 100644 --- a/internal/application/service/vectorstore_test.go +++ b/internal/application/service/vectorstore_test.go @@ -393,6 +393,28 @@ func TestCreateStore_DuplicateCheck_EnvStore(t *testing.T) { assert.Contains(t, appErr.Error(), "environment variables") } +func TestCreateStore_DuplicateCheck_QdrantEnvStore(t *testing.T) { + withSSRFWhitelist(t, "qdrant") + t.Setenv("RETRIEVE_DRIVER", "qdrant") + t.Setenv("QDRANT_HOST", "qdrant") + t.Setenv("QDRANT_PORT", "7443") + t.Setenv("QDRANT_USE_TLS", "true") + t.Setenv("QDRANT_COLLECTION", "custom_vectors") + + repo := &mockVectorStoreRepo{} + svc := NewVectorStoreService(repo, nil, nil, nil, nil) + err := svc.CreateStore(context.Background(), &types.VectorStore{ + TenantID: 1, Name: "duplicate", EngineType: types.QdrantRetrieverEngineType, + ConnectionConfig: types.ConnectionConfig{Host: "qdrant", Port: 7443, UseTLS: true}, + IndexConfig: types.IndexConfig{CollectionPrefix: "custom_vectors"}, + }) + var appErr *errors.AppError + require.ErrorAs(t, err, &appErr) + assert.Equal(t, errors.ErrConflict, appErr.Code) + assert.Contains(t, appErr.Error(), "environment variables") + assert.Empty(t, repo.stores) +} + func TestCreateStore_DuplicateCheck_EnvStore_DifferentIndex_Allowed(t *testing.T) { // Same endpoint as env store but different index — should be allowed. // Use an httptest server so CreateStore's connection probe sees a real diff --git a/internal/container/container.go b/internal/container/container.go index 3a5a52ad..37d87ba3 100644 --- a/internal/container/container.go +++ b/internal/container/container.go @@ -1407,36 +1407,16 @@ func initRetrieveEngineRegistry( } if slices.Contains(retrieveDriver, "qdrant") { - qdrantHost := os.Getenv("QDRANT_HOST") - if qdrantHost == "" { - qdrantHost = "localhost" - } + store := types.FindEnvVectorStore("qdrant", os.Getenv, "__env_qdrant__") + cc := store.ConnectionConfig - qdrantPort := 6334 // Default port - if portStr := os.Getenv("QDRANT_PORT"); portStr != "" { - if port, err := strconv.Atoi(portStr); err == nil { - qdrantPort = port - } - } + log.Infof("Connecting to Qdrant at %s:%d (TLS: %v)", cc.Host, cc.Port, cc.UseTLS) - // API key for authentication (optional) - qdrantAPIKey := os.Getenv("QDRANT_API_KEY") - - // TLS configuration (optional, defaults to false) - // Enable TLS unless explicitly set to "false" or "0" (case insensitive) - qdrantUseTLS := false - if useTLSStr := os.Getenv("QDRANT_USE_TLS"); useTLSStr != "" { - useTLSLower := strings.ToLower(strings.TrimSpace(useTLSStr)) - qdrantUseTLS = useTLSLower != "false" && useTLSLower != "0" - } - - log.Infof("Connecting to Qdrant at %s:%d (TLS: %v)", qdrantHost, qdrantPort, qdrantUseTLS) - - client, err := newEnvQdrantClient(qdrantHost, qdrantPort, qdrantAPIKey, qdrantUseTLS) + client, err := newEnvQdrantClient(cc.Host, cc.Port, cc.APIKey, cc.UseTLS) if err != nil { log.Errorf("Create qdrant client failed: %v", err) } else { - qdrantRepository := qdrantRepo.NewQdrantRetrieveEngineRepository(client, nil) + qdrantRepository := qdrantRepo.NewQdrantRetrieveEngineRepository(client, &store.IndexConfig) if err := registry.Register( retriever.NewKVHybridRetrieveEngine( qdrantRepository, types.QdrantRetrieverEngineType, diff --git a/internal/handler/vectorstore_env_test.go b/internal/handler/vectorstore_env_test.go new file mode 100644 index 00000000..446cdb44 --- /dev/null +++ b/internal/handler/vectorstore_env_test.go @@ -0,0 +1,52 @@ +package handler + +import ( + "context" + "net/http" + "net/http/httptest" + "testing" + + "github.com/Tencent/WeKnora/internal/types" + "github.com/Tencent/WeKnora/internal/types/interfaces" + "github.com/gin-gonic/gin" + "github.com/stretchr/testify/assert" +) + +type envConnectionTestService struct { + interfaces.VectorStoreService + engine types.RetrieverEngineType + config types.ConnectionConfig + called bool +} + +func (s *envConnectionTestService) TestConnection( + _ context.Context, engine types.RetrieverEngineType, config types.ConnectionConfig, +) (string, error) { + s.called, s.engine, s.config = true, engine, config + return "test-version", nil +} + +func TestQdrantEnvStoreConnectionSettings(t *testing.T) { + t.Setenv("RETRIEVE_DRIVER", "qdrant") + t.Setenv("QDRANT_HOST", "vectors.example") + t.Setenv("QDRANT_PORT", "7443") + t.Setenv("QDRANT_API_KEY", "secret") + t.Setenv("QDRANT_USE_TLS", "true") + + svc := &envConnectionTestService{} + h := NewVectorStoreHandler(nil, svc) + w := httptest.NewRecorder() + c, _ := gin.CreateTestContext(w) + c.Request = httptest.NewRequest(http.MethodPost, "/vector-stores/__env_qdrant__/test", nil) + c.Params = gin.Params{{Key: "id", Value: "__env_qdrant__"}} + c.Set(types.TenantIDContextKey.String(), uint64(1)) + h.TestStoreByID(c) + + assert.Equal(t, http.StatusOK, w.Code) + assert.True(t, svc.called) + assert.Equal(t, types.QdrantRetrieverEngineType, svc.engine) + assert.Equal(t, types.ConnectionConfig{ + Host: "vectors.example", Port: 7443, APIKey: "secret", UseTLS: true, + }, svc.config) + assert.NotContains(t, w.Body.String(), "secret") +} diff --git a/internal/types/vectorstore.go b/internal/types/vectorstore.go index 48b3d621..18986edc 100644 --- a/internal/types/vectorstore.go +++ b/internal/types/vectorstore.go @@ -893,13 +893,32 @@ func buildEnvStoreForDriver(driver string, envLookup EnvLookupFunc) *VectorStore }, } case "qdrant": + host := envLookup("QDRANT_HOST") + if host == "" { + host = "localhost" + } + port := 6334 + if value, err := strconv.Atoi(envLookup("QDRANT_PORT")); err == nil { + port = value + } + useTLS := false + // Preserve the env convention: any nonempty value except false or 0 enables TLS. + if value := envLookup("QDRANT_USE_TLS"); value != "" { + value = strings.ToLower(strings.TrimSpace(value)) + useTLS = value != "false" && value != "0" + } return &VectorStore{ ID: "__env_qdrant__", Name: "Qdrant", EngineType: QdrantRetrieverEngineType, ConnectionConfig: ConnectionConfig{ - Host: envLookup("QDRANT_HOST"), + Host: host, + Port: port, APIKey: envLookup("QDRANT_API_KEY"), + UseTLS: useTLS, + }, + IndexConfig: IndexConfig{ + CollectionPrefix: envLookup("QDRANT_COLLECTION"), }, } case "milvus": diff --git a/internal/types/vectorstore_test.go b/internal/types/vectorstore_test.go index f21ad2e5..ae38abe3 100644 --- a/internal/types/vectorstore_test.go +++ b/internal/types/vectorstore_test.go @@ -41,6 +41,72 @@ func TestIsEnvStoreID(t *testing.T) { } } +func TestQdrantEnvSettings(t *testing.T) { + for _, tt := range []struct { + name string + env map[string]string + connection ConnectionConfig + collection string + }{ + { + name: "defaults", + connection: ConnectionConfig{Host: "localhost", Port: 6334}, + collection: "weknora_embeddings", + }, + { + name: "custom settings", + env: map[string]string{ + "QDRANT_HOST": "vectors.example", "QDRANT_PORT": "7443", + "QDRANT_API_KEY": "secret", "QDRANT_USE_TLS": "true", + "QDRANT_COLLECTION": "custom_vectors", + }, + connection: ConnectionConfig{Host: "vectors.example", Port: 7443, APIKey: "secret", UseTLS: true}, + collection: "custom_vectors", + }, + { + name: "invalid port uses default", + env: map[string]string{"QDRANT_PORT": "invalid"}, + connection: ConnectionConfig{Host: "localhost", Port: 6334}, + collection: "weknora_embeddings", + }, + } { + t.Run(tt.name, func(t *testing.T) { + store := FindEnvVectorStore("qdrant", mockEnvLookup(tt.env), "__env_qdrant__") + require.NotNil(t, store) + assert.Equal(t, tt.connection, store.ConnectionConfig) + assert.Equal(t, tt.connection.GetEndpoint(), store.ConnectionConfig.GetEndpoint()) + assert.Equal(t, tt.collection, store.IndexConfig.GetIndexNameOrDefault(QdrantRetrieverEngineType)) + }) + } +} + +func TestQdrantEnvTLSCompatibility(t *testing.T) { + for _, tt := range []struct { + value string + want bool + }{ + {"", false}, + {"false", false}, + {" FaLsE ", false}, + {"0", false}, + {" 0 ", false}, + {"true", true}, + {" TRUE ", true}, + {"1", true}, + {"yes", true}, + {"off", true}, + {" ", true}, + } { + t.Run(tt.value, func(t *testing.T) { + store := FindEnvVectorStore("qdrant", mockEnvLookup(map[string]string{ + "QDRANT_USE_TLS": tt.value, + }), "__env_qdrant__") + require.NotNil(t, store) + assert.Equal(t, tt.want, store.ConnectionConfig.UseTLS) + }) + } +} + func TestBuildEnvVectorStores(t *testing.T) { envMap := map[string]string{ "ELASTICSEARCH_ADDR": "http://es:9200",