docker_cli_pull_trusted_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. testRequires(c, NotaryHosting)
  201. repoName := fmt.Sprintf("%v/dockerclireleasesdelegationpulling/trusted", privateRegistryURL)
  202. targetName := fmt.Sprintf("%s:latest", repoName)
  203. // Push with targets first, initializing the repo
  204. dockerCmd(c, "tag", "busybox", targetName)
  205. pushCmd := exec.Command(dockerBinary, "push", targetName)
  206. s.trustedCmd(pushCmd)
  207. out, _, err := runCommandWithOutput(pushCmd)
  208. c.Assert(err, check.IsNil, check.Commentf(out))
  209. s.assertTargetInRoles(c, repoName, "latest", "targets")
  210. // Try pull, check we retrieve from targets role
  211. pullCmd := exec.Command(dockerBinary, "-D", "pull", repoName)
  212. s.trustedCmd(pullCmd)
  213. out, _, err = runCommandWithOutput(pullCmd)
  214. c.Assert(err, check.IsNil, check.Commentf(out))
  215. c.Assert(out, checker.Contains, "retrieving target for targets role")
  216. // Now we'll create the releases role, and try pushing and pulling
  217. s.notaryCreateDelegation(c, repoName, "targets/releases", s.not.keys[0].Public)
  218. s.notaryImportKey(c, repoName, "targets/releases", s.not.keys[0].Private)
  219. s.notaryPublish(c, repoName)
  220. // try a pull, check that we can still pull because we can still read the
  221. // old tag in the targets role
  222. pullCmd = exec.Command(dockerBinary, "-D", "pull", repoName)
  223. s.trustedCmd(pullCmd)
  224. out, _, err = runCommandWithOutput(pullCmd)
  225. c.Assert(err, check.IsNil, check.Commentf(out))
  226. c.Assert(out, checker.Contains, "retrieving target for targets role")
  227. // try a pull -a, check that it succeeds because we can still pull from the
  228. // targets role
  229. pullCmd = exec.Command(dockerBinary, "-D", "pull", "-a", repoName)
  230. s.trustedCmd(pullCmd)
  231. out, _, err = runCommandWithOutput(pullCmd)
  232. c.Assert(err, check.IsNil, check.Commentf(out))
  233. // Push, should sign with targets/releases
  234. dockerCmd(c, "tag", "busybox", targetName)
  235. pushCmd = exec.Command(dockerBinary, "push", targetName)
  236. s.trustedCmd(pushCmd)
  237. out, _, err = runCommandWithOutput(pushCmd)
  238. s.assertTargetInRoles(c, repoName, "latest", "targets", "targets/releases")
  239. // Try pull, check we retrieve from targets/releases role
  240. pullCmd = exec.Command(dockerBinary, "-D", "pull", repoName)
  241. s.trustedCmd(pullCmd)
  242. out, _, err = runCommandWithOutput(pullCmd)
  243. c.Assert(out, checker.Contains, "retrieving target for targets/releases role")
  244. // Create another delegation that we'll sign with
  245. s.notaryCreateDelegation(c, repoName, "targets/other", s.not.keys[1].Public)
  246. s.notaryImportKey(c, repoName, "targets/other", s.not.keys[1].Private)
  247. s.notaryPublish(c, repoName)
  248. dockerCmd(c, "tag", "busybox", targetName)
  249. pushCmd = exec.Command(dockerBinary, "push", targetName)
  250. s.trustedCmd(pushCmd)
  251. out, _, err = runCommandWithOutput(pushCmd)
  252. s.assertTargetInRoles(c, repoName, "latest", "targets", "targets/releases", "targets/other")
  253. // Try pull, check we retrieve from targets/releases role
  254. pullCmd = exec.Command(dockerBinary, "-D", "pull", repoName)
  255. s.trustedCmd(pullCmd)
  256. out, _, err = runCommandWithOutput(pullCmd)
  257. c.Assert(out, checker.Contains, "retrieving target for targets/releases role")
  258. }
  259. func (s *DockerTrustSuite) TestTrustedPullIgnoresOtherDelegationRoles(c *check.C) {
  260. testRequires(c, NotaryHosting)
  261. repoName := fmt.Sprintf("%v/dockerclipullotherdelegation/trusted", privateRegistryURL)
  262. targetName := fmt.Sprintf("%s:latest", repoName)
  263. // We'll create a repo first with a non-release delegation role, so that when we
  264. // push we'll sign it into the delegation role
  265. s.notaryInitRepo(c, repoName)
  266. s.notaryCreateDelegation(c, repoName, "targets/other", s.not.keys[0].Public)
  267. s.notaryImportKey(c, repoName, "targets/other", s.not.keys[0].Private)
  268. s.notaryPublish(c, repoName)
  269. // Push should write to the delegation role, not targets
  270. dockerCmd(c, "tag", "busybox", targetName)
  271. pushCmd := exec.Command(dockerBinary, "push", targetName)
  272. s.trustedCmd(pushCmd)
  273. out, _, err := runCommandWithOutput(pushCmd)
  274. c.Assert(err, check.IsNil, check.Commentf(out))
  275. s.assertTargetInRoles(c, repoName, "latest", "targets/other")
  276. s.assertTargetNotInRoles(c, repoName, "latest", "targets")
  277. // Try pull - we should fail, since pull will only pull from the targets/releases
  278. // role or the targets role
  279. pullCmd := exec.Command(dockerBinary, "-D", "pull", repoName)
  280. s.trustedCmd(pullCmd)
  281. out, _, err = runCommandWithOutput(pullCmd)
  282. c.Assert(err, check.NotNil, check.Commentf(out))
  283. c.Assert(out, checker.Contains, "No trust data for")
  284. // try a pull -a: we should fail since pull will only pull from the targets/releases
  285. // role or the targets role
  286. pullCmd = exec.Command(dockerBinary, "-D", "pull", "-a", repoName)
  287. s.trustedCmd(pullCmd)
  288. out, _, err = runCommandWithOutput(pullCmd)
  289. c.Assert(err, check.NotNil, check.Commentf(out))
  290. c.Assert(out, checker.Contains, "No trusted tags for")
  291. }