Skip to content

Commit 9bb8991

Browse files
authored
fix(digest): check container image info for nil (#1027)
1 parent 7221704 commit 9bb8991

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

internal/actions/mocks/container.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ func CreateMockContainer(id string, name string, image string, created time.Time
4343

4444
// CreateMockContainerWithImageInfo should only be used for testing
4545
func CreateMockContainerWithImageInfo(id string, name string, image string, created time.Time, imageInfo types.ImageInspect) container.Container {
46+
return CreateMockContainerWithImageInfoP(id, name, image, created, &imageInfo)
47+
}
48+
49+
// CreateMockContainerWithImageInfoP should only be used for testing
50+
func CreateMockContainerWithImageInfoP(id string, name string, image string, created time.Time, imageInfo *types.ImageInspect) container.Container {
4651
content := types.ContainerJSON{
4752
ContainerJSONBase: &types.ContainerJSONBase{
4853
ID: id,
@@ -57,7 +62,7 @@ func CreateMockContainerWithImageInfo(id string, name string, image string, crea
5762
}
5863
return *container.NewContainer(
5964
&content,
60-
&imageInfo,
65+
imageInfo,
6166
)
6267
}
6368

pkg/registry/digest/digest.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ const ContentDigestHeader = "Docker-Content-Digest"
2222

2323
// CompareDigest ...
2424
func CompareDigest(container types.Container, registryAuth string) (bool, error) {
25+
if !container.HasImageInfo() {
26+
return false, errors.New("container image info missing")
27+
}
28+
2529
var digest string
2630

2731
registryAuth = TransformAuth(registryAuth)

pkg/registry/digest/digest_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ var _ = Describe("Digests", func() {
5959
mockCreated,
6060
mockDigest)
6161

62+
mockContainerNoImage := mocks.CreateMockContainerWithImageInfoP(mockId, mockName, mockImage, mockCreated, nil)
63+
6264
When("a digest comparison is done", func() {
6365
It("should return true if digests match",
6466
SkipIfCredentialsEmpty(GHCRCredentials, func() {
@@ -75,6 +77,11 @@ var _ = Describe("Digests", func() {
7577
It("should return an error if the registry isn't available", func() {
7678

7779
})
80+
It("should return an error when container contains no image info", func() {
81+
matches, err := digest.CompareDigest(mockContainerNoImage, `user:pass`)
82+
Expect(err).To(HaveOccurred())
83+
Expect(matches).To(Equal(false))
84+
})
7885
})
7986
When("using different registries", func() {
8087
It("should work with DockerHub",

0 commit comments

Comments
 (0)