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:
congqixia 2024-11-07 10:08:24 +08:00 committed by GitHub
parent ee54a98578
commit 6325d02504
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 42 additions and 5 deletions

View File

@ -30,12 +30,14 @@ import (
"time" "time"
"github.com/cockroachdb/errors" "github.com/cockroachdb/errors"
"go.uber.org/zap"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
grpcStatus "google.golang.org/grpc/status" 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/commonpb"
"github.com/milvus-io/milvus-proto/go-api/v2/milvuspb" "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-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"
"github.com/milvus-io/milvus/pkg/util/typeutil" "github.com/milvus-io/milvus/pkg/util/typeutil"
) )
@ -57,6 +59,19 @@ func GetIP(ip string) string {
if len(ip) == 0 { if len(ip) == 0 {
return GetLocalIP() 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 return ip
} }

View File

@ -91,11 +91,33 @@ func Test_GetLocalIP(t *testing.T) {
} }
func Test_GetIP(t *testing.T) { func Test_GetIP(t *testing.T) {
ip := GetIP("") t.Run("empty_fallback_auto", func(t *testing.T) {
assert.NotNil(t, ip) ip := GetIP("")
assert.NotZero(t, len(ip)) assert.NotNil(t, ip)
ip = GetIP("127.0.0") assert.NotZero(t, len(ip))
assert.Equal(t, ip, "127.0.0") })
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) { func Test_ParseIndexParamsMap(t *testing.T) {