2016-03-09 02:18:53 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2017-06-12 12:47:27 +00:00
|
|
|
"os"
|
2016-03-09 02:18:53 +00:00
|
|
|
"regexp"
|
2019-09-09 21:06:12 +00:00
|
|
|
"testing"
|
2016-03-09 02:18:53 +00:00
|
|
|
|
2019-08-29 20:52:40 +00:00
|
|
|
"github.com/docker/docker/testutil/registry"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
2016-03-09 02:18:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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(`\\\(`)
|
2019-08-05 15:54:15 +00:00
|
|
|
ret = re.ReplaceAll(ret, []byte("("))
|
2016-03-09 02:18:53 +00:00
|
|
|
|
2016-03-18 21:42:40 +00:00
|
|
|
re = regexp.MustCompile(`\\\)`)
|
2019-08-05 15:54:15 +00:00
|
|
|
ret = re.ReplaceAll(ret, []byte(")"))
|
2016-03-09 02:18:53 +00:00
|
|
|
|
2016-03-18 21:42:40 +00:00
|
|
|
re = regexp.MustCompile(`\\\\`)
|
2019-08-05 15:54:15 +00:00
|
|
|
ret = re.ReplaceAll(ret, []byte(`\`))
|
2016-03-09 02:18:53 +00:00
|
|
|
|
|
|
|
return string(ret)
|
|
|
|
}
|
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func regexpCheckUA(c *testing.T, ua string) {
|
2016-03-09 02:18:53 +00:00
|
|
|
re := regexp.MustCompile("(?P<dockerUA>.+) UpstreamClient(?P<upstreamUA>.+)")
|
|
|
|
substrArr := re.FindStringSubmatch(ua)
|
|
|
|
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, len(substrArr), 3, "Expected 'UpstreamClient()' with upstream client UA")
|
2016-03-09 02:18:53 +00:00
|
|
|
dockerUA := substrArr[1]
|
|
|
|
upstreamUAEscaped := substrArr[2]
|
|
|
|
|
|
|
|
// check dockerUA looks correct
|
|
|
|
reDockerUA := regexp.MustCompile("^docker/[0-9A-Za-z+]")
|
|
|
|
bMatchDockerUA := reDockerUA.MatchString(dockerUA)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, bMatchDockerUA, "Docker Engine User-Agent malformed")
|
2016-03-09 02:18:53 +00:00
|
|
|
|
|
|
|
// check upstreamUA looks correct
|
|
|
|
// Expecting something like: Docker-Client/1.11.0-dev (linux)
|
|
|
|
upstreamUA := unescapeBackslashSemicolonParens(upstreamUAEscaped)
|
2019-08-05 14:53:46 +00:00
|
|
|
reUpstreamUA := regexp.MustCompile(`^\(Docker-Client/[0-9A-Za-z+]`)
|
2016-03-09 02:18:53 +00:00
|
|
|
bMatchUpstreamUA := reUpstreamUA.MatchString(upstreamUA)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, bMatchUpstreamUA, "(Upstream) Docker Client User-Agent malformed")
|
2016-03-09 02:18:53 +00:00
|
|
|
}
|
|
|
|
|
2017-06-12 13:02:51 +00: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 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)
|
2017-06-12 13:02:51 +00:00
|
|
|
w.Write([]byte(`{"errors":[{"code": "UNSUPPORTED","message": "this is a mock registry"}]}`))
|
2016-03-09 02:18:53 +00:00
|
|
|
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
|
2017-01-17 04:45:27 +00:00
|
|
|
// [docker engine UA] UpstreamClientSTREAM-CLIENT([client UA])
|
2019-09-09 21:05:55 +00:00
|
|
|
func (s *DockerRegistrySuite) TestUserAgentPassThrough(c *testing.T) {
|
2017-06-12 13:02:51 +00:00
|
|
|
var ua string
|
2016-03-09 02:18:53 +00:00
|
|
|
|
2017-06-12 13:02:51 +00:00
|
|
|
reg, err := registry.NewMock(c)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2019-08-28 17:56:38 +00:00
|
|
|
defer reg.Close()
|
|
|
|
|
2017-06-12 13:02:51 +00:00
|
|
|
registerUserAgentHandler(reg, &ua)
|
|
|
|
repoName := fmt.Sprintf("%s/busybox", reg.URL())
|
2016-03-09 02:18:53 +00:00
|
|
|
|
2017-06-12 13:02:51 +00:00
|
|
|
s.d.StartWithBusybox(c, "--insecure-registry", reg.URL())
|
2016-03-18 21:42:40 +00:00
|
|
|
|
2021-08-24 10:10:50 +00:00
|
|
|
tmp, err := os.MkdirTemp("", "integration-cli-")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2017-06-12 12:47:27 +00:00
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
2017-06-12 13:02:51 +00:00
|
|
|
dockerfile, err := makefile(tmp, fmt.Sprintf("FROM %s", repoName))
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err, "Unable to create test dockerfile")
|
2017-06-12 12:47:27 +00:00
|
|
|
|
2017-06-12 13:02:51 +00:00
|
|
|
s.d.Cmd("build", "--file", dockerfile, tmp)
|
|
|
|
regexpCheckUA(c, ua)
|
2016-03-09 02:18:53 +00:00
|
|
|
|
2017-06-12 13:02:51 +00:00
|
|
|
s.d.Cmd("login", "-u", "richard", "-p", "testtest", reg.URL())
|
|
|
|
regexpCheckUA(c, ua)
|
2016-03-18 21:42:40 +00:00
|
|
|
|
2017-06-12 13:02:51 +00:00
|
|
|
s.d.Cmd("pull", repoName)
|
|
|
|
regexpCheckUA(c, ua)
|
2016-03-09 02:18:53 +00:00
|
|
|
|
2017-06-12 13:02:51 +00:00
|
|
|
s.d.Cmd("tag", "busybox", repoName)
|
|
|
|
s.d.Cmd("push", repoName)
|
|
|
|
regexpCheckUA(c, ua)
|
2016-03-09 02:18:53 +00:00
|
|
|
}
|