From aadc1d25b3dcf7a86197dfd233416ac1823774ed Mon Sep 17 00:00:00 2001 From: Andrew Sy Kim Date: Tue, 10 Nov 2020 13:44:07 -0500 Subject: [PATCH] pkg/credentialprovider: export URL parsing and matching helper functions Signed-off-by: Andrew Sy Kim --- pkg/credentialprovider/keyring.go | 27 +++++++++++++------------- pkg/credentialprovider/keyring_test.go | 4 ++-- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/pkg/credentialprovider/keyring.go b/pkg/credentialprovider/keyring.go index 494770c0e2f..5ce467f9a8b 100644 --- a/pkg/credentialprovider/keyring.go +++ b/pkg/credentialprovider/keyring.go @@ -158,9 +158,10 @@ func isDefaultRegistryMatch(image string) bool { return !strings.ContainsAny(parts[0], ".:") } +// ParseSchemelessURL parses a schemeless url and returns a url.URL // url.Parse require a scheme, but ours don't have schemes. Adding a // scheme to make url.Parse happy, then clear out the resulting scheme. -func parseSchemelessURL(schemelessURL string) (*url.URL, error) { +func ParseSchemelessURL(schemelessURL string) (*url.URL, error) { parsed, err := url.Parse("https://" + schemelessURL) if err != nil { return nil, err @@ -170,8 +171,8 @@ func parseSchemelessURL(schemelessURL string) (*url.URL, error) { return parsed, nil } -// split the host name into parts, as well as the port -func splitURL(url *url.URL) (parts []string, port string) { +// SplitURL splits the host name into parts, as well as the port +func SplitURL(url *url.URL) (parts []string, port string) { host, port, err := net.SplitHostPort(url.Host) if err != nil { // could not parse port @@ -180,20 +181,20 @@ func splitURL(url *url.URL) (parts []string, port string) { return strings.Split(host, "."), port } -// overloaded version of urlsMatch, operating on strings instead of URLs. -func urlsMatchStr(glob string, target string) (bool, error) { - globURL, err := parseSchemelessURL(glob) +// URLsMatchStr is wrapper for URLsMatch, operating on strings instead of URLs. +func URLsMatchStr(glob string, target string) (bool, error) { + globURL, err := ParseSchemelessURL(glob) if err != nil { return false, err } - targetURL, err := parseSchemelessURL(target) + targetURL, err := ParseSchemelessURL(target) if err != nil { return false, err } - return urlsMatch(globURL, targetURL) + return URLsMatch(globURL, targetURL) } -// check whether the given target url matches the glob url, which may have +// URLsMatch checks whether the given target url matches the glob url, which may have // glob wild cards in the host name. // // Examples: @@ -201,9 +202,9 @@ func urlsMatchStr(glob string, target string) (bool, error) { // globURL=*.docker.io, targetURL=not.right.io => no match // // Note that we don't support wildcards in ports and paths yet. -func urlsMatch(globURL *url.URL, targetURL *url.URL) (bool, error) { - globURLParts, globPort := splitURL(globURL) - targetURLParts, targetPort := splitURL(targetURL) +func URLsMatch(globURL *url.URL, targetURL *url.URL) (bool, error) { + globURLParts, globPort := SplitURL(globURL) + targetURLParts, targetPort := SplitURL(targetURL) if globPort != targetPort { // port doesn't match return false, nil @@ -240,7 +241,7 @@ func (dk *BasicDockerKeyring) Lookup(image string) ([]AuthConfig, bool) { for _, k := range dk.index { // both k and image are schemeless URLs because even though schemes are allowed // in the credential configurations, we remove them in Add. - if matched, _ := urlsMatchStr(k, image); matched { + if matched, _ := URLsMatchStr(k, image); matched { ret = append(ret, dk.creds[k]...) } } diff --git a/pkg/credentialprovider/keyring_test.go b/pkg/credentialprovider/keyring_test.go index 8b5893a201d..3dd7bc2410d 100644 --- a/pkg/credentialprovider/keyring_test.go +++ b/pkg/credentialprovider/keyring_test.go @@ -23,7 +23,7 @@ import ( "testing" ) -func TestUrlsMatch(t *testing.T) { +func TestURLsMatch(t *testing.T) { tests := []struct { globURL string targetURL string @@ -112,7 +112,7 @@ func TestUrlsMatch(t *testing.T) { }, } for _, test := range tests { - matched, _ := urlsMatchStr(test.globURL, test.targetURL) + matched, _ := URLsMatchStr(test.globURL, test.targetURL) if matched != test.matchExpected { t.Errorf("Expected match result of %s and %s to be %t, but was %t", test.globURL, test.targetURL, test.matchExpected, matched)