agent: use http.StatusBadRequest instead of 400

pull/3416/head
Frank Schroeder 2017-08-23 16:20:44 +02:00 committed by Frank Schröder
parent 01eae2e9cf
commit 5d1546b052
10 changed files with 47 additions and 47 deletions

View File

@ -64,7 +64,7 @@ func (s *HTTPServer) ACLDestroy(resp http.ResponseWriter, req *http.Request) (in
// Pull out the acl id
args.ACL.ID = strings.TrimPrefix(req.URL.Path, "/v1/acl/destroy/")
if args.ACL.ID == "" {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprint(resp, "Missing ACL")
return nil, nil
}
@ -103,7 +103,7 @@ func (s *HTTPServer) aclSet(resp http.ResponseWriter, req *http.Request, update
// Handle optional request body
if req.ContentLength > 0 {
if err := decodeBody(req, &args.ACL, nil); err != nil {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprintf(resp, "Request decode failed: %v", err)
return nil, nil
}
@ -112,7 +112,7 @@ func (s *HTTPServer) aclSet(resp http.ResponseWriter, req *http.Request, update
// Ensure there is an ID set for update. ID is optional for
// create, as one will be generated if not provided.
if update && args.ACL.ID == "" {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprint(resp, "ACL ID must be set")
return nil, nil
}
@ -145,7 +145,7 @@ func (s *HTTPServer) ACLClone(resp http.ResponseWriter, req *http.Request) (inte
// Pull out the acl id
args.ACL = strings.TrimPrefix(req.URL.Path, "/v1/acl/clone/")
if args.ACL == "" {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprint(resp, "Missing ACL")
return nil, nil
}
@ -193,7 +193,7 @@ func (s *HTTPServer) ACLGet(resp http.ResponseWriter, req *http.Request) (interf
// Pull out the acl id
args.ACL = strings.TrimPrefix(req.URL.Path, "/v1/acl/info/")
if args.ACL == "" {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprint(resp, "Missing ACL")
return nil, nil
}

View File

@ -252,20 +252,20 @@ func (s *HTTPServer) AgentRegisterCheck(resp http.ResponseWriter, req *http.Requ
return FixupCheckType(raw)
}
if err := decodeBody(req, &args, decodeCB); err != nil {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprintf(resp, "Request decode failed: %v", err)
return nil, nil
}
// Verify the check has a name.
if args.Name == "" {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprint(resp, "Missing check name")
return nil, nil
}
if args.Status != "" && !structs.ValidStatus(args.Status) {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprint(resp, "Bad check status")
return nil, nil
}
@ -276,7 +276,7 @@ func (s *HTTPServer) AgentRegisterCheck(resp http.ResponseWriter, req *http.Requ
// Verify the check type.
chkType := args.CheckType()
if !chkType.Valid() {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprint(resp, invalidCheckMessage)
return nil, nil
}
@ -390,7 +390,7 @@ func (s *HTTPServer) AgentCheckUpdate(resp http.ResponseWriter, req *http.Reques
var update checkUpdate
if err := decodeBody(req, &update, nil); err != nil {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprintf(resp, "Request decode failed: %v", err)
return nil, nil
}
@ -400,7 +400,7 @@ func (s *HTTPServer) AgentCheckUpdate(resp http.ResponseWriter, req *http.Reques
case api.HealthWarning:
case api.HealthCritical:
default:
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprintf(resp, "Invalid check status: '%s'", update.Status)
return nil, nil
}
@ -457,14 +457,14 @@ func (s *HTTPServer) AgentRegisterService(resp http.ResponseWriter, req *http.Re
return nil
}
if err := decodeBody(req, &args, decodeCB); err != nil {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprintf(resp, "Request decode failed: %v", err)
return nil, nil
}
// Verify the service has a name.
if args.Name == "" {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprint(resp, "Missing service name")
return nil, nil
}
@ -472,7 +472,7 @@ func (s *HTTPServer) AgentRegisterService(resp http.ResponseWriter, req *http.Re
// Check the service address here and in the catalog RPC endpoint
// since service registration isn't sychronous.
if ipaddr.IsAny(args.Address) {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprintf(resp, "Invalid service address")
return nil, nil
}
@ -484,12 +484,12 @@ func (s *HTTPServer) AgentRegisterService(resp http.ResponseWriter, req *http.Re
chkTypes := args.CheckTypes()
for _, check := range chkTypes {
if check.Status != "" && !structs.ValidStatus(check.Status) {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprint(resp, "Status for checks must 'passing', 'warning', 'critical'")
return nil, nil
}
if !check.Valid() {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprint(resp, invalidCheckMessage)
return nil, nil
}
@ -537,7 +537,7 @@ func (s *HTTPServer) AgentServiceMaintenance(resp http.ResponseWriter, req *http
// Ensure we have a service ID
serviceID := strings.TrimPrefix(req.URL.Path, "/v1/agent/service/maintenance/")
if serviceID == "" {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprint(resp, "Missing service ID")
return nil, nil
}
@ -545,7 +545,7 @@ func (s *HTTPServer) AgentServiceMaintenance(resp http.ResponseWriter, req *http
// Ensure we have some action
params := req.URL.Query()
if _, ok := params["enable"]; !ok {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprint(resp, "Missing value for enable")
return nil, nil
}
@ -553,7 +553,7 @@ func (s *HTTPServer) AgentServiceMaintenance(resp http.ResponseWriter, req *http
raw := params.Get("enable")
enable, err := strconv.ParseBool(raw)
if err != nil {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprintf(resp, "Invalid value for enable: %q", raw)
return nil, nil
}
@ -593,7 +593,7 @@ func (s *HTTPServer) AgentNodeMaintenance(resp http.ResponseWriter, req *http.Re
// Ensure we have some action
params := req.URL.Query()
if _, ok := params["enable"]; !ok {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprint(resp, "Missing value for enable")
return nil, nil
}
@ -601,7 +601,7 @@ func (s *HTTPServer) AgentNodeMaintenance(resp http.ResponseWriter, req *http.Re
raw := params.Get("enable")
enable, err := strconv.ParseBool(raw)
if err != nil {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprintf(resp, "Invalid value for enable: %q", raw)
return nil, nil
}
@ -657,7 +657,7 @@ func (s *HTTPServer) AgentMonitor(resp http.ResponseWriter, req *http.Request) (
filter := logger.LevelFilter()
filter.MinLevel = logutils.LogLevel(logLevel)
if !logger.ValidateLevelFilter(filter.MinLevel, filter) {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprintf(resp, "Unknown log level: %s", filter.MinLevel)
return nil, nil
}

View File

@ -11,7 +11,7 @@ import (
func (s *HTTPServer) CatalogRegister(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
var args structs.RegisterRequest
if err := decodeBody(req, &args, nil); err != nil {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprintf(resp, "Request decode failed: %v", err)
return nil, nil
}
@ -33,7 +33,7 @@ func (s *HTTPServer) CatalogRegister(resp http.ResponseWriter, req *http.Request
func (s *HTTPServer) CatalogDeregister(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
var args structs.DeregisterRequest
if err := decodeBody(req, &args, nil); err != nil {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprintf(resp, "Request decode failed: %v", err)
return nil, nil
}
@ -123,7 +123,7 @@ func (s *HTTPServer) CatalogServiceNodes(resp http.ResponseWriter, req *http.Req
// Pull out the service name
args.ServiceName = strings.TrimPrefix(req.URL.Path, "/v1/catalog/service/")
if args.ServiceName == "" {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprint(resp, "Missing service name")
return nil, nil
}
@ -158,7 +158,7 @@ func (s *HTTPServer) CatalogNodeServices(resp http.ResponseWriter, req *http.Req
// Pull out the node name
args.Node = strings.TrimPrefix(req.URL.Path, "/v1/catalog/node/")
if args.Node == "" {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprint(resp, "Missing node name")
return nil, nil
}

View File

@ -33,7 +33,7 @@ func (s *HTTPServer) EventFire(resp http.ResponseWriter, req *http.Request) (int
event := &UserEvent{}
event.Name = strings.TrimPrefix(req.URL.Path, "/v1/event/fire/")
if event.Name == "" {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprint(resp, "Missing name")
return nil, nil
}

View File

@ -22,7 +22,7 @@ func (s *HTTPServer) HealthChecksInState(resp http.ResponseWriter, req *http.Req
// Pull out the service name
args.State = strings.TrimPrefix(req.URL.Path, "/v1/health/state/")
if args.State == "" {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprint(resp, "Missing check state")
return nil, nil
}
@ -56,7 +56,7 @@ func (s *HTTPServer) HealthNodeChecks(resp http.ResponseWriter, req *http.Reques
// Pull out the service name
args.Node = strings.TrimPrefix(req.URL.Path, "/v1/health/node/")
if args.Node == "" {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprint(resp, "Missing node name")
return nil, nil
}
@ -92,7 +92,7 @@ func (s *HTTPServer) HealthServiceChecks(resp http.ResponseWriter, req *http.Req
// Pull out the service name
args.ServiceName = strings.TrimPrefix(req.URL.Path, "/v1/health/checks/")
if args.ServiceName == "" {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprint(resp, "Missing service name")
return nil, nil
}
@ -135,7 +135,7 @@ func (s *HTTPServer) HealthServiceNodes(resp http.ResponseWriter, req *http.Requ
// Pull out the service name
args.ServiceName = strings.TrimPrefix(req.URL.Path, "/v1/health/service/")
if args.ServiceName == "" {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprint(resp, "Missing service name")
return nil, nil
}
@ -159,7 +159,7 @@ func (s *HTTPServer) HealthServiceNodes(resp http.ResponseWriter, req *http.Requ
var err error
filter, err = strconv.ParseBool(val)
if err != nil {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprint(resp, "Invalid value for ?passing")
return nil, nil
}

View File

@ -257,7 +257,7 @@ func (s *HTTPServer) KVSDelete(resp http.ResponseWriter, req *http.Request, args
// missingKey checks if the key is missing
func missingKey(resp http.ResponseWriter, args *structs.KeyRequest) bool {
if args.Key == "" {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprint(resp, "Missing key name")
return true
}
@ -272,7 +272,7 @@ func conflictingFlags(resp http.ResponseWriter, req *http.Request, flags ...stri
for _, conflict := range flags {
if _, ok := params[conflict]; ok {
if found {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprint(resp, "Conflicting flags: "+params.Encode())
return true
}

View File

@ -90,7 +90,7 @@ func (s *HTTPServer) OperatorKeyringEndpoint(resp http.ResponseWriter, req *http
var args keyringArgs
if req.Method == "POST" || req.Method == "PUT" || req.Method == "DELETE" {
if err := decodeBody(req, &args, nil); err != nil {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprintf(resp, "Request decode failed: %v", err)
return nil, nil
}
@ -101,14 +101,14 @@ func (s *HTTPServer) OperatorKeyringEndpoint(resp http.ResponseWriter, req *http
if relayFactor := req.URL.Query().Get("relay-factor"); relayFactor != "" {
n, err := strconv.Atoi(relayFactor)
if err != nil {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprintf(resp, "Error parsing relay factor: %v", err)
return nil, nil
}
args.RelayFactor, err = ParseRelayFactor(n)
if err != nil {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprintf(resp, "Invalid relay factor: %v", err)
return nil, nil
}
@ -224,7 +224,7 @@ func (s *HTTPServer) OperatorAutopilotConfiguration(resp http.ResponseWriter, re
var conf api.AutopilotConfiguration
if err := decodeBody(req, &conf, FixupConfigDurations); err != nil {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprintf(resp, "Error parsing autopilot config: %v", err)
return nil, nil
}
@ -244,7 +244,7 @@ func (s *HTTPServer) OperatorAutopilotConfiguration(resp http.ResponseWriter, re
if _, ok := params["cas"]; ok {
casVal, err := strconv.ParseUint(params.Get("cas"), 10, 64)
if err != nil {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprintf(resp, "Error parsing cas value: %v", err)
return nil, nil
}

View File

@ -29,7 +29,7 @@ func (s *HTTPServer) preparedQueryCreate(resp http.ResponseWriter, req *http.Req
s.parseToken(req, &args.Token)
if req.ContentLength > 0 {
if err := decodeBody(req, &args.Query, nil); err != nil {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprintf(resp, "Request decode failed: %v", err)
return nil, nil
}
@ -196,7 +196,7 @@ func (s *HTTPServer) preparedQueryUpdate(id string, resp http.ResponseWriter, re
s.parseToken(req, &args.Token)
if req.ContentLength > 0 {
if err := decodeBody(req, &args.Query, nil); err != nil {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprintf(resp, "Request decode failed: %v", err)
return nil, nil
}

View File

@ -50,7 +50,7 @@ func (s *HTTPServer) SessionCreate(resp http.ResponseWriter, req *http.Request)
// Handle optional request body
if req.ContentLength > 0 {
if err := decodeBody(req, &args.Session, FixupLockDelay); err != nil {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprintf(resp, "Request decode failed: %v", err)
return nil, nil
}
@ -122,7 +122,7 @@ func (s *HTTPServer) SessionDestroy(resp http.ResponseWriter, req *http.Request)
// Pull out the session id
args.Session.ID = strings.TrimPrefix(req.URL.Path, "/v1/session/destroy/")
if args.Session.ID == "" {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprint(resp, "Missing session")
return nil, nil
}
@ -150,7 +150,7 @@ func (s *HTTPServer) SessionRenew(resp http.ResponseWriter, req *http.Request) (
// Pull out the session id
args.Session = strings.TrimPrefix(req.URL.Path, "/v1/session/renew/")
if args.Session == "" {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprint(resp, "Missing session")
return nil, nil
}
@ -177,7 +177,7 @@ func (s *HTTPServer) SessionGet(resp http.ResponseWriter, req *http.Request) (in
// Pull out the session id
args.Session = strings.TrimPrefix(req.URL.Path, "/v1/session/info/")
if args.Session == "" {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprint(resp, "Missing session")
return nil, nil
}
@ -225,7 +225,7 @@ func (s *HTTPServer) SessionsForNode(resp http.ResponseWriter, req *http.Request
// Pull out the node name
args.Node = strings.TrimPrefix(req.URL.Path, "/v1/session/node/")
if args.Node == "" {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprint(resp, "Missing node name")
return nil, nil
}

View File

@ -68,7 +68,7 @@ func (s *HTTPServer) UINodeInfo(resp http.ResponseWriter, req *http.Request) (in
// Verify we have some DC, or use the default
args.Node = strings.TrimPrefix(req.URL.Path, "/v1/internal/ui/node/")
if args.Node == "" {
resp.WriteHeader(400)
resp.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprint(resp, "Missing node name")
return nil, nil
}