Skip to content

Commit

Permalink
fix: [2.4] Add IP address validation from paramtable (#37416) (#37500)
Browse files Browse the repository at this point in the history
Cherry-pick from master
pr: #37416
See also #37404 #37402

IP address in paramtable need validation and fail fast with reasonable
error message

---------

Signed-off-by: Congqi Xia <[email protected]>
  • Loading branch information
congqixia authored Nov 11, 2024
1 parent cedc340 commit 4f42611
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
15 changes: 15 additions & 0 deletions pkg/util/funcutil/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
}

Expand Down
32 changes: 27 additions & 5 deletions pkg/util/funcutil/func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 4f42611

Please sign in to comment.