fix(milvus): validate connections and preserve gRPC resolver defaults (#3654)
* fix(docreader): preserve gRPC resolver defaults * fix(milvus): validate connection with SDK handshake * fix(docparser): preserve gRPC resolver defaults * fix(milvus): satisfy connection check lint --------- Co-authored-by: tunan <tunan@ahyk.com>main
parent
9fa9e7c29d
commit
afc6cbd502
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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) (s
|
|||
addr = "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")
|
||||
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 conn.Close()
|
||||
defer func() {
|
||||
if closeErr := client.Close(context.Background()); closeErr != nil {
|
||||
logger.Warnf(ctx, "Failed to close Milvus connection test client: %v", closeErr)
|
||||
}
|
||||
}()
|
||||
|
||||
return "", nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,13 +2,45 @@ package service
|
|||
|
||||
import (
|
||||
"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)
|
||||
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue