From 618fd45e22a4515b85cbc9017cdc64d20dccd01f Mon Sep 17 00:00:00 2001 From: Patryk Dobrowolski Date: Tue, 1 Oct 2024 16:38:18 +0200 Subject: [PATCH] Revert image digest to manifest digest, Switch to use digest.Hex instead of digest.String() --- pkg/sign/notary.go | 36 ++++++----------------------------- pkg/sign/notary_test.go | 42 ++--------------------------------------- 2 files changed, 8 insertions(+), 70 deletions(-) diff --git a/pkg/sign/notary.go b/pkg/sign/notary.go index 90b3d7378294..4285d72a439c 100644 --- a/pkg/sign/notary.go +++ b/pkg/sign/notary.go @@ -42,10 +42,6 @@ type ReferenceInterface interface { type ImageInterface interface { // Manifest retrieves the manifest of the image. Manifest() (ManifestInterface, error) - // GetDigest returns the digest of the image manifest. - GetDigest() (string, error) - // GetSize returns the size of the image. - GetSize() (int64, error) } // ManifestInterface abstracts the functionality of v1.Manifest. @@ -123,20 +119,6 @@ type ImageWrapper struct { img v1.Image } -// GetDigest returns the digest of the image manifest. -func (iw *ImageWrapper) GetDigest() (string, error) { - digest, err := iw.img.Digest() - if err != nil { - return "", err - } - return digest.Hex, nil -} - -// GetSize returns the size of the image. -func (iw *ImageWrapper) GetSize() (int64, error) { - return iw.img.Size() -} - // Manifest retrieves the manifest of the image. func (iw *ImageWrapper) Manifest() (ManifestInterface, error) { manifest, err := iw.img.Manifest() @@ -156,7 +138,7 @@ func (mw *ManifestWrapper) GetConfigSize() int64 { return mw.manifest.Config.Size } -// GetConfigDigest returns the digest of the image config without the algorithm prefix. +// GetConfigDigest returns the digest of the image config. func (mw *ManifestWrapper) GetConfigDigest() string { return mw.manifest.Config.Digest.Hex } @@ -195,23 +177,17 @@ func (pb *PayloadBuilder) BuildPayload(images []string) (SigningPayload, error) return SigningPayload{}, fmt.Errorf("failed to fetch image: %w", err) } - // Retrieve the image manifest digest. - imageDigest, err := img.GetDigest() - if err != nil { - return SigningPayload{}, fmt.Errorf("failed to get image digest: %w", err) - } - - // Retrieve the image size. - imageSize, err := img.GetSize() + // Retrieve the image manifest. + manifest, err := img.Manifest() if err != nil { - return SigningPayload{}, fmt.Errorf("failed to get image size: %w", err) + return SigningPayload{}, fmt.Errorf("failed to get image manifest: %w", err) } // Build the target information. target := Target{ Name: tag, - ByteSize: imageSize, - Digest: imageDigest, + ByteSize: manifest.GetConfigSize(), + Digest: manifest.GetConfigDigest(), } // Build the GUN (Global Unique Name) targets. diff --git a/pkg/sign/notary_test.go b/pkg/sign/notary_test.go index fc03aa281940..87101e921266 100644 --- a/pkg/sign/notary_test.go +++ b/pkg/sign/notary_test.go @@ -127,12 +127,6 @@ func TestImageService_GetImage_Valid(t *testing.T) { }, }, nil }, - MockGetDigest: func() (string, error) { - return "dummy-digest", nil - }, - MockGetSize: func() (int64, error) { - return 1024, nil - }, }, nil }, } @@ -159,23 +153,6 @@ func TestImageService_GetImage_Valid(t *testing.T) { if manifest.GetConfigDigest() != "sha256:dummy-digest" { t.Errorf("Expected config digest to be 'sha256:dummy-digest', got '%s'", manifest.GetConfigDigest()) } - - // Additional checks for new methods - digest, err := img.GetDigest() - if err != nil { - t.Errorf("Expected no error getting digest, got %v", err) - } - if digest != "dummy-digest" { - t.Errorf("Expected digest to be 'dummy-digest', got '%s'", digest) - } - - size, err := img.GetSize() - if err != nil { - t.Errorf("Expected no error getting size, got %v", err) - } - if size != 1024 { - t.Errorf("Expected size to be 1024, got %d", size) - } } // TestImageService_GetImage_Invalid checks fetching an invalid image. @@ -218,7 +195,7 @@ func TestPayloadBuilder_BuildPayload_Valid(t *testing.T) { return 1024 }, MockGetConfigDigest: func() string { - return "sha256:dummy-config-digest" + return "sha256:dummy-digest" }, } @@ -226,12 +203,6 @@ func TestPayloadBuilder_BuildPayload_Valid(t *testing.T) { MockManifest: func() (ManifestInterface, error) { return mockManifest, nil }, - MockGetDigest: func() (string, error) { - return "dummy-manifest-digest", nil - }, - MockGetSize: func() (int64, error) { - return 2048, nil - }, } mockImageRepository := &MockImageRepository{ @@ -254,15 +225,6 @@ func TestPayloadBuilder_BuildPayload_Valid(t *testing.T) { if len(payload.GunTargets) == 0 { t.Errorf("Expected GunTargets to be populated") } - - // Additional checks - target := payload.GunTargets[0].Targets[0] - if target.Digest != "dummy-manifest-digest" { - t.Errorf("Expected digest to be 'dummy-manifest-digest', got '%s'", target.Digest) - } - if target.ByteSize != 2048 { - t.Errorf("Expected byteSize to be 2048, got %d", target.ByteSize) - } } // TestPayloadBuilder_BuildPayload_Invalid checks building a payload for invalid images. @@ -383,7 +345,7 @@ func TestNotarySigner_Sign_Valid(t *testing.T) { { Name: "latest", ByteSize: 1024, - Digest: "dummy-manifest-digest", + Digest: "sha256:dummy-digest", }, }, },