docker_cli_push_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. package main
  2. import (
  3. "archive/tar"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "net/http/httptest"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. "sync"
  12. "github.com/docker/distribution/reference"
  13. "github.com/docker/docker/cli/config"
  14. "github.com/docker/docker/integration-cli/checker"
  15. "github.com/docker/docker/integration-cli/cli"
  16. "github.com/docker/docker/integration-cli/cli/build"
  17. "github.com/go-check/check"
  18. "github.com/gotestyourself/gotestyourself/icmd"
  19. )
  20. // Pushing an image to a private registry.
  21. func testPushBusyboxImage(c *check.C) {
  22. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  23. // tag the image to upload it to the private registry
  24. dockerCmd(c, "tag", "busybox", repoName)
  25. // push the image to the registry
  26. dockerCmd(c, "push", repoName)
  27. }
  28. func (s *DockerRegistrySuite) TestPushBusyboxImage(c *check.C) {
  29. testPushBusyboxImage(c)
  30. }
  31. func (s *DockerSchema1RegistrySuite) TestPushBusyboxImage(c *check.C) {
  32. testPushBusyboxImage(c)
  33. }
  34. // pushing an image without a prefix should throw an error
  35. func (s *DockerSuite) TestPushUnprefixedRepo(c *check.C) {
  36. out, _, err := dockerCmdWithError("push", "busybox")
  37. c.Assert(err, check.NotNil, check.Commentf("pushing an unprefixed repo didn't result in a non-zero exit status: %s", out))
  38. }
  39. func testPushUntagged(c *check.C) {
  40. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  41. expected := "An image does not exist locally with the tag"
  42. out, _, err := dockerCmdWithError("push", repoName)
  43. c.Assert(err, check.NotNil, check.Commentf("pushing the image to the private registry should have failed: output %q", out))
  44. c.Assert(out, checker.Contains, expected, check.Commentf("pushing the image failed"))
  45. }
  46. func (s *DockerRegistrySuite) TestPushUntagged(c *check.C) {
  47. testPushUntagged(c)
  48. }
  49. func (s *DockerSchema1RegistrySuite) TestPushUntagged(c *check.C) {
  50. testPushUntagged(c)
  51. }
  52. func testPushBadTag(c *check.C) {
  53. repoName := fmt.Sprintf("%v/dockercli/busybox:latest", privateRegistryURL)
  54. expected := "does not exist"
  55. out, _, err := dockerCmdWithError("push", repoName)
  56. c.Assert(err, check.NotNil, check.Commentf("pushing the image to the private registry should have failed: output %q", out))
  57. c.Assert(out, checker.Contains, expected, check.Commentf("pushing the image failed"))
  58. }
  59. func (s *DockerRegistrySuite) TestPushBadTag(c *check.C) {
  60. testPushBadTag(c)
  61. }
  62. func (s *DockerSchema1RegistrySuite) TestPushBadTag(c *check.C) {
  63. testPushBadTag(c)
  64. }
  65. func testPushMultipleTags(c *check.C) {
  66. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  67. repoTag1 := fmt.Sprintf("%v/dockercli/busybox:t1", privateRegistryURL)
  68. repoTag2 := fmt.Sprintf("%v/dockercli/busybox:t2", privateRegistryURL)
  69. // tag the image and upload it to the private registry
  70. dockerCmd(c, "tag", "busybox", repoTag1)
  71. dockerCmd(c, "tag", "busybox", repoTag2)
  72. dockerCmd(c, "push", repoName)
  73. // Ensure layer list is equivalent for repoTag1 and repoTag2
  74. out1, _ := dockerCmd(c, "pull", repoTag1)
  75. imageAlreadyExists := ": Image already exists"
  76. var out1Lines []string
  77. for _, outputLine := range strings.Split(out1, "\n") {
  78. if strings.Contains(outputLine, imageAlreadyExists) {
  79. out1Lines = append(out1Lines, outputLine)
  80. }
  81. }
  82. out2, _ := dockerCmd(c, "pull", repoTag2)
  83. var out2Lines []string
  84. for _, outputLine := range strings.Split(out2, "\n") {
  85. if strings.Contains(outputLine, imageAlreadyExists) {
  86. out1Lines = append(out1Lines, outputLine)
  87. }
  88. }
  89. c.Assert(out2Lines, checker.HasLen, len(out1Lines))
  90. for i := range out1Lines {
  91. c.Assert(out1Lines[i], checker.Equals, out2Lines[i])
  92. }
  93. }
  94. func (s *DockerRegistrySuite) TestPushMultipleTags(c *check.C) {
  95. testPushMultipleTags(c)
  96. }
  97. func (s *DockerSchema1RegistrySuite) TestPushMultipleTags(c *check.C) {
  98. testPushMultipleTags(c)
  99. }
  100. func testPushEmptyLayer(c *check.C) {
  101. repoName := fmt.Sprintf("%v/dockercli/emptylayer", privateRegistryURL)
  102. emptyTarball, err := ioutil.TempFile("", "empty_tarball")
  103. c.Assert(err, check.IsNil, check.Commentf("Unable to create test file"))
  104. tw := tar.NewWriter(emptyTarball)
  105. err = tw.Close()
  106. c.Assert(err, check.IsNil, check.Commentf("Error creating empty tarball"))
  107. freader, err := os.Open(emptyTarball.Name())
  108. c.Assert(err, check.IsNil, check.Commentf("Could not open test tarball"))
  109. defer freader.Close()
  110. icmd.RunCmd(icmd.Cmd{
  111. Command: []string{dockerBinary, "import", "-", repoName},
  112. Stdin: freader,
  113. }).Assert(c, icmd.Success)
  114. // Now verify we can push it
  115. out, _, err := dockerCmdWithError("push", repoName)
  116. c.Assert(err, check.IsNil, check.Commentf("pushing the image to the private registry has failed: %s", out))
  117. }
  118. func (s *DockerRegistrySuite) TestPushEmptyLayer(c *check.C) {
  119. testPushEmptyLayer(c)
  120. }
  121. func (s *DockerSchema1RegistrySuite) TestPushEmptyLayer(c *check.C) {
  122. testPushEmptyLayer(c)
  123. }
  124. // testConcurrentPush pushes multiple tags to the same repo
  125. // concurrently.
  126. func testConcurrentPush(c *check.C) {
  127. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  128. repos := []string{}
  129. for _, tag := range []string{"push1", "push2", "push3"} {
  130. repo := fmt.Sprintf("%v:%v", repoName, tag)
  131. buildImageSuccessfully(c, repo, build.WithDockerfile(fmt.Sprintf(`
  132. FROM busybox
  133. ENTRYPOINT ["/bin/echo"]
  134. ENV FOO foo
  135. ENV BAR bar
  136. CMD echo %s
  137. `, repo)))
  138. repos = append(repos, repo)
  139. }
  140. // Push tags, in parallel
  141. results := make(chan error)
  142. for _, repo := range repos {
  143. go func(repo string) {
  144. result := icmd.RunCommand(dockerBinary, "push", repo)
  145. results <- result.Error
  146. }(repo)
  147. }
  148. for range repos {
  149. err := <-results
  150. c.Assert(err, checker.IsNil, check.Commentf("concurrent push failed with error: %v", err))
  151. }
  152. // Clear local images store.
  153. args := append([]string{"rmi"}, repos...)
  154. dockerCmd(c, args...)
  155. // Re-pull and run individual tags, to make sure pushes succeeded
  156. for _, repo := range repos {
  157. dockerCmd(c, "pull", repo)
  158. dockerCmd(c, "inspect", repo)
  159. out, _ := dockerCmd(c, "run", "--rm", repo)
  160. c.Assert(strings.TrimSpace(out), checker.Equals, "/bin/sh -c echo "+repo)
  161. }
  162. }
  163. func (s *DockerRegistrySuite) TestConcurrentPush(c *check.C) {
  164. testConcurrentPush(c)
  165. }
  166. func (s *DockerSchema1RegistrySuite) TestConcurrentPush(c *check.C) {
  167. testConcurrentPush(c)
  168. }
  169. func (s *DockerRegistrySuite) TestCrossRepositoryLayerPush(c *check.C) {
  170. sourceRepoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  171. // tag the image to upload it to the private registry
  172. dockerCmd(c, "tag", "busybox", sourceRepoName)
  173. // push the image to the registry
  174. out1, _, err := dockerCmdWithError("push", sourceRepoName)
  175. c.Assert(err, check.IsNil, check.Commentf("pushing the image to the private registry has failed: %s", out1))
  176. // ensure that none of the layers were mounted from another repository during push
  177. c.Assert(strings.Contains(out1, "Mounted from"), check.Equals, false)
  178. digest1 := reference.DigestRegexp.FindString(out1)
  179. c.Assert(len(digest1), checker.GreaterThan, 0, check.Commentf("no digest found for pushed manifest"))
  180. destRepoName := fmt.Sprintf("%v/dockercli/crossrepopush", privateRegistryURL)
  181. // retag the image to upload the same layers to another repo in the same registry
  182. dockerCmd(c, "tag", "busybox", destRepoName)
  183. // push the image to the registry
  184. out2, _, err := dockerCmdWithError("push", destRepoName)
  185. c.Assert(err, check.IsNil, check.Commentf("pushing the image to the private registry has failed: %s", out2))
  186. // ensure that layers were mounted from the first repo during push
  187. c.Assert(strings.Contains(out2, "Mounted from dockercli/busybox"), check.Equals, true)
  188. digest2 := reference.DigestRegexp.FindString(out2)
  189. c.Assert(len(digest2), checker.GreaterThan, 0, check.Commentf("no digest found for pushed manifest"))
  190. c.Assert(digest1, check.Equals, digest2)
  191. // ensure that pushing again produces the same digest
  192. out3, _, err := dockerCmdWithError("push", destRepoName)
  193. c.Assert(err, check.IsNil, check.Commentf("pushing the image to the private registry has failed: %s", out2))
  194. digest3 := reference.DigestRegexp.FindString(out3)
  195. c.Assert(len(digest2), checker.GreaterThan, 0, check.Commentf("no digest found for pushed manifest"))
  196. c.Assert(digest3, check.Equals, digest2)
  197. // ensure that we can pull and run the cross-repo-pushed repository
  198. dockerCmd(c, "rmi", destRepoName)
  199. dockerCmd(c, "pull", destRepoName)
  200. out4, _ := dockerCmd(c, "run", destRepoName, "echo", "-n", "hello world")
  201. c.Assert(out4, check.Equals, "hello world")
  202. }
  203. func (s *DockerSchema1RegistrySuite) TestCrossRepositoryLayerPushNotSupported(c *check.C) {
  204. sourceRepoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  205. // tag the image to upload it to the private registry
  206. dockerCmd(c, "tag", "busybox", sourceRepoName)
  207. // push the image to the registry
  208. out1, _, err := dockerCmdWithError("push", sourceRepoName)
  209. c.Assert(err, check.IsNil, check.Commentf("pushing the image to the private registry has failed: %s", out1))
  210. // ensure that none of the layers were mounted from another repository during push
  211. c.Assert(strings.Contains(out1, "Mounted from"), check.Equals, false)
  212. digest1 := reference.DigestRegexp.FindString(out1)
  213. c.Assert(len(digest1), checker.GreaterThan, 0, check.Commentf("no digest found for pushed manifest"))
  214. destRepoName := fmt.Sprintf("%v/dockercli/crossrepopush", privateRegistryURL)
  215. // retag the image to upload the same layers to another repo in the same registry
  216. dockerCmd(c, "tag", "busybox", destRepoName)
  217. // push the image to the registry
  218. out2, _, err := dockerCmdWithError("push", destRepoName)
  219. c.Assert(err, check.IsNil, check.Commentf("pushing the image to the private registry has failed: %s", out2))
  220. // schema1 registry should not support cross-repo layer mounts, so ensure that this does not happen
  221. c.Assert(strings.Contains(out2, "Mounted from"), check.Equals, false)
  222. digest2 := reference.DigestRegexp.FindString(out2)
  223. c.Assert(len(digest2), checker.GreaterThan, 0, check.Commentf("no digest found for pushed manifest"))
  224. c.Assert(digest1, check.Not(check.Equals), digest2)
  225. // ensure that we can pull and run the second pushed repository
  226. dockerCmd(c, "rmi", destRepoName)
  227. dockerCmd(c, "pull", destRepoName)
  228. out3, _ := dockerCmd(c, "run", destRepoName, "echo", "-n", "hello world")
  229. c.Assert(out3, check.Equals, "hello world")
  230. }
  231. func (s *DockerTrustSuite) TestTrustedPush(c *check.C) {
  232. repoName := fmt.Sprintf("%v/dockerclitrusted/pushtest:latest", privateRegistryURL)
  233. // tag the image and upload it to the private registry
  234. cli.DockerCmd(c, "tag", "busybox", repoName)
  235. cli.Docker(cli.Args("push", repoName), trustedCmd).Assert(c, SuccessSigningAndPushing)
  236. // Try pull after push
  237. cli.Docker(cli.Args("pull", repoName), trustedCmd).Assert(c, icmd.Expected{
  238. Out: "Status: Image is up to date",
  239. })
  240. // Assert that we rotated the snapshot key to the server by checking our local keystore
  241. contents, err := ioutil.ReadDir(filepath.Join(config.Dir(), "trust/private/tuf_keys", privateRegistryURL, "dockerclitrusted/pushtest"))
  242. c.Assert(err, check.IsNil, check.Commentf("Unable to read local tuf key files"))
  243. // Check that we only have 1 key (targets key)
  244. c.Assert(contents, checker.HasLen, 1)
  245. }
  246. func (s *DockerTrustSuite) TestTrustedPushWithEnvPasswords(c *check.C) {
  247. repoName := fmt.Sprintf("%v/dockerclienv/trusted:latest", privateRegistryURL)
  248. // tag the image and upload it to the private registry
  249. cli.DockerCmd(c, "tag", "busybox", repoName)
  250. cli.Docker(cli.Args("push", repoName), trustedCmdWithPassphrases("12345678", "12345678")).Assert(c, SuccessSigningAndPushing)
  251. // Try pull after push
  252. cli.Docker(cli.Args("pull", repoName), trustedCmd).Assert(c, icmd.Expected{
  253. Out: "Status: Image is up to date",
  254. })
  255. }
  256. func (s *DockerTrustSuite) TestTrustedPushWithFailingServer(c *check.C) {
  257. repoName := fmt.Sprintf("%v/dockerclitrusted/failingserver:latest", privateRegistryURL)
  258. // tag the image and upload it to the private registry
  259. cli.DockerCmd(c, "tag", "busybox", repoName)
  260. // Using a name that doesn't resolve to an address makes this test faster
  261. cli.Docker(cli.Args("push", repoName), trustedCmdWithServer("https://server.invalid:81/")).Assert(c, icmd.Expected{
  262. ExitCode: 1,
  263. Err: "error contacting notary server",
  264. })
  265. }
  266. func (s *DockerTrustSuite) TestTrustedPushWithoutServerAndUntrusted(c *check.C) {
  267. repoName := fmt.Sprintf("%v/dockerclitrusted/trustedandnot:latest", privateRegistryURL)
  268. // tag the image and upload it to the private registry
  269. cli.DockerCmd(c, "tag", "busybox", repoName)
  270. result := cli.Docker(cli.Args("push", "--disable-content-trust", repoName), trustedCmdWithServer("https://server.invalid:81/"))
  271. result.Assert(c, icmd.Success)
  272. c.Assert(result.Combined(), check.Not(checker.Contains), "Error establishing connection to notary repository", check.Commentf("Missing expected output on trusted push with --disable-content-trust:"))
  273. }
  274. func (s *DockerTrustSuite) TestTrustedPushWithExistingTag(c *check.C) {
  275. repoName := fmt.Sprintf("%v/dockerclitag/trusted:latest", privateRegistryURL)
  276. // tag the image and upload it to the private registry
  277. cli.DockerCmd(c, "tag", "busybox", repoName)
  278. cli.DockerCmd(c, "push", repoName)
  279. cli.Docker(cli.Args("push", repoName), trustedCmd).Assert(c, SuccessSigningAndPushing)
  280. // Try pull after push
  281. cli.Docker(cli.Args("pull", repoName), trustedCmd).Assert(c, icmd.Expected{
  282. Out: "Status: Image is up to date",
  283. })
  284. }
  285. func (s *DockerTrustSuite) TestTrustedPushWithExistingSignedTag(c *check.C) {
  286. repoName := fmt.Sprintf("%v/dockerclipushpush/trusted:latest", privateRegistryURL)
  287. // tag the image and upload it to the private registry
  288. cli.DockerCmd(c, "tag", "busybox", repoName)
  289. // Do a trusted push
  290. cli.Docker(cli.Args("push", repoName), trustedCmd).Assert(c, SuccessSigningAndPushing)
  291. // Do another trusted push
  292. cli.Docker(cli.Args("push", repoName), trustedCmd).Assert(c, SuccessSigningAndPushing)
  293. cli.DockerCmd(c, "rmi", repoName)
  294. // Try pull to ensure the double push did not break our ability to pull
  295. cli.Docker(cli.Args("pull", repoName), trustedCmd).Assert(c, SuccessDownloaded)
  296. }
  297. func (s *DockerTrustSuite) TestTrustedPushWithIncorrectPassphraseForNonRoot(c *check.C) {
  298. repoName := fmt.Sprintf("%v/dockercliincorretpwd/trusted:latest", privateRegistryURL)
  299. // tag the image and upload it to the private registry
  300. cli.DockerCmd(c, "tag", "busybox", repoName)
  301. // Push with default passphrases
  302. cli.Docker(cli.Args("push", repoName), trustedCmd).Assert(c, SuccessSigningAndPushing)
  303. // Push with wrong passphrases
  304. cli.Docker(cli.Args("push", repoName), trustedCmdWithPassphrases("12345678", "87654321")).Assert(c, icmd.Expected{
  305. ExitCode: 1,
  306. Err: "could not find necessary signing keys",
  307. })
  308. }
  309. func (s *DockerTrustSuite) TestTrustedPushWithReleasesDelegationOnly(c *check.C) {
  310. testRequires(c, NotaryHosting)
  311. repoName := fmt.Sprintf("%v/dockerclireleasedelegationinitfirst/trusted", privateRegistryURL)
  312. targetName := fmt.Sprintf("%s:latest", repoName)
  313. s.notaryInitRepo(c, repoName)
  314. s.notaryCreateDelegation(c, repoName, "targets/releases", s.not.keys[0].Public)
  315. s.notaryPublish(c, repoName)
  316. s.notaryImportKey(c, repoName, "targets/releases", s.not.keys[0].Private)
  317. // tag the image and upload it to the private registry
  318. cli.DockerCmd(c, "tag", "busybox", targetName)
  319. cli.Docker(cli.Args("push", targetName), trustedCmd).Assert(c, SuccessSigningAndPushing)
  320. // check to make sure that the target has been added to targets/releases and not targets
  321. s.assertTargetInRoles(c, repoName, "latest", "targets/releases")
  322. s.assertTargetNotInRoles(c, repoName, "latest", "targets")
  323. // Try pull after push
  324. os.RemoveAll(filepath.Join(config.Dir(), "trust"))
  325. cli.Docker(cli.Args("pull", targetName), trustedCmd).Assert(c, icmd.Expected{
  326. Out: "Status: Image is up to date",
  327. })
  328. }
  329. func (s *DockerTrustSuite) TestTrustedPushSignsAllFirstLevelRolesWeHaveKeysFor(c *check.C) {
  330. testRequires(c, NotaryHosting)
  331. repoName := fmt.Sprintf("%v/dockerclimanyroles/trusted", privateRegistryURL)
  332. targetName := fmt.Sprintf("%s:latest", repoName)
  333. s.notaryInitRepo(c, repoName)
  334. s.notaryCreateDelegation(c, repoName, "targets/role1", s.not.keys[0].Public)
  335. s.notaryCreateDelegation(c, repoName, "targets/role2", s.not.keys[1].Public)
  336. s.notaryCreateDelegation(c, repoName, "targets/role3", s.not.keys[2].Public)
  337. // import everything except the third key
  338. s.notaryImportKey(c, repoName, "targets/role1", s.not.keys[0].Private)
  339. s.notaryImportKey(c, repoName, "targets/role2", s.not.keys[1].Private)
  340. s.notaryCreateDelegation(c, repoName, "targets/role1/subrole", s.not.keys[3].Public)
  341. s.notaryImportKey(c, repoName, "targets/role1/subrole", s.not.keys[3].Private)
  342. s.notaryPublish(c, repoName)
  343. // tag the image and upload it to the private registry
  344. cli.DockerCmd(c, "tag", "busybox", targetName)
  345. cli.Docker(cli.Args("push", targetName), trustedCmd).Assert(c, SuccessSigningAndPushing)
  346. // check to make sure that the target has been added to targets/role1 and targets/role2, and
  347. // not targets (because there are delegations) or targets/role3 (due to missing key) or
  348. // targets/role1/subrole (due to it being a second level delegation)
  349. s.assertTargetInRoles(c, repoName, "latest", "targets/role1", "targets/role2")
  350. s.assertTargetNotInRoles(c, repoName, "latest", "targets")
  351. // Try pull after push
  352. os.RemoveAll(filepath.Join(config.Dir(), "trust"))
  353. // pull should fail because none of these are the releases role
  354. cli.Docker(cli.Args("pull", targetName), trustedCmd).Assert(c, icmd.Expected{
  355. ExitCode: 1,
  356. })
  357. }
  358. func (s *DockerTrustSuite) TestTrustedPushSignsForRolesWithKeysAndValidPaths(c *check.C) {
  359. repoName := fmt.Sprintf("%v/dockerclirolesbykeysandpaths/trusted", privateRegistryURL)
  360. targetName := fmt.Sprintf("%s:latest", repoName)
  361. s.notaryInitRepo(c, repoName)
  362. s.notaryCreateDelegation(c, repoName, "targets/role1", s.not.keys[0].Public, "l", "z")
  363. s.notaryCreateDelegation(c, repoName, "targets/role2", s.not.keys[1].Public, "x", "y")
  364. s.notaryCreateDelegation(c, repoName, "targets/role3", s.not.keys[2].Public, "latest")
  365. s.notaryCreateDelegation(c, repoName, "targets/role4", s.not.keys[3].Public, "latest")
  366. // import everything except the third key
  367. s.notaryImportKey(c, repoName, "targets/role1", s.not.keys[0].Private)
  368. s.notaryImportKey(c, repoName, "targets/role2", s.not.keys[1].Private)
  369. s.notaryImportKey(c, repoName, "targets/role4", s.not.keys[3].Private)
  370. s.notaryPublish(c, repoName)
  371. // tag the image and upload it to the private registry
  372. cli.DockerCmd(c, "tag", "busybox", targetName)
  373. cli.Docker(cli.Args("push", targetName), trustedCmd).Assert(c, SuccessSigningAndPushing)
  374. // check to make sure that the target has been added to targets/role1 and targets/role4, and
  375. // not targets (because there are delegations) or targets/role2 (due to path restrictions) or
  376. // targets/role3 (due to missing key)
  377. s.assertTargetInRoles(c, repoName, "latest", "targets/role1", "targets/role4")
  378. s.assertTargetNotInRoles(c, repoName, "latest", "targets")
  379. // Try pull after push
  380. os.RemoveAll(filepath.Join(config.Dir(), "trust"))
  381. // pull should fail because none of these are the releases role
  382. cli.Docker(cli.Args("pull", targetName), trustedCmd).Assert(c, icmd.Expected{
  383. ExitCode: 1,
  384. })
  385. }
  386. func (s *DockerTrustSuite) TestTrustedPushDoesntSignTargetsIfDelegationsExist(c *check.C) {
  387. testRequires(c, NotaryHosting)
  388. repoName := fmt.Sprintf("%v/dockerclireleasedelegationnotsignable/trusted", privateRegistryURL)
  389. targetName := fmt.Sprintf("%s:latest", repoName)
  390. s.notaryInitRepo(c, repoName)
  391. s.notaryCreateDelegation(c, repoName, "targets/role1", s.not.keys[0].Public)
  392. s.notaryPublish(c, repoName)
  393. // do not import any delegations key
  394. // tag the image and upload it to the private registry
  395. cli.DockerCmd(c, "tag", "busybox", targetName)
  396. cli.Docker(cli.Args("push", targetName), trustedCmd).Assert(c, icmd.Expected{
  397. ExitCode: 1,
  398. Err: "no valid signing keys",
  399. })
  400. s.assertTargetNotInRoles(c, repoName, "latest", "targets", "targets/role1")
  401. }
  402. func (s *DockerRegistryAuthHtpasswdSuite) TestPushNoCredentialsNoRetry(c *check.C) {
  403. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  404. dockerCmd(c, "tag", "busybox", repoName)
  405. out, _, err := dockerCmdWithError("push", repoName)
  406. c.Assert(err, check.NotNil, check.Commentf(out))
  407. c.Assert(out, check.Not(checker.Contains), "Retrying")
  408. c.Assert(out, checker.Contains, "no basic auth credentials")
  409. }
  410. // This may be flaky but it's needed not to regress on unauthorized push, see #21054
  411. func (s *DockerSuite) TestPushToCentralRegistryUnauthorized(c *check.C) {
  412. testRequires(c, Network)
  413. repoName := "test/busybox"
  414. dockerCmd(c, "tag", "busybox", repoName)
  415. out, _, err := dockerCmdWithError("push", repoName)
  416. c.Assert(err, check.NotNil, check.Commentf(out))
  417. c.Assert(out, check.Not(checker.Contains), "Retrying")
  418. }
  419. func getTestTokenService(status int, body string, retries int) *httptest.Server {
  420. var mu sync.Mutex
  421. return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  422. mu.Lock()
  423. if retries > 0 {
  424. w.WriteHeader(http.StatusServiceUnavailable)
  425. w.Header().Set("Content-Type", "application/json")
  426. w.Write([]byte(`{"errors":[{"code":"UNAVAILABLE","message":"cannot create token at this time"}]}`))
  427. retries--
  428. } else {
  429. w.WriteHeader(status)
  430. w.Header().Set("Content-Type", "application/json")
  431. w.Write([]byte(body))
  432. }
  433. mu.Unlock()
  434. }))
  435. }
  436. func (s *DockerRegistryAuthTokenSuite) TestPushTokenServiceUnauthResponse(c *check.C) {
  437. ts := getTestTokenService(http.StatusUnauthorized, `{"errors": [{"Code":"UNAUTHORIZED", "message": "a message", "detail": null}]}`, 0)
  438. defer ts.Close()
  439. s.setupRegistryWithTokenService(c, ts.URL)
  440. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  441. dockerCmd(c, "tag", "busybox", repoName)
  442. out, _, err := dockerCmdWithError("push", repoName)
  443. c.Assert(err, check.NotNil, check.Commentf(out))
  444. c.Assert(out, checker.Not(checker.Contains), "Retrying")
  445. c.Assert(out, checker.Contains, "unauthorized: a message")
  446. }
  447. func (s *DockerRegistryAuthTokenSuite) TestPushMisconfiguredTokenServiceResponseUnauthorized(c *check.C) {
  448. ts := getTestTokenService(http.StatusUnauthorized, `{"error": "unauthorized"}`, 0)
  449. defer ts.Close()
  450. s.setupRegistryWithTokenService(c, ts.URL)
  451. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  452. dockerCmd(c, "tag", "busybox", repoName)
  453. out, _, err := dockerCmdWithError("push", repoName)
  454. c.Assert(err, check.NotNil, check.Commentf(out))
  455. c.Assert(out, checker.Not(checker.Contains), "Retrying")
  456. split := strings.Split(out, "\n")
  457. c.Assert(split[len(split)-2], check.Equals, "unauthorized: authentication required")
  458. }
  459. func (s *DockerRegistryAuthTokenSuite) TestPushMisconfiguredTokenServiceResponseError(c *check.C) {
  460. ts := getTestTokenService(http.StatusTooManyRequests, `{"errors": [{"code":"TOOMANYREQUESTS","message":"out of tokens"}]}`, 3)
  461. defer ts.Close()
  462. s.setupRegistryWithTokenService(c, ts.URL)
  463. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  464. dockerCmd(c, "tag", "busybox", repoName)
  465. out, _, err := dockerCmdWithError("push", repoName)
  466. c.Assert(err, check.NotNil, check.Commentf(out))
  467. // TODO: isolate test so that it can be guaranteed that the 503 will trigger xfer retries
  468. //c.Assert(out, checker.Contains, "Retrying")
  469. //c.Assert(out, checker.Not(checker.Contains), "Retrying in 15")
  470. split := strings.Split(out, "\n")
  471. c.Assert(split[len(split)-2], check.Equals, "toomanyrequests: out of tokens")
  472. }
  473. func (s *DockerRegistryAuthTokenSuite) TestPushMisconfiguredTokenServiceResponseUnparsable(c *check.C) {
  474. ts := getTestTokenService(http.StatusForbidden, `no way`, 0)
  475. defer ts.Close()
  476. s.setupRegistryWithTokenService(c, ts.URL)
  477. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  478. dockerCmd(c, "tag", "busybox", repoName)
  479. out, _, err := dockerCmdWithError("push", repoName)
  480. c.Assert(err, check.NotNil, check.Commentf(out))
  481. c.Assert(out, checker.Not(checker.Contains), "Retrying")
  482. split := strings.Split(out, "\n")
  483. c.Assert(split[len(split)-2], checker.Contains, "error parsing HTTP 403 response body: ")
  484. }
  485. func (s *DockerRegistryAuthTokenSuite) TestPushMisconfiguredTokenServiceResponseNoToken(c *check.C) {
  486. ts := getTestTokenService(http.StatusOK, `{"something": "wrong"}`, 0)
  487. defer ts.Close()
  488. s.setupRegistryWithTokenService(c, ts.URL)
  489. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  490. dockerCmd(c, "tag", "busybox", repoName)
  491. out, _, err := dockerCmdWithError("push", repoName)
  492. c.Assert(err, check.NotNil, check.Commentf(out))
  493. c.Assert(out, checker.Not(checker.Contains), "Retrying")
  494. split := strings.Split(out, "\n")
  495. c.Assert(split[len(split)-2], check.Equals, "authorization server did not include a token in the response")
  496. }