docker_cli_push_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. package main
  2. import (
  3. "archive/tar"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "strings"
  10. "time"
  11. "github.com/docker/docker/cliconfig"
  12. "github.com/docker/docker/pkg/integration/checker"
  13. "github.com/go-check/check"
  14. )
  15. // Pushing an image to a private registry.
  16. func (s *DockerRegistrySuite) TestPushBusyboxImage(c *check.C) {
  17. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  18. // tag the image to upload it to the private registry
  19. dockerCmd(c, "tag", "busybox", repoName)
  20. // push the image to the registry
  21. dockerCmd(c, "push", repoName)
  22. }
  23. // pushing an image without a prefix should throw an error
  24. func (s *DockerSuite) TestPushUnprefixedRepo(c *check.C) {
  25. out, _, err := dockerCmdWithError("push", "busybox")
  26. c.Assert(err, check.NotNil, check.Commentf("pushing an unprefixed repo didn't result in a non-zero exit status: %s", out))
  27. }
  28. func (s *DockerRegistrySuite) TestPushUntagged(c *check.C) {
  29. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  30. expected := "Repository does not exist"
  31. out, _, err := dockerCmdWithError("push", repoName)
  32. c.Assert(err, check.NotNil, check.Commentf("pushing the image to the private registry should have failed: output %q", out))
  33. c.Assert(out, checker.Contains, expected, check.Commentf("pushing the image failed"))
  34. }
  35. func (s *DockerRegistrySuite) TestPushBadTag(c *check.C) {
  36. repoName := fmt.Sprintf("%v/dockercli/busybox:latest", privateRegistryURL)
  37. expected := "does not exist"
  38. out, _, err := dockerCmdWithError("push", repoName)
  39. c.Assert(err, check.NotNil, check.Commentf("pushing the image to the private registry should have failed: output %q", out))
  40. c.Assert(out, checker.Contains, expected, check.Commentf("pushing the image failed"))
  41. }
  42. func (s *DockerRegistrySuite) TestPushMultipleTags(c *check.C) {
  43. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  44. repoTag1 := fmt.Sprintf("%v/dockercli/busybox:t1", privateRegistryURL)
  45. repoTag2 := fmt.Sprintf("%v/dockercli/busybox:t2", privateRegistryURL)
  46. // tag the image and upload it to the private registry
  47. dockerCmd(c, "tag", "busybox", repoTag1)
  48. dockerCmd(c, "tag", "busybox", repoTag2)
  49. dockerCmd(c, "push", repoName)
  50. // Ensure layer list is equivalent for repoTag1 and repoTag2
  51. out1, _ := dockerCmd(c, "pull", repoTag1)
  52. imageAlreadyExists := ": Image already exists"
  53. var out1Lines []string
  54. for _, outputLine := range strings.Split(out1, "\n") {
  55. if strings.Contains(outputLine, imageAlreadyExists) {
  56. out1Lines = append(out1Lines, outputLine)
  57. }
  58. }
  59. out2, _ := dockerCmd(c, "pull", repoTag2)
  60. var out2Lines []string
  61. for _, outputLine := range strings.Split(out2, "\n") {
  62. if strings.Contains(outputLine, imageAlreadyExists) {
  63. out1Lines = append(out1Lines, outputLine)
  64. }
  65. }
  66. c.Assert(out2Lines, checker.HasLen, len(out1Lines))
  67. for i := range out1Lines {
  68. c.Assert(out1Lines[i], checker.Equals, out2Lines[i])
  69. }
  70. }
  71. func (s *DockerRegistrySuite) TestPushEmptyLayer(c *check.C) {
  72. repoName := fmt.Sprintf("%v/dockercli/emptylayer", privateRegistryURL)
  73. emptyTarball, err := ioutil.TempFile("", "empty_tarball")
  74. c.Assert(err, check.IsNil, check.Commentf("Unable to create test file"))
  75. tw := tar.NewWriter(emptyTarball)
  76. err = tw.Close()
  77. c.Assert(err, check.IsNil, check.Commentf("Error creating empty tarball"))
  78. freader, err := os.Open(emptyTarball.Name())
  79. c.Assert(err, check.IsNil, check.Commentf("Could not open test tarball"))
  80. importCmd := exec.Command(dockerBinary, "import", "-", repoName)
  81. importCmd.Stdin = freader
  82. out, _, err := runCommandWithOutput(importCmd)
  83. c.Assert(err, check.IsNil, check.Commentf("import failed: %q", out))
  84. // Now verify we can push it
  85. out, _, err = dockerCmdWithError("push", repoName)
  86. c.Assert(err, check.IsNil, check.Commentf("pushing the image to the private registry has failed: %s", out))
  87. }
  88. func (s *DockerTrustSuite) TestTrustedPush(c *check.C) {
  89. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  90. // tag the image and upload it to the private registry
  91. dockerCmd(c, "tag", "busybox", repoName)
  92. pushCmd := exec.Command(dockerBinary, "push", repoName)
  93. s.trustedCmd(pushCmd)
  94. out, _, err := runCommandWithOutput(pushCmd)
  95. c.Assert(err, check.IsNil, check.Commentf("Error running trusted push: %s\n%s", err, out))
  96. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  97. // Try pull after push
  98. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  99. s.trustedCmd(pullCmd)
  100. out, _, err = runCommandWithOutput(pullCmd)
  101. c.Assert(err, check.IsNil, check.Commentf(out))
  102. c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf(out))
  103. }
  104. func (s *DockerTrustSuite) TestTrustedPushWithEnvPasswords(c *check.C) {
  105. repoName := fmt.Sprintf("%v/dockerclienv/trusted:latest", privateRegistryURL)
  106. // tag the image and upload it to the private registry
  107. dockerCmd(c, "tag", "busybox", repoName)
  108. pushCmd := exec.Command(dockerBinary, "push", repoName)
  109. s.trustedCmdWithPassphrases(pushCmd, "12345678", "12345678")
  110. out, _, err := runCommandWithOutput(pushCmd)
  111. c.Assert(err, check.IsNil, check.Commentf("Error running trusted push: %s\n%s", err, out))
  112. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  113. // Try pull after push
  114. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  115. s.trustedCmd(pullCmd)
  116. out, _, err = runCommandWithOutput(pullCmd)
  117. c.Assert(err, check.IsNil, check.Commentf(out))
  118. c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf(out))
  119. }
  120. // This test ensures backwards compatibility with old ENV variables. Should be
  121. // deprecated by 1.10
  122. func (s *DockerTrustSuite) TestTrustedPushWithDeprecatedEnvPasswords(c *check.C) {
  123. repoName := fmt.Sprintf("%v/dockercli/trusteddeprecated:latest", privateRegistryURL)
  124. // tag the image and upload it to the private registry
  125. dockerCmd(c, "tag", "busybox", repoName)
  126. pushCmd := exec.Command(dockerBinary, "push", repoName)
  127. s.trustedCmdWithDeprecatedEnvPassphrases(pushCmd, "12345678", "12345678")
  128. out, _, err := runCommandWithOutput(pushCmd)
  129. c.Assert(err, check.IsNil, check.Commentf("Error running trusted push: %s\n%s", err, out))
  130. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  131. }
  132. func (s *DockerTrustSuite) TestTrustedPushWithFailingServer(c *check.C) {
  133. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  134. // tag the image and upload it to the private registry
  135. dockerCmd(c, "tag", "busybox", repoName)
  136. pushCmd := exec.Command(dockerBinary, "push", repoName)
  137. s.trustedCmdWithServer(pushCmd, "https://example.com:81/")
  138. out, _, err := runCommandWithOutput(pushCmd)
  139. c.Assert(err, check.NotNil, check.Commentf("Missing error while running trusted push w/ no server"))
  140. c.Assert(out, checker.Contains, "error contacting notary server", check.Commentf("Missing expected output on trusted push"))
  141. }
  142. func (s *DockerTrustSuite) TestTrustedPushWithoutServerAndUntrusted(c *check.C) {
  143. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  144. // tag the image and upload it to the private registry
  145. dockerCmd(c, "tag", "busybox", repoName)
  146. pushCmd := exec.Command(dockerBinary, "push", "--disable-content-trust", repoName)
  147. s.trustedCmdWithServer(pushCmd, "https://example.com/")
  148. out, _, err := runCommandWithOutput(pushCmd)
  149. c.Assert(err, check.IsNil, check.Commentf("trusted push with no server and --disable-content-trust failed: %s\n%s", err, out))
  150. c.Assert(out, check.Not(checker.Contains), "Error establishing connection to notary repository", check.Commentf("Missing expected output on trusted push with --disable-content-trust:"))
  151. }
  152. func (s *DockerTrustSuite) TestTrustedPushWithExistingTag(c *check.C) {
  153. repoName := fmt.Sprintf("%v/dockerclitag/trusted:latest", privateRegistryURL)
  154. // tag the image and upload it to the private registry
  155. dockerCmd(c, "tag", "busybox", repoName)
  156. dockerCmd(c, "push", repoName)
  157. pushCmd := exec.Command(dockerBinary, "push", repoName)
  158. s.trustedCmd(pushCmd)
  159. out, _, err := runCommandWithOutput(pushCmd)
  160. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  161. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  162. // Try pull after push
  163. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  164. s.trustedCmd(pullCmd)
  165. out, _, err = runCommandWithOutput(pullCmd)
  166. c.Assert(err, check.IsNil, check.Commentf(out))
  167. c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf(out))
  168. }
  169. func (s *DockerTrustSuite) TestTrustedPushWithExistingSignedTag(c *check.C) {
  170. repoName := fmt.Sprintf("%v/dockerclipushpush/trusted:latest", privateRegistryURL)
  171. // tag the image and upload it to the private registry
  172. dockerCmd(c, "tag", "busybox", repoName)
  173. // Do a trusted push
  174. pushCmd := exec.Command(dockerBinary, "push", repoName)
  175. s.trustedCmd(pushCmd)
  176. out, _, err := runCommandWithOutput(pushCmd)
  177. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  178. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  179. // Do another trusted push
  180. pushCmd = exec.Command(dockerBinary, "push", repoName)
  181. s.trustedCmd(pushCmd)
  182. out, _, err = runCommandWithOutput(pushCmd)
  183. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  184. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  185. dockerCmd(c, "rmi", repoName)
  186. // Try pull to ensure the double push did not break our ability to pull
  187. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  188. s.trustedCmd(pullCmd)
  189. out, _, err = runCommandWithOutput(pullCmd)
  190. c.Assert(err, check.IsNil, check.Commentf("Error running trusted pull: %s\n%s", err, out))
  191. c.Assert(out, checker.Contains, "Status: Downloaded", check.Commentf("Missing expected output on trusted pull with --disable-content-trust"))
  192. }
  193. func (s *DockerTrustSuite) TestTrustedPushWithIncorrectPassphraseForNonRoot(c *check.C) {
  194. repoName := fmt.Sprintf("%v/dockercliincorretpwd/trusted:latest", privateRegistryURL)
  195. // tag the image and upload it to the private registry
  196. dockerCmd(c, "tag", "busybox", repoName)
  197. // Push with default passphrases
  198. pushCmd := exec.Command(dockerBinary, "push", repoName)
  199. s.trustedCmd(pushCmd)
  200. out, _, err := runCommandWithOutput(pushCmd)
  201. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  202. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push:\n%s", out))
  203. // Push with wrong passphrases
  204. pushCmd = exec.Command(dockerBinary, "push", repoName)
  205. s.trustedCmdWithPassphrases(pushCmd, "12345678", "87654321")
  206. out, _, err = runCommandWithOutput(pushCmd)
  207. c.Assert(err, check.NotNil, check.Commentf("Error missing from trusted push with short targets passphrase: \n%s", out))
  208. c.Assert(out, checker.Contains, "could not find necessary signing keys", check.Commentf("Missing expected output on trusted push with short targets/snapsnot passphrase"))
  209. }
  210. // This test ensures backwards compatibility with old ENV variables. Should be
  211. // deprecated by 1.10
  212. func (s *DockerTrustSuite) TestTrustedPushWithIncorrectDeprecatedPassphraseForNonRoot(c *check.C) {
  213. repoName := fmt.Sprintf("%v/dockercliincorretdeprecatedpwd/trusted:latest", privateRegistryURL)
  214. // tag the image and upload it to the private registry
  215. dockerCmd(c, "tag", "busybox", repoName)
  216. // Push with default passphrases
  217. pushCmd := exec.Command(dockerBinary, "push", repoName)
  218. s.trustedCmd(pushCmd)
  219. out, _, err := runCommandWithOutput(pushCmd)
  220. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  221. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  222. // Push with wrong passphrases
  223. pushCmd = exec.Command(dockerBinary, "push", repoName)
  224. s.trustedCmdWithDeprecatedEnvPassphrases(pushCmd, "12345678", "87654321")
  225. out, _, err = runCommandWithOutput(pushCmd)
  226. c.Assert(err, check.NotNil, check.Commentf("Error missing from trusted push with short targets passphrase: \n%s", out))
  227. c.Assert(out, checker.Contains, "could not find necessary signing keys", check.Commentf("Missing expected output on trusted push with short targets/snapsnot passphrase"))
  228. }
  229. func (s *DockerTrustSuite) TestTrustedPushWithExpiredSnapshot(c *check.C) {
  230. c.Skip("Currently changes system time, causing instability")
  231. repoName := fmt.Sprintf("%v/dockercliexpiredsnapshot/trusted:latest", privateRegistryURL)
  232. // tag the image and upload it to the private registry
  233. dockerCmd(c, "tag", "busybox", repoName)
  234. // Push with default passphrases
  235. pushCmd := exec.Command(dockerBinary, "push", repoName)
  236. s.trustedCmd(pushCmd)
  237. out, _, err := runCommandWithOutput(pushCmd)
  238. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  239. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  240. // Snapshots last for three years. This should be expired
  241. fourYearsLater := time.Now().Add(time.Hour * 24 * 365 * 4)
  242. runAtDifferentDate(fourYearsLater, func() {
  243. // Push with wrong passphrases
  244. pushCmd = exec.Command(dockerBinary, "push", repoName)
  245. s.trustedCmd(pushCmd)
  246. out, _, err = runCommandWithOutput(pushCmd)
  247. c.Assert(err, check.NotNil, check.Commentf("Error missing from trusted push with expired snapshot: \n%s", out))
  248. c.Assert(out, checker.Contains, "repository out-of-date", check.Commentf("Missing expected output on trusted push with expired snapshot"))
  249. })
  250. }
  251. func (s *DockerTrustSuite) TestTrustedPushWithExpiredTimestamp(c *check.C) {
  252. c.Skip("Currently changes system time, causing instability")
  253. repoName := fmt.Sprintf("%v/dockercliexpiredtimestamppush/trusted:latest", privateRegistryURL)
  254. // tag the image and upload it to the private registry
  255. dockerCmd(c, "tag", "busybox", repoName)
  256. // Push with default passphrases
  257. pushCmd := exec.Command(dockerBinary, "push", repoName)
  258. s.trustedCmd(pushCmd)
  259. out, _, err := runCommandWithOutput(pushCmd)
  260. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  261. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  262. // The timestamps expire in two weeks. Lets check three
  263. threeWeeksLater := time.Now().Add(time.Hour * 24 * 21)
  264. // Should succeed because the server transparently re-signs one
  265. runAtDifferentDate(threeWeeksLater, func() {
  266. pushCmd := exec.Command(dockerBinary, "push", repoName)
  267. s.trustedCmd(pushCmd)
  268. out, _, err := runCommandWithOutput(pushCmd)
  269. c.Assert(err, check.IsNil, check.Commentf("Error running trusted push: %s\n%s", err, out))
  270. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with expired timestamp"))
  271. })
  272. }
  273. func (s *DockerTrustSuite) TestTrustedPushWithReleasesDelegation(c *check.C) {
  274. repoName := fmt.Sprintf("%v/dockerclireleasedelegation/trusted", privateRegistryURL)
  275. targetName := fmt.Sprintf("%s:latest", repoName)
  276. pwd := "12345678"
  277. s.setupDelegations(c, repoName, pwd)
  278. // tag the image and upload it to the private registry
  279. dockerCmd(c, "tag", "busybox", targetName)
  280. pushCmd := exec.Command(dockerBinary, "-D", "push", targetName)
  281. s.trustedCmdWithPassphrases(pushCmd, pwd, pwd)
  282. out, _, err := runCommandWithOutput(pushCmd)
  283. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  284. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  285. // Try pull after push
  286. pullCmd := exec.Command(dockerBinary, "pull", targetName)
  287. s.trustedCmd(pullCmd)
  288. out, _, err = runCommandWithOutput(pullCmd)
  289. c.Assert(err, check.IsNil, check.Commentf(out))
  290. c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf(out))
  291. // check to make sure that the target has been added to targets/releases and not targets
  292. contents, err := ioutil.ReadFile(filepath.Join(cliconfig.ConfigDir(), "trust/tuf", repoName, "metadata/targets.json"))
  293. c.Assert(err, check.IsNil, check.Commentf("Unable to read targets metadata"))
  294. c.Assert(strings.Contains(string(contents), `"latest"`), checker.False, check.Commentf(string(contents)))
  295. contents, err = ioutil.ReadFile(filepath.Join(cliconfig.ConfigDir(), "trust/tuf", repoName, "metadata/targets/releases.json"))
  296. c.Assert(err, check.IsNil, check.Commentf("Unable to read targets/releases metadata"))
  297. c.Assert(string(contents), checker.Contains, `"latest"`, check.Commentf(string(contents)))
  298. }