smallstep-certificates/api/utils.go

111 lines
2.9 KiB
Go
Raw Normal View History

2018-10-05 21:48:36 +00:00
package api
import (
"encoding/json"
"io"
"io/ioutil"
2018-10-05 21:48:36 +00:00
"log"
"net/http"
"github.com/smallstep/certificates/errs"
"github.com/smallstep/certificates/logging"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
2018-10-05 21:48:36 +00:00
)
2019-05-27 00:41:10 +00:00
// EnableLogger is an interface that enables response logging for an object.
type EnableLogger interface {
ToLog() (interface{}, error)
}
2018-10-05 21:48:36 +00:00
// LogError adds to the response writer the given error if it implements
// logging.ResponseLogger. If it does not implement it, then writes the error
// using the log package.
func LogError(rw http.ResponseWriter, err error) {
if rl, ok := rw.(logging.ResponseLogger); ok {
rl.WithFields(map[string]interface{}{
"error": err,
})
} else {
log.Println(err)
}
}
2019-05-27 00:41:10 +00:00
// LogEnabledResponse log the response object if it implements the EnableLogger
// interface.
func LogEnabledResponse(rw http.ResponseWriter, v interface{}) {
if el, ok := v.(EnableLogger); ok {
out, err := el.ToLog()
if err != nil {
LogError(rw, err)
return
}
if rl, ok := rw.(logging.ResponseLogger); ok {
rl.WithFields(map[string]interface{}{
"response": out,
})
} else {
log.Println(out)
}
}
}
2018-10-05 21:48:36 +00:00
// JSON writes the passed value into the http.ResponseWriter.
func JSON(w http.ResponseWriter, v interface{}) {
2019-05-27 00:41:10 +00:00
JSONStatus(w, v, http.StatusOK)
}
// JSONStatus writes the given value into the http.ResponseWriter and the
// given status is written as the status code of the response.
func JSONStatus(w http.ResponseWriter, v interface{}, status int) {
2018-10-05 21:48:36 +00:00
w.Header().Set("Content-Type", "application/json")
2019-05-27 00:41:10 +00:00
w.WriteHeader(status)
2018-10-05 21:48:36 +00:00
if err := json.NewEncoder(w).Encode(v); err != nil {
LogError(w, err)
2019-05-27 00:41:10 +00:00
return
2018-10-05 21:48:36 +00:00
}
2019-05-27 00:41:10 +00:00
LogEnabledResponse(w, v)
2018-10-05 21:48:36 +00:00
}
// ProtoJSON writes the passed value into the http.ResponseWriter.
func ProtoJSON(w http.ResponseWriter, m proto.Message) {
ProtoJSONStatus(w, m, http.StatusOK)
}
// ProtoJSONStatus writes the given value into the http.ResponseWriter and the
// given status is written as the status code of the response.
func ProtoJSONStatus(w http.ResponseWriter, m proto.Message, status int) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
b, err := protojson.Marshal(m)
if err != nil {
LogError(w, err)
return
}
if _, err := w.Write(b); err != nil {
LogError(w, err)
return
}
//LogEnabledResponse(w, v)
}
2018-10-05 21:48:36 +00:00
// ReadJSON reads JSON from the request body and stores it in the value
// pointed by v.
func ReadJSON(r io.Reader, v interface{}) error {
if err := json.NewDecoder(r).Decode(v); err != nil {
2020-01-24 06:04:34 +00:00
return errs.Wrap(http.StatusBadRequest, err, "error decoding json")
2018-10-05 21:48:36 +00:00
}
return nil
}
// ReadProtoJSON reads JSON from the request body and stores it in the value
// pointed by v.
func ReadProtoJSON(r io.Reader, m proto.Message) error {
data, err := ioutil.ReadAll(r)
if err != nil {
return errs.Wrap(http.StatusBadRequest, err, "error reading request body")
}
return protojson.Unmarshal(data, m)
}