docker_cli_by_digest_test.go 26 KB

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