docker_cli_by_digest_test.go 26 KB

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