docker_cli_push_test.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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/pkg/testutil"
  17. icmd "github.com/docker/docker/pkg/testutil/cmd"
  18. "github.com/go-check/check"
  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, 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. dockerCmd(c, "tag", "busybox", repoName)
  235. icmd.RunCmd(icmd.Command(dockerBinary, "push", repoName), trustedCmd).Assert(c, SuccessSigningAndPushing)
  236. // Try pull after push
  237. icmd.RunCmd(icmd.Command(dockerBinary, "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(cliconfig.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. dockerCmd(c, "tag", "busybox", repoName)
  250. icmd.RunCmd(icmd.Command(dockerBinary, "push", repoName), trustedCmdWithPassphrases("12345678", "12345678")).Assert(c, SuccessSigningAndPushing)
  251. // Try pull after push
  252. icmd.RunCmd(icmd.Command(dockerBinary, "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. dockerCmd(c, "tag", "busybox", repoName)
  260. // Using a name that doesn't resolve to an address makes this test faster
  261. icmd.RunCmd(icmd.Command(dockerBinary, "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. dockerCmd(c, "tag", "busybox", repoName)
  270. result := icmd.RunCmd(icmd.Command(dockerBinary, "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. dockerCmd(c, "tag", "busybox", repoName)
  278. dockerCmd(c, "push", repoName)
  279. icmd.RunCmd(icmd.Command(dockerBinary, "push", repoName), trustedCmd).Assert(c, SuccessSigningAndPushing)
  280. // Try pull after push
  281. icmd.RunCmd(icmd.Command(dockerBinary, "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. dockerCmd(c, "tag", "busybox", repoName)
  289. // Do a trusted push
  290. icmd.RunCmd(icmd.Command(dockerBinary, "push", repoName), trustedCmd).Assert(c, SuccessSigningAndPushing)
  291. // Do another trusted push
  292. icmd.RunCmd(icmd.Command(dockerBinary, "push", repoName), trustedCmd).Assert(c, SuccessSigningAndPushing)
  293. dockerCmd(c, "rmi", repoName)
  294. // Try pull to ensure the double push did not break our ability to pull
  295. icmd.RunCmd(icmd.Command(dockerBinary, "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. dockerCmd(c, "tag", "busybox", repoName)
  301. // Push with default passphrases
  302. icmd.RunCmd(icmd.Command(dockerBinary, "push", repoName), trustedCmd).Assert(c, SuccessSigningAndPushing)
  303. // Push with wrong passphrases
  304. icmd.RunCmd(icmd.Command(dockerBinary, "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) TestTrustedPushWithExpiredSnapshot(c *check.C) {
  310. c.Skip("Currently changes system time, causing instability")
  311. repoName := fmt.Sprintf("%v/dockercliexpiredsnapshot/trusted:latest", privateRegistryURL)
  312. // tag the image and upload it to the private registry
  313. dockerCmd(c, "tag", "busybox", repoName)
  314. // Push with default passphrases
  315. icmd.RunCmd(icmd.Command(dockerBinary, "push", repoName), trustedCmd).Assert(c, SuccessSigningAndPushing)
  316. // Snapshots last for three years. This should be expired
  317. fourYearsLater := time.Now().Add(time.Hour * 24 * 365 * 4)
  318. testutil.RunAtDifferentDate(fourYearsLater, func() {
  319. // Push with wrong passphrases
  320. icmd.RunCmd(icmd.Cmd{
  321. Command: []string{dockerBinary, "push", repoName},
  322. }, trustedCmd).Assert(c, icmd.Expected{
  323. ExitCode: 1,
  324. Err: "repository out-of-date",
  325. })
  326. })
  327. }
  328. func (s *DockerTrustSuite) TestTrustedPushWithExpiredTimestamp(c *check.C) {
  329. c.Skip("Currently changes system time, causing instability")
  330. repoName := fmt.Sprintf("%v/dockercliexpiredtimestamppush/trusted:latest", privateRegistryURL)
  331. // tag the image and upload it to the private registry
  332. dockerCmd(c, "tag", "busybox", repoName)
  333. // Push with default passphrases
  334. icmd.RunCmd(icmd.Command(dockerBinary, "push", repoName), trustedCmd).Assert(c, SuccessSigningAndPushing)
  335. // The timestamps expire in two weeks. Lets check three
  336. threeWeeksLater := time.Now().Add(time.Hour * 24 * 21)
  337. // Should succeed because the server transparently re-signs one
  338. testutil.RunAtDifferentDate(threeWeeksLater, func() {
  339. icmd.RunCmd(icmd.Command(dockerBinary, "push", repoName),
  340. trustedCmd).Assert(c, SuccessSigningAndPushing)
  341. })
  342. }
  343. func (s *DockerTrustSuite) TestTrustedPushWithReleasesDelegationOnly(c *check.C) {
  344. testRequires(c, NotaryHosting)
  345. repoName := fmt.Sprintf("%v/dockerclireleasedelegationinitfirst/trusted", privateRegistryURL)
  346. targetName := fmt.Sprintf("%s:latest", repoName)
  347. s.notaryInitRepo(c, repoName)
  348. s.notaryCreateDelegation(c, repoName, "targets/releases", s.not.keys[0].Public)
  349. s.notaryPublish(c, repoName)
  350. s.notaryImportKey(c, repoName, "targets/releases", s.not.keys[0].Private)
  351. // tag the image and upload it to the private registry
  352. dockerCmd(c, "tag", "busybox", targetName)
  353. icmd.RunCmd(icmd.Command(dockerBinary, "push", targetName), trustedCmd).Assert(c, SuccessSigningAndPushing)
  354. // check to make sure that the target has been added to targets/releases and not targets
  355. s.assertTargetInRoles(c, repoName, "latest", "targets/releases")
  356. s.assertTargetNotInRoles(c, repoName, "latest", "targets")
  357. // Try pull after push
  358. os.RemoveAll(filepath.Join(cliconfig.Dir(), "trust"))
  359. icmd.RunCmd(icmd.Command(dockerBinary, "pull", targetName), trustedCmd).Assert(c, icmd.Expected{
  360. Out: "Status: Image is up to date",
  361. })
  362. }
  363. func (s *DockerTrustSuite) TestTrustedPushSignsAllFirstLevelRolesWeHaveKeysFor(c *check.C) {
  364. testRequires(c, NotaryHosting)
  365. repoName := fmt.Sprintf("%v/dockerclimanyroles/trusted", privateRegistryURL)
  366. targetName := fmt.Sprintf("%s:latest", repoName)
  367. s.notaryInitRepo(c, repoName)
  368. s.notaryCreateDelegation(c, repoName, "targets/role1", s.not.keys[0].Public)
  369. s.notaryCreateDelegation(c, repoName, "targets/role2", s.not.keys[1].Public)
  370. s.notaryCreateDelegation(c, repoName, "targets/role3", s.not.keys[2].Public)
  371. // import everything except the third key
  372. s.notaryImportKey(c, repoName, "targets/role1", s.not.keys[0].Private)
  373. s.notaryImportKey(c, repoName, "targets/role2", s.not.keys[1].Private)
  374. s.notaryCreateDelegation(c, repoName, "targets/role1/subrole", s.not.keys[3].Public)
  375. s.notaryImportKey(c, repoName, "targets/role1/subrole", s.not.keys[3].Private)
  376. s.notaryPublish(c, repoName)
  377. // tag the image and upload it to the private registry
  378. dockerCmd(c, "tag", "busybox", targetName)
  379. icmd.RunCmd(icmd.Command(dockerBinary, "push", targetName), trustedCmd).Assert(c, SuccessSigningAndPushing)
  380. // check to make sure that the target has been added to targets/role1 and targets/role2, and
  381. // not targets (because there are delegations) or targets/role3 (due to missing key) or
  382. // targets/role1/subrole (due to it being a second level delegation)
  383. s.assertTargetInRoles(c, repoName, "latest", "targets/role1", "targets/role2")
  384. s.assertTargetNotInRoles(c, repoName, "latest", "targets")
  385. // Try pull after push
  386. os.RemoveAll(filepath.Join(cliconfig.Dir(), "trust"))
  387. // pull should fail because none of these are the releases role
  388. icmd.RunCmd(icmd.Command(dockerBinary, "pull", targetName), trustedCmd).Assert(c, icmd.Expected{
  389. ExitCode: 1,
  390. })
  391. }
  392. func (s *DockerTrustSuite) TestTrustedPushSignsForRolesWithKeysAndValidPaths(c *check.C) {
  393. repoName := fmt.Sprintf("%v/dockerclirolesbykeysandpaths/trusted", privateRegistryURL)
  394. targetName := fmt.Sprintf("%s:latest", repoName)
  395. s.notaryInitRepo(c, repoName)
  396. s.notaryCreateDelegation(c, repoName, "targets/role1", s.not.keys[0].Public, "l", "z")
  397. s.notaryCreateDelegation(c, repoName, "targets/role2", s.not.keys[1].Public, "x", "y")
  398. s.notaryCreateDelegation(c, repoName, "targets/role3", s.not.keys[2].Public, "latest")
  399. s.notaryCreateDelegation(c, repoName, "targets/role4", s.not.keys[3].Public, "latest")
  400. // import everything except the third key
  401. s.notaryImportKey(c, repoName, "targets/role1", s.not.keys[0].Private)
  402. s.notaryImportKey(c, repoName, "targets/role2", s.not.keys[1].Private)
  403. s.notaryImportKey(c, repoName, "targets/role4", s.not.keys[3].Private)
  404. s.notaryPublish(c, repoName)
  405. // tag the image and upload it to the private registry
  406. dockerCmd(c, "tag", "busybox", targetName)
  407. icmd.RunCmd(icmd.Command(dockerBinary, "push", targetName), trustedCmd).Assert(c, SuccessSigningAndPushing)
  408. // check to make sure that the target has been added to targets/role1 and targets/role4, and
  409. // not targets (because there are delegations) or targets/role2 (due to path restrictions) or
  410. // targets/role3 (due to missing key)
  411. s.assertTargetInRoles(c, repoName, "latest", "targets/role1", "targets/role4")
  412. s.assertTargetNotInRoles(c, repoName, "latest", "targets")
  413. // Try pull after push
  414. os.RemoveAll(filepath.Join(cliconfig.Dir(), "trust"))
  415. // pull should fail because none of these are the releases role
  416. icmd.RunCmd(icmd.Command(dockerBinary, "pull", targetName), trustedCmd).Assert(c, icmd.Expected{
  417. ExitCode: 1,
  418. })
  419. }
  420. func (s *DockerTrustSuite) TestTrustedPushDoesntSignTargetsIfDelegationsExist(c *check.C) {
  421. testRequires(c, NotaryHosting)
  422. repoName := fmt.Sprintf("%v/dockerclireleasedelegationnotsignable/trusted", privateRegistryURL)
  423. targetName := fmt.Sprintf("%s:latest", repoName)
  424. s.notaryInitRepo(c, repoName)
  425. s.notaryCreateDelegation(c, repoName, "targets/role1", s.not.keys[0].Public)
  426. s.notaryPublish(c, repoName)
  427. // do not import any delegations key
  428. // tag the image and upload it to the private registry
  429. dockerCmd(c, "tag", "busybox", targetName)
  430. icmd.RunCmd(icmd.Command(dockerBinary, "push", targetName), trustedCmd).Assert(c, icmd.Expected{
  431. ExitCode: 1,
  432. Err: "no valid signing keys",
  433. })
  434. s.assertTargetNotInRoles(c, repoName, "latest", "targets", "targets/role1")
  435. }
  436. func (s *DockerRegistryAuthHtpasswdSuite) TestPushNoCredentialsNoRetry(c *check.C) {
  437. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  438. dockerCmd(c, "tag", "busybox", repoName)
  439. out, _, err := dockerCmdWithError("push", repoName)
  440. c.Assert(err, check.NotNil, check.Commentf(out))
  441. c.Assert(out, check.Not(checker.Contains), "Retrying")
  442. c.Assert(out, checker.Contains, "no basic auth credentials")
  443. }
  444. // This may be flaky but it's needed not to regress on unauthorized push, see #21054
  445. func (s *DockerSuite) TestPushToCentralRegistryUnauthorized(c *check.C) {
  446. testRequires(c, Network)
  447. repoName := "test/busybox"
  448. dockerCmd(c, "tag", "busybox", repoName)
  449. out, _, err := dockerCmdWithError("push", repoName)
  450. c.Assert(err, check.NotNil, check.Commentf(out))
  451. c.Assert(out, check.Not(checker.Contains), "Retrying")
  452. }
  453. func getTestTokenService(status int, body string, retries int) *httptest.Server {
  454. var mu sync.Mutex
  455. return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  456. mu.Lock()
  457. if retries > 0 {
  458. w.WriteHeader(http.StatusServiceUnavailable)
  459. w.Header().Set("Content-Type", "application/json")
  460. w.Write([]byte(`{"errors":[{"code":"UNAVAILABLE","message":"cannot create token at this time"}]}`))
  461. retries--
  462. } else {
  463. w.WriteHeader(status)
  464. w.Header().Set("Content-Type", "application/json")
  465. w.Write([]byte(body))
  466. }
  467. mu.Unlock()
  468. }))
  469. }
  470. func (s *DockerRegistryAuthTokenSuite) TestPushTokenServiceUnauthResponse(c *check.C) {
  471. ts := getTestTokenService(http.StatusUnauthorized, `{"errors": [{"Code":"UNAUTHORIZED", "message": "a message", "detail": null}]}`, 0)
  472. defer ts.Close()
  473. s.setupRegistryWithTokenService(c, ts.URL)
  474. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  475. dockerCmd(c, "tag", "busybox", repoName)
  476. out, _, err := dockerCmdWithError("push", repoName)
  477. c.Assert(err, check.NotNil, check.Commentf(out))
  478. c.Assert(out, checker.Not(checker.Contains), "Retrying")
  479. c.Assert(out, checker.Contains, "unauthorized: a message")
  480. }
  481. func (s *DockerRegistryAuthTokenSuite) TestPushMisconfiguredTokenServiceResponseUnauthorized(c *check.C) {
  482. ts := getTestTokenService(http.StatusUnauthorized, `{"error": "unauthorized"}`, 0)
  483. defer ts.Close()
  484. s.setupRegistryWithTokenService(c, ts.URL)
  485. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  486. dockerCmd(c, "tag", "busybox", repoName)
  487. out, _, err := dockerCmdWithError("push", repoName)
  488. c.Assert(err, check.NotNil, check.Commentf(out))
  489. c.Assert(out, checker.Not(checker.Contains), "Retrying")
  490. split := strings.Split(out, "\n")
  491. c.Assert(split[len(split)-2], check.Equals, "unauthorized: authentication required")
  492. }
  493. func (s *DockerRegistryAuthTokenSuite) TestPushMisconfiguredTokenServiceResponseError(c *check.C) {
  494. ts := getTestTokenService(http.StatusTooManyRequests, `{"errors": [{"code":"TOOMANYREQUESTS","message":"out of tokens"}]}`, 10)
  495. defer ts.Close()
  496. s.setupRegistryWithTokenService(c, ts.URL)
  497. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  498. dockerCmd(c, "tag", "busybox", repoName)
  499. out, _, err := dockerCmdWithError("push", repoName)
  500. c.Assert(err, check.NotNil, check.Commentf(out))
  501. c.Assert(out, checker.Contains, "Retrying")
  502. c.Assert(out, checker.Not(checker.Contains), "Retrying in 15")
  503. split := strings.Split(out, "\n")
  504. c.Assert(split[len(split)-2], check.Equals, "toomanyrequests: out of tokens")
  505. }
  506. func (s *DockerRegistryAuthTokenSuite) TestPushMisconfiguredTokenServiceResponseUnparsable(c *check.C) {
  507. ts := getTestTokenService(http.StatusForbidden, `no way`, 0)
  508. defer ts.Close()
  509. s.setupRegistryWithTokenService(c, ts.URL)
  510. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  511. dockerCmd(c, "tag", "busybox", repoName)
  512. out, _, err := dockerCmdWithError("push", repoName)
  513. c.Assert(err, check.NotNil, check.Commentf(out))
  514. c.Assert(out, checker.Not(checker.Contains), "Retrying")
  515. split := strings.Split(out, "\n")
  516. c.Assert(split[len(split)-2], checker.Contains, "error parsing HTTP 403 response body: ")
  517. }
  518. func (s *DockerRegistryAuthTokenSuite) TestPushMisconfiguredTokenServiceResponseNoToken(c *check.C) {
  519. ts := getTestTokenService(http.StatusOK, `{"something": "wrong"}`, 0)
  520. defer ts.Close()
  521. s.setupRegistryWithTokenService(c, ts.URL)
  522. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  523. dockerCmd(c, "tag", "busybox", repoName)
  524. out, _, err := dockerCmdWithError("push", repoName)
  525. c.Assert(err, check.NotNil, check.Commentf(out))
  526. c.Assert(out, checker.Not(checker.Contains), "Retrying")
  527. split := strings.Split(out, "\n")
  528. c.Assert(split[len(split)-2], check.Equals, "authorization server did not include a token in the response")
  529. }