docker_cli_by_digest_test.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "regexp"
  8. "strings"
  9. "github.com/docker/distribution/manifest/schema1"
  10. "github.com/docker/distribution/manifest/schema2"
  11. "github.com/docker/docker/api/types"
  12. "github.com/docker/docker/integration-cli/checker"
  13. "github.com/docker/docker/pkg/stringutils"
  14. "github.com/go-check/check"
  15. "github.com/opencontainers/go-digest"
  16. )
  17. var (
  18. remoteRepoName = "dockercli/busybox-by-dgst"
  19. repoName = fmt.Sprintf("%s/%s", privateRegistryURL, remoteRepoName)
  20. pushDigestRegex = regexp.MustCompile("[\\S]+: digest: ([\\S]+) size: [0-9]+")
  21. digestRegex = regexp.MustCompile("Digest: ([\\S]+)")
  22. )
  23. func setupImage(c *check.C) (digest.Digest, error) {
  24. return setupImageWithTag(c, "latest")
  25. }
  26. func setupImageWithTag(c *check.C, tag string) (digest.Digest, error) {
  27. containerName := "busyboxbydigest"
  28. // new file is committed because this layer is used for detecting malicious
  29. // changes. if this was committed as empty layer it would be skipped on pull
  30. // and malicious changes would never be detected.
  31. dockerCmd(c, "run", "-e", "digest=1", "--name", containerName, "busybox", "touch", "anewfile")
  32. // tag the image to upload it to the private registry
  33. repoAndTag := repoName + ":" + tag
  34. out, _, err := dockerCmdWithError("commit", containerName, repoAndTag)
  35. c.Assert(err, checker.IsNil, check.Commentf("image tagging failed: %s", out))
  36. // delete the container as we don't need it any more
  37. err = deleteContainer(containerName)
  38. c.Assert(err, checker.IsNil)
  39. // push the image
  40. out, _, err = dockerCmdWithError("push", repoAndTag)
  41. c.Assert(err, checker.IsNil, check.Commentf("pushing the image to the private registry has failed: %s", out))
  42. // delete our local repo that we previously tagged
  43. rmiout, _, err := dockerCmdWithError("rmi", repoAndTag)
  44. c.Assert(err, checker.IsNil, check.Commentf("error deleting images prior to real test: %s", rmiout))
  45. matches := pushDigestRegex.FindStringSubmatch(out)
  46. c.Assert(matches, checker.HasLen, 2, check.Commentf("unable to parse digest from push output: %s", out))
  47. pushDigest := matches[1]
  48. return digest.Digest(pushDigest), nil
  49. }
  50. func testPullByTagDisplaysDigest(c *check.C) {
  51. testRequires(c, DaemonIsLinux)
  52. pushDigest, err := setupImage(c)
  53. c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
  54. // pull from the registry using the tag
  55. out, _ := dockerCmd(c, "pull", repoName)
  56. // the pull output includes "Digest: <digest>", so find that
  57. matches := digestRegex.FindStringSubmatch(out)
  58. c.Assert(matches, checker.HasLen, 2, check.Commentf("unable to parse digest from pull output: %s", out))
  59. pullDigest := matches[1]
  60. // make sure the pushed and pull digests match
  61. c.Assert(pushDigest.String(), checker.Equals, pullDigest)
  62. }
  63. func (s *DockerRegistrySuite) TestPullByTagDisplaysDigest(c *check.C) {
  64. testPullByTagDisplaysDigest(c)
  65. }
  66. func (s *DockerSchema1RegistrySuite) TestPullByTagDisplaysDigest(c *check.C) {
  67. testPullByTagDisplaysDigest(c)
  68. }
  69. func testPullByDigest(c *check.C) {
  70. testRequires(c, DaemonIsLinux)
  71. pushDigest, err := setupImage(c)
  72. c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
  73. // pull from the registry using the <name>@<digest> reference
  74. imageReference := fmt.Sprintf("%s@%s", repoName, pushDigest)
  75. out, _ := dockerCmd(c, "pull", imageReference)
  76. // the pull output includes "Digest: <digest>", so find that
  77. matches := digestRegex.FindStringSubmatch(out)
  78. c.Assert(matches, checker.HasLen, 2, check.Commentf("unable to parse digest from pull output: %s", out))
  79. pullDigest := matches[1]
  80. // make sure the pushed and pull digests match
  81. c.Assert(pushDigest.String(), checker.Equals, pullDigest)
  82. }
  83. func (s *DockerRegistrySuite) TestPullByDigest(c *check.C) {
  84. testPullByDigest(c)
  85. }
  86. func (s *DockerSchema1RegistrySuite) TestPullByDigest(c *check.C) {
  87. testPullByDigest(c)
  88. }
  89. func testPullByDigestNoFallback(c *check.C) {
  90. testRequires(c, DaemonIsLinux)
  91. // pull from the registry using the <name>@<digest> reference
  92. imageReference := fmt.Sprintf("%s@sha256:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", repoName)
  93. out, _, err := dockerCmdWithError("pull", imageReference)
  94. c.Assert(err, checker.NotNil, check.Commentf("expected non-zero exit status and correct error message when pulling non-existing image"))
  95. c.Assert(out, checker.Contains, fmt.Sprintf("manifest for %s not found", imageReference), check.Commentf("expected non-zero exit status and correct error message when pulling non-existing image"))
  96. }
  97. func (s *DockerRegistrySuite) TestPullByDigestNoFallback(c *check.C) {
  98. testPullByDigestNoFallback(c)
  99. }
  100. func (s *DockerSchema1RegistrySuite) TestPullByDigestNoFallback(c *check.C) {
  101. testPullByDigestNoFallback(c)
  102. }
  103. func (s *DockerRegistrySuite) TestCreateByDigest(c *check.C) {
  104. pushDigest, err := setupImage(c)
  105. c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
  106. imageReference := fmt.Sprintf("%s@%s", repoName, pushDigest)
  107. containerName := "createByDigest"
  108. dockerCmd(c, "create", "--name", containerName, imageReference)
  109. res := inspectField(c, containerName, "Config.Image")
  110. c.Assert(res, checker.Equals, imageReference)
  111. }
  112. func (s *DockerRegistrySuite) TestRunByDigest(c *check.C) {
  113. pushDigest, err := setupImage(c)
  114. c.Assert(err, checker.IsNil)
  115. imageReference := fmt.Sprintf("%s@%s", repoName, pushDigest)
  116. containerName := "runByDigest"
  117. out, _ := dockerCmd(c, "run", "--name", containerName, imageReference, "sh", "-c", "echo found=$digest")
  118. foundRegex := regexp.MustCompile("found=([^\n]+)")
  119. matches := foundRegex.FindStringSubmatch(out)
  120. c.Assert(matches, checker.HasLen, 2, check.Commentf("unable to parse digest from pull output: %s", out))
  121. c.Assert(matches[1], checker.Equals, "1", check.Commentf("Expected %q, got %q", "1", matches[1]))
  122. res := inspectField(c, containerName, "Config.Image")
  123. c.Assert(res, checker.Equals, imageReference)
  124. }
  125. func (s *DockerRegistrySuite) TestRemoveImageByDigest(c *check.C) {
  126. digest, err := setupImage(c)
  127. c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
  128. imageReference := fmt.Sprintf("%s@%s", repoName, digest)
  129. // pull from the registry using the <name>@<digest> reference
  130. dockerCmd(c, "pull", imageReference)
  131. // make sure inspect runs ok
  132. inspectField(c, imageReference, "Id")
  133. // do the delete
  134. err = deleteImages(imageReference)
  135. c.Assert(err, checker.IsNil, check.Commentf("unexpected error deleting image"))
  136. // try to inspect again - it should error this time
  137. _, err = inspectFieldWithError(imageReference, "Id")
  138. //unexpected nil err trying to inspect what should be a non-existent image
  139. c.Assert(err, checker.NotNil)
  140. c.Assert(err.Error(), checker.Contains, "No such object")
  141. }
  142. func (s *DockerRegistrySuite) TestBuildByDigest(c *check.C) {
  143. digest, err := setupImage(c)
  144. c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
  145. imageReference := fmt.Sprintf("%s@%s", repoName, digest)
  146. // pull from the registry using the <name>@<digest> reference
  147. dockerCmd(c, "pull", imageReference)
  148. // get the image id
  149. imageID := inspectField(c, imageReference, "Id")
  150. // do the build
  151. name := "buildbydigest"
  152. buildImageSuccessfully(c, name, withDockerfile(fmt.Sprintf(
  153. `FROM %s
  154. CMD ["/bin/echo", "Hello World"]`, imageReference)))
  155. c.Assert(err, checker.IsNil)
  156. // get the build's image id
  157. res := inspectField(c, name, "Config.Image")
  158. // make sure they match
  159. c.Assert(res, checker.Equals, imageID)
  160. }
  161. func (s *DockerRegistrySuite) TestTagByDigest(c *check.C) {
  162. digest, err := setupImage(c)
  163. c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
  164. imageReference := fmt.Sprintf("%s@%s", repoName, digest)
  165. // pull from the registry using the <name>@<digest> reference
  166. dockerCmd(c, "pull", imageReference)
  167. // tag it
  168. tag := "tagbydigest"
  169. dockerCmd(c, "tag", imageReference, tag)
  170. expectedID := inspectField(c, imageReference, "Id")
  171. tagID := inspectField(c, tag, "Id")
  172. c.Assert(tagID, checker.Equals, expectedID)
  173. }
  174. func (s *DockerRegistrySuite) TestListImagesWithoutDigests(c *check.C) {
  175. digest, err := setupImage(c)
  176. c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
  177. imageReference := fmt.Sprintf("%s@%s", repoName, digest)
  178. // pull from the registry using the <name>@<digest> reference
  179. dockerCmd(c, "pull", imageReference)
  180. out, _ := dockerCmd(c, "images")
  181. c.Assert(out, checker.Not(checker.Contains), "DIGEST", check.Commentf("list output should not have contained DIGEST header"))
  182. }
  183. func (s *DockerRegistrySuite) TestListImagesWithDigests(c *check.C) {
  184. // setup image1
  185. digest1, err := setupImageWithTag(c, "tag1")
  186. c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
  187. imageReference1 := fmt.Sprintf("%s@%s", repoName, digest1)
  188. c.Logf("imageReference1 = %s", imageReference1)
  189. // pull image1 by digest
  190. dockerCmd(c, "pull", imageReference1)
  191. // list images
  192. out, _ := dockerCmd(c, "images", "--digests")
  193. // make sure repo shown, tag=<none>, digest = $digest1
  194. re1 := regexp.MustCompile(`\s*` + repoName + `\s*<none>\s*` + digest1.String() + `\s`)
  195. c.Assert(re1.MatchString(out), checker.True, check.Commentf("expected %q: %s", re1.String(), out))
  196. // setup image2
  197. digest2, err := setupImageWithTag(c, "tag2")
  198. //error setting up image
  199. c.Assert(err, checker.IsNil)
  200. imageReference2 := fmt.Sprintf("%s@%s", repoName, digest2)
  201. c.Logf("imageReference2 = %s", imageReference2)
  202. // pull image1 by digest
  203. dockerCmd(c, "pull", imageReference1)
  204. // pull image2 by digest
  205. dockerCmd(c, "pull", imageReference2)
  206. // list images
  207. out, _ = dockerCmd(c, "images", "--digests")
  208. // make sure repo shown, tag=<none>, digest = $digest1
  209. c.Assert(re1.MatchString(out), checker.True, check.Commentf("expected %q: %s", re1.String(), out))
  210. // make sure repo shown, tag=<none>, digest = $digest2
  211. re2 := regexp.MustCompile(`\s*` + repoName + `\s*<none>\s*` + digest2.String() + `\s`)
  212. c.Assert(re2.MatchString(out), checker.True, check.Commentf("expected %q: %s", re2.String(), out))
  213. // pull tag1
  214. dockerCmd(c, "pull", repoName+":tag1")
  215. // list images
  216. out, _ = dockerCmd(c, "images", "--digests")
  217. // make sure image 1 has repo, tag, <none> AND repo, <none>, digest
  218. reWithDigest1 := regexp.MustCompile(`\s*` + repoName + `\s*tag1\s*` + digest1.String() + `\s`)
  219. c.Assert(reWithDigest1.MatchString(out), checker.True, check.Commentf("expected %q: %s", reWithDigest1.String(), out))
  220. // make sure image 2 has repo, <none>, digest
  221. c.Assert(re2.MatchString(out), checker.True, check.Commentf("expected %q: %s", re2.String(), out))
  222. // pull tag 2
  223. dockerCmd(c, "pull", repoName+":tag2")
  224. // list images
  225. out, _ = dockerCmd(c, "images", "--digests")
  226. // make sure image 1 has repo, tag, digest
  227. c.Assert(reWithDigest1.MatchString(out), checker.True, check.Commentf("expected %q: %s", reWithDigest1.String(), out))
  228. // make sure image 2 has repo, tag, digest
  229. reWithDigest2 := regexp.MustCompile(`\s*` + repoName + `\s*tag2\s*` + digest2.String() + `\s`)
  230. c.Assert(reWithDigest2.MatchString(out), checker.True, check.Commentf("expected %q: %s", reWithDigest2.String(), out))
  231. // list images
  232. out, _ = dockerCmd(c, "images", "--digests")
  233. // make sure image 1 has repo, tag, digest
  234. c.Assert(reWithDigest1.MatchString(out), checker.True, check.Commentf("expected %q: %s", reWithDigest1.String(), out))
  235. // make sure image 2 has repo, tag, digest
  236. c.Assert(reWithDigest2.MatchString(out), checker.True, check.Commentf("expected %q: %s", reWithDigest2.String(), out))
  237. // make sure busybox has tag, but not digest
  238. busyboxRe := regexp.MustCompile(`\s*busybox\s*latest\s*<none>\s`)
  239. c.Assert(busyboxRe.MatchString(out), checker.True, check.Commentf("expected %q: %s", busyboxRe.String(), out))
  240. }
  241. func (s *DockerRegistrySuite) TestListDanglingImagesWithDigests(c *check.C) {
  242. // setup image1
  243. digest1, err := setupImageWithTag(c, "dangle1")
  244. c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
  245. imageReference1 := fmt.Sprintf("%s@%s", repoName, digest1)
  246. c.Logf("imageReference1 = %s", imageReference1)
  247. // pull image1 by digest
  248. dockerCmd(c, "pull", imageReference1)
  249. // list images
  250. out, _ := dockerCmd(c, "images", "--digests")
  251. // make sure repo shown, tag=<none>, digest = $digest1
  252. re1 := regexp.MustCompile(`\s*` + repoName + `\s*<none>\s*` + digest1.String() + `\s`)
  253. c.Assert(re1.MatchString(out), checker.True, check.Commentf("expected %q: %s", re1.String(), out))
  254. // setup image2
  255. digest2, err := setupImageWithTag(c, "dangle2")
  256. //error setting up image
  257. c.Assert(err, checker.IsNil)
  258. imageReference2 := fmt.Sprintf("%s@%s", repoName, digest2)
  259. c.Logf("imageReference2 = %s", imageReference2)
  260. // pull image1 by digest
  261. dockerCmd(c, "pull", imageReference1)
  262. // pull image2 by digest
  263. dockerCmd(c, "pull", imageReference2)
  264. // list images
  265. out, _ = dockerCmd(c, "images", "--digests", "--filter=dangling=true")
  266. // make sure repo shown, tag=<none>, digest = $digest1
  267. c.Assert(re1.MatchString(out), checker.True, check.Commentf("expected %q: %s", re1.String(), out))
  268. // make sure repo shown, tag=<none>, digest = $digest2
  269. re2 := regexp.MustCompile(`\s*` + repoName + `\s*<none>\s*` + digest2.String() + `\s`)
  270. c.Assert(re2.MatchString(out), checker.True, check.Commentf("expected %q: %s", re2.String(), out))
  271. // pull dangle1 tag
  272. dockerCmd(c, "pull", repoName+":dangle1")
  273. // list images
  274. out, _ = dockerCmd(c, "images", "--digests", "--filter=dangling=true")
  275. // make sure image 1 has repo, tag, <none> AND repo, <none>, digest
  276. reWithDigest1 := regexp.MustCompile(`\s*` + repoName + `\s*dangle1\s*` + digest1.String() + `\s`)
  277. c.Assert(reWithDigest1.MatchString(out), checker.False, check.Commentf("unexpected %q: %s", reWithDigest1.String(), out))
  278. // make sure image 2 has repo, <none>, digest
  279. c.Assert(re2.MatchString(out), checker.True, check.Commentf("expected %q: %s", re2.String(), out))
  280. // pull dangle2 tag
  281. dockerCmd(c, "pull", repoName+":dangle2")
  282. // list images, show tagged images
  283. out, _ = dockerCmd(c, "images", "--digests")
  284. // make sure image 1 has repo, tag, digest
  285. c.Assert(reWithDigest1.MatchString(out), checker.True, check.Commentf("expected %q: %s", reWithDigest1.String(), out))
  286. // make sure image 2 has repo, tag, digest
  287. reWithDigest2 := regexp.MustCompile(`\s*` + repoName + `\s*dangle2\s*` + digest2.String() + `\s`)
  288. c.Assert(reWithDigest2.MatchString(out), checker.True, check.Commentf("expected %q: %s", reWithDigest2.String(), out))
  289. // list images, no longer dangling, should not match
  290. out, _ = dockerCmd(c, "images", "--digests", "--filter=dangling=true")
  291. // make sure image 1 has repo, tag, digest
  292. c.Assert(reWithDigest1.MatchString(out), checker.False, check.Commentf("unexpected %q: %s", reWithDigest1.String(), out))
  293. // make sure image 2 has repo, tag, digest
  294. c.Assert(reWithDigest2.MatchString(out), checker.False, check.Commentf("unexpected %q: %s", reWithDigest2.String(), out))
  295. }
  296. func (s *DockerRegistrySuite) TestInspectImageWithDigests(c *check.C) {
  297. digest, err := setupImage(c)
  298. c.Assert(err, check.IsNil, check.Commentf("error setting up image"))
  299. imageReference := fmt.Sprintf("%s@%s", repoName, digest)
  300. // pull from the registry using the <name>@<digest> reference
  301. dockerCmd(c, "pull", imageReference)
  302. out, _ := dockerCmd(c, "inspect", imageReference)
  303. var imageJSON []types.ImageInspect
  304. err = json.Unmarshal([]byte(out), &imageJSON)
  305. c.Assert(err, checker.IsNil)
  306. c.Assert(imageJSON, checker.HasLen, 1)
  307. c.Assert(imageJSON[0].RepoDigests, checker.HasLen, 1)
  308. c.Assert(stringutils.InSlice(imageJSON[0].RepoDigests, imageReference), checker.Equals, true)
  309. }
  310. func (s *DockerRegistrySuite) TestPsListContainersFilterAncestorImageByDigest(c *check.C) {
  311. digest, err := setupImage(c)
  312. c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
  313. imageReference := fmt.Sprintf("%s@%s", repoName, digest)
  314. // pull from the registry using the <name>@<digest> reference
  315. dockerCmd(c, "pull", imageReference)
  316. // build an image from it
  317. imageName1 := "images_ps_filter_test"
  318. buildImageSuccessfully(c, imageName1, withDockerfile(fmt.Sprintf(
  319. `FROM %s
  320. LABEL match me 1`, imageReference)))
  321. // run a container based on that
  322. dockerCmd(c, "run", "--name=test1", imageReference, "echo", "hello")
  323. expectedID := getIDByName(c, "test1")
  324. // run a container based on the a descendant of that too
  325. dockerCmd(c, "run", "--name=test2", imageName1, "echo", "hello")
  326. expectedID1 := getIDByName(c, "test2")
  327. expectedIDs := []string{expectedID, expectedID1}
  328. // Invalid imageReference
  329. out, _ := dockerCmd(c, "ps", "-a", "-q", "--no-trunc", fmt.Sprintf("--filter=ancestor=busybox@%s", digest))
  330. // Filter container for ancestor filter should be empty
  331. c.Assert(strings.TrimSpace(out), checker.Equals, "")
  332. // Valid imageReference
  333. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=ancestor="+imageReference)
  334. checkPsAncestorFilterOutput(c, out, imageReference, expectedIDs)
  335. }
  336. func (s *DockerRegistrySuite) TestDeleteImageByIDOnlyPulledByDigest(c *check.C) {
  337. pushDigest, err := setupImage(c)
  338. c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
  339. // pull from the registry using the <name>@<digest> reference
  340. imageReference := fmt.Sprintf("%s@%s", repoName, pushDigest)
  341. dockerCmd(c, "pull", imageReference)
  342. // just in case...
  343. dockerCmd(c, "tag", imageReference, repoName+":sometag")
  344. imageID := inspectField(c, imageReference, "Id")
  345. dockerCmd(c, "rmi", imageID)
  346. _, err = inspectFieldWithError(imageID, "Id")
  347. c.Assert(err, checker.NotNil, check.Commentf("image should have been deleted"))
  348. }
  349. func (s *DockerRegistrySuite) TestDeleteImageWithDigestAndTag(c *check.C) {
  350. pushDigest, err := setupImage(c)
  351. c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
  352. // pull from the registry using the <name>@<digest> reference
  353. imageReference := fmt.Sprintf("%s@%s", repoName, pushDigest)
  354. dockerCmd(c, "pull", imageReference)
  355. imageID := inspectField(c, imageReference, "Id")
  356. repoTag := repoName + ":sometag"
  357. repoTag2 := repoName + ":othertag"
  358. dockerCmd(c, "tag", imageReference, repoTag)
  359. dockerCmd(c, "tag", imageReference, repoTag2)
  360. dockerCmd(c, "rmi", repoTag2)
  361. // rmi should have deleted only repoTag2, because there's another tag
  362. inspectField(c, repoTag, "Id")
  363. dockerCmd(c, "rmi", repoTag)
  364. // rmi should have deleted the tag, the digest reference, and the image itself
  365. _, err = inspectFieldWithError(imageID, "Id")
  366. c.Assert(err, checker.NotNil, check.Commentf("image should have been deleted"))
  367. }
  368. func (s *DockerRegistrySuite) TestDeleteImageWithDigestAndMultiRepoTag(c *check.C) {
  369. pushDigest, err := setupImage(c)
  370. c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
  371. repo2 := fmt.Sprintf("%s/%s", repoName, "repo2")
  372. // pull from the registry using the <name>@<digest> reference
  373. imageReference := fmt.Sprintf("%s@%s", repoName, pushDigest)
  374. dockerCmd(c, "pull", imageReference)
  375. imageID := inspectField(c, imageReference, "Id")
  376. repoTag := repoName + ":sometag"
  377. repoTag2 := repo2 + ":othertag"
  378. dockerCmd(c, "tag", imageReference, repoTag)
  379. dockerCmd(c, "tag", imageReference, repoTag2)
  380. dockerCmd(c, "rmi", repoTag)
  381. // rmi should have deleted repoTag and image reference, but left repoTag2
  382. inspectField(c, repoTag2, "Id")
  383. _, err = inspectFieldWithError(imageReference, "Id")
  384. c.Assert(err, checker.NotNil, check.Commentf("image digest reference should have been removed"))
  385. _, err = inspectFieldWithError(repoTag, "Id")
  386. c.Assert(err, checker.NotNil, check.Commentf("image tag reference should have been removed"))
  387. dockerCmd(c, "rmi", repoTag2)
  388. // rmi should have deleted the tag, the digest reference, and the image itself
  389. _, err = inspectFieldWithError(imageID, "Id")
  390. c.Assert(err, checker.NotNil, check.Commentf("image should have been deleted"))
  391. }
  392. // TestPullFailsWithAlteredManifest tests that a `docker pull` fails when
  393. // we have modified a manifest blob and its digest cannot be verified.
  394. // This is the schema2 version of the test.
  395. func (s *DockerRegistrySuite) TestPullFailsWithAlteredManifest(c *check.C) {
  396. testRequires(c, DaemonIsLinux)
  397. manifestDigest, err := setupImage(c)
  398. c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
  399. // Load the target manifest blob.
  400. manifestBlob := s.reg.ReadBlobContents(c, manifestDigest)
  401. var imgManifest schema2.Manifest
  402. err = json.Unmarshal(manifestBlob, &imgManifest)
  403. c.Assert(err, checker.IsNil, check.Commentf("unable to decode image manifest from blob"))
  404. // Change a layer in the manifest.
  405. imgManifest.Layers[0].Digest = digest.Digest("sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")
  406. // Move the existing data file aside, so that we can replace it with a
  407. // malicious blob of data. NOTE: we defer the returned undo func.
  408. undo := s.reg.TempMoveBlobData(c, manifestDigest)
  409. defer undo()
  410. alteredManifestBlob, err := json.MarshalIndent(imgManifest, "", " ")
  411. c.Assert(err, checker.IsNil, check.Commentf("unable to encode altered image manifest to JSON"))
  412. s.reg.WriteBlobContents(c, manifestDigest, alteredManifestBlob)
  413. // Now try pulling that image by digest. We should get an error about
  414. // digest verification for the manifest digest.
  415. // Pull from the registry using the <name>@<digest> reference.
  416. imageReference := fmt.Sprintf("%s@%s", repoName, manifestDigest)
  417. out, exitStatus, _ := dockerCmdWithError("pull", imageReference)
  418. c.Assert(exitStatus, checker.Not(check.Equals), 0)
  419. expectedErrorMsg := fmt.Sprintf("manifest verification failed for digest %s", manifestDigest)
  420. c.Assert(out, checker.Contains, expectedErrorMsg)
  421. }
  422. // TestPullFailsWithAlteredManifest tests that a `docker pull` fails when
  423. // we have modified a manifest blob and its digest cannot be verified.
  424. // This is the schema1 version of the test.
  425. func (s *DockerSchema1RegistrySuite) TestPullFailsWithAlteredManifest(c *check.C) {
  426. testRequires(c, DaemonIsLinux)
  427. manifestDigest, err := setupImage(c)
  428. c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
  429. // Load the target manifest blob.
  430. manifestBlob := s.reg.ReadBlobContents(c, manifestDigest)
  431. var imgManifest schema1.Manifest
  432. err = json.Unmarshal(manifestBlob, &imgManifest)
  433. c.Assert(err, checker.IsNil, check.Commentf("unable to decode image manifest from blob"))
  434. // Change a layer in the manifest.
  435. imgManifest.FSLayers[0] = schema1.FSLayer{
  436. BlobSum: digest.Digest("sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"),
  437. }
  438. // Move the existing data file aside, so that we can replace it with a
  439. // malicious blob of data. NOTE: we defer the returned undo func.
  440. undo := s.reg.TempMoveBlobData(c, manifestDigest)
  441. defer undo()
  442. alteredManifestBlob, err := json.MarshalIndent(imgManifest, "", " ")
  443. c.Assert(err, checker.IsNil, check.Commentf("unable to encode altered image manifest to JSON"))
  444. s.reg.WriteBlobContents(c, manifestDigest, alteredManifestBlob)
  445. // Now try pulling that image by digest. We should get an error about
  446. // digest verification for the manifest digest.
  447. // Pull from the registry using the <name>@<digest> reference.
  448. imageReference := fmt.Sprintf("%s@%s", repoName, manifestDigest)
  449. out, exitStatus, _ := dockerCmdWithError("pull", imageReference)
  450. c.Assert(exitStatus, checker.Not(check.Equals), 0)
  451. expectedErrorMsg := fmt.Sprintf("image verification failed for digest %s", manifestDigest)
  452. c.Assert(out, checker.Contains, expectedErrorMsg)
  453. }
  454. // TestPullFailsWithAlteredLayer tests that a `docker pull` fails when
  455. // we have modified a layer blob and its digest cannot be verified.
  456. // This is the schema2 version of the test.
  457. func (s *DockerRegistrySuite) TestPullFailsWithAlteredLayer(c *check.C) {
  458. testRequires(c, DaemonIsLinux)
  459. manifestDigest, err := setupImage(c)
  460. c.Assert(err, checker.IsNil)
  461. // Load the target manifest blob.
  462. manifestBlob := s.reg.ReadBlobContents(c, manifestDigest)
  463. var imgManifest schema2.Manifest
  464. err = json.Unmarshal(manifestBlob, &imgManifest)
  465. c.Assert(err, checker.IsNil)
  466. // Next, get the digest of one of the layers from the manifest.
  467. targetLayerDigest := imgManifest.Layers[0].Digest
  468. // Move the existing data file aside, so that we can replace it with a
  469. // malicious blob of data. NOTE: we defer the returned undo func.
  470. undo := s.reg.TempMoveBlobData(c, targetLayerDigest)
  471. defer undo()
  472. // Now make a fake data blob in this directory.
  473. s.reg.WriteBlobContents(c, targetLayerDigest, []byte("This is not the data you are looking for."))
  474. // Now try pulling that image by digest. We should get an error about
  475. // digest verification for the target layer digest.
  476. // Remove distribution cache to force a re-pull of the blobs
  477. if err := os.RemoveAll(filepath.Join(testEnv.DockerBasePath(), "image", s.d.StorageDriver(), "distribution")); err != nil {
  478. c.Fatalf("error clearing distribution cache: %v", err)
  479. }
  480. // Pull from the registry using the <name>@<digest> reference.
  481. imageReference := fmt.Sprintf("%s@%s", repoName, manifestDigest)
  482. out, exitStatus, _ := dockerCmdWithError("pull", imageReference)
  483. c.Assert(exitStatus, checker.Not(check.Equals), 0, check.Commentf("expected a non-zero exit status"))
  484. expectedErrorMsg := fmt.Sprintf("filesystem layer verification failed for digest %s", targetLayerDigest)
  485. c.Assert(out, checker.Contains, expectedErrorMsg, check.Commentf("expected error message in output: %s", out))
  486. }
  487. // TestPullFailsWithAlteredLayer tests that a `docker pull` fails when
  488. // we have modified a layer blob and its digest cannot be verified.
  489. // This is the schema1 version of the test.
  490. func (s *DockerSchema1RegistrySuite) TestPullFailsWithAlteredLayer(c *check.C) {
  491. testRequires(c, DaemonIsLinux)
  492. manifestDigest, err := setupImage(c)
  493. c.Assert(err, checker.IsNil)
  494. // Load the target manifest blob.
  495. manifestBlob := s.reg.ReadBlobContents(c, manifestDigest)
  496. var imgManifest schema1.Manifest
  497. err = json.Unmarshal(manifestBlob, &imgManifest)
  498. c.Assert(err, checker.IsNil)
  499. // Next, get the digest of one of the layers from the manifest.
  500. targetLayerDigest := imgManifest.FSLayers[0].BlobSum
  501. // Move the existing data file aside, so that we can replace it with a
  502. // malicious blob of data. NOTE: we defer the returned undo func.
  503. undo := s.reg.TempMoveBlobData(c, targetLayerDigest)
  504. defer undo()
  505. // Now make a fake data blob in this directory.
  506. s.reg.WriteBlobContents(c, targetLayerDigest, []byte("This is not the data you are looking for."))
  507. // Now try pulling that image by digest. We should get an error about
  508. // digest verification for the target layer digest.
  509. // Remove distribution cache to force a re-pull of the blobs
  510. if err := os.RemoveAll(filepath.Join(testEnv.DockerBasePath(), "image", s.d.StorageDriver(), "distribution")); err != nil {
  511. c.Fatalf("error clearing distribution cache: %v", err)
  512. }
  513. // Pull from the registry using the <name>@<digest> reference.
  514. imageReference := fmt.Sprintf("%s@%s", repoName, manifestDigest)
  515. out, exitStatus, _ := dockerCmdWithError("pull", imageReference)
  516. c.Assert(exitStatus, checker.Not(check.Equals), 0, check.Commentf("expected a non-zero exit status"))
  517. expectedErrorMsg := fmt.Sprintf("filesystem layer verification failed for digest %s", targetLayerDigest)
  518. c.Assert(out, checker.Contains, expectedErrorMsg, check.Commentf("expected error message in output: %s", out))
  519. }