Skip to content

Commit

Permalink
Merge pull request #842 from hashicorp/f-validate-command
Browse files Browse the repository at this point in the history
drivers: validate that command contains one field
  • Loading branch information
diptanu committed Feb 23, 2016
2 parents c100064 + 704ad6e commit 37e8248
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
5 changes: 5 additions & 0 deletions client/driver/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
4 changes: 2 additions & 2 deletions client/driver/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions client/driver/raw_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions client/driver/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"os"
"strings"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-plugin"
Expand Down Expand Up @@ -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
}

0 comments on commit 37e8248

Please sign in to comment.