docker_cli_pull_trusted_test.go 14 KB

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