From 39d942dcaa8523411ab21e07d6d0df3bb9d759e6 Mon Sep 17 00:00:00 2001 From: Luca Di Maio Date: Thu, 31 Aug 2023 12:37:10 +0000 Subject: [PATCH 1/6] fix: correct spacing in logs Signed-off-by: Luca Di Maio --- pkg/ssh/ssh.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/ssh/ssh.go b/pkg/ssh/ssh.go index e84f857..f513548 100644 --- a/pkg/ssh/ssh.go +++ b/pkg/ssh/ssh.go @@ -36,7 +36,7 @@ func NewProvider(logs log.Logger) (*SSHProvider, error) { func returnSSHError(provider *SSHProvider, command string) error { sshError := "Please make sure you have configured the correct SSH host\nand the following command can be executed on your system:\n" - return fmt.Errorf(sshError + "ssh" + strings.Join(getSSHCommand(provider), " ") + " " + command) + return fmt.Errorf(sshError + "ssh " + strings.Join(getSSHCommand(provider), " ") + " " + command) } func getSSHCommand(provider *SSHProvider) []string { From a475913047564848e156c104d5afed41a018b46c Mon Sep 17 00:00:00 2001 From: Luca Di Maio Date: Thu, 31 Aug 2023 12:37:26 +0000 Subject: [PATCH 2/6] fix: properly set EXTRA_FLAGS when passed Signed-off-by: Luca Di Maio --- pkg/options/options.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/options/options.go b/pkg/options/options.go index 58c0cfa..7ac968a 100644 --- a/pkg/options/options.go +++ b/pkg/options/options.go @@ -47,6 +47,11 @@ func FromEnv() (*Options, error) { return nil, err } + retOptions.ExtraFlags, err = fromEnvOrError(EXTRA_FLAGS) + if err != nil { + return nil, err + } + retOptions.Host, err = fromEnvOrError(HOST) if err != nil { return nil, err From cf6b8c880a882a1a76454d95fcafe47428eeb2cf Mon Sep 17 00:00:00 2001 From: Luca Di Maio Date: Thu, 31 Aug 2023 12:37:57 +0000 Subject: [PATCH 3/6] fix: remove dead code Signed-off-by: Luca Di Maio --- pkg/options/options.go | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/pkg/options/options.go b/pkg/options/options.go index 7ac968a..dabe2f2 100644 --- a/pkg/options/options.go +++ b/pkg/options/options.go @@ -22,16 +22,6 @@ type Options struct { ExtraFlags string } -func ConfigFromEnv() (Options, error) { - return Options{ - DockerPath: os.Getenv(DOCKER_PATH), - AgentPath: os.Getenv(AGENT_PATH), - Host: os.Getenv(HOST), - Port: os.Getenv(PORT), - ExtraFlags: os.Getenv(EXTRA_FLAGS), - }, nil -} - func FromEnv() (*Options, error) { retOptions := &Options{} From e142b194ee5688fc3e769275902cd47cdaedf5b5 Mon Sep 17 00:00:00 2001 From: Luca Di Maio Date: Thu, 31 Aug 2023 12:58:56 +0000 Subject: [PATCH 4/6] fix: use proper shell splitting for EXTRA_FLAGS Signed-off-by: Luca Di Maio --- go.mod | 2 +- pkg/ssh/ssh.go | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index e7c7671..5a6f87b 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/loft-sh/devpod-provider-ssh go 1.20 require ( + github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 github.com/loft-sh/devpod v0.1.10 github.com/onsi/ginkgo/v2 v2.9.1 github.com/onsi/gomega v1.27.4 @@ -43,7 +44,6 @@ require ( github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213 // indirect - github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect github.com/klauspost/compress v1.15.12 // indirect github.com/loft-sh/utils v0.0.16 // indirect github.com/mattn/go-colorable v0.1.13 // indirect diff --git a/pkg/ssh/ssh.go b/pkg/ssh/ssh.go index f513548..2744b0b 100644 --- a/pkg/ssh/ssh.go +++ b/pkg/ssh/ssh.go @@ -9,6 +9,7 @@ import ( "path" "strings" + "github.com/kballard/go-shellquote" "github.com/loft-sh/devpod-provider-ssh/pkg/options" "github.com/loft-sh/devpod/pkg/log" ) @@ -47,7 +48,10 @@ func getSSHCommand(provider *SSHProvider) []string { } if provider.Config.ExtraFlags != "" { - result = append(result, strings.Split(provider.Config.ExtraFlags, " ")...) + flags, err := shellquote.Split(provider.Config.ExtraFlags) + if err == nil { + result = append(result, flags...) + } } result = append(result, provider.Config.Host) From bbc1600bcf2f6b8066ac0afedd9d98e8499719fc Mon Sep 17 00:00:00 2001 From: Luca Di Maio Date: Thu, 31 Aug 2023 13:14:28 +0000 Subject: [PATCH 5/6] fix: EXTRA_FLAGS is not mandatory Signed-off-by: Luca Di Maio --- pkg/options/options.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pkg/options/options.go b/pkg/options/options.go index dabe2f2..a4574a7 100644 --- a/pkg/options/options.go +++ b/pkg/options/options.go @@ -37,10 +37,7 @@ func FromEnv() (*Options, error) { return nil, err } - retOptions.ExtraFlags, err = fromEnvOrError(EXTRA_FLAGS) - if err != nil { - return nil, err - } + retOptions.ExtraFlags = os.Getenv(EXTRA_FLAGS) retOptions.Host, err = fromEnvOrError(HOST) if err != nil { From 6f706e2ec7f527aefcd71490913d63dad2327a35 Mon Sep 17 00:00:00 2001 From: Luca Di Maio Date: Fri, 1 Sep 2023 13:29:46 +0000 Subject: [PATCH 6/6] fix: handle errors in shell quoting split Signed-off-by: Luca Di Maio --- pkg/ssh/ssh.go | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/pkg/ssh/ssh.go b/pkg/ssh/ssh.go index 2744b0b..32a18a7 100644 --- a/pkg/ssh/ssh.go +++ b/pkg/ssh/ssh.go @@ -37,10 +37,16 @@ func NewProvider(logs log.Logger) (*SSHProvider, error) { func returnSSHError(provider *SSHProvider, command string) error { sshError := "Please make sure you have configured the correct SSH host\nand the following command can be executed on your system:\n" - return fmt.Errorf(sshError + "ssh " + strings.Join(getSSHCommand(provider), " ") + " " + command) + + sshcmd, err := getSSHCommand(provider) + if err != nil { + return err + } + + return fmt.Errorf(sshError + "ssh " + strings.Join(sshcmd, " ") + " " + command) } -func getSSHCommand(provider *SSHProvider) []string { +func getSSHCommand(provider *SSHProvider) ([]string, error) { result := []string{"-oStrictHostKeyChecking=no", "-oBatchMode=yes"} if provider.Config.Port != "22" { @@ -49,17 +55,23 @@ func getSSHCommand(provider *SSHProvider) []string { if provider.Config.ExtraFlags != "" { flags, err := shellquote.Split(provider.Config.ExtraFlags) - if err == nil { - result = append(result, flags...) + if err != nil { + return nil, fmt.Errorf("error managing EXTRA_ARGS, %v", err) } + + result = append(result, flags...) } result = append(result, provider.Config.Host) - return result + return result, nil } func execSSHCommand(provider *SSHProvider, command string, output io.Writer) error { - commandToRun := getSSHCommand(provider) + commandToRun, err := getSSHCommand(provider) + if err != nil { + return err + } + commandToRun = append(commandToRun, command) cmd := exec.Command("ssh", commandToRun...)