From 704ad6ecb4d97709616f2ee7330c09621f6cf6c5 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Tue, 23 Feb 2016 10:19:40 -0800 Subject: [PATCH] validate that command contains one field --- client/driver/docker.go | 5 +++++ client/driver/exec.go | 4 ++-- client/driver/raw_exec.go | 4 ++-- client/driver/utils.go | 22 ++++++++++++++++++++++ 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/client/driver/docker.go b/client/driver/docker.go index 100bc980e92..78f5d02e803 100644 --- a/client/driver/docker.go +++ b/client/driver/docker.go @@ -369,6 +369,11 @@ func (d *DockerDriver) createContainer(ctx *ExecContext, task *structs.Task, // If the user specified a custom command to run as their entrypoint, we'll // inject it here. if driverConfig.Command != "" { + // Validate command + if err := validateCommand(driverConfig.Command, "args"); err != nil { + return c, err + } + cmd := []string{driverConfig.Command} if len(driverConfig.Args) != 0 { cmd = append(cmd, parsedArgs...) diff --git a/client/driver/exec.go b/client/driver/exec.go index 54b0b33b9e9..3a1475b855d 100644 --- a/client/driver/exec.go +++ b/client/driver/exec.go @@ -77,8 +77,8 @@ func (d *ExecDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle, } // Get the command to be ran command := driverConfig.Command - if command == "" { - return nil, fmt.Errorf("missing command for exec driver") + if err := validateCommand(command, "args"); err != nil { + return nil, err } // Create a location to download the artifact. diff --git a/client/driver/raw_exec.go b/client/driver/raw_exec.go index 31e25fd5b0a..2b476658434 100644 --- a/client/driver/raw_exec.go +++ b/client/driver/raw_exec.go @@ -77,8 +77,8 @@ func (d *RawExecDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandl // Get the command to be ran command := driverConfig.Command - if command == "" { - return nil, fmt.Errorf("missing command for Raw Exec driver") + if err := validateCommand(command, "args"); err != nil { + return nil, err } // Check if an artificat is specified and attempt to download it diff --git a/client/driver/utils.go b/client/driver/utils.go index bfb923e1b38..51ba54232f0 100644 --- a/client/driver/utils.go +++ b/client/driver/utils.go @@ -4,6 +4,7 @@ import ( "fmt" "io" "os" + "strings" "github.com/hashicorp/go-multierror" "github.com/hashicorp/go-plugin" @@ -87,3 +88,24 @@ func destroyPlugin(pluginPid int, userPid int) error { } return merr } + +// validateCommand validates that the command only has a single value and +// returns a user friendly error message telling them to use the passed +// argField. +func validateCommand(command, argField string) error { + trimmed := strings.TrimSpace(command) + if len(trimmed) == 0 { + return fmt.Errorf("command empty: %q", command) + } + + if len(trimmed) != len(command) { + return fmt.Errorf("command contains extra white space: %q", command) + } + + split := strings.Split(trimmed, " ") + if len(split) != 1 { + return fmt.Errorf("command contained more than one input. Use %q field to pass arguments", argField) + } + + return nil +}