2016-03-09 02:18:53 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
|
2016-12-30 18:10:04 +00:00
|
|
|
"github.com/docker/docker/integration-cli/registry"
|
2016-03-09 02:18:53 +00:00
|
|
|
"github.com/go-check/check"
|
|
|
|
)
|
|
|
|
|
|
|
|
// unescapeBackslashSemicolonParens unescapes \;()
|
|
|
|
func unescapeBackslashSemicolonParens(s string) string {
|
2016-03-18 21:42:40 +00:00
|
|
|
re := regexp.MustCompile(`\\;`)
|
2016-03-09 02:18:53 +00:00
|
|
|
ret := re.ReplaceAll([]byte(s), []byte(";"))
|
|
|
|
|
2016-03-18 21:42:40 +00:00
|
|
|
re = regexp.MustCompile(`\\\(`)
|
2016-03-09 02:18:53 +00:00
|
|
|
ret = re.ReplaceAll([]byte(ret), []byte("("))
|
|
|
|
|
2016-03-18 21:42:40 +00:00
|
|
|
re = regexp.MustCompile(`\\\)`)
|
2016-03-09 02:18:53 +00:00
|
|
|
ret = re.ReplaceAll([]byte(ret), []byte(")"))
|
|
|
|
|
2016-03-18 21:42:40 +00:00
|
|
|
re = regexp.MustCompile(`\\\\`)
|
|
|
|
ret = re.ReplaceAll([]byte(ret), []byte(`\`))
|
2016-03-09 02:18:53 +00: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"))
|
|
|
|
}
|
|
|
|
|
2016-12-30 18:10:04 +00:00
|
|
|
func registerUserAgentHandler(reg *registry.Mock, result *string) {
|
|
|
|
reg.RegisterHandler("/v2/", func(w http.ResponseWriter, r *http.Request) {
|
2016-03-09 02:18:53 +00:00
|
|
|
w.WriteHeader(404)
|
|
|
|
var ua string
|
|
|
|
for k, v := range r.Header {
|
|
|
|
if k == "User-Agent" {
|
|
|
|
ua = v[0]
|
|
|
|
}
|
|
|
|
}
|
2016-03-18 21:42:40 +00:00
|
|
|
*result = ua
|
2016-03-09 02:18:53 +00:00
|
|
|
})
|
2016-03-18 21:42:40 +00:00
|
|
|
}
|
|
|
|
|
2016-08-08 03:28:44 +00:00
|
|
|
// TestUserAgentPassThrough verifies that when an image is pulled from
|
2016-03-18 21:42:40 +00:00
|
|
|
// a registry, the registry should see a User-Agent string of the form
|
2016-08-08 03:28:44 +00:00
|
|
|
// [docker engine UA] UptreamClientSTREAM-CLIENT([client UA])
|
2016-03-18 21:42:40 +00:00
|
|
|
func (s *DockerRegistrySuite) TestUserAgentPassThrough(c *check.C) {
|
|
|
|
var (
|
|
|
|
buildUA string
|
|
|
|
pullUA string
|
|
|
|
pushUA string
|
|
|
|
loginUA string
|
|
|
|
)
|
|
|
|
|
2016-12-30 18:10:04 +00:00
|
|
|
buildReg, err := registry.NewMock(c)
|
2017-01-11 20:38:52 +00:00
|
|
|
defer buildReg.Close()
|
2016-03-18 21:42:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
registerUserAgentHandler(buildReg, &buildUA)
|
2016-12-30 18:10:04 +00:00
|
|
|
buildRepoName := fmt.Sprintf("%s/busybox", buildReg.URL())
|
2016-03-09 02:18:53 +00:00
|
|
|
|
2016-12-30 18:10:04 +00:00
|
|
|
pullReg, err := registry.NewMock(c)
|
2017-01-11 20:38:52 +00:00
|
|
|
defer pullReg.Close()
|
2016-03-09 02:18:53 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2016-03-18 21:42:40 +00:00
|
|
|
registerUserAgentHandler(pullReg, &pullUA)
|
2016-12-30 18:10:04 +00:00
|
|
|
pullRepoName := fmt.Sprintf("%s/busybox", pullReg.URL())
|
2016-03-09 02:18:53 +00:00
|
|
|
|
2016-12-30 18:10:04 +00:00
|
|
|
pushReg, err := registry.NewMock(c)
|
2017-01-11 20:38:52 +00:00
|
|
|
defer pushReg.Close()
|
2016-03-18 21:42:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
registerUserAgentHandler(pushReg, &pushUA)
|
2016-12-30 18:10:04 +00:00
|
|
|
pushRepoName := fmt.Sprintf("%s/busybox", pushReg.URL())
|
2016-03-09 02:18:53 +00:00
|
|
|
|
2016-12-30 18:10:04 +00:00
|
|
|
loginReg, err := registry.NewMock(c)
|
2017-01-11 20:38:52 +00:00
|
|
|
defer loginReg.Close()
|
2016-03-18 21:42:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
registerUserAgentHandler(loginReg, &loginUA)
|
|
|
|
|
2016-12-09 22:20:14 +00:00
|
|
|
s.d.Start(c,
|
2016-12-30 18:10:04 +00:00
|
|
|
"--insecure-registry", buildReg.URL(),
|
|
|
|
"--insecure-registry", pullReg.URL(),
|
|
|
|
"--insecure-registry", pushReg.URL(),
|
|
|
|
"--insecure-registry", loginReg.URL(),
|
2016-03-18 21:42:40 +00:00
|
|
|
"--disable-legacy-registry=true")
|
|
|
|
|
|
|
|
dockerfileName, cleanup1, err := makefile(fmt.Sprintf("FROM %s", buildRepoName))
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf("Unable to create test dockerfile"))
|
|
|
|
defer cleanup1()
|
2016-03-09 02:18:53 +00:00
|
|
|
s.d.Cmd("build", "--file", dockerfileName, ".")
|
2016-03-18 21:42:40 +00:00
|
|
|
regexpCheckUA(c, buildUA)
|
|
|
|
|
2016-12-30 18:10:04 +00:00
|
|
|
s.d.Cmd("login", "-u", "richard", "-p", "testtest", loginReg.URL())
|
2016-03-18 21:42:40 +00:00
|
|
|
regexpCheckUA(c, loginUA)
|
2016-03-09 02:18:53 +00:00
|
|
|
|
2016-03-18 21:42:40 +00:00
|
|
|
s.d.Cmd("pull", pullRepoName)
|
|
|
|
regexpCheckUA(c, pullUA)
|
|
|
|
|
|
|
|
dockerfileName, cleanup2, err := makefile(`FROM scratch
|
|
|
|
ENV foo bar`)
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf("Unable to create test dockerfile"))
|
|
|
|
defer cleanup2()
|
|
|
|
s.d.Cmd("build", "-t", pushRepoName, "--file", dockerfileName, ".")
|
2016-03-09 02:18:53 +00:00
|
|
|
|
2016-03-18 21:42:40 +00:00
|
|
|
s.d.Cmd("push", pushRepoName)
|
|
|
|
regexpCheckUA(c, pushUA)
|
2016-03-09 02:18:53 +00:00
|
|
|
}
|