From 8b0a7b025d3c2bfb2e2f3f28baed73c59d6f1a15 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 26 Jun 2024 13:21:01 +0200 Subject: [PATCH] cli/config/credentials: ConvertToHostname: handle IP-addresses Signed-off-by: Sebastiaan van Stijn --- cli/config/credentials/file_store.go | 3 +- cli/config/credentials/file_store_test.go | 38 +++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/cli/config/credentials/file_store.go b/cli/config/credentials/file_store.go index 2b37769c36..3b8955994d 100644 --- a/cli/config/credentials/file_store.go +++ b/cli/config/credentials/file_store.go @@ -1,6 +1,7 @@ package credentials import ( + "net" "net/url" "strings" @@ -77,7 +78,7 @@ func ConvertToHostname(maybeURL string) string { if u.Port() == "" { return u.Hostname() } - return u.Hostname() + ":" + u.Port() + return net.JoinHostPort(u.Hostname(), u.Port()) } } hostName, _, _ := strings.Cut(stripped, "/") diff --git a/cli/config/credentials/file_store_test.go b/cli/config/credentials/file_store_test.go index 436a3c8121..94e505e13c 100644 --- a/cli/config/credentials/file_store_test.go +++ b/cli/config/credentials/file_store_test.go @@ -137,6 +137,19 @@ func TestFileStoreErase(t *testing.T) { func TestConvertToHostname(t *testing.T) { 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", expected: "example.com", @@ -168,10 +181,35 @@ func TestConvertToHostname(t *testing.T) { expected: "example.com", }, // 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", 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", expected: "example.com:6555",