diff --git a/pkg/client/client.go b/pkg/client/client.go index 1ad0a57538..1412b37c5a 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -20,6 +20,7 @@ import ( "context" "encoding/json" "fmt" + "net" "net/url" "os" "path/filepath" @@ -218,12 +219,30 @@ func validateAndSanitizePrismCentralInfoAddress(address string) (string, error) if address == "" { return "", ErrPrismAddressNotSet } - u, err := url.Parse(address) - if err != nil { - return "", fmt.Errorf("failed to parse url from given address %w", err) + u, urlParseErr := url.Parse(address) + if urlParseErr != nil { + ipHost, ipErr := parseIP(address) + if ipErr != nil { + return "", fmt.Errorf("failed to resolve %s as url or ip addr %w", address, ipErr) + } + return ipHost, nil } if u.Scheme != "" || u.Port() != "" { return u.Hostname(), nil } return address, nil } + +func parseIP(s string) (string, error) { + ip, _, err := net.SplitHostPort(s) + if err == nil { + return ip, nil + } + + ip2 := net.ParseIP(s) + if ip2 == nil { + return "", fmt.Errorf("invalid IP %s", ip2) + } + + return ip2.String(), nil +} diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index f36664ad71..190e3f663a 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -36,6 +36,30 @@ func Test_validateAndSanitizePrismCentralInfoAddress(t *testing.T) { "", ErrPrismAddressNotSet, }, + { + "using a IP with scheme and port", + "https://1.2.3.4:9440", + "1.2.3.4", + nil, + }, + { + "using a IP with scheme", + "https://1.2.3.4", + "1.2.3.4", + nil, + }, + { + "using a IP with port", + "1.2.3.4:9440", + "1.2.3.4", + nil, + }, + { + "just an IP", + "1.2.3.4", + "1.2.3.4", + nil, + }, } for _, test := range tests {