From 524cb0d0c31bed27ce1dfd56f2f917e9d4637c46 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 b21dae5f5..a0bd63539 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 99659e9014c1493ff27c873845325c5f578c3691 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 0a702263f..da098d6eb 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 a75b7273918091558e6e56f3a29824742515c5af 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 1a394f4e9..38b5ee169 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,