From f487ae5c4058bd70cab85ea773f8eff8d54df2d5 Mon Sep 17 00:00:00 2001 From: Josh Dolitsky <393494+jdolitsky@users.noreply.github.com> Date: Tue, 11 Jan 2022 09:21:15 -0600 Subject: [PATCH] Enable fallback on Docker config using custom auth file (#92) Signed-off-by: Josh Dolitsky --- pkg/auth/docker/client.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/pkg/auth/docker/client.go b/pkg/auth/docker/client.go index 8303798f..b225c89c 100644 --- a/pkg/auth/docker/client.go +++ b/pkg/auth/docker/client.go @@ -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 NewClient() + } + + 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) }