From 583abab5528ba8ba288399412bdc00b0093da878 Mon Sep 17 00:00:00 2001 From: Sean Chittenden Date: Thu, 31 Mar 2016 14:47:55 -0700 Subject: [PATCH 1/2] Node names are not allowed to be empty --- command/agent/command.go | 8 +++++- command/agent/command_test.go | 49 ++++++++++++++++++++++++----------- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/command/agent/command.go b/command/agent/command.go index 73acf307e2..16025c9d5d 100644 --- a/command/agent/command.go +++ b/command/agent/command.go @@ -164,7 +164,13 @@ func (c *Command) readConfig() *Config { if config.NodeName == "" { hostname, err := os.Hostname() if err != nil { - c.Ui.Error(fmt.Sprintf("Error determining hostname: %s", err)) + c.Ui.Error(fmt.Sprintf("Error determining node name: %s", err)) + return nil + } + + hostname = strings.TrimSpace(hostname) + if hostname == "" { + c.Ui.Error("Node name can not be empty") return nil } config.NodeName = hostname diff --git a/command/agent/command_test.go b/command/agent/command_test.go index bfa68ffad9..cc0ec21e35 100644 --- a/command/agent/command_test.go +++ b/command/agent/command_test.go @@ -114,24 +114,43 @@ func TestReadCliConfig(t *testing.T) { shutdownCh := make(chan struct{}) defer close(shutdownCh) - tmpDir, err := ioutil.TempDir("", "consul") - if err != nil { - t.Fatalf("err: %s", err) + // Test config parse + { + tmpDir, err := ioutil.TempDir("", "consul") + if err != nil { + t.Fatalf("err: %s", err) + } + + cmd := &Command{ + args: []string{ + "-data-dir", tmpDir, + "-node", `"a"`, + "-advertise-wan", "1.2.3.4", + }, + ShutdownCh: shutdownCh, + Ui: new(cli.MockUi), + } + + config := cmd.readConfig() + if config.AdvertiseAddrWan != "1.2.3.4" { + t.Fatalf("expected -advertise-addr-wan 1.2.3.4 got %s", config.AdvertiseAddrWan) + } } - cmd := &Command{ - args: []string{ - "-data-dir", tmpDir, - "-node", `"a"`, - "-advertise-wan", "1.2.3.4", - }, - ShutdownCh: shutdownCh, - Ui: new(cli.MockUi), - } + // Test empty node name + { + cmd := &Command{ + args: []string{ + "-node", `""`, + }, + ShutdownCh: shutdownCh, + Ui: new(cli.MockUi), + } - config := cmd.readConfig() - if config.AdvertiseAddrWan != "1.2.3.4" { - t.Fatalf("expected -advertise-addr-wan 1.2.3.4 got %s", config.AdvertiseAddrWan) + config := cmd.readConfig() + if config != nil { + t.Errorf(`Expected -node="" to fail`) + } } } From ff529ffa40e840b259f6fcb276f17998bcf1f0ed Mon Sep 17 00:00:00 2001 From: Sean Chittenden Date: Thu, 31 Mar 2016 15:02:58 -0700 Subject: [PATCH 2/2] Guard against a node name containing pure whitespace --- command/agent/command.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/command/agent/command.go b/command/agent/command.go index 16025c9d5d..dfd24ef57e 100644 --- a/command/agent/command.go +++ b/command/agent/command.go @@ -167,14 +167,13 @@ func (c *Command) readConfig() *Config { c.Ui.Error(fmt.Sprintf("Error determining node name: %s", err)) return nil } - - hostname = strings.TrimSpace(hostname) - if hostname == "" { - c.Ui.Error("Node name can not be empty") - return nil - } config.NodeName = hostname } + hostname = strings.TrimSpace(hostname) + if hostname == "" { + c.Ui.Error("Node name can not be empty") + return nil + } // Ensure we have a data directory if config.DataDir == "" && !dev {