2016-03-08 21:18:53 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2017-06-12 08:47:27 -04:00
|
|
|
"os"
|
2016-03-08 21:18:53 -05:00
|
|
|
"regexp"
|
2019-09-09 17:06:12 -04:00
|
|
|
"testing"
|
2016-03-08 21:18:53 -05:00
|
|
|
|
2019-08-29 16:52:40 -04:00
|
|
|
"github.com/docker/docker/testutil/registry"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2016-03-08 21:18:53 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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(`\\\(`)
|
2019-08-05 11:54:15 -04:00
|
|
|
ret = re.ReplaceAll(ret, []byte("("))
|
2016-03-08 21:18:53 -05:00
|
|
|
|
2016-03-18 17:42:40 -04:00
|
|
|
re = regexp.MustCompile(`\\\)`)
|
2019-08-05 11:54:15 -04:00
|
|
|
ret = re.ReplaceAll(ret, []byte(")"))
|
2016-03-08 21:18:53 -05:00
|
|
|
|
2016-03-18 17:42:40 -04:00
|
|
|
re = regexp.MustCompile(`\\\\`)
|
2019-08-05 11:54:15 -04:00
|
|
|
ret = re.ReplaceAll(ret, []byte(`\`))
|
2016-03-08 21:18:53 -05:00
|
|
|
|
|
|
|
return string(ret)
|
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func regexpCheckUA(c *testing.T, ua string) {
|
2016-03-08 21:18:53 -05:00
|
|
|
re := regexp.MustCompile("(?P<dockerUA>.+) UpstreamClient(?P<upstreamUA>.+)")
|
|
|
|
substrArr := re.FindStringSubmatch(ua)
|
|
|
|
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, len(substrArr), 3, "Expected 'UpstreamClient()' with upstream client UA")
|
2016-03-08 21:18:53 -05: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 09:23:19 -04:00
|
|
|
assert.Assert(c, bMatchDockerUA, "Docker Engine User-Agent malformed")
|
2016-03-08 21:18:53 -05:00
|
|
|
|
|
|
|
// check upstreamUA looks correct
|
|
|
|
// Expecting something like: Docker-Client/1.11.0-dev (linux)
|
|
|
|
upstreamUA := unescapeBackslashSemicolonParens(upstreamUAEscaped)
|
2019-08-05 10:53:46 -04:00
|
|
|
reUpstreamUA := regexp.MustCompile(`^\(Docker-Client/[0-9A-Za-z+]`)
|
2016-03-08 21:18:53 -05:00
|
|
|
bMatchUpstreamUA := reUpstreamUA.MatchString(upstreamUA)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, bMatchUpstreamUA, "(Upstream) Docker Client User-Agent malformed")
|
2016-03-08 21:18:53 -05:00
|
|
|
}
|
|
|
|
|
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])
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerRegistrySuite) TestUserAgentPassThrough(c *testing.T) {
|
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)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2019-08-28 13:56:38 -04:00
|
|
|
defer reg.Close()
|
|
|
|
|
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
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
tmp, err := os.MkdirTemp("", "integration-cli-")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2017-06-12 08:47:27 -04:00
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
2017-06-12 09:02:51 -04:00
|
|
|
dockerfile, err := makefile(tmp, fmt.Sprintf("FROM %s", repoName))
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err, "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
|
|
|
}
|