debug.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package middleware // import "github.com/docker/docker/api/server/middleware"
  2. import (
  3. "bufio"
  4. "encoding/json"
  5. "io"
  6. "net/http"
  7. "strings"
  8. "github.com/docker/docker/api/server/httputils"
  9. "github.com/docker/docker/pkg/ioutils"
  10. "github.com/sirupsen/logrus"
  11. "golang.org/x/net/context"
  12. )
  13. // DebugRequestMiddleware dumps the request to logger
  14. func DebugRequestMiddleware(handler func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error) func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  15. return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  16. logrus.Debugf("Calling %s %s", r.Method, r.RequestURI)
  17. if r.Method != "POST" {
  18. return handler(ctx, w, r, vars)
  19. }
  20. if err := httputils.CheckForJSON(r); err != nil {
  21. return handler(ctx, w, r, vars)
  22. }
  23. maxBodySize := 4096 // 4KB
  24. if r.ContentLength > int64(maxBodySize) {
  25. return handler(ctx, w, r, vars)
  26. }
  27. body := r.Body
  28. bufReader := bufio.NewReaderSize(body, maxBodySize)
  29. r.Body = ioutils.NewReadCloserWrapper(bufReader, func() error { return body.Close() })
  30. b, err := bufReader.Peek(maxBodySize)
  31. if err != io.EOF {
  32. // either there was an error reading, or the buffer is full (in which case the request is too large)
  33. return handler(ctx, w, r, vars)
  34. }
  35. var postForm map[string]interface{}
  36. if err := json.Unmarshal(b, &postForm); err == nil {
  37. maskSecretKeys(postForm, r.RequestURI)
  38. formStr, errMarshal := json.Marshal(postForm)
  39. if errMarshal == nil {
  40. logrus.Debugf("form data: %s", string(formStr))
  41. } else {
  42. logrus.Debugf("form data: %q", postForm)
  43. }
  44. }
  45. return handler(ctx, w, r, vars)
  46. }
  47. }
  48. func maskSecretKeys(inp interface{}, path string) {
  49. // Remove any query string from the path
  50. idx := strings.Index(path, "?")
  51. if idx != -1 {
  52. path = path[:idx]
  53. }
  54. // Remove trailing / characters
  55. path = strings.TrimRight(path, "/")
  56. if arr, ok := inp.([]interface{}); ok {
  57. for _, f := range arr {
  58. maskSecretKeys(f, path)
  59. }
  60. return
  61. }
  62. if form, ok := inp.(map[string]interface{}); ok {
  63. loop0:
  64. for k, v := range form {
  65. for _, m := range []string{"password", "secret", "jointoken", "unlockkey", "signingcakey"} {
  66. if strings.EqualFold(m, k) {
  67. form[k] = "*****"
  68. continue loop0
  69. }
  70. }
  71. maskSecretKeys(v, path)
  72. }
  73. // Route-specific redactions
  74. if strings.HasSuffix(path, "/secrets/create") {
  75. for k := range form {
  76. if k == "Data" {
  77. form[k] = "*****"
  78. }
  79. }
  80. }
  81. }
  82. }