docker_cli_push_test.go 30 KB

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