docker_cli_pull_trusted_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os/exec"
  6. "strings"
  7. "time"
  8. "github.com/docker/docker/pkg/integration/checker"
  9. "github.com/go-check/check"
  10. )
  11. func (s *DockerTrustSuite) TestTrustedPull(c *check.C) {
  12. repoName := s.setupTrustedImage(c, "trusted-pull")
  13. // Try pull
  14. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  15. s.trustedCmd(pullCmd)
  16. out, _, err := runCommandWithOutput(pullCmd)
  17. c.Assert(err, check.IsNil, check.Commentf(out))
  18. c.Assert(string(out), checker.Contains, "Tagging", check.Commentf(out))
  19. dockerCmd(c, "rmi", repoName)
  20. // Try untrusted pull to ensure we pushed the tag to the registry
  21. pullCmd = exec.Command(dockerBinary, "pull", "--disable-content-trust=true", repoName)
  22. s.trustedCmd(pullCmd)
  23. out, _, err = runCommandWithOutput(pullCmd)
  24. c.Assert(err, check.IsNil, check.Commentf(out))
  25. c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf(out))
  26. }
  27. func (s *DockerTrustSuite) TestTrustedIsolatedPull(c *check.C) {
  28. repoName := s.setupTrustedImage(c, "trusted-isolated-pull")
  29. // Try pull (run from isolated directory without trust information)
  30. pullCmd := exec.Command(dockerBinary, "--config", "/tmp/docker-isolated", "pull", repoName)
  31. s.trustedCmd(pullCmd)
  32. out, _, err := runCommandWithOutput(pullCmd)
  33. c.Assert(err, check.IsNil, check.Commentf(out))
  34. c.Assert(string(out), checker.Contains, "Tagging", check.Commentf(string(out)))
  35. dockerCmd(c, "rmi", repoName)
  36. }
  37. func (s *DockerTrustSuite) TestUntrustedPull(c *check.C) {
  38. repoName := fmt.Sprintf("%v/dockercliuntrusted/pulltest:latest", privateRegistryURL)
  39. // tag the image and upload it to the private registry
  40. dockerCmd(c, "tag", "busybox", repoName)
  41. dockerCmd(c, "push", repoName)
  42. dockerCmd(c, "rmi", repoName)
  43. // Try trusted pull on untrusted tag
  44. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  45. s.trustedCmd(pullCmd)
  46. out, _, err := runCommandWithOutput(pullCmd)
  47. c.Assert(err, check.NotNil, check.Commentf(out))
  48. c.Assert(string(out), checker.Contains, "Error: remote trust data does not exist", check.Commentf(out))
  49. }
  50. func (s *DockerTrustSuite) TestPullWhenCertExpired(c *check.C) {
  51. c.Skip("Currently changes system time, causing instability")
  52. repoName := s.setupTrustedImage(c, "trusted-cert-expired")
  53. // Certificates have 10 years of expiration
  54. elevenYearsFromNow := time.Now().Add(time.Hour * 24 * 365 * 11)
  55. runAtDifferentDate(elevenYearsFromNow, func() {
  56. // Try pull
  57. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  58. s.trustedCmd(pullCmd)
  59. out, _, err := runCommandWithOutput(pullCmd)
  60. c.Assert(err, check.NotNil, check.Commentf(out))
  61. c.Assert(string(out), checker.Contains, "could not validate the path to a trusted root", check.Commentf(out))
  62. })
  63. runAtDifferentDate(elevenYearsFromNow, func() {
  64. // Try pull
  65. pullCmd := exec.Command(dockerBinary, "pull", "--disable-content-trust", repoName)
  66. s.trustedCmd(pullCmd)
  67. out, _, err := runCommandWithOutput(pullCmd)
  68. c.Assert(err, check.IsNil, check.Commentf(out))
  69. c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf(out))
  70. })
  71. }
  72. func (s *DockerTrustSuite) TestTrustedPullFromBadTrustServer(c *check.C) {
  73. repoName := fmt.Sprintf("%v/dockerclievilpull/trusted:latest", privateRegistryURL)
  74. evilLocalConfigDir, err := ioutil.TempDir("", "evil-local-config-dir")
  75. if err != nil {
  76. c.Fatalf("Failed to create local temp dir")
  77. }
  78. // tag the image and upload it to the private registry
  79. dockerCmd(c, "tag", "busybox", repoName)
  80. pushCmd := exec.Command(dockerBinary, "push", repoName)
  81. s.trustedCmd(pushCmd)
  82. out, _, err := runCommandWithOutput(pushCmd)
  83. c.Assert(err, check.IsNil, check.Commentf(out))
  84. c.Assert(string(out), checker.Contains, "Signing and pushing trust metadata", check.Commentf(out))
  85. dockerCmd(c, "rmi", repoName)
  86. // Try pull
  87. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  88. s.trustedCmd(pullCmd)
  89. out, _, err = runCommandWithOutput(pullCmd)
  90. c.Assert(err, check.IsNil, check.Commentf(out))
  91. c.Assert(string(out), checker.Contains, "Tagging", check.Commentf(out))
  92. dockerCmd(c, "rmi", repoName)
  93. // Kill the notary server, start a new "evil" one.
  94. s.not.Close()
  95. s.not, err = newTestNotary(c)
  96. c.Assert(err, check.IsNil, check.Commentf("Restarting notary server failed."))
  97. // In order to make an evil server, lets re-init a client (with a different trust dir) and push new data.
  98. // tag an image and upload it to the private registry
  99. dockerCmd(c, "--config", evilLocalConfigDir, "tag", "busybox", repoName)
  100. // Push up to the new server
  101. pushCmd = exec.Command(dockerBinary, "--config", evilLocalConfigDir, "push", repoName)
  102. s.trustedCmd(pushCmd)
  103. out, _, err = runCommandWithOutput(pushCmd)
  104. c.Assert(err, check.IsNil, check.Commentf(out))
  105. c.Assert(string(out), checker.Contains, "Signing and pushing trust metadata", check.Commentf(out))
  106. // Now, try pulling with the original client from this new trust server. This should fall back to cached metadata.
  107. pullCmd = exec.Command(dockerBinary, "pull", repoName)
  108. s.trustedCmd(pullCmd)
  109. out, _, err = runCommandWithOutput(pullCmd)
  110. if err != nil {
  111. c.Fatalf("Error falling back to cached trust data: %s\n%s", err, out)
  112. }
  113. if !strings.Contains(string(out), "Error while downloading remote metadata, using cached timestamp") {
  114. c.Fatalf("Missing expected output on trusted pull:\n%s", out)
  115. }
  116. }
  117. func (s *DockerTrustSuite) TestTrustedPullWithExpiredSnapshot(c *check.C) {
  118. c.Skip("Currently changes system time, causing instability")
  119. repoName := fmt.Sprintf("%v/dockercliexpiredtimestamppull/trusted:latest", privateRegistryURL)
  120. // tag the image and upload it to the private registry
  121. dockerCmd(c, "tag", "busybox", repoName)
  122. // Push with default passphrases
  123. pushCmd := exec.Command(dockerBinary, "push", repoName)
  124. s.trustedCmd(pushCmd)
  125. out, _, err := runCommandWithOutput(pushCmd)
  126. c.Assert(err, check.IsNil, check.Commentf(out))
  127. c.Assert(string(out), checker.Contains, "Signing and pushing trust metadata", check.Commentf(out))
  128. dockerCmd(c, "rmi", repoName)
  129. // Snapshots last for three years. This should be expired
  130. fourYearsLater := time.Now().Add(time.Hour * 24 * 365 * 4)
  131. runAtDifferentDate(fourYearsLater, func() {
  132. // Try pull
  133. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  134. s.trustedCmd(pullCmd)
  135. out, _, err = runCommandWithOutput(pullCmd)
  136. c.Assert(err, check.NotNil, check.Commentf("Missing expected error running trusted pull with expired snapshots"))
  137. c.Assert(string(out), checker.Contains, "repository out-of-date", check.Commentf(out))
  138. })
  139. }
  140. func (s *DockerTrustSuite) TestTrustedOfflinePull(c *check.C) {
  141. repoName := s.setupTrustedImage(c, "trusted-offline-pull")
  142. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  143. s.trustedCmdWithServer(pullCmd, "https://invalidnotaryserver")
  144. out, _, err := runCommandWithOutput(pullCmd)
  145. c.Assert(err, check.NotNil, check.Commentf(out))
  146. c.Assert(string(out), checker.Contains, "error contacting notary server", check.Commentf(out))
  147. // Do valid trusted pull to warm cache
  148. pullCmd = exec.Command(dockerBinary, "pull", repoName)
  149. s.trustedCmd(pullCmd)
  150. out, _, err = runCommandWithOutput(pullCmd)
  151. c.Assert(err, check.IsNil, check.Commentf(out))
  152. c.Assert(string(out), checker.Contains, "Tagging", check.Commentf(out))
  153. dockerCmd(c, "rmi", repoName)
  154. // Try pull again with invalid notary server, should use cache
  155. pullCmd = exec.Command(dockerBinary, "pull", repoName)
  156. s.trustedCmdWithServer(pullCmd, "https://invalidnotaryserver")
  157. out, _, err = runCommandWithOutput(pullCmd)
  158. c.Assert(err, check.IsNil, check.Commentf(out))
  159. c.Assert(string(out), checker.Contains, "Tagging", check.Commentf(out))
  160. }
  161. func (s *DockerTrustSuite) TestTrustedPullDelete(c *check.C) {
  162. repoName := fmt.Sprintf("%v/dockercli/%s:latest", privateRegistryURL, "trusted-pull-delete")
  163. // tag the image and upload it to the private registry
  164. _, err := buildImage(repoName, `
  165. FROM busybox
  166. CMD echo trustedpulldelete
  167. `, true)
  168. pushCmd := exec.Command(dockerBinary, "push", repoName)
  169. s.trustedCmd(pushCmd)
  170. out, _, err := runCommandWithOutput(pushCmd)
  171. if err != nil {
  172. c.Fatalf("Error running trusted push: %s\n%s", err, out)
  173. }
  174. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  175. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  176. }
  177. if out, status := dockerCmd(c, "rmi", repoName); status != 0 {
  178. c.Fatalf("Error removing image %q\n%s", repoName, out)
  179. }
  180. // Try pull
  181. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  182. s.trustedCmd(pullCmd)
  183. out, _, err = runCommandWithOutput(pullCmd)
  184. c.Assert(err, check.IsNil, check.Commentf(out))
  185. matches := digestRegex.FindStringSubmatch(out)
  186. c.Assert(matches, checker.HasLen, 2, check.Commentf("unable to parse digest from pull output: %s", out))
  187. pullDigest := matches[1]
  188. imageID := inspectField(c, repoName, "Id")
  189. imageByDigest := repoName + "@" + pullDigest
  190. byDigestID := inspectField(c, imageByDigest, "Id")
  191. c.Assert(byDigestID, checker.Equals, imageID)
  192. // rmi of tag should also remove the digest reference
  193. dockerCmd(c, "rmi", repoName)
  194. _, err = inspectFieldWithError(imageByDigest, "Id")
  195. c.Assert(err, checker.NotNil, check.Commentf("digest reference should have been removed"))
  196. _, err = inspectFieldWithError(imageID, "Id")
  197. c.Assert(err, checker.NotNil, check.Commentf("image should have been deleted"))
  198. }
  199. func (s *DockerTrustSuite) TestTrustedPullReadsFromReleasesRole(c *check.C) {
  200. repoName := fmt.Sprintf("%v/dockerclireleasesdelegationpulling/trusted", privateRegistryURL)
  201. targetName := fmt.Sprintf("%s:latest", repoName)
  202. pwd := "12345678"
  203. // Push with targets first, initializing the repo
  204. dockerCmd(c, "tag", "busybox", targetName)
  205. pushCmd := exec.Command(dockerBinary, "push", targetName)
  206. s.trustedCmdWithPassphrases(pushCmd, pwd, pwd)
  207. out, _, err := runCommandWithOutput(pushCmd)
  208. c.Assert(err, check.IsNil, check.Commentf(out))
  209. // Try pull, check we retrieve from targets role
  210. pullCmd := exec.Command(dockerBinary, "-D", "pull", repoName)
  211. s.trustedCmd(pullCmd)
  212. out, _, err = runCommandWithOutput(pullCmd)
  213. c.Assert(err, check.IsNil, check.Commentf(out))
  214. c.Assert(out, checker.Contains, "retrieving target for targets role")
  215. // Now we'll create the releases role, and try pushing and pulling
  216. s.notaryCreateDelegation(c, repoName, pwd, "targets/releases", s.not.keys[0].Public)
  217. s.notaryImportKey(c, repoName, "targets/releases", s.not.keys[0].Private)
  218. s.notaryPublish(c, repoName, pwd)
  219. // Push, should sign with targets/releases
  220. dockerCmd(c, "tag", "busybox", targetName)
  221. pushCmd = exec.Command(dockerBinary, "push", targetName)
  222. s.trustedCmdWithPassphrases(pushCmd, pwd, pwd)
  223. out, _, err = runCommandWithOutput(pushCmd)
  224. // Try pull, check we retrieve from targets/releases role
  225. pullCmd = exec.Command(dockerBinary, "-D", "pull", repoName)
  226. s.trustedCmd(pullCmd)
  227. out, _, err = runCommandWithOutput(pullCmd)
  228. c.Assert(out, checker.Contains, "retrieving target for targets/releases role")
  229. // Create another delegation that we'll sign with
  230. s.notaryCreateDelegation(c, repoName, pwd, "targets/other", s.not.keys[1].Public)
  231. s.notaryImportKey(c, repoName, "targets/other", s.not.keys[1].Private)
  232. s.notaryPublish(c, repoName, pwd)
  233. dockerCmd(c, "tag", "busybox", targetName)
  234. pushCmd = exec.Command(dockerBinary, "push", targetName)
  235. s.trustedCmdWithPassphrases(pushCmd, pwd, pwd)
  236. out, _, err = runCommandWithOutput(pushCmd)
  237. // Try pull, check we retrieve from targets/releases role
  238. pullCmd = exec.Command(dockerBinary, "-D", "pull", repoName)
  239. s.trustedCmd(pullCmd)
  240. out, _, err = runCommandWithOutput(pullCmd)
  241. c.Assert(out, checker.Contains, "retrieving target for targets/releases role")
  242. }