docker_cli_by_digest_test.go 27 KB

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