Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

agent: reject config with invalid options #576

Merged
merged 2 commits into from
Jan 5, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should leave this false and remove all the unused fields from Config. Instead use the Unused field in Metadata. We can scan that for fields that are not (services, checks, service, check) and error / warn on those. This way we have more fine grain control later when we deprecate flags and stuff too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that seems less ghetto - I'll fix this up.

})
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