docker_cli_by_digest_test.go 27 KB

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