From 9bb8991a768b5d0071bd69f878b47ef3be513311 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?nils=20m=C3=A5s=C3=A9n?= Date: Sun, 25 Jul 2021 12:44:29 +0200 Subject: [PATCH] fix(digest): check container image info for nil (#1027) --- internal/actions/mocks/container.go | 7 ++++++- pkg/registry/digest/digest.go | 4 ++++ pkg/registry/digest/digest_test.go | 7 +++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/internal/actions/mocks/container.go b/internal/actions/mocks/container.go index f854114..167d571 100644 --- a/internal/actions/mocks/container.go +++ b/internal/actions/mocks/container.go @@ -43,6 +43,11 @@ func CreateMockContainer(id string, name string, image string, created time.Time // CreateMockContainerWithImageInfo should only be used for testing func CreateMockContainerWithImageInfo(id string, name string, image string, created time.Time, imageInfo types.ImageInspect) container.Container { + return CreateMockContainerWithImageInfoP(id, name, image, created, &imageInfo) +} + +// CreateMockContainerWithImageInfoP should only be used for testing +func CreateMockContainerWithImageInfoP(id string, name string, image string, created time.Time, imageInfo *types.ImageInspect) container.Container { content := types.ContainerJSON{ ContainerJSONBase: &types.ContainerJSONBase{ ID: id, @@ -57,7 +62,7 @@ func CreateMockContainerWithImageInfo(id string, name string, image string, crea } return *container.NewContainer( &content, - &imageInfo, + imageInfo, ) } diff --git a/pkg/registry/digest/digest.go b/pkg/registry/digest/digest.go index 858bf33..26fbd8e 100644 --- a/pkg/registry/digest/digest.go +++ b/pkg/registry/digest/digest.go @@ -22,6 +22,10 @@ const ContentDigestHeader = "Docker-Content-Digest" // CompareDigest ... func CompareDigest(container types.Container, registryAuth string) (bool, error) { + if !container.HasImageInfo() { + return false, errors.New("container image info missing") + } + var digest string registryAuth = TransformAuth(registryAuth) diff --git a/pkg/registry/digest/digest_test.go b/pkg/registry/digest/digest_test.go index 0321c1f..70193b8 100644 --- a/pkg/registry/digest/digest_test.go +++ b/pkg/registry/digest/digest_test.go @@ -59,6 +59,8 @@ var _ = Describe("Digests", func() { mockCreated, mockDigest) + mockContainerNoImage := mocks.CreateMockContainerWithImageInfoP(mockId, mockName, mockImage, mockCreated, nil) + When("a digest comparison is done", func() { It("should return true if digests match", SkipIfCredentialsEmpty(GHCRCredentials, func() { @@ -75,6 +77,11 @@ var _ = Describe("Digests", func() { It("should return an error if the registry isn't available", func() { }) + It("should return an error when container contains no image info", func() { + matches, err := digest.CompareDigest(mockContainerNoImage, `user:pass`) + Expect(err).To(HaveOccurred()) + Expect(matches).To(Equal(false)) + }) }) When("using different registries", func() { It("should work with DockerHub",