Skip to content

Commit

Permalink
api: Return 404 when deregistering a non-existent check (#11950)
Browse files Browse the repository at this point in the history
Update the `/agent/check/deregister/` API endpoint to return a 404
HTTP response code when an attempt is made to de-register a check ID
that does not exist on the agent.

This brings the behavior of /agent/check/deregister/ in line with the
behavior of /agent/service/deregister/ which was changed in #10632 to
similarly return a 404 when de-registering non-existent services.

Fixes #5821
  • Loading branch information
blake authored Jan 6, 2022
1 parent 1eac39a commit 4bd9292
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .changelog/11950.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
api: Return 404 when de-registering a non-existent check
```
4 changes: 3 additions & 1 deletion agent/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ func (a *Agent) vetCheckUpdateWithAuthorizer(authz acl.Authorizer, checkID struc
}
}
} else {
return fmt.Errorf("Unknown check ID %q. Ensure that the check ID is passed, not the check name.", checkID.String())
return NotFoundError{Reason: fmt.Sprintf(
"Unknown check ID %q. Ensure that the check ID is passed, not the check name.",
checkID.String())}
}

return nil
Expand Down
33 changes: 27 additions & 6 deletions agent/agent_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2889,12 +2889,19 @@ func TestAgent_DeregisterCheck(t *testing.T) {
t.Fatalf("err: %v", err)
}

req, _ := http.NewRequest("PUT", "/v1/agent/check/deregister/test", nil)
resp := httptest.NewRecorder()
a.srv.h.ServeHTTP(resp, req)
if http.StatusOK != resp.Code {
t.Fatalf("expected 200 but got %v", resp.Code)
}
t.Run("remove registered check", func(t *testing.T) {
req, _ := http.NewRequest("PUT", "/v1/agent/check/deregister/test", nil)
resp := httptest.NewRecorder()
a.srv.h.ServeHTTP(resp, req)
require.Equal(t, http.StatusOK, resp.Code)
})

t.Run("remove non-existent check", func(t *testing.T) {
req, _ := http.NewRequest("PUT", "/v1/agent/check/deregister/test", nil)
resp := httptest.NewRecorder()
a.srv.h.ServeHTTP(resp, req)
require.Equal(t, http.StatusNotFound, resp.Code)
})

// Ensure we have a check mapping
requireCheckMissing(t, a, "test")
Expand Down Expand Up @@ -2928,6 +2935,20 @@ func TestAgent_DeregisterCheckACLDeny(t *testing.T) {
a.srv.h.ServeHTTP(resp, req)
require.Equal(t, http.StatusOK, resp.Code)
})

t.Run("non-existent check without token", func(t *testing.T) {
req, _ := http.NewRequest("PUT", "/v1/agent/check/deregister/_nope_", nil)
resp := httptest.NewRecorder()
a.srv.h.ServeHTTP(resp, req)
require.Equal(t, http.StatusNotFound, resp.Code)
})

t.Run("non-existent check with token", func(t *testing.T) {
req, _ := http.NewRequest("PUT", "/v1/agent/check/deregister/_nope_?token=root", nil)
resp := httptest.NewRecorder()
a.srv.h.ServeHTTP(resp, req)
require.Equal(t, http.StatusNotFound, resp.Code)
})
}

func TestAgent_PassCheck(t *testing.T) {
Expand Down

0 comments on commit 4bd9292

Please sign in to comment.