Skip to content

Commit

Permalink
Merge pull request #634 from hashicorp/f-httpargs-conflict
Browse files Browse the repository at this point in the history
agent: error from KVS endpoint if incompatible flags are passed.
  • Loading branch information
ryanuber committed Jan 23, 2015
2 parents 33b2e71 + b69b780 commit 0bfffd4
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
25 changes: 25 additions & 0 deletions command/agent/kvs_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
42 changes: 42 additions & 0 deletions command/agent/kvs_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
})
}

0 comments on commit 0bfffd4

Please sign in to comment.