diff --git a/constants.go b/constants.go index 25be2bb6cb452..a1aec8a2e8de8 100644 --- a/constants.go +++ b/constants.go @@ -38,8 +38,8 @@ const ( // SSHTeleportUser is the current Teleport user that is logged in. SSHTeleportUser = "SSH_TELEPORT_USER" - // SSHSessionWebproxyAddr is the address the web proxy. - SSHSessionWebproxyAddr = "SSH_SESSION_WEBPROXY_ADDR" + // SSHSessionWebProxyAddr is the address the web proxy. + SSHSessionWebProxyAddr = "SSH_SESSION_WEBPROXY_ADDR" // SSHTeleportClusterName is the name of the cluster this node belongs to. SSHTeleportClusterName = "SSH_TELEPORT_CLUSTER_NAME" diff --git a/integration/integration_test.go b/integration/integration_test.go index 1b31ff8a6f252..75c6920e310fe 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -2075,15 +2075,15 @@ func enterInput(ctx context.Context, person *Terminal, command, pattern string) } } -// TestInvalidLogins validates that you can't login with invalid login or -// with invalid 'site' parameter +// testEnvironmentVariables validates that session specific environment +// variables set by Teleport are present. func testEnvironmentVariables(t *testing.T, suite *integrationTestSuite) { ctx := context.Background() tr := utils.NewTracer(utils.ThisFunction()).Start() defer tr.Stop() s := suite.newTeleport(t, nil, true) - defer s.StopAll() + t.Cleanup(func() { require.NoError(t, s.StopAll()) }) // make sure sessions set run command tc, err := s.NewClient(helpers.ClientConfig{ @@ -2094,25 +2094,27 @@ func testEnvironmentVariables(t *testing.T, suite *integrationTestSuite) { }) require.NoError(t, err) - // if SessionID is provided, it should be set in the session env vars. tc.SessionID = uuid.NewString() - cmd := []string{"printenv", sshutils.SessionEnvVar} + + // The SessionID and Web address should be set in the session env vars. + cmd := []string{"printenv", sshutils.SessionEnvVar, ";", "printenv", teleport.SSHSessionWebProxyAddr} out := &bytes.Buffer{} tc.Stdout = out tc.Stdin = nil err = tc.SSH(ctx, cmd, false /* runLocally */) - require.NoError(t, err) - require.Equal(t, tc.SessionID, strings.TrimSpace(out.String())) - - // The proxy url should be set in the session env vars. - cmd = []string{"printenv", teleport.SSHSessionWebproxyAddr} - out = &bytes.Buffer{} - tc.Stdout = out - err = tc.SSH(ctx, cmd, false /* runLocally */) + output := out.String() + require.Contains(t, output, tc.SessionID) + require.Contains(t, output, tc.WebProxyAddr) + term := NewTerminal(250) + tc.Stdout = term + tc.Stdin = strings.NewReader(strings.Join(cmd, " ") + "\r\nexit\r\n") + err = tc.SSH(ctx, nil, false /* runLocally */) require.NoError(t, err) - require.Equal(t, tc.WebProxyAddr, strings.TrimSpace(out.String())) + output = term.AllOutput() + require.Contains(t, output, tc.SessionID) + require.Contains(t, output, tc.WebProxyAddr) } // TestInvalidLogins validates that you can't login with invalid login or diff --git a/lib/client/api.go b/lib/client/api.go index 6b0457a44ca5a..e017c0be6d0b7 100644 --- a/lib/client/api.go +++ b/lib/client/api.go @@ -2672,7 +2672,7 @@ func (tc *TeleportClient) runCommandOnNodes(ctx context.Context, clt *ClusterCli func (tc *TeleportClient) newSessionEnv() map[string]string { env := map[string]string{ - teleport.SSHSessionWebproxyAddr: tc.WebProxyAddr, + teleport.SSHSessionWebProxyAddr: tc.WebProxyAddr, } if tc.SessionID != "" { env[sshutils.SessionEnvVar] = tc.SessionID diff --git a/lib/client/client.go b/lib/client/client.go index 4e6cbaa7f68ed..1821aaac99e4b 100644 --- a/lib/client/client.go +++ b/lib/client/client.go @@ -1579,11 +1579,12 @@ func NewNodeClient(ctx context.Context, sshConfig *ssh.ClientConfig, conn net.Co close(emptyCh) nc := &NodeClient{ - Client: tracessh.NewClient(sshconn, chans, emptyCh), - Namespace: apidefaults.Namespace, - TC: tc, - Tracer: tc.Tracer, - FIPSEnabled: fipsEnabled, + Client: tracessh.NewClient(sshconn, chans, emptyCh), + Namespace: apidefaults.Namespace, + TC: tc, + Tracer: tc.Tracer, + FIPSEnabled: fipsEnabled, + ProxyPublicAddr: tc.WebProxyAddr, } // Start a goroutine that will run for the duration of the client to process @@ -1617,7 +1618,9 @@ func (c *NodeClient) RunInteractiveShell(ctx context.Context, mode types.Session // Overwrite "SSH_SESSION_WEBPROXY_ADDR" with the public addr reported by the proxy. Otherwise, // this would be set to the localhost addr (tc.WebProxyAddr) used for Web UI client connections. - env[teleport.SSHSessionWebproxyAddr] = c.ProxyPublicAddr + if c.ProxyPublicAddr != "" && c.TC.WebProxyAddr != c.ProxyPublicAddr { + env[teleport.SSHSessionWebProxyAddr] = c.ProxyPublicAddr + } nodeSession, err := newSession(ctx, c, sessToJoin, env, c.TC.Stdin, c.TC.Stdout, c.TC.Stderr, c.TC.EnableEscapeSequences) if err != nil { diff --git a/tool/teleport/common/teleport.go b/tool/teleport/common/teleport.go index 7bd93d215b4ac..7276d0c88469a 100644 --- a/tool/teleport/common/teleport.go +++ b/tool/teleport/common/teleport.go @@ -547,7 +547,7 @@ func onStatus() error { sshClient := os.Getenv("SSH_CLIENT") systemUser := os.Getenv("USER") teleportUser := os.Getenv(teleport.SSHTeleportUser) - proxyAddr := os.Getenv(teleport.SSHSessionWebproxyAddr) + proxyAddr := os.Getenv(teleport.SSHSessionWebProxyAddr) clusterName := os.Getenv(teleport.SSHTeleportClusterName) hostUUID := os.Getenv(teleport.SSHTeleportHostUUID) sid := os.Getenv(teleport.SSHSessionID) diff --git a/tool/tsh/common/tsh.go b/tool/tsh/common/tsh.go index 5de728d117930..d303879644e60 100644 --- a/tool/tsh/common/tsh.go +++ b/tool/tsh/common/tsh.go @@ -4634,7 +4634,7 @@ func setEnvFlags(cf *CLIConf, getEnv envGetter) { // When using Headless, check for missing proxy/user/cluster values from the teleport session env variables. if cf.Headless || cf.AuthConnector == constants.HeadlessConnector { if cf.Proxy == "" { - cf.Proxy = getEnv(teleport.SSHSessionWebproxyAddr) + cf.Proxy = getEnv(teleport.SSHSessionWebProxyAddr) } if cf.Username == "" { cf.Username = getEnv(teleport.SSHTeleportUser) diff --git a/tool/tsh/common/tsh_test.go b/tool/tsh/common/tsh_test.go index c351a16c2ad0e..7d96259a80c5c 100644 --- a/tool/tsh/common/tsh_test.go +++ b/tool/tsh/common/tsh_test.go @@ -2495,7 +2495,7 @@ func TestEnvFlags(t *testing.T) { Headless: false, }, envMap: map[string]string{ - teleport.SSHSessionWebproxyAddr: "proxy.example.com", + teleport.SSHSessionWebProxyAddr: "proxy.example.com", teleport.SSHTeleportUser: "alice", teleport.SSHTeleportClusterName: "root-cluster", }, @@ -2508,7 +2508,7 @@ func TestEnvFlags(t *testing.T) { Headless: true, }, envMap: map[string]string{ - teleport.SSHSessionWebproxyAddr: "proxy.example.com", + teleport.SSHSessionWebProxyAddr: "proxy.example.com", teleport.SSHTeleportUser: "alice", teleport.SSHTeleportClusterName: "root-cluster", }, @@ -2524,7 +2524,7 @@ func TestEnvFlags(t *testing.T) { AuthConnector: constants.HeadlessConnector, }, envMap: map[string]string{ - teleport.SSHSessionWebproxyAddr: "proxy.example.com", + teleport.SSHSessionWebProxyAddr: "proxy.example.com", teleport.SSHTeleportUser: "alice", teleport.SSHTeleportClusterName: "root-cluster", }, @@ -2543,7 +2543,7 @@ func TestEnvFlags(t *testing.T) { SiteName: "root-cluster", }, envMap: map[string]string{ - teleport.SSHSessionWebproxyAddr: "other.example.com", + teleport.SSHSessionWebProxyAddr: "other.example.com", teleport.SSHTeleportUser: "bob", teleport.SSHTeleportClusterName: "leaf-cluster", },