Returns a 404 from a get or execute of a nonexistent query.

pull/1389/head
James Phillips 2015-11-13 12:57:06 -08:00
parent e9480ecb02
commit 67fd4fa78d
4 changed files with 66 additions and 10 deletions

View File

@ -6,6 +6,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/hashicorp/consul/consul"
"github.com/hashicorp/consul/consul/structs" "github.com/hashicorp/consul/consul/structs"
) )
@ -101,6 +102,13 @@ func (s *HTTPServer) PreparedQuerySpecific(resp http.ResponseWriter, req *http.R
var reply structs.PreparedQueryExecuteResponse var reply structs.PreparedQueryExecuteResponse
if err := s.agent.RPC(endpoint+".Execute", &args, &reply); err != nil { if err := s.agent.RPC(endpoint+".Execute", &args, &reply); err != nil {
// We have to check the string since the RPC sheds
// the specific error type.
if err.Error() == consul.ErrQueryNotFound.Error() {
resp.WriteHeader(404)
resp.Write([]byte(err.Error()))
return nil, nil
}
return nil, err return nil, err
} }
return reply, nil return reply, nil
@ -114,6 +122,13 @@ func (s *HTTPServer) PreparedQuerySpecific(resp http.ResponseWriter, req *http.R
var reply structs.IndexedPreparedQueries var reply structs.IndexedPreparedQueries
if err := s.agent.RPC(endpoint+".Get", &args, &reply); err != nil { if err := s.agent.RPC(endpoint+".Get", &args, &reply); err != nil {
// We have to check the string since the RPC sheds
// the specific error type.
if err.Error() == consul.ErrQueryNotFound.Error() {
resp.WriteHeader(404)
resp.Write([]byte(err.Error()))
return nil, nil
}
return nil, err return nil, err
} }
return reply.Queries, nil return reply.Queries, nil

View File

@ -245,6 +245,23 @@ func TestPreparedQuery_Execute(t *testing.T) {
t.Fatalf("bad: %v", r) t.Fatalf("bad: %v", r)
} }
}) })
httpTest(t, func(srv *HTTPServer) {
body := bytes.NewBuffer(nil)
req, err := http.NewRequest("GET", "/v1/query/not-there/execute", body)
if err != nil {
t.Fatalf("err: %v", err)
}
resp := httptest.NewRecorder()
_, err = srv.PreparedQuerySpecific(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
if resp.Code != 404 {
t.Fatalf("bad code: %d", resp.Code)
}
})
} }
func TestPreparedQuery_Get(t *testing.T) { func TestPreparedQuery_Get(t *testing.T) {
@ -296,6 +313,23 @@ func TestPreparedQuery_Get(t *testing.T) {
t.Fatalf("bad: %v", r) t.Fatalf("bad: %v", r)
} }
}) })
httpTest(t, func(srv *HTTPServer) {
body := bytes.NewBuffer(nil)
req, err := http.NewRequest("GET", "/v1/query/f004177f-2c28-83b7-4229-eacc25fe55d1", body)
if err != nil {
t.Fatalf("err: %v", err)
}
resp := httptest.NewRecorder()
_, err = srv.PreparedQuerySpecific(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
if resp.Code != 404 {
t.Fatalf("bad code: %d", resp.Code)
}
})
} }
func TestPreparedQuery_Update(t *testing.T) { func TestPreparedQuery_Update(t *testing.T) {

View File

@ -206,18 +206,17 @@ func (p *PreparedQuery) Get(args *structs.PreparedQuerySpecificRequest,
if err != nil { if err != nil {
return err return err
} }
if query == nil {
return ErrQueryNotFound
}
if (query != nil) && (query.Token != args.Token) && (acl != nil && !acl.QueryList()) { if (query.Token != args.Token) && (acl != nil && !acl.QueryList()) {
p.srv.logger.Printf("[WARN] consul.prepared_query: Request to get prepared query '%s' denied because ACL didn't match ACL used to create the query, and a management token wasn't supplied", args.QueryID) p.srv.logger.Printf("[WARN] consul.prepared_query: Request to get prepared query '%s' denied because ACL didn't match ACL used to create the query, and a management token wasn't supplied", args.QueryID)
return permissionDeniedErr return permissionDeniedErr
} }
reply.Index = index reply.Index = index
if query != nil {
reply.Queries = structs.PreparedQueries{query} reply.Queries = structs.PreparedQueries{query}
} else {
reply.Queries = nil
}
return nil return nil
}) })

View File

@ -184,8 +184,10 @@ func TestPreparedQuery_Apply(t *testing.T) {
} }
var resp structs.IndexedPreparedQueries var resp structs.IndexedPreparedQueries
if err = msgpackrpc.CallWithCodec(codec, "PreparedQuery.Get", req, &resp); err != nil { if err = msgpackrpc.CallWithCodec(codec, "PreparedQuery.Get", req, &resp); err != nil {
if err.Error() != ErrQueryNotFound.Error() {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
}
if len(resp.Queries) != 0 { if len(resp.Queries) != 0 {
t.Fatalf("bad: %v", resp) t.Fatalf("bad: %v", resp)
@ -363,8 +365,10 @@ func TestPreparedQuery_Apply_ACLDeny(t *testing.T) {
} }
var resp structs.IndexedPreparedQueries var resp structs.IndexedPreparedQueries
if err = msgpackrpc.CallWithCodec(codec, "PreparedQuery.Get", req, &resp); err != nil { if err = msgpackrpc.CallWithCodec(codec, "PreparedQuery.Get", req, &resp); err != nil {
if err.Error() != ErrQueryNotFound.Error() {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
}
if len(resp.Queries) != 0 { if len(resp.Queries) != 0 {
t.Fatalf("bad: %v", resp) t.Fatalf("bad: %v", resp)
@ -492,8 +496,10 @@ func TestPreparedQuery_Apply_ACLDeny(t *testing.T) {
} }
var resp structs.IndexedPreparedQueries var resp structs.IndexedPreparedQueries
if err = msgpackrpc.CallWithCodec(codec, "PreparedQuery.Get", req, &resp); err != nil { if err = msgpackrpc.CallWithCodec(codec, "PreparedQuery.Get", req, &resp); err != nil {
if err.Error() != ErrQueryNotFound.Error() {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
}
if len(resp.Queries) != 0 { if len(resp.Queries) != 0 {
t.Fatalf("bad: %v", resp) t.Fatalf("bad: %v", resp)
@ -792,8 +798,10 @@ func TestPreparedQuery_Get(t *testing.T) {
} }
var resp structs.IndexedPreparedQueries var resp structs.IndexedPreparedQueries
if err := msgpackrpc.CallWithCodec(codec, "PreparedQuery.Get", req, &resp); err != nil { if err := msgpackrpc.CallWithCodec(codec, "PreparedQuery.Get", req, &resp); err != nil {
if err.Error() != ErrQueryNotFound.Error() {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
}
if len(resp.Queries) != 0 { if len(resp.Queries) != 0 {
t.Fatalf("bad: %v", resp) t.Fatalf("bad: %v", resp)