2016-02-24 13:17:43 -05:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2016-06-30 16:15:43 -04:00
|
|
|
"strings"
|
2016-02-24 13:17:43 -05:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/docker/api/server/httputils"
|
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DebugRequestMiddleware dumps the request to logger
|
2016-04-08 19:22:39 -04:00
|
|
|
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 {
|
2016-02-24 13:17:43 -05:00
|
|
|
return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2016-03-07 02:59:48 -05:00
|
|
|
logrus.Debugf("Calling %s %s", r.Method, r.RequestURI)
|
2016-02-24 13:17:43 -05:00
|
|
|
|
|
|
|
if r.Method != "POST" {
|
|
|
|
return handler(ctx, w, r, vars)
|
|
|
|
}
|
|
|
|
if err := httputils.CheckForJSON(r); err != nil {
|
|
|
|
return handler(ctx, w, r, vars)
|
|
|
|
}
|
|
|
|
maxBodySize := 4096 // 4KB
|
|
|
|
if r.ContentLength > int64(maxBodySize) {
|
|
|
|
return handler(ctx, w, r, vars)
|
|
|
|
}
|
|
|
|
|
|
|
|
body := r.Body
|
|
|
|
bufReader := bufio.NewReaderSize(body, maxBodySize)
|
|
|
|
r.Body = ioutils.NewReadCloserWrapper(bufReader, func() error { return body.Close() })
|
|
|
|
|
|
|
|
b, err := bufReader.Peek(maxBodySize)
|
|
|
|
if err != io.EOF {
|
|
|
|
// either there was an error reading, or the buffer is full (in which case the request is too large)
|
|
|
|
return handler(ctx, w, r, vars)
|
|
|
|
}
|
|
|
|
|
|
|
|
var postForm map[string]interface{}
|
|
|
|
if err := json.Unmarshal(b, &postForm); err == nil {
|
2016-06-30 16:15:43 -04:00
|
|
|
maskSecretKeys(postForm)
|
2016-02-24 13:17:43 -05:00
|
|
|
formStr, errMarshal := json.Marshal(postForm)
|
|
|
|
if errMarshal == nil {
|
|
|
|
logrus.Debugf("form data: %s", string(formStr))
|
|
|
|
} else {
|
|
|
|
logrus.Debugf("form data: %q", postForm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return handler(ctx, w, r, vars)
|
|
|
|
}
|
|
|
|
}
|
2016-06-30 16:15:43 -04:00
|
|
|
|
|
|
|
func maskSecretKeys(inp interface{}) {
|
|
|
|
if arr, ok := inp.([]interface{}); ok {
|
|
|
|
for _, f := range arr {
|
|
|
|
maskSecretKeys(f)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if form, ok := inp.(map[string]interface{}); ok {
|
|
|
|
loop0:
|
|
|
|
for k, v := range form {
|
2016-08-02 18:15:23 -04:00
|
|
|
for _, m := range []string{"password", "secret", "jointoken"} {
|
2016-06-30 16:15:43 -04:00
|
|
|
if strings.EqualFold(m, k) {
|
|
|
|
form[k] = "*****"
|
|
|
|
continue loop0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
maskSecretKeys(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|