Skip to content

Commit

Permalink
fix: fallback to URL-path when parsing auth config URL without scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
p-jahn committed Apr 18, 2024
1 parent 4002fc2 commit 6fb299e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
8 changes: 7 additions & 1 deletion docker_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
21 changes: 20 additions & 1 deletion docker_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6fb299e

Please sign in to comment.