docker_cli_by_digest_test.go 26 KB

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