docker_cli_push_test.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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. func (s *DockerTrustSuite) TestTrustedPushWithFailingServer(c *check.C) {
  261. repoName := fmt.Sprintf("%v/dockerclitrusted/failingserver: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.trustedCmdWithServer(pushCmd, "https://example.com:81/")
  266. out, _, err := runCommandWithOutput(pushCmd)
  267. c.Assert(err, check.NotNil, check.Commentf("Missing error while running trusted push w/ no server"))
  268. c.Assert(out, checker.Contains, "error contacting notary server", check.Commentf("Missing expected output on trusted push"))
  269. }
  270. func (s *DockerTrustSuite) TestTrustedPushWithoutServerAndUntrusted(c *check.C) {
  271. repoName := fmt.Sprintf("%v/dockerclitrusted/trustedandnot: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", "--disable-content-trust", repoName)
  275. s.trustedCmdWithServer(pushCmd, "https://example.com/")
  276. out, _, err := runCommandWithOutput(pushCmd)
  277. c.Assert(err, check.IsNil, check.Commentf("trusted push with no server and --disable-content-trust failed: %s\n%s", err, out))
  278. 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:"))
  279. }
  280. func (s *DockerTrustSuite) TestTrustedPushWithExistingTag(c *check.C) {
  281. repoName := fmt.Sprintf("%v/dockerclitag/trusted:latest", privateRegistryURL)
  282. // tag the image and upload it to the private registry
  283. dockerCmd(c, "tag", "busybox", repoName)
  284. dockerCmd(c, "push", repoName)
  285. pushCmd := exec.Command(dockerBinary, "push", repoName)
  286. s.trustedCmd(pushCmd)
  287. out, _, err := runCommandWithOutput(pushCmd)
  288. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  289. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  290. // Try pull after push
  291. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  292. s.trustedCmd(pullCmd)
  293. out, _, err = runCommandWithOutput(pullCmd)
  294. c.Assert(err, check.IsNil, check.Commentf(out))
  295. c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf(out))
  296. }
  297. func (s *DockerTrustSuite) TestTrustedPushWithExistingSignedTag(c *check.C) {
  298. repoName := fmt.Sprintf("%v/dockerclipushpush/trusted:latest", privateRegistryURL)
  299. // tag the image and upload it to the private registry
  300. dockerCmd(c, "tag", "busybox", repoName)
  301. // Do a trusted push
  302. pushCmd := exec.Command(dockerBinary, "push", repoName)
  303. s.trustedCmd(pushCmd)
  304. out, _, err := runCommandWithOutput(pushCmd)
  305. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  306. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  307. // Do another trusted push
  308. pushCmd = exec.Command(dockerBinary, "push", repoName)
  309. s.trustedCmd(pushCmd)
  310. out, _, err = runCommandWithOutput(pushCmd)
  311. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  312. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  313. dockerCmd(c, "rmi", repoName)
  314. // Try pull to ensure the double push did not break our ability to pull
  315. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  316. s.trustedCmd(pullCmd)
  317. out, _, err = runCommandWithOutput(pullCmd)
  318. c.Assert(err, check.IsNil, check.Commentf("Error running trusted pull: %s\n%s", err, out))
  319. c.Assert(out, checker.Contains, "Status: Downloaded", check.Commentf("Missing expected output on trusted pull with --disable-content-trust"))
  320. }
  321. func (s *DockerTrustSuite) TestTrustedPushWithIncorrectPassphraseForNonRoot(c *check.C) {
  322. repoName := fmt.Sprintf("%v/dockercliincorretpwd/trusted:latest", privateRegistryURL)
  323. // tag the image and upload it to the private registry
  324. dockerCmd(c, "tag", "busybox", repoName)
  325. // Push with default passphrases
  326. pushCmd := exec.Command(dockerBinary, "push", repoName)
  327. s.trustedCmd(pushCmd)
  328. out, _, err := runCommandWithOutput(pushCmd)
  329. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  330. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push:\n%s", out))
  331. // Push with wrong passphrases
  332. pushCmd = exec.Command(dockerBinary, "push", repoName)
  333. s.trustedCmdWithPassphrases(pushCmd, "12345678", "87654321")
  334. out, _, err = runCommandWithOutput(pushCmd)
  335. c.Assert(err, check.NotNil, check.Commentf("Error missing from trusted push with short targets passphrase: \n%s", out))
  336. c.Assert(out, checker.Contains, "could not find necessary signing keys", check.Commentf("Missing expected output on trusted push with short targets/snapsnot passphrase"))
  337. }
  338. func (s *DockerTrustSuite) TestTrustedPushWithExpiredSnapshot(c *check.C) {
  339. c.Skip("Currently changes system time, causing instability")
  340. repoName := fmt.Sprintf("%v/dockercliexpiredsnapshot/trusted:latest", privateRegistryURL)
  341. // tag the image and upload it to the private registry
  342. dockerCmd(c, "tag", "busybox", repoName)
  343. // Push with default passphrases
  344. pushCmd := exec.Command(dockerBinary, "push", repoName)
  345. s.trustedCmd(pushCmd)
  346. out, _, err := runCommandWithOutput(pushCmd)
  347. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  348. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  349. // Snapshots last for three years. This should be expired
  350. fourYearsLater := time.Now().Add(time.Hour * 24 * 365 * 4)
  351. runAtDifferentDate(fourYearsLater, func() {
  352. // Push with wrong passphrases
  353. pushCmd = exec.Command(dockerBinary, "push", repoName)
  354. s.trustedCmd(pushCmd)
  355. out, _, err = runCommandWithOutput(pushCmd)
  356. c.Assert(err, check.NotNil, check.Commentf("Error missing from trusted push with expired snapshot: \n%s", out))
  357. c.Assert(out, checker.Contains, "repository out-of-date", check.Commentf("Missing expected output on trusted push with expired snapshot"))
  358. })
  359. }
  360. func (s *DockerTrustSuite) TestTrustedPushWithExpiredTimestamp(c *check.C) {
  361. c.Skip("Currently changes system time, causing instability")
  362. repoName := fmt.Sprintf("%v/dockercliexpiredtimestamppush/trusted:latest", privateRegistryURL)
  363. // tag the image and upload it to the private registry
  364. dockerCmd(c, "tag", "busybox", repoName)
  365. // Push with default passphrases
  366. pushCmd := exec.Command(dockerBinary, "push", repoName)
  367. s.trustedCmd(pushCmd)
  368. out, _, err := runCommandWithOutput(pushCmd)
  369. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  370. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  371. // The timestamps expire in two weeks. Lets check three
  372. threeWeeksLater := time.Now().Add(time.Hour * 24 * 21)
  373. // Should succeed because the server transparently re-signs one
  374. runAtDifferentDate(threeWeeksLater, func() {
  375. pushCmd := exec.Command(dockerBinary, "push", repoName)
  376. s.trustedCmd(pushCmd)
  377. out, _, err := runCommandWithOutput(pushCmd)
  378. c.Assert(err, check.IsNil, check.Commentf("Error running trusted push: %s\n%s", err, out))
  379. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with expired timestamp"))
  380. })
  381. }
  382. func (s *DockerTrustSuite) TestTrustedPushWithReleasesDelegationOnly(c *check.C) {
  383. testRequires(c, NotaryHosting)
  384. repoName := fmt.Sprintf("%v/dockerclireleasedelegationinitfirst/trusted", privateRegistryURL)
  385. targetName := fmt.Sprintf("%s:latest", repoName)
  386. s.notaryInitRepo(c, repoName)
  387. s.notaryCreateDelegation(c, repoName, "targets/releases", s.not.keys[0].Public)
  388. s.notaryPublish(c, repoName)
  389. s.notaryImportKey(c, repoName, "targets/releases", s.not.keys[0].Private)
  390. // tag the image and upload it to the private registry
  391. dockerCmd(c, "tag", "busybox", targetName)
  392. pushCmd := exec.Command(dockerBinary, "push", targetName)
  393. s.trustedCmd(pushCmd)
  394. out, _, err := runCommandWithOutput(pushCmd)
  395. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  396. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  397. // check to make sure that the target has been added to targets/releases and not targets
  398. s.assertTargetInRoles(c, repoName, "latest", "targets/releases")
  399. s.assertTargetNotInRoles(c, repoName, "latest", "targets")
  400. // Try pull after push
  401. os.RemoveAll(filepath.Join(cliconfig.ConfigDir(), "trust"))
  402. pullCmd := exec.Command(dockerBinary, "pull", targetName)
  403. s.trustedCmd(pullCmd)
  404. out, _, err = runCommandWithOutput(pullCmd)
  405. c.Assert(err, check.IsNil, check.Commentf(out))
  406. c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf(out))
  407. }
  408. func (s *DockerTrustSuite) TestTrustedPushSignsAllFirstLevelRolesWeHaveKeysFor(c *check.C) {
  409. testRequires(c, NotaryHosting)
  410. repoName := fmt.Sprintf("%v/dockerclimanyroles/trusted", privateRegistryURL)
  411. targetName := fmt.Sprintf("%s:latest", repoName)
  412. s.notaryInitRepo(c, repoName)
  413. s.notaryCreateDelegation(c, repoName, "targets/role1", s.not.keys[0].Public)
  414. s.notaryCreateDelegation(c, repoName, "targets/role2", s.not.keys[1].Public)
  415. s.notaryCreateDelegation(c, repoName, "targets/role3", s.not.keys[2].Public)
  416. // import everything except the third key
  417. s.notaryImportKey(c, repoName, "targets/role1", s.not.keys[0].Private)
  418. s.notaryImportKey(c, repoName, "targets/role2", s.not.keys[1].Private)
  419. s.notaryCreateDelegation(c, repoName, "targets/role1/subrole", s.not.keys[3].Public)
  420. s.notaryImportKey(c, repoName, "targets/role1/subrole", s.not.keys[3].Private)
  421. s.notaryPublish(c, repoName)
  422. // tag the image and upload it to the private registry
  423. dockerCmd(c, "tag", "busybox", targetName)
  424. pushCmd := exec.Command(dockerBinary, "push", targetName)
  425. s.trustedCmd(pushCmd)
  426. out, _, err := runCommandWithOutput(pushCmd)
  427. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  428. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  429. // check to make sure that the target has been added to targets/role1 and targets/role2, and
  430. // not targets (because there are delegations) or targets/role3 (due to missing key) or
  431. // targets/role1/subrole (due to it being a second level delegation)
  432. s.assertTargetInRoles(c, repoName, "latest", "targets/role1", "targets/role2")
  433. s.assertTargetNotInRoles(c, repoName, "latest", "targets")
  434. // Try pull after push
  435. os.RemoveAll(filepath.Join(cliconfig.ConfigDir(), "trust"))
  436. // pull should fail because none of these are the releases role
  437. pullCmd := exec.Command(dockerBinary, "pull", targetName)
  438. s.trustedCmd(pullCmd)
  439. out, _, err = runCommandWithOutput(pullCmd)
  440. c.Assert(err, check.NotNil, check.Commentf(out))
  441. }
  442. func (s *DockerTrustSuite) TestTrustedPushSignsForRolesWithKeysAndValidPaths(c *check.C) {
  443. repoName := fmt.Sprintf("%v/dockerclirolesbykeysandpaths/trusted", privateRegistryURL)
  444. targetName := fmt.Sprintf("%s:latest", repoName)
  445. s.notaryInitRepo(c, repoName)
  446. s.notaryCreateDelegation(c, repoName, "targets/role1", s.not.keys[0].Public, "l", "z")
  447. s.notaryCreateDelegation(c, repoName, "targets/role2", s.not.keys[1].Public, "x", "y")
  448. s.notaryCreateDelegation(c, repoName, "targets/role3", s.not.keys[2].Public, "latest")
  449. s.notaryCreateDelegation(c, repoName, "targets/role4", s.not.keys[3].Public, "latest")
  450. // import everything except the third key
  451. s.notaryImportKey(c, repoName, "targets/role1", s.not.keys[0].Private)
  452. s.notaryImportKey(c, repoName, "targets/role2", s.not.keys[1].Private)
  453. s.notaryImportKey(c, repoName, "targets/role4", s.not.keys[3].Private)
  454. s.notaryPublish(c, repoName)
  455. // tag the image and upload it to the private registry
  456. dockerCmd(c, "tag", "busybox", targetName)
  457. pushCmd := exec.Command(dockerBinary, "push", targetName)
  458. s.trustedCmd(pushCmd)
  459. out, _, err := runCommandWithOutput(pushCmd)
  460. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  461. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  462. // check to make sure that the target has been added to targets/role1 and targets/role4, and
  463. // not targets (because there are delegations) or targets/role2 (due to path restrictions) or
  464. // targets/role3 (due to missing key)
  465. s.assertTargetInRoles(c, repoName, "latest", "targets/role1", "targets/role4")
  466. s.assertTargetNotInRoles(c, repoName, "latest", "targets")
  467. // Try pull after push
  468. os.RemoveAll(filepath.Join(cliconfig.ConfigDir(), "trust"))
  469. // pull should fail because none of these are the releases role
  470. pullCmd := exec.Command(dockerBinary, "pull", targetName)
  471. s.trustedCmd(pullCmd)
  472. out, _, err = runCommandWithOutput(pullCmd)
  473. c.Assert(err, check.NotNil, check.Commentf(out))
  474. }
  475. func (s *DockerTrustSuite) TestTrustedPushDoesntSignTargetsIfDelegationsExist(c *check.C) {
  476. testRequires(c, NotaryHosting)
  477. repoName := fmt.Sprintf("%v/dockerclireleasedelegationnotsignable/trusted", privateRegistryURL)
  478. targetName := fmt.Sprintf("%s:latest", repoName)
  479. s.notaryInitRepo(c, repoName)
  480. s.notaryCreateDelegation(c, repoName, "targets/role1", s.not.keys[0].Public)
  481. s.notaryPublish(c, repoName)
  482. // do not import any delegations key
  483. // tag the image and upload it to the private registry
  484. dockerCmd(c, "tag", "busybox", targetName)
  485. pushCmd := exec.Command(dockerBinary, "push", targetName)
  486. s.trustedCmd(pushCmd)
  487. out, _, err := runCommandWithOutput(pushCmd)
  488. c.Assert(err, check.NotNil, check.Commentf("trusted push succeeded but should have failed:\n%s", out))
  489. c.Assert(out, checker.Contains, "no valid signing keys",
  490. check.Commentf("Missing expected output on trusted push without keys"))
  491. s.assertTargetNotInRoles(c, repoName, "latest", "targets", "targets/role1")
  492. }
  493. func (s *DockerRegistryAuthHtpasswdSuite) TestPushNoCredentialsNoRetry(c *check.C) {
  494. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  495. dockerCmd(c, "tag", "busybox", repoName)
  496. out, _, err := dockerCmdWithError("push", repoName)
  497. c.Assert(err, check.NotNil, check.Commentf(out))
  498. c.Assert(out, check.Not(checker.Contains), "Retrying")
  499. c.Assert(out, checker.Contains, "no basic auth credentials")
  500. }
  501. // This may be flaky but it's needed not to regress on unauthorized push, see #21054
  502. func (s *DockerSuite) TestPushToCentralRegistryUnauthorized(c *check.C) {
  503. testRequires(c, Network)
  504. repoName := "test/busybox"
  505. dockerCmd(c, "tag", "busybox", repoName)
  506. out, _, err := dockerCmdWithError("push", repoName)
  507. c.Assert(err, check.NotNil, check.Commentf(out))
  508. c.Assert(out, check.Not(checker.Contains), "Retrying")
  509. }
  510. func getTestTokenService(status int, body string) *httptest.Server {
  511. return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  512. w.WriteHeader(status)
  513. w.Header().Set("Content-Type", "application/json")
  514. w.Write([]byte(body))
  515. }))
  516. }
  517. func (s *DockerRegistryAuthTokenSuite) TestPushTokenServiceUnauthResponse(c *check.C) {
  518. ts := getTestTokenService(http.StatusUnauthorized, `{"errors": [{"Code":"UNAUTHORIZED", "message": "a message", "detail": null}]}`)
  519. defer ts.Close()
  520. s.setupRegistryWithTokenService(c, ts.URL)
  521. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  522. dockerCmd(c, "tag", "busybox", repoName)
  523. out, _, err := dockerCmdWithError("push", repoName)
  524. c.Assert(err, check.NotNil, check.Commentf(out))
  525. c.Assert(out, checker.Not(checker.Contains), "Retrying")
  526. c.Assert(out, checker.Contains, "unauthorized: a message")
  527. }
  528. func (s *DockerRegistryAuthTokenSuite) TestPushMisconfiguredTokenServiceResponseUnauthorized(c *check.C) {
  529. ts := getTestTokenService(http.StatusUnauthorized, `{"error": "unauthorized"}`)
  530. defer ts.Close()
  531. s.setupRegistryWithTokenService(c, ts.URL)
  532. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  533. dockerCmd(c, "tag", "busybox", repoName)
  534. out, _, err := dockerCmdWithError("push", repoName)
  535. c.Assert(err, check.NotNil, check.Commentf(out))
  536. c.Assert(out, checker.Not(checker.Contains), "Retrying")
  537. split := strings.Split(out, "\n")
  538. c.Assert(split[len(split)-2], check.Equals, "unauthorized: authentication required")
  539. }
  540. func (s *DockerRegistryAuthTokenSuite) TestPushMisconfiguredTokenServiceResponseError(c *check.C) {
  541. ts := getTestTokenService(http.StatusInternalServerError, `{"error": "unexpected"}`)
  542. defer ts.Close()
  543. s.setupRegistryWithTokenService(c, ts.URL)
  544. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  545. dockerCmd(c, "tag", "busybox", repoName)
  546. out, _, err := dockerCmdWithError("push", repoName)
  547. c.Assert(err, check.NotNil, check.Commentf(out))
  548. c.Assert(out, checker.Contains, "Retrying")
  549. split := strings.Split(out, "\n")
  550. c.Assert(split[len(split)-2], check.Equals, "received unexpected HTTP status: 500 Internal Server Error")
  551. }
  552. func (s *DockerRegistryAuthTokenSuite) TestPushMisconfiguredTokenServiceResponseUnparsable(c *check.C) {
  553. ts := getTestTokenService(http.StatusForbidden, `no way`)
  554. defer ts.Close()
  555. s.setupRegistryWithTokenService(c, ts.URL)
  556. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  557. dockerCmd(c, "tag", "busybox", repoName)
  558. out, _, err := dockerCmdWithError("push", repoName)
  559. c.Assert(err, check.NotNil, check.Commentf(out))
  560. c.Assert(out, checker.Not(checker.Contains), "Retrying")
  561. split := strings.Split(out, "\n")
  562. c.Assert(split[len(split)-2], checker.Contains, "error parsing HTTP 403 response body: ")
  563. }
  564. func (s *DockerRegistryAuthTokenSuite) TestPushMisconfiguredTokenServiceResponseNoToken(c *check.C) {
  565. ts := getTestTokenService(http.StatusOK, `{"something": "wrong"}`)
  566. defer ts.Close()
  567. s.setupRegistryWithTokenService(c, ts.URL)
  568. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  569. dockerCmd(c, "tag", "busybox", repoName)
  570. out, _, err := dockerCmdWithError("push", repoName)
  571. c.Assert(err, check.NotNil, check.Commentf(out))
  572. c.Assert(out, checker.Not(checker.Contains), "Retrying")
  573. split := strings.Split(out, "\n")
  574. c.Assert(split[len(split)-2], check.Equals, "authorization server did not include a token in the response")
  575. }