docker_cli_push_test.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. package main
  2. import (
  3. "archive/tar"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "net/http/httptest"
  8. "os"
  9. "os/exec"
  10. "path/filepath"
  11. "strings"
  12. "sync"
  13. "time"
  14. "github.com/docker/distribution/reference"
  15. cliconfig "github.com/docker/docker/cli/config"
  16. "github.com/docker/docker/pkg/integration"
  17. "github.com/docker/docker/pkg/integration/checker"
  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. importCmd := exec.Command(dockerBinary, "import", "-", repoName)
  111. importCmd.Stdin = freader
  112. out, _, err := runCommandWithOutput(importCmd)
  113. c.Assert(err, check.IsNil, check.Commentf("import failed: %q", out))
  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. _, err := buildImage(repo, fmt.Sprintf(`
  132. FROM busybox
  133. ENTRYPOINT ["/bin/echo"]
  134. ENV FOO foo
  135. ENV BAR bar
  136. CMD echo %s
  137. `, repo), true)
  138. c.Assert(err, checker.IsNil)
  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. _, _, err := runCommandWithOutput(exec.Command(dockerBinary, "push", repo))
  146. results <- err
  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. pushCmd := exec.Command(dockerBinary, "push", repoName)
  237. s.trustedCmd(pushCmd)
  238. out, _, err := runCommandWithOutput(pushCmd)
  239. c.Assert(err, check.IsNil, check.Commentf("Error running trusted push: %s\n%s", err, out))
  240. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  241. // Try pull after push
  242. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  243. s.trustedCmd(pullCmd)
  244. out, _, err = runCommandWithOutput(pullCmd)
  245. c.Assert(err, check.IsNil, check.Commentf(out))
  246. c.Assert(string(out), checker.Contains, "Status: Image is up to date", check.Commentf(out))
  247. // Assert that we rotated the snapshot key to the server by checking our local keystore
  248. contents, err := ioutil.ReadDir(filepath.Join(cliconfig.Dir(), "trust/private/tuf_keys", privateRegistryURL, "dockerclitrusted/pushtest"))
  249. c.Assert(err, check.IsNil, check.Commentf("Unable to read local tuf key files"))
  250. // Check that we only have 1 key (targets key)
  251. c.Assert(contents, checker.HasLen, 1)
  252. }
  253. func (s *DockerTrustSuite) TestTrustedPushWithEnvPasswords(c *check.C) {
  254. repoName := fmt.Sprintf("%v/dockerclienv/trusted:latest", privateRegistryURL)
  255. // tag the image and upload it to the private registry
  256. dockerCmd(c, "tag", "busybox", repoName)
  257. pushCmd := exec.Command(dockerBinary, "push", repoName)
  258. s.trustedCmdWithPassphrases(pushCmd, "12345678", "12345678")
  259. out, _, err := runCommandWithOutput(pushCmd)
  260. c.Assert(err, check.IsNil, check.Commentf("Error running trusted push: %s\n%s", err, out))
  261. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  262. // Try pull after push
  263. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  264. s.trustedCmd(pullCmd)
  265. out, _, err = runCommandWithOutput(pullCmd)
  266. c.Assert(err, check.IsNil, check.Commentf(out))
  267. c.Assert(string(out), checker.Contains, "Status: Image is up to date", check.Commentf(out))
  268. }
  269. func (s *DockerTrustSuite) TestTrustedPushWithFailingServer(c *check.C) {
  270. repoName := fmt.Sprintf("%v/dockerclitrusted/failingserver:latest", privateRegistryURL)
  271. // tag the image and upload it to the private registry
  272. dockerCmd(c, "tag", "busybox", repoName)
  273. pushCmd := exec.Command(dockerBinary, "push", repoName)
  274. // Using a name that doesn't resolve to an address makes this test faster
  275. s.trustedCmdWithServer(pushCmd, "https://server.invalid:81/")
  276. out, _, err := runCommandWithOutput(pushCmd)
  277. c.Assert(err, check.NotNil, check.Commentf("Missing error while running trusted push w/ no server"))
  278. c.Assert(out, checker.Contains, "error contacting notary server", check.Commentf("Missing expected output on trusted push"))
  279. }
  280. func (s *DockerTrustSuite) TestTrustedPushWithoutServerAndUntrusted(c *check.C) {
  281. repoName := fmt.Sprintf("%v/dockerclitrusted/trustedandnot:latest", privateRegistryURL)
  282. // tag the image and upload it to the private registry
  283. dockerCmd(c, "tag", "busybox", repoName)
  284. pushCmd := exec.Command(dockerBinary, "push", "--disable-content-trust", repoName)
  285. // Using a name that doesn't resolve to an address makes this test faster
  286. s.trustedCmdWithServer(pushCmd, "https://server.invalid")
  287. out, _, err := runCommandWithOutput(pushCmd)
  288. c.Assert(err, check.IsNil, check.Commentf("trusted push with no server and --disable-content-trust failed: %s\n%s", err, out))
  289. c.Assert(out, check.Not(checker.Contains), "Error establishing connection to notary repository", check.Commentf("Missing expected output on trusted push with --disable-content-trust:"))
  290. }
  291. func (s *DockerTrustSuite) TestTrustedPushWithExistingTag(c *check.C) {
  292. repoName := fmt.Sprintf("%v/dockerclitag/trusted:latest", privateRegistryURL)
  293. // tag the image and upload it to the private registry
  294. dockerCmd(c, "tag", "busybox", repoName)
  295. dockerCmd(c, "push", repoName)
  296. pushCmd := exec.Command(dockerBinary, "push", repoName)
  297. s.trustedCmd(pushCmd)
  298. out, _, err := runCommandWithOutput(pushCmd)
  299. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  300. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  301. // Try pull after push
  302. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  303. s.trustedCmd(pullCmd)
  304. out, _, err = runCommandWithOutput(pullCmd)
  305. c.Assert(err, check.IsNil, check.Commentf(out))
  306. c.Assert(string(out), checker.Contains, "Status: Image is up to date", check.Commentf(out))
  307. }
  308. func (s *DockerTrustSuite) TestTrustedPushWithExistingSignedTag(c *check.C) {
  309. repoName := fmt.Sprintf("%v/dockerclipushpush/trusted:latest", privateRegistryURL)
  310. // tag the image and upload it to the private registry
  311. dockerCmd(c, "tag", "busybox", repoName)
  312. // Do a trusted push
  313. pushCmd := exec.Command(dockerBinary, "push", repoName)
  314. s.trustedCmd(pushCmd)
  315. out, _, err := runCommandWithOutput(pushCmd)
  316. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  317. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  318. // Do another trusted push
  319. pushCmd = exec.Command(dockerBinary, "push", repoName)
  320. s.trustedCmd(pushCmd)
  321. out, _, err = runCommandWithOutput(pushCmd)
  322. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  323. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  324. dockerCmd(c, "rmi", repoName)
  325. // Try pull to ensure the double push did not break our ability to pull
  326. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  327. s.trustedCmd(pullCmd)
  328. out, _, err = runCommandWithOutput(pullCmd)
  329. c.Assert(err, check.IsNil, check.Commentf("Error running trusted pull: %s\n%s", err, out))
  330. c.Assert(out, checker.Contains, "Status: Downloaded", check.Commentf("Missing expected output on trusted pull with --disable-content-trust"))
  331. }
  332. func (s *DockerTrustSuite) TestTrustedPushWithIncorrectPassphraseForNonRoot(c *check.C) {
  333. repoName := fmt.Sprintf("%v/dockercliincorretpwd/trusted:latest", privateRegistryURL)
  334. // tag the image and upload it to the private registry
  335. dockerCmd(c, "tag", "busybox", repoName)
  336. // Push with default passphrases
  337. pushCmd := exec.Command(dockerBinary, "push", repoName)
  338. s.trustedCmd(pushCmd)
  339. out, _, err := runCommandWithOutput(pushCmd)
  340. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  341. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push:\n%s", out))
  342. // Push with wrong passphrases
  343. pushCmd = exec.Command(dockerBinary, "push", repoName)
  344. s.trustedCmdWithPassphrases(pushCmd, "12345678", "87654321")
  345. out, _, err = runCommandWithOutput(pushCmd)
  346. c.Assert(err, check.NotNil, check.Commentf("Error missing from trusted push with short targets passphrase: \n%s", out))
  347. c.Assert(out, checker.Contains, "could not find necessary signing keys", check.Commentf("Missing expected output on trusted push with short targets/snapsnot passphrase"))
  348. }
  349. func (s *DockerTrustSuite) TestTrustedPushWithExpiredSnapshot(c *check.C) {
  350. c.Skip("Currently changes system time, causing instability")
  351. repoName := fmt.Sprintf("%v/dockercliexpiredsnapshot/trusted:latest", privateRegistryURL)
  352. // tag the image and upload it to the private registry
  353. dockerCmd(c, "tag", "busybox", repoName)
  354. // Push with default passphrases
  355. pushCmd := exec.Command(dockerBinary, "push", repoName)
  356. s.trustedCmd(pushCmd)
  357. out, _, err := runCommandWithOutput(pushCmd)
  358. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  359. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  360. // Snapshots last for three years. This should be expired
  361. fourYearsLater := time.Now().Add(time.Hour * 24 * 365 * 4)
  362. integration.RunAtDifferentDate(fourYearsLater, func() {
  363. // Push with wrong passphrases
  364. pushCmd = exec.Command(dockerBinary, "push", repoName)
  365. s.trustedCmd(pushCmd)
  366. out, _, err = runCommandWithOutput(pushCmd)
  367. c.Assert(err, check.NotNil, check.Commentf("Error missing from trusted push with expired snapshot: \n%s", out))
  368. c.Assert(out, checker.Contains, "repository out-of-date", check.Commentf("Missing expected output on trusted push with expired snapshot"))
  369. })
  370. }
  371. func (s *DockerTrustSuite) TestTrustedPushWithExpiredTimestamp(c *check.C) {
  372. c.Skip("Currently changes system time, causing instability")
  373. repoName := fmt.Sprintf("%v/dockercliexpiredtimestamppush/trusted:latest", privateRegistryURL)
  374. // tag the image and upload it to the private registry
  375. dockerCmd(c, "tag", "busybox", repoName)
  376. // Push with default passphrases
  377. pushCmd := exec.Command(dockerBinary, "push", repoName)
  378. s.trustedCmd(pushCmd)
  379. out, _, err := runCommandWithOutput(pushCmd)
  380. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  381. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  382. // The timestamps expire in two weeks. Lets check three
  383. threeWeeksLater := time.Now().Add(time.Hour * 24 * 21)
  384. // Should succeed because the server transparently re-signs one
  385. integration.RunAtDifferentDate(threeWeeksLater, func() {
  386. pushCmd := exec.Command(dockerBinary, "push", repoName)
  387. s.trustedCmd(pushCmd)
  388. out, _, err := runCommandWithOutput(pushCmd)
  389. c.Assert(err, check.IsNil, check.Commentf("Error running trusted push: %s\n%s", err, out))
  390. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with expired timestamp"))
  391. })
  392. }
  393. func (s *DockerTrustSuite) TestTrustedPushWithReleasesDelegationOnly(c *check.C) {
  394. testRequires(c, NotaryHosting)
  395. repoName := fmt.Sprintf("%v/dockerclireleasedelegationinitfirst/trusted", privateRegistryURL)
  396. targetName := fmt.Sprintf("%s:latest", repoName)
  397. s.notaryInitRepo(c, repoName)
  398. s.notaryCreateDelegation(c, repoName, "targets/releases", s.not.keys[0].Public)
  399. s.notaryPublish(c, repoName)
  400. s.notaryImportKey(c, repoName, "targets/releases", s.not.keys[0].Private)
  401. // tag the image and upload it to the private registry
  402. dockerCmd(c, "tag", "busybox", targetName)
  403. pushCmd := exec.Command(dockerBinary, "push", targetName)
  404. s.trustedCmd(pushCmd)
  405. out, _, err := runCommandWithOutput(pushCmd)
  406. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  407. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  408. // check to make sure that the target has been added to targets/releases and not targets
  409. s.assertTargetInRoles(c, repoName, "latest", "targets/releases")
  410. s.assertTargetNotInRoles(c, repoName, "latest", "targets")
  411. // Try pull after push
  412. os.RemoveAll(filepath.Join(cliconfig.Dir(), "trust"))
  413. pullCmd := exec.Command(dockerBinary, "pull", targetName)
  414. s.trustedCmd(pullCmd)
  415. out, _, err = runCommandWithOutput(pullCmd)
  416. c.Assert(err, check.IsNil, check.Commentf(out))
  417. c.Assert(string(out), checker.Contains, "Status: Image is up to date", check.Commentf(out))
  418. }
  419. func (s *DockerTrustSuite) TestTrustedPushSignsAllFirstLevelRolesWeHaveKeysFor(c *check.C) {
  420. testRequires(c, NotaryHosting)
  421. repoName := fmt.Sprintf("%v/dockerclimanyroles/trusted", privateRegistryURL)
  422. targetName := fmt.Sprintf("%s:latest", repoName)
  423. s.notaryInitRepo(c, repoName)
  424. s.notaryCreateDelegation(c, repoName, "targets/role1", s.not.keys[0].Public)
  425. s.notaryCreateDelegation(c, repoName, "targets/role2", s.not.keys[1].Public)
  426. s.notaryCreateDelegation(c, repoName, "targets/role3", s.not.keys[2].Public)
  427. // import everything except the third key
  428. s.notaryImportKey(c, repoName, "targets/role1", s.not.keys[0].Private)
  429. s.notaryImportKey(c, repoName, "targets/role2", s.not.keys[1].Private)
  430. s.notaryCreateDelegation(c, repoName, "targets/role1/subrole", s.not.keys[3].Public)
  431. s.notaryImportKey(c, repoName, "targets/role1/subrole", s.not.keys[3].Private)
  432. s.notaryPublish(c, repoName)
  433. // tag the image and upload it to the private registry
  434. dockerCmd(c, "tag", "busybox", targetName)
  435. pushCmd := exec.Command(dockerBinary, "push", targetName)
  436. s.trustedCmd(pushCmd)
  437. out, _, err := runCommandWithOutput(pushCmd)
  438. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  439. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  440. // check to make sure that the target has been added to targets/role1 and targets/role2, and
  441. // not targets (because there are delegations) or targets/role3 (due to missing key) or
  442. // targets/role1/subrole (due to it being a second level delegation)
  443. s.assertTargetInRoles(c, repoName, "latest", "targets/role1", "targets/role2")
  444. s.assertTargetNotInRoles(c, repoName, "latest", "targets")
  445. // Try pull after push
  446. os.RemoveAll(filepath.Join(cliconfig.Dir(), "trust"))
  447. // pull should fail because none of these are the releases role
  448. pullCmd := exec.Command(dockerBinary, "pull", targetName)
  449. s.trustedCmd(pullCmd)
  450. out, _, err = runCommandWithOutput(pullCmd)
  451. c.Assert(err, check.NotNil, check.Commentf(out))
  452. }
  453. func (s *DockerTrustSuite) TestTrustedPushSignsForRolesWithKeysAndValidPaths(c *check.C) {
  454. repoName := fmt.Sprintf("%v/dockerclirolesbykeysandpaths/trusted", privateRegistryURL)
  455. targetName := fmt.Sprintf("%s:latest", repoName)
  456. s.notaryInitRepo(c, repoName)
  457. s.notaryCreateDelegation(c, repoName, "targets/role1", s.not.keys[0].Public, "l", "z")
  458. s.notaryCreateDelegation(c, repoName, "targets/role2", s.not.keys[1].Public, "x", "y")
  459. s.notaryCreateDelegation(c, repoName, "targets/role3", s.not.keys[2].Public, "latest")
  460. s.notaryCreateDelegation(c, repoName, "targets/role4", s.not.keys[3].Public, "latest")
  461. // import everything except the third key
  462. s.notaryImportKey(c, repoName, "targets/role1", s.not.keys[0].Private)
  463. s.notaryImportKey(c, repoName, "targets/role2", s.not.keys[1].Private)
  464. s.notaryImportKey(c, repoName, "targets/role4", s.not.keys[3].Private)
  465. s.notaryPublish(c, repoName)
  466. // tag the image and upload it to the private registry
  467. dockerCmd(c, "tag", "busybox", targetName)
  468. pushCmd := exec.Command(dockerBinary, "push", targetName)
  469. s.trustedCmd(pushCmd)
  470. out, _, err := runCommandWithOutput(pushCmd)
  471. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  472. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  473. // check to make sure that the target has been added to targets/role1 and targets/role4, and
  474. // not targets (because there are delegations) or targets/role2 (due to path restrictions) or
  475. // targets/role3 (due to missing key)
  476. s.assertTargetInRoles(c, repoName, "latest", "targets/role1", "targets/role4")
  477. s.assertTargetNotInRoles(c, repoName, "latest", "targets")
  478. // Try pull after push
  479. os.RemoveAll(filepath.Join(cliconfig.Dir(), "trust"))
  480. // pull should fail because none of these are the releases role
  481. pullCmd := exec.Command(dockerBinary, "pull", targetName)
  482. s.trustedCmd(pullCmd)
  483. out, _, err = runCommandWithOutput(pullCmd)
  484. c.Assert(err, check.NotNil, check.Commentf(out))
  485. }
  486. func (s *DockerTrustSuite) TestTrustedPushDoesntSignTargetsIfDelegationsExist(c *check.C) {
  487. testRequires(c, NotaryHosting)
  488. repoName := fmt.Sprintf("%v/dockerclireleasedelegationnotsignable/trusted", privateRegistryURL)
  489. targetName := fmt.Sprintf("%s:latest", repoName)
  490. s.notaryInitRepo(c, repoName)
  491. s.notaryCreateDelegation(c, repoName, "targets/role1", s.not.keys[0].Public)
  492. s.notaryPublish(c, repoName)
  493. // do not import any delegations key
  494. // tag the image and upload it to the private registry
  495. dockerCmd(c, "tag", "busybox", targetName)
  496. pushCmd := exec.Command(dockerBinary, "push", targetName)
  497. s.trustedCmd(pushCmd)
  498. out, _, err := runCommandWithOutput(pushCmd)
  499. c.Assert(err, check.NotNil, check.Commentf("trusted push succeeded but should have failed:\n%s", out))
  500. c.Assert(out, checker.Contains, "no valid signing keys",
  501. check.Commentf("Missing expected output on trusted push without keys"))
  502. s.assertTargetNotInRoles(c, repoName, "latest", "targets", "targets/role1")
  503. }
  504. func (s *DockerRegistryAuthHtpasswdSuite) TestPushNoCredentialsNoRetry(c *check.C) {
  505. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  506. dockerCmd(c, "tag", "busybox", repoName)
  507. out, _, err := dockerCmdWithError("push", repoName)
  508. c.Assert(err, check.NotNil, check.Commentf(out))
  509. c.Assert(out, check.Not(checker.Contains), "Retrying")
  510. c.Assert(out, checker.Contains, "no basic auth credentials")
  511. }
  512. // This may be flaky but it's needed not to regress on unauthorized push, see #21054
  513. func (s *DockerSuite) TestPushToCentralRegistryUnauthorized(c *check.C) {
  514. testRequires(c, Network)
  515. repoName := "test/busybox"
  516. dockerCmd(c, "tag", "busybox", repoName)
  517. out, _, err := dockerCmdWithError("push", repoName)
  518. c.Assert(err, check.NotNil, check.Commentf(out))
  519. c.Assert(out, check.Not(checker.Contains), "Retrying")
  520. }
  521. func getTestTokenService(status int, body string, retries int) *httptest.Server {
  522. var mu sync.Mutex
  523. return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  524. mu.Lock()
  525. if retries > 0 {
  526. w.WriteHeader(http.StatusServiceUnavailable)
  527. w.Header().Set("Content-Type", "application/json")
  528. w.Write([]byte(`{"errors":[{"code":"UNAVAILABLE","message":"cannot create token at this time"}]}`))
  529. retries--
  530. } else {
  531. w.WriteHeader(status)
  532. w.Header().Set("Content-Type", "application/json")
  533. w.Write([]byte(body))
  534. }
  535. mu.Unlock()
  536. }))
  537. }
  538. func (s *DockerRegistryAuthTokenSuite) TestPushTokenServiceUnauthResponse(c *check.C) {
  539. ts := getTestTokenService(http.StatusUnauthorized, `{"errors": [{"Code":"UNAUTHORIZED", "message": "a message", "detail": null}]}`, 0)
  540. defer ts.Close()
  541. s.setupRegistryWithTokenService(c, ts.URL)
  542. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  543. dockerCmd(c, "tag", "busybox", repoName)
  544. out, _, err := dockerCmdWithError("push", repoName)
  545. c.Assert(err, check.NotNil, check.Commentf(out))
  546. c.Assert(out, checker.Not(checker.Contains), "Retrying")
  547. c.Assert(out, checker.Contains, "unauthorized: a message")
  548. }
  549. func (s *DockerRegistryAuthTokenSuite) TestPushMisconfiguredTokenServiceResponseUnauthorized(c *check.C) {
  550. ts := getTestTokenService(http.StatusUnauthorized, `{"error": "unauthorized"}`, 0)
  551. defer ts.Close()
  552. s.setupRegistryWithTokenService(c, ts.URL)
  553. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  554. dockerCmd(c, "tag", "busybox", repoName)
  555. out, _, err := dockerCmdWithError("push", repoName)
  556. c.Assert(err, check.NotNil, check.Commentf(out))
  557. c.Assert(out, checker.Not(checker.Contains), "Retrying")
  558. split := strings.Split(out, "\n")
  559. c.Assert(split[len(split)-2], check.Equals, "unauthorized: authentication required")
  560. }
  561. func (s *DockerRegistryAuthTokenSuite) TestPushMisconfiguredTokenServiceResponseError(c *check.C) {
  562. ts := getTestTokenService(http.StatusTooManyRequests, `{"errors": [{"code":"TOOMANYREQUESTS","message":"out of tokens"}]}`, 4)
  563. defer ts.Close()
  564. s.setupRegistryWithTokenService(c, ts.URL)
  565. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  566. dockerCmd(c, "tag", "busybox", repoName)
  567. out, _, err := dockerCmdWithError("push", repoName)
  568. c.Assert(err, check.NotNil, check.Commentf(out))
  569. c.Assert(out, checker.Contains, "Retrying")
  570. c.Assert(out, checker.Not(checker.Contains), "Retrying in 15")
  571. split := strings.Split(out, "\n")
  572. c.Assert(split[len(split)-2], check.Equals, "toomanyrequests: out of tokens")
  573. }
  574. func (s *DockerRegistryAuthTokenSuite) TestPushMisconfiguredTokenServiceResponseUnparsable(c *check.C) {
  575. ts := getTestTokenService(http.StatusForbidden, `no way`, 0)
  576. defer ts.Close()
  577. s.setupRegistryWithTokenService(c, ts.URL)
  578. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  579. dockerCmd(c, "tag", "busybox", repoName)
  580. out, _, err := dockerCmdWithError("push", repoName)
  581. c.Assert(err, check.NotNil, check.Commentf(out))
  582. c.Assert(out, checker.Not(checker.Contains), "Retrying")
  583. split := strings.Split(out, "\n")
  584. c.Assert(split[len(split)-2], checker.Contains, "error parsing HTTP 403 response body: ")
  585. }
  586. func (s *DockerRegistryAuthTokenSuite) TestPushMisconfiguredTokenServiceResponseNoToken(c *check.C) {
  587. ts := getTestTokenService(http.StatusOK, `{"something": "wrong"}`, 0)
  588. defer ts.Close()
  589. s.setupRegistryWithTokenService(c, ts.URL)
  590. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  591. dockerCmd(c, "tag", "busybox", repoName)
  592. out, _, err := dockerCmdWithError("push", repoName)
  593. c.Assert(err, check.NotNil, check.Commentf(out))
  594. c.Assert(out, checker.Not(checker.Contains), "Retrying")
  595. split := strings.Split(out, "\n")
  596. c.Assert(split[len(split)-2], check.Equals, "authorization server did not include a token in the response")
  597. }