parse ssh args and command

This commit is contained in:
Jose Garcia 2021-09-16 10:32:27 -04:00
parent 514448dde8
commit 8a0f8b6d1c
2 changed files with 117 additions and 10 deletions

View file

@ -12,7 +12,7 @@ import (
// port-forwarding session. It runs until the shell is terminated
// (including by cancellation of the context).
func Shell(ctx context.Context, log logger, sshArgs []string, port int, destination string, usingCustomPort bool) error {
cmd, connArgs := newSSHCommand(ctx, port, destination, "")
cmd, connArgs := newSSHCommand(ctx, port, destination, sshArgs)
if usingCustomPort {
log.Println("Connection Details: ssh " + destination + " " + strings.Join(connArgs, " "))
@ -23,23 +23,21 @@ func Shell(ctx context.Context, log logger, sshArgs []string, port int, destinat
// NewRemoteCommand returns an exec.Cmd that will securely run a shell
// command on the remote machine.
func NewRemoteCommand(ctx context.Context, tunnelPort int, destination, command string) *exec.Cmd {
cmd, _ := newSSHCommand(ctx, tunnelPort, destination, command)
func NewRemoteCommand(ctx context.Context, tunnelPort int, destination string, sshArgs ...string) *exec.Cmd {
cmd, _ := newSSHCommand(ctx, tunnelPort, destination, sshArgs)
return cmd
}
// newSSHCommand populates an exec.Cmd to run a command (or if blank,
// an interactive shell) over ssh.
func newSSHCommand(ctx context.Context, port int, dst, command string) (*exec.Cmd, []string) {
func newSSHCommand(ctx context.Context, port int, dst string, cmdArgs []string) (*exec.Cmd, []string) {
connArgs := []string{"-p", strconv.Itoa(port), "-o", "NoHostAuthenticationForLocalhost=yes"}
cmdArgs := []string{dst, "-C"} // Always use Compression
if command == "" {
// if we are in a shell send X11 and X11Trust
cmdArgs = append(cmdArgs, "-X", "-Y")
}
cmdArgs, command := parseSSHArgs(cmdArgs)
cmdArgs = append(cmdArgs, connArgs...)
cmdArgs = append(cmdArgs, "-C") // Compression
cmdArgs = append(cmdArgs, dst) // user@host
if command != "" {
cmdArgs = append(cmdArgs, command)
}
@ -51,3 +49,48 @@ func newSSHCommand(ctx context.Context, port int, dst, command string) (*exec.Cm
return cmd, connArgs
}
var sshArgumentFlags = map[string]bool{
"-b": true,
"-c": true,
"-D": true,
"-e": true,
"-F": true,
"-I": true,
"-i": true,
"-L": true,
"-l": true,
"-m": true,
"-O": true,
"-o": true,
"-p": true,
"-R": true,
"-S": true,
"-W": true,
"-w": true,
}
func parseSSHArgs(sshArgs []string) ([]string, string) {
var (
cmdArgs []string
command []string
flagArgument bool
)
for _, arg := range sshArgs {
switch {
case strings.HasPrefix(arg, "-"):
cmdArgs = append(cmdArgs, arg)
if _, ok := sshArgumentFlags[arg]; ok {
flagArgument = true
}
case flagArgument:
cmdArgs = append(cmdArgs, arg)
flagArgument = false
default:
command = append(command, arg)
}
}
return cmdArgs, strings.Join(command, " ")
}

View file

@ -0,0 +1,64 @@
package codespaces
import "testing"
func TestParseSSHArgs(t *testing.T) {
type testCase struct {
Args []string
ParsedArgs []string
Command string
}
testCases := []testCase{
{
Args: []string{"-X", "-Y"},
ParsedArgs: []string{"-X", "-Y"},
Command: "",
},
{
Args: []string{"-X", "-Y", "-o", "someoption=test"},
ParsedArgs: []string{"-X", "-Y", "-o", "someoption=test"},
Command: "",
},
{
Args: []string{"-X", "-Y", "-o", "someoption=test", "somecommand"},
ParsedArgs: []string{"-X", "-Y", "-o", "someoption=test"},
Command: "somecommand",
},
{
Args: []string{"-X", "-Y", "-o", "someoption=test", "echo", "test"},
ParsedArgs: []string{"-X", "-Y", "-o", "someoption=test"},
Command: "echo test",
},
{
Args: []string{"somecommand"},
ParsedArgs: []string{},
Command: "somecommand",
},
{
Args: []string{"echo", "test"},
ParsedArgs: []string{},
Command: "echo test",
},
{
Args: []string{"-v", "echo", "hello", "world"},
ParsedArgs: []string{"-v"},
Command: "echo hello world",
},
}
for _, tcase := range testCases {
args, command := parseSSHArgs(tcase.Args)
if len(args) != len(tcase.ParsedArgs) {
t.Fatalf("args do not match length of expected args. %#v, got '%d', expected: '%d'", tcase, len(args), len(tcase.ParsedArgs))
}
for i, arg := range args {
if arg != tcase.ParsedArgs[i] {
t.Fatalf("arg does not match expected parsed arg. %v, got '%s', expected: '%s'", tcase, arg, tcase.ParsedArgs[i])
}
}
if command != tcase.Command {
t.Fatalf("command does not match expected command. %v, got: '%s', expected: '%s'", tcase, command, tcase.Command)
}
}
}