Skip to content

Commit e6f5bbf

Browse files
Meyer DanielMeyer Daniel
Meyer Daniel
authored and
Meyer Daniel
committed
fix testcontainers#2767 allow missing repository config
- allow missing config file -- replace registry cache key with a key generated by any defined config - allow no provided config
1 parent 553afd3 commit e6f5bbf

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

docker_auth.go

+9-22
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"encoding/json"
99
"errors"
1010
"fmt"
11-
"io"
1211
"net/url"
1312
"os"
1413
"sync"
@@ -137,26 +136,14 @@ func (c *credentialsCache) Get(hostname, configKey string) (string, string, erro
137136
return user, password, nil
138137
}
139138

140-
// configFileKey returns a key to use for caching credentials based on
139+
// configKey returns a key to use for caching credentials based on
141140
// the contents of the currently active config.
142-
func configFileKey() (string, error) {
143-
configPath, err := dockercfg.ConfigPath()
141+
func configKey(cfg dockercfg.Config) (string, error) {
142+
h := md5.New()
143+
err := json.NewEncoder(h).Encode(cfg)
144144
if err != nil {
145145
return "", err
146146
}
147-
148-
f, err := os.Open(configPath)
149-
if err != nil {
150-
return "", fmt.Errorf("open config file: %w", err)
151-
}
152-
153-
defer f.Close()
154-
155-
h := md5.New()
156-
if _, err := io.Copy(h, f); err != nil {
157-
return "", fmt.Errorf("copying config file: %w", err)
158-
}
159-
160147
return hex.EncodeToString(h.Sum(nil)), nil
161148
}
162149

@@ -165,10 +152,11 @@ func configFileKey() (string, error) {
165152
func getDockerAuthConfigs() (map[string]registry.AuthConfig, error) {
166153
cfg, err := getDockerConfig()
167154
if err != nil {
168-
return nil, err
155+
// accept no configured registries since all might be accessible anonymously
156+
return map[string]registry.AuthConfig{}, nil
169157
}
170158

171-
configKey, err := configFileKey()
159+
key, err := configKey(cfg)
172160
if err != nil {
173161
return nil, err
174162
}
@@ -195,7 +183,7 @@ func getDockerAuthConfigs() (map[string]registry.AuthConfig, error) {
195183
switch {
196184
case ac.Username == "" && ac.Password == "":
197185
// Look up credentials from the credential store.
198-
u, p, err := creds.Get(k, configKey)
186+
u, p, err := creds.Get(k, key)
199187
if err != nil {
200188
results <- authConfigResult{err: err}
201189
return
@@ -218,12 +206,11 @@ func getDockerAuthConfigs() (map[string]registry.AuthConfig, error) {
218206
go func(k string) {
219207
defer wg.Done()
220208

221-
u, p, err := creds.Get(k, configKey)
209+
u, p, err := creds.Get(k, key)
222210
if err != nil {
223211
results <- authConfigResult{err: err}
224212
return
225213
}
226-
227214
results <- authConfigResult{
228215
key: k,
229216
cfg: registry.AuthConfig{

docker_auth_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -414,4 +414,12 @@ func Test_getDockerAuthConfigs(t *testing.T) {
414414
}
415415
require.Equal(t, expected, got)
416416
})
417+
418+
t.Run("none", func(t *testing.T) {
419+
// make sure no config is set
420+
t.Setenv("DOCKER_CONFIG", "not existing")
421+
got, err := getDockerAuthConfigs()
422+
require.NoError(t, err)
423+
require.Empty(t, got)
424+
})
417425
}

0 commit comments

Comments
 (0)