docker_cli_push_test.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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/checker"
  17. "github.com/go-check/check"
  18. )
  19. // Pushing an image to a private registry.
  20. func testPushBusyboxImage(c *check.C) {
  21. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  22. // tag the image to upload it to the private registry
  23. dockerCmd(c, "tag", "busybox", repoName)
  24. // push the image to the registry
  25. dockerCmd(c, "push", repoName)
  26. }
  27. func (s *DockerRegistrySuite) TestPushBusyboxImage(c *check.C) {
  28. testPushBusyboxImage(c)
  29. }
  30. func (s *DockerSchema1RegistrySuite) TestPushBusyboxImage(c *check.C) {
  31. testPushBusyboxImage(c)
  32. }
  33. // pushing an image without a prefix should throw an error
  34. func (s *DockerSuite) TestPushUnprefixedRepo(c *check.C) {
  35. out, _, err := dockerCmdWithError("push", "busybox")
  36. c.Assert(err, check.NotNil, check.Commentf("pushing an unprefixed repo didn't result in a non-zero exit status: %s", out))
  37. }
  38. func testPushUntagged(c *check.C) {
  39. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  40. expected := "An image does not exist locally with the tag"
  41. out, _, err := dockerCmdWithError("push", repoName)
  42. c.Assert(err, check.NotNil, check.Commentf("pushing the image to the private registry should have failed: output %q", out))
  43. c.Assert(out, checker.Contains, expected, check.Commentf("pushing the image failed"))
  44. }
  45. func (s *DockerRegistrySuite) TestPushUntagged(c *check.C) {
  46. testPushUntagged(c)
  47. }
  48. func (s *DockerSchema1RegistrySuite) TestPushUntagged(c *check.C) {
  49. testPushUntagged(c)
  50. }
  51. func testPushBadTag(c *check.C) {
  52. repoName := fmt.Sprintf("%v/dockercli/busybox:latest", privateRegistryURL)
  53. expected := "does not exist"
  54. out, _, err := dockerCmdWithError("push", repoName)
  55. c.Assert(err, check.NotNil, check.Commentf("pushing the image to the private registry should have failed: output %q", out))
  56. c.Assert(out, checker.Contains, expected, check.Commentf("pushing the image failed"))
  57. }
  58. func (s *DockerRegistrySuite) TestPushBadTag(c *check.C) {
  59. testPushBadTag(c)
  60. }
  61. func (s *DockerSchema1RegistrySuite) TestPushBadTag(c *check.C) {
  62. testPushBadTag(c)
  63. }
  64. func testPushMultipleTags(c *check.C) {
  65. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  66. repoTag1 := fmt.Sprintf("%v/dockercli/busybox:t1", privateRegistryURL)
  67. repoTag2 := fmt.Sprintf("%v/dockercli/busybox:t2", privateRegistryURL)
  68. // tag the image and upload it to the private registry
  69. dockerCmd(c, "tag", "busybox", repoTag1)
  70. dockerCmd(c, "tag", "busybox", repoTag2)
  71. dockerCmd(c, "push", repoName)
  72. // Ensure layer list is equivalent for repoTag1 and repoTag2
  73. out1, _ := dockerCmd(c, "pull", repoTag1)
  74. imageAlreadyExists := ": Image already exists"
  75. var out1Lines []string
  76. for _, outputLine := range strings.Split(out1, "\n") {
  77. if strings.Contains(outputLine, imageAlreadyExists) {
  78. out1Lines = append(out1Lines, outputLine)
  79. }
  80. }
  81. out2, _ := dockerCmd(c, "pull", repoTag2)
  82. var out2Lines []string
  83. for _, outputLine := range strings.Split(out2, "\n") {
  84. if strings.Contains(outputLine, imageAlreadyExists) {
  85. out1Lines = append(out1Lines, outputLine)
  86. }
  87. }
  88. c.Assert(out2Lines, checker.HasLen, len(out1Lines))
  89. for i := range out1Lines {
  90. c.Assert(out1Lines[i], checker.Equals, out2Lines[i])
  91. }
  92. }
  93. func (s *DockerRegistrySuite) TestPushMultipleTags(c *check.C) {
  94. testPushMultipleTags(c)
  95. }
  96. func (s *DockerSchema1RegistrySuite) TestPushMultipleTags(c *check.C) {
  97. testPushMultipleTags(c)
  98. }
  99. func testPushEmptyLayer(c *check.C) {
  100. repoName := fmt.Sprintf("%v/dockercli/emptylayer", privateRegistryURL)
  101. emptyTarball, err := ioutil.TempFile("", "empty_tarball")
  102. c.Assert(err, check.IsNil, check.Commentf("Unable to create test file"))
  103. tw := tar.NewWriter(emptyTarball)
  104. err = tw.Close()
  105. c.Assert(err, check.IsNil, check.Commentf("Error creating empty tarball"))
  106. freader, err := os.Open(emptyTarball.Name())
  107. c.Assert(err, check.IsNil, check.Commentf("Could not open test tarball"))
  108. defer freader.Close()
  109. importCmd := exec.Command(dockerBinary, "import", "-", repoName)
  110. importCmd.Stdin = freader
  111. out, _, err := runCommandWithOutput(importCmd)
  112. c.Assert(err, check.IsNil, check.Commentf("import failed: %q", out))
  113. // Now verify we can push it
  114. out, _, err = dockerCmdWithError("push", repoName)
  115. c.Assert(err, check.IsNil, check.Commentf("pushing the image to the private registry has failed: %s", out))
  116. }
  117. func (s *DockerRegistrySuite) TestPushEmptyLayer(c *check.C) {
  118. testPushEmptyLayer(c)
  119. }
  120. func (s *DockerSchema1RegistrySuite) TestPushEmptyLayer(c *check.C) {
  121. testPushEmptyLayer(c)
  122. }
  123. // testConcurrentPush pushes multiple tags to the same repo
  124. // concurrently.
  125. func testConcurrentPush(c *check.C) {
  126. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  127. repos := []string{}
  128. for _, tag := range []string{"push1", "push2", "push3"} {
  129. repo := fmt.Sprintf("%v:%v", repoName, tag)
  130. _, err := buildImage(repo, fmt.Sprintf(`
  131. FROM busybox
  132. ENTRYPOINT ["/bin/echo"]
  133. ENV FOO foo
  134. ENV BAR bar
  135. CMD echo %s
  136. `, repo), true)
  137. c.Assert(err, checker.IsNil)
  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. _, _, err := runCommandWithOutput(exec.Command(dockerBinary, "push", repo))
  145. results <- err
  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. pushCmd := exec.Command(dockerBinary, "push", repoName)
  236. s.trustedCmd(pushCmd)
  237. out, _, err := runCommandWithOutput(pushCmd)
  238. c.Assert(err, check.IsNil, check.Commentf("Error running trusted push: %s\n%s", err, out))
  239. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  240. // Try pull after push
  241. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  242. s.trustedCmd(pullCmd)
  243. out, _, err = runCommandWithOutput(pullCmd)
  244. c.Assert(err, check.IsNil, check.Commentf(out))
  245. c.Assert(string(out), checker.Contains, "Status: Image is up to date", check.Commentf(out))
  246. // Assert that we rotated the snapshot key to the server by checking our local keystore
  247. contents, err := ioutil.ReadDir(filepath.Join(cliconfig.Dir(), "trust/private/tuf_keys", privateRegistryURL, "dockerclitrusted/pushtest"))
  248. c.Assert(err, check.IsNil, check.Commentf("Unable to read local tuf key files"))
  249. // Check that we only have 1 key (targets key)
  250. c.Assert(contents, checker.HasLen, 1)
  251. }
  252. func (s *DockerTrustSuite) TestTrustedPushWithEnvPasswords(c *check.C) {
  253. repoName := fmt.Sprintf("%v/dockerclienv/trusted:latest", privateRegistryURL)
  254. // tag the image and upload it to the private registry
  255. dockerCmd(c, "tag", "busybox", repoName)
  256. pushCmd := exec.Command(dockerBinary, "push", repoName)
  257. s.trustedCmdWithPassphrases(pushCmd, "12345678", "12345678")
  258. out, _, err := runCommandWithOutput(pushCmd)
  259. c.Assert(err, check.IsNil, check.Commentf("Error running trusted push: %s\n%s", err, out))
  260. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  261. // Try pull after push
  262. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  263. s.trustedCmd(pullCmd)
  264. out, _, err = runCommandWithOutput(pullCmd)
  265. c.Assert(err, check.IsNil, check.Commentf(out))
  266. c.Assert(string(out), checker.Contains, "Status: Image is up to date", check.Commentf(out))
  267. }
  268. func (s *DockerTrustSuite) TestTrustedPushWithFailingServer(c *check.C) {
  269. repoName := fmt.Sprintf("%v/dockerclitrusted/failingserver:latest", privateRegistryURL)
  270. // tag the image and upload it to the private registry
  271. dockerCmd(c, "tag", "busybox", repoName)
  272. pushCmd := exec.Command(dockerBinary, "push", repoName)
  273. // Using a name that doesn't resolve to an address makes this test faster
  274. s.trustedCmdWithServer(pushCmd, "https://server.invalid:81/")
  275. out, _, err := runCommandWithOutput(pushCmd)
  276. c.Assert(err, check.NotNil, check.Commentf("Missing error while running trusted push w/ no server"))
  277. c.Assert(out, checker.Contains, "error contacting notary server", check.Commentf("Missing expected output on trusted push"))
  278. }
  279. func (s *DockerTrustSuite) TestTrustedPushWithoutServerAndUntrusted(c *check.C) {
  280. repoName := fmt.Sprintf("%v/dockerclitrusted/trustedandnot:latest", privateRegistryURL)
  281. // tag the image and upload it to the private registry
  282. dockerCmd(c, "tag", "busybox", repoName)
  283. pushCmd := exec.Command(dockerBinary, "push", "--disable-content-trust", repoName)
  284. // Using a name that doesn't resolve to an address makes this test faster
  285. s.trustedCmdWithServer(pushCmd, "https://server.invalid")
  286. out, _, err := runCommandWithOutput(pushCmd)
  287. c.Assert(err, check.IsNil, check.Commentf("trusted push with no server and --disable-content-trust failed: %s\n%s", err, out))
  288. 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:"))
  289. }
  290. func (s *DockerTrustSuite) TestTrustedPushWithExistingTag(c *check.C) {
  291. repoName := fmt.Sprintf("%v/dockerclitag/trusted:latest", privateRegistryURL)
  292. // tag the image and upload it to the private registry
  293. dockerCmd(c, "tag", "busybox", repoName)
  294. dockerCmd(c, "push", repoName)
  295. pushCmd := exec.Command(dockerBinary, "push", repoName)
  296. s.trustedCmd(pushCmd)
  297. out, _, err := runCommandWithOutput(pushCmd)
  298. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  299. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  300. // Try pull after push
  301. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  302. s.trustedCmd(pullCmd)
  303. out, _, err = runCommandWithOutput(pullCmd)
  304. c.Assert(err, check.IsNil, check.Commentf(out))
  305. c.Assert(string(out), checker.Contains, "Status: Image is up to date", check.Commentf(out))
  306. }
  307. func (s *DockerTrustSuite) TestTrustedPushWithExistingSignedTag(c *check.C) {
  308. repoName := fmt.Sprintf("%v/dockerclipushpush/trusted:latest", privateRegistryURL)
  309. // tag the image and upload it to the private registry
  310. dockerCmd(c, "tag", "busybox", repoName)
  311. // Do a trusted push
  312. pushCmd := exec.Command(dockerBinary, "push", repoName)
  313. s.trustedCmd(pushCmd)
  314. out, _, err := runCommandWithOutput(pushCmd)
  315. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  316. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  317. // Do another trusted push
  318. pushCmd = exec.Command(dockerBinary, "push", repoName)
  319. s.trustedCmd(pushCmd)
  320. out, _, err = runCommandWithOutput(pushCmd)
  321. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  322. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  323. dockerCmd(c, "rmi", repoName)
  324. // Try pull to ensure the double push did not break our ability to pull
  325. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  326. s.trustedCmd(pullCmd)
  327. out, _, err = runCommandWithOutput(pullCmd)
  328. c.Assert(err, check.IsNil, check.Commentf("Error running trusted pull: %s\n%s", err, out))
  329. c.Assert(out, checker.Contains, "Status: Downloaded", check.Commentf("Missing expected output on trusted pull with --disable-content-trust"))
  330. }
  331. func (s *DockerTrustSuite) TestTrustedPushWithIncorrectPassphraseForNonRoot(c *check.C) {
  332. repoName := fmt.Sprintf("%v/dockercliincorretpwd/trusted:latest", privateRegistryURL)
  333. // tag the image and upload it to the private registry
  334. dockerCmd(c, "tag", "busybox", repoName)
  335. // Push with default passphrases
  336. pushCmd := exec.Command(dockerBinary, "push", repoName)
  337. s.trustedCmd(pushCmd)
  338. out, _, err := runCommandWithOutput(pushCmd)
  339. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  340. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push:\n%s", out))
  341. // Push with wrong passphrases
  342. pushCmd = exec.Command(dockerBinary, "push", repoName)
  343. s.trustedCmdWithPassphrases(pushCmd, "12345678", "87654321")
  344. out, _, err = runCommandWithOutput(pushCmd)
  345. c.Assert(err, check.NotNil, check.Commentf("Error missing from trusted push with short targets passphrase: \n%s", out))
  346. c.Assert(out, checker.Contains, "could not find necessary signing keys", check.Commentf("Missing expected output on trusted push with short targets/snapsnot passphrase"))
  347. }
  348. func (s *DockerTrustSuite) TestTrustedPushWithExpiredSnapshot(c *check.C) {
  349. c.Skip("Currently changes system time, causing instability")
  350. repoName := fmt.Sprintf("%v/dockercliexpiredsnapshot/trusted:latest", privateRegistryURL)
  351. // tag the image and upload it to the private registry
  352. dockerCmd(c, "tag", "busybox", repoName)
  353. // Push with default passphrases
  354. pushCmd := exec.Command(dockerBinary, "push", repoName)
  355. s.trustedCmd(pushCmd)
  356. out, _, err := runCommandWithOutput(pushCmd)
  357. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  358. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  359. // Snapshots last for three years. This should be expired
  360. fourYearsLater := time.Now().Add(time.Hour * 24 * 365 * 4)
  361. runAtDifferentDate(fourYearsLater, func() {
  362. // Push with wrong passphrases
  363. pushCmd = exec.Command(dockerBinary, "push", repoName)
  364. s.trustedCmd(pushCmd)
  365. out, _, err = runCommandWithOutput(pushCmd)
  366. c.Assert(err, check.NotNil, check.Commentf("Error missing from trusted push with expired snapshot: \n%s", out))
  367. c.Assert(out, checker.Contains, "repository out-of-date", check.Commentf("Missing expected output on trusted push with expired snapshot"))
  368. })
  369. }
  370. func (s *DockerTrustSuite) TestTrustedPushWithExpiredTimestamp(c *check.C) {
  371. c.Skip("Currently changes system time, causing instability")
  372. repoName := fmt.Sprintf("%v/dockercliexpiredtimestamppush/trusted:latest", privateRegistryURL)
  373. // tag the image and upload it to the private registry
  374. dockerCmd(c, "tag", "busybox", repoName)
  375. // Push with default passphrases
  376. pushCmd := exec.Command(dockerBinary, "push", repoName)
  377. s.trustedCmd(pushCmd)
  378. out, _, err := runCommandWithOutput(pushCmd)
  379. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  380. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  381. // The timestamps expire in two weeks. Lets check three
  382. threeWeeksLater := time.Now().Add(time.Hour * 24 * 21)
  383. // Should succeed because the server transparently re-signs one
  384. runAtDifferentDate(threeWeeksLater, func() {
  385. pushCmd := exec.Command(dockerBinary, "push", repoName)
  386. s.trustedCmd(pushCmd)
  387. out, _, err := runCommandWithOutput(pushCmd)
  388. c.Assert(err, check.IsNil, check.Commentf("Error running trusted push: %s\n%s", err, out))
  389. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with expired timestamp"))
  390. })
  391. }
  392. func (s *DockerTrustSuite) TestTrustedPushWithReleasesDelegationOnly(c *check.C) {
  393. testRequires(c, NotaryHosting)
  394. repoName := fmt.Sprintf("%v/dockerclireleasedelegationinitfirst/trusted", privateRegistryURL)
  395. targetName := fmt.Sprintf("%s:latest", repoName)
  396. s.notaryInitRepo(c, repoName)
  397. s.notaryCreateDelegation(c, repoName, "targets/releases", s.not.keys[0].Public)
  398. s.notaryPublish(c, repoName)
  399. s.notaryImportKey(c, repoName, "targets/releases", s.not.keys[0].Private)
  400. // tag the image and upload it to the private registry
  401. dockerCmd(c, "tag", "busybox", targetName)
  402. pushCmd := exec.Command(dockerBinary, "push", targetName)
  403. s.trustedCmd(pushCmd)
  404. out, _, err := runCommandWithOutput(pushCmd)
  405. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  406. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  407. // check to make sure that the target has been added to targets/releases and not targets
  408. s.assertTargetInRoles(c, repoName, "latest", "targets/releases")
  409. s.assertTargetNotInRoles(c, repoName, "latest", "targets")
  410. // Try pull after push
  411. os.RemoveAll(filepath.Join(cliconfig.Dir(), "trust"))
  412. pullCmd := exec.Command(dockerBinary, "pull", targetName)
  413. s.trustedCmd(pullCmd)
  414. out, _, err = runCommandWithOutput(pullCmd)
  415. c.Assert(err, check.IsNil, check.Commentf(out))
  416. c.Assert(string(out), checker.Contains, "Status: Image is up to date", check.Commentf(out))
  417. }
  418. func (s *DockerTrustSuite) TestTrustedPushSignsAllFirstLevelRolesWeHaveKeysFor(c *check.C) {
  419. testRequires(c, NotaryHosting)
  420. repoName := fmt.Sprintf("%v/dockerclimanyroles/trusted", privateRegistryURL)
  421. targetName := fmt.Sprintf("%s:latest", repoName)
  422. s.notaryInitRepo(c, repoName)
  423. s.notaryCreateDelegation(c, repoName, "targets/role1", s.not.keys[0].Public)
  424. s.notaryCreateDelegation(c, repoName, "targets/role2", s.not.keys[1].Public)
  425. s.notaryCreateDelegation(c, repoName, "targets/role3", s.not.keys[2].Public)
  426. // import everything except the third key
  427. s.notaryImportKey(c, repoName, "targets/role1", s.not.keys[0].Private)
  428. s.notaryImportKey(c, repoName, "targets/role2", s.not.keys[1].Private)
  429. s.notaryCreateDelegation(c, repoName, "targets/role1/subrole", s.not.keys[3].Public)
  430. s.notaryImportKey(c, repoName, "targets/role1/subrole", s.not.keys[3].Private)
  431. s.notaryPublish(c, repoName)
  432. // tag the image and upload it to the private registry
  433. dockerCmd(c, "tag", "busybox", targetName)
  434. pushCmd := exec.Command(dockerBinary, "push", targetName)
  435. s.trustedCmd(pushCmd)
  436. out, _, err := runCommandWithOutput(pushCmd)
  437. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  438. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  439. // check to make sure that the target has been added to targets/role1 and targets/role2, and
  440. // not targets (because there are delegations) or targets/role3 (due to missing key) or
  441. // targets/role1/subrole (due to it being a second level delegation)
  442. s.assertTargetInRoles(c, repoName, "latest", "targets/role1", "targets/role2")
  443. s.assertTargetNotInRoles(c, repoName, "latest", "targets")
  444. // Try pull after push
  445. os.RemoveAll(filepath.Join(cliconfig.Dir(), "trust"))
  446. // pull should fail because none of these are the releases role
  447. pullCmd := exec.Command(dockerBinary, "pull", targetName)
  448. s.trustedCmd(pullCmd)
  449. out, _, err = runCommandWithOutput(pullCmd)
  450. c.Assert(err, check.NotNil, check.Commentf(out))
  451. }
  452. func (s *DockerTrustSuite) TestTrustedPushSignsForRolesWithKeysAndValidPaths(c *check.C) {
  453. repoName := fmt.Sprintf("%v/dockerclirolesbykeysandpaths/trusted", privateRegistryURL)
  454. targetName := fmt.Sprintf("%s:latest", repoName)
  455. s.notaryInitRepo(c, repoName)
  456. s.notaryCreateDelegation(c, repoName, "targets/role1", s.not.keys[0].Public, "l", "z")
  457. s.notaryCreateDelegation(c, repoName, "targets/role2", s.not.keys[1].Public, "x", "y")
  458. s.notaryCreateDelegation(c, repoName, "targets/role3", s.not.keys[2].Public, "latest")
  459. s.notaryCreateDelegation(c, repoName, "targets/role4", s.not.keys[3].Public, "latest")
  460. // import everything except the third key
  461. s.notaryImportKey(c, repoName, "targets/role1", s.not.keys[0].Private)
  462. s.notaryImportKey(c, repoName, "targets/role2", s.not.keys[1].Private)
  463. s.notaryImportKey(c, repoName, "targets/role4", s.not.keys[3].Private)
  464. s.notaryPublish(c, repoName)
  465. // tag the image and upload it to the private registry
  466. dockerCmd(c, "tag", "busybox", targetName)
  467. pushCmd := exec.Command(dockerBinary, "push", targetName)
  468. s.trustedCmd(pushCmd)
  469. out, _, err := runCommandWithOutput(pushCmd)
  470. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  471. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  472. // check to make sure that the target has been added to targets/role1 and targets/role4, and
  473. // not targets (because there are delegations) or targets/role2 (due to path restrictions) or
  474. // targets/role3 (due to missing key)
  475. s.assertTargetInRoles(c, repoName, "latest", "targets/role1", "targets/role4")
  476. s.assertTargetNotInRoles(c, repoName, "latest", "targets")
  477. // Try pull after push
  478. os.RemoveAll(filepath.Join(cliconfig.Dir(), "trust"))
  479. // pull should fail because none of these are the releases role
  480. pullCmd := exec.Command(dockerBinary, "pull", targetName)
  481. s.trustedCmd(pullCmd)
  482. out, _, err = runCommandWithOutput(pullCmd)
  483. c.Assert(err, check.NotNil, check.Commentf(out))
  484. }
  485. func (s *DockerTrustSuite) TestTrustedPushDoesntSignTargetsIfDelegationsExist(c *check.C) {
  486. testRequires(c, NotaryHosting)
  487. repoName := fmt.Sprintf("%v/dockerclireleasedelegationnotsignable/trusted", privateRegistryURL)
  488. targetName := fmt.Sprintf("%s:latest", repoName)
  489. s.notaryInitRepo(c, repoName)
  490. s.notaryCreateDelegation(c, repoName, "targets/role1", s.not.keys[0].Public)
  491. s.notaryPublish(c, repoName)
  492. // do not import any delegations key
  493. // tag the image and upload it to the private registry
  494. dockerCmd(c, "tag", "busybox", targetName)
  495. pushCmd := exec.Command(dockerBinary, "push", targetName)
  496. s.trustedCmd(pushCmd)
  497. out, _, err := runCommandWithOutput(pushCmd)
  498. c.Assert(err, check.NotNil, check.Commentf("trusted push succeeded but should have failed:\n%s", out))
  499. c.Assert(out, checker.Contains, "no valid signing keys",
  500. check.Commentf("Missing expected output on trusted push without keys"))
  501. s.assertTargetNotInRoles(c, repoName, "latest", "targets", "targets/role1")
  502. }
  503. func (s *DockerRegistryAuthHtpasswdSuite) TestPushNoCredentialsNoRetry(c *check.C) {
  504. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  505. dockerCmd(c, "tag", "busybox", repoName)
  506. out, _, err := dockerCmdWithError("push", repoName)
  507. c.Assert(err, check.NotNil, check.Commentf(out))
  508. c.Assert(out, check.Not(checker.Contains), "Retrying")
  509. c.Assert(out, checker.Contains, "no basic auth credentials")
  510. }
  511. // This may be flaky but it's needed not to regress on unauthorized push, see #21054
  512. func (s *DockerSuite) TestPushToCentralRegistryUnauthorized(c *check.C) {
  513. testRequires(c, Network)
  514. repoName := "test/busybox"
  515. dockerCmd(c, "tag", "busybox", repoName)
  516. out, _, err := dockerCmdWithError("push", repoName)
  517. c.Assert(err, check.NotNil, check.Commentf(out))
  518. c.Assert(out, check.Not(checker.Contains), "Retrying")
  519. }
  520. func getTestTokenService(status int, body string, retries int) *httptest.Server {
  521. var mu sync.Mutex
  522. return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  523. mu.Lock()
  524. if retries > 0 {
  525. w.WriteHeader(http.StatusServiceUnavailable)
  526. w.Header().Set("Content-Type", "application/json")
  527. w.Write([]byte(`{"errors":[{"code":"UNAVAILABLE","message":"cannot create token at this time"}]}`))
  528. retries--
  529. } else {
  530. w.WriteHeader(status)
  531. w.Header().Set("Content-Type", "application/json")
  532. w.Write([]byte(body))
  533. }
  534. mu.Unlock()
  535. }))
  536. }
  537. func (s *DockerRegistryAuthTokenSuite) TestPushTokenServiceUnauthResponse(c *check.C) {
  538. ts := getTestTokenService(http.StatusUnauthorized, `{"errors": [{"Code":"UNAUTHORIZED", "message": "a message", "detail": null}]}`, 0)
  539. defer ts.Close()
  540. s.setupRegistryWithTokenService(c, ts.URL)
  541. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  542. dockerCmd(c, "tag", "busybox", repoName)
  543. out, _, err := dockerCmdWithError("push", repoName)
  544. c.Assert(err, check.NotNil, check.Commentf(out))
  545. c.Assert(out, checker.Not(checker.Contains), "Retrying")
  546. c.Assert(out, checker.Contains, "unauthorized: a message")
  547. }
  548. func (s *DockerRegistryAuthTokenSuite) TestPushMisconfiguredTokenServiceResponseUnauthorized(c *check.C) {
  549. ts := getTestTokenService(http.StatusUnauthorized, `{"error": "unauthorized"}`, 0)
  550. defer ts.Close()
  551. s.setupRegistryWithTokenService(c, ts.URL)
  552. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  553. dockerCmd(c, "tag", "busybox", repoName)
  554. out, _, err := dockerCmdWithError("push", repoName)
  555. c.Assert(err, check.NotNil, check.Commentf(out))
  556. c.Assert(out, checker.Not(checker.Contains), "Retrying")
  557. split := strings.Split(out, "\n")
  558. c.Assert(split[len(split)-2], check.Equals, "unauthorized: authentication required")
  559. }
  560. func (s *DockerRegistryAuthTokenSuite) TestPushMisconfiguredTokenServiceResponseError(c *check.C) {
  561. ts := getTestTokenService(http.StatusTooManyRequests, `{"errors": [{"code":"TOOMANYREQUESTS","message":"out of tokens"}]}`, 4)
  562. defer ts.Close()
  563. s.setupRegistryWithTokenService(c, ts.URL)
  564. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  565. dockerCmd(c, "tag", "busybox", repoName)
  566. out, _, err := dockerCmdWithError("push", repoName)
  567. c.Assert(err, check.NotNil, check.Commentf(out))
  568. c.Assert(out, checker.Contains, "Retrying")
  569. c.Assert(out, checker.Not(checker.Contains), "Retrying in 15")
  570. split := strings.Split(out, "\n")
  571. c.Assert(split[len(split)-2], check.Equals, "toomanyrequests: out of tokens")
  572. }
  573. func (s *DockerRegistryAuthTokenSuite) TestPushMisconfiguredTokenServiceResponseUnparsable(c *check.C) {
  574. ts := getTestTokenService(http.StatusForbidden, `no way`, 0)
  575. defer ts.Close()
  576. s.setupRegistryWithTokenService(c, ts.URL)
  577. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  578. dockerCmd(c, "tag", "busybox", repoName)
  579. out, _, err := dockerCmdWithError("push", repoName)
  580. c.Assert(err, check.NotNil, check.Commentf(out))
  581. c.Assert(out, checker.Not(checker.Contains), "Retrying")
  582. split := strings.Split(out, "\n")
  583. c.Assert(split[len(split)-2], checker.Contains, "error parsing HTTP 403 response body: ")
  584. }
  585. func (s *DockerRegistryAuthTokenSuite) TestPushMisconfiguredTokenServiceResponseNoToken(c *check.C) {
  586. ts := getTestTokenService(http.StatusOK, `{"something": "wrong"}`, 0)
  587. defer ts.Close()
  588. s.setupRegistryWithTokenService(c, ts.URL)
  589. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  590. dockerCmd(c, "tag", "busybox", repoName)
  591. out, _, err := dockerCmdWithError("push", repoName)
  592. c.Assert(err, check.NotNil, check.Commentf(out))
  593. c.Assert(out, checker.Not(checker.Contains), "Retrying")
  594. split := strings.Split(out, "\n")
  595. c.Assert(split[len(split)-2], check.Equals, "authorization server did not include a token in the response")
  596. }