Skip to content

Commit c883ada

Browse files
committed
Merge branch 'main' into tailnet
2 parents 5a53494 + 253e6cb commit c883ada

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+497
-48
lines changed

agent/agent.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,15 @@ func (a *agent) createCommand(ctx context.Context, rawCommand string, env []stri
578578
unixExecutablePath := strings.ReplaceAll(executablePath, "\\", "/")
579579
cmd.Env = append(cmd.Env, fmt.Sprintf(`GIT_SSH_COMMAND=%s gitssh --`, unixExecutablePath))
580580

581+
// Set SSH connection environment variables (these are also set by OpenSSH
582+
// and thus expected to be present by SSH clients). Since the agent does
583+
// networking in-memory, trying to provide accurate values here would be
584+
// nonsensical. For now, we hard code these values so that they're present.
585+
srcAddr, srcPort := "0.0.0.0", "0"
586+
dstAddr, dstPort := "0.0.0.0", "0"
587+
cmd.Env = append(cmd.Env, fmt.Sprintf("SSH_CLIENT=%s %s %s", srcAddr, srcPort, dstPort))
588+
cmd.Env = append(cmd.Env, fmt.Sprintf("SSH_CONNECTION=%s %s %s %s", srcAddr, srcPort, dstAddr, dstPort))
589+
581590
// Load environment variables passed via the agent.
582591
// These should override all variables we manually specify.
583592
for envKey, value := range metadata.EnvironmentVariables {
@@ -615,6 +624,8 @@ func (a *agent) handleSSHSession(session ssh.Session) (retErr error) {
615624
sshPty, windowSize, isPty := session.Pty()
616625
if isPty {
617626
cmd.Env = append(cmd.Env, fmt.Sprintf("TERM=%s", sshPty.Term))
627+
628+
// The pty package sets `SSH_TTY` on supported platforms.
618629
ptty, process, err := pty.Start(cmd)
619630
if err != nil {
620631
return xerrors.Errorf("start command: %w", err)

agent/agent_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,29 @@ func TestAgent(t *testing.T) {
256256
}
257257
})
258258

259+
t.Run("SSH connection env vars", func(t *testing.T) {
260+
t.Parallel()
261+
262+
// Note: the SSH_TTY environment variable should only be set for TTYs.
263+
// For some reason this test produces a TTY locally and a non-TTY in CI
264+
// so we don't test for the absence of SSH_TTY.
265+
for _, key := range []string{"SSH_CONNECTION", "SSH_CLIENT"} {
266+
key := key
267+
t.Run(key, func(t *testing.T) {
268+
t.Parallel()
269+
270+
session := setupSSHSession(t, agent.Metadata{})
271+
command := "sh -c 'echo $" + key + "'"
272+
if runtime.GOOS == "windows" {
273+
command = "cmd.exe /c echo %" + key + "%"
274+
}
275+
output, err := session.Output(command)
276+
require.NoError(t, err)
277+
require.NotEmpty(t, strings.TrimSpace(string(output)))
278+
})
279+
}
280+
})
281+
259282
t.Run("StartupScript", func(t *testing.T) {
260283
t.Parallel()
261284
tempPath := filepath.Join(t.TempDir(), "content.txt")

cli/clitest/clitest.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ import (
2121
// New creates a CLI instance with a configuration pointed to a
2222
// temporary testing directory.
2323
func New(t *testing.T, args ...string) (*cobra.Command, config.Root) {
24-
cmd := cli.Root(cli.AGPL())
24+
return NewWithSubcommands(t, cli.AGPL(), args...)
25+
}
26+
27+
func NewWithSubcommands(
28+
t *testing.T, subcommands []*cobra.Command, args ...string,
29+
) (*cobra.Command, config.Root) {
30+
cmd := cli.Root(subcommands)
2531
dir := t.TempDir()
2632
root := config.Root(dir)
2733
cmd.SetArgs(append([]string{"--global-config", dir}, args...))

cli/configssh.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func configSSH() *cobra.Command {
158158
),
159159
Args: cobra.ExactArgs(0),
160160
RunE: func(cmd *cobra.Command, _ []string) error {
161-
client, err := createClient(cmd)
161+
client, err := CreateClient(cmd)
162162
if err != nil {
163163
return err
164164
}

cli/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func create() *cobra.Command {
2727
Use: "create [name]",
2828
Short: "Create a workspace from a template",
2929
RunE: func(cmd *cobra.Command, args []string) error {
30-
client, err := createClient(cmd)
30+
client, err := CreateClient(cmd)
3131
if err != nil {
3232
return err
3333
}

cli/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func deleteWorkspace() *cobra.Command {
2828
return err
2929
}
3030

31-
client, err := createClient(cmd)
31+
client, err := CreateClient(cmd)
3232
if err != nil {
3333
return err
3434
}

cli/features.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func featuresList() *cobra.Command {
3636
Use: "list",
3737
Aliases: []string{"ls"},
3838
RunE: func(cmd *cobra.Command, args []string) error {
39-
client, err := createClient(cmd)
39+
client, err := CreateClient(cmd)
4040
if err != nil {
4141
return err
4242
}

cli/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func list() *cobra.Command {
6565
Aliases: []string{"ls"},
6666
Args: cobra.ExactArgs(0),
6767
RunE: func(cmd *cobra.Command, args []string) error {
68-
client, err := createClient(cmd)
68+
client, err := CreateClient(cmd)
6969
if err != nil {
7070
return err
7171
}

cli/logout.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func logout() *cobra.Command {
1616
Use: "logout",
1717
Short: "Remove the local authenticated session",
1818
RunE: func(cmd *cobra.Command, args []string) error {
19-
client, err := createClient(cmd)
19+
client, err := CreateClient(cmd)
2020
if err != nil {
2121
return err
2222
}

cli/parameterslist.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func parameterList() *cobra.Command {
2222
RunE: func(cmd *cobra.Command, args []string) error {
2323
scope, name := args[0], args[1]
2424

25-
client, err := createClient(cmd)
25+
client, err := CreateClient(cmd)
2626
if err != nil {
2727
return err
2828
}

0 commit comments

Comments
 (0)