From 9aaded4a5bb512dcaa1e6971af1cfb206d372b39 Mon Sep 17 00:00:00 2001 From: qingliu Date: Sat, 26 Aug 2023 11:25:53 +0800 Subject: [PATCH] feat: enhance get docker auth allow registry with suffix / --- apis/artifacts/v1alpha1/registry_types.go | 6 ++++++ apis/artifacts/v1alpha1/registry_types_test.go | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/apis/artifacts/v1alpha1/registry_types.go b/apis/artifacts/v1alpha1/registry_types.go index 77321b81..b37aed24 100644 --- a/apis/artifacts/v1alpha1/registry_types.go +++ b/apis/artifacts/v1alpha1/registry_types.go @@ -68,6 +68,12 @@ func GetAuthFromDockerConfigJson(registry string, dockerConfigJsonBytes []byte) candidate = append(candidate, u.Host, "https://"+u.Host, "http://"+u.Host) } + // generate all possible address + for address, auth := range dockerConfig.Auths { + address = strings.TrimRight(address, "/") + dockerConfig.Auths[address] = auth + } + for _, address := range candidate { if auth, ok := dockerConfig.Auths[address]; ok { return auth.Username, auth.Password, nil diff --git a/apis/artifacts/v1alpha1/registry_types_test.go b/apis/artifacts/v1alpha1/registry_types_test.go index ca228570..38b1ee30 100644 --- a/apis/artifacts/v1alpha1/registry_types_test.go +++ b/apis/artifacts/v1alpha1/registry_types_test.go @@ -44,6 +44,10 @@ var _ = Describe("Test.GetAuthFromDockerConfigJson", func() { "https://docker.io/user": { "username": "u4", "password": "p4" + }, + "https://suffix.docker.io/////": { + "username": "u5", + "password": "p5" } } } @@ -82,6 +86,9 @@ var _ = Describe("Test.GetAuthFromDockerConfigJson", func() { Entry("just matched registry", "https://docker.io/user", mockAuths, "u4", "p4", nil, ), + Entry("matched registry suffixed with /", "https://suffix.docker.io", mockAuths, + "u5", "p5", nil, + ), Entry("fallback to host", "https://docker.io/not-exist", mockAuths, "u1", "p1", nil, ),