docker_cli_push_test.go 26 KB

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