Skip to content

Commit

Permalink
fix: Handle scheme in mirror endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmidyson committed Nov 22, 2022
1 parent 6d1615c commit ca3dbd9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions pkg/credentialprovider/shim/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
13 changes: 8 additions & 5 deletions pkg/urlglobber/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit ca3dbd9

Please sign in to comment.