docker_cli_push_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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 dockercli/busybox"), 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. }
  237. func (s *DockerTrustSuite) TestTrustedPushWithEnvPasswords(c *check.C) {
  238. repoName := fmt.Sprintf("%v/dockerclienv/trusted:latest", privateRegistryURL)
  239. // tag the image and upload it to the private registry
  240. dockerCmd(c, "tag", "busybox", repoName)
  241. pushCmd := exec.Command(dockerBinary, "push", repoName)
  242. s.trustedCmdWithPassphrases(pushCmd, "12345678", "12345678")
  243. out, _, err := runCommandWithOutput(pushCmd)
  244. c.Assert(err, check.IsNil, check.Commentf("Error running trusted push: %s\n%s", err, out))
  245. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  246. // Try pull after push
  247. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  248. s.trustedCmd(pullCmd)
  249. out, _, err = runCommandWithOutput(pullCmd)
  250. c.Assert(err, check.IsNil, check.Commentf(out))
  251. c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf(out))
  252. }
  253. // This test ensures backwards compatibility with old ENV variables. Should be
  254. // deprecated by 1.10
  255. func (s *DockerTrustSuite) TestTrustedPushWithDeprecatedEnvPasswords(c *check.C) {
  256. repoName := fmt.Sprintf("%v/dockercli/trusteddeprecated:latest", privateRegistryURL)
  257. // tag the image and upload it to the private registry
  258. dockerCmd(c, "tag", "busybox", repoName)
  259. pushCmd := exec.Command(dockerBinary, "push", repoName)
  260. s.trustedCmdWithDeprecatedEnvPassphrases(pushCmd, "12345678", "12345678")
  261. out, _, err := runCommandWithOutput(pushCmd)
  262. c.Assert(err, check.IsNil, check.Commentf("Error running trusted push: %s\n%s", err, out))
  263. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  264. }
  265. func (s *DockerTrustSuite) TestTrustedPushWithFailingServer(c *check.C) {
  266. repoName := fmt.Sprintf("%v/dockerclitrusted/failingserver:latest", privateRegistryURL)
  267. // tag the image and upload it to the private registry
  268. dockerCmd(c, "tag", "busybox", repoName)
  269. pushCmd := exec.Command(dockerBinary, "push", repoName)
  270. s.trustedCmdWithServer(pushCmd, "https://example.com:81/")
  271. out, _, err := runCommandWithOutput(pushCmd)
  272. c.Assert(err, check.NotNil, check.Commentf("Missing error while running trusted push w/ no server"))
  273. c.Assert(out, checker.Contains, "error contacting notary server", check.Commentf("Missing expected output on trusted push"))
  274. }
  275. func (s *DockerTrustSuite) TestTrustedPushWithoutServerAndUntrusted(c *check.C) {
  276. repoName := fmt.Sprintf("%v/dockerclitrusted/trustedandnot:latest", privateRegistryURL)
  277. // tag the image and upload it to the private registry
  278. dockerCmd(c, "tag", "busybox", repoName)
  279. pushCmd := exec.Command(dockerBinary, "push", "--disable-content-trust", repoName)
  280. s.trustedCmdWithServer(pushCmd, "https://example.com/")
  281. out, _, err := runCommandWithOutput(pushCmd)
  282. c.Assert(err, check.IsNil, check.Commentf("trusted push with no server and --disable-content-trust failed: %s\n%s", err, out))
  283. 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:"))
  284. }
  285. func (s *DockerTrustSuite) TestTrustedPushWithExistingTag(c *check.C) {
  286. repoName := fmt.Sprintf("%v/dockerclitag/trusted:latest", privateRegistryURL)
  287. // tag the image and upload it to the private registry
  288. dockerCmd(c, "tag", "busybox", repoName)
  289. dockerCmd(c, "push", repoName)
  290. pushCmd := exec.Command(dockerBinary, "push", repoName)
  291. s.trustedCmd(pushCmd)
  292. out, _, err := runCommandWithOutput(pushCmd)
  293. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  294. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  295. // Try pull after push
  296. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  297. s.trustedCmd(pullCmd)
  298. out, _, err = runCommandWithOutput(pullCmd)
  299. c.Assert(err, check.IsNil, check.Commentf(out))
  300. c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf(out))
  301. }
  302. func (s *DockerTrustSuite) TestTrustedPushWithExistingSignedTag(c *check.C) {
  303. repoName := fmt.Sprintf("%v/dockerclipushpush/trusted:latest", privateRegistryURL)
  304. // tag the image and upload it to the private registry
  305. dockerCmd(c, "tag", "busybox", repoName)
  306. // Do a trusted push
  307. pushCmd := exec.Command(dockerBinary, "push", repoName)
  308. s.trustedCmd(pushCmd)
  309. out, _, err := runCommandWithOutput(pushCmd)
  310. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  311. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  312. // Do another trusted push
  313. pushCmd = exec.Command(dockerBinary, "push", repoName)
  314. s.trustedCmd(pushCmd)
  315. out, _, err = runCommandWithOutput(pushCmd)
  316. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  317. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  318. dockerCmd(c, "rmi", repoName)
  319. // Try pull to ensure the double push did not break our ability to pull
  320. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  321. s.trustedCmd(pullCmd)
  322. out, _, err = runCommandWithOutput(pullCmd)
  323. c.Assert(err, check.IsNil, check.Commentf("Error running trusted pull: %s\n%s", err, out))
  324. c.Assert(out, checker.Contains, "Status: Downloaded", check.Commentf("Missing expected output on trusted pull with --disable-content-trust"))
  325. }
  326. func (s *DockerTrustSuite) TestTrustedPushWithIncorrectPassphraseForNonRoot(c *check.C) {
  327. repoName := fmt.Sprintf("%v/dockercliincorretpwd/trusted:latest", privateRegistryURL)
  328. // tag the image and upload it to the private registry
  329. dockerCmd(c, "tag", "busybox", repoName)
  330. // Push with default passphrases
  331. pushCmd := exec.Command(dockerBinary, "push", repoName)
  332. s.trustedCmd(pushCmd)
  333. out, _, err := runCommandWithOutput(pushCmd)
  334. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  335. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push:\n%s", out))
  336. // Push with wrong passphrases
  337. pushCmd = exec.Command(dockerBinary, "push", repoName)
  338. s.trustedCmdWithPassphrases(pushCmd, "12345678", "87654321")
  339. out, _, err = runCommandWithOutput(pushCmd)
  340. c.Assert(err, check.NotNil, check.Commentf("Error missing from trusted push with short targets passphrase: \n%s", out))
  341. c.Assert(out, checker.Contains, "could not find necessary signing keys", check.Commentf("Missing expected output on trusted push with short targets/snapsnot passphrase"))
  342. }
  343. // This test ensures backwards compatibility with old ENV variables. Should be
  344. // deprecated by 1.10
  345. func (s *DockerTrustSuite) TestTrustedPushWithIncorrectDeprecatedPassphraseForNonRoot(c *check.C) {
  346. repoName := fmt.Sprintf("%v/dockercliincorretdeprecatedpwd/trusted:latest", privateRegistryURL)
  347. // tag the image and upload it to the private registry
  348. dockerCmd(c, "tag", "busybox", repoName)
  349. // Push with default passphrases
  350. pushCmd := exec.Command(dockerBinary, "push", repoName)
  351. s.trustedCmd(pushCmd)
  352. out, _, err := runCommandWithOutput(pushCmd)
  353. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  354. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  355. // Push with wrong passphrases
  356. pushCmd = exec.Command(dockerBinary, "push", repoName)
  357. s.trustedCmdWithDeprecatedEnvPassphrases(pushCmd, "12345678", "87654321")
  358. out, _, err = runCommandWithOutput(pushCmd)
  359. c.Assert(err, check.NotNil, check.Commentf("Error missing from trusted push with short targets passphrase: \n%s", out))
  360. c.Assert(out, checker.Contains, "could not find necessary signing keys", check.Commentf("Missing expected output on trusted push with short targets/snapsnot passphrase"))
  361. }
  362. func (s *DockerTrustSuite) TestTrustedPushWithExpiredSnapshot(c *check.C) {
  363. c.Skip("Currently changes system time, causing instability")
  364. repoName := fmt.Sprintf("%v/dockercliexpiredsnapshot/trusted:latest", privateRegistryURL)
  365. // tag the image and upload it to the private registry
  366. dockerCmd(c, "tag", "busybox", repoName)
  367. // Push with default passphrases
  368. pushCmd := exec.Command(dockerBinary, "push", repoName)
  369. s.trustedCmd(pushCmd)
  370. out, _, err := runCommandWithOutput(pushCmd)
  371. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  372. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  373. // Snapshots last for three years. This should be expired
  374. fourYearsLater := time.Now().Add(time.Hour * 24 * 365 * 4)
  375. runAtDifferentDate(fourYearsLater, func() {
  376. // Push with wrong passphrases
  377. pushCmd = exec.Command(dockerBinary, "push", repoName)
  378. s.trustedCmd(pushCmd)
  379. out, _, err = runCommandWithOutput(pushCmd)
  380. c.Assert(err, check.NotNil, check.Commentf("Error missing from trusted push with expired snapshot: \n%s", out))
  381. c.Assert(out, checker.Contains, "repository out-of-date", check.Commentf("Missing expected output on trusted push with expired snapshot"))
  382. })
  383. }
  384. func (s *DockerTrustSuite) TestTrustedPushWithExpiredTimestamp(c *check.C) {
  385. c.Skip("Currently changes system time, causing instability")
  386. repoName := fmt.Sprintf("%v/dockercliexpiredtimestamppush/trusted:latest", privateRegistryURL)
  387. // tag the image and upload it to the private registry
  388. dockerCmd(c, "tag", "busybox", repoName)
  389. // Push with default passphrases
  390. pushCmd := exec.Command(dockerBinary, "push", repoName)
  391. s.trustedCmd(pushCmd)
  392. out, _, err := runCommandWithOutput(pushCmd)
  393. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  394. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  395. // The timestamps expire in two weeks. Lets check three
  396. threeWeeksLater := time.Now().Add(time.Hour * 24 * 21)
  397. // Should succeed because the server transparently re-signs one
  398. runAtDifferentDate(threeWeeksLater, func() {
  399. pushCmd := exec.Command(dockerBinary, "push", repoName)
  400. s.trustedCmd(pushCmd)
  401. out, _, err := runCommandWithOutput(pushCmd)
  402. c.Assert(err, check.IsNil, check.Commentf("Error running trusted push: %s\n%s", err, out))
  403. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with expired timestamp"))
  404. })
  405. }
  406. func (s *DockerTrustSuite) TestTrustedPushWithReleasesDelegation(c *check.C) {
  407. repoName := fmt.Sprintf("%v/dockerclireleasedelegation/trusted", privateRegistryURL)
  408. targetName := fmt.Sprintf("%s:latest", repoName)
  409. pwd := "12345678"
  410. s.setupDelegations(c, repoName, pwd)
  411. // tag the image and upload it to the private registry
  412. dockerCmd(c, "tag", "busybox", targetName)
  413. pushCmd := exec.Command(dockerBinary, "-D", "push", targetName)
  414. s.trustedCmdWithPassphrases(pushCmd, pwd, pwd)
  415. out, _, err := runCommandWithOutput(pushCmd)
  416. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  417. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  418. // Try pull after push
  419. pullCmd := exec.Command(dockerBinary, "pull", targetName)
  420. s.trustedCmd(pullCmd)
  421. out, _, err = runCommandWithOutput(pullCmd)
  422. c.Assert(err, check.IsNil, check.Commentf(out))
  423. c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf(out))
  424. // check to make sure that the target has been added to targets/releases and not targets
  425. contents, err := ioutil.ReadFile(filepath.Join(cliconfig.ConfigDir(), "trust/tuf", repoName, "metadata/targets.json"))
  426. c.Assert(err, check.IsNil, check.Commentf("Unable to read targets metadata"))
  427. c.Assert(strings.Contains(string(contents), `"latest"`), checker.False, check.Commentf(string(contents)))
  428. contents, err = ioutil.ReadFile(filepath.Join(cliconfig.ConfigDir(), "trust/tuf", repoName, "metadata/targets/releases.json"))
  429. c.Assert(err, check.IsNil, check.Commentf("Unable to read targets/releases metadata"))
  430. c.Assert(string(contents), checker.Contains, `"latest"`, check.Commentf(string(contents)))
  431. }