Removing direct dependencies from the server configuration.
Signed-off-by: David Calavera <david.calavera@gmail.com>
| ... | ... |
@@ -1,195 +1,41 @@ |
| 1 | 1 |
package server |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 |
- "bufio" |
|
| 5 |
- "encoding/json" |
|
| 6 |
- "io" |
|
| 7 |
- "net/http" |
|
| 8 |
- "runtime" |
|
| 9 |
- "strings" |
|
| 10 |
- |
|
| 11 | 4 |
"github.com/Sirupsen/logrus" |
| 12 | 5 |
"github.com/docker/docker/api" |
| 13 | 6 |
"github.com/docker/docker/api/server/httputils" |
| 7 |
+ "github.com/docker/docker/api/server/middleware" |
|
| 14 | 8 |
"github.com/docker/docker/dockerversion" |
| 15 |
- "github.com/docker/docker/errors" |
|
| 16 | 9 |
"github.com/docker/docker/pkg/authorization" |
| 17 |
- "github.com/docker/docker/pkg/ioutils" |
|
| 18 |
- "github.com/docker/docker/pkg/version" |
|
| 19 |
- "golang.org/x/net/context" |
|
| 20 | 10 |
) |
| 21 | 11 |
|
| 22 |
-// middleware is an adapter to allow the use of ordinary functions as Docker API filters. |
|
| 23 |
-// Any function that has the appropriate signature can be register as a middleware. |
|
| 24 |
-type middleware func(handler httputils.APIFunc) httputils.APIFunc |
|
| 25 |
- |
|
| 26 |
-// debugRequestMiddleware dumps the request to logger |
|
| 27 |
-func debugRequestMiddleware(handler httputils.APIFunc) httputils.APIFunc {
|
|
| 28 |
- return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
| 29 |
- logrus.Debugf("%s %s", r.Method, r.RequestURI)
|
|
| 30 |
- |
|
| 31 |
- if r.Method != "POST" {
|
|
| 32 |
- return handler(ctx, w, r, vars) |
|
| 33 |
- } |
|
| 34 |
- if err := httputils.CheckForJSON(r); err != nil {
|
|
| 35 |
- return handler(ctx, w, r, vars) |
|
| 36 |
- } |
|
| 37 |
- maxBodySize := 4096 // 4KB |
|
| 38 |
- if r.ContentLength > int64(maxBodySize) {
|
|
| 39 |
- return handler(ctx, w, r, vars) |
|
| 40 |
- } |
|
| 41 |
- |
|
| 42 |
- body := r.Body |
|
| 43 |
- bufReader := bufio.NewReaderSize(body, maxBodySize) |
|
| 44 |
- r.Body = ioutils.NewReadCloserWrapper(bufReader, func() error { return body.Close() })
|
|
| 45 |
- |
|
| 46 |
- b, err := bufReader.Peek(maxBodySize) |
|
| 47 |
- if err != io.EOF {
|
|
| 48 |
- // either there was an error reading, or the buffer is full (in which case the request is too large) |
|
| 49 |
- return handler(ctx, w, r, vars) |
|
| 50 |
- } |
|
| 51 |
- |
|
| 52 |
- var postForm map[string]interface{}
|
|
| 53 |
- if err := json.Unmarshal(b, &postForm); err == nil {
|
|
| 54 |
- if _, exists := postForm["password"]; exists {
|
|
| 55 |
- postForm["password"] = "*****" |
|
| 56 |
- } |
|
| 57 |
- formStr, errMarshal := json.Marshal(postForm) |
|
| 58 |
- if errMarshal == nil {
|
|
| 59 |
- logrus.Debugf("form data: %s", string(formStr))
|
|
| 60 |
- } else {
|
|
| 61 |
- logrus.Debugf("form data: %q", postForm)
|
|
| 62 |
- } |
|
| 63 |
- } |
|
| 64 |
- |
|
| 65 |
- return handler(ctx, w, r, vars) |
|
| 66 |
- } |
|
| 67 |
-} |
|
| 68 |
- |
|
| 69 |
-// authorizationMiddleware perform authorization on the request. |
|
| 70 |
-func (s *Server) authorizationMiddleware(handler httputils.APIFunc) httputils.APIFunc {
|
|
| 71 |
- return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
| 72 |
- // FIXME: fill when authN gets in |
|
| 73 |
- // User and UserAuthNMethod are taken from AuthN plugins |
|
| 74 |
- // Currently tracked in https://github.com/docker/docker/pull/13994 |
|
| 75 |
- user := "" |
|
| 76 |
- userAuthNMethod := "" |
|
| 77 |
- authCtx := authorization.NewCtx(s.authZPlugins, user, userAuthNMethod, r.Method, r.RequestURI) |
|
| 78 |
- |
|
| 79 |
- if err := authCtx.AuthZRequest(w, r); err != nil {
|
|
| 80 |
- logrus.Errorf("AuthZRequest for %s %s returned error: %s", r.Method, r.RequestURI, err)
|
|
| 81 |
- return err |
|
| 82 |
- } |
|
| 83 |
- |
|
| 84 |
- rw := authorization.NewResponseModifier(w) |
|
| 85 |
- |
|
| 86 |
- if err := handler(ctx, rw, r, vars); err != nil {
|
|
| 87 |
- logrus.Errorf("Handler for %s %s returned error: %s", r.Method, r.RequestURI, err)
|
|
| 88 |
- return err |
|
| 89 |
- } |
|
| 90 |
- |
|
| 91 |
- if err := authCtx.AuthZResponse(rw, r); err != nil {
|
|
| 92 |
- logrus.Errorf("AuthZResponse for %s %s returned error: %s", r.Method, r.RequestURI, err)
|
|
| 93 |
- return err |
|
| 94 |
- } |
|
| 95 |
- return nil |
|
| 96 |
- } |
|
| 97 |
-} |
|
| 98 |
- |
|
| 99 |
-// userAgentMiddleware checks the User-Agent header looking for a valid docker client spec. |
|
| 100 |
-func (s *Server) userAgentMiddleware(handler httputils.APIFunc) httputils.APIFunc {
|
|
| 101 |
- return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
| 102 |
- if strings.Contains(r.Header.Get("User-Agent"), "Docker-Client/") {
|
|
| 103 |
- dockerVersion := version.Version(s.cfg.Version) |
|
| 104 |
- |
|
| 105 |
- userAgent := strings.Split(r.Header.Get("User-Agent"), "/")
|
|
| 106 |
- |
|
| 107 |
- // v1.20 onwards includes the GOOS of the client after the version |
|
| 108 |
- // such as Docker/1.7.0 (linux) |
|
| 109 |
- if len(userAgent) == 2 && strings.Contains(userAgent[1], " ") {
|
|
| 110 |
- userAgent[1] = strings.Split(userAgent[1], " ")[0] |
|
| 111 |
- } |
|
| 112 |
- |
|
| 113 |
- if len(userAgent) == 2 && !dockerVersion.Equal(version.Version(userAgent[1])) {
|
|
| 114 |
- logrus.Debugf("Client and server don't have the same version (client: %s, server: %s)", userAgent[1], dockerVersion)
|
|
| 115 |
- } |
|
| 116 |
- } |
|
| 117 |
- return handler(ctx, w, r, vars) |
|
| 118 |
- } |
|
| 119 |
-} |
|
| 120 |
- |
|
| 121 |
-// corsMiddleware sets the CORS header expectations in the server. |
|
| 122 |
-func (s *Server) corsMiddleware(handler httputils.APIFunc) httputils.APIFunc {
|
|
| 123 |
- return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
| 124 |
- // If "api-cors-header" is not given, but "api-enable-cors" is true, we set cors to "*" |
|
| 125 |
- // otherwise, all head values will be passed to HTTP handler |
|
| 126 |
- corsHeaders := s.cfg.CorsHeaders |
|
| 127 |
- if corsHeaders == "" && s.cfg.EnableCors {
|
|
| 128 |
- corsHeaders = "*" |
|
| 129 |
- } |
|
| 130 |
- |
|
| 131 |
- if corsHeaders != "" {
|
|
| 132 |
- writeCorsHeaders(w, r, corsHeaders) |
|
| 133 |
- } |
|
| 134 |
- return handler(ctx, w, r, vars) |
|
| 135 |
- } |
|
| 136 |
-} |
|
| 137 |
- |
|
| 138 |
-// versionMiddleware checks the api version requirements before passing the request to the server handler. |
|
| 139 |
-func versionMiddleware(handler httputils.APIFunc) httputils.APIFunc {
|
|
| 140 |
- return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
| 141 |
- apiVersion := version.Version(vars["version"]) |
|
| 142 |
- if apiVersion == "" {
|
|
| 143 |
- apiVersion = api.DefaultVersion |
|
| 144 |
- } |
|
| 145 |
- |
|
| 146 |
- if apiVersion.GreaterThan(api.DefaultVersion) {
|
|
| 147 |
- return errors.ErrorCodeNewerClientVersion.WithArgs(apiVersion, api.DefaultVersion) |
|
| 148 |
- } |
|
| 149 |
- if apiVersion.LessThan(api.MinVersion) {
|
|
| 150 |
- return errors.ErrorCodeOldClientVersion.WithArgs(apiVersion, api.MinVersion) |
|
| 151 |
- } |
|
| 152 |
- |
|
| 153 |
- w.Header().Set("Server", "Docker/"+dockerversion.Version+" ("+runtime.GOOS+")")
|
|
| 154 |
- ctx = context.WithValue(ctx, httputils.APIVersionKey, apiVersion) |
|
| 155 |
- return handler(ctx, w, r, vars) |
|
| 156 |
- } |
|
| 157 |
-} |
|
| 158 |
- |
|
| 159 | 12 |
// handleWithGlobalMiddlwares wraps the handler function for a request with |
| 160 | 13 |
// the server's global middlewares. The order of the middlewares is backwards, |
| 161 | 14 |
// meaning that the first in the list will be evaluated last. |
| 162 |
-// |
|
| 163 |
-// Example: handleWithGlobalMiddlewares(s.getContainersName) |
|
| 164 |
-// |
|
| 165 |
-// s.loggingMiddleware( |
|
| 166 |
-// s.userAgentMiddleware( |
|
| 167 |
-// s.corsMiddleware( |
|
| 168 |
-// versionMiddleware(s.getContainersName) |
|
| 169 |
-// ) |
|
| 170 |
-// ) |
|
| 171 |
-// ) |
|
| 172 |
-// ) |
|
| 173 | 15 |
func (s *Server) handleWithGlobalMiddlewares(handler httputils.APIFunc) httputils.APIFunc {
|
| 174 |
- middlewares := []middleware{
|
|
| 175 |
- versionMiddleware, |
|
| 176 |
- s.corsMiddleware, |
|
| 177 |
- s.userAgentMiddleware, |
|
| 16 |
+ next := handler |
|
| 17 |
+ |
|
| 18 |
+ handleVersion := middleware.NewVersionMiddleware(dockerversion.Version, api.DefaultVersion, api.MinVersion) |
|
| 19 |
+ next = handleVersion(next) |
|
| 20 |
+ |
|
| 21 |
+ if s.cfg.EnableCors {
|
|
| 22 |
+ handleCORS := middleware.NewCORSMiddleware(s.cfg.CorsHeaders) |
|
| 23 |
+ next = handleCORS(next) |
|
| 178 | 24 |
} |
| 179 | 25 |
|
| 26 |
+ handleUserAgent := middleware.NewUserAgentMiddleware(s.cfg.Version) |
|
| 27 |
+ next = handleUserAgent(next) |
|
| 28 |
+ |
|
| 180 | 29 |
// Only want this on debug level |
| 181 | 30 |
if s.cfg.Logging && logrus.GetLevel() == logrus.DebugLevel {
|
| 182 |
- middlewares = append(middlewares, debugRequestMiddleware) |
|
| 31 |
+ next = middleware.DebugRequestMiddleware(next) |
|
| 183 | 32 |
} |
| 184 | 33 |
|
| 185 | 34 |
if len(s.cfg.AuthorizationPluginNames) > 0 {
|
| 186 | 35 |
s.authZPlugins = authorization.NewPlugins(s.cfg.AuthorizationPluginNames) |
| 187 |
- middlewares = append(middlewares, s.authorizationMiddleware) |
|
| 36 |
+ handleAuthorization := middleware.NewAuthorizationMiddleware(s.authZPlugins) |
|
| 37 |
+ next = handleAuthorization(next) |
|
| 188 | 38 |
} |
| 189 | 39 |
|
| 190 |
- h := handler |
|
| 191 |
- for _, m := range middlewares {
|
|
| 192 |
- h = m(h) |
|
| 193 |
- } |
|
| 194 |
- return h |
|
| 40 |
+ return next |
|
| 195 | 41 |
} |
| 196 | 42 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,42 @@ |
| 0 |
+package middleware |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "net/http" |
|
| 4 |
+ |
|
| 5 |
+ "github.com/Sirupsen/logrus" |
|
| 6 |
+ "github.com/docker/docker/api/server/httputils" |
|
| 7 |
+ "github.com/docker/docker/pkg/authorization" |
|
| 8 |
+ "golang.org/x/net/context" |
|
| 9 |
+) |
|
| 10 |
+ |
|
| 11 |
+// NewAuthorizationMiddleware creates a new Authorization middleware. |
|
| 12 |
+func NewAuthorizationMiddleware(plugins []authorization.Plugin) Middleware {
|
|
| 13 |
+ return func(handler httputils.APIFunc) httputils.APIFunc {
|
|
| 14 |
+ return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
| 15 |
+ // FIXME: fill when authN gets in |
|
| 16 |
+ // User and UserAuthNMethod are taken from AuthN plugins |
|
| 17 |
+ // Currently tracked in https://github.com/docker/docker/pull/13994 |
|
| 18 |
+ user := "" |
|
| 19 |
+ userAuthNMethod := "" |
|
| 20 |
+ authCtx := authorization.NewCtx(plugins, user, userAuthNMethod, r.Method, r.RequestURI) |
|
| 21 |
+ |
|
| 22 |
+ if err := authCtx.AuthZRequest(w, r); err != nil {
|
|
| 23 |
+ logrus.Errorf("AuthZRequest for %s %s returned error: %s", r.Method, r.RequestURI, err)
|
|
| 24 |
+ return err |
|
| 25 |
+ } |
|
| 26 |
+ |
|
| 27 |
+ rw := authorization.NewResponseModifier(w) |
|
| 28 |
+ |
|
| 29 |
+ if err := handler(ctx, rw, r, vars); err != nil {
|
|
| 30 |
+ logrus.Errorf("Handler for %s %s returned error: %s", r.Method, r.RequestURI, err)
|
|
| 31 |
+ return err |
|
| 32 |
+ } |
|
| 33 |
+ |
|
| 34 |
+ if err := authCtx.AuthZResponse(rw, r); err != nil {
|
|
| 35 |
+ logrus.Errorf("AuthZResponse for %s %s returned error: %s", r.Method, r.RequestURI, err)
|
|
| 36 |
+ return err |
|
| 37 |
+ } |
|
| 38 |
+ return nil |
|
| 39 |
+ } |
|
| 40 |
+ } |
|
| 41 |
+} |
| 0 | 42 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,33 @@ |
| 0 |
+package middleware |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "net/http" |
|
| 4 |
+ |
|
| 5 |
+ "github.com/Sirupsen/logrus" |
|
| 6 |
+ "github.com/docker/docker/api/server/httputils" |
|
| 7 |
+ "golang.org/x/net/context" |
|
| 8 |
+) |
|
| 9 |
+ |
|
| 10 |
+// NewCORSMiddleware creates a new CORS middleware. |
|
| 11 |
+func NewCORSMiddleware(defaultHeaders string) Middleware {
|
|
| 12 |
+ return func(handler httputils.APIFunc) httputils.APIFunc {
|
|
| 13 |
+ return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
| 14 |
+ // If "api-cors-header" is not given, but "api-enable-cors" is true, we set cors to "*" |
|
| 15 |
+ // otherwise, all head values will be passed to HTTP handler |
|
| 16 |
+ corsHeaders := defaultHeaders |
|
| 17 |
+ if corsHeaders == "" {
|
|
| 18 |
+ corsHeaders = "*" |
|
| 19 |
+ } |
|
| 20 |
+ |
|
| 21 |
+ writeCorsHeaders(w, r, corsHeaders) |
|
| 22 |
+ return handler(ctx, w, r, vars) |
|
| 23 |
+ } |
|
| 24 |
+ } |
|
| 25 |
+} |
|
| 26 |
+ |
|
| 27 |
+func writeCorsHeaders(w http.ResponseWriter, r *http.Request, corsHeaders string) {
|
|
| 28 |
+ logrus.Debugf("CORS header is enabled and set to: %s", corsHeaders)
|
|
| 29 |
+ w.Header().Add("Access-Control-Allow-Origin", corsHeaders)
|
|
| 30 |
+ w.Header().Add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, X-Registry-Auth")
|
|
| 31 |
+ w.Header().Add("Access-Control-Allow-Methods", "HEAD, GET, POST, DELETE, PUT, OPTIONS")
|
|
| 32 |
+} |
| 0 | 33 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,56 @@ |
| 0 |
+package middleware |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "bufio" |
|
| 4 |
+ "encoding/json" |
|
| 5 |
+ "io" |
|
| 6 |
+ "net/http" |
|
| 7 |
+ |
|
| 8 |
+ "github.com/Sirupsen/logrus" |
|
| 9 |
+ "github.com/docker/docker/api/server/httputils" |
|
| 10 |
+ "github.com/docker/docker/pkg/ioutils" |
|
| 11 |
+ "golang.org/x/net/context" |
|
| 12 |
+) |
|
| 13 |
+ |
|
| 14 |
+// DebugRequestMiddleware dumps the request to logger |
|
| 15 |
+func DebugRequestMiddleware(handler httputils.APIFunc) httputils.APIFunc {
|
|
| 16 |
+ return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
| 17 |
+ logrus.Debugf("%s %s", r.Method, r.RequestURI)
|
|
| 18 |
+ |
|
| 19 |
+ if r.Method != "POST" {
|
|
| 20 |
+ return handler(ctx, w, r, vars) |
|
| 21 |
+ } |
|
| 22 |
+ if err := httputils.CheckForJSON(r); err != nil {
|
|
| 23 |
+ return handler(ctx, w, r, vars) |
|
| 24 |
+ } |
|
| 25 |
+ maxBodySize := 4096 // 4KB |
|
| 26 |
+ if r.ContentLength > int64(maxBodySize) {
|
|
| 27 |
+ return handler(ctx, w, r, vars) |
|
| 28 |
+ } |
|
| 29 |
+ |
|
| 30 |
+ body := r.Body |
|
| 31 |
+ bufReader := bufio.NewReaderSize(body, maxBodySize) |
|
| 32 |
+ r.Body = ioutils.NewReadCloserWrapper(bufReader, func() error { return body.Close() })
|
|
| 33 |
+ |
|
| 34 |
+ b, err := bufReader.Peek(maxBodySize) |
|
| 35 |
+ if err != io.EOF {
|
|
| 36 |
+ // either there was an error reading, or the buffer is full (in which case the request is too large) |
|
| 37 |
+ return handler(ctx, w, r, vars) |
|
| 38 |
+ } |
|
| 39 |
+ |
|
| 40 |
+ var postForm map[string]interface{}
|
|
| 41 |
+ if err := json.Unmarshal(b, &postForm); err == nil {
|
|
| 42 |
+ if _, exists := postForm["password"]; exists {
|
|
| 43 |
+ postForm["password"] = "*****" |
|
| 44 |
+ } |
|
| 45 |
+ formStr, errMarshal := json.Marshal(postForm) |
|
| 46 |
+ if errMarshal == nil {
|
|
| 47 |
+ logrus.Debugf("form data: %s", string(formStr))
|
|
| 48 |
+ } else {
|
|
| 49 |
+ logrus.Debugf("form data: %q", postForm)
|
|
| 50 |
+ } |
|
| 51 |
+ } |
|
| 52 |
+ |
|
| 53 |
+ return handler(ctx, w, r, vars) |
|
| 54 |
+ } |
|
| 55 |
+} |
| 0 | 56 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,7 @@ |
| 0 |
+package middleware |
|
| 1 |
+ |
|
| 2 |
+import "github.com/docker/docker/api/server/httputils" |
|
| 3 |
+ |
|
| 4 |
+// Middleware is an adapter to allow the use of ordinary functions as Docker API filters. |
|
| 5 |
+// Any function that has the appropriate signature can be register as a middleware. |
|
| 6 |
+type Middleware func(handler httputils.APIFunc) httputils.APIFunc |
| 0 | 7 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,35 @@ |
| 0 |
+package middleware |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "net/http" |
|
| 4 |
+ "strings" |
|
| 5 |
+ |
|
| 6 |
+ "github.com/Sirupsen/logrus" |
|
| 7 |
+ "github.com/docker/docker/api/server/httputils" |
|
| 8 |
+ "github.com/docker/docker/pkg/version" |
|
| 9 |
+ "golang.org/x/net/context" |
|
| 10 |
+) |
|
| 11 |
+ |
|
| 12 |
+// NewUserAgentMiddleware creates a new UserAgent middleware. |
|
| 13 |
+func NewUserAgentMiddleware(versionCheck string) Middleware {
|
|
| 14 |
+ serverVersion := version.Version(versionCheck) |
|
| 15 |
+ |
|
| 16 |
+ return func(handler httputils.APIFunc) httputils.APIFunc {
|
|
| 17 |
+ return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
| 18 |
+ if strings.Contains(r.Header.Get("User-Agent"), "Docker-Client/") {
|
|
| 19 |
+ userAgent := strings.Split(r.Header.Get("User-Agent"), "/")
|
|
| 20 |
+ |
|
| 21 |
+ // v1.20 onwards includes the GOOS of the client after the version |
|
| 22 |
+ // such as Docker/1.7.0 (linux) |
|
| 23 |
+ if len(userAgent) == 2 && strings.Contains(userAgent[1], " ") {
|
|
| 24 |
+ userAgent[1] = strings.Split(userAgent[1], " ")[0] |
|
| 25 |
+ } |
|
| 26 |
+ |
|
| 27 |
+ if len(userAgent) == 2 && !serverVersion.Equal(version.Version(userAgent[1])) {
|
|
| 28 |
+ logrus.Debugf("Client and server don't have the same version (client: %s, server: %s)", userAgent[1], serverVersion)
|
|
| 29 |
+ } |
|
| 30 |
+ } |
|
| 31 |
+ return handler(ctx, w, r, vars) |
|
| 32 |
+ } |
|
| 33 |
+ } |
|
| 34 |
+} |
| 0 | 35 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,38 @@ |
| 0 |
+package middleware |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "fmt" |
|
| 4 |
+ "net/http" |
|
| 5 |
+ "runtime" |
|
| 6 |
+ |
|
| 7 |
+ "github.com/docker/docker/api/server/httputils" |
|
| 8 |
+ "github.com/docker/docker/errors" |
|
| 9 |
+ "github.com/docker/docker/pkg/version" |
|
| 10 |
+ "golang.org/x/net/context" |
|
| 11 |
+) |
|
| 12 |
+ |
|
| 13 |
+// NewVersionMiddleware creates a new Version middleware. |
|
| 14 |
+func NewVersionMiddleware(versionCheck string, defaultVersion, minVersion version.Version) Middleware {
|
|
| 15 |
+ serverVersion := version.Version(versionCheck) |
|
| 16 |
+ |
|
| 17 |
+ return func(handler httputils.APIFunc) httputils.APIFunc {
|
|
| 18 |
+ return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
| 19 |
+ apiVersion := version.Version(vars["version"]) |
|
| 20 |
+ if apiVersion == "" {
|
|
| 21 |
+ apiVersion = defaultVersion |
|
| 22 |
+ } |
|
| 23 |
+ |
|
| 24 |
+ if apiVersion.GreaterThan(defaultVersion) {
|
|
| 25 |
+ return errors.ErrorCodeNewerClientVersion.WithArgs(apiVersion, defaultVersion) |
|
| 26 |
+ } |
|
| 27 |
+ if apiVersion.LessThan(minVersion) {
|
|
| 28 |
+ return errors.ErrorCodeOldClientVersion.WithArgs(apiVersion, minVersion) |
|
| 29 |
+ } |
|
| 30 |
+ |
|
| 31 |
+ header := fmt.Sprintf("Docker/%s (%s)", serverVersion, runtime.GOOS)
|
|
| 32 |
+ w.Header().Set("Server", header)
|
|
| 33 |
+ ctx = context.WithValue(ctx, httputils.APIVersionKey, apiVersion) |
|
| 34 |
+ return handler(ctx, w, r, vars) |
|
| 35 |
+ } |
|
| 36 |
+ } |
|
| 37 |
+} |
| 0 | 38 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,64 @@ |
| 0 |
+package middleware |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "net/http" |
|
| 4 |
+ "net/http/httptest" |
|
| 5 |
+ "strings" |
|
| 6 |
+ "testing" |
|
| 7 |
+ |
|
| 8 |
+ "github.com/docker/docker/api/server/httputils" |
|
| 9 |
+ "github.com/docker/docker/pkg/version" |
|
| 10 |
+ "golang.org/x/net/context" |
|
| 11 |
+) |
|
| 12 |
+ |
|
| 13 |
+func TestVersionMiddleware(t *testing.T) {
|
|
| 14 |
+ handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
| 15 |
+ if httputils.VersionFromContext(ctx) == "" {
|
|
| 16 |
+ t.Fatalf("Expected version, got empty string")
|
|
| 17 |
+ } |
|
| 18 |
+ return nil |
|
| 19 |
+ } |
|
| 20 |
+ |
|
| 21 |
+ defaultVersion := version.Version("1.10.0")
|
|
| 22 |
+ minVersion := version.Version("1.2.0")
|
|
| 23 |
+ m := NewVersionMiddleware(defaultVersion.String(), defaultVersion, minVersion) |
|
| 24 |
+ h := m(handler) |
|
| 25 |
+ |
|
| 26 |
+ req, _ := http.NewRequest("GET", "/containers/json", nil)
|
|
| 27 |
+ resp := httptest.NewRecorder() |
|
| 28 |
+ ctx := context.Background() |
|
| 29 |
+ if err := h(ctx, resp, req, map[string]string{}); err != nil {
|
|
| 30 |
+ t.Fatal(err) |
|
| 31 |
+ } |
|
| 32 |
+} |
|
| 33 |
+ |
|
| 34 |
+func TestVersionMiddlewareWithErrors(t *testing.T) {
|
|
| 35 |
+ handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
| 36 |
+ if httputils.VersionFromContext(ctx) == "" {
|
|
| 37 |
+ t.Fatalf("Expected version, got empty string")
|
|
| 38 |
+ } |
|
| 39 |
+ return nil |
|
| 40 |
+ } |
|
| 41 |
+ |
|
| 42 |
+ defaultVersion := version.Version("1.10.0")
|
|
| 43 |
+ minVersion := version.Version("1.2.0")
|
|
| 44 |
+ m := NewVersionMiddleware(defaultVersion.String(), defaultVersion, minVersion) |
|
| 45 |
+ h := m(handler) |
|
| 46 |
+ |
|
| 47 |
+ req, _ := http.NewRequest("GET", "/containers/json", nil)
|
|
| 48 |
+ resp := httptest.NewRecorder() |
|
| 49 |
+ ctx := context.Background() |
|
| 50 |
+ |
|
| 51 |
+ vars := map[string]string{"version": "0.1"}
|
|
| 52 |
+ err := h(ctx, resp, req, vars) |
|
| 53 |
+ |
|
| 54 |
+ if !strings.Contains(err.Error(), "client version 0.1 is too old. Minimum supported API version is 1.2.0") {
|
|
| 55 |
+ t.Fatalf("Expected ErrorCodeOldClientVersion, got %v", err)
|
|
| 56 |
+ } |
|
| 57 |
+ |
|
| 58 |
+ vars["version"] = "100000" |
|
| 59 |
+ err = h(ctx, resp, req, vars) |
|
| 60 |
+ if !strings.Contains(err.Error(), "client is newer than server") {
|
|
| 61 |
+ t.Fatalf("Expected ErrorCodeNewerClientVersion, got %v", err)
|
|
| 62 |
+ } |
|
| 63 |
+} |
| 0 | 64 |
deleted file mode 100644 |
| ... | ... |
@@ -1,57 +0,0 @@ |
| 1 |
-package server |
|
| 2 |
- |
|
| 3 |
-import ( |
|
| 4 |
- "net/http" |
|
| 5 |
- "net/http/httptest" |
|
| 6 |
- "testing" |
|
| 7 |
- |
|
| 8 |
- "github.com/docker/distribution/registry/api/errcode" |
|
| 9 |
- "github.com/docker/docker/api/server/httputils" |
|
| 10 |
- "github.com/docker/docker/errors" |
|
| 11 |
- "golang.org/x/net/context" |
|
| 12 |
-) |
|
| 13 |
- |
|
| 14 |
-func TestVersionMiddleware(t *testing.T) {
|
|
| 15 |
- handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
| 16 |
- if httputils.VersionFromContext(ctx) == "" {
|
|
| 17 |
- t.Fatalf("Expected version, got empty string")
|
|
| 18 |
- } |
|
| 19 |
- return nil |
|
| 20 |
- } |
|
| 21 |
- |
|
| 22 |
- h := versionMiddleware(handler) |
|
| 23 |
- |
|
| 24 |
- req, _ := http.NewRequest("GET", "/containers/json", nil)
|
|
| 25 |
- resp := httptest.NewRecorder() |
|
| 26 |
- ctx := context.Background() |
|
| 27 |
- if err := h(ctx, resp, req, map[string]string{}); err != nil {
|
|
| 28 |
- t.Fatal(err) |
|
| 29 |
- } |
|
| 30 |
-} |
|
| 31 |
- |
|
| 32 |
-func TestVersionMiddlewareWithErrors(t *testing.T) {
|
|
| 33 |
- handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
| 34 |
- if httputils.VersionFromContext(ctx) == "" {
|
|
| 35 |
- t.Fatalf("Expected version, got empty string")
|
|
| 36 |
- } |
|
| 37 |
- return nil |
|
| 38 |
- } |
|
| 39 |
- |
|
| 40 |
- h := versionMiddleware(handler) |
|
| 41 |
- |
|
| 42 |
- req, _ := http.NewRequest("GET", "/containers/json", nil)
|
|
| 43 |
- resp := httptest.NewRecorder() |
|
| 44 |
- ctx := context.Background() |
|
| 45 |
- |
|
| 46 |
- vars := map[string]string{"version": "0.1"}
|
|
| 47 |
- err := h(ctx, resp, req, vars) |
|
| 48 |
- if derr, ok := err.(errcode.Error); !ok || derr.ErrorCode() != errors.ErrorCodeOldClientVersion {
|
|
| 49 |
- t.Fatalf("Expected ErrorCodeOldClientVersion, got %v", err)
|
|
| 50 |
- } |
|
| 51 |
- |
|
| 52 |
- vars["version"] = "100000" |
|
| 53 |
- err = h(ctx, resp, req, vars) |
|
| 54 |
- if derr, ok := err.(errcode.Error); !ok || derr.ErrorCode() != errors.ErrorCodeNewerClientVersion {
|
|
| 55 |
- t.Fatalf("Expected ErrorCodeNewerClientVersion, got %v", err)
|
|
| 56 |
- } |
|
| 57 |
-} |
| ... | ... |
@@ -113,13 +113,6 @@ func (s *HTTPServer) Close() error {
|
| 113 | 113 |
return s.l.Close() |
| 114 | 114 |
} |
| 115 | 115 |
|
| 116 |
-func writeCorsHeaders(w http.ResponseWriter, r *http.Request, corsHeaders string) {
|
|
| 117 |
- logrus.Debugf("CORS header is enabled and set to: %s", corsHeaders)
|
|
| 118 |
- w.Header().Add("Access-Control-Allow-Origin", corsHeaders)
|
|
| 119 |
- w.Header().Add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, X-Registry-Auth")
|
|
| 120 |
- w.Header().Add("Access-Control-Allow-Methods", "HEAD, GET, POST, DELETE, PUT, OPTIONS")
|
|
| 121 |
-} |
|
| 122 |
- |
|
| 123 | 116 |
func (s *Server) makeHTTPHandler(handler httputils.APIFunc) http.HandlerFunc {
|
| 124 | 117 |
return func(w http.ResponseWriter, r *http.Request) {
|
| 125 | 118 |
// log the handler call |