docker_cli_push_test.go 30 KB

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