diff --git a/pkg/util/funcutil/func.go b/pkg/util/funcutil/func.go index 218d4082cc557..1a9862c9c371e 100644 --- a/pkg/util/funcutil/func.go +++ b/pkg/util/funcutil/func.go @@ -30,12 +30,14 @@ import ( "time" "github.com/cockroachdb/errors" + "go.uber.org/zap" "google.golang.org/grpc/codes" grpcStatus "google.golang.org/grpc/status" "github.com/milvus-io/milvus-proto/go-api/v2/commonpb" "github.com/milvus-io/milvus-proto/go-api/v2/milvuspb" "github.com/milvus-io/milvus-proto/go-api/v2/schemapb" + "github.com/milvus-io/milvus/pkg/log" "github.com/milvus-io/milvus/pkg/util" "github.com/milvus-io/milvus/pkg/util/typeutil" ) @@ -57,6 +59,19 @@ func GetIP(ip string) string { if len(ip) == 0 { return GetLocalIP() } + netIP := net.ParseIP(ip) + // not a valid ip addr + if netIP == nil { + log.Warn("cannot parse input ip, treat it as hostname/service name", zap.String("ip", ip)) + return ip + } + // only localhost or unicast is acceptable + if netIP.IsUnspecified() { + panic(errors.Newf(`"%s" in param table is Unspecified IP address and cannot be used`)) + } + if netIP.IsMulticast() || netIP.IsLinkLocalMulticast() || netIP.IsInterfaceLocalMulticast() { + panic(errors.Newf(`"%s" in param table is Multicast IP address and cannot be used`)) + } return ip } diff --git a/pkg/util/funcutil/func_test.go b/pkg/util/funcutil/func_test.go index c5c05a11ec686..ed9c2555f54a3 100644 --- a/pkg/util/funcutil/func_test.go +++ b/pkg/util/funcutil/func_test.go @@ -61,11 +61,33 @@ func Test_GetLocalIP(t *testing.T) { } func Test_GetIP(t *testing.T) { - ip := GetIP("") - assert.NotNil(t, ip) - assert.NotZero(t, len(ip)) - ip = GetIP("127.0.0") - assert.Equal(t, ip, "127.0.0") + t.Run("empty_fallback_auto", func(t *testing.T) { + ip := GetIP("") + assert.NotNil(t, ip) + assert.NotZero(t, len(ip)) + }) + + t.Run("valid_ip", func(t *testing.T) { + assert.NotPanics(t, func() { + ip := GetIP("8.8.8.8") + assert.Equal(t, "8.8.8.8", ip) + }) + }) + + t.Run("invalid_ip", func(t *testing.T) { + assert.NotPanics(t, func() { + ip := GetIP("null") + assert.Equal(t, "null", ip) + }, "non ip format, could be hostname or service name") + + assert.Panics(t, func() { + GetIP("0.0.0.0") + }, "input is unspecified ip address, panicking") + + assert.Panics(t, func() { + GetIP("224.0.0.1") + }, "input is multicast ip address, panicking") + }) } func Test_ParseIndexParamsMap(t *testing.T) {