Skip to content

Commit

Permalink
Merge pull request #576 from hashicorp/f-verify-config
Browse files Browse the repository at this point in the history
agent: reject config with invalid options
  • Loading branch information
ryanuber committed Jan 5, 2015
2 parents f86d7c3 + 42ace3a commit db3c502
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
14 changes: 14 additions & 0 deletions command/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,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 @@ -606,6 +607,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 db3c502

Please sign in to comment.