From 8ec6154cb22f70df14c9e3092969a4de30e7258e Mon Sep 17 00:00:00 2001 From: Tim Ross Date: Thu, 25 May 2023 11:45:48 -0400 Subject: [PATCH] Fetch ClusterAlerts a single time during login `onLogin` was inadvertently retrieving ClusterAlerts twice by explicitly retrieving them and by calling `onStatus`, which also gets the alerts so they are shown when `tsh status` is invoked. The portion of `onStatus` which prints the profile has been separated into `outputProfiles` so that `onLogin` may call that directly instead of `onStatus`. --- tool/tsh/tsh.go | 96 ++++++++++++++++++++++++++----------------------- 1 file changed, 52 insertions(+), 44 deletions(-) diff --git a/tool/tsh/tsh.go b/tool/tsh/tsh.go index 9650aab2601d3..1d2f834dc592f 100644 --- a/tool/tsh/tsh.go +++ b/tool/tsh/tsh.go @@ -1589,11 +1589,8 @@ func onLogin(cf *CLIConf) error { if err := updateKubeConfigOnLogin(cf, tc, ""); err != nil { return trace.Wrap(err) } - env := getTshEnv() - active, others := makeAllProfileInfo(profile, profiles, env) - printProfiles(cf.Debug, active, others, env, cf.Verbose) - return nil + return trace.Wrap(printProfiles(cf, profile, profiles)) // if the proxy names match but nothing else is specified; show motd and update active profile and kube configs case host(cf.Proxy) == host(profile.ProxyURL.Host) && @@ -1797,8 +1794,13 @@ func onLogin(cf *CLIConf) error { webProxyHost, _ := tc.WebProxyHostPort() cf.Proxy = webProxyHost + profile, profiles, err = cf.FullProfileStatus() + if err != nil { + return trace.Wrap(err) + } + // Print status to show information of the logged in user. - if err := onStatus(cf); err != nil { + if err := printProfiles(cf, profile, profiles); err != nil { return trace.Wrap(err) } @@ -3836,6 +3838,49 @@ func printStatus(debug bool, p *profileInfo, env map[string]string, isActive boo fmt.Printf("\n") } +// printProfiles displays the provided profile information +// to the user. +func printProfiles(cf *CLIConf, profile *client.ProfileStatus, profiles []*client.ProfileStatus) error { + env := getTshEnv() + active, others := makeAllProfileInfo(profile, profiles, env) + + format := strings.ToLower(cf.Format) + switch format { + case teleport.JSON, teleport.YAML: + out, err := serializeProfiles(active, others, env, format) + if err != nil { + return trace.Wrap(err) + } + fmt.Println(out) + default: + if profile == nil && len(profiles) == 0 { + return nil + } + + // Print the active profile. + if profile != nil { + printStatus(cf.Debug, active, env, true) + } + + // Print all other profiles. + for _, p := range others { + printStatus(cf.Debug, p, env, false) + } + + // Print relevant active env vars, if they are set. + if cf.Verbose { + if len(env) > 0 { + fmt.Println("Active Environment:") + } + for k, v := range env { + fmt.Printf("\t%s=%s\n", k, v) + } + } + } + + return nil +} + // onStatus command shows which proxy the user is logged into and metadata // about the certificate. func onStatus(cf *CLIConf) error { @@ -3851,19 +3896,8 @@ func onStatus(cf *CLIConf) error { return trace.Wrap(err) } - env := getTshEnv() - active, others := makeAllProfileInfo(profile, profiles, env) - - format := strings.ToLower(cf.Format) - switch format { - case teleport.JSON, teleport.YAML: - out, err := serializeProfiles(active, others, env, format) - if err != nil { - return trace.Wrap(err) - } - fmt.Println(out) - default: - printProfiles(cf.Debug, active, others, env, cf.Verbose) + if err := printProfiles(cf, profile, profiles); err != nil { + return trace.Wrap(err) } if profile == nil { @@ -4046,32 +4080,6 @@ func getTshEnv() map[string]string { return env } -func printProfiles(debug bool, profile *profileInfo, profiles []*profileInfo, env map[string]string, verbose bool) { - if profile == nil && len(profiles) == 0 { - return - } - - // Print the active profile. - if profile != nil { - printStatus(debug, profile, env, true) - } - - // Print all other profiles. - for _, p := range profiles { - printStatus(debug, p, env, false) - } - - // Print relevant active env vars, if they are set. - if verbose { - if len(env) > 0 { - fmt.Println("Active Environment:") - } - for k, v := range env { - fmt.Printf("\t%s=%s\n", k, v) - } - } -} - // host is a utility function that extracts // host from the host:port pair, in case of any error // returns the original value