tsh: handle missing cluster name in profile (#6257)

Cluster name can be missing in profiles created by older tsh versions.
Trying to load the client.Key without a cluster name now causes a
failure when using WithAllCerts (because ssh/db/kube certs are
per-cluster).

Also added some output to `tsh status` when no profiles can be loaded.
This commit is contained in:
Andrew Lytvynov 2021-04-02 18:00:15 +00:00 committed by GitHub
parent 4fde837c59
commit 6d200faecb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 12 deletions

View file

@ -247,6 +247,13 @@ func profileFromFile(filePath string) (*Profile, error) {
return nil, trace.Wrap(err)
}
p.Dir = filepath.Dir(filePath)
// Older versions of tsh did not always store the cluster name in the
// profile. If no cluster name is found, fallback to the name of the profile
// for backward compatibility.
if p.SiteName == "" {
p.SiteName = p.Name()
}
return p, nil
}

View file

@ -44,6 +44,7 @@ func TestProfileBasics(t *testing.T) {
ForwardedPorts: []string{"8000:example.com:8000"},
DynamicForwardedPorts: []string{"localhost:8080"},
Dir: dir,
SiteName: "example.com",
}
// verify that profile name is proxy host component

View file

@ -555,17 +555,6 @@ func readProfile(profileDir string, profileName string) (*ProfileStatus, error)
}
sort.Strings(extensions)
// Extract cluster name from the profile.
clusterName := profile.SiteName
// DELETE IN: 4.2.0.
//
// Older versions of tsh did not always store the cluster name in the
// profile. If no cluster name is found, fallback to the name of the profile
// for backward compatibility.
if clusterName == "" {
clusterName = profile.Name()
}
tlsCert, err := key.TeleportTLSCertificate()
if err != nil {
return nil, trace.Wrap(err)
@ -617,7 +606,7 @@ func readProfile(profileDir string, profileName string) (*ProfileStatus, error)
ValidUntil: validUntil,
Extensions: extensions,
Roles: roles,
Cluster: clusterName,
Cluster: profile.SiteName,
Traits: traits,
ActiveRequests: activeRequests,
KubeEnabled: profile.KubeProxyAddr != "",

View file

@ -1928,6 +1928,11 @@ func onStatus(cf *CLIConf) error {
}
func printProfiles(debug bool, profile *client.ProfileStatus, profiles []*client.ProfileStatus) {
if profile == nil && len(profiles) == 0 {
fmt.Printf("Not logged in.\n")
return
}
// Print the active profile.
if profile != nil {
printStatus(debug, profile, true)