Skip to content

Commit 50fae20

Browse files
committed
cli/config/credentials: ConvertToHostname: handle IP-addresses
Signed-off-by: Sebastiaan van Stijn <[email protected]> (cherry picked from commit 8b0a7b0) Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 37533c2 commit 50fae20

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

cli/config/credentials/file_store.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package credentials
22

33
import (
4+
"net"
45
"net/url"
56
"strings"
67

@@ -77,7 +78,7 @@ func ConvertToHostname(maybeURL string) string {
7778
if u.Port() == "" {
7879
return u.Hostname()
7980
}
80-
return u.Hostname() + ":" + u.Port()
81+
return net.JoinHostPort(u.Hostname(), u.Port())
8182
}
8283
}
8384
hostName, _, _ := strings.Cut(stripped, "/")

cli/config/credentials/file_store_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,19 @@ func TestFileStoreErase(t *testing.T) {
137137

138138
func TestConvertToHostname(t *testing.T) {
139139
tests := []struct{ input, expected string }{
140+
{
141+
input: "127.0.0.1",
142+
expected: "127.0.0.1",
143+
},
144+
{
145+
input: "::1",
146+
expected: "::1",
147+
},
148+
{
149+
// FIXME(thaJeztah): this should be normalized to "::1" if there's no port (or vice-versa, as long as we're consistent)
150+
input: "[::1]",
151+
expected: "[::1]",
152+
},
140153
{
141154
input: "example.com",
142155
expected: "example.com",
@@ -168,10 +181,35 @@ func TestConvertToHostname(t *testing.T) {
168181
expected: "example.com",
169182
},
170183
// should support non-standard port in registry url
184+
{
185+
input: "127.0.0.1:6556",
186+
expected: "127.0.0.1:6556",
187+
},
188+
{
189+
// FIXME(thaJeztah): this should be normalized to "[::1]:6556"
190+
input: "::1:6556",
191+
expected: "::1:6556",
192+
},
193+
{
194+
input: "[::1]:6556",
195+
expected: "[::1]:6556",
196+
},
171197
{
172198
input: "example.com:6555",
173199
expected: "example.com:6555",
174200
},
201+
{
202+
input: "https://127.0.0.1:6555/v2/",
203+
expected: "127.0.0.1:6555",
204+
},
205+
{
206+
input: "https://::1:6555/v2/",
207+
expected: "[::1]:6555",
208+
},
209+
{
210+
input: "https://[::1]:6555/v2/",
211+
expected: "[::1]:6555",
212+
},
175213
{
176214
input: "http://example.com:6555",
177215
expected: "example.com:6555",

0 commit comments

Comments
 (0)