2016-03-08 21:18:53 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
|
2016-12-30 13:10:04 -05:00
|
|
|
"github.com/docker/docker/integration-cli/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"))
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
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
|
2016-08-07 23:28:44 -04:00
|
|
|
// [docker engine UA] UptreamClientSTREAM-CLIENT([client UA])
|
2016-03-18 17:42:40 -04:00
|
|
|
func (s *DockerRegistrySuite) TestUserAgentPassThrough(c *check.C) {
|
|
|
|
var (
|
|
|
|
buildUA string
|
|
|
|
pullUA string
|
|
|
|
pushUA string
|
|
|
|
loginUA string
|
|
|
|
)
|
|
|
|
|
2016-12-30 13:10:04 -05:00
|
|
|
buildReg, err := registry.NewMock(c)
|
2016-03-18 17:42:40 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
registerUserAgentHandler(buildReg, &buildUA)
|
2016-12-30 13:10:04 -05:00
|
|
|
buildRepoName := fmt.Sprintf("%s/busybox", buildReg.URL())
|
2016-03-08 21:18:53 -05:00
|
|
|
|
2016-12-30 13:10:04 -05:00
|
|
|
pullReg, err := registry.NewMock(c)
|
2016-03-08 21:18:53 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
2016-03-18 17:42:40 -04:00
|
|
|
registerUserAgentHandler(pullReg, &pullUA)
|
2016-12-30 13:10:04 -05:00
|
|
|
pullRepoName := fmt.Sprintf("%s/busybox", pullReg.URL())
|
2016-03-08 21:18:53 -05:00
|
|
|
|
2016-12-30 13:10:04 -05:00
|
|
|
pushReg, err := registry.NewMock(c)
|
2016-03-18 17:42:40 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
registerUserAgentHandler(pushReg, &pushUA)
|
2016-12-30 13:10:04 -05:00
|
|
|
pushRepoName := fmt.Sprintf("%s/busybox", pushReg.URL())
|
2016-03-08 21:18:53 -05:00
|
|
|
|
2016-12-30 13:10:04 -05:00
|
|
|
loginReg, err := registry.NewMock(c)
|
2016-03-18 17:42:40 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
registerUserAgentHandler(loginReg, &loginUA)
|
|
|
|
|
2016-12-09 17:20:14 -05:00
|
|
|
s.d.Start(c,
|
2016-12-30 13:10:04 -05:00
|
|
|
"--insecure-registry", buildReg.URL(),
|
|
|
|
"--insecure-registry", pullReg.URL(),
|
|
|
|
"--insecure-registry", pushReg.URL(),
|
|
|
|
"--insecure-registry", loginReg.URL(),
|
2016-03-18 17:42:40 -04: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-08 21:18:53 -05:00
|
|
|
s.d.Cmd("build", "--file", dockerfileName, ".")
|
2016-03-18 17:42:40 -04:00
|
|
|
regexpCheckUA(c, buildUA)
|
|
|
|
|
2016-12-30 13:10:04 -05:00
|
|
|
s.d.Cmd("login", "-u", "richard", "-p", "testtest", loginReg.URL())
|
2016-03-18 17:42:40 -04:00
|
|
|
regexpCheckUA(c, loginUA)
|
2016-03-08 21:18:53 -05:00
|
|
|
|
2016-03-18 17:42:40 -04: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-08 21:18:53 -05:00
|
|
|
|
2016-03-18 17:42:40 -04:00
|
|
|
s.d.Cmd("push", pushRepoName)
|
|
|
|
regexpCheckUA(c, pushUA)
|
2016-03-08 21:18:53 -05:00
|
|
|
}
|