From 401bc71cb23cbc49b714d2316b05763e1891f20c Mon Sep 17 00:00:00 2001 From: foostan Date: Tue, 27 Jan 2015 01:06:49 +0900 Subject: [PATCH 1/3] Validate ServiceID/CheckID when deregistering. --- command/agent/agent.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/command/agent/agent.go b/command/agent/agent.go index b21dae5f58..a0bd63539a 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -637,6 +637,11 @@ func (a *Agent) RemoveService(serviceID string, persist bool) error { consul.ConsulServiceID) } + // Validate ServiceID + if serviceID == "" { + return fmt.Errorf("ServiceID missing") + } + // Remove service immeidately a.state.RemoveService(serviceID) @@ -757,6 +762,11 @@ func (a *Agent) AddCheck(check *structs.HealthCheck, chkType *CheckType, persist // RemoveCheck is used to remove a health check. // The agent will make a best effort to ensure it is deregistered func (a *Agent) RemoveCheck(checkID string, persist bool) error { + // Validate CheckID + if checkID == "" { + return fmt.Errorf("CheckID missing") + } + // Add to the local state for anti-entropy a.state.RemoveCheck(checkID) From 9316596e252875438a1254bee9710d59a284bffa Mon Sep 17 00:00:00 2001 From: foostan Date: Tue, 27 Jan 2015 18:10:56 +0900 Subject: [PATCH 2/3] Add tests to remove service/check without an ID --- command/agent/agent_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/command/agent/agent_test.go b/command/agent/agent_test.go index 0a702263ff..da098d6eb9 100644 --- a/command/agent/agent_test.go +++ b/command/agent/agent_test.go @@ -247,6 +247,11 @@ func TestAgent_RemoveService(t *testing.T) { t.Fatalf("should have errored") } + // Remove without an ID + if err := agent.RemoveService("", false); err == nil { + t.Fatalf("should have errored") + } + // Removing a service with a single check works { srv := &structs.NodeService{ @@ -406,6 +411,11 @@ func TestAgent_RemoveCheck(t *testing.T) { t.Fatalf("err: %v", err) } + // Remove without an ID + if err := agent.RemoveCheck("", false); err == nil { + t.Fatalf("should have errored") + } + health := &structs.HealthCheck{ Node: "foo", CheckID: "mem", From 2df98c182436d4648db39614cb7f5aded56e3900 Mon Sep 17 00:00:00 2001 From: foostan Date: Tue, 27 Jan 2015 18:11:57 +0900 Subject: [PATCH 3/3] Validation ServiceID/CheckID when deleting in deleteService() in local.go --- command/agent/local.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/command/agent/local.go b/command/agent/local.go index 1a394f4e9f..38b5ee1699 100644 --- a/command/agent/local.go +++ b/command/agent/local.go @@ -1,6 +1,7 @@ package agent import ( + "fmt" "log" "reflect" "strings" @@ -408,6 +409,10 @@ func (l *localState) syncChanges() error { // deleteService is used to delete a service from the server func (l *localState) deleteService(id string) error { + if id == "" { + return fmt.Errorf("ServiceID missing") + } + req := structs.DeregisterRequest{ Datacenter: l.config.Datacenter, Node: l.config.NodeName, @@ -425,6 +430,10 @@ func (l *localState) deleteService(id string) error { // deleteCheck is used to delete a service from the server func (l *localState) deleteCheck(id string) error { + if id == "" { + return fmt.Errorf("CheckID missing") + } + req := structs.DeregisterRequest{ Datacenter: l.config.Datacenter, Node: l.config.NodeName,