From f648b5ad0aa9efe7eea8db612e7ffbefaeca9729 Mon Sep 17 00:00:00 2001 From: Oliver Bristow Date: Mon, 14 Nov 2016 00:55:33 +0000 Subject: [PATCH] Refactor SSHExternal flag so it isn't quietly ignored on Windows --- commands/prepare.go | 4 ++++ config/config.go | 5 +++++ scan/sshutil.go | 11 +++-------- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/commands/prepare.go b/commands/prepare.go index c44bc87653..a3d18ba62c 100644 --- a/commands/prepare.go +++ b/commands/prepare.go @@ -145,6 +145,10 @@ func (p *PrepareCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{ c.Conf.Debug = p.debug c.Conf.SSHExternal = p.sshExternal + logrus.Info("Validating Config...") + if !c.Conf.Validate() { + return subcommands.ExitUsageError + } // Set up custom logger logger := util.NewCustomLogger(c.ServerInfo{}) diff --git a/config/config.go b/config/config.go index 8043aea943..ec34eb60cf 100644 --- a/config/config.go +++ b/config/config.go @@ -19,6 +19,7 @@ package config import ( "fmt" + "runtime" "strings" log "github.com/Sirupsen/logrus" @@ -70,6 +71,10 @@ type Config struct { func (c Config) Validate() bool { errs := []error{} + if runtime.GOOS == "windows" && c.SSHExternal { + errs = append(errs, fmt.Errorf("-ssh-external cannot be used on windows")) + } + if len(c.ResultsDir) != 0 { if ok, _ := valid.IsFilePath(c.ResultsDir); !ok { errs = append(errs, fmt.Errorf( diff --git a/scan/sshutil.go b/scan/sshutil.go index ebdd200178..fe245e158f 100644 --- a/scan/sshutil.go +++ b/scan/sshutil.go @@ -26,7 +26,6 @@ import ( "net" "os" "os/exec" - "runtime" "strings" "syscall" "time" @@ -150,10 +149,10 @@ func parallelSSHExec(fn func(osTypeInterface) error, timeoutSec ...int) (errs [] } func sshExec(c conf.ServerInfo, cmd string, sudo bool, log ...*logrus.Entry) (result sshResult) { - if isSSHExecNative() { - result = sshExecNative(c, cmd, sudo) - } else { + if conf.Conf.SSHExternal { result = sshExecExternal(c, cmd, sudo) + } else { + result = sshExecNative(c, cmd, sudo) } logger := getSSHLogger(log...) @@ -161,10 +160,6 @@ func sshExec(c conf.ServerInfo, cmd string, sudo bool, log ...*logrus.Entry) (re return } -func isSSHExecNative() bool { - return runtime.GOOS == "windows" || !conf.Conf.SSHExternal -} - func sshExecNative(c conf.ServerInfo, cmd string, sudo bool) (result sshResult) { result.Servername = c.ServerName result.Host = c.Host