docker_cli_pull_trusted_test.go 11 KB

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