2019-03-14 13:36:18 +00:00
|
|
|
// Copyright 2019 Sorint.lab
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2019-04-08 09:35:45 +00:00
|
|
|
"encoding/json"
|
2019-03-14 13:36:18 +00:00
|
|
|
"net/http"
|
2019-03-14 13:36:18 +00:00
|
|
|
"net/url"
|
2019-03-14 13:36:18 +00:00
|
|
|
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/services/types"
|
|
|
|
"agola.io/agola/internal/util"
|
2019-05-23 09:23:14 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
errors "golang.org/x/xerrors"
|
2019-03-14 13:36:18 +00:00
|
|
|
)
|
|
|
|
|
2019-04-08 09:35:45 +00:00
|
|
|
type ErrorResponse struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func ErrorResponseFromError(err error) *ErrorResponse {
|
2019-05-23 10:59:11 +00:00
|
|
|
var aerr error
|
|
|
|
// use inner errors if of these types
|
2019-04-09 12:53:00 +00:00
|
|
|
switch {
|
2019-05-23 10:59:11 +00:00
|
|
|
case errors.Is(err, &util.ErrBadRequest{}):
|
|
|
|
var cerr *util.ErrBadRequest
|
|
|
|
errors.As(err, &cerr)
|
|
|
|
aerr = cerr
|
|
|
|
case errors.Is(err, &util.ErrNotFound{}):
|
|
|
|
var cerr *util.ErrNotFound
|
|
|
|
errors.As(err, &cerr)
|
|
|
|
aerr = cerr
|
|
|
|
case errors.Is(err, &util.ErrForbidden{}):
|
|
|
|
var cerr *util.ErrForbidden
|
|
|
|
errors.As(err, &cerr)
|
|
|
|
aerr = cerr
|
|
|
|
case errors.Is(err, &util.ErrUnauthorized{}):
|
|
|
|
var cerr *util.ErrUnauthorized
|
|
|
|
errors.As(err, &cerr)
|
|
|
|
aerr = cerr
|
2019-06-11 08:59:21 +00:00
|
|
|
case errors.Is(err, &util.ErrInternal{}):
|
|
|
|
var cerr *util.ErrInternal
|
|
|
|
errors.As(err, &cerr)
|
|
|
|
aerr = cerr
|
2019-05-23 10:59:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if aerr != nil {
|
|
|
|
return &ErrorResponse{Message: aerr.Error()}
|
2019-04-08 09:35:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// on generic error return an generic message to not leak the real error
|
|
|
|
return &ErrorResponse{Message: "internal server error"}
|
|
|
|
}
|
|
|
|
|
2019-03-14 13:36:18 +00:00
|
|
|
func httpError(w http.ResponseWriter, err error) bool {
|
2019-04-09 12:53:00 +00:00
|
|
|
if err == nil {
|
|
|
|
return false
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 12:53:00 +00:00
|
|
|
response := ErrorResponseFromError(err)
|
|
|
|
resj, merr := json.Marshal(response)
|
|
|
|
if merr != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
switch {
|
2019-05-23 10:59:11 +00:00
|
|
|
case errors.Is(err, &util.ErrBadRequest{}):
|
2019-04-09 12:53:00 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2019-07-02 13:35:14 +00:00
|
|
|
_, _ = w.Write(resj)
|
2019-05-23 10:59:11 +00:00
|
|
|
case errors.Is(err, &util.ErrNotFound{}):
|
2019-04-09 12:53:00 +00:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2019-07-02 13:35:14 +00:00
|
|
|
_, _ = w.Write(resj)
|
2019-05-23 10:59:11 +00:00
|
|
|
case errors.Is(err, &util.ErrForbidden{}):
|
2019-05-03 21:18:51 +00:00
|
|
|
w.WriteHeader(http.StatusForbidden)
|
2019-07-02 13:35:14 +00:00
|
|
|
_, _ = w.Write(resj)
|
2019-05-23 10:59:11 +00:00
|
|
|
case errors.Is(err, &util.ErrUnauthorized{}):
|
2019-05-13 12:23:41 +00:00
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
2019-07-02 13:35:14 +00:00
|
|
|
_, _ = w.Write(resj)
|
2019-06-11 08:59:21 +00:00
|
|
|
case errors.Is(err, &util.ErrInternal{}):
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2019-07-02 13:35:14 +00:00
|
|
|
_, _ = w.Write(resj)
|
2019-04-09 12:53:00 +00:00
|
|
|
default:
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2019-07-02 13:35:14 +00:00
|
|
|
_, _ = w.Write(resj)
|
2019-04-09 12:53:00 +00:00
|
|
|
}
|
|
|
|
return true
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
2019-03-14 13:36:18 +00:00
|
|
|
|
2019-04-08 09:35:45 +00:00
|
|
|
func httpResponse(w http.ResponseWriter, code int, res interface{}) error {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
if res != nil {
|
|
|
|
resj, err := json.Marshal(res)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.WriteHeader(code)
|
|
|
|
_, err = w.Write(resj)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(code)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-09 12:53:00 +00:00
|
|
|
func httpErrorFromRemote(w http.ResponseWriter, resp *http.Response, err error) bool {
|
|
|
|
if err != nil {
|
|
|
|
// on generic error return an generic message to not leak the real error
|
|
|
|
response := &ErrorResponse{Message: "internal server error"}
|
|
|
|
if resp != nil {
|
|
|
|
response = &ErrorResponse{Message: err.Error()}
|
|
|
|
}
|
|
|
|
resj, merr := json.Marshal(response)
|
|
|
|
if merr != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
w.WriteHeader(resp.StatusCode)
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
2019-07-02 13:35:14 +00:00
|
|
|
_, _ = w.Write(resj)
|
2019-04-09 12:53:00 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-03-14 13:36:18 +00:00
|
|
|
func GetConfigTypeRef(r *http.Request) (types.ConfigType, string, error) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
projectRef, err := url.PathUnescape(vars["projectref"])
|
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return "", "", util.NewErrBadRequest(errors.Errorf("wrong projectref %q: %w", vars["projectref"], err))
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
if projectRef != "" {
|
|
|
|
return types.ConfigTypeProject, projectRef, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
projectGroupRef, err := url.PathUnescape(vars["projectgroupref"])
|
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return "", "", util.NewErrBadRequest(errors.Errorf("wrong projectgroupref %q: %w", vars["projectgroupref"], err))
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
if projectGroupRef != "" {
|
|
|
|
return types.ConfigTypeProjectGroup, projectGroupRef, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", "", util.NewErrBadRequest(errors.Errorf("cannot get project or projectgroup ref"))
|
|
|
|
}
|