Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [2.4] Add IP address validation from paramtable (#37416) #37500

Open
wants to merge 1 commit into
base: 2.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading