Merge pull request #576 from hashicorp/f-verify-config

agent: reject config with invalid options
This commit is contained in:
Ryan Uber 2015-01-05 14:51:28 -08:00
commit db3c502dcc
2 changed files with 23 additions and 0 deletions

View File

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

View File

@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
)
@ -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": [