Skip to content

Commit

Permalink
auth: refactor how the configuration file is loaded
Browse files Browse the repository at this point in the history
Combine configs instead of returning the first successful one.

Closes #827.
  • Loading branch information
fsouza committed Apr 9, 2020
1 parent f866d4d commit 4a7bf99
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
33 changes: 20 additions & 13 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,20 @@ func NewAuthConfigurationsFromFile(path string) (*AuthConfigurations, error) {
}

func cfgPaths(dockerConfigEnv string, homeEnv string) []string {
var paths []string
if dockerConfigEnv != "" {
paths = append(paths, path.Join(dockerConfigEnv, "plaintext-passwords.json"))
paths = append(paths, path.Join(dockerConfigEnv, "config.json"))
return []string{
path.Join(dockerConfigEnv, "plaintext-passwords.json"),
path.Join(dockerConfigEnv, "config.json"),
}
}
if homeEnv != "" {
paths = append(paths, path.Join(homeEnv, ".docker", "plaintext-passwords.json"))
paths = append(paths, path.Join(homeEnv, ".docker", "config.json"))
paths = append(paths, path.Join(homeEnv, ".dockercfg"))
return []string{
path.Join(homeEnv, ".docker", "plaintext-passwords.json"),
path.Join(homeEnv, ".docker", "config.json"),
path.Join(homeEnv, ".dockercfg"),
}
}
return paths
return nil
}

// NewAuthConfigurationsFromDockerCfg returns AuthConfigurations from
Expand All @@ -128,6 +131,7 @@ func NewAuthConfigurationsFromDockerCfg() (*AuthConfigurations, error) {
return nil, errors.New("no docker configuration found")
}

var result *AuthConfigurations
var auths *AuthConfigurations
var err error
for _, path := range pathsToTry {
Expand All @@ -136,14 +140,17 @@ func NewAuthConfigurationsFromDockerCfg() (*AuthConfigurations, error) {
continue
}

if !auths.isEmpty() {
return auths, nil
}
if err == nil {
return auths, nil
if result == nil {
result = auths
} else {
result.merge(*auths)
}
}
return auths, err

if result != nil {
return result, nil
}
return result, err
}

// NewAuthConfigurations returns AuthConfigurations from a JSON encoded string in the
Expand Down
2 changes: 1 addition & 1 deletion auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestAuthConfigurationSearchPath(t *testing.T) {
{"", "", []string{}},
{"", "home", []string{path.Join("home", ".docker", "plaintext-passwords.json"), path.Join("home", ".docker", "config.json"), path.Join("home", ".dockercfg")}},
{"docker_config", "", []string{path.Join("docker_config", "plaintext-passwords.json"), path.Join("docker_config", "config.json")}},
{"a", "b", []string{path.Join("a", "plaintext-passwords.json"), path.Join("a", "config.json"), path.Join("b", ".docker", "plaintext-passwords.json"), path.Join("b", ".docker", "config.json"), path.Join("b", ".dockercfg")}},
{"a", "b", []string{path.Join("a", "plaintext-passwords.json"), path.Join("a", "config.json")}},
}
for _, tt := range testData {
tt := tt
Expand Down

0 comments on commit 4a7bf99

Please sign in to comment.