Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions pkg/cmd/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,7 @@ func shellIntoExternalNode(t *terminal.Terminal, sstore ShellStore, node *nodev1
}

func runSSHWithPort(target string, port int32, identityFile string) error {
sshAgentEval := `if [ -z "$SSH_AUTH_SOCK" ]; then eval $(ssh-agent -s) > /dev/null; fi`
cmd := fmt.Sprintf("%s && ssh -i %q -o StrictHostKeyChecking=no -p %d %s", sshAgentEval, identityFile, port, target)
cmd := buildSSHWithPortCommand(target, port, identityFile)

sshCmd := exec.Command("bash", "-c", cmd) //nolint:gosec //cmd is constructed from API data
sshCmd.Stderr = os.Stderr
Expand All @@ -209,6 +208,17 @@ func runSSHWithPort(target string, port int32, identityFile string) error {
return nil
}

func buildSSHWithPortCommand(target string, port int32, identityFile string) string {
sshAgentEval := `if [ -z "$SSH_AUTH_SOCK" ]; then eval $(ssh-agent -s) > /dev/null; fi`
return fmt.Sprintf(
"%s && ssh -i %q -o StrictHostKeyChecking=no -o ServerAliveInterval=30 -o ServerAliveCountMax=3 -p %d %s",
sshAgentEval,
identityFile,
port,
target,
)
}

func runSSH(sshAlias string, host bool) error {
return runSSHWithOptions(sshAlias, host, true)
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/cmd/shell/shell_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package shell

import (
"strings"
"testing"

nodev1 "buf.build/gen/go/brevdev/devplane/protocolbuffers/go/devplaneapi/v1"
Expand All @@ -10,6 +11,19 @@ import (

func strPtr(s string) *string { return &s }

func TestBuildSSHWithPortCommandIncludesServerKeepalives(t *testing.T) {
cmd := buildSSHWithPortCommand("[email protected]", 2222, "/tmp/test key")

for _, option := range []string{
"-o ServerAliveInterval=30",
"-o ServerAliveCountMax=3",
} {
if !strings.Contains(cmd, option) {
t.Errorf("buildSSHWithPortCommand() = %q, missing %q", cmd, option)
}
}
}

// TestResolveExternalNodeSSH_BuildsCorrectInfo tests that the SSH info
// returned by ResolveNodeSSHEntry has the correct target, alias, and home path —
// the same values shellIntoExternalNode uses to build its SSH command.
Expand Down
Loading