docker_cli_push_test.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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/distribution/digest"
  12. "github.com/docker/docker/cliconfig"
  13. "github.com/docker/docker/pkg/integration/checker"
  14. "github.com/go-check/check"
  15. )
  16. // Pushing an image to a private registry.
  17. func testPushBusyboxImage(c *check.C) {
  18. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  19. // tag the image to upload it to the private registry
  20. dockerCmd(c, "tag", "busybox", repoName)
  21. // push the image to the registry
  22. dockerCmd(c, "push", repoName)
  23. }
  24. func (s *DockerRegistrySuite) TestPushBusyboxImage(c *check.C) {
  25. testPushBusyboxImage(c)
  26. }
  27. func (s *DockerSchema1RegistrySuite) TestPushBusyboxImage(c *check.C) {
  28. testPushBusyboxImage(c)
  29. }
  30. // pushing an image without a prefix should throw an error
  31. func (s *DockerSuite) TestPushUnprefixedRepo(c *check.C) {
  32. out, _, err := dockerCmdWithError("push", "busybox")
  33. c.Assert(err, check.NotNil, check.Commentf("pushing an unprefixed repo didn't result in a non-zero exit status: %s", out))
  34. }
  35. func testPushUntagged(c *check.C) {
  36. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  37. expected := "Repository 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) TestPushUntagged(c *check.C) {
  43. testPushUntagged(c)
  44. }
  45. func (s *DockerSchema1RegistrySuite) TestPushUntagged(c *check.C) {
  46. testPushUntagged(c)
  47. }
  48. func testPushBadTag(c *check.C) {
  49. repoName := fmt.Sprintf("%v/dockercli/busybox:latest", privateRegistryURL)
  50. expected := "does not exist"
  51. out, _, err := dockerCmdWithError("push", repoName)
  52. c.Assert(err, check.NotNil, check.Commentf("pushing the image to the private registry should have failed: output %q", out))
  53. c.Assert(out, checker.Contains, expected, check.Commentf("pushing the image failed"))
  54. }
  55. func (s *DockerRegistrySuite) TestPushBadTag(c *check.C) {
  56. testPushBadTag(c)
  57. }
  58. func (s *DockerSchema1RegistrySuite) TestPushBadTag(c *check.C) {
  59. testPushBadTag(c)
  60. }
  61. func testPushMultipleTags(c *check.C) {
  62. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  63. repoTag1 := fmt.Sprintf("%v/dockercli/busybox:t1", privateRegistryURL)
  64. repoTag2 := fmt.Sprintf("%v/dockercli/busybox:t2", privateRegistryURL)
  65. // tag the image and upload it to the private registry
  66. dockerCmd(c, "tag", "busybox", repoTag1)
  67. dockerCmd(c, "tag", "busybox", repoTag2)
  68. dockerCmd(c, "push", repoName)
  69. // Ensure layer list is equivalent for repoTag1 and repoTag2
  70. out1, _ := dockerCmd(c, "pull", repoTag1)
  71. imageAlreadyExists := ": Image already exists"
  72. var out1Lines []string
  73. for _, outputLine := range strings.Split(out1, "\n") {
  74. if strings.Contains(outputLine, imageAlreadyExists) {
  75. out1Lines = append(out1Lines, outputLine)
  76. }
  77. }
  78. out2, _ := dockerCmd(c, "pull", repoTag2)
  79. var out2Lines []string
  80. for _, outputLine := range strings.Split(out2, "\n") {
  81. if strings.Contains(outputLine, imageAlreadyExists) {
  82. out1Lines = append(out1Lines, outputLine)
  83. }
  84. }
  85. c.Assert(out2Lines, checker.HasLen, len(out1Lines))
  86. for i := range out1Lines {
  87. c.Assert(out1Lines[i], checker.Equals, out2Lines[i])
  88. }
  89. }
  90. func (s *DockerRegistrySuite) TestPushMultipleTags(c *check.C) {
  91. testPushMultipleTags(c)
  92. }
  93. func (s *DockerSchema1RegistrySuite) TestPushMultipleTags(c *check.C) {
  94. testPushMultipleTags(c)
  95. }
  96. func testPushEmptyLayer(c *check.C) {
  97. repoName := fmt.Sprintf("%v/dockercli/emptylayer", privateRegistryURL)
  98. emptyTarball, err := ioutil.TempFile("", "empty_tarball")
  99. c.Assert(err, check.IsNil, check.Commentf("Unable to create test file"))
  100. tw := tar.NewWriter(emptyTarball)
  101. err = tw.Close()
  102. c.Assert(err, check.IsNil, check.Commentf("Error creating empty tarball"))
  103. freader, err := os.Open(emptyTarball.Name())
  104. c.Assert(err, check.IsNil, check.Commentf("Could not open test tarball"))
  105. importCmd := exec.Command(dockerBinary, "import", "-", repoName)
  106. importCmd.Stdin = freader
  107. out, _, err := runCommandWithOutput(importCmd)
  108. c.Assert(err, check.IsNil, check.Commentf("import failed: %q", out))
  109. // Now verify we can push it
  110. out, _, err = dockerCmdWithError("push", repoName)
  111. c.Assert(err, check.IsNil, check.Commentf("pushing the image to the private registry has failed: %s", out))
  112. }
  113. func (s *DockerRegistrySuite) TestPushEmptyLayer(c *check.C) {
  114. testPushEmptyLayer(c)
  115. }
  116. func (s *DockerSchema1RegistrySuite) TestPushEmptyLayer(c *check.C) {
  117. testPushEmptyLayer(c)
  118. }
  119. // testConcurrentPush pushes multiple tags to the same repo
  120. // concurrently.
  121. func testConcurrentPush(c *check.C) {
  122. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  123. repos := []string{}
  124. for _, tag := range []string{"push1", "push2", "push3"} {
  125. repo := fmt.Sprintf("%v:%v", repoName, tag)
  126. _, err := buildImage(repo, fmt.Sprintf(`
  127. FROM busybox
  128. ENTRYPOINT ["/bin/echo"]
  129. ENV FOO foo
  130. ENV BAR bar
  131. CMD echo %s
  132. `, repo), true)
  133. c.Assert(err, checker.IsNil)
  134. repos = append(repos, repo)
  135. }
  136. // Push tags, in parallel
  137. results := make(chan error)
  138. for _, repo := range repos {
  139. go func(repo string) {
  140. _, _, err := runCommandWithOutput(exec.Command(dockerBinary, "push", repo))
  141. results <- err
  142. }(repo)
  143. }
  144. for range repos {
  145. err := <-results
  146. c.Assert(err, checker.IsNil, check.Commentf("concurrent push failed with error: %v", err))
  147. }
  148. // Clear local images store.
  149. args := append([]string{"rmi"}, repos...)
  150. dockerCmd(c, args...)
  151. // Re-pull and run individual tags, to make sure pushes succeeded
  152. for _, repo := range repos {
  153. dockerCmd(c, "pull", repo)
  154. dockerCmd(c, "inspect", repo)
  155. out, _ := dockerCmd(c, "run", "--rm", repo)
  156. c.Assert(strings.TrimSpace(out), checker.Equals, "/bin/sh -c echo "+repo)
  157. }
  158. }
  159. func (s *DockerRegistrySuite) TestConcurrentPush(c *check.C) {
  160. testConcurrentPush(c)
  161. }
  162. func (s *DockerSchema1RegistrySuite) TestConcurrentPush(c *check.C) {
  163. testConcurrentPush(c)
  164. }
  165. func (s *DockerRegistrySuite) TestCrossRepositoryLayerPush(c *check.C) {
  166. sourceRepoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  167. // tag the image to upload it to the private registry
  168. dockerCmd(c, "tag", "busybox", sourceRepoName)
  169. // push the image to the registry
  170. out1, _, err := dockerCmdWithError("push", sourceRepoName)
  171. c.Assert(err, check.IsNil, check.Commentf("pushing the image to the private registry has failed: %s", out1))
  172. // ensure that none of the layers were mounted from another repository during push
  173. c.Assert(strings.Contains(out1, "Mounted from"), check.Equals, false)
  174. digest1 := digest.DigestRegexp.FindString(out1)
  175. c.Assert(len(digest1), checker.GreaterThan, 0, check.Commentf("no digest found for pushed manifest"))
  176. destRepoName := fmt.Sprintf("%v/dockercli/crossrepopush", privateRegistryURL)
  177. // retag the image to upload the same layers to another repo in the same registry
  178. dockerCmd(c, "tag", "busybox", destRepoName)
  179. // push the image to the registry
  180. out2, _, err := dockerCmdWithError("push", destRepoName)
  181. c.Assert(err, check.IsNil, check.Commentf("pushing the image to the private registry has failed: %s", out2))
  182. // ensure that layers were mounted from the first repo during push
  183. c.Assert(strings.Contains(out2, "Mounted from dockercli/busybox"), check.Equals, true)
  184. digest2 := digest.DigestRegexp.FindString(out2)
  185. c.Assert(len(digest2), checker.GreaterThan, 0, check.Commentf("no digest found for pushed manifest"))
  186. c.Assert(digest1, check.Equals, digest2)
  187. // ensure that we can pull and run the cross-repo-pushed repository
  188. dockerCmd(c, "rmi", destRepoName)
  189. dockerCmd(c, "pull", destRepoName)
  190. out3, _ := dockerCmd(c, "run", destRepoName, "echo", "-n", "hello world")
  191. c.Assert(out3, check.Equals, "hello world")
  192. }
  193. func (s *DockerSchema1RegistrySuite) TestCrossRepositoryLayerPushNotSupported(c *check.C) {
  194. sourceRepoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  195. // tag the image to upload it to the private registry
  196. dockerCmd(c, "tag", "busybox", sourceRepoName)
  197. // push the image to the registry
  198. out1, _, err := dockerCmdWithError("push", sourceRepoName)
  199. c.Assert(err, check.IsNil, check.Commentf("pushing the image to the private registry has failed: %s", out1))
  200. // ensure that none of the layers were mounted from another repository during push
  201. c.Assert(strings.Contains(out1, "Mounted from"), check.Equals, false)
  202. digest1 := digest.DigestRegexp.FindString(out1)
  203. c.Assert(len(digest1), checker.GreaterThan, 0, check.Commentf("no digest found for pushed manifest"))
  204. destRepoName := fmt.Sprintf("%v/dockercli/crossrepopush", privateRegistryURL)
  205. // retag the image to upload the same layers to another repo in the same registry
  206. dockerCmd(c, "tag", "busybox", destRepoName)
  207. // push the image to the registry
  208. out2, _, err := dockerCmdWithError("push", destRepoName)
  209. c.Assert(err, check.IsNil, check.Commentf("pushing the image to the private registry has failed: %s", out2))
  210. // schema1 registry should not support cross-repo layer mounts, so ensure that this does not happen
  211. c.Assert(strings.Contains(out2, "Mounted from"), check.Equals, false)
  212. digest2 := digest.DigestRegexp.FindString(out2)
  213. c.Assert(len(digest2), checker.GreaterThan, 0, check.Commentf("no digest found for pushed manifest"))
  214. c.Assert(digest1, check.Equals, digest2)
  215. // ensure that we can pull and run the second pushed repository
  216. dockerCmd(c, "rmi", destRepoName)
  217. dockerCmd(c, "pull", destRepoName)
  218. out3, _ := dockerCmd(c, "run", destRepoName, "echo", "-n", "hello world")
  219. c.Assert(out3, check.Equals, "hello world")
  220. }
  221. func (s *DockerTrustSuite) TestTrustedPush(c *check.C) {
  222. repoName := fmt.Sprintf("%v/dockerclitrusted/pushtest:latest", privateRegistryURL)
  223. // tag the image and upload it to the private registry
  224. dockerCmd(c, "tag", "busybox", repoName)
  225. pushCmd := exec.Command(dockerBinary, "push", repoName)
  226. s.trustedCmd(pushCmd)
  227. out, _, err := runCommandWithOutput(pushCmd)
  228. c.Assert(err, check.IsNil, check.Commentf("Error running trusted push: %s\n%s", err, out))
  229. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  230. // Try pull after push
  231. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  232. s.trustedCmd(pullCmd)
  233. out, _, err = runCommandWithOutput(pullCmd)
  234. c.Assert(err, check.IsNil, check.Commentf(out))
  235. c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf(out))
  236. // Assert that we rotated the snapshot key to the server by checking our local keystore
  237. contents, err := ioutil.ReadDir(filepath.Join(cliconfig.ConfigDir(), "trust/private/tuf_keys", privateRegistryURL, "dockerclitrusted/pushtest"))
  238. c.Assert(err, check.IsNil, check.Commentf("Unable to read local tuf key files"))
  239. // Check that we only have 1 key (targets key)
  240. c.Assert(contents, checker.HasLen, 1)
  241. }
  242. func (s *DockerTrustSuite) TestTrustedPushWithEnvPasswords(c *check.C) {
  243. repoName := fmt.Sprintf("%v/dockerclienv/trusted:latest", privateRegistryURL)
  244. // tag the image and upload it to the private registry
  245. dockerCmd(c, "tag", "busybox", repoName)
  246. pushCmd := exec.Command(dockerBinary, "push", repoName)
  247. s.trustedCmdWithPassphrases(pushCmd, "12345678", "12345678")
  248. out, _, err := runCommandWithOutput(pushCmd)
  249. c.Assert(err, check.IsNil, check.Commentf("Error running trusted push: %s\n%s", err, out))
  250. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  251. // Try pull after push
  252. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  253. s.trustedCmd(pullCmd)
  254. out, _, err = runCommandWithOutput(pullCmd)
  255. c.Assert(err, check.IsNil, check.Commentf(out))
  256. c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf(out))
  257. }
  258. // This test ensures backwards compatibility with old ENV variables. Should be
  259. // deprecated by 1.10
  260. func (s *DockerTrustSuite) TestTrustedPushWithDeprecatedEnvPasswords(c *check.C) {
  261. repoName := fmt.Sprintf("%v/dockercli/trusteddeprecated:latest", privateRegistryURL)
  262. // tag the image and upload it to the private registry
  263. dockerCmd(c, "tag", "busybox", repoName)
  264. pushCmd := exec.Command(dockerBinary, "push", repoName)
  265. s.trustedCmdWithDeprecatedEnvPassphrases(pushCmd, "12345678", "12345678")
  266. out, _, err := runCommandWithOutput(pushCmd)
  267. c.Assert(err, check.IsNil, check.Commentf("Error running trusted push: %s\n%s", err, out))
  268. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  269. }
  270. func (s *DockerTrustSuite) TestTrustedPushWithFailingServer(c *check.C) {
  271. repoName := fmt.Sprintf("%v/dockerclitrusted/failingserver:latest", privateRegistryURL)
  272. // tag the image and upload it to the private registry
  273. dockerCmd(c, "tag", "busybox", repoName)
  274. pushCmd := exec.Command(dockerBinary, "push", repoName)
  275. s.trustedCmdWithServer(pushCmd, "https://example.com:81/")
  276. out, _, err := runCommandWithOutput(pushCmd)
  277. c.Assert(err, check.NotNil, check.Commentf("Missing error while running trusted push w/ no server"))
  278. c.Assert(out, checker.Contains, "error contacting notary server", check.Commentf("Missing expected output on trusted push"))
  279. }
  280. func (s *DockerTrustSuite) TestTrustedPushWithoutServerAndUntrusted(c *check.C) {
  281. repoName := fmt.Sprintf("%v/dockerclitrusted/trustedandnot:latest", privateRegistryURL)
  282. // tag the image and upload it to the private registry
  283. dockerCmd(c, "tag", "busybox", repoName)
  284. pushCmd := exec.Command(dockerBinary, "push", "--disable-content-trust", repoName)
  285. s.trustedCmdWithServer(pushCmd, "https://example.com/")
  286. out, _, err := runCommandWithOutput(pushCmd)
  287. c.Assert(err, check.IsNil, check.Commentf("trusted push with no server and --disable-content-trust failed: %s\n%s", err, out))
  288. 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:"))
  289. }
  290. func (s *DockerTrustSuite) TestTrustedPushWithExistingTag(c *check.C) {
  291. repoName := fmt.Sprintf("%v/dockerclitag/trusted:latest", privateRegistryURL)
  292. // tag the image and upload it to the private registry
  293. dockerCmd(c, "tag", "busybox", repoName)
  294. dockerCmd(c, "push", repoName)
  295. pushCmd := exec.Command(dockerBinary, "push", repoName)
  296. s.trustedCmd(pushCmd)
  297. out, _, err := runCommandWithOutput(pushCmd)
  298. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  299. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  300. // Try pull after push
  301. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  302. s.trustedCmd(pullCmd)
  303. out, _, err = runCommandWithOutput(pullCmd)
  304. c.Assert(err, check.IsNil, check.Commentf(out))
  305. c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf(out))
  306. }
  307. func (s *DockerTrustSuite) TestTrustedPushWithExistingSignedTag(c *check.C) {
  308. repoName := fmt.Sprintf("%v/dockerclipushpush/trusted:latest", privateRegistryURL)
  309. // tag the image and upload it to the private registry
  310. dockerCmd(c, "tag", "busybox", repoName)
  311. // Do a trusted push
  312. pushCmd := exec.Command(dockerBinary, "push", repoName)
  313. s.trustedCmd(pushCmd)
  314. out, _, err := runCommandWithOutput(pushCmd)
  315. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  316. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  317. // Do another trusted push
  318. pushCmd = exec.Command(dockerBinary, "push", repoName)
  319. s.trustedCmd(pushCmd)
  320. out, _, err = runCommandWithOutput(pushCmd)
  321. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  322. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  323. dockerCmd(c, "rmi", repoName)
  324. // Try pull to ensure the double push did not break our ability to pull
  325. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  326. s.trustedCmd(pullCmd)
  327. out, _, err = runCommandWithOutput(pullCmd)
  328. c.Assert(err, check.IsNil, check.Commentf("Error running trusted pull: %s\n%s", err, out))
  329. c.Assert(out, checker.Contains, "Status: Downloaded", check.Commentf("Missing expected output on trusted pull with --disable-content-trust"))
  330. }
  331. func (s *DockerTrustSuite) TestTrustedPushWithIncorrectPassphraseForNonRoot(c *check.C) {
  332. repoName := fmt.Sprintf("%v/dockercliincorretpwd/trusted:latest", privateRegistryURL)
  333. // tag the image and upload it to the private registry
  334. dockerCmd(c, "tag", "busybox", repoName)
  335. // Push with default passphrases
  336. pushCmd := exec.Command(dockerBinary, "push", repoName)
  337. s.trustedCmd(pushCmd)
  338. out, _, err := runCommandWithOutput(pushCmd)
  339. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  340. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push:\n%s", out))
  341. // Push with wrong passphrases
  342. pushCmd = exec.Command(dockerBinary, "push", repoName)
  343. s.trustedCmdWithPassphrases(pushCmd, "12345678", "87654321")
  344. out, _, err = runCommandWithOutput(pushCmd)
  345. c.Assert(err, check.NotNil, check.Commentf("Error missing from trusted push with short targets passphrase: \n%s", out))
  346. c.Assert(out, checker.Contains, "could not find necessary signing keys", check.Commentf("Missing expected output on trusted push with short targets/snapsnot passphrase"))
  347. }
  348. // This test ensures backwards compatibility with old ENV variables. Should be
  349. // deprecated by 1.10
  350. func (s *DockerTrustSuite) TestTrustedPushWithIncorrectDeprecatedPassphraseForNonRoot(c *check.C) {
  351. repoName := fmt.Sprintf("%v/dockercliincorretdeprecatedpwd/trusted:latest", privateRegistryURL)
  352. // tag the image and upload it to the private registry
  353. dockerCmd(c, "tag", "busybox", repoName)
  354. // Push with default passphrases
  355. pushCmd := exec.Command(dockerBinary, "push", repoName)
  356. s.trustedCmd(pushCmd)
  357. out, _, err := runCommandWithOutput(pushCmd)
  358. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  359. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  360. // Push with wrong passphrases
  361. pushCmd = exec.Command(dockerBinary, "push", repoName)
  362. s.trustedCmdWithDeprecatedEnvPassphrases(pushCmd, "12345678", "87654321")
  363. out, _, err = runCommandWithOutput(pushCmd)
  364. c.Assert(err, check.NotNil, check.Commentf("Error missing from trusted push with short targets passphrase: \n%s", out))
  365. c.Assert(out, checker.Contains, "could not find necessary signing keys", check.Commentf("Missing expected output on trusted push with short targets/snapsnot passphrase"))
  366. }
  367. func (s *DockerTrustSuite) TestTrustedPushWithExpiredSnapshot(c *check.C) {
  368. c.Skip("Currently changes system time, causing instability")
  369. repoName := fmt.Sprintf("%v/dockercliexpiredsnapshot/trusted:latest", privateRegistryURL)
  370. // tag the image and upload it to the private registry
  371. dockerCmd(c, "tag", "busybox", repoName)
  372. // Push with default passphrases
  373. pushCmd := exec.Command(dockerBinary, "push", repoName)
  374. s.trustedCmd(pushCmd)
  375. out, _, err := runCommandWithOutput(pushCmd)
  376. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  377. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  378. // Snapshots last for three years. This should be expired
  379. fourYearsLater := time.Now().Add(time.Hour * 24 * 365 * 4)
  380. runAtDifferentDate(fourYearsLater, func() {
  381. // Push with wrong passphrases
  382. pushCmd = exec.Command(dockerBinary, "push", repoName)
  383. s.trustedCmd(pushCmd)
  384. out, _, err = runCommandWithOutput(pushCmd)
  385. c.Assert(err, check.NotNil, check.Commentf("Error missing from trusted push with expired snapshot: \n%s", out))
  386. c.Assert(out, checker.Contains, "repository out-of-date", check.Commentf("Missing expected output on trusted push with expired snapshot"))
  387. })
  388. }
  389. func (s *DockerTrustSuite) TestTrustedPushWithExpiredTimestamp(c *check.C) {
  390. c.Skip("Currently changes system time, causing instability")
  391. repoName := fmt.Sprintf("%v/dockercliexpiredtimestamppush/trusted:latest", privateRegistryURL)
  392. // tag the image and upload it to the private registry
  393. dockerCmd(c, "tag", "busybox", repoName)
  394. // Push with default passphrases
  395. pushCmd := exec.Command(dockerBinary, "push", repoName)
  396. s.trustedCmd(pushCmd)
  397. out, _, err := runCommandWithOutput(pushCmd)
  398. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  399. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  400. // The timestamps expire in two weeks. Lets check three
  401. threeWeeksLater := time.Now().Add(time.Hour * 24 * 21)
  402. // Should succeed because the server transparently re-signs one
  403. runAtDifferentDate(threeWeeksLater, func() {
  404. pushCmd := exec.Command(dockerBinary, "push", repoName)
  405. s.trustedCmd(pushCmd)
  406. out, _, err := runCommandWithOutput(pushCmd)
  407. c.Assert(err, check.IsNil, check.Commentf("Error running trusted push: %s\n%s", err, out))
  408. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with expired timestamp"))
  409. })
  410. }
  411. func (s *DockerTrustSuite) TestTrustedPushWithReleasesDelegation(c *check.C) {
  412. repoName := fmt.Sprintf("%v/dockerclireleasedelegation/trusted", privateRegistryURL)
  413. targetName := fmt.Sprintf("%s:latest", repoName)
  414. pwd := "12345678"
  415. s.setupDelegations(c, repoName, pwd)
  416. // tag the image and upload it to the private registry
  417. dockerCmd(c, "tag", "busybox", targetName)
  418. pushCmd := exec.Command(dockerBinary, "-D", "push", targetName)
  419. s.trustedCmdWithPassphrases(pushCmd, pwd, pwd)
  420. out, _, err := runCommandWithOutput(pushCmd)
  421. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  422. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  423. // Try pull after push
  424. pullCmd := exec.Command(dockerBinary, "pull", targetName)
  425. s.trustedCmd(pullCmd)
  426. out, _, err = runCommandWithOutput(pullCmd)
  427. c.Assert(err, check.IsNil, check.Commentf(out))
  428. c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf(out))
  429. // check to make sure that the target has been added to targets/releases and not targets
  430. contents, err := ioutil.ReadFile(filepath.Join(cliconfig.ConfigDir(), "trust/tuf", repoName, "metadata/targets.json"))
  431. c.Assert(err, check.IsNil, check.Commentf("Unable to read targets metadata"))
  432. c.Assert(strings.Contains(string(contents), `"latest"`), checker.False, check.Commentf(string(contents)))
  433. contents, err = ioutil.ReadFile(filepath.Join(cliconfig.ConfigDir(), "trust/tuf", repoName, "metadata/targets/releases.json"))
  434. c.Assert(err, check.IsNil, check.Commentf("Unable to read targets/releases metadata"))
  435. c.Assert(string(contents), checker.Contains, `"latest"`, check.Commentf(string(contents)))
  436. }
  437. func (s *DockerRegistryAuthSuite) TestPushNoCredentialsNoRetry(c *check.C) {
  438. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  439. dockerCmd(c, "tag", "busybox", repoName)
  440. out, _, err := dockerCmdWithError("push", repoName)
  441. c.Assert(err, check.NotNil, check.Commentf(out))
  442. c.Assert(out, check.Not(checker.Contains), "Retrying")
  443. c.Assert(out, checker.Contains, "no basic auth credentials")
  444. }