docker_cli_push_test.go 30 KB

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