2016-03-08 21:18:53 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-06-12 08:47:27 -04:00
|
|
|
"io/ioutil"
|
2016-03-08 21:18:53 -05:00
|
|
|
"net/http"
|
2017-06-12 08:47:27 -04:00
|
|
|
"os"
|
2016-03-08 21:18:53 -05:00
|
|
|
"regexp"
|
|
|
|
|
2018-04-13 04:45:34 -04:00
|
|
|
"github.com/docker/docker/internal/test/registry"
|
2016-03-08 21:18:53 -05:00
|
|
|
"github.com/go-check/check"
|
|
|
|
)
|
|
|
|
|
|
|
|
// unescapeBackslashSemicolonParens unescapes \;()
|
|
|
|
func unescapeBackslashSemicolonParens(s string) string {
|
2016-03-18 17:42:40 -04:00
|
|
|
re := regexp.MustCompile(`\\;`)
|
2016-03-08 21:18:53 -05:00
|
|
|
ret := re.ReplaceAll([]byte(s), []byte(";"))
|
|
|
|
|
2016-03-18 17:42:40 -04:00
|
|
|
re = regexp.MustCompile(`\\\(`)
|
2016-03-08 21:18:53 -05:00
|
|
|
ret = re.ReplaceAll([]byte(ret), []byte("("))
|
|
|
|
|
2016-03-18 17:42:40 -04:00
|
|
|
re = regexp.MustCompile(`\\\)`)
|
2016-03-08 21:18:53 -05:00
|
|
|
ret = re.ReplaceAll([]byte(ret), []byte(")"))
|
|
|
|
|
2016-03-18 17:42:40 -04:00
|
|
|
re = regexp.MustCompile(`\\\\`)
|
|
|
|
ret = re.ReplaceAll([]byte(ret), []byte(`\`))
|
2016-03-08 21:18:53 -05:00
|
|
|
|
|
|
|
return string(ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
func regexpCheckUA(c *check.C, ua string) {
|
|
|
|
re := regexp.MustCompile("(?P<dockerUA>.+) UpstreamClient(?P<upstreamUA>.+)")
|
|
|
|
substrArr := re.FindStringSubmatch(ua)
|
|
|
|
|
|
|
|
c.Assert(substrArr, check.HasLen, 3, check.Commentf("Expected 'UpstreamClient()' with upstream client UA"))
|
|
|
|
dockerUA := substrArr[1]
|
|
|
|
upstreamUAEscaped := substrArr[2]
|
|
|
|
|
|
|
|
// check dockerUA looks correct
|
|
|
|
reDockerUA := regexp.MustCompile("^docker/[0-9A-Za-z+]")
|
|
|
|
bMatchDockerUA := reDockerUA.MatchString(dockerUA)
|
|
|
|
c.Assert(bMatchDockerUA, check.Equals, true, check.Commentf("Docker Engine User-Agent malformed"))
|
|
|
|
|
|
|
|
// check upstreamUA looks correct
|
|
|
|
// Expecting something like: Docker-Client/1.11.0-dev (linux)
|
|
|
|
upstreamUA := unescapeBackslashSemicolonParens(upstreamUAEscaped)
|
|
|
|
reUpstreamUA := regexp.MustCompile("^\\(Docker-Client/[0-9A-Za-z+]")
|
|
|
|
bMatchUpstreamUA := reUpstreamUA.MatchString(upstreamUA)
|
|
|
|
c.Assert(bMatchUpstreamUA, check.Equals, true, check.Commentf("(Upstream) Docker Client User-Agent malformed"))
|
|
|
|
}
|
|
|
|
|
2017-06-12 09:02:51 -04:00
|
|
|
// registerUserAgentHandler registers a handler for the `/v2/*` endpoint.
|
|
|
|
// Note that a 404 is returned to prevent the client to proceed.
|
|
|
|
// We are only checking if the client sent a valid User Agent string along
|
|
|
|
// with the request.
|
2016-12-30 13:10:04 -05:00
|
|
|
func registerUserAgentHandler(reg *registry.Mock, result *string) {
|
|
|
|
reg.RegisterHandler("/v2/", func(w http.ResponseWriter, r *http.Request) {
|
2016-03-08 21:18:53 -05:00
|
|
|
w.WriteHeader(404)
|
2017-06-12 09:02:51 -04:00
|
|
|
w.Write([]byte(`{"errors":[{"code": "UNSUPPORTED","message": "this is a mock registry"}]}`))
|
2016-03-08 21:18:53 -05:00
|
|
|
var ua string
|
|
|
|
for k, v := range r.Header {
|
|
|
|
if k == "User-Agent" {
|
|
|
|
ua = v[0]
|
|
|
|
}
|
|
|
|
}
|
2016-03-18 17:42:40 -04:00
|
|
|
*result = ua
|
2016-03-08 21:18:53 -05:00
|
|
|
})
|
2016-03-18 17:42:40 -04:00
|
|
|
}
|
|
|
|
|
2016-08-07 23:28:44 -04:00
|
|
|
// TestUserAgentPassThrough verifies that when an image is pulled from
|
2016-03-18 17:42:40 -04:00
|
|
|
// a registry, the registry should see a User-Agent string of the form
|
2017-01-16 23:45:27 -05:00
|
|
|
// [docker engine UA] UpstreamClientSTREAM-CLIENT([client UA])
|
2016-03-18 17:42:40 -04:00
|
|
|
func (s *DockerRegistrySuite) TestUserAgentPassThrough(c *check.C) {
|
2017-06-12 09:02:51 -04:00
|
|
|
var ua string
|
2016-03-08 21:18:53 -05:00
|
|
|
|
2017-06-12 09:02:51 -04:00
|
|
|
reg, err := registry.NewMock(c)
|
|
|
|
defer reg.Close()
|
2016-03-18 17:42:40 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2017-06-12 09:02:51 -04:00
|
|
|
registerUserAgentHandler(reg, &ua)
|
|
|
|
repoName := fmt.Sprintf("%s/busybox", reg.URL())
|
2016-03-08 21:18:53 -05:00
|
|
|
|
2017-06-12 09:02:51 -04:00
|
|
|
s.d.StartWithBusybox(c, "--insecure-registry", reg.URL())
|
2016-03-18 17:42:40 -04:00
|
|
|
|
2017-06-12 08:47:27 -04:00
|
|
|
tmp, err := ioutil.TempDir("", "integration-cli-")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
2017-06-12 09:02:51 -04:00
|
|
|
dockerfile, err := makefile(tmp, fmt.Sprintf("FROM %s", repoName))
|
2016-03-18 17:42:40 -04:00
|
|
|
c.Assert(err, check.IsNil, check.Commentf("Unable to create test dockerfile"))
|
2017-06-12 08:47:27 -04:00
|
|
|
|
2017-06-12 09:02:51 -04:00
|
|
|
s.d.Cmd("build", "--file", dockerfile, tmp)
|
|
|
|
regexpCheckUA(c, ua)
|
2016-03-08 21:18:53 -05:00
|
|
|
|
2017-06-12 09:02:51 -04:00
|
|
|
s.d.Cmd("login", "-u", "richard", "-p", "testtest", reg.URL())
|
|
|
|
regexpCheckUA(c, ua)
|
2016-03-18 17:42:40 -04:00
|
|
|
|
2017-06-12 09:02:51 -04:00
|
|
|
s.d.Cmd("pull", repoName)
|
|
|
|
regexpCheckUA(c, ua)
|
2016-03-08 21:18:53 -05:00
|
|
|
|
2017-06-12 09:02:51 -04:00
|
|
|
s.d.Cmd("tag", "busybox", repoName)
|
|
|
|
s.d.Cmd("push", repoName)
|
|
|
|
regexpCheckUA(c, ua)
|
2016-03-08 21:18:53 -05:00
|
|
|
}
|