From 9d56ebb611e008f5ad09048499331f7aac1ceed3 Mon Sep 17 00:00:00 2001 From: Miloslav Trmač Date: Sat, 11 Sep 2021 22:47:52 +0200 Subject: Consolidate the error handling path in GetCredentials MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We'll share even more code here in the future. Should not change behavior. Signed-off-by: Miloslav Trmač --- pkg/auth/auth.go | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'pkg/auth') diff --git a/pkg/auth/auth.go b/pkg/auth/auth.go index a95ae4764..a4d7896a8 100644 --- a/pkg/auth/auth.go +++ b/pkg/auth/auth.go @@ -37,20 +37,23 @@ func GetCredentials(r *http.Request) (*types.DockerAuthConfig, string, error) { hdr := r.Header.Values(key.String()) return hdr, len(hdr) > 0 } + var override *types.DockerAuthConfig + var authFile string + var headerName HeaderAuthName + var err error if hdr, ok := nonemptyHeaderValue(XRegistryConfigHeader); ok { - c, f, err := getConfigCredentials(r, hdr) - if err != nil { - return nil, "", errors.Wrapf(err, "failed to parse %q header for %s", XRegistryConfigHeader, r.URL.String()) - } - return c, f, nil + headerName = XRegistryConfigHeader + override, authFile, err = getConfigCredentials(r, hdr) } else if hdr, ok := nonemptyHeaderValue(XRegistryAuthHeader); ok { - c, f, err := getAuthCredentials(hdr) - if err != nil { - return nil, "", errors.Wrapf(err, "failed to parse %q header for %s", XRegistryAuthHeader, r.URL.String()) - } - return c, f, nil + headerName = XRegistryAuthHeader + override, authFile, err = getAuthCredentials(hdr) + } else { + return nil, "", nil + } + if err != nil { + return nil, "", errors.Wrapf(err, "failed to parse %q header for %s", headerName, r.URL.String()) } - return nil, "", nil + return override, authFile, nil } // getConfigCredentials extracts one or more docker.AuthConfig from a request and its -- cgit v1.2.3-54-g00ecf