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
This commit is contained in:
Chavez 2016-03-28 09:56:24 -07:00
parent af0f87ed90
commit 10f297e8fc
2 changed files with 32 additions and 0 deletions

View File

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

View File

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