From 71fde20e17ebcc6180d04e6cd44dd333ca437eaf Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 13 Apr 2023 23:10:53 +0200 Subject: [PATCH 1/3] cli/command/system: prettyPrintServerInfo: simplify username Starting with b4ca1c7368daeead400fcc1b8f2d61951a0d9d1e, docker login no longer depends on info.IndexServerAddress to determine the default registry. The prettyPrintServerInfo() still depended on this information, which could potentially show the wrong information. This patch changes it to also depend on the same information as docker login now does. Signed-off-by: Sebastiaan van Stijn --- cli/command/system/info.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/cli/command/system/info.go b/cli/command/system/info.go index 1d675c75f0..a8b4b95a24 100644 --- a/cli/command/system/info.go +++ b/cli/command/system/info.go @@ -19,6 +19,7 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/swarm" "github.com/docker/docker/api/types/versions" + "github.com/docker/docker/registry" "github.com/docker/go-units" "github.com/spf13/cobra" ) @@ -319,12 +320,8 @@ func prettyPrintServerInfo(dockerCli command.Cli, info types.Info) []error { fprintlnNonEmpty(dockerCli.Out(), " HTTPS Proxy:", info.HTTPSProxy) fprintlnNonEmpty(dockerCli.Out(), " No Proxy:", info.NoProxy) - if info.IndexServerAddress != "" { - u := dockerCli.ConfigFile().AuthConfigs[info.IndexServerAddress].Username - if len(u) > 0 { - fmt.Fprintln(dockerCli.Out(), " Username:", u) - } - } + u := dockerCli.ConfigFile().AuthConfigs[registry.IndexServer].Username + fprintlnNonEmpty(dockerCli.Out(), " Username:", u) if len(info.Labels) > 0 { fmt.Fprintln(dockerCli.Out(), " Labels:") From be307c579244fe7a301719d2a4faadc7f7c90d3d Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 14 Apr 2023 00:25:08 +0200 Subject: [PATCH 2/3] cli/command/system: prettyPrintServerInfo: move out collecting username Make this function only _print_ the info we have, and not read the username from the credential-store. This patch adds a Username field to the (local) `info` type, and sets it when needed, so that prettyPrintServerInfo only has to format and print the information, instead of calling out to the credential-store. Signed-off-by: Sebastiaan van Stijn --- cli/command/system/info.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/cli/command/system/info.go b/cli/command/system/info.go index a8b4b95a24..c4daa4220b 100644 --- a/cli/command/system/info.go +++ b/cli/command/system/info.go @@ -42,6 +42,7 @@ type info struct { // object. *types.Info `json:",omitempty"` ServerErrors []string `json:",omitempty"` + UserName string `json:"-"` ClientInfo *clientInfo `json:",omitempty"` ClientErrors []string `json:",omitempty"` @@ -113,6 +114,7 @@ func runInfo(cmd *cobra.Command, dockerCli command.Cli, opts *infoOptions) error } if opts.format == "" { + info.UserName = dockerCli.ConfigFile().AuthConfigs[registry.IndexServer].Username return prettyPrintInfo(dockerCli, info) } return formatInfo(dockerCli, info, opts.format) @@ -174,7 +176,7 @@ func prettyPrintInfo(dockerCli command.Cli, info info) error { fmt.Fprintln(dockerCli.Out()) fmt.Fprintln(dockerCli.Out(), "Server:") if info.Info != nil { - for _, err := range prettyPrintServerInfo(dockerCli, *info.Info) { + for _, err := range prettyPrintServerInfo(dockerCli, &info) { info.ServerErrors = append(info.ServerErrors, err.Error()) } } @@ -212,7 +214,7 @@ func prettyPrintClientInfo(dockerCli command.Cli, info clientInfo) { } //nolint:gocyclo -func prettyPrintServerInfo(dockerCli command.Cli, info types.Info) []error { +func prettyPrintServerInfo(dockerCli command.Cli, info *info) []error { var errs []error fmt.Fprintln(dockerCli.Out(), " Containers:", info.Containers) @@ -247,7 +249,7 @@ func prettyPrintServerInfo(dockerCli command.Cli, info types.Info) []error { fmt.Fprintln(dockerCli.Out(), " Log:", strings.Join(info.Plugins.Log, " ")) fmt.Fprintln(dockerCli.Out(), " Swarm:", info.Swarm.LocalNodeState) - printSwarmInfo(dockerCli, info) + printSwarmInfo(dockerCli, *info.Info) if len(info.Runtimes) > 0 { fmt.Fprint(dockerCli.Out(), " Runtimes:") @@ -319,10 +321,7 @@ func prettyPrintServerInfo(dockerCli command.Cli, info types.Info) []error { fprintlnNonEmpty(dockerCli.Out(), " HTTP Proxy:", info.HTTPProxy) fprintlnNonEmpty(dockerCli.Out(), " HTTPS Proxy:", info.HTTPSProxy) fprintlnNonEmpty(dockerCli.Out(), " No Proxy:", info.NoProxy) - - u := dockerCli.ConfigFile().AuthConfigs[registry.IndexServer].Username - fprintlnNonEmpty(dockerCli.Out(), " Username:", u) - + fprintlnNonEmpty(dockerCli.Out(), " Username:", info.UserName) if len(info.Labels) > 0 { fmt.Fprintln(dockerCli.Out(), " Labels:") for _, lbl := range info.Labels { @@ -367,7 +366,7 @@ func prettyPrintServerInfo(dockerCli command.Cli, info types.Info) []error { fmt.Fprint(dockerCli.Out(), "\n") - printServerWarnings(dockerCli, info) + printServerWarnings(dockerCli, *info.Info) return errs } From da3871fd8ea965b003bbfa6937646a51c2c86a9e Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 14 Apr 2023 00:49:49 +0200 Subject: [PATCH 3/3] cli/command/system: printServerWarnings: use client API version from info Set the client's API version that's used in the info, instead of requesting it as part of printing. Signed-off-by: Sebastiaan van Stijn --- cli/command/system/info.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/cli/command/system/info.go b/cli/command/system/info.go index c4daa4220b..b95db351ac 100644 --- a/cli/command/system/info.go +++ b/cli/command/system/info.go @@ -115,6 +115,7 @@ func runInfo(cmd *cobra.Command, dockerCli command.Cli, opts *infoOptions) error if opts.format == "" { info.UserName = dockerCli.ConfigFile().AuthConfigs[registry.IndexServer].Username + info.ClientInfo.APIVersion = dockerCli.CurrentVersion() return prettyPrintInfo(dockerCli, info) } return formatInfo(dockerCli, info, opts.format) @@ -366,7 +367,7 @@ func prettyPrintServerInfo(dockerCli command.Cli, info *info) []error { fmt.Fprint(dockerCli.Out(), "\n") - printServerWarnings(dockerCli, *info.Info) + printServerWarnings(dockerCli, info) return errs } @@ -440,16 +441,16 @@ func printSwarmInfo(dockerCli command.Cli, info types.Info) { } } -func printServerWarnings(dockerCli command.Cli, info types.Info) { - if versions.LessThan(dockerCli.Client().ClientVersion(), "1.42") { - printSecurityOptionsWarnings(dockerCli, info) +func printServerWarnings(dockerCli command.Cli, info *info) { + if versions.LessThan(info.ClientInfo.APIVersion, "1.42") { + printSecurityOptionsWarnings(dockerCli, *info.Info) } if len(info.Warnings) > 0 { fmt.Fprintln(dockerCli.Err(), strings.Join(info.Warnings, "\n")) return } // daemon didn't return warnings. Fallback to old behavior - printServerWarningsLegacy(dockerCli, info) + printServerWarningsLegacy(dockerCli, *info.Info) } // printSecurityOptionsWarnings prints warnings based on the security options