From ca3dbd95327140cbacccc41d93a9e7e54788f859 Mon Sep 17 00:00:00 2001 From: Jimmi Dyson Date: Tue, 22 Nov 2022 20:38:08 +0000 Subject: [PATCH] fix: Handle scheme in mirror endpoint --- .../v1alpha1/credentialproviderconfig_validation.go | 2 +- pkg/credentialprovider/shim/shim.go | 4 ++-- pkg/urlglobber/matcher.go | 13 ++++++++----- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/apis/config/v1alpha1/credentialproviderconfig_validation.go b/apis/config/v1alpha1/credentialproviderconfig_validation.go index dece6a5..23e5df0 100644 --- a/apis/config/v1alpha1/credentialproviderconfig_validation.go +++ b/apis/config/v1alpha1/credentialproviderconfig_validation.go @@ -128,7 +128,7 @@ func validateCredentialProviderConfig( } for _, matchImage := range provider.MatchImages { - if _, err := urlglobber.ParseSchemelessURL(matchImage); err != nil { + if _, err := urlglobber.ParsePotentiallySchemelessURL(matchImage); err != nil { allErrs = append( allErrs, field.Invalid( diff --git a/pkg/credentialprovider/shim/shim.go b/pkg/credentialprovider/shim/shim.go index 5d00044..95a54e7 100644 --- a/pkg/credentialprovider/shim/shim.go +++ b/pkg/credentialprovider/shim/shim.go @@ -234,7 +234,7 @@ func (p *shimProvider) getMirrorCredentialsForImage( return credentialproviderv1beta1.AuthConfig{}, 0, false, nil } - imgURL, err := urlglobber.ParseSchemelessURL(img) + imgURL, err := urlglobber.ParsePotentiallySchemelessURL(img) if err != nil { return credentialproviderv1beta1.AuthConfig{}, 0, false, fmt.Errorf( "failed to parse image %q to a URL: %w", @@ -243,7 +243,7 @@ func (p *shimProvider) getMirrorCredentialsForImage( ) } - mirrorURL, err := urlglobber.ParseSchemelessURL(p.cfg.Mirror.Endpoint) + mirrorURL, err := urlglobber.ParsePotentiallySchemelessURL(p.cfg.Mirror.Endpoint) if err != nil { return credentialproviderv1beta1.AuthConfig{}, 0, false, fmt.Errorf( "failed to parse mirror %q to a URL: %w", diff --git a/pkg/urlglobber/matcher.go b/pkg/urlglobber/matcher.go index 3b70ca1..dbd0d50 100644 --- a/pkg/urlglobber/matcher.go +++ b/pkg/urlglobber/matcher.go @@ -28,11 +28,14 @@ import ( "strings" ) -// ParseSchemelessURL parses a schemeless url and returns a url.URL +// ParsePotentiallySchemelessURL 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) { - parsed, err := url.Parse("https://" + schemelessURL) +func ParsePotentiallySchemelessURL(u string) (*url.URL, error) { + if !(strings.HasPrefix(u, "https://") || strings.HasPrefix(u, "http://")) { + u = "https://" + u + } + parsed, err := url.Parse(u) if err != nil { return nil, err } @@ -53,11 +56,11 @@ func SplitURL(u *url.URL) (parts []string, port string) { // URLsMatchStr is wrapper for URLsMatch, operating on strings instead of URLs. func URLsMatchStr(glob, target string) (bool, error) { - globURL, err := ParseSchemelessURL(glob) + globURL, err := ParsePotentiallySchemelessURL(glob) if err != nil { return false, err } - targetURL, err := ParseSchemelessURL(target) + targetURL, err := ParsePotentiallySchemelessURL(target) if err != nil { return false, err }