2015-09-15 19:01:49 -04:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2015-10-22 10:55:23 -04:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
2015-09-15 19:01:49 -04:00
|
|
|
"net/http"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/docker/api"
|
2015-09-23 19:42:08 -04:00
|
|
|
"github.com/docker/docker/api/server/httputils"
|
2015-11-09 13:32:46 -05:00
|
|
|
"github.com/docker/docker/dockerversion"
|
2015-09-15 19:01:49 -04:00
|
|
|
"github.com/docker/docker/errors"
|
2015-11-12 06:06:47 -05:00
|
|
|
"github.com/docker/docker/pkg/authorization"
|
2015-09-15 19:01:49 -04:00
|
|
|
"github.com/docker/docker/pkg/version"
|
2015-09-29 17:32:07 -04:00
|
|
|
"golang.org/x/net/context"
|
2015-09-15 19:01:49 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// middleware is an adapter to allow the use of ordinary functions as Docker API filters.
|
|
|
|
// Any function that has the appropriate signature can be register as a middleware.
|
2015-09-23 19:42:08 -04:00
|
|
|
type middleware func(handler httputils.APIFunc) httputils.APIFunc
|
2015-09-15 19:01:49 -04:00
|
|
|
|
2015-10-22 10:55:23 -04:00
|
|
|
// debugRequestMiddleware dumps the request to logger
|
2015-12-01 14:33:33 -05:00
|
|
|
func debugRequestMiddleware(handler httputils.APIFunc) httputils.APIFunc {
|
2015-10-22 10:55:23 -04:00
|
|
|
return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-12-01 14:33:33 -05:00
|
|
|
logrus.Debugf("%s %s", r.Method, r.RequestURI)
|
|
|
|
|
|
|
|
if r.Method == "POST" {
|
2015-10-22 10:55:23 -04:00
|
|
|
if err := httputils.CheckForJSON(r); err == nil {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if _, err := buf.ReadFrom(r.Body); err == nil {
|
|
|
|
r.Body.Close()
|
|
|
|
r.Body = ioutil.NopCloser(&buf)
|
|
|
|
var postForm map[string]interface{}
|
|
|
|
if err := json.Unmarshal(buf.Bytes(), &postForm); err == nil {
|
|
|
|
if _, exists := postForm["password"]; exists {
|
|
|
|
postForm["password"] = "*****"
|
|
|
|
}
|
2015-12-17 02:55:39 -05:00
|
|
|
formStr, errMarshal := json.Marshal(postForm)
|
|
|
|
if errMarshal == nil {
|
|
|
|
logrus.Debugf("form data: %s", string(formStr))
|
|
|
|
} else {
|
|
|
|
logrus.Debugf("form data: %q", postForm)
|
|
|
|
}
|
2015-10-22 10:55:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-01 14:33:33 -05:00
|
|
|
|
2015-10-22 10:55:23 -04:00
|
|
|
return handler(ctx, w, r, vars)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 06:06:47 -05:00
|
|
|
// authorizationMiddleware perform authorization on the request.
|
|
|
|
func (s *Server) authorizationMiddleware(handler httputils.APIFunc) httputils.APIFunc {
|
|
|
|
return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-12-18 06:34:19 -05:00
|
|
|
// FIXME: fill when authN gets in
|
2015-11-12 06:06:47 -05:00
|
|
|
// User and UserAuthNMethod are taken from AuthN plugins
|
|
|
|
// Currently tracked in https://github.com/docker/docker/pull/13994
|
|
|
|
user := ""
|
|
|
|
userAuthNMethod := ""
|
|
|
|
authCtx := authorization.NewCtx(s.authZPlugins, user, userAuthNMethod, r.Method, r.RequestURI)
|
|
|
|
|
|
|
|
if err := authCtx.AuthZRequest(w, r); err != nil {
|
|
|
|
logrus.Errorf("AuthZRequest for %s %s returned error: %s", r.Method, r.RequestURI, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rw := authorization.NewResponseModifier(w)
|
|
|
|
|
|
|
|
if err := handler(ctx, rw, r, vars); err != nil {
|
|
|
|
logrus.Errorf("Handler for %s %s returned error: %s", r.Method, r.RequestURI, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := authCtx.AuthZResponse(rw, r); err != nil {
|
|
|
|
logrus.Errorf("AuthZResponse for %s %s returned error: %s", r.Method, r.RequestURI, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-15 19:01:49 -04:00
|
|
|
// userAgentMiddleware checks the User-Agent header looking for a valid docker client spec.
|
2015-09-23 19:42:08 -04:00
|
|
|
func (s *Server) userAgentMiddleware(handler httputils.APIFunc) httputils.APIFunc {
|
2015-09-15 19:01:49 -04:00
|
|
|
return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
if strings.Contains(r.Header.Get("User-Agent"), "Docker-Client/") {
|
|
|
|
dockerVersion := version.Version(s.cfg.Version)
|
|
|
|
|
|
|
|
userAgent := strings.Split(r.Header.Get("User-Agent"), "/")
|
|
|
|
|
|
|
|
// v1.20 onwards includes the GOOS of the client after the version
|
|
|
|
// such as Docker/1.7.0 (linux)
|
|
|
|
if len(userAgent) == 2 && strings.Contains(userAgent[1], " ") {
|
|
|
|
userAgent[1] = strings.Split(userAgent[1], " ")[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(userAgent) == 2 && !dockerVersion.Equal(version.Version(userAgent[1])) {
|
|
|
|
logrus.Debugf("Warning: client and server don't have the same version (client: %s, server: %s)", userAgent[1], dockerVersion)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return handler(ctx, w, r, vars)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// corsMiddleware sets the CORS header expectations in the server.
|
2015-09-23 19:42:08 -04:00
|
|
|
func (s *Server) corsMiddleware(handler httputils.APIFunc) httputils.APIFunc {
|
2015-09-15 19:01:49 -04:00
|
|
|
return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
// If "api-cors-header" is not given, but "api-enable-cors" is true, we set cors to "*"
|
|
|
|
// otherwise, all head values will be passed to HTTP handler
|
|
|
|
corsHeaders := s.cfg.CorsHeaders
|
|
|
|
if corsHeaders == "" && s.cfg.EnableCors {
|
|
|
|
corsHeaders = "*"
|
|
|
|
}
|
|
|
|
|
|
|
|
if corsHeaders != "" {
|
|
|
|
writeCorsHeaders(w, r, corsHeaders)
|
|
|
|
}
|
|
|
|
return handler(ctx, w, r, vars)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// versionMiddleware checks the api version requirements before passing the request to the server handler.
|
2015-09-23 19:42:08 -04:00
|
|
|
func versionMiddleware(handler httputils.APIFunc) httputils.APIFunc {
|
2015-09-15 19:01:49 -04:00
|
|
|
return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
apiVersion := version.Version(vars["version"])
|
|
|
|
if apiVersion == "" {
|
2015-08-31 17:45:27 -04:00
|
|
|
apiVersion = api.DefaultVersion
|
2015-09-15 19:01:49 -04:00
|
|
|
}
|
|
|
|
|
2015-08-31 17:45:27 -04:00
|
|
|
if apiVersion.GreaterThan(api.DefaultVersion) {
|
|
|
|
return errors.ErrorCodeNewerClientVersion.WithArgs(apiVersion, api.DefaultVersion)
|
2015-09-15 19:01:49 -04:00
|
|
|
}
|
|
|
|
if apiVersion.LessThan(api.MinVersion) {
|
2015-08-31 17:45:27 -04:00
|
|
|
return errors.ErrorCodeOldClientVersion.WithArgs(apiVersion, api.DefaultVersion)
|
2015-09-15 19:01:49 -04:00
|
|
|
}
|
|
|
|
|
2015-11-09 13:32:46 -05:00
|
|
|
w.Header().Set("Server", "Docker/"+dockerversion.Version+" ("+runtime.GOOS+")")
|
2015-09-23 19:42:08 -04:00
|
|
|
ctx = context.WithValue(ctx, httputils.APIVersionKey, apiVersion)
|
2015-09-15 19:01:49 -04:00
|
|
|
return handler(ctx, w, r, vars)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleWithGlobalMiddlwares wraps the handler function for a request with
|
|
|
|
// the server's global middlewares. The order of the middlewares is backwards,
|
2015-12-13 11:00:39 -05:00
|
|
|
// meaning that the first in the list will be evaluated last.
|
2015-09-15 19:01:49 -04:00
|
|
|
//
|
|
|
|
// Example: handleWithGlobalMiddlewares(s.getContainersName)
|
|
|
|
//
|
|
|
|
// s.loggingMiddleware(
|
|
|
|
// s.userAgentMiddleware(
|
|
|
|
// s.corsMiddleware(
|
|
|
|
// versionMiddleware(s.getContainersName)
|
|
|
|
// )
|
|
|
|
// )
|
|
|
|
// )
|
2015-09-23 19:42:08 -04:00
|
|
|
// )
|
|
|
|
func (s *Server) handleWithGlobalMiddlewares(handler httputils.APIFunc) httputils.APIFunc {
|
2015-09-15 19:01:49 -04:00
|
|
|
middlewares := []middleware{
|
|
|
|
versionMiddleware,
|
|
|
|
s.corsMiddleware,
|
|
|
|
s.userAgentMiddleware,
|
|
|
|
}
|
|
|
|
|
2015-10-22 10:55:23 -04:00
|
|
|
// Only want this on debug level
|
2015-12-01 14:33:33 -05:00
|
|
|
if s.cfg.Logging && logrus.GetLevel() == logrus.DebugLevel {
|
|
|
|
middlewares = append(middlewares, debugRequestMiddleware)
|
2015-10-22 10:55:23 -04:00
|
|
|
}
|
|
|
|
|
2015-11-12 06:06:47 -05:00
|
|
|
if len(s.cfg.AuthZPluginNames) > 0 {
|
|
|
|
s.authZPlugins = authorization.NewPlugins(s.cfg.AuthZPluginNames)
|
|
|
|
middlewares = append(middlewares, s.authorizationMiddleware)
|
|
|
|
}
|
|
|
|
|
2015-09-15 19:01:49 -04:00
|
|
|
h := handler
|
|
|
|
for _, m := range middlewares {
|
|
|
|
h = m(h)
|
|
|
|
}
|
|
|
|
return h
|
|
|
|
}
|