docker_cli_push_test.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. package main
  2. import (
  3. "archive/tar"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "net/http/httptest"
  8. "os"
  9. "os/exec"
  10. "path/filepath"
  11. "strings"
  12. "time"
  13. "github.com/docker/distribution/digest"
  14. "github.com/docker/docker/cliconfig"
  15. "github.com/docker/docker/pkg/integration/checker"
  16. "github.com/go-check/check"
  17. )
  18. // Pushing an image to a private registry.
  19. func testPushBusyboxImage(c *check.C) {
  20. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  21. // tag the image to upload it to the private registry
  22. dockerCmd(c, "tag", "busybox", repoName)
  23. // push the image to the registry
  24. dockerCmd(c, "push", repoName)
  25. }
  26. func (s *DockerRegistrySuite) TestPushBusyboxImage(c *check.C) {
  27. testPushBusyboxImage(c)
  28. }
  29. func (s *DockerSchema1RegistrySuite) TestPushBusyboxImage(c *check.C) {
  30. testPushBusyboxImage(c)
  31. }
  32. // pushing an image without a prefix should throw an error
  33. func (s *DockerSuite) TestPushUnprefixedRepo(c *check.C) {
  34. out, _, err := dockerCmdWithError("push", "busybox")
  35. c.Assert(err, check.NotNil, check.Commentf("pushing an unprefixed repo didn't result in a non-zero exit status: %s", out))
  36. }
  37. func testPushUntagged(c *check.C) {
  38. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  39. expected := "An image does not exist locally with the tag"
  40. out, _, err := dockerCmdWithError("push", repoName)
  41. c.Assert(err, check.NotNil, check.Commentf("pushing the image to the private registry should have failed: output %q", out))
  42. c.Assert(out, checker.Contains, expected, check.Commentf("pushing the image failed"))
  43. }
  44. func (s *DockerRegistrySuite) TestPushUntagged(c *check.C) {
  45. testPushUntagged(c)
  46. }
  47. func (s *DockerSchema1RegistrySuite) TestPushUntagged(c *check.C) {
  48. testPushUntagged(c)
  49. }
  50. func testPushBadTag(c *check.C) {
  51. repoName := fmt.Sprintf("%v/dockercli/busybox:latest", privateRegistryURL)
  52. expected := "does not exist"
  53. out, _, err := dockerCmdWithError("push", repoName)
  54. c.Assert(err, check.NotNil, check.Commentf("pushing the image to the private registry should have failed: output %q", out))
  55. c.Assert(out, checker.Contains, expected, check.Commentf("pushing the image failed"))
  56. }
  57. func (s *DockerRegistrySuite) TestPushBadTag(c *check.C) {
  58. testPushBadTag(c)
  59. }
  60. func (s *DockerSchema1RegistrySuite) TestPushBadTag(c *check.C) {
  61. testPushBadTag(c)
  62. }
  63. func testPushMultipleTags(c *check.C) {
  64. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  65. repoTag1 := fmt.Sprintf("%v/dockercli/busybox:t1", privateRegistryURL)
  66. repoTag2 := fmt.Sprintf("%v/dockercli/busybox:t2", privateRegistryURL)
  67. // tag the image and upload it to the private registry
  68. dockerCmd(c, "tag", "busybox", repoTag1)
  69. dockerCmd(c, "tag", "busybox", repoTag2)
  70. dockerCmd(c, "push", repoName)
  71. // Ensure layer list is equivalent for repoTag1 and repoTag2
  72. out1, _ := dockerCmd(c, "pull", repoTag1)
  73. imageAlreadyExists := ": Image already exists"
  74. var out1Lines []string
  75. for _, outputLine := range strings.Split(out1, "\n") {
  76. if strings.Contains(outputLine, imageAlreadyExists) {
  77. out1Lines = append(out1Lines, outputLine)
  78. }
  79. }
  80. out2, _ := dockerCmd(c, "pull", repoTag2)
  81. var out2Lines []string
  82. for _, outputLine := range strings.Split(out2, "\n") {
  83. if strings.Contains(outputLine, imageAlreadyExists) {
  84. out1Lines = append(out1Lines, outputLine)
  85. }
  86. }
  87. c.Assert(out2Lines, checker.HasLen, len(out1Lines))
  88. for i := range out1Lines {
  89. c.Assert(out1Lines[i], checker.Equals, out2Lines[i])
  90. }
  91. }
  92. func (s *DockerRegistrySuite) TestPushMultipleTags(c *check.C) {
  93. testPushMultipleTags(c)
  94. }
  95. func (s *DockerSchema1RegistrySuite) TestPushMultipleTags(c *check.C) {
  96. testPushMultipleTags(c)
  97. }
  98. func testPushEmptyLayer(c *check.C) {
  99. repoName := fmt.Sprintf("%v/dockercli/emptylayer", privateRegistryURL)
  100. emptyTarball, err := ioutil.TempFile("", "empty_tarball")
  101. c.Assert(err, check.IsNil, check.Commentf("Unable to create test file"))
  102. tw := tar.NewWriter(emptyTarball)
  103. err = tw.Close()
  104. c.Assert(err, check.IsNil, check.Commentf("Error creating empty tarball"))
  105. freader, err := os.Open(emptyTarball.Name())
  106. c.Assert(err, check.IsNil, check.Commentf("Could not open test tarball"))
  107. importCmd := exec.Command(dockerBinary, "import", "-", repoName)
  108. importCmd.Stdin = freader
  109. out, _, err := runCommandWithOutput(importCmd)
  110. c.Assert(err, check.IsNil, check.Commentf("import failed: %q", out))
  111. // Now verify we can push it
  112. out, _, err = dockerCmdWithError("push", repoName)
  113. c.Assert(err, check.IsNil, check.Commentf("pushing the image to the private registry has failed: %s", out))
  114. }
  115. func (s *DockerRegistrySuite) TestPushEmptyLayer(c *check.C) {
  116. testPushEmptyLayer(c)
  117. }
  118. func (s *DockerSchema1RegistrySuite) TestPushEmptyLayer(c *check.C) {
  119. testPushEmptyLayer(c)
  120. }
  121. // testConcurrentPush pushes multiple tags to the same repo
  122. // concurrently.
  123. func testConcurrentPush(c *check.C) {
  124. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  125. repos := []string{}
  126. for _, tag := range []string{"push1", "push2", "push3"} {
  127. repo := fmt.Sprintf("%v:%v", repoName, tag)
  128. _, err := buildImage(repo, fmt.Sprintf(`
  129. FROM busybox
  130. ENTRYPOINT ["/bin/echo"]
  131. ENV FOO foo
  132. ENV BAR bar
  133. CMD echo %s
  134. `, repo), true)
  135. c.Assert(err, checker.IsNil)
  136. repos = append(repos, repo)
  137. }
  138. // Push tags, in parallel
  139. results := make(chan error)
  140. for _, repo := range repos {
  141. go func(repo string) {
  142. _, _, err := runCommandWithOutput(exec.Command(dockerBinary, "push", repo))
  143. results <- err
  144. }(repo)
  145. }
  146. for range repos {
  147. err := <-results
  148. c.Assert(err, checker.IsNil, check.Commentf("concurrent push failed with error: %v", err))
  149. }
  150. // Clear local images store.
  151. args := append([]string{"rmi"}, repos...)
  152. dockerCmd(c, args...)
  153. // Re-pull and run individual tags, to make sure pushes succeeded
  154. for _, repo := range repos {
  155. dockerCmd(c, "pull", repo)
  156. dockerCmd(c, "inspect", repo)
  157. out, _ := dockerCmd(c, "run", "--rm", repo)
  158. c.Assert(strings.TrimSpace(out), checker.Equals, "/bin/sh -c echo "+repo)
  159. }
  160. }
  161. func (s *DockerRegistrySuite) TestConcurrentPush(c *check.C) {
  162. testConcurrentPush(c)
  163. }
  164. func (s *DockerSchema1RegistrySuite) TestConcurrentPush(c *check.C) {
  165. testConcurrentPush(c)
  166. }
  167. func (s *DockerRegistrySuite) TestCrossRepositoryLayerPush(c *check.C) {
  168. sourceRepoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  169. // tag the image to upload it to the private registry
  170. dockerCmd(c, "tag", "busybox", sourceRepoName)
  171. // push the image to the registry
  172. out1, _, err := dockerCmdWithError("push", sourceRepoName)
  173. c.Assert(err, check.IsNil, check.Commentf("pushing the image to the private registry has failed: %s", out1))
  174. // ensure that none of the layers were mounted from another repository during push
  175. c.Assert(strings.Contains(out1, "Mounted from"), check.Equals, false)
  176. digest1 := digest.DigestRegexp.FindString(out1)
  177. c.Assert(len(digest1), checker.GreaterThan, 0, check.Commentf("no digest found for pushed manifest"))
  178. destRepoName := fmt.Sprintf("%v/dockercli/crossrepopush", privateRegistryURL)
  179. // retag the image to upload the same layers to another repo in the same registry
  180. dockerCmd(c, "tag", "busybox", destRepoName)
  181. // push the image to the registry
  182. out2, _, err := dockerCmdWithError("push", destRepoName)
  183. c.Assert(err, check.IsNil, check.Commentf("pushing the image to the private registry has failed: %s", out2))
  184. // ensure that layers were mounted from the first repo during push
  185. c.Assert(strings.Contains(out2, "Mounted from dockercli/busybox"), check.Equals, true)
  186. digest2 := digest.DigestRegexp.FindString(out2)
  187. c.Assert(len(digest2), checker.GreaterThan, 0, check.Commentf("no digest found for pushed manifest"))
  188. c.Assert(digest1, check.Equals, digest2)
  189. // ensure that we can pull and run the cross-repo-pushed repository
  190. dockerCmd(c, "rmi", destRepoName)
  191. dockerCmd(c, "pull", destRepoName)
  192. out3, _ := dockerCmd(c, "run", destRepoName, "echo", "-n", "hello world")
  193. c.Assert(out3, check.Equals, "hello world")
  194. }
  195. func (s *DockerSchema1RegistrySuite) TestCrossRepositoryLayerPushNotSupported(c *check.C) {
  196. sourceRepoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  197. // tag the image to upload it to the private registry
  198. dockerCmd(c, "tag", "busybox", sourceRepoName)
  199. // push the image to the registry
  200. out1, _, err := dockerCmdWithError("push", sourceRepoName)
  201. c.Assert(err, check.IsNil, check.Commentf("pushing the image to the private registry has failed: %s", out1))
  202. // ensure that none of the layers were mounted from another repository during push
  203. c.Assert(strings.Contains(out1, "Mounted from"), check.Equals, false)
  204. digest1 := digest.DigestRegexp.FindString(out1)
  205. c.Assert(len(digest1), checker.GreaterThan, 0, check.Commentf("no digest found for pushed manifest"))
  206. destRepoName := fmt.Sprintf("%v/dockercli/crossrepopush", privateRegistryURL)
  207. // retag the image to upload the same layers to another repo in the same registry
  208. dockerCmd(c, "tag", "busybox", destRepoName)
  209. // push the image to the registry
  210. out2, _, err := dockerCmdWithError("push", destRepoName)
  211. c.Assert(err, check.IsNil, check.Commentf("pushing the image to the private registry has failed: %s", out2))
  212. // schema1 registry should not support cross-repo layer mounts, so ensure that this does not happen
  213. c.Assert(strings.Contains(out2, "Mounted from"), check.Equals, false)
  214. digest2 := digest.DigestRegexp.FindString(out2)
  215. c.Assert(len(digest2), checker.GreaterThan, 0, check.Commentf("no digest found for pushed manifest"))
  216. c.Assert(digest1, check.Equals, digest2)
  217. // ensure that we can pull and run the second pushed repository
  218. dockerCmd(c, "rmi", destRepoName)
  219. dockerCmd(c, "pull", destRepoName)
  220. out3, _ := dockerCmd(c, "run", destRepoName, "echo", "-n", "hello world")
  221. c.Assert(out3, check.Equals, "hello world")
  222. }
  223. func (s *DockerTrustSuite) TestTrustedPush(c *check.C) {
  224. repoName := fmt.Sprintf("%v/dockerclitrusted/pushtest:latest", privateRegistryURL)
  225. // tag the image and upload it to the private registry
  226. dockerCmd(c, "tag", "busybox", repoName)
  227. pushCmd := exec.Command(dockerBinary, "push", repoName)
  228. s.trustedCmd(pushCmd)
  229. out, _, err := runCommandWithOutput(pushCmd)
  230. c.Assert(err, check.IsNil, check.Commentf("Error running trusted push: %s\n%s", err, out))
  231. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  232. // Try pull after push
  233. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  234. s.trustedCmd(pullCmd)
  235. out, _, err = runCommandWithOutput(pullCmd)
  236. c.Assert(err, check.IsNil, check.Commentf(out))
  237. c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf(out))
  238. // Assert that we rotated the snapshot key to the server by checking our local keystore
  239. contents, err := ioutil.ReadDir(filepath.Join(cliconfig.ConfigDir(), "trust/private/tuf_keys", privateRegistryURL, "dockerclitrusted/pushtest"))
  240. c.Assert(err, check.IsNil, check.Commentf("Unable to read local tuf key files"))
  241. // Check that we only have 1 key (targets key)
  242. c.Assert(contents, checker.HasLen, 1)
  243. }
  244. func (s *DockerTrustSuite) TestTrustedPushWithEnvPasswords(c *check.C) {
  245. repoName := fmt.Sprintf("%v/dockerclienv/trusted:latest", privateRegistryURL)
  246. // tag the image and upload it to the private registry
  247. dockerCmd(c, "tag", "busybox", repoName)
  248. pushCmd := exec.Command(dockerBinary, "push", repoName)
  249. s.trustedCmdWithPassphrases(pushCmd, "12345678", "12345678")
  250. out, _, err := runCommandWithOutput(pushCmd)
  251. c.Assert(err, check.IsNil, check.Commentf("Error running trusted push: %s\n%s", err, out))
  252. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  253. // Try pull after push
  254. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  255. s.trustedCmd(pullCmd)
  256. out, _, err = runCommandWithOutput(pullCmd)
  257. c.Assert(err, check.IsNil, check.Commentf(out))
  258. c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf(out))
  259. }
  260. // This test ensures backwards compatibility with old ENV variables. Should be
  261. // deprecated by 1.10
  262. func (s *DockerTrustSuite) TestTrustedPushWithDeprecatedEnvPasswords(c *check.C) {
  263. repoName := fmt.Sprintf("%v/dockercli/trusteddeprecated:latest", privateRegistryURL)
  264. // tag the image and upload it to the private registry
  265. dockerCmd(c, "tag", "busybox", repoName)
  266. pushCmd := exec.Command(dockerBinary, "push", repoName)
  267. s.trustedCmdWithDeprecatedEnvPassphrases(pushCmd, "12345678", "12345678")
  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"))
  271. }
  272. func (s *DockerTrustSuite) TestTrustedPushWithFailingServer(c *check.C) {
  273. repoName := fmt.Sprintf("%v/dockerclitrusted/failingserver:latest", privateRegistryURL)
  274. // tag the image and upload it to the private registry
  275. dockerCmd(c, "tag", "busybox", repoName)
  276. pushCmd := exec.Command(dockerBinary, "push", repoName)
  277. s.trustedCmdWithServer(pushCmd, "https://example.com:81/")
  278. out, _, err := runCommandWithOutput(pushCmd)
  279. c.Assert(err, check.NotNil, check.Commentf("Missing error while running trusted push w/ no server"))
  280. c.Assert(out, checker.Contains, "error contacting notary server", check.Commentf("Missing expected output on trusted push"))
  281. }
  282. func (s *DockerTrustSuite) TestTrustedPushWithoutServerAndUntrusted(c *check.C) {
  283. repoName := fmt.Sprintf("%v/dockerclitrusted/trustedandnot:latest", privateRegistryURL)
  284. // tag the image and upload it to the private registry
  285. dockerCmd(c, "tag", "busybox", repoName)
  286. pushCmd := exec.Command(dockerBinary, "push", "--disable-content-trust", repoName)
  287. s.trustedCmdWithServer(pushCmd, "https://example.com/")
  288. out, _, err := runCommandWithOutput(pushCmd)
  289. c.Assert(err, check.IsNil, check.Commentf("trusted push with no server and --disable-content-trust failed: %s\n%s", err, out))
  290. 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:"))
  291. }
  292. func (s *DockerTrustSuite) TestTrustedPushWithExistingTag(c *check.C) {
  293. repoName := fmt.Sprintf("%v/dockerclitag/trusted:latest", privateRegistryURL)
  294. // tag the image and upload it to the private registry
  295. dockerCmd(c, "tag", "busybox", repoName)
  296. dockerCmd(c, "push", repoName)
  297. pushCmd := exec.Command(dockerBinary, "push", repoName)
  298. s.trustedCmd(pushCmd)
  299. out, _, err := runCommandWithOutput(pushCmd)
  300. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  301. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  302. // Try pull after push
  303. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  304. s.trustedCmd(pullCmd)
  305. out, _, err = runCommandWithOutput(pullCmd)
  306. c.Assert(err, check.IsNil, check.Commentf(out))
  307. c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf(out))
  308. }
  309. func (s *DockerTrustSuite) TestTrustedPushWithExistingSignedTag(c *check.C) {
  310. repoName := fmt.Sprintf("%v/dockerclipushpush/trusted:latest", privateRegistryURL)
  311. // tag the image and upload it to the private registry
  312. dockerCmd(c, "tag", "busybox", repoName)
  313. // Do a trusted push
  314. pushCmd := exec.Command(dockerBinary, "push", repoName)
  315. s.trustedCmd(pushCmd)
  316. out, _, err := runCommandWithOutput(pushCmd)
  317. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  318. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  319. // Do another trusted push
  320. pushCmd = exec.Command(dockerBinary, "push", repoName)
  321. s.trustedCmd(pushCmd)
  322. out, _, err = runCommandWithOutput(pushCmd)
  323. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  324. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  325. dockerCmd(c, "rmi", repoName)
  326. // Try pull to ensure the double push did not break our ability to pull
  327. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  328. s.trustedCmd(pullCmd)
  329. out, _, err = runCommandWithOutput(pullCmd)
  330. c.Assert(err, check.IsNil, check.Commentf("Error running trusted pull: %s\n%s", err, out))
  331. c.Assert(out, checker.Contains, "Status: Downloaded", check.Commentf("Missing expected output on trusted pull with --disable-content-trust"))
  332. }
  333. func (s *DockerTrustSuite) TestTrustedPushWithIncorrectPassphraseForNonRoot(c *check.C) {
  334. repoName := fmt.Sprintf("%v/dockercliincorretpwd/trusted:latest", privateRegistryURL)
  335. // tag the image and upload it to the private registry
  336. dockerCmd(c, "tag", "busybox", repoName)
  337. // Push with default passphrases
  338. pushCmd := exec.Command(dockerBinary, "push", repoName)
  339. s.trustedCmd(pushCmd)
  340. out, _, err := runCommandWithOutput(pushCmd)
  341. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  342. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push:\n%s", out))
  343. // Push with wrong passphrases
  344. pushCmd = exec.Command(dockerBinary, "push", repoName)
  345. s.trustedCmdWithPassphrases(pushCmd, "12345678", "87654321")
  346. out, _, err = runCommandWithOutput(pushCmd)
  347. c.Assert(err, check.NotNil, check.Commentf("Error missing from trusted push with short targets passphrase: \n%s", out))
  348. c.Assert(out, checker.Contains, "could not find necessary signing keys", check.Commentf("Missing expected output on trusted push with short targets/snapsnot passphrase"))
  349. }
  350. // This test ensures backwards compatibility with old ENV variables. Should be
  351. // deprecated by 1.10
  352. func (s *DockerTrustSuite) TestTrustedPushWithIncorrectDeprecatedPassphraseForNonRoot(c *check.C) {
  353. repoName := fmt.Sprintf("%v/dockercliincorretdeprecatedpwd/trusted:latest", privateRegistryURL)
  354. // tag the image and upload it to the private registry
  355. dockerCmd(c, "tag", "busybox", repoName)
  356. // Push with default passphrases
  357. pushCmd := exec.Command(dockerBinary, "push", repoName)
  358. s.trustedCmd(pushCmd)
  359. out, _, err := runCommandWithOutput(pushCmd)
  360. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  361. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  362. // Push with wrong passphrases
  363. pushCmd = exec.Command(dockerBinary, "push", repoName)
  364. s.trustedCmdWithDeprecatedEnvPassphrases(pushCmd, "12345678", "87654321")
  365. out, _, err = runCommandWithOutput(pushCmd)
  366. c.Assert(err, check.NotNil, check.Commentf("Error missing from trusted push with short targets passphrase: \n%s", out))
  367. c.Assert(out, checker.Contains, "could not find necessary signing keys", check.Commentf("Missing expected output on trusted push with short targets/snapsnot passphrase"))
  368. }
  369. func (s *DockerTrustSuite) TestTrustedPushWithExpiredSnapshot(c *check.C) {
  370. c.Skip("Currently changes system time, causing instability")
  371. repoName := fmt.Sprintf("%v/dockercliexpiredsnapshot/trusted:latest", privateRegistryURL)
  372. // tag the image and upload it to the private registry
  373. dockerCmd(c, "tag", "busybox", repoName)
  374. // Push with default passphrases
  375. pushCmd := exec.Command(dockerBinary, "push", repoName)
  376. s.trustedCmd(pushCmd)
  377. out, _, err := runCommandWithOutput(pushCmd)
  378. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  379. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  380. // Snapshots last for three years. This should be expired
  381. fourYearsLater := time.Now().Add(time.Hour * 24 * 365 * 4)
  382. runAtDifferentDate(fourYearsLater, func() {
  383. // Push with wrong passphrases
  384. pushCmd = exec.Command(dockerBinary, "push", repoName)
  385. s.trustedCmd(pushCmd)
  386. out, _, err = runCommandWithOutput(pushCmd)
  387. c.Assert(err, check.NotNil, check.Commentf("Error missing from trusted push with expired snapshot: \n%s", out))
  388. c.Assert(out, checker.Contains, "repository out-of-date", check.Commentf("Missing expected output on trusted push with expired snapshot"))
  389. })
  390. }
  391. func (s *DockerTrustSuite) TestTrustedPushWithExpiredTimestamp(c *check.C) {
  392. c.Skip("Currently changes system time, causing instability")
  393. repoName := fmt.Sprintf("%v/dockercliexpiredtimestamppush/trusted:latest", privateRegistryURL)
  394. // tag the image and upload it to the private registry
  395. dockerCmd(c, "tag", "busybox", repoName)
  396. // Push with default passphrases
  397. pushCmd := exec.Command(dockerBinary, "push", repoName)
  398. s.trustedCmd(pushCmd)
  399. out, _, err := runCommandWithOutput(pushCmd)
  400. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  401. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  402. // The timestamps expire in two weeks. Lets check three
  403. threeWeeksLater := time.Now().Add(time.Hour * 24 * 21)
  404. // Should succeed because the server transparently re-signs one
  405. runAtDifferentDate(threeWeeksLater, func() {
  406. pushCmd := exec.Command(dockerBinary, "push", repoName)
  407. s.trustedCmd(pushCmd)
  408. out, _, err := runCommandWithOutput(pushCmd)
  409. c.Assert(err, check.IsNil, check.Commentf("Error running trusted push: %s\n%s", err, out))
  410. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with expired timestamp"))
  411. })
  412. }
  413. func (s *DockerTrustSuite) TestTrustedPushWithReleasesDelegationOnly(c *check.C) {
  414. testRequires(c, NotaryHosting)
  415. repoName := fmt.Sprintf("%v/dockerclireleasedelegationinitfirst/trusted", privateRegistryURL)
  416. targetName := fmt.Sprintf("%s:latest", repoName)
  417. s.notaryInitRepo(c, repoName)
  418. s.notaryCreateDelegation(c, repoName, "targets/releases", s.not.keys[0].Public)
  419. s.notaryPublish(c, repoName)
  420. s.notaryImportKey(c, repoName, "targets/releases", s.not.keys[0].Private)
  421. // tag the image and upload it to the private registry
  422. dockerCmd(c, "tag", "busybox", targetName)
  423. pushCmd := exec.Command(dockerBinary, "push", targetName)
  424. s.trustedCmd(pushCmd)
  425. out, _, err := runCommandWithOutput(pushCmd)
  426. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  427. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  428. // check to make sure that the target has been added to targets/releases and not targets
  429. s.assertTargetInRoles(c, repoName, "latest", "targets/releases")
  430. s.assertTargetNotInRoles(c, repoName, "latest", "targets")
  431. // Try pull after push
  432. os.RemoveAll(filepath.Join(cliconfig.ConfigDir(), "trust"))
  433. pullCmd := exec.Command(dockerBinary, "pull", targetName)
  434. s.trustedCmd(pullCmd)
  435. out, _, err = runCommandWithOutput(pullCmd)
  436. c.Assert(err, check.IsNil, check.Commentf(out))
  437. c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf(out))
  438. }
  439. func (s *DockerTrustSuite) TestTrustedPushSignsAllFirstLevelRolesWeHaveKeysFor(c *check.C) {
  440. testRequires(c, NotaryHosting)
  441. repoName := fmt.Sprintf("%v/dockerclimanyroles/trusted", privateRegistryURL)
  442. targetName := fmt.Sprintf("%s:latest", repoName)
  443. s.notaryInitRepo(c, repoName)
  444. s.notaryCreateDelegation(c, repoName, "targets/role1", s.not.keys[0].Public)
  445. s.notaryCreateDelegation(c, repoName, "targets/role2", s.not.keys[1].Public)
  446. s.notaryCreateDelegation(c, repoName, "targets/role3", s.not.keys[2].Public)
  447. // import everything except the third key
  448. s.notaryImportKey(c, repoName, "targets/role1", s.not.keys[0].Private)
  449. s.notaryImportKey(c, repoName, "targets/role2", s.not.keys[1].Private)
  450. s.notaryCreateDelegation(c, repoName, "targets/role1/subrole", s.not.keys[3].Public)
  451. s.notaryImportKey(c, repoName, "targets/role1/subrole", s.not.keys[3].Private)
  452. s.notaryPublish(c, repoName)
  453. // tag the image and upload it to the private registry
  454. dockerCmd(c, "tag", "busybox", targetName)
  455. pushCmd := exec.Command(dockerBinary, "push", targetName)
  456. s.trustedCmd(pushCmd)
  457. out, _, err := runCommandWithOutput(pushCmd)
  458. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  459. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  460. // check to make sure that the target has been added to targets/role1 and targets/role2, and
  461. // not targets (because there are delegations) or targets/role3 (due to missing key) or
  462. // targets/role1/subrole (due to it being a second level delegation)
  463. s.assertTargetInRoles(c, repoName, "latest", "targets/role1", "targets/role2")
  464. s.assertTargetNotInRoles(c, repoName, "latest", "targets")
  465. // Try pull after push
  466. os.RemoveAll(filepath.Join(cliconfig.ConfigDir(), "trust"))
  467. // pull should fail because none of these are the releases role
  468. pullCmd := exec.Command(dockerBinary, "pull", targetName)
  469. s.trustedCmd(pullCmd)
  470. out, _, err = runCommandWithOutput(pullCmd)
  471. c.Assert(err, check.NotNil, check.Commentf(out))
  472. }
  473. func (s *DockerTrustSuite) TestTrustedPushSignsForRolesWithKeysAndValidPaths(c *check.C) {
  474. repoName := fmt.Sprintf("%v/dockerclirolesbykeysandpaths/trusted", privateRegistryURL)
  475. targetName := fmt.Sprintf("%s:latest", repoName)
  476. s.notaryInitRepo(c, repoName)
  477. s.notaryCreateDelegation(c, repoName, "targets/role1", s.not.keys[0].Public, "l", "z")
  478. s.notaryCreateDelegation(c, repoName, "targets/role2", s.not.keys[1].Public, "x", "y")
  479. s.notaryCreateDelegation(c, repoName, "targets/role3", s.not.keys[2].Public, "latest")
  480. s.notaryCreateDelegation(c, repoName, "targets/role4", s.not.keys[3].Public, "latest")
  481. // import everything except the third key
  482. s.notaryImportKey(c, repoName, "targets/role1", s.not.keys[0].Private)
  483. s.notaryImportKey(c, repoName, "targets/role2", s.not.keys[1].Private)
  484. s.notaryImportKey(c, repoName, "targets/role4", s.not.keys[3].Private)
  485. s.notaryPublish(c, repoName)
  486. // tag the image and upload it to the private registry
  487. dockerCmd(c, "tag", "busybox", targetName)
  488. pushCmd := exec.Command(dockerBinary, "push", targetName)
  489. s.trustedCmd(pushCmd)
  490. out, _, err := runCommandWithOutput(pushCmd)
  491. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  492. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  493. // check to make sure that the target has been added to targets/role1 and targets/role4, and
  494. // not targets (because there are delegations) or targets/role2 (due to path restrictions) or
  495. // targets/role3 (due to missing key)
  496. s.assertTargetInRoles(c, repoName, "latest", "targets/role1", "targets/role4")
  497. s.assertTargetNotInRoles(c, repoName, "latest", "targets")
  498. // Try pull after push
  499. os.RemoveAll(filepath.Join(cliconfig.ConfigDir(), "trust"))
  500. // pull should fail because none of these are the releases role
  501. pullCmd := exec.Command(dockerBinary, "pull", targetName)
  502. s.trustedCmd(pullCmd)
  503. out, _, err = runCommandWithOutput(pullCmd)
  504. c.Assert(err, check.NotNil, check.Commentf(out))
  505. }
  506. func (s *DockerTrustSuite) TestTrustedPushDoesntSignTargetsIfDelegationsExist(c *check.C) {
  507. testRequires(c, NotaryHosting)
  508. repoName := fmt.Sprintf("%v/dockerclireleasedelegationnotsignable/trusted", privateRegistryURL)
  509. targetName := fmt.Sprintf("%s:latest", repoName)
  510. s.notaryInitRepo(c, repoName)
  511. s.notaryCreateDelegation(c, repoName, "targets/role1", s.not.keys[0].Public)
  512. s.notaryPublish(c, repoName)
  513. // do not import any delegations key
  514. // tag the image and upload it to the private registry
  515. dockerCmd(c, "tag", "busybox", targetName)
  516. pushCmd := exec.Command(dockerBinary, "push", targetName)
  517. s.trustedCmd(pushCmd)
  518. out, _, err := runCommandWithOutput(pushCmd)
  519. c.Assert(err, check.NotNil, check.Commentf("trusted push succeeded but should have failed:\n%s", out))
  520. c.Assert(out, checker.Contains, "no valid signing keys",
  521. check.Commentf("Missing expected output on trusted push without keys"))
  522. s.assertTargetNotInRoles(c, repoName, "latest", "targets", "targets/role1")
  523. }
  524. func (s *DockerRegistryAuthHtpasswdSuite) TestPushNoCredentialsNoRetry(c *check.C) {
  525. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  526. dockerCmd(c, "tag", "busybox", repoName)
  527. out, _, err := dockerCmdWithError("push", repoName)
  528. c.Assert(err, check.NotNil, check.Commentf(out))
  529. c.Assert(out, check.Not(checker.Contains), "Retrying")
  530. c.Assert(out, checker.Contains, "no basic auth credentials")
  531. }
  532. // This may be flaky but it's needed not to regress on unauthorized push, see #21054
  533. func (s *DockerSuite) TestPushToCentralRegistryUnauthorized(c *check.C) {
  534. testRequires(c, Network)
  535. repoName := "test/busybox"
  536. dockerCmd(c, "tag", "busybox", repoName)
  537. out, _, err := dockerCmdWithError("push", repoName)
  538. c.Assert(err, check.NotNil, check.Commentf(out))
  539. c.Assert(out, check.Not(checker.Contains), "Retrying")
  540. }
  541. func getTestTokenService(status int, body string) *httptest.Server {
  542. return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  543. w.WriteHeader(status)
  544. w.Header().Set("Content-Type", "application/json")
  545. w.Write([]byte(body))
  546. }))
  547. }
  548. func (s *DockerRegistryAuthTokenSuite) TestPushTokenServiceUnauthResponse(c *check.C) {
  549. ts := getTestTokenService(http.StatusUnauthorized, `{"errors": [{"Code":"UNAUTHORIZED", "message": "a message", "detail": null}]}`)
  550. defer ts.Close()
  551. s.setupRegistryWithTokenService(c, ts.URL)
  552. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  553. dockerCmd(c, "tag", "busybox", repoName)
  554. out, _, err := dockerCmdWithError("push", repoName)
  555. c.Assert(err, check.NotNil, check.Commentf(out))
  556. c.Assert(out, checker.Not(checker.Contains), "Retrying")
  557. c.Assert(out, checker.Contains, "unauthorized: a message")
  558. }
  559. func (s *DockerRegistryAuthTokenSuite) TestPushMisconfiguredTokenServiceResponseUnauthorized(c *check.C) {
  560. ts := getTestTokenService(http.StatusUnauthorized, `{"error": "unauthorized"}`)
  561. defer ts.Close()
  562. s.setupRegistryWithTokenService(c, ts.URL)
  563. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  564. dockerCmd(c, "tag", "busybox", repoName)
  565. out, _, err := dockerCmdWithError("push", repoName)
  566. c.Assert(err, check.NotNil, check.Commentf(out))
  567. c.Assert(out, checker.Not(checker.Contains), "Retrying")
  568. split := strings.Split(out, "\n")
  569. c.Assert(split[len(split)-2], check.Equals, "unauthorized: authentication required")
  570. }
  571. func (s *DockerRegistryAuthTokenSuite) TestPushMisconfiguredTokenServiceResponseError(c *check.C) {
  572. ts := getTestTokenService(http.StatusInternalServerError, `{"error": "unexpected"}`)
  573. defer ts.Close()
  574. s.setupRegistryWithTokenService(c, ts.URL)
  575. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  576. dockerCmd(c, "tag", "busybox", repoName)
  577. out, _, err := dockerCmdWithError("push", repoName)
  578. c.Assert(err, check.NotNil, check.Commentf(out))
  579. c.Assert(out, checker.Contains, "Retrying")
  580. split := strings.Split(out, "\n")
  581. c.Assert(split[len(split)-2], check.Equals, "received unexpected HTTP status: 500 Internal Server Error")
  582. }
  583. func (s *DockerRegistryAuthTokenSuite) TestPushMisconfiguredTokenServiceResponseUnparsable(c *check.C) {
  584. ts := getTestTokenService(http.StatusForbidden, `no way`)
  585. defer ts.Close()
  586. s.setupRegistryWithTokenService(c, ts.URL)
  587. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  588. dockerCmd(c, "tag", "busybox", repoName)
  589. out, _, err := dockerCmdWithError("push", repoName)
  590. c.Assert(err, check.NotNil, check.Commentf(out))
  591. c.Assert(out, checker.Not(checker.Contains), "Retrying")
  592. split := strings.Split(out, "\n")
  593. c.Assert(split[len(split)-2], checker.Contains, "error parsing HTTP 403 response body: ")
  594. }
  595. func (s *DockerRegistryAuthTokenSuite) TestPushMisconfiguredTokenServiceResponseNoToken(c *check.C) {
  596. ts := getTestTokenService(http.StatusOK, `{"something": "wrong"}`)
  597. defer ts.Close()
  598. s.setupRegistryWithTokenService(c, ts.URL)
  599. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  600. dockerCmd(c, "tag", "busybox", repoName)
  601. out, _, err := dockerCmdWithError("push", repoName)
  602. c.Assert(err, check.NotNil, check.Commentf(out))
  603. c.Assert(out, checker.Not(checker.Contains), "Retrying")
  604. split := strings.Split(out, "\n")
  605. c.Assert(split[len(split)-2], check.Equals, "authorization server did not include a token in the response")
  606. }