version.go 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package middleware // import "github.com/docker/docker/api/server/middleware"
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "runtime"
  7. "github.com/docker/docker/api"
  8. "github.com/docker/docker/api/server/httputils"
  9. "github.com/docker/docker/api/types/versions"
  10. )
  11. // VersionMiddleware is a middleware that
  12. // validates the client and server versions.
  13. type VersionMiddleware struct {
  14. serverVersion string
  15. // defaultAPIVersion is the default API version provided by the API server,
  16. // specified as "major.minor". It is usually configured to the latest API
  17. // version [github.com/docker/docker/api.DefaultVersion].
  18. //
  19. // API requests for API versions greater than this version are rejected by
  20. // the server and produce a [versionUnsupportedError].
  21. defaultAPIVersion string
  22. // minAPIVersion is the minimum API version provided by the API server,
  23. // specified as "major.minor".
  24. //
  25. // API requests for API versions lower than this version are rejected by
  26. // the server and produce a [versionUnsupportedError].
  27. minAPIVersion string
  28. }
  29. // NewVersionMiddleware creates a VersionMiddleware with the given versions.
  30. func NewVersionMiddleware(serverVersion, defaultAPIVersion, minAPIVersion string) (*VersionMiddleware, error) {
  31. if versions.LessThan(defaultAPIVersion, api.MinSupportedAPIVersion) || versions.GreaterThan(defaultAPIVersion, api.DefaultVersion) {
  32. return nil, fmt.Errorf("invalid default API version (%s): must be between %s and %s", defaultAPIVersion, api.MinSupportedAPIVersion, api.DefaultVersion)
  33. }
  34. if versions.LessThan(minAPIVersion, api.MinSupportedAPIVersion) || versions.GreaterThan(minAPIVersion, api.DefaultVersion) {
  35. return nil, fmt.Errorf("invalid minimum API version (%s): must be between %s and %s", minAPIVersion, api.MinSupportedAPIVersion, api.DefaultVersion)
  36. }
  37. if versions.GreaterThan(minAPIVersion, defaultAPIVersion) {
  38. return nil, fmt.Errorf("invalid API version: the minimum API version (%s) is higher than the default version (%s)", minAPIVersion, defaultAPIVersion)
  39. }
  40. return &VersionMiddleware{
  41. serverVersion: serverVersion,
  42. defaultAPIVersion: defaultAPIVersion,
  43. minAPIVersion: minAPIVersion,
  44. }, nil
  45. }
  46. type versionUnsupportedError struct {
  47. version, minVersion, maxVersion string
  48. }
  49. func (e versionUnsupportedError) Error() string {
  50. if e.minVersion != "" {
  51. return fmt.Sprintf("client version %s is too old. Minimum supported API version is %s, please upgrade your client to a newer version", e.version, e.minVersion)
  52. }
  53. return fmt.Sprintf("client version %s is too new. Maximum supported API version is %s", e.version, e.maxVersion)
  54. }
  55. func (e versionUnsupportedError) InvalidParameter() {}
  56. // WrapHandler returns a new handler function wrapping the previous one in the request chain.
  57. func (v VersionMiddleware) WrapHandler(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 {
  58. return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  59. w.Header().Set("Server", fmt.Sprintf("Docker/%s (%s)", v.serverVersion, runtime.GOOS))
  60. w.Header().Set("API-Version", v.defaultAPIVersion)
  61. w.Header().Set("OSType", runtime.GOOS)
  62. apiVersion := vars["version"]
  63. if apiVersion == "" {
  64. apiVersion = v.defaultAPIVersion
  65. }
  66. if versions.LessThan(apiVersion, v.minAPIVersion) {
  67. return versionUnsupportedError{version: apiVersion, minVersion: v.minAPIVersion}
  68. }
  69. if versions.GreaterThan(apiVersion, v.defaultAPIVersion) {
  70. return versionUnsupportedError{version: apiVersion, maxVersion: v.defaultAPIVersion}
  71. }
  72. ctx = context.WithValue(ctx, httputils.APIVersionKey{}, apiVersion)
  73. return handler(ctx, w, r, vars)
  74. }
  75. }