From 6fb299e860c58550ea5893ab2b134db7f99f4775 Mon Sep 17 00:00:00 2001 From: Patrick Jahn <33724206+p-jahn@users.noreply.github.com> Date: Wed, 17 Apr 2024 12:42:59 +0200 Subject: [PATCH] fix: fallback to URL-path when parsing auth config URL without scheme --- docker_auth.go | 8 +++++++- docker_auth_test.go | 21 ++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/docker_auth.go b/docker_auth.go index e6fcfedf83..92719b3261 100644 --- a/docker_auth.go +++ b/docker_auth.go @@ -44,7 +44,13 @@ func getRegistryAuth(reg string, cfgs map[string]registry.AuthConfig) (registry. continue } - if keyURL.Host == reg { + host := keyURL.Host + if keyURL.Scheme == "" { + // url.Parse: The url may be relative (a path, without a host) [...] + host = keyURL.Path + } + + if host == reg { return cfg, true } } diff --git a/docker_auth_test.go b/docker_auth_test.go index 514cf753c7..756ef699fc 100644 --- a/docker_auth_test.go +++ b/docker_auth_test.go @@ -153,7 +153,26 @@ func TestGetDockerConfig(t *testing.T) { }`) registry, cfg, err := DockerImageAuth(context.Background(), imageReg+imagePath) - require.Equal(t, err, dockercfg.ErrCredentialsNotFound) + require.ErrorIs(t, err, dockercfg.ErrCredentialsNotFound) + require.Empty(t, cfg) + + assert.Equal(t, imageReg, registry) + }) + + t.Run("fail to match registry authentication by host with empty URL scheme creds and missing registry", func(t *testing.T) { + base64 := "Z29waGVyOnNlY3JldA==" // gopher:secret + imageReg := "" + imagePath := "image:latest" + + t.Setenv("DOCKER_AUTH_CONFIG", `{ + "auths": { + "example-auth.com": { "username": "gopher", "password": "secret", "auth": "`+base64+`" } + }, + "credsStore": "desktop" + }`) + + registry, cfg, err := DockerImageAuth(context.Background(), imageReg+imagePath) + require.ErrorIs(t, err, dockercfg.ErrCredentialsNotFound) require.Empty(t, cfg) assert.Equal(t, imageReg, registry)