docker_cli_pull_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. package main
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "strings"
  6. "time"
  7. "io/ioutil"
  8. "github.com/go-check/check"
  9. )
  10. // See issue docker/docker#8141
  11. func (s *DockerRegistrySuite) TestPullImageWithAliases(c *check.C) {
  12. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  13. repos := []string{}
  14. for _, tag := range []string{"recent", "fresh"} {
  15. repos = append(repos, fmt.Sprintf("%v:%v", repoName, tag))
  16. }
  17. // Tag and push the same image multiple times.
  18. for _, repo := range repos {
  19. dockerCmd(c, "tag", "busybox", repo)
  20. dockerCmd(c, "push", repo)
  21. }
  22. // Clear local images store.
  23. args := append([]string{"rmi"}, repos...)
  24. dockerCmd(c, args...)
  25. // Pull a single tag and verify it doesn't bring down all aliases.
  26. dockerCmd(c, "pull", repos[0])
  27. dockerCmd(c, "inspect", repos[0])
  28. for _, repo := range repos[1:] {
  29. if _, _, err := dockerCmdWithError(c, "inspect", repo); err == nil {
  30. c.Fatalf("Image %v shouldn't have been pulled down", repo)
  31. }
  32. }
  33. }
  34. // pulling library/hello-world should show verified message
  35. func (s *DockerSuite) TestPullVerified(c *check.C) {
  36. c.Skip("Skipping hub dependent test")
  37. // Image must be pulled from central repository to get verified message
  38. // unless keychain is manually updated to contain the daemon's sign key.
  39. verifiedName := "hello-world"
  40. // pull it
  41. expected := "The image you are pulling has been verified"
  42. if out, exitCode, err := dockerCmdWithError(c, "pull", verifiedName); err != nil || !strings.Contains(out, expected) {
  43. if err != nil || exitCode != 0 {
  44. c.Skip(fmt.Sprintf("pulling the '%s' image from the registry has failed: %v", verifiedName, err))
  45. }
  46. c.Fatalf("pulling a verified image failed. expected: %s\ngot: %s, %v", expected, out, err)
  47. }
  48. // pull it again
  49. if out, exitCode, err := dockerCmdWithError(c, "pull", verifiedName); err != nil || strings.Contains(out, expected) {
  50. if err != nil || exitCode != 0 {
  51. c.Skip(fmt.Sprintf("pulling the '%s' image from the registry has failed: %v", verifiedName, err))
  52. }
  53. c.Fatalf("pulling a verified image failed. unexpected verify message\ngot: %s, %v", out, err)
  54. }
  55. }
  56. // pulling an image from the central registry should work
  57. func (s *DockerSuite) TestPullImageFromCentralRegistry(c *check.C) {
  58. testRequires(c, Network)
  59. dockerCmd(c, "pull", "hello-world")
  60. }
  61. // pulling a non-existing image from the central registry should return a non-zero exit code
  62. func (s *DockerSuite) TestPullNonExistingImage(c *check.C) {
  63. testRequires(c, Network)
  64. name := "sadfsadfasdf"
  65. out, _, err := dockerCmdWithError(c, "pull", name)
  66. if err == nil || !strings.Contains(out, fmt.Sprintf("Error: image library/%s:latest not found", name)) {
  67. c.Fatalf("expected non-zero exit status when pulling non-existing image: %s", out)
  68. }
  69. }
  70. // pulling an image from the central registry using official names should work
  71. // ensure all pulls result in the same image
  72. func (s *DockerSuite) TestPullImageOfficialNames(c *check.C) {
  73. testRequires(c, Network)
  74. names := []string{
  75. "library/hello-world",
  76. "docker.io/library/hello-world",
  77. "index.docker.io/library/hello-world",
  78. }
  79. for _, name := range names {
  80. out, exitCode, err := dockerCmdWithError(c, "pull", name)
  81. if err != nil || exitCode != 0 {
  82. c.Errorf("pulling the '%s' image from the registry has failed: %s", name, err)
  83. continue
  84. }
  85. // ensure we don't have multiple image names.
  86. out, _ = dockerCmd(c, "images")
  87. if strings.Contains(out, name) {
  88. c.Errorf("images should not have listed '%s'", name)
  89. }
  90. }
  91. }
  92. func (s *DockerSuite) TestPullScratchNotAllowed(c *check.C) {
  93. testRequires(c, Network)
  94. out, exitCode, err := dockerCmdWithError(c, "pull", "scratch")
  95. if err == nil {
  96. c.Fatal("expected pull of scratch to fail, but it didn't")
  97. }
  98. if exitCode != 1 {
  99. c.Fatalf("pulling scratch expected exit code 1, got %d", exitCode)
  100. }
  101. if strings.Contains(out, "Pulling repository scratch") {
  102. c.Fatalf("pulling scratch should not have begun: %s", out)
  103. }
  104. if !strings.Contains(out, "'scratch' is a reserved name") {
  105. c.Fatalf("unexpected output pulling scratch: %s", out)
  106. }
  107. }
  108. // pulling an image with --all-tags=true
  109. func (s *DockerSuite) TestPullImageWithAllTagFromCentralRegistry(c *check.C) {
  110. testRequires(c, Network)
  111. dockerCmd(c, "pull", "busybox")
  112. outImageCmd, _ := dockerCmd(c, "images", "busybox")
  113. dockerCmd(c, "pull", "--all-tags=true", "busybox")
  114. outImageAllTagCmd, _ := dockerCmd(c, "images", "busybox")
  115. if strings.Count(outImageCmd, "busybox") >= strings.Count(outImageAllTagCmd, "busybox") {
  116. c.Fatalf("Pulling with all tags should get more images")
  117. }
  118. // FIXME has probably no effect (tags already pushed)
  119. dockerCmd(c, "pull", "-a", "busybox")
  120. outImageAllTagCmd, _ = dockerCmd(c, "images", "busybox")
  121. if strings.Count(outImageCmd, "busybox") >= strings.Count(outImageAllTagCmd, "busybox") {
  122. c.Fatalf("Pulling with all tags should get more images")
  123. }
  124. }
  125. func (s *DockerTrustSuite) TestTrustedPull(c *check.C) {
  126. repoName := s.setupTrustedImage(c, "trusted-pull")
  127. // Try pull
  128. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  129. s.trustedCmd(pullCmd)
  130. out, _, err := runCommandWithOutput(pullCmd)
  131. if err != nil {
  132. c.Fatalf("Error running trusted pull: %s\n%s", err, out)
  133. }
  134. if !strings.Contains(string(out), "Tagging") {
  135. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  136. }
  137. dockerCmd(c, "rmi", repoName)
  138. // Try untrusted pull to ensure we pushed the tag to the registry
  139. pullCmd = exec.Command(dockerBinary, "pull", "--disable-content-trust=true", repoName)
  140. s.trustedCmd(pullCmd)
  141. out, _, err = runCommandWithOutput(pullCmd)
  142. if err != nil {
  143. c.Fatalf("Error running trusted pull: %s\n%s", err, out)
  144. }
  145. if !strings.Contains(string(out), "Status: Downloaded") {
  146. c.Fatalf("Missing expected output on trusted pull with --disable-content-trust:\n%s", out)
  147. }
  148. }
  149. func (s *DockerTrustSuite) TestTrustedIsolatedPull(c *check.C) {
  150. repoName := s.setupTrustedImage(c, "trusted-isolatd-pull")
  151. // Try pull (run from isolated directory without trust information)
  152. pullCmd := exec.Command(dockerBinary, "--config", "/tmp/docker-isolated", "pull", repoName)
  153. s.trustedCmd(pullCmd)
  154. out, _, err := runCommandWithOutput(pullCmd)
  155. if err != nil {
  156. c.Fatalf("Error running trusted pull: %s\n%s", err, out)
  157. }
  158. if !strings.Contains(string(out), "Tagging") {
  159. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  160. }
  161. dockerCmd(c, "rmi", repoName)
  162. }
  163. func (s *DockerTrustSuite) TestUntrustedPull(c *check.C) {
  164. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  165. // tag the image and upload it to the private registry
  166. dockerCmd(c, "tag", "busybox", repoName)
  167. dockerCmd(c, "push", repoName)
  168. dockerCmd(c, "rmi", repoName)
  169. // Try trusted pull on untrusted tag
  170. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  171. s.trustedCmd(pullCmd)
  172. out, _, err := runCommandWithOutput(pullCmd)
  173. if err == nil {
  174. c.Fatalf("Error expected when running trusted pull with:\n%s", out)
  175. }
  176. if !strings.Contains(string(out), "no trust data available") {
  177. c.Fatalf("Missing expected output on trusted pull:\n%s", out)
  178. }
  179. }
  180. func (s *DockerTrustSuite) TestPullWhenCertExpired(c *check.C) {
  181. repoName := s.setupTrustedImage(c, "trusted-cert-expired")
  182. // Certificates have 10 years of expiration
  183. elevenYearsFromNow := time.Now().Add(time.Hour * 24 * 365 * 11)
  184. runAtDifferentDate(elevenYearsFromNow, func() {
  185. // Try pull
  186. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  187. s.trustedCmd(pullCmd)
  188. out, _, err := runCommandWithOutput(pullCmd)
  189. if err == nil {
  190. c.Fatalf("Error running trusted pull in the distant future: %s\n%s", err, out)
  191. }
  192. if !strings.Contains(string(out), "could not validate the path to a trusted root") {
  193. c.Fatalf("Missing expected output on trusted pull in the distant future:\n%s", out)
  194. }
  195. })
  196. runAtDifferentDate(elevenYearsFromNow, func() {
  197. // Try pull
  198. pullCmd := exec.Command(dockerBinary, "pull", "--disable-content-trust", repoName)
  199. s.trustedCmd(pullCmd)
  200. out, _, err := runCommandWithOutput(pullCmd)
  201. if err != nil {
  202. c.Fatalf("Error running untrusted pull in the distant future: %s\n%s", err, out)
  203. }
  204. if !strings.Contains(string(out), "Status: Downloaded") {
  205. c.Fatalf("Missing expected output on untrusted pull in the distant future:\n%s", out)
  206. }
  207. })
  208. }
  209. func (s *DockerTrustSuite) TestTrustedPullFromBadTrustServer(c *check.C) {
  210. repoName := fmt.Sprintf("%v/dockerclievilpull/trusted:latest", privateRegistryURL)
  211. evilLocalConfigDir, err := ioutil.TempDir("", "evil-local-config-dir")
  212. if err != nil {
  213. c.Fatalf("Failed to create local temp dir")
  214. }
  215. // tag the image and upload it to the private registry
  216. dockerCmd(c, "tag", "busybox", repoName)
  217. pushCmd := exec.Command(dockerBinary, "push", repoName)
  218. s.trustedCmd(pushCmd)
  219. out, _, err := runCommandWithOutput(pushCmd)
  220. if err != nil {
  221. c.Fatalf("Error running trusted push: %s\n%s", err, out)
  222. }
  223. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  224. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  225. }
  226. dockerCmd(c, "rmi", repoName)
  227. // Try pull
  228. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  229. s.trustedCmd(pullCmd)
  230. out, _, err = runCommandWithOutput(pullCmd)
  231. if err != nil {
  232. c.Fatalf("Error running trusted pull: %s\n%s", err, out)
  233. }
  234. if !strings.Contains(string(out), "Tagging") {
  235. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  236. }
  237. dockerCmd(c, "rmi", repoName)
  238. // Kill the notary server, start a new "evil" one.
  239. s.not.Close()
  240. s.not, err = newTestNotary(c)
  241. if err != nil {
  242. c.Fatalf("Restarting notary server failed.")
  243. }
  244. // In order to make an evil server, lets re-init a client (with a different trust dir) and push new data.
  245. // tag an image and upload it to the private registry
  246. dockerCmd(c, "--config", evilLocalConfigDir, "tag", "busybox", repoName)
  247. // Push up to the new server
  248. pushCmd = exec.Command(dockerBinary, "--config", evilLocalConfigDir, "push", repoName)
  249. s.trustedCmd(pushCmd)
  250. out, _, err = runCommandWithOutput(pushCmd)
  251. if err != nil {
  252. c.Fatalf("Error running trusted push: %s\n%s", err, out)
  253. }
  254. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  255. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  256. }
  257. // Now, try pulling with the original client from this new trust server. This should fail.
  258. pullCmd = exec.Command(dockerBinary, "pull", repoName)
  259. s.trustedCmd(pullCmd)
  260. out, _, err = runCommandWithOutput(pullCmd)
  261. if err == nil {
  262. c.Fatalf("Expected to fail on this pull due to different remote data: %s\n%s", err, out)
  263. }
  264. if !strings.Contains(string(out), "failed to validate data with current trusted certificates") {
  265. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  266. }
  267. }
  268. func (s *DockerTrustSuite) TestTrustedPullWithExpiredSnapshot(c *check.C) {
  269. repoName := fmt.Sprintf("%v/dockercliexpiredtimestamppull/trusted:latest", privateRegistryURL)
  270. // tag the image and upload it to the private registry
  271. dockerCmd(c, "tag", "busybox", repoName)
  272. // Push with default passphrases
  273. pushCmd := exec.Command(dockerBinary, "push", repoName)
  274. s.trustedCmd(pushCmd)
  275. out, _, err := runCommandWithOutput(pushCmd)
  276. if err != nil {
  277. c.Fatalf("trusted push failed: %s\n%s", err, out)
  278. }
  279. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  280. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  281. }
  282. dockerCmd(c, "rmi", repoName)
  283. // Snapshots last for three years. This should be expired
  284. fourYearsLater := time.Now().Add(time.Hour * 24 * 365 * 4)
  285. // Should succeed because the server transparently re-signs one
  286. runAtDifferentDate(fourYearsLater, func() {
  287. // Try pull
  288. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  289. s.trustedCmd(pullCmd)
  290. out, _, err = runCommandWithOutput(pullCmd)
  291. if err == nil {
  292. c.Fatalf("Missing expected error running trusted pull with expired snapshots")
  293. }
  294. if !strings.Contains(string(out), "repository out-of-date") {
  295. c.Fatalf("Missing expected output on trusted pull with expired snapshot:\n%s", out)
  296. }
  297. })
  298. }