From 5a12f90b4cbf675f3b7d3c0a216ce5eee911e88a Mon Sep 17 00:00:00 2001 From: Simon Ferquel Date: Mon, 15 Jun 2020 14:01:17 +0200 Subject: [PATCH] Don't filter out registries to logout from with config file contents Previously, if a registry AuthInfo was not present in the CLI config file, docker logout could not be used to ask the credential helper to forget about it. It causes problem for people working with multiple alternative config files, and it causes problems for cases like Docker Desktop w/ WSL 2, as it uses the same win32 credential helper as the Windows CLI, but a different config file, leading to bugs where I cannot logout from a registry from wsl2 if I logged in from Windows and vice-versa. Signed-off-by: Simon Ferquel (cherry picked from commit 6248f2fb6fa6c6035d7b94193a70b44ffd9b3c9e) Signed-off-by: Sebastiaan van Stijn --- cli/command/registry/logout.go | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/cli/command/registry/logout.go b/cli/command/registry/logout.go index ac84139f7e..513fef26c8 100644 --- a/cli/command/registry/logout.go +++ b/cli/command/registry/logout.go @@ -39,36 +39,29 @@ func runLogout(dockerCli command.Cli, serverAddress string) error { } var ( - loggedIn bool - regsToLogout []string + regsToLogout = []string{serverAddress} hostnameAddress = serverAddress - regsToTry = []string{serverAddress} ) if !isDefaultRegistry { hostnameAddress = registry.ConvertToHostname(serverAddress) // the tries below are kept for backward compatibility where a user could have // saved the registry in one of the following format. - regsToTry = append(regsToTry, hostnameAddress, "http://"+hostnameAddress, "https://"+hostnameAddress) - } - - // check if we're logged in based on the records in the config file - // which means it couldn't have user/pass cause they may be in the creds store - for _, s := range regsToTry { - if _, ok := dockerCli.ConfigFile().AuthConfigs[s]; ok { - loggedIn = true - regsToLogout = append(regsToLogout, s) - } - } - - if !loggedIn { - fmt.Fprintf(dockerCli.Out(), "Not logged in to %s\n", hostnameAddress) - return nil + regsToLogout = append(regsToLogout, hostnameAddress, "http://"+hostnameAddress, "https://"+hostnameAddress) } fmt.Fprintf(dockerCli.Out(), "Removing login credentials for %s\n", hostnameAddress) + errs := make(map[string]error) for _, r := range regsToLogout { if err := dockerCli.ConfigFile().GetCredentialsStore(r).Erase(r); err != nil { - fmt.Fprintf(dockerCli.Err(), "WARNING: could not erase credentials: %v\n", err) + errs[r] = err + } + } + + // if at least one removal succeeded, report success. Otherwise report errors + if len(errs) == len(regsToLogout) { + fmt.Fprintln(dockerCli.Err(), "WARNING: could not erase credentials:") + for k, v := range errs { + fmt.Fprintf(dockerCli.Err(), "%s: %s\n", k, v) } }