docker_cli_pull_trusted_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os/exec"
  6. "strings"
  7. "time"
  8. "github.com/go-check/check"
  9. )
  10. func (s *DockerTrustSuite) TestTrustedPull(c *check.C) {
  11. repoName := s.setupTrustedImage(c, "trusted-pull")
  12. // Try pull
  13. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  14. s.trustedCmd(pullCmd)
  15. out, _, err := runCommandWithOutput(pullCmd)
  16. if err != nil {
  17. c.Fatalf("Error running trusted pull: %s\n%s", err, out)
  18. }
  19. if !strings.Contains(string(out), "Tagging") {
  20. c.Fatalf("Missing expected output on trusted pull:\n%s", out)
  21. }
  22. dockerCmd(c, "rmi", repoName)
  23. // Try untrusted pull to ensure we pushed the tag to the registry
  24. pullCmd = exec.Command(dockerBinary, "pull", "--disable-content-trust=true", repoName)
  25. s.trustedCmd(pullCmd)
  26. out, _, err = runCommandWithOutput(pullCmd)
  27. if err != nil {
  28. c.Fatalf("Error running trusted pull: %s\n%s", err, out)
  29. }
  30. if !strings.Contains(string(out), "Status: Downloaded") {
  31. c.Fatalf("Missing expected output on trusted pull with --disable-content-trust:\n%s", out)
  32. }
  33. }
  34. func (s *DockerTrustSuite) TestTrustedIsolatedPull(c *check.C) {
  35. repoName := s.setupTrustedImage(c, "trusted-isolated-pull")
  36. // Try pull (run from isolated directory without trust information)
  37. pullCmd := exec.Command(dockerBinary, "--config", "/tmp/docker-isolated", "pull", repoName)
  38. s.trustedCmd(pullCmd)
  39. out, _, err := runCommandWithOutput(pullCmd)
  40. if err != nil {
  41. c.Fatalf("Error running trusted pull: %s\n%s", err, out)
  42. }
  43. if !strings.Contains(string(out), "Tagging") {
  44. c.Fatalf("Missing expected output on trusted pull:\n%s", out)
  45. }
  46. dockerCmd(c, "rmi", repoName)
  47. }
  48. func (s *DockerTrustSuite) TestUntrustedPull(c *check.C) {
  49. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  50. // tag the image and upload it to the private registry
  51. dockerCmd(c, "tag", "busybox", repoName)
  52. dockerCmd(c, "push", repoName)
  53. dockerCmd(c, "rmi", repoName)
  54. // Try trusted pull on untrusted tag
  55. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  56. s.trustedCmd(pullCmd)
  57. out, _, err := runCommandWithOutput(pullCmd)
  58. if err == nil {
  59. c.Fatalf("Error expected when running trusted pull with:\n%s", out)
  60. }
  61. if !strings.Contains(string(out), "no trust data available") {
  62. c.Fatalf("Missing expected output on trusted pull:\n%s", out)
  63. }
  64. }
  65. func (s *DockerTrustSuite) TestPullWhenCertExpired(c *check.C) {
  66. c.Skip("Currently changes system time, causing instability")
  67. repoName := s.setupTrustedImage(c, "trusted-cert-expired")
  68. // Certificates have 10 years of expiration
  69. elevenYearsFromNow := time.Now().Add(time.Hour * 24 * 365 * 11)
  70. runAtDifferentDate(elevenYearsFromNow, func() {
  71. // Try pull
  72. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  73. s.trustedCmd(pullCmd)
  74. out, _, err := runCommandWithOutput(pullCmd)
  75. if err == nil {
  76. c.Fatalf("Error running trusted pull in the distant future: %s\n%s", err, out)
  77. }
  78. if !strings.Contains(string(out), "could not validate the path to a trusted root") {
  79. c.Fatalf("Missing expected output on trusted pull in the distant future:\n%s", out)
  80. }
  81. })
  82. runAtDifferentDate(elevenYearsFromNow, func() {
  83. // Try pull
  84. pullCmd := exec.Command(dockerBinary, "pull", "--disable-content-trust", repoName)
  85. s.trustedCmd(pullCmd)
  86. out, _, err := runCommandWithOutput(pullCmd)
  87. if err != nil {
  88. c.Fatalf("Error running untrusted pull in the distant future: %s\n%s", err, out)
  89. }
  90. if !strings.Contains(string(out), "Status: Downloaded") {
  91. c.Fatalf("Missing expected output on untrusted pull in the distant future:\n%s", out)
  92. }
  93. })
  94. }
  95. func (s *DockerTrustSuite) TestTrustedPullFromBadTrustServer(c *check.C) {
  96. repoName := fmt.Sprintf("%v/dockerclievilpull/trusted:latest", privateRegistryURL)
  97. evilLocalConfigDir, err := ioutil.TempDir("", "evil-local-config-dir")
  98. if err != nil {
  99. c.Fatalf("Failed to create local temp dir")
  100. }
  101. // tag the image and upload it to the private registry
  102. dockerCmd(c, "tag", "busybox", repoName)
  103. pushCmd := exec.Command(dockerBinary, "push", repoName)
  104. s.trustedCmd(pushCmd)
  105. out, _, err := runCommandWithOutput(pushCmd)
  106. if err != nil {
  107. c.Fatalf("Error running trusted push: %s\n%s", err, out)
  108. }
  109. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  110. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  111. }
  112. dockerCmd(c, "rmi", repoName)
  113. // Try pull
  114. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  115. s.trustedCmd(pullCmd)
  116. out, _, err = runCommandWithOutput(pullCmd)
  117. if err != nil {
  118. c.Fatalf("Error running trusted pull: %s\n%s", err, out)
  119. }
  120. if !strings.Contains(string(out), "Tagging") {
  121. c.Fatalf("Missing expected output on trusted pull:\n%s", out)
  122. }
  123. dockerCmd(c, "rmi", repoName)
  124. // Kill the notary server, start a new "evil" one.
  125. s.not.Close()
  126. s.not, err = newTestNotary(c)
  127. if err != nil {
  128. c.Fatalf("Restarting notary server failed.")
  129. }
  130. // In order to make an evil server, lets re-init a client (with a different trust dir) and push new data.
  131. // tag an image and upload it to the private registry
  132. dockerCmd(c, "--config", evilLocalConfigDir, "tag", "busybox", repoName)
  133. // Push up to the new server
  134. pushCmd = exec.Command(dockerBinary, "--config", evilLocalConfigDir, "push", repoName)
  135. s.trustedCmd(pushCmd)
  136. out, _, err = runCommandWithOutput(pushCmd)
  137. if err != nil {
  138. c.Fatalf("Error running trusted push: %s\n%s", err, out)
  139. }
  140. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  141. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  142. }
  143. // Now, try pulling with the original client from this new trust server. This should fail.
  144. pullCmd = exec.Command(dockerBinary, "pull", repoName)
  145. s.trustedCmd(pullCmd)
  146. out, _, err = runCommandWithOutput(pullCmd)
  147. if err == nil {
  148. c.Fatalf("Expected to fail on this pull due to different remote data: %s\n%s", err, out)
  149. }
  150. if !strings.Contains(string(out), "failed to validate data with current trusted certificates") {
  151. c.Fatalf("Missing expected output on trusted pull:\n%s", out)
  152. }
  153. }
  154. func (s *DockerTrustSuite) TestTrustedPullWithExpiredSnapshot(c *check.C) {
  155. c.Skip("Currently changes system time, causing instability")
  156. repoName := fmt.Sprintf("%v/dockercliexpiredtimestamppull/trusted:latest", privateRegistryURL)
  157. // tag the image and upload it to the private registry
  158. dockerCmd(c, "tag", "busybox", repoName)
  159. // Push with default passphrases
  160. pushCmd := exec.Command(dockerBinary, "push", repoName)
  161. s.trustedCmd(pushCmd)
  162. out, _, err := runCommandWithOutput(pushCmd)
  163. if err != nil {
  164. c.Fatalf("trusted push failed: %s\n%s", err, out)
  165. }
  166. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  167. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  168. }
  169. dockerCmd(c, "rmi", repoName)
  170. // Snapshots last for three years. This should be expired
  171. fourYearsLater := time.Now().Add(time.Hour * 24 * 365 * 4)
  172. runAtDifferentDate(fourYearsLater, func() {
  173. // Try pull
  174. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  175. s.trustedCmd(pullCmd)
  176. out, _, err = runCommandWithOutput(pullCmd)
  177. if err == nil {
  178. c.Fatalf("Missing expected error running trusted pull with expired snapshots")
  179. }
  180. if !strings.Contains(string(out), "repository out-of-date") {
  181. c.Fatalf("Missing expected output on trusted pull with expired snapshot:\n%s", out)
  182. }
  183. })
  184. }
  185. func (s *DockerTrustSuite) TestTrustedOfflinePull(c *check.C) {
  186. repoName := s.setupTrustedImage(c, "trusted-offline-pull")
  187. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  188. s.trustedCmdWithServer(pullCmd, "https://invalidnotaryserver")
  189. out, _, err := runCommandWithOutput(pullCmd)
  190. if err == nil {
  191. c.Fatalf("Expected error pulling with invalid notary server:\n%s", out)
  192. }
  193. if !strings.Contains(string(out), "error contacting notary server") {
  194. c.Fatalf("Missing expected output on trusted pull:\n%s", out)
  195. }
  196. // Do valid trusted pull to warm cache
  197. pullCmd = exec.Command(dockerBinary, "pull", repoName)
  198. s.trustedCmd(pullCmd)
  199. out, _, err = runCommandWithOutput(pullCmd)
  200. if err != nil {
  201. c.Fatalf("Error running trusted pull: %s\n%s", err, out)
  202. }
  203. if !strings.Contains(string(out), "Tagging") {
  204. c.Fatalf("Missing expected output on trusted pull:\n%s", out)
  205. }
  206. dockerCmd(c, "rmi", repoName)
  207. // Try pull again with invalid notary server, should use cache
  208. pullCmd = exec.Command(dockerBinary, "pull", repoName)
  209. s.trustedCmdWithServer(pullCmd, "https://invalidnotaryserver")
  210. out, _, err = runCommandWithOutput(pullCmd)
  211. if err != nil {
  212. c.Fatalf("Error running trusted pull: %s\n%s", err, out)
  213. }
  214. if !strings.Contains(string(out), "Tagging") {
  215. c.Fatalf("Missing expected output on trusted pull:\n%s", out)
  216. }
  217. }