From efccecbd35bf8788c3c1ed22bf4c677bf97649cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Mei=C3=9Fner?= Date: Thu, 11 Jul 2019 14:08:23 +0200 Subject: [PATCH] parse credentials without scheme When the host within the docker config has format IP:PORT url.Parse(host) fails. This commit will first try reparsing with prepended scheme before returning any error. --- registry/credentials.go | 23 ++++++++++++----------- registry/credentials_test.go | 4 ++++ 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/registry/credentials.go b/registry/credentials.go index 9962b8903..ed9e53419 100644 --- a/registry/credentials.go +++ b/registry/credentials.go @@ -74,28 +74,29 @@ func ParseCredentials(from string, b []byte) (Credentials, error) { return Credentials{}, err } + if host == "http://" || host == "https://" { + return Credentials{}, errors.New("Empty registry auth url") + } + // Some users were passing in credentials in the form of // http://docker.io and http://docker.io/v1/, etc. // So strip everything down to the host. // Also, the registry might be local and on a different port. // So we need to check for that because url.Parse won't parse the ip:port format very well. u, err := url.Parse(host) - if err != nil { - return Credentials{}, err - } - if u.Host == "" && u.Path == "" && !strings.Contains(host, ":") || host == "http://" || host == "https://" { - return Credentials{}, errors.New("Empty registry auth url") - } - if u.Host == "" { // If there's no https:// prefix, it won't parse the host. + + // if anything went wrong try to prepend https:// + if err != nil || u.Host == "" { u, err = url.Parse(fmt.Sprintf("https://%s/", host)) if err != nil { return Credentials{}, err } - // If the host is still empty, then there's probably a rogue / - if u.Host == "" { - return Credentials{}, errors.New("Invalid registry auth url. Must be a valid http address (e.g. https://gcr.io/v1/)") - } } + + if u.Host == "" { // If host is still empty the url must be broken. + return Credentials{}, errors.New("Invalid registry auth url. Must be a valid http address (e.g. https://gcr.io/v1/)") + } + host = u.Host creds.registry = host diff --git a/registry/credentials_test.go b/registry/credentials_test.go index b93c8f282..44c013ebc 100644 --- a/registry/credentials_test.go +++ b/registry/credentials_test.go @@ -39,6 +39,10 @@ func TestRemoteFactory_ParseHost(t *testing.T) { host: "localhost:5000/v2/", imagePrefix: "localhost:5000", }, + { + host: "192.168.99.100:5000", + imagePrefix: "192.168.99.100:5000", + }, { host: "https://192.168.99.100:5000/v2", imagePrefix: "192.168.99.100:5000",