mirror of
https://gitee.com/milvus-io/milvus.git
synced 2024-11-29 18:38:44 +08:00
fix: Add IP address validation from paramtable (#37416)
See also #37404 #37402 IP address in paramtable need validation and fail fast with reasonable error message --------- Signed-off-by: Congqi Xia <congqi.xia@zilliz.com>
This commit is contained in:
parent
ee54a98578
commit
6325d02504
@ -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
|
||||
}
|
||||
|
||||
|
@ -91,11 +91,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) {
|
||||
|
Loading…
Reference in New Issue
Block a user