Merge pull request #634 from hashicorp/f-httpargs-conflict

agent: error from KVS endpoint if incompatible flags are passed.
This commit is contained in:
Ryan Uber 2015-01-23 13:13:40 -08:00
commit 3b864656b7
2 changed files with 67 additions and 0 deletions

View File

@ -136,6 +136,9 @@ func (s *HTTPServer) KVSPut(resp http.ResponseWriter, req *http.Request, args *s
if missingKey(resp, args) {
return nil, nil
}
if conflictingFlags(resp, req, "cas", "acquire", "release") {
return nil, nil
}
applyReq := structs.KVSRequest{
Datacenter: args.Datacenter,
Op: structs.KVSSet,
@ -209,6 +212,9 @@ func (s *HTTPServer) KVSPut(resp http.ResponseWriter, req *http.Request, args *s
// KVSPut handles a DELETE request
func (s *HTTPServer) KVSDelete(resp http.ResponseWriter, req *http.Request, args *structs.KeyRequest) (interface{}, error) {
if conflictingFlags(resp, req, "recurse", "cas") {
return nil, nil
}
applyReq := structs.KVSRequest{
Datacenter: args.Datacenter,
Op: structs.KVSDelete,
@ -259,3 +265,22 @@ func missingKey(resp http.ResponseWriter, args *structs.KeyRequest) bool {
}
return false
}
// conflictingFlags determines if non-composable flags were passed in a request.
func conflictingFlags(resp http.ResponseWriter, req *http.Request, flags ...string) bool {
params := req.URL.Query()
found := false
for _, conflict := range flags {
if _, ok := params[conflict]; ok {
if found {
resp.WriteHeader(400)
resp.Write([]byte("Conflicting flags: " + params.Encode()))
return true
}
found = true
}
}
return false
}

View File

@ -531,3 +531,45 @@ func TestKVSEndpoint_GET_Raw(t *testing.T) {
}
})
}
func TestKVSEndpoint_PUT_ConflictingFlags(t *testing.T) {
httpTest(t, func(srv *HTTPServer) {
req, err := http.NewRequest("PUT", "/v1/kv/test?cas=0&acquire=xxx", nil)
if err != nil {
t.Fatalf("err: %v", err)
}
resp := httptest.NewRecorder()
if _, err := srv.KVSEndpoint(resp, req); err != nil {
t.Fatalf("err: %v", err)
}
if resp.Code != 400 {
t.Fatalf("expected 400, got %d", resp.Code)
}
if !bytes.Contains(resp.Body.Bytes(), []byte("Conflicting")) {
t.Fatalf("expected conflicting args error")
}
})
}
func TestKVSEndpoint_DELETE_ConflictingFlags(t *testing.T) {
httpTest(t, func(srv *HTTPServer) {
req, err := http.NewRequest("DELETE", "/v1/kv/test?recurse&cas=0", nil)
if err != nil {
t.Fatalf("err: %v", err)
}
resp := httptest.NewRecorder()
if _, err := srv.KVSEndpoint(resp, req); err != nil {
t.Fatalf("err: %v", err)
}
if resp.Code != 400 {
t.Fatalf("expected 400, got %d", resp.Code)
}
if !bytes.Contains(resp.Body.Bytes(), []byte("Conflicting")) {
t.Fatalf("expected conflicting args error")
}
})
}