docker_cli_pull_trusted_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os/exec"
  6. "time"
  7. "github.com/docker/docker/pkg/integration/checker"
  8. "github.com/go-check/check"
  9. )
  10. func (s *DockerTrustSuite) TestTrustedPull(c *check.C) {
  11. repoName := s.setupTrustedImage(c, "trusted-pull")
  12. // Try pull
  13. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  14. s.trustedCmd(pullCmd)
  15. out, _, err := runCommandWithOutput(pullCmd)
  16. c.Assert(err, check.IsNil, check.Commentf(out))
  17. c.Assert(string(out), checker.Contains, "Tagging", check.Commentf(out))
  18. dockerCmd(c, "rmi", repoName)
  19. // Try untrusted pull to ensure we pushed the tag to the registry
  20. pullCmd = exec.Command(dockerBinary, "pull", "--disable-content-trust=true", repoName)
  21. s.trustedCmd(pullCmd)
  22. out, _, err = runCommandWithOutput(pullCmd)
  23. c.Assert(err, check.IsNil, check.Commentf(out))
  24. c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf(out))
  25. }
  26. func (s *DockerTrustSuite) TestTrustedIsolatedPull(c *check.C) {
  27. repoName := s.setupTrustedImage(c, "trusted-isolated-pull")
  28. // Try pull (run from isolated directory without trust information)
  29. pullCmd := exec.Command(dockerBinary, "--config", "/tmp/docker-isolated", "pull", repoName)
  30. s.trustedCmd(pullCmd)
  31. out, _, err := runCommandWithOutput(pullCmd)
  32. c.Assert(err, check.IsNil, check.Commentf(out))
  33. c.Assert(string(out), checker.Contains, "Tagging", check.Commentf(string(out)))
  34. dockerCmd(c, "rmi", repoName)
  35. }
  36. func (s *DockerTrustSuite) TestUntrustedPull(c *check.C) {
  37. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  38. // tag the image and upload it to the private registry
  39. dockerCmd(c, "tag", "busybox", repoName)
  40. dockerCmd(c, "push", repoName)
  41. dockerCmd(c, "rmi", repoName)
  42. // Try trusted pull on untrusted tag
  43. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  44. s.trustedCmd(pullCmd)
  45. out, _, err := runCommandWithOutput(pullCmd)
  46. c.Assert(err, check.NotNil, check.Commentf(out))
  47. c.Assert(string(out), checker.Contains, "Error: remote trust data repository not initialized", check.Commentf(out))
  48. }
  49. func (s *DockerTrustSuite) TestPullWhenCertExpired(c *check.C) {
  50. c.Skip("Currently changes system time, causing instability")
  51. repoName := s.setupTrustedImage(c, "trusted-cert-expired")
  52. // Certificates have 10 years of expiration
  53. elevenYearsFromNow := time.Now().Add(time.Hour * 24 * 365 * 11)
  54. runAtDifferentDate(elevenYearsFromNow, func() {
  55. // Try pull
  56. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  57. s.trustedCmd(pullCmd)
  58. out, _, err := runCommandWithOutput(pullCmd)
  59. c.Assert(err, check.NotNil, check.Commentf(out))
  60. c.Assert(string(out), checker.Contains, "could not validate the path to a trusted root", check.Commentf(out))
  61. })
  62. runAtDifferentDate(elevenYearsFromNow, func() {
  63. // Try pull
  64. pullCmd := exec.Command(dockerBinary, "pull", "--disable-content-trust", repoName)
  65. s.trustedCmd(pullCmd)
  66. out, _, err := runCommandWithOutput(pullCmd)
  67. c.Assert(err, check.IsNil, check.Commentf(out))
  68. c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf(out))
  69. })
  70. }
  71. func (s *DockerTrustSuite) TestTrustedPullFromBadTrustServer(c *check.C) {
  72. repoName := fmt.Sprintf("%v/dockerclievilpull/trusted:latest", privateRegistryURL)
  73. evilLocalConfigDir, err := ioutil.TempDir("", "evil-local-config-dir")
  74. if err != nil {
  75. c.Fatalf("Failed to create local temp dir")
  76. }
  77. // tag the image and upload it to the private registry
  78. dockerCmd(c, "tag", "busybox", repoName)
  79. pushCmd := exec.Command(dockerBinary, "push", repoName)
  80. s.trustedCmd(pushCmd)
  81. out, _, err := runCommandWithOutput(pushCmd)
  82. c.Assert(err, check.IsNil, check.Commentf(out))
  83. c.Assert(string(out), checker.Contains, "Signing and pushing trust metadata", check.Commentf(out))
  84. dockerCmd(c, "rmi", repoName)
  85. // Try pull
  86. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  87. s.trustedCmd(pullCmd)
  88. out, _, err = runCommandWithOutput(pullCmd)
  89. c.Assert(err, check.IsNil, check.Commentf(out))
  90. c.Assert(string(out), checker.Contains, "Tagging", check.Commentf(out))
  91. dockerCmd(c, "rmi", repoName)
  92. // Kill the notary server, start a new "evil" one.
  93. s.not.Close()
  94. s.not, err = newTestNotary(c)
  95. c.Assert(err, check.IsNil, check.Commentf("Restarting notary server failed."))
  96. // In order to make an evil server, lets re-init a client (with a different trust dir) and push new data.
  97. // tag an image and upload it to the private registry
  98. dockerCmd(c, "--config", evilLocalConfigDir, "tag", "busybox", repoName)
  99. // Push up to the new server
  100. pushCmd = exec.Command(dockerBinary, "--config", evilLocalConfigDir, "push", repoName)
  101. s.trustedCmd(pushCmd)
  102. out, _, err = runCommandWithOutput(pushCmd)
  103. c.Assert(err, check.IsNil, check.Commentf(out))
  104. c.Assert(string(out), checker.Contains, "Signing and pushing trust metadata", check.Commentf(out))
  105. // Now, try pulling with the original client from this new trust server. This should fail.
  106. pullCmd = exec.Command(dockerBinary, "pull", repoName)
  107. s.trustedCmd(pullCmd)
  108. out, _, err = runCommandWithOutput(pullCmd)
  109. c.Assert(err, check.NotNil, check.Commentf(out))
  110. c.Assert(string(out), checker.Contains, "failed to validate data with current trusted certificates", check.Commentf(out))
  111. }
  112. func (s *DockerTrustSuite) TestTrustedPullWithExpiredSnapshot(c *check.C) {
  113. c.Skip("Currently changes system time, causing instability")
  114. repoName := fmt.Sprintf("%v/dockercliexpiredtimestamppull/trusted:latest", privateRegistryURL)
  115. // tag the image and upload it to the private registry
  116. dockerCmd(c, "tag", "busybox", repoName)
  117. // Push with default passphrases
  118. pushCmd := exec.Command(dockerBinary, "push", repoName)
  119. s.trustedCmd(pushCmd)
  120. out, _, err := runCommandWithOutput(pushCmd)
  121. c.Assert(err, check.IsNil, check.Commentf(out))
  122. c.Assert(string(out), checker.Contains, "Signing and pushing trust metadata", check.Commentf(out))
  123. dockerCmd(c, "rmi", repoName)
  124. // Snapshots last for three years. This should be expired
  125. fourYearsLater := time.Now().Add(time.Hour * 24 * 365 * 4)
  126. runAtDifferentDate(fourYearsLater, func() {
  127. // Try pull
  128. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  129. s.trustedCmd(pullCmd)
  130. out, _, err = runCommandWithOutput(pullCmd)
  131. c.Assert(err, check.NotNil, check.Commentf("Missing expected error running trusted pull with expired snapshots"))
  132. c.Assert(string(out), checker.Contains, "repository out-of-date", check.Commentf(out))
  133. })
  134. }
  135. func (s *DockerTrustSuite) TestTrustedOfflinePull(c *check.C) {
  136. repoName := s.setupTrustedImage(c, "trusted-offline-pull")
  137. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  138. s.trustedCmdWithServer(pullCmd, "https://invalidnotaryserver")
  139. out, _, err := runCommandWithOutput(pullCmd)
  140. c.Assert(err, check.NotNil, check.Commentf(out))
  141. c.Assert(string(out), checker.Contains, "error contacting notary server", check.Commentf(out))
  142. // Do valid trusted pull to warm cache
  143. pullCmd = exec.Command(dockerBinary, "pull", repoName)
  144. s.trustedCmd(pullCmd)
  145. out, _, err = runCommandWithOutput(pullCmd)
  146. c.Assert(err, check.IsNil, check.Commentf(out))
  147. c.Assert(string(out), checker.Contains, "Tagging", check.Commentf(out))
  148. dockerCmd(c, "rmi", repoName)
  149. // Try pull again with invalid notary server, should use cache
  150. pullCmd = exec.Command(dockerBinary, "pull", repoName)
  151. s.trustedCmdWithServer(pullCmd, "https://invalidnotaryserver")
  152. out, _, err = runCommandWithOutput(pullCmd)
  153. c.Assert(err, check.IsNil, check.Commentf(out))
  154. c.Assert(string(out), checker.Contains, "Tagging", check.Commentf(out))
  155. }