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 all commits
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
14 changes: 14 additions & 0 deletions command/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,20 @@ func DecodeConfig(r io.Reader) (*Config, error) {
return nil, err
}

// Check unused fields and verify that no bad configuration options were
// passed to Consul. There are a few additional fields which don't directly
// use mapstructure decoding, so we need to account for those as well.
allowedKeys := []string{"service", "services", "check", "checks"}
var unused []string
for _, field := range md.Unused {
if !strContains(allowedKeys, field) {
unused = append(unused, field)
}
}
if len(unused) > 0 {
return nil, fmt.Errorf("Config has invalid keys: %s", strings.Join(unused, ","))
}

// Handle time conversions
if raw := result.DNSConfig.NodeTTLRaw; raw != "" {
dur, err := time.ParseDuration(raw)
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