utils.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package api
  2. import (
  3. "encoding/json"
  4. "io"
  5. "log"
  6. "net/http"
  7. "github.com/smallstep/certificates/errs"
  8. "github.com/smallstep/certificates/logging"
  9. "google.golang.org/protobuf/encoding/protojson"
  10. "google.golang.org/protobuf/proto"
  11. )
  12. // EnableLogger is an interface that enables response logging for an object.
  13. type EnableLogger interface {
  14. ToLog() (interface{}, error)
  15. }
  16. // LogError adds to the response writer the given error if it implements
  17. // logging.ResponseLogger. If it does not implement it, then writes the error
  18. // using the log package.
  19. func LogError(rw http.ResponseWriter, err error) {
  20. if rl, ok := rw.(logging.ResponseLogger); ok {
  21. rl.WithFields(map[string]interface{}{
  22. "error": err,
  23. })
  24. } else {
  25. log.Println(err)
  26. }
  27. }
  28. // LogEnabledResponse log the response object if it implements the EnableLogger
  29. // interface.
  30. func LogEnabledResponse(rw http.ResponseWriter, v interface{}) {
  31. if el, ok := v.(EnableLogger); ok {
  32. out, err := el.ToLog()
  33. if err != nil {
  34. LogError(rw, err)
  35. return
  36. }
  37. if rl, ok := rw.(logging.ResponseLogger); ok {
  38. rl.WithFields(map[string]interface{}{
  39. "response": out,
  40. })
  41. } else {
  42. log.Println(out)
  43. }
  44. }
  45. }
  46. // JSON writes the passed value into the http.ResponseWriter.
  47. func JSON(w http.ResponseWriter, v interface{}) {
  48. JSONStatus(w, v, http.StatusOK)
  49. }
  50. // JSONStatus writes the given value into the http.ResponseWriter and the
  51. // given status is written as the status code of the response.
  52. func JSONStatus(w http.ResponseWriter, v interface{}, status int) {
  53. w.Header().Set("Content-Type", "application/json")
  54. w.WriteHeader(status)
  55. if err := json.NewEncoder(w).Encode(v); err != nil {
  56. LogError(w, err)
  57. return
  58. }
  59. LogEnabledResponse(w, v)
  60. }
  61. // ProtoJSON writes the passed value into the http.ResponseWriter.
  62. func ProtoJSON(w http.ResponseWriter, m proto.Message) {
  63. ProtoJSONStatus(w, m, http.StatusOK)
  64. }
  65. // ProtoJSONStatus writes the given value into the http.ResponseWriter and the
  66. // given status is written as the status code of the response.
  67. func ProtoJSONStatus(w http.ResponseWriter, m proto.Message, status int) {
  68. w.Header().Set("Content-Type", "application/json")
  69. w.WriteHeader(status)
  70. b, err := protojson.Marshal(m)
  71. if err != nil {
  72. LogError(w, err)
  73. return
  74. }
  75. if _, err := w.Write(b); err != nil {
  76. LogError(w, err)
  77. return
  78. }
  79. //LogEnabledResponse(w, v)
  80. }
  81. // ReadJSON reads JSON from the request body and stores it in the value
  82. // pointed by v.
  83. func ReadJSON(r io.Reader, v interface{}) error {
  84. if err := json.NewDecoder(r).Decode(v); err != nil {
  85. return errs.BadRequestErr(err, "error decoding json")
  86. }
  87. return nil
  88. }
  89. // ReadProtoJSON reads JSON from the request body and stores it in the value
  90. // pointed by v.
  91. func ReadProtoJSON(r io.Reader, m proto.Message) error {
  92. data, err := io.ReadAll(r)
  93. if err != nil {
  94. return errs.BadRequestErr(err, "error reading request body")
  95. }
  96. return protojson.Unmarshal(data, m)
  97. }