Merge pull request #5196 from thaJeztah/carry_fix_custom_ports

cli/config/credentials: ConvertToHostname: handle IP-addresses
This commit is contained in:
Sebastiaan van Stijn 2024-06-26 15:09:11 +02:00 committed by GitHub
commit 1996259c78
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 1 deletions

View File

@ -1,6 +1,7 @@
package credentials package credentials
import ( import (
"net"
"net/url" "net/url"
"strings" "strings"
@ -77,7 +78,7 @@ func ConvertToHostname(maybeURL string) string {
if u.Port() == "" { if u.Port() == "" {
return u.Hostname() return u.Hostname()
} }
return u.Hostname() + ":" + u.Port() return net.JoinHostPort(u.Hostname(), u.Port())
} }
} }
hostName, _, _ := strings.Cut(stripped, "/") hostName, _, _ := strings.Cut(stripped, "/")

View File

@ -137,6 +137,19 @@ func TestFileStoreErase(t *testing.T) {
func TestConvertToHostname(t *testing.T) { func TestConvertToHostname(t *testing.T) {
tests := []struct{ input, expected string }{ tests := []struct{ input, expected string }{
{
input: "127.0.0.1",
expected: "127.0.0.1",
},
{
input: "::1",
expected: "::1",
},
{
// FIXME(thaJeztah): this should be normalized to "::1" if there's no port (or vice-versa, as long as we're consistent)
input: "[::1]",
expected: "[::1]",
},
{ {
input: "example.com", input: "example.com",
expected: "example.com", expected: "example.com",
@ -168,10 +181,35 @@ func TestConvertToHostname(t *testing.T) {
expected: "example.com", expected: "example.com",
}, },
// should support non-standard port in registry url // should support non-standard port in registry url
{
input: "127.0.0.1:6556",
expected: "127.0.0.1:6556",
},
{
// FIXME(thaJeztah): this should be normalized to "[::1]:6556"
input: "::1:6556",
expected: "::1:6556",
},
{
input: "[::1]:6556",
expected: "[::1]:6556",
},
{ {
input: "example.com:6555", input: "example.com:6555",
expected: "example.com:6555", expected: "example.com:6555",
}, },
{
input: "https://127.0.0.1:6555/v2/",
expected: "127.0.0.1:6555",
},
{
input: "https://::1:6555/v2/",
expected: "[::1]:6555",
},
{
input: "https://[::1]:6555/v2/",
expected: "[::1]:6555",
},
{ {
input: "http://example.com:6555", input: "http://example.com:6555",
expected: "example.com:6555", expected: "example.com:6555",