docker_cli_push_test.go 17 KB

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