Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e5ded9c

Browse files
committed
Merge pull request moby#13997 from runcom/drop-old-client
Error out if client API version is too old
2 parents 7313a9a + 910322a commit e5ded9c

6 files changed

Lines changed: 52 additions & 11 deletions

File tree

api/client/hijack.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (cli *DockerCli) hijack(method, path string, setRawTerminal bool, in io.Rea
138138
if err != nil {
139139
return err
140140
}
141-
req, err := http.NewRequest(method, fmt.Sprintf("/v%s%s", api.APIVERSION, path), params)
141+
req, err := http.NewRequest(method, fmt.Sprintf("/v%s%s", api.Version, path), params)
142142
if err != nil {
143143
return err
144144
}

api/client/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (cli *DockerCli) clientRequest(method, path string, in io.Reader, headers m
5353
if expectedPayload && in == nil {
5454
in = bytes.NewReader([]byte{})
5555
}
56-
req, err := http.NewRequest(method, fmt.Sprintf("/v%s%s", api.APIVERSION, path), in)
56+
req, err := http.NewRequest(method, fmt.Sprintf("/v%s%s", api.Version, path), in)
5757
if err != nil {
5858
return nil, nil, -1, err
5959
}

api/client/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (cli *DockerCli) CmdVersion(args ...string) error {
2626
if dockerversion.VERSION != "" {
2727
fmt.Fprintf(cli.out, "Client version: %s\n", dockerversion.VERSION)
2828
}
29-
fmt.Fprintf(cli.out, "Client API version: %s\n", api.APIVERSION)
29+
fmt.Fprintf(cli.out, "Client API version: %s\n", api.Version)
3030
fmt.Fprintf(cli.out, "Go version (client): %s\n", runtime.Version())
3131
if dockerversion.GITCOMMIT != "" {
3232
fmt.Fprintf(cli.out, "Git commit (client): %s\n", dockerversion.GITCOMMIT)

api/common.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@ import (
1616

1717
// Common constants for daemon and client.
1818
const (
19-
APIVERSION version.Version = "1.20" // Current REST API version
20-
DefaultDockerfileName string = "Dockerfile" // Default filename with Docker commands, read by docker build
19+
// Current REST API version
20+
Version version.Version = "1.20"
21+
22+
// Minimun REST API version supported
23+
MinVersion version.Version = "1.12"
24+
25+
// Default filename with Docker commands, read by docker build
26+
DefaultDockerfileName string = "Dockerfile"
2127
)
2228

2329
type ByPrivatePort []types.Port

api/server/server.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ func (s *Server) postAuth(version version.Version, w http.ResponseWriter, r *htt
244244
func (s *Server) getVersion(version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
245245
v := &types.Version{
246246
Version: dockerversion.VERSION,
247-
ApiVersion: api.APIVERSION,
247+
ApiVersion: api.Version,
248248
GitCommit: dockerversion.GITCOMMIT,
249249
GoVersion: runtime.Version(),
250250
Os: runtime.GOOS,
@@ -1477,14 +1477,18 @@ func makeHttpHandler(logging bool, localMethod string, localRoute string, handle
14771477
}
14781478
version := version.Version(mux.Vars(r)["version"])
14791479
if version == "" {
1480-
version = api.APIVERSION
1480+
version = api.Version
14811481
}
14821482
if corsHeaders != "" {
14831483
writeCorsHeaders(w, r, corsHeaders)
14841484
}
14851485

1486-
if version.GreaterThan(api.APIVERSION) {
1487-
http.Error(w, fmt.Errorf("client is newer than server (client API version: %s, server API version: %s)", version, api.APIVERSION).Error(), http.StatusBadRequest)
1486+
if version.GreaterThan(api.Version) {
1487+
http.Error(w, fmt.Errorf("client is newer than server (client API version: %s, server API version: %s)", version, api.Version).Error(), http.StatusBadRequest)
1488+
return
1489+
}
1490+
if version.LessThan(api.MinVersion) {
1491+
http.Error(w, fmt.Errorf("client is too old, minimum supported API version is %s, please upgrade your client to a newer version", api.MinVersion).Error(), http.StatusBadRequest)
14881492
return
14891493
}
14901494

integration-cli/docker_api_test.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ package main
33
import (
44
"net/http"
55
"net/http/httputil"
6+
"strconv"
7+
"strings"
68
"time"
79

10+
"github.com/docker/docker/api"
811
"github.com/go-check/check"
912
)
1013

1114
func (s *DockerSuite) TestApiOptionsRoute(c *check.C) {
1215
status, _, err := sockRequest("OPTIONS", "/", nil)
13-
c.Assert(status, check.Equals, http.StatusOK)
1416
c.Assert(err, check.IsNil)
17+
c.Assert(status, check.Equals, http.StatusOK)
1518
}
1619

1720
func (s *DockerSuite) TestApiGetEnabledCors(c *check.C) {
@@ -26,7 +29,7 @@ func (s *DockerSuite) TestApiGetEnabledCors(c *check.C) {
2629
//c.Assert(res.Header.Get("Access-Control-Allow-Headers"), check.Equals, "Origin, X-Requested-With, Content-Type, Accept, X-Registry-Auth")
2730
}
2831

29-
func (s *DockerSuite) TestVersionStatusCode(c *check.C) {
32+
func (s *DockerSuite) TestApiVersionStatusCode(c *check.C) {
3033
conn, err := sockConn(time.Duration(10 * time.Second))
3134
c.Assert(err, check.IsNil)
3235

@@ -40,3 +43,31 @@ func (s *DockerSuite) TestVersionStatusCode(c *check.C) {
4043
res, err := client.Do(req)
4144
c.Assert(res.StatusCode, check.Equals, http.StatusBadRequest)
4245
}
46+
47+
func (s *DockerSuite) TestApiClientVersionNewerThanServer(c *check.C) {
48+
v := strings.Split(string(api.Version), ".")
49+
vMinInt, err := strconv.Atoi(v[1])
50+
c.Assert(err, check.IsNil)
51+
vMinInt++
52+
v[1] = strconv.Itoa(vMinInt)
53+
version := strings.Join(v, ".")
54+
55+
status, body, err := sockRequest("GET", "/v"+version+"/version", nil)
56+
c.Assert(err, check.IsNil)
57+
c.Assert(status, check.Equals, http.StatusBadRequest)
58+
c.Assert(len(string(body)), check.Not(check.Equals), 0) // Expected not empty body
59+
}
60+
61+
func (s *DockerSuite) TestApiClientVersionOldNotSupported(c *check.C) {
62+
v := strings.Split(string(api.MinVersion), ".")
63+
vMinInt, err := strconv.Atoi(v[1])
64+
c.Assert(err, check.IsNil)
65+
vMinInt--
66+
v[1] = strconv.Itoa(vMinInt)
67+
version := strings.Join(v, ".")
68+
69+
status, body, err := sockRequest("GET", "/v"+version+"/version", nil)
70+
c.Assert(err, check.IsNil)
71+
c.Assert(status, check.Equals, http.StatusBadRequest)
72+
c.Assert(len(string(body)), check.Not(check.Equals), 0) // Expected not empty body
73+
}

0 commit comments

Comments
 (0)