Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #2767 allow missing repository config #2771

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 9 additions & 22 deletions docker_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net/url"
"os"
"sync"
Expand Down Expand Up @@ -137,26 +136,14 @@ func (c *credentialsCache) Get(hostname, configKey string) (string, string, erro
return user, password, nil
}

// configFileKey returns a key to use for caching credentials based on
// configKey returns a key to use for caching credentials based on
// the contents of the currently active config.
func configFileKey() (string, error) {
configPath, err := dockercfg.ConfigPath()
func configKey(cfg dockercfg.Config) (string, error) {
h := md5.New()
err := json.NewEncoder(h).Encode(cfg)
if err != nil {
return "", err
}

f, err := os.Open(configPath)
if err != nil {
return "", fmt.Errorf("open config file: %w", err)
}

defer f.Close()

h := md5.New()
if _, err := io.Copy(h, f); err != nil {
return "", fmt.Errorf("copying config file: %w", err)
}

return hex.EncodeToString(h.Sum(nil)), nil
}

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

configKey, err := configFileKey()
key, err := configKey(cfg)
if err != nil {
return nil, err
}
Expand All @@ -195,7 +183,7 @@ func getDockerAuthConfigs() (map[string]registry.AuthConfig, error) {
switch {
case ac.Username == "" && ac.Password == "":
// Look up credentials from the credential store.
u, p, err := creds.Get(k, configKey)
u, p, err := creds.Get(k, key)
if err != nil {
results <- authConfigResult{err: err}
return
Expand All @@ -218,12 +206,11 @@ func getDockerAuthConfigs() (map[string]registry.AuthConfig, error) {
go func(k string) {
defer wg.Done()

u, p, err := creds.Get(k, configKey)
u, p, err := creds.Get(k, key)
if err != nil {
results <- authConfigResult{err: err}
return
}

results <- authConfigResult{
key: k,
cfg: registry.AuthConfig{
Expand Down
8 changes: 8 additions & 0 deletions docker_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,4 +414,12 @@ func Test_getDockerAuthConfigs(t *testing.T) {
}
require.Equal(t, expected, got)
})

t.Run("none", func(t *testing.T) {
// make sure no config is set
t.Setenv("DOCKER_CONFIG", "not existing")
got, err := getDockerAuthConfigs()
require.NoError(t, err)
require.Empty(t, got)
})
}