docker_cli_registry_user_agent_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "regexp"
  6. "github.com/go-check/check"
  7. )
  8. // unescapeBackslashSemicolonParens unescapes \;()
  9. func unescapeBackslashSemicolonParens(s string) string {
  10. re := regexp.MustCompile("\\\\;")
  11. ret := re.ReplaceAll([]byte(s), []byte(";"))
  12. re = regexp.MustCompile("\\\\\\(")
  13. ret = re.ReplaceAll([]byte(ret), []byte("("))
  14. re = regexp.MustCompile("\\\\\\)")
  15. ret = re.ReplaceAll([]byte(ret), []byte(")"))
  16. re = regexp.MustCompile("\\\\\\\\")
  17. ret = re.ReplaceAll([]byte(ret), []byte("\\"))
  18. return string(ret)
  19. }
  20. func regexpCheckUA(c *check.C, ua string) {
  21. re := regexp.MustCompile("(?P<dockerUA>.+) UpstreamClient(?P<upstreamUA>.+)")
  22. substrArr := re.FindStringSubmatch(ua)
  23. c.Assert(substrArr, check.HasLen, 3, check.Commentf("Expected 'UpstreamClient()' with upstream client UA"))
  24. dockerUA := substrArr[1]
  25. upstreamUAEscaped := substrArr[2]
  26. // check dockerUA looks correct
  27. reDockerUA := regexp.MustCompile("^docker/[0-9A-Za-z+]")
  28. bMatchDockerUA := reDockerUA.MatchString(dockerUA)
  29. c.Assert(bMatchDockerUA, check.Equals, true, check.Commentf("Docker Engine User-Agent malformed"))
  30. // check upstreamUA looks correct
  31. // Expecting something like: Docker-Client/1.11.0-dev (linux)
  32. upstreamUA := unescapeBackslashSemicolonParens(upstreamUAEscaped)
  33. reUpstreamUA := regexp.MustCompile("^\\(Docker-Client/[0-9A-Za-z+]")
  34. bMatchUpstreamUA := reUpstreamUA.MatchString(upstreamUA)
  35. c.Assert(bMatchUpstreamUA, check.Equals, true, check.Commentf("(Upstream) Docker Client User-Agent malformed"))
  36. }
  37. // TestUserAgentPassThroughOnPull verifies that when an image is pulled from
  38. // a registry, the registry should see a User-Agent string of the form
  39. // [docker engine UA] UptreamClientSTREAM-CLIENT([client UA])
  40. func (s *DockerRegistrySuite) TestUserAgentPassThroughOnPull(c *check.C) {
  41. reg, err := newTestRegistry(c)
  42. c.Assert(err, check.IsNil)
  43. expectUpstreamUA := false
  44. reg.registerHandler("/v2/", func(w http.ResponseWriter, r *http.Request) {
  45. w.WriteHeader(404)
  46. var ua string
  47. for k, v := range r.Header {
  48. if k == "User-Agent" {
  49. ua = v[0]
  50. }
  51. }
  52. c.Assert(ua, check.Not(check.Equals), "", check.Commentf("No User-Agent found in request"))
  53. if r.URL.Path == "/v2/busybox/manifests/latest" {
  54. if expectUpstreamUA {
  55. regexpCheckUA(c, ua)
  56. }
  57. }
  58. })
  59. repoName := fmt.Sprintf("%s/busybox", reg.hostport)
  60. err = s.d.Start("--insecure-registry", reg.hostport, "--disable-legacy-registry=true")
  61. c.Assert(err, check.IsNil)
  62. dockerfileName, cleanup, err := makefile(fmt.Sprintf("FROM %s/busybox", reg.hostport))
  63. c.Assert(err, check.IsNil, check.Commentf("Unable to create test dockerfile"))
  64. defer cleanup()
  65. s.d.Cmd("build", "--file", dockerfileName, ".")
  66. s.d.Cmd("run", repoName)
  67. s.d.Cmd("login", "-u", "richard", "-p", "testtest", "-e", "testuser@testdomain.com", reg.hostport)
  68. s.d.Cmd("tag", "busybox", repoName)
  69. s.d.Cmd("push", repoName)
  70. expectUpstreamUA = true
  71. s.d.Cmd("pull", repoName)
  72. }