From a286ad7533b344b9e12f5a305963a2d4cb7ddd74 Mon Sep 17 00:00:00 2001 From: Preetha Appan Date: Mon, 25 Sep 2017 17:17:12 -0500 Subject: [PATCH 1/2] Enforce json or hcl extension to Consul config files, updated unit tests --- agent/config/builder.go | 3 +++ command/agent_test.go | 12 +++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/agent/config/builder.go b/agent/config/builder.go index 9f7c0e0050..73c0678115 100644 --- a/agent/config/builder.go +++ b/agent/config/builder.go @@ -174,6 +174,9 @@ func (b *Builder) ReadPath(path string) error { // ReadFile parses a JSON or HCL config file and appends it to the list of // config sources. func (b *Builder) ReadFile(path string) error { + if !strings.HasSuffix(path, ".json") && !strings.HasSuffix(path, ".hcl") { + return fmt.Errorf(`Missing or invalid file extension for %q. Please use ".json" or ".hcl".`, path) + } data, err := ioutil.ReadFile(path) if err != nil { return fmt.Errorf("config: ReadFile failed on %s: %s", path, err) diff --git a/command/agent_test.go b/command/agent_test.go index 431fff7e68..2ab73e35e5 100644 --- a/command/agent_test.go +++ b/command/agent_test.go @@ -229,11 +229,17 @@ func TestProtectDataDir(t *testing.T) { t.Fatalf("err: %v", err) } - cfgFile := testutil.TempFile(t, "consul") - defer os.Remove(cfgFile.Name()) + cfgDir := testutil.TempDir(t, "consul-config") + defer os.RemoveAll(cfgDir) + + cfgFilePath := filepath.Join(cfgDir, "consul.json") + cfgFile, err := os.Create(cfgFilePath) + if err != nil { + t.Fatalf("Unable to create file %v, got error:%v", cfgFilePath, err) + } content := fmt.Sprintf(`{"server": true, "bind_addr" : "10.0.0.1", "data_dir": "%s"}`, dir) - _, err := cfgFile.Write([]byte(content)) + _, err = cfgFile.Write([]byte(content)) if err != nil { t.Fatalf("err: %v", err) } From a4331a480cb64d259cc51ba39bb53abb9d3217eb Mon Sep 17 00:00:00 2001 From: Preetha Appan Date: Mon, 25 Sep 2017 17:26:49 -0500 Subject: [PATCH 2/2] Fix one more unit test that created a config file --- api/agent_test.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/api/agent_test.go b/api/agent_test.go index a51f467a0c..c5c56d00c8 100644 --- a/api/agent_test.go +++ b/api/agent_test.go @@ -7,6 +7,9 @@ import ( "testing" "time" + "os" + "path/filepath" + "github.com/hashicorp/consul/testutil" "github.com/hashicorp/serf/serf" ) @@ -54,11 +57,14 @@ func TestAPI_AgentReload(t *testing.T) { t.Parallel() // Create our initial empty config file, to be overwritten later - configFile := testutil.TempFile(t, "reload") - if _, err := configFile.Write([]byte("{}")); err != nil { - t.Fatalf("err: %s", err) + cfgDir := testutil.TempDir(t, "consul-config") + defer os.RemoveAll(cfgDir) + + cfgFilePath := filepath.Join(cfgDir, "reload.json") + configFile, err := os.Create(cfgFilePath) + if err != nil { + t.Fatalf("Unable to create file %v, got error:%v", cfgFilePath, err) } - configFile.Close() c, s := makeClientWithConfig(t, nil, func(conf *testutil.TestServerConfig) { conf.Args = []string{"-config-file", configFile.Name()} @@ -69,7 +75,7 @@ func TestAPI_AgentReload(t *testing.T) { // Update the config file with a service definition config := `{"service":{"name":"redis", "port":1234}}` - err := ioutil.WriteFile(configFile.Name(), []byte(config), 0644) + err = ioutil.WriteFile(configFile.Name(), []byte(config), 0644) if err != nil { t.Fatalf("err: %v", err) }