From de40c2b1723566865c9679cc770add2388ca849d Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 7 Jan 2021 13:51:21 +0100 Subject: [PATCH] Fix panic when failing to get DefaultAuthConfig Commit f32731f9020f12ae2600a3f4c90e668565220cb6 fixed a potential panic when an error was returned while trying to get existing credentials. However, other code paths currently use the result of `GetDefaultAuthConfig()` even in an error condition; this resulted in a panic, because a `nil` was returned. Signed-off-by: Sebastiaan van Stijn (cherry picked from commit c2820a7e3b38539a1f845bfafe3bf9eb36e6447a) Signed-off-by: Sebastiaan van Stijn --- cli/command/registry.go | 3 +++ cli/command/registry/login.go | 7 ++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/cli/command/registry.go b/cli/command/registry.go index 68cb43144d..e6311c8bfb 100644 --- a/cli/command/registry.go +++ b/cli/command/registry.go @@ -63,6 +63,9 @@ func RegistryAuthenticationPrivilegedFunc(cli Cli, index *registrytypes.IndexInf indexServer := registry.GetAuthConfigKey(index) isDefaultRegistry := indexServer == ElectAuthServer(context.Background(), cli) authConfig, err := GetDefaultAuthConfig(cli, true, indexServer, isDefaultRegistry) + if authConfig == nil { + authConfig = &types.AuthConfig{} + } if err != nil { fmt.Fprintf(cli.Err(), "Unable to retrieve stored credentials for %s, error: %s.\n", indexServer, err) } diff --git a/cli/command/registry/login.go b/cli/command/registry/login.go index f6cb579e75..61dd90c33f 100644 --- a/cli/command/registry/login.go +++ b/cli/command/registry/login.go @@ -111,11 +111,12 @@ func runLogin(dockerCli command.Cli, opts loginOptions) error { //nolint: gocycl serverAddress = authServer } - var err error - var authConfig *types.AuthConfig var response registrytypes.AuthenticateOKBody isDefaultRegistry := serverAddress == authServer - authConfig, err = command.GetDefaultAuthConfig(dockerCli, opts.user == "" && opts.password == "", serverAddress, isDefaultRegistry) + authConfig, err := command.GetDefaultAuthConfig(dockerCli, opts.user == "" && opts.password == "", serverAddress, isDefaultRegistry) + if authConfig == nil { + authConfig = &types.AuthConfig{} + } if err == nil && authConfig.Username != "" && authConfig.Password != "" { response, err = loginWithCredStoreCreds(ctx, dockerCli, authConfig) }