fix(registry): image name parsing behavior (#1526)
Co-authored-by: nils måsén <nils@piksel.se>pull/1516/head
parent
aa50d12389
commit
25fdb40312
@ -1,36 +1,28 @@
|
|||||||
package helpers
|
package helpers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"github.com/docker/distribution/reference"
|
||||||
url2 "net/url"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ConvertToHostname strips a url from everything but the hostname part
|
// domains for Docker Hub, the default registry
|
||||||
func ConvertToHostname(url string) (string, string, error) {
|
const (
|
||||||
urlWithSchema := fmt.Sprintf("x://%s", url)
|
DefaultRegistryDomain = "docker.io"
|
||||||
u, err := url2.Parse(urlWithSchema)
|
DefaultRegistryHost = "index.docker.io"
|
||||||
if err != nil {
|
LegacyDefaultRegistryDomain = "index.docker.io"
|
||||||
return "", "", err
|
)
|
||||||
}
|
|
||||||
hostName := u.Hostname()
|
|
||||||
port := u.Port()
|
|
||||||
|
|
||||||
return hostName, port, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// NormalizeRegistry makes sure variations of DockerHubs registry
|
// GetRegistryAddress parses an image name
|
||||||
func NormalizeRegistry(registry string) (string, error) {
|
// and returns the address of the specified registry
|
||||||
hostName, port, err := ConvertToHostname(registry)
|
func GetRegistryAddress(imageRef string) (string, error) {
|
||||||
|
normalizedRef, err := reference.ParseNormalizedNamed(imageRef)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
if hostName == "registry-1.docker.io" || hostName == "docker.io" {
|
address := reference.Domain(normalizedRef)
|
||||||
hostName = "index.docker.io"
|
|
||||||
}
|
|
||||||
|
|
||||||
if port != "" {
|
if address == DefaultRegistryDomain {
|
||||||
return fmt.Sprintf("%s:%s", hostName, port), nil
|
address = DefaultRegistryHost
|
||||||
}
|
}
|
||||||
return hostName, nil
|
return address, nil
|
||||||
}
|
}
|
||||||
|
@ -1,65 +1,49 @@
|
|||||||
package registry
|
package registry
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
. "github.com/onsi/ginkgo"
|
. "github.com/onsi/ginkgo"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ = Describe("Testing with Ginkgo", func() {
|
var _ = Describe("Registry credential helpers", func() {
|
||||||
It("encoded env auth_ should return an error if repo envs are unset", func() {
|
Describe("EncodedAuth", func() {
|
||||||
_ = os.Unsetenv("REPO_USER")
|
It("should return repo credentials from env when set", func() {
|
||||||
_ = os.Unsetenv("REPO_PASS")
|
var err error
|
||||||
|
expected := "eyJ1c2VybmFtZSI6ImNvbnRhaW5ycnItdXNlciIsInBhc3N3b3JkIjoiY29udGFpbnJyci1wYXNzIn0="
|
||||||
_, err := EncodedEnvAuth("")
|
|
||||||
Expect(err).To(HaveOccurred())
|
|
||||||
})
|
|
||||||
It("encoded env auth_ should return auth hash if repo envs are set", func() {
|
|
||||||
var err error
|
|
||||||
expectedHash := "eyJ1c2VybmFtZSI6ImNvbnRhaW5ycnItdXNlciIsInBhc3N3b3JkIjoiY29udGFpbnJyci1wYXNzIn0="
|
|
||||||
|
|
||||||
err = os.Setenv("REPO_USER", "containrrr-user")
|
err = os.Setenv("REPO_USER", "containrrr-user")
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
err = os.Setenv("REPO_PASS", "containrrr-pass")
|
err = os.Setenv("REPO_PASS", "containrrr-pass")
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
config, err := EncodedEnvAuth("")
|
config, err := EncodedEnvAuth()
|
||||||
Expect(config).To(Equal(expectedHash))
|
Expect(config).To(Equal(expected))
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
})
|
||||||
})
|
})
|
||||||
It("encoded config auth_ should return an error if file is not present", func() {
|
|
||||||
var err error
|
|
||||||
|
|
||||||
err = os.Setenv("DOCKER_CONFIG", "/dev/null/should-fail")
|
|
||||||
Expect(err).NotTo(HaveOccurred())
|
|
||||||
|
|
||||||
_, err = EncodedConfigAuth("")
|
Describe("EncodedEnvAuth", func() {
|
||||||
Expect(err).To(HaveOccurred())
|
It("should return an error if repo envs are unset", func() {
|
||||||
|
_ = os.Unsetenv("REPO_USER")
|
||||||
|
_ = os.Unsetenv("REPO_PASS")
|
||||||
|
|
||||||
|
_, err := EncodedEnvAuth()
|
||||||
|
Expect(err).To(HaveOccurred())
|
||||||
|
})
|
||||||
})
|
})
|
||||||
/*
|
|
||||||
* TODO:
|
|
||||||
* This part only confirms that it still works in the same way as it did
|
|
||||||
* with the old version of the docker api client sdk. I'd say that
|
|
||||||
* ParseServerAddress likely needs to be elaborated a bit to default to
|
|
||||||
* dockerhub in case no server address was provided.
|
|
||||||
*
|
|
||||||
* ++ @simskij, 2019-04-04
|
|
||||||
*/
|
|
||||||
It("parse server address_ should return error if passed empty string", func() {
|
|
||||||
|
|
||||||
_, err := ParseServerAddress("")
|
Describe("EncodedConfigAuth", func() {
|
||||||
Expect(err).To(HaveOccurred())
|
It("should return an error if file is not present", func() {
|
||||||
})
|
var err error
|
||||||
It("parse server address_ should return the organization part if passed an image name missing server name", func() {
|
|
||||||
|
|
||||||
val, _ := ParseServerAddress("containrrr/config")
|
err = os.Setenv("DOCKER_CONFIG", "/dev/null/should-fail")
|
||||||
Expect(val).To(Equal("containrrr"))
|
Expect(err).NotTo(HaveOccurred())
|
||||||
})
|
|
||||||
It("parse server address_ should return the server name if passed a fully qualified image name", func() {
|
|
||||||
|
|
||||||
val, _ := ParseServerAddress("github.com/containrrrr/config")
|
_, err = EncodedConfigAuth("")
|
||||||
Expect(val).To(Equal("github.com"))
|
Expect(err).To(HaveOccurred())
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue