From c990920026a88fe89637c888374e64772995025c Mon Sep 17 00:00:00 2001 From: Alex McGrath Date: Mon, 28 Mar 2022 14:28:20 +0000 Subject: [PATCH] tctl: respect TELEPORT_HOME variable when reading profiles --- api/types/constants.go | 4 ++++ lib/service/cfg.go | 4 ++++ tool/tctl/common/tctl.go | 7 ++++++- tool/tsh/kube.go | 2 +- tool/tsh/tsh.go | 3 +-- tool/tsh/tsh_test.go | 4 ++-- 6 files changed, 18 insertions(+), 6 deletions(-) diff --git a/api/types/constants.go b/api/types/constants.go index 52a4d64210e98..7b88f5c5b5b5b 100644 --- a/api/types/constants.go +++ b/api/types/constants.go @@ -33,6 +33,10 @@ const ( // True holds "true" string value True = "true" + // HomeEnvVar specifies the home location for tsh configuration + // and data + HomeEnvVar = "TELEPORT_HOME" + // KindNamespace is a namespace KindNamespace = "namespace" diff --git a/lib/service/cfg.go b/lib/service/cfg.go index d781b911e9289..3cd7955fb55dc 100644 --- a/lib/service/cfg.go +++ b/lib/service/cfg.go @@ -255,6 +255,10 @@ type Config struct { // ConnectFailureC is a channel to notify of failures to connect to auth (used in tests). ConnectFailureC chan time.Duration + + // TeleportHome is the path to tsh configuration and data, used + // for loading profiles when TELEPORT_HOME is set + TeleportHome string } // ApplyToken assigns a given token to all internal services but only if token diff --git a/tool/tctl/common/tctl.go b/tool/tctl/common/tctl.go index ea7c8855eeb47..b75b6efa3b54b 100644 --- a/tool/tctl/common/tctl.go +++ b/tool/tctl/common/tctl.go @@ -149,6 +149,11 @@ func Run(commands []CLICommand) { return } + cfg.TeleportHome = os.Getenv(types.HomeEnvVar) + if cfg.TeleportHome != "" { + cfg.TeleportHome = filepath.Clean(cfg.TeleportHome) + } + // configure all commands with Teleport configuration (they share 'cfg') clientConfig, err := applyConfig(&ccf, cfg) if err != nil { @@ -337,7 +342,7 @@ func loadConfigFromProfile(ccf *GlobalCLIFlags, cfg *service.Config) (*authclien log.WithFields(log.Fields{"proxy": profile.ProxyURL.String(), "user": profile.Username}).Debugf("Found active profile.") c := client.MakeDefaultConfig() - if err := c.LoadProfile("", proxyAddr); err != nil { + if err := c.LoadProfile(cfg.TeleportHome, proxyAddr); err != nil { return nil, trace.Wrap(err) } keyStore, err := client.NewFSLocalKeyStore(c.KeysDir) diff --git a/tool/tsh/kube.go b/tool/tsh/kube.go index 59d0a583dc70c..d482b552b7f4b 100644 --- a/tool/tsh/kube.go +++ b/tool/tsh/kube.go @@ -848,7 +848,7 @@ func buildKubeConfigUpdate(cf *CLIConf, kubeStatus *kubernetesStatus) (*kubeconf } if cf.HomePath != "" { - v.Exec.Env[homeEnvVar] = cf.HomePath + v.Exec.Env[types.HomeEnvVar] = cf.HomePath } // Only switch the current context if kube-cluster is explicitly set on the command line. diff --git a/tool/tsh/tsh.go b/tool/tsh/tsh.go index 572c9890ea859..7692f755c4e37 100644 --- a/tool/tsh/tsh.go +++ b/tool/tsh/tsh.go @@ -342,7 +342,6 @@ const ( loginEnvVar = "TELEPORT_LOGIN" bindAddrEnvVar = "TELEPORT_LOGIN_BIND_ADDR" proxyEnvVar = "TELEPORT_PROXY" - homeEnvVar = "TELEPORT_HOME" // TELEPORT_SITE uses the older deprecated "site" terminology to refer to a // cluster. All new code should use TELEPORT_CLUSTER instead. siteEnvVar = "TELEPORT_SITE" @@ -2565,7 +2564,7 @@ func setSiteNameFromEnv(cf *CLIConf, fn envGetter) { // setTeleportHomeFromEnv sets home directory from environment if configured. func setTeleportHomeFromEnv(cf *CLIConf, fn envGetter) { - if homeDir := fn(homeEnvVar); homeDir != "" { + if homeDir := fn(types.HomeEnvVar); homeDir != "" { cf.HomePath = path.Clean(homeDir) } } diff --git a/tool/tsh/tsh_test.go b/tool/tsh/tsh_test.go index 7ad4a1265623d..96a6a32ed68b8 100644 --- a/tool/tsh/tsh_test.go +++ b/tool/tsh/tsh_test.go @@ -887,7 +887,7 @@ func TestEnvFlags(t *testing.T) { })) t.Run("TELEPORT_HOME set", testEnvFlag(testCase{ envMap: map[string]string{ - homeEnvVar: "teleport-data/", + types.HomeEnvVar: "teleport-data/", }, outCLIConf: CLIConf{ HomePath: "teleport-data", @@ -898,7 +898,7 @@ func TestEnvFlags(t *testing.T) { HomePath: "teleport-data", }, envMap: map[string]string{ - homeEnvVar: "teleport-data/", + types.HomeEnvVar: "teleport-data/", }, outCLIConf: CLIConf{ HomePath: "teleport-data",