Skip to content

Commit e9d5273

Browse files
p-jahnmdelapenya
authored andcommitted
fix: fallback to URL-path when parsing auth config URL without scheme (testcontainers#2488)
1 parent 9f03b53 commit e9d5273

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

docker_auth.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ import (
1313
"github.com/testcontainers/testcontainers-go/internal/core"
1414
)
1515

16+
// defaultRegistryFn is variable overwritten in tests to check for behaviour with different default values.
17+
var defaultRegistryFn = defaultRegistry
18+
1619
// DockerImageAuth returns the auth config for the given Docker image, extracting first its Docker registry.
1720
// Finally, it will use the credential helpers to extract the information from the docker config file
1821
// for that registry, if it exists.
1922
func DockerImageAuth(ctx context.Context, image string) (string, registry.AuthConfig, error) {
20-
defaultRegistry := defaultRegistry(ctx)
23+
defaultRegistry := defaultRegistryFn(ctx)
2124
reg := core.ExtractRegistry(image, defaultRegistry)
2225

2326
cfgs, err := getDockerAuthConfigs()
@@ -44,7 +47,13 @@ func getRegistryAuth(reg string, cfgs map[string]registry.AuthConfig) (registry.
4447
continue
4548
}
4649

47-
if keyURL.Host == reg {
50+
host := keyURL.Host
51+
if keyURL.Scheme == "" {
52+
// url.Parse: The url may be relative (a path, without a host) [...]
53+
host = keyURL.Path
54+
}
55+
56+
if host == reg {
4857
return cfg, true
4958
}
5059
}

docker_auth_test.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,34 @@ func TestGetDockerConfig(t *testing.T) {
153153
}`)
154154

155155
registry, cfg, err := DockerImageAuth(context.Background(), imageReg+imagePath)
156-
require.Equal(t, err, dockercfg.ErrCredentialsNotFound)
156+
require.ErrorIs(t, err, dockercfg.ErrCredentialsNotFound)
157+
require.Empty(t, cfg)
158+
159+
assert.Equal(t, imageReg, registry)
160+
})
161+
162+
t.Run("fail to match registry authentication by host with empty URL scheme creds and missing default", func(t *testing.T) {
163+
origDefaultRegistryFn := defaultRegistryFn
164+
t.Cleanup(func() {
165+
defaultRegistryFn = origDefaultRegistryFn
166+
})
167+
defaultRegistryFn = func(ctx context.Context) string {
168+
return ""
169+
}
170+
171+
base64 := "Z29waGVyOnNlY3JldA==" // gopher:secret
172+
imageReg := ""
173+
imagePath := "image:latest"
174+
175+
t.Setenv("DOCKER_AUTH_CONFIG", `{
176+
"auths": {
177+
"example-auth.com": { "username": "gopher", "password": "secret", "auth": "`+base64+`" }
178+
},
179+
"credsStore": "desktop"
180+
}`)
181+
182+
registry, cfg, err := DockerImageAuth(context.Background(), imageReg+imagePath)
183+
require.ErrorIs(t, err, dockercfg.ErrCredentialsNotFound)
157184
require.Empty(t, cfg)
158185

159186
assert.Equal(t, imageReg, registry)

0 commit comments

Comments
 (0)