docker_cli_pull_trusted_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "time"
  6. "github.com/docker/docker/integration-cli/checker"
  7. "github.com/docker/docker/pkg/testutil"
  8. icmd "github.com/docker/docker/pkg/testutil/cmd"
  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. icmd.RunCmd(icmd.Command(dockerBinary, "pull", repoName), trustedCmd).Assert(c, SuccessTagging)
  15. dockerCmd(c, "rmi", repoName)
  16. // Try untrusted pull to ensure we pushed the tag to the registry
  17. icmd.RunCmd(icmd.Command(dockerBinary, "pull", "--disable-content-trust=true", repoName), trustedCmd).Assert(c, SuccessDownloaded)
  18. }
  19. func (s *DockerTrustSuite) TestTrustedIsolatedPull(c *check.C) {
  20. repoName := s.setupTrustedImage(c, "trusted-isolated-pull")
  21. // Try pull (run from isolated directory without trust information)
  22. icmd.RunCmd(icmd.Command(dockerBinary, "--config", "/tmp/docker-isolated", "pull", repoName), trustedCmd).Assert(c, SuccessTagging)
  23. dockerCmd(c, "rmi", repoName)
  24. }
  25. func (s *DockerTrustSuite) TestUntrustedPull(c *check.C) {
  26. repoName := fmt.Sprintf("%v/dockercliuntrusted/pulltest:latest", privateRegistryURL)
  27. // tag the image and upload it to the private registry
  28. dockerCmd(c, "tag", "busybox", repoName)
  29. dockerCmd(c, "push", repoName)
  30. dockerCmd(c, "rmi", repoName)
  31. // Try trusted pull on untrusted tag
  32. icmd.RunCmd(icmd.Command(dockerBinary, "pull", repoName), trustedCmd).Assert(c, icmd.Expected{
  33. ExitCode: 1,
  34. Err: "Error: remote trust data does not exist",
  35. })
  36. }
  37. func (s *DockerTrustSuite) TestPullWhenCertExpired(c *check.C) {
  38. c.Skip("Currently changes system time, causing instability")
  39. repoName := s.setupTrustedImage(c, "trusted-cert-expired")
  40. // Certificates have 10 years of expiration
  41. elevenYearsFromNow := time.Now().Add(time.Hour * 24 * 365 * 11)
  42. testutil.RunAtDifferentDate(elevenYearsFromNow, func() {
  43. // Try pull
  44. icmd.RunCmd(icmd.Cmd{
  45. Command: []string{dockerBinary, "pull", repoName},
  46. }, trustedCmd).Assert(c, icmd.Expected{
  47. ExitCode: 1,
  48. Err: "could not validate the path to a trusted root",
  49. })
  50. })
  51. testutil.RunAtDifferentDate(elevenYearsFromNow, func() {
  52. // Try pull
  53. icmd.RunCmd(icmd.Cmd{
  54. Command: []string{dockerBinary, "pull", "--disable-content-trust", repoName},
  55. }, trustedCmd).Assert(c, SuccessDownloaded)
  56. })
  57. }
  58. func (s *DockerTrustSuite) TestTrustedPullFromBadTrustServer(c *check.C) {
  59. repoName := fmt.Sprintf("%v/dockerclievilpull/trusted:latest", privateRegistryURL)
  60. evilLocalConfigDir, err := ioutil.TempDir("", "evil-local-config-dir")
  61. if err != nil {
  62. c.Fatalf("Failed to create local temp dir")
  63. }
  64. // tag the image and upload it to the private registry
  65. dockerCmd(c, "tag", "busybox", repoName)
  66. icmd.RunCmd(icmd.Command(dockerBinary, "push", repoName), trustedCmd).Assert(c, SuccessSigningAndPushing)
  67. dockerCmd(c, "rmi", repoName)
  68. // Try pull
  69. icmd.RunCmd(icmd.Command(dockerBinary, "pull", repoName), trustedCmd).Assert(c, SuccessTagging)
  70. dockerCmd(c, "rmi", repoName)
  71. // Kill the notary server, start a new "evil" one.
  72. s.not.Close()
  73. s.not, err = newTestNotary(c)
  74. c.Assert(err, check.IsNil, check.Commentf("Restarting notary server failed."))
  75. // In order to make an evil server, lets re-init a client (with a different trust dir) and push new data.
  76. // tag an image and upload it to the private registry
  77. dockerCmd(c, "--config", evilLocalConfigDir, "tag", "busybox", repoName)
  78. // Push up to the new server
  79. icmd.RunCmd(icmd.Command(dockerBinary, "--config", evilLocalConfigDir, "push", repoName), trustedCmd).Assert(c, SuccessSigningAndPushing)
  80. // Now, try pulling with the original client from this new trust server. This should fail because the new root is invalid.
  81. icmd.RunCmd(icmd.Command(dockerBinary, "pull", repoName), trustedCmd).Assert(c, icmd.Expected{
  82. ExitCode: 1,
  83. Err: "could not rotate trust to a new trusted root",
  84. })
  85. }
  86. func (s *DockerTrustSuite) TestTrustedPullWithExpiredSnapshot(c *check.C) {
  87. c.Skip("Currently changes system time, causing instability")
  88. repoName := fmt.Sprintf("%v/dockercliexpiredtimestamppull/trusted:latest", privateRegistryURL)
  89. // tag the image and upload it to the private registry
  90. dockerCmd(c, "tag", "busybox", repoName)
  91. // Push with default passphrases
  92. icmd.RunCmd(icmd.Command(dockerBinary, "push", repoName), trustedCmd).Assert(c, SuccessSigningAndPushing)
  93. dockerCmd(c, "rmi", repoName)
  94. // Snapshots last for three years. This should be expired
  95. fourYearsLater := time.Now().Add(time.Hour * 24 * 365 * 4)
  96. testutil.RunAtDifferentDate(fourYearsLater, func() {
  97. // Try pull
  98. icmd.RunCmd(icmd.Cmd{
  99. Command: []string{dockerBinary, "pull", repoName},
  100. }, trustedCmd).Assert(c, icmd.Expected{
  101. ExitCode: 1,
  102. Err: "repository out-of-date",
  103. })
  104. })
  105. }
  106. func (s *DockerTrustSuite) TestTrustedOfflinePull(c *check.C) {
  107. repoName := s.setupTrustedImage(c, "trusted-offline-pull")
  108. icmd.RunCmd(icmd.Command(dockerBinary, "pull", repoName), trustedCmdWithServer("https://invalidnotaryserver")).Assert(c, icmd.Expected{
  109. ExitCode: 1,
  110. Err: "error contacting notary server",
  111. })
  112. // Do valid trusted pull to warm cache
  113. icmd.RunCmd(icmd.Command(dockerBinary, "pull", repoName), trustedCmd).Assert(c, SuccessTagging)
  114. dockerCmd(c, "rmi", repoName)
  115. // Try pull again with invalid notary server, should use cache
  116. icmd.RunCmd(icmd.Command(dockerBinary, "pull", repoName), trustedCmdWithServer("https://invalidnotaryserver")).Assert(c, SuccessTagging)
  117. }
  118. func (s *DockerTrustSuite) TestTrustedPullDelete(c *check.C) {
  119. repoName := fmt.Sprintf("%v/dockercli/%s:latest", privateRegistryURL, "trusted-pull-delete")
  120. // tag the image and upload it to the private registry
  121. buildImageSuccessfully(c, repoName, withDockerfile(`
  122. FROM busybox
  123. CMD echo trustedpulldelete
  124. `))
  125. icmd.RunCmd(icmd.Command(dockerBinary, "push", repoName), trustedCmd).Assert(c, SuccessSigningAndPushing)
  126. dockerCmd(c, "rmi", repoName)
  127. // Try pull
  128. result := icmd.RunCmd(icmd.Command(dockerBinary, "pull", repoName), trustedCmd)
  129. result.Assert(c, icmd.Success)
  130. matches := digestRegex.FindStringSubmatch(result.Combined())
  131. c.Assert(matches, checker.HasLen, 2, check.Commentf("unable to parse digest from pull output: %s", result.Combined()))
  132. pullDigest := matches[1]
  133. imageID := inspectField(c, repoName, "Id")
  134. imageByDigest := repoName + "@" + pullDigest
  135. byDigestID := inspectField(c, imageByDigest, "Id")
  136. c.Assert(byDigestID, checker.Equals, imageID)
  137. // rmi of tag should also remove the digest reference
  138. dockerCmd(c, "rmi", repoName)
  139. _, err := inspectFieldWithError(imageByDigest, "Id")
  140. c.Assert(err, checker.NotNil, check.Commentf("digest reference should have been removed"))
  141. _, err = inspectFieldWithError(imageID, "Id")
  142. c.Assert(err, checker.NotNil, check.Commentf("image should have been deleted"))
  143. }
  144. func (s *DockerTrustSuite) TestTrustedPullReadsFromReleasesRole(c *check.C) {
  145. testRequires(c, NotaryHosting)
  146. repoName := fmt.Sprintf("%v/dockerclireleasesdelegationpulling/trusted", privateRegistryURL)
  147. targetName := fmt.Sprintf("%s:latest", repoName)
  148. // Push with targets first, initializing the repo
  149. dockerCmd(c, "tag", "busybox", targetName)
  150. icmd.RunCmd(icmd.Command(dockerBinary, "push", targetName), trustedCmd).Assert(c, icmd.Success)
  151. s.assertTargetInRoles(c, repoName, "latest", "targets")
  152. // Try pull, check we retrieve from targets role
  153. icmd.RunCmd(icmd.Command(dockerBinary, "-D", "pull", repoName), trustedCmd).Assert(c, icmd.Expected{
  154. Err: "retrieving target for targets role",
  155. })
  156. // Now we'll create the releases role, and try pushing and pulling
  157. s.notaryCreateDelegation(c, repoName, "targets/releases", s.not.keys[0].Public)
  158. s.notaryImportKey(c, repoName, "targets/releases", s.not.keys[0].Private)
  159. s.notaryPublish(c, repoName)
  160. // try a pull, check that we can still pull because we can still read the
  161. // old tag in the targets role
  162. icmd.RunCmd(icmd.Command(dockerBinary, "-D", "pull", repoName), trustedCmd).Assert(c, icmd.Expected{
  163. Err: "retrieving target for targets role",
  164. })
  165. // try a pull -a, check that it succeeds because we can still pull from the
  166. // targets role
  167. icmd.RunCmd(icmd.Command(dockerBinary, "-D", "pull", "-a", repoName), trustedCmd).Assert(c, icmd.Success)
  168. // Push, should sign with targets/releases
  169. dockerCmd(c, "tag", "busybox", targetName)
  170. icmd.RunCmd(icmd.Command(dockerBinary, "push", targetName), trustedCmd).Assert(c, icmd.Success)
  171. s.assertTargetInRoles(c, repoName, "latest", "targets", "targets/releases")
  172. // Try pull, check we retrieve from targets/releases role
  173. icmd.RunCmd(icmd.Command(dockerBinary, "-D", "pull", repoName), trustedCmd).Assert(c, icmd.Expected{
  174. Err: "retrieving target for targets/releases role",
  175. })
  176. // Create another delegation that we'll sign with
  177. s.notaryCreateDelegation(c, repoName, "targets/other", s.not.keys[1].Public)
  178. s.notaryImportKey(c, repoName, "targets/other", s.not.keys[1].Private)
  179. s.notaryPublish(c, repoName)
  180. dockerCmd(c, "tag", "busybox", targetName)
  181. icmd.RunCmd(icmd.Command(dockerBinary, "push", targetName), trustedCmd).Assert(c, icmd.Success)
  182. s.assertTargetInRoles(c, repoName, "latest", "targets", "targets/releases", "targets/other")
  183. // Try pull, check we retrieve from targets/releases role
  184. icmd.RunCmd(icmd.Command(dockerBinary, "-D", "pull", repoName), trustedCmd).Assert(c, icmd.Expected{
  185. Err: "retrieving target for targets/releases role",
  186. })
  187. }
  188. func (s *DockerTrustSuite) TestTrustedPullIgnoresOtherDelegationRoles(c *check.C) {
  189. testRequires(c, NotaryHosting)
  190. repoName := fmt.Sprintf("%v/dockerclipullotherdelegation/trusted", privateRegistryURL)
  191. targetName := fmt.Sprintf("%s:latest", repoName)
  192. // We'll create a repo first with a non-release delegation role, so that when we
  193. // push we'll sign it into the delegation role
  194. s.notaryInitRepo(c, repoName)
  195. s.notaryCreateDelegation(c, repoName, "targets/other", s.not.keys[0].Public)
  196. s.notaryImportKey(c, repoName, "targets/other", s.not.keys[0].Private)
  197. s.notaryPublish(c, repoName)
  198. // Push should write to the delegation role, not targets
  199. dockerCmd(c, "tag", "busybox", targetName)
  200. icmd.RunCmd(icmd.Command(dockerBinary, "push", targetName), trustedCmd).Assert(c, icmd.Success)
  201. s.assertTargetInRoles(c, repoName, "latest", "targets/other")
  202. s.assertTargetNotInRoles(c, repoName, "latest", "targets")
  203. // Try pull - we should fail, since pull will only pull from the targets/releases
  204. // role or the targets role
  205. dockerCmd(c, "tag", "busybox", targetName)
  206. icmd.RunCmd(icmd.Command(dockerBinary, "-D", "pull", repoName), trustedCmd).Assert(c, icmd.Expected{
  207. ExitCode: 1,
  208. Err: "No trust data for",
  209. })
  210. // try a pull -a: we should fail since pull will only pull from the targets/releases
  211. // role or the targets role
  212. icmd.RunCmd(icmd.Command(dockerBinary, "-D", "pull", "-a", repoName), trustedCmd).Assert(c, icmd.Expected{
  213. ExitCode: 1,
  214. Err: "No trusted tags for",
  215. })
  216. }