docker_cli_by_digest_test.go 25 KB

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