docker_cli_push_test.go 31 KB

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