Skip to content

Commit

Permalink
agent: reject config with invalid options
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanuber committed Jan 5, 2015
1 parent e9615c5 commit b8740b6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
12 changes: 10 additions & 2 deletions command/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,13 @@ type Config struct {

// WatchPlans contains the compiled watches
WatchPlans []*watch.WatchPlan `mapstructure:"-" json:"-"`

// Allow the following fields to be present in configuration files without
// mapstructure erroring on them.
_ interface{} `mapstructure:"services"`
_ interface{} `mapstructure:"checks"`
_ interface{} `mapstructure:"service"`
_ interface{} `mapstructure:"check"`
}

type dirEnts []os.FileInfo
Expand Down Expand Up @@ -463,8 +470,9 @@ func DecodeConfig(r io.Reader) (*Config, error) {
// Decode
var md mapstructure.Metadata
msdec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Metadata: &md,
Result: &result,
Metadata: &md,
Result: &result,
ErrorUnused: true,
})
if err != nil {
return nil, err
Expand Down
9 changes: 9 additions & 0 deletions command/agent/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -591,6 +592,14 @@ func TestDecodeConfig(t *testing.T) {
}
}

func TestDecodeConfig_invalidKeys(t *testing.T) {
input := `{"bad": "no way jose"}`
_, err := DecodeConfig(bytes.NewReader([]byte(input)))
if err == nil || !strings.Contains(err.Error(), "invalid keys") {
t.Fatalf("should have rejected invalid config keys")
}
}

func TestDecodeConfig_Services(t *testing.T) {
input := `{
"services": [
Expand Down

0 comments on commit b8740b6

Please sign in to comment.