debug.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package middleware // import "github.com/docker/docker/api/server/middleware"
  2. import (
  3. "bufio"
  4. "context"
  5. "encoding/json"
  6. "io"
  7. "net/http"
  8. "strings"
  9. "github.com/containerd/log"
  10. "github.com/docker/docker/api/server/httputils"
  11. "github.com/docker/docker/pkg/ioutils"
  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. log.G(ctx).Debugf("Calling %s %s", r.Method, r.RequestURI)
  17. if r.Method != http.MethodPost {
  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)
  38. formStr, errMarshal := json.Marshal(postForm)
  39. if errMarshal == nil {
  40. log.G(ctx).Debugf("form data: %s", string(formStr))
  41. } else {
  42. log.G(ctx).Debugf("form data: %q", postForm)
  43. }
  44. }
  45. return handler(ctx, w, r, vars)
  46. }
  47. }
  48. func maskSecretKeys(inp interface{}) {
  49. if arr, ok := inp.([]interface{}); ok {
  50. for _, f := range arr {
  51. maskSecretKeys(f)
  52. }
  53. return
  54. }
  55. if form, ok := inp.(map[string]interface{}); ok {
  56. scrub := []string{
  57. // Note: The Data field contains the base64-encoded secret in 'secret'
  58. // and 'config' create and update requests. Currently, no other POST
  59. // API endpoints use a data field, so we scrub this field unconditionally.
  60. // Change this handling to be conditional if a new endpoint is added
  61. // in future where this field should not be scrubbed.
  62. "data",
  63. "jointoken",
  64. "password",
  65. "secret",
  66. "signingcakey",
  67. "unlockkey",
  68. }
  69. loop0:
  70. for k, v := range form {
  71. for _, m := range scrub {
  72. if strings.EqualFold(m, k) {
  73. form[k] = "*****"
  74. continue loop0
  75. }
  76. }
  77. maskSecretKeys(v)
  78. }
  79. }
  80. }