From 10f297e8fc24cf4c448f401fc15d50f34df59a3e Mon Sep 17 00:00:00 2001 From: Chavez Date: Mon, 28 Mar 2016 09:56:24 -0700 Subject: [PATCH 1/4] command: Data directory permission error message * Check for invalid data directory permissions * Display appropriate permissions error message * Add command test for bad data directory permissions --- command/agent/command.go | 6 ++++++ command/agent/command_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/command/agent/command.go b/command/agent/command.go index 17d26b5ce..af2281529 100644 --- a/command/agent/command.go +++ b/command/agent/command.go @@ -204,6 +204,12 @@ func (c *Command) readConfig() *Config { if config.Server { mdbPath := filepath.Join(config.DataDir, "mdb") if _, err := os.Stat(mdbPath); !os.IsNotExist(err) { + if os.IsPermission(err) { + c.Ui.Error(fmt.Sprintf("CRITICAL: Permission denied for data folder at %q!", mdbPath)) + c.Ui.Error("Consul will refuse to boot without access to this directory.") + c.Ui.Error("Please correct permissions and try starting again.") + return nil + } c.Ui.Error(fmt.Sprintf("CRITICAL: Deprecated data folder found at %q!", mdbPath)) c.Ui.Error("Consul will refuse to boot with this directory present.") c.Ui.Error("See https://www.consul.io/docs/upgrade-specific.html for more information.") diff --git a/command/agent/command_test.go b/command/agent/command_test.go index 773a510d8..d04a8007b 100644 --- a/command/agent/command_test.go +++ b/command/agent/command_test.go @@ -403,3 +403,29 @@ func TestProtectDataDir(t *testing.T) { t.Fatalf("expected mdb dir error, got: %s", out) } } + +func TestBadDataDirPermissions(t *testing.T) { + dir, err := ioutil.TempDir("", "consul") + if err != nil { + t.Fatalf("err: %v", err) + } + defer os.RemoveAll(dir) + + dataDir := filepath.Join(dir, "mdb") + if err := os.MkdirAll(dataDir, 0400); err != nil { + t.Fatalf("err: %v", err) + } + defer os.RemoveAll(dataDir) + + ui := new(cli.MockUi) + cmd := &Command{ + Ui: ui, + args: []string{"-data-dir=" + dataDir, "-server=true"}, + } + if conf := cmd.readConfig(); conf != nil { + t.Fatalf("Should fail with bad data directory permissions") + } + if out := ui.ErrorWriter.String(); !strings.Contains(out, "Permission denied") { + t.Fatalf("expected permission denied error, got: %s", out) + } +} From 09f962a75ba238447c2656cf972758f25ffbef7e Mon Sep 17 00:00:00 2001 From: Chavez Date: Tue, 29 Mar 2016 11:30:18 -0700 Subject: [PATCH 2/4] Test agent RPC client connection retries --- command/agent/rpc_client_test.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/command/agent/rpc_client_test.go b/command/agent/rpc_client_test.go index af1af23ea..9217cf953 100644 --- a/command/agent/rpc_client_test.go +++ b/command/agent/rpc_client_test.go @@ -3,8 +3,6 @@ package agent import ( "errors" "fmt" - "github.com/hashicorp/consul/testutil" - "github.com/hashicorp/serf/serf" "io" "io/ioutil" "net" @@ -14,6 +12,9 @@ import ( "strings" "testing" "time" + + "github.com/hashicorp/consul/testutil" + "github.com/hashicorp/serf/serf" ) type rpcParts struct { @@ -40,6 +41,9 @@ func testRPCClientWithConfig(t *testing.T, cb func(c *Config)) *rpcParts { lw := NewLogWriter(512) mult := io.MultiWriter(os.Stderr, lw) + configTry := 0 +RECONF: + configTry += 1 conf := nextConfig() cb(conf) @@ -50,6 +54,9 @@ func testRPCClientWithConfig(t *testing.T, cb func(c *Config)) *rpcParts { l, err := net.Listen(rpcAddr.Network(), rpcAddr.String()) if err != nil { + if configTry < 3 { + goto RECONF + } t.Fatalf("err: %s", err) } From c9602c561c99420c051d8303264a92ffcf6e26aa Mon Sep 17 00:00:00 2001 From: Chavez Date: Tue, 29 Mar 2016 12:08:20 -0700 Subject: [PATCH 3/4] Add description to rpc test client pool member failure message --- consul/client_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/consul/client_test.go b/consul/client_test.go index 3e4d7706c..b4d932b38 100644 --- a/consul/client_test.go +++ b/consul/client_test.go @@ -212,7 +212,7 @@ func TestClient_RPC_Pool(t *testing.T) { t.Fatalf("err: %v", err) } if len(s1.LANMembers()) != 2 || len(c1.LANMembers()) != 2 { - t.Fatalf("bad len") + t.Fatalf("Server has %v of %v expected members; Client has %v of %v expected members.", len(s1.LANMembers()), 2, len(c1.LANMembers()), 2) } // Blast out a bunch of RPC requests at the same time to try to get From f75c0f4390094ab59407f3c612116e1a22f3408c Mon Sep 17 00:00:00 2001 From: Chavez Date: Tue, 29 Mar 2016 12:09:23 -0700 Subject: [PATCH 4/4] Retry http server connecting in agent/http_test --- command/agent/http_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/command/agent/http_test.go b/command/agent/http_test.go index 1afabb094..685f5cbd4 100644 --- a/command/agent/http_test.go +++ b/command/agent/http_test.go @@ -28,6 +28,9 @@ func makeHTTPServer(t *testing.T) (string, *HTTPServer) { } func makeHTTPServerWithConfig(t *testing.T, cb func(c *Config)) (string, *HTTPServer) { + configTry := 0 +RECONF: + configTry += 1 conf := nextConfig() if cb != nil { cb(conf) @@ -36,6 +39,9 @@ func makeHTTPServerWithConfig(t *testing.T, cb func(c *Config)) (string, *HTTPSe dir, agent := makeAgent(t, conf) servers, err := NewHTTPServers(agent, conf, agent.logOutput) if err != nil { + if configTry < 3 { + goto RECONF + } t.Fatalf("err: %v", err) } if len(servers) == 0 {