2018-02-05 16:05:59 -05:00
|
|
|
package middleware // import "github.com/docker/docker/api/server/middleware"
|
2015-09-15 19:01:49 -04:00
|
|
|
|
|
|
|
import (
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2015-09-15 19:01:49 -04:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2017-10-10 06:40:15 -04:00
|
|
|
"runtime"
|
2015-09-15 19:01:49 -04:00
|
|
|
"testing"
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
"github.com/docker/docker/api/server/httputils"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2015-09-15 19:01:49 -04:00
|
|
|
)
|
|
|
|
|
2018-01-15 05:50:30 -05:00
|
|
|
func TestVersionMiddlewareVersion(t *testing.T) {
|
2016-04-19 10:56:54 -04:00
|
|
|
defaultVersion := "1.10.0"
|
|
|
|
minVersion := "1.2.0"
|
2018-01-15 05:50:30 -05:00
|
|
|
expectedVersion := defaultVersion
|
2015-09-15 19:01:49 -04:00
|
|
|
handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2018-01-15 05:50:30 -05:00
|
|
|
v := httputils.VersionFromContext(ctx)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(expectedVersion, v))
|
2015-09-15 19:01:49 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-08 19:22:39 -04:00
|
|
|
m := NewVersionMiddleware(defaultVersion, defaultVersion, minVersion)
|
|
|
|
h := m.WrapHandler(handler)
|
2015-09-15 19:01:49 -04:00
|
|
|
|
2019-10-12 14:40:19 -04:00
|
|
|
req, _ := http.NewRequest(http.MethodGet, "/containers/json", nil)
|
2015-09-15 19:01:49 -04:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-01-15 05:50:30 -05:00
|
|
|
tests := []struct {
|
|
|
|
reqVersion string
|
|
|
|
expectedVersion string
|
|
|
|
errString string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
expectedVersion: "1.10.0",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
reqVersion: "1.9.0",
|
|
|
|
expectedVersion: "1.9.0",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
reqVersion: "0.1",
|
|
|
|
errString: "client version 0.1 is too old. Minimum supported API version is 1.2.0, please upgrade your client to a newer version",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
reqVersion: "9999.9999",
|
|
|
|
errString: "client version 9999.9999 is too new. Maximum supported API version is 1.10.0",
|
|
|
|
},
|
2015-09-15 19:01:49 -04:00
|
|
|
}
|
Return 400 error if API client is too new
Commit e98e4a71110fd33852bb755a9b8b4ebc9df904db implemented API version
negotiation using the `/_ping` endpoint. In that change, URL validation for the
maximum supported API version was removed from the API server (validation for
the _minimum_ version was kept in place).
With this feature, clients that support version negotiation would negotiate the
maximum version supported by the daemon, and downgrade to an older API version
if the client's default API version is not supported.
However, clients that do _not_ support version negotiation can call API versions
that are higher than the maximum supported version. Due to the missing version
check, this is silently ignored, and the daemon's default API version is used.
This is a problem, because the actual API version in use is non-deterministic;
for example, calling `/v9999.9999/version` on a daemon that runs API v1.34 will
use API v1.34, but calling the same URL on an older daemon may use API version
v1.24.
This patch reverts the removal of the API check for maximum supported versions.
The documentation has been updated accordingly
Before this patch is applied, the daemon returns a 200 (success):
$ curl -v --unix-socket /var/run/docker.sock http://localhost/v9999.9999/version
* Trying /var/run/docker.sock...
* Connected to localhost (/Users/sebastiaan/Library/Containers/com.dock) port 80 (#0)
> GET /v9999.9999/version HTTP/1.1
> Host: localhost
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Api-Version: 1.32
< Content-Length: 240
< Content-Type: application/json
< Date: Tue, 10 Oct 2017 09:11:29 GMT
< Docker-Experimental: true
< Ostype: linux
< Server: Docker/17.09.0-ce (linux)
<
{"Version":"17.09.0-ce","ApiVersion":"1.32","MinAPIVersion":"1.12","GitCommit":"afdb6d4","GoVersion":"go1.8.3","Os":"linux","Arch":"amd64","KernelVersion":"4.9.49-moby","Experimental":true,"BuildTime":"2017-09-26T22:45:38.000000000+00:00"}
* Connection #0 to host localhost left intact
After this patch is applied, a 400 (Bad Request) is returned:
$ curl -v --unix-socket /var/run/docker.sock http://localhost/v9999.9999/version
* Trying /var/run/docker.sock...
* Connected to localhost (/var/run/docker.sock) port 80 (#0)
> GET /v9999.9999/info HTTP/1.1
> Host: localhost
> User-Agent: curl/7.52.1
> Accept: */*
>
< HTTP/1.1 400 Bad Request
< Content-Type: application/json
< Date: Tue, 10 Oct 2017 08:08:34 GMT
< Content-Length: 89
<
{"message":"client version 9999.9999 is too new. Maximim supported API version is 1.34"}
* Curl_http_done: called premature == 0
* Connection #0 to host localhost left intact
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2017-10-10 04:28:46 -04:00
|
|
|
|
2018-01-15 05:50:30 -05:00
|
|
|
for _, test := range tests {
|
|
|
|
expectedVersion = test.expectedVersion
|
Return 400 error if API client is too new
Commit e98e4a71110fd33852bb755a9b8b4ebc9df904db implemented API version
negotiation using the `/_ping` endpoint. In that change, URL validation for the
maximum supported API version was removed from the API server (validation for
the _minimum_ version was kept in place).
With this feature, clients that support version negotiation would negotiate the
maximum version supported by the daemon, and downgrade to an older API version
if the client's default API version is not supported.
However, clients that do _not_ support version negotiation can call API versions
that are higher than the maximum supported version. Due to the missing version
check, this is silently ignored, and the daemon's default API version is used.
This is a problem, because the actual API version in use is non-deterministic;
for example, calling `/v9999.9999/version` on a daemon that runs API v1.34 will
use API v1.34, but calling the same URL on an older daemon may use API version
v1.24.
This patch reverts the removal of the API check for maximum supported versions.
The documentation has been updated accordingly
Before this patch is applied, the daemon returns a 200 (success):
$ curl -v --unix-socket /var/run/docker.sock http://localhost/v9999.9999/version
* Trying /var/run/docker.sock...
* Connected to localhost (/Users/sebastiaan/Library/Containers/com.dock) port 80 (#0)
> GET /v9999.9999/version HTTP/1.1
> Host: localhost
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Api-Version: 1.32
< Content-Length: 240
< Content-Type: application/json
< Date: Tue, 10 Oct 2017 09:11:29 GMT
< Docker-Experimental: true
< Ostype: linux
< Server: Docker/17.09.0-ce (linux)
<
{"Version":"17.09.0-ce","ApiVersion":"1.32","MinAPIVersion":"1.12","GitCommit":"afdb6d4","GoVersion":"go1.8.3","Os":"linux","Arch":"amd64","KernelVersion":"4.9.49-moby","Experimental":true,"BuildTime":"2017-09-26T22:45:38.000000000+00:00"}
* Connection #0 to host localhost left intact
After this patch is applied, a 400 (Bad Request) is returned:
$ curl -v --unix-socket /var/run/docker.sock http://localhost/v9999.9999/version
* Trying /var/run/docker.sock...
* Connected to localhost (/var/run/docker.sock) port 80 (#0)
> GET /v9999.9999/info HTTP/1.1
> Host: localhost
> User-Agent: curl/7.52.1
> Accept: */*
>
< HTTP/1.1 400 Bad Request
< Content-Type: application/json
< Date: Tue, 10 Oct 2017 08:08:34 GMT
< Content-Length: 89
<
{"message":"client version 9999.9999 is too new. Maximim supported API version is 1.34"}
* Curl_http_done: called premature == 0
* Connection #0 to host localhost left intact
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2017-10-10 04:28:46 -04:00
|
|
|
|
2018-01-15 05:50:30 -05:00
|
|
|
err := h(ctx, resp, req, map[string]string{"version": test.reqVersion})
|
Return 400 error if API client is too new
Commit e98e4a71110fd33852bb755a9b8b4ebc9df904db implemented API version
negotiation using the `/_ping` endpoint. In that change, URL validation for the
maximum supported API version was removed from the API server (validation for
the _minimum_ version was kept in place).
With this feature, clients that support version negotiation would negotiate the
maximum version supported by the daemon, and downgrade to an older API version
if the client's default API version is not supported.
However, clients that do _not_ support version negotiation can call API versions
that are higher than the maximum supported version. Due to the missing version
check, this is silently ignored, and the daemon's default API version is used.
This is a problem, because the actual API version in use is non-deterministic;
for example, calling `/v9999.9999/version` on a daemon that runs API v1.34 will
use API v1.34, but calling the same URL on an older daemon may use API version
v1.24.
This patch reverts the removal of the API check for maximum supported versions.
The documentation has been updated accordingly
Before this patch is applied, the daemon returns a 200 (success):
$ curl -v --unix-socket /var/run/docker.sock http://localhost/v9999.9999/version
* Trying /var/run/docker.sock...
* Connected to localhost (/Users/sebastiaan/Library/Containers/com.dock) port 80 (#0)
> GET /v9999.9999/version HTTP/1.1
> Host: localhost
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Api-Version: 1.32
< Content-Length: 240
< Content-Type: application/json
< Date: Tue, 10 Oct 2017 09:11:29 GMT
< Docker-Experimental: true
< Ostype: linux
< Server: Docker/17.09.0-ce (linux)
<
{"Version":"17.09.0-ce","ApiVersion":"1.32","MinAPIVersion":"1.12","GitCommit":"afdb6d4","GoVersion":"go1.8.3","Os":"linux","Arch":"amd64","KernelVersion":"4.9.49-moby","Experimental":true,"BuildTime":"2017-09-26T22:45:38.000000000+00:00"}
* Connection #0 to host localhost left intact
After this patch is applied, a 400 (Bad Request) is returned:
$ curl -v --unix-socket /var/run/docker.sock http://localhost/v9999.9999/version
* Trying /var/run/docker.sock...
* Connected to localhost (/var/run/docker.sock) port 80 (#0)
> GET /v9999.9999/info HTTP/1.1
> Host: localhost
> User-Agent: curl/7.52.1
> Accept: */*
>
< HTTP/1.1 400 Bad Request
< Content-Type: application/json
< Date: Tue, 10 Oct 2017 08:08:34 GMT
< Content-Length: 89
<
{"message":"client version 9999.9999 is too new. Maximim supported API version is 1.34"}
* Curl_http_done: called premature == 0
* Connection #0 to host localhost left intact
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2017-10-10 04:28:46 -04:00
|
|
|
|
2018-01-15 05:50:30 -05:00
|
|
|
if test.errString != "" {
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Error(err, test.errString))
|
2018-01-15 05:50:30 -05:00
|
|
|
} else {
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err)
|
2018-01-15 05:50:30 -05:00
|
|
|
}
|
Return 400 error if API client is too new
Commit e98e4a71110fd33852bb755a9b8b4ebc9df904db implemented API version
negotiation using the `/_ping` endpoint. In that change, URL validation for the
maximum supported API version was removed from the API server (validation for
the _minimum_ version was kept in place).
With this feature, clients that support version negotiation would negotiate the
maximum version supported by the daemon, and downgrade to an older API version
if the client's default API version is not supported.
However, clients that do _not_ support version negotiation can call API versions
that are higher than the maximum supported version. Due to the missing version
check, this is silently ignored, and the daemon's default API version is used.
This is a problem, because the actual API version in use is non-deterministic;
for example, calling `/v9999.9999/version` on a daemon that runs API v1.34 will
use API v1.34, but calling the same URL on an older daemon may use API version
v1.24.
This patch reverts the removal of the API check for maximum supported versions.
The documentation has been updated accordingly
Before this patch is applied, the daemon returns a 200 (success):
$ curl -v --unix-socket /var/run/docker.sock http://localhost/v9999.9999/version
* Trying /var/run/docker.sock...
* Connected to localhost (/Users/sebastiaan/Library/Containers/com.dock) port 80 (#0)
> GET /v9999.9999/version HTTP/1.1
> Host: localhost
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Api-Version: 1.32
< Content-Length: 240
< Content-Type: application/json
< Date: Tue, 10 Oct 2017 09:11:29 GMT
< Docker-Experimental: true
< Ostype: linux
< Server: Docker/17.09.0-ce (linux)
<
{"Version":"17.09.0-ce","ApiVersion":"1.32","MinAPIVersion":"1.12","GitCommit":"afdb6d4","GoVersion":"go1.8.3","Os":"linux","Arch":"amd64","KernelVersion":"4.9.49-moby","Experimental":true,"BuildTime":"2017-09-26T22:45:38.000000000+00:00"}
* Connection #0 to host localhost left intact
After this patch is applied, a 400 (Bad Request) is returned:
$ curl -v --unix-socket /var/run/docker.sock http://localhost/v9999.9999/version
* Trying /var/run/docker.sock...
* Connected to localhost (/var/run/docker.sock) port 80 (#0)
> GET /v9999.9999/info HTTP/1.1
> Host: localhost
> User-Agent: curl/7.52.1
> Accept: */*
>
< HTTP/1.1 400 Bad Request
< Content-Type: application/json
< Date: Tue, 10 Oct 2017 08:08:34 GMT
< Content-Length: 89
<
{"message":"client version 9999.9999 is too new. Maximim supported API version is 1.34"}
* Curl_http_done: called premature == 0
* Connection #0 to host localhost left intact
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2017-10-10 04:28:46 -04:00
|
|
|
}
|
|
|
|
}
|
2017-10-10 06:40:15 -04:00
|
|
|
|
|
|
|
func TestVersionMiddlewareWithErrorsReturnsHeaders(t *testing.T) {
|
|
|
|
handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2018-01-15 05:50:30 -05:00
|
|
|
v := httputils.VersionFromContext(ctx)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, len(v) != 0)
|
2017-10-10 06:40:15 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultVersion := "1.10.0"
|
|
|
|
minVersion := "1.2.0"
|
|
|
|
m := NewVersionMiddleware(defaultVersion, defaultVersion, minVersion)
|
|
|
|
h := m.WrapHandler(handler)
|
|
|
|
|
2019-10-12 14:40:19 -04:00
|
|
|
req, _ := http.NewRequest(http.MethodGet, "/containers/json", nil)
|
2017-10-10 06:40:15 -04:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
vars := map[string]string{"version": "0.1"}
|
|
|
|
err := h(ctx, resp, req, vars)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.ErrorContains(err, ""))
|
2018-01-15 05:50:30 -05:00
|
|
|
|
2017-10-10 06:40:15 -04:00
|
|
|
hdr := resp.Result().Header
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Contains(hdr.Get("Server"), "Docker/"+defaultVersion))
|
|
|
|
assert.Check(t, is.Contains(hdr.Get("Server"), runtime.GOOS))
|
|
|
|
assert.Check(t, is.Equal(hdr.Get("API-Version"), defaultVersion))
|
|
|
|
assert.Check(t, is.Equal(hdr.Get("OSType"), runtime.GOOS))
|
2017-10-10 06:40:15 -04:00
|
|
|
}
|