From c5347cb0c61ab51b04d962af3e2399c1c1825cb4 Mon Sep 17 00:00:00 2001 From: kidsan <8798449+Kidsan@users.noreply.github.com> Date: Fri, 20 Jan 2023 16:12:06 +0100 Subject: [PATCH] netutil: consistently format ipv6 addresses This formats ipv6 addresses to ensure they can be compared safely Signed-off-by: kidsan <8798449+Kidsan@users.noreply.github.com> --- pkg/netutil/netutil.go | 2 +- pkg/netutil/netutil_test.go | 29 ++++++++++++++++++++--------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/pkg/netutil/netutil.go b/pkg/netutil/netutil.go index 7d332b99a70..3317d561ba7 100644 --- a/pkg/netutil/netutil.go +++ b/pkg/netutil/netutil.go @@ -103,7 +103,7 @@ func resolveURL(ctx context.Context, lg *zap.Logger, u url.URL) (string, error) ) return "", err } - if host == "localhost" || net.ParseIP(host) != nil { + if host == "localhost" { return "", nil } for ctx.Err() == nil { diff --git a/pkg/netutil/netutil_test.go b/pkg/netutil/netutil_test.go index 22db427e0a0..c085969c0b0 100644 --- a/pkg/netutil/netutil_test.go +++ b/pkg/netutil/netutil_test.go @@ -120,13 +120,16 @@ func TestResolveTCPAddrs(t *testing.T) { if err != nil { return nil, err } - if tt.hostMap[host] == "" { - return nil, errors.New("cannot resolve host") - } i, err := strconv.Atoi(port) if err != nil { return nil, err } + if ip := net.ParseIP(host); ip != nil { + return &net.TCPAddr{IP: ip, Port: i, Zone: ""}, nil + } + if tt.hostMap[host] == "" { + return nil, errors.New("cannot resolve host") + } return &net.TCPAddr{IP: net.ParseIP(tt.hostMap[host]), Port: i, Zone: ""}, nil } ctx, cancel := context.WithTimeout(context.TODO(), time.Second) @@ -152,17 +155,20 @@ func TestURLsEqual(t *testing.T) { "second.com": "10.0.11.2", } resolveTCPAddr = func(ctx context.Context, addr string) (*net.TCPAddr, error) { - host, port, herr := net.SplitHostPort(addr) - if herr != nil { - return nil, herr - } - if _, ok := hostm[host]; !ok { - return nil, errors.New("cannot resolve host.") + host, port, err := net.SplitHostPort(addr) + if err != nil { + return nil, err } i, err := strconv.Atoi(port) if err != nil { return nil, err } + if ip := net.ParseIP(host); ip != nil { + return &net.TCPAddr{IP: ip, Port: i, Zone: ""}, nil + } + if hostm[host] == "" { + return nil, errors.New("cannot resolve host") + } return &net.TCPAddr{IP: net.ParseIP(hostm[host]), Port: i, Zone: ""}, nil } @@ -330,6 +336,11 @@ func TestURLStringsEqual(t *testing.T) { "http://host1:8080", "http://host2:8080", }, errOnResolve}, + { + urlsA: []string{"https://[c262:266f:fa53:0ee6:966e:e3f0:d68f:b046]:2380"}, + urlsB: []string{"https://[c262:266f:fa53:ee6:966e:e3f0:d68f:b046]:2380"}, + resolver: resolveTCPAddrDefault, + }, } for idx, c := range cases { t.Logf("TestURLStringsEqual, case #%d", idx)