Skip to content

Commit

Permalink
Enable fallback on Docker config using custom auth file
Browse files Browse the repository at this point in the history
Signed-off-by: Josh Dolitsky <josh@dolit.ski>
  • Loading branch information
jdolitsky committed Jan 11, 2022
1 parent a05ce7f commit 0c51bc9
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions pkg/auth/docker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,39 @@ func NewClient(configPaths ...string) (auth.Client, error) {
}, nil
}

// NewClientWithDockerFallback creates a new auth client
// which falls back on Docker's default config path.
// This allows support for ~/.docker/config.json as a fallback,
// as well as support for the DOCKER_CONFIG environment variable.
func NewClientWithDockerFallback(configPaths ...string) (auth.Client, error) {
if len(configPaths) == 0 {
return nil, errors.New("must provide at least one config path")
}

var configs []*configfile.ConfigFile
for _, path := range configPaths {
cfg, err := loadConfigFile(path)
if err != nil {
return nil, errors.Wrap(err, path)
}
configs = append(configs, cfg)
}

// Add the Docker default config last
dockerFallbackCfg, err := config.Load(config.Dir())
if err != nil {
return nil, err
}
if !dockerFallbackCfg.ContainsAuth() {
dockerFallbackCfg.CredentialsStore = credentials.DetectDefaultStore(dockerFallbackCfg.CredentialsStore)
}
configs = append(configs, dockerFallbackCfg)

return &Client{
configs: configs,
}, nil
}

func (c *Client) primaryCredentialsStore(hostname string) credentials.Store {
return c.configs[0].GetCredentialsStore(hostname)
}
Expand Down

0 comments on commit 0c51bc9

Please sign in to comment.