Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix(milvus): validate connections and preserve gRPC resolver defaults (#3654)
AI即世界afc6cbd50264
diff --git a/docreader/client/client.go b/docreader/client/client.goindex e3c3d706c..51ab6cf04 100644--- a/docreader/client/client.go+++ b/docreader/client/client.go@@ -9,7 +9,6 @@ import ("github.com/Tencent/WeKnora/docreader/proto""google.golang.org/grpc"- "google.golang.org/grpc/resolver")func getMaxMessageSize() int {@@ -58,8 +57,6 @@ func NewClientWithAuth(addr string, authConfig *AuthConfig) (*Client, error) {return nil, err}- resolver.SetDefaultScheme("dns")-startTime := time.Now()conn, err := grpc.Dial("dns:///"+addr, opts...)if err != nil {diff --git a/docreader/client/client_test.go b/docreader/client/client_test.goindex 53bee3616..b45260ff5 100644--- a/docreader/client/client_test.go+++ b/docreader/client/client_test.go@@ -8,8 +8,24 @@ import ("time""github.com/Tencent/WeKnora/docreader/proto"+ "google.golang.org/grpc/resolver")+func TestNewClientDoesNotChangeGlobalGRPCResolver(t *testing.T) {+ previous := resolver.GetDefaultScheme()+ resolver.SetDefaultScheme("passthrough")+ t.Cleanup(func() { resolver.SetDefaultScheme(previous) })++ client, err := NewClientWithAuth("127.0.0.1:1", &AuthConfig{})+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { _ = client.Close() })+ if got := resolver.GetDefaultScheme(); got != "passthrough" {+ t.Fatalf("DocReader changed global gRPC resolver to %q", got)+ }+}+func init() {log.SetOutput(os.Stdout)log.SetFlags(log.LstdFlags | log.Lmicroseconds | log.Lshortfile)diff --git a/internal/application/service/vectorstore_healthcheck.go b/internal/application/service/vectorstore_healthcheck.goindex 44f1caf9f..ff7c50a93 100644--- a/internal/application/service/vectorstore_healthcheck.go+++ b/internal/application/service/vectorstore_healthcheck.go@@ -17,12 +17,15 @@ import (secutils "github.com/Tencent/WeKnora/internal/utils""github.com/go-sql-driver/mysql" // MySQL driver for database/sql, used by Doris connection test_ "github.com/jackc/pgx/v5/stdlib" // pgx driver for database/sql+ "github.com/milvus-io/milvus/client/v2/milvusclient""github.com/qdrant/go-client/qdrant""github.com/tencent/vectordatabase-sdk-go/tcvectordb""github.com/weaviate/weaviate-go-client/v5/weaviate""github.com/weaviate/weaviate-go-client/v5/weaviate/auth"wgrpc "github.com/weaviate/weaviate-go-client/v5/weaviate/grpc""google.golang.org/grpc"+ "google.golang.org/grpc/codes"+ "google.golang.org/grpc/status")const connectionTestTimeout = 10 * time.Second@@ -175,11 +178,8 @@ func testQdrantConnection(ctx context.Context, config types.ConnectionConfig) (s}func testMilvusConnection(ctx context.Context, config types.ConnectionConfig) (string, error) {- // Use TCP dial instead of the Milvus SDK to avoid protobuf namespace conflict- // between milvus-proto and qdrant-client (both register "common.proto").- // A TCP dial is sufficient for connectivity verification; the Milvus SDK client- // creation in container.go (PR 3) will validate full gRPC connectivity.- // Version detection is not possible with TCP dial alone.+ // The same SDK handshake used by the runtime is needed here: a TCP dial can+ // succeed even when Milvus rejects the configured credentials or database.testCtx, cancel := context.WithTimeout(ctx, connectionTestTimeout)defer cancel()@@ -188,12 +188,37 @@ func testMilvusConnection(ctx context.Context, config types.ConnectionConfig) (saddr = "localhost:19530"}- conn, err := secutils.SSRFSafeDialContext(testCtx, "tcp", addr)+ client, err := milvusclient.New(testCtx, &milvusclient.ClientConfig{+ Address: addr,+ Username: config.Username,+ Password: config.Password,+ DBName: config.Database,+ DialOptions: []grpc.DialOption{+ grpc.WithContextDialer(secutils.SSRFSafeGRPCDialer),+ },+ })if err != nil {- logger.Warnf(ctx, "Milvus connection test failed: %v", err)- return "", errors.NewBadRequestError("failed to connect to milvus: connection refused or server unreachable")- }- defer conn.Close()+ logger.Warnf(ctx, "Milvus connection test failed (gRPC status: %s)", status.Code(err))+ switch status.Code(err) {+ case codes.Unauthenticated:+ return "", errors.NewBadRequestError(+ "failed to connect to milvus: authentication failed; check username and password",+ )+ case codes.PermissionDenied:+ return "", errors.NewBadRequestError("failed to connect to milvus: permission denied; check user access")+ case codes.NotFound:+ return "", errors.NewBadRequestError("failed to connect to milvus: database not found; check database name")+ default:+ return "", errors.NewBadRequestError(+ "failed to connect to milvus: server unreachable or connection configuration invalid",+ )+ }+ }+ defer func() {+ if closeErr := client.Close(context.Background()); closeErr != nil {+ logger.Warnf(ctx, "Failed to close Milvus connection test client: %v", closeErr)+ }+ }()return "", nil}diff --git a/internal/application/service/vectorstore_healthcheck_ssrf_test.go b/internal/application/service/vectorstore_healthcheck_ssrf_test.goindex 2e23cf004..401cd3bc6 100644--- a/internal/application/service/vectorstore_healthcheck_ssrf_test.go+++ b/internal/application/service/vectorstore_healthcheck_ssrf_test.go@@ -2,13 +2,45 @@ package serviceimport ("context"+ "net""strings""testing""time""github.com/Tencent/WeKnora/internal/types"+ secutils "github.com/Tencent/WeKnora/internal/utils"+ "google.golang.org/grpc"+ "google.golang.org/grpc/codes"+ "google.golang.org/grpc/status")+func TestTestConnection_MilvusRejectsAuthenticationFailure(t *testing.T) {+ listener, err := net.Listen("tcp", "127.0.0.1:0")+ if err != nil {+ t.Fatal(err)+ }+ server := grpc.NewServer(grpc.UnknownServiceHandler(func(_ any, _ grpc.ServerStream) error {+ return status.Error(codes.Unauthenticated, "invalid credentials")+ }))+ go func() { _ = server.Serve(listener) }()+ t.Cleanup(func() {+ server.Stop()+ _ = listener.Close()+ secutils.SetSSRFWhitelistFromRaw("")+ })+ secutils.SetSSRFWhitelistFromRaw("127.0.0.1")++ svc := NewVectorStoreService(&mockVectorStoreRepo{}, nil, nil, nil, nil)+ ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)+ defer cancel()+ _, err = svc.TestConnection(ctx, types.MilvusRetrieverEngineType, types.ConnectionConfig{+ Addr: listener.Addr().String(), Username: "user", Password: "wrong",+ })+ if err == nil || !strings.Contains(err.Error(), "authentication failed") {+ t.Fatalf("expected Milvus authentication failure, got %v", err)+ }+}+func TestTestConnection_MilvusBlocksUnsafeAddrAtDialSink(t *testing.T) {svc := NewVectorStoreService(&mockVectorStoreRepo{}, nil, nil, nil, nil)diff --git a/internal/infrastructure/docparser/grpc_parser.go b/internal/infrastructure/docparser/grpc_parser.goindex e78b2ca43..287bc81f1 100644--- a/internal/infrastructure/docparser/grpc_parser.go+++ b/internal/infrastructure/docparser/grpc_parser.go@@ -15,7 +15,6 @@ import ("github.com/Tencent/WeKnora/internal/types""google.golang.org/grpc""google.golang.org/grpc/codes"- "google.golang.org/grpc/resolver""google.golang.org/grpc/status")@@ -62,8 +61,6 @@ func (p *GRPCDocumentReader) connect(addr string) error {)}- resolver.SetDefaultScheme("dns")-start := time.Now()conn, err := grpc.Dial("dns:///"+addr, opts...)if err != nil {diff --git a/internal/infrastructure/docparser/grpc_parser_resolver_test.go b/internal/infrastructure/docparser/grpc_parser_resolver_test.gonew file mode 100644index 000000000..a2d4817ad--- /dev/null+++ b/internal/infrastructure/docparser/grpc_parser_resolver_test.go@@ -0,0 +1,22 @@+package docparser++import (+ "testing"++ "google.golang.org/grpc/resolver"+)++func TestGRPCDocumentReaderDoesNotChangeGlobalResolver(t *testing.T) {+ previous := resolver.GetDefaultScheme()+ resolver.SetDefaultScheme("passthrough")+ t.Cleanup(func() { resolver.SetDefaultScheme(previous) })++ reader, err := NewGRPCDocumentReader("127.0.0.1:1")+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { _ = reader.Close() })+ if got := resolver.GetDefaultScheme(); got != "passthrough" {+ t.Fatalf("DocReader changed global gRPC resolver to %q", got)+ }+}