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