2023-07-18 12:27:26 -05:00
|
|
|
package meta_test
|
2023-01-09 15:37:44 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path"
|
2023-11-16 13:39:27 -05:00
|
|
|
"path/filepath"
|
2023-01-09 15:37:44 -05:00
|
|
|
"testing"
|
2023-11-01 11:16:18 -05:00
|
|
|
"time"
|
2023-01-09 15:37:44 -05:00
|
|
|
|
|
|
|
godigest "github.com/opencontainers/go-digest"
|
|
|
|
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
|
2024-01-31 23:34:07 -05:00
|
|
|
zcommon "zotregistry.dev/zot/pkg/common"
|
|
|
|
"zotregistry.dev/zot/pkg/extensions/monitoring"
|
|
|
|
"zotregistry.dev/zot/pkg/log"
|
|
|
|
"zotregistry.dev/zot/pkg/meta"
|
|
|
|
"zotregistry.dev/zot/pkg/meta/boltdb"
|
|
|
|
"zotregistry.dev/zot/pkg/meta/dynamodb"
|
|
|
|
mTypes "zotregistry.dev/zot/pkg/meta/types"
|
|
|
|
"zotregistry.dev/zot/pkg/storage"
|
|
|
|
"zotregistry.dev/zot/pkg/storage/local"
|
|
|
|
storageTypes "zotregistry.dev/zot/pkg/storage/types"
|
|
|
|
tcommon "zotregistry.dev/zot/pkg/test/common"
|
|
|
|
. "zotregistry.dev/zot/pkg/test/image-utils"
|
|
|
|
"zotregistry.dev/zot/pkg/test/mocks"
|
|
|
|
ociutils "zotregistry.dev/zot/pkg/test/oci-utils"
|
|
|
|
"zotregistry.dev/zot/pkg/test/signature"
|
|
|
|
tskip "zotregistry.dev/zot/pkg/test/skip"
|
2023-01-09 15:37:44 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const repo = "repo"
|
|
|
|
|
2023-03-23 13:08:11 -05:00
|
|
|
func TestParseStorageErrors(t *testing.T) {
|
2023-11-08 16:37:52 -05:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2023-03-28 12:20:09 -05:00
|
|
|
Convey("ParseStorage", t, func() {
|
2023-01-09 15:37:44 -05:00
|
|
|
imageStore := mocks.MockedImageStore{
|
|
|
|
GetIndexContentFn: func(repo string) ([]byte, error) {
|
|
|
|
return nil, ErrTestError
|
|
|
|
},
|
|
|
|
GetRepositoriesFn: func() ([]string, error) {
|
|
|
|
return []string{"repo1", "repo2"}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
storeController := storage.StoreController{DefaultStore: imageStore}
|
2023-07-18 12:27:26 -05:00
|
|
|
metaDB := mocks.MetaDBMock{}
|
2023-01-09 15:37:44 -05:00
|
|
|
|
|
|
|
// sync repo fail
|
2023-07-18 12:27:26 -05:00
|
|
|
err := meta.ParseStorage(metaDB, storeController, log.NewLogger("debug", ""))
|
2023-11-16 13:39:27 -05:00
|
|
|
So(err, ShouldBeNil)
|
2023-01-09 15:37:44 -05:00
|
|
|
|
|
|
|
Convey("getAllRepos errors", func() {
|
|
|
|
imageStore1 := mocks.MockedImageStore{
|
|
|
|
GetRepositoriesFn: func() ([]string, error) {
|
|
|
|
return []string{"repo1", "repo2"}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
imageStore2 := mocks.MockedImageStore{
|
|
|
|
GetRepositoriesFn: func() ([]string, error) {
|
|
|
|
return nil, ErrTestError
|
|
|
|
},
|
|
|
|
}
|
|
|
|
storeController := storage.StoreController{
|
|
|
|
DefaultStore: imageStore1,
|
2023-05-26 13:08:19 -05:00
|
|
|
SubStore: map[string]storageTypes.ImageStore{
|
2023-01-09 15:37:44 -05:00
|
|
|
"a": imageStore2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
err := meta.ParseStorage(metaDB, storeController, log.NewLogger("debug", ""))
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
2023-11-16 13:39:27 -05:00
|
|
|
|
|
|
|
Convey("metaDB.GetAllRepoNames errors", func() {
|
|
|
|
metaDB.GetAllRepoNamesFn = func() ([]string, error) { return nil, ErrTestError }
|
|
|
|
|
|
|
|
err := meta.ParseStorage(metaDB, storeController, log.NewLogger("debug", ""))
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("metaDB.DeleteRepoMeta errors", func() {
|
|
|
|
imageStore1 := mocks.MockedImageStore{
|
|
|
|
GetRepositoriesFn: func() ([]string, error) { return []string{"repo1", "repo2"}, nil },
|
|
|
|
}
|
|
|
|
storeController := storage.StoreController{DefaultStore: imageStore1}
|
|
|
|
|
|
|
|
metaDB.GetAllRepoNamesFn = func() ([]string, error) { return []string{"deleted"}, nil }
|
|
|
|
metaDB.DeleteRepoMetaFn = func(repo string) error { return ErrTestError }
|
|
|
|
|
|
|
|
err := meta.ParseStorage(metaDB, storeController, log.NewLogger("debug", ""))
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("StatIndex errors", func() {
|
|
|
|
imageStore1 := mocks.MockedImageStore{
|
|
|
|
GetRepositoriesFn: func() ([]string, error) { return []string{"repo1", "repo2"}, nil },
|
|
|
|
}
|
|
|
|
imageStore1.StatIndexFn = func(repo string) (bool, int64, time.Time, error) {
|
|
|
|
return false, 0, time.Time{}, ErrTestError
|
|
|
|
}
|
|
|
|
|
|
|
|
storeController := storage.StoreController{DefaultStore: imageStore1}
|
|
|
|
|
|
|
|
err := meta.ParseStorage(metaDB, storeController, log.NewLogger("debug", ""))
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
2023-01-09 15:37:44 -05:00
|
|
|
})
|
|
|
|
|
2023-03-28 12:20:09 -05:00
|
|
|
Convey("Parse Repo", t, func() {
|
2023-01-09 15:37:44 -05:00
|
|
|
imageStore := mocks.MockedImageStore{}
|
|
|
|
storeController := storage.StoreController{DefaultStore: &imageStore}
|
2023-07-18 12:27:26 -05:00
|
|
|
metaDB := mocks.MetaDBMock{}
|
2023-01-09 15:37:44 -05:00
|
|
|
log := log.NewLogger("debug", "")
|
|
|
|
|
|
|
|
Convey("imageStore.GetIndexContent errors", func() {
|
|
|
|
imageStore.GetIndexContentFn = func(repo string) ([]byte, error) {
|
|
|
|
return nil, ErrTestError
|
|
|
|
}
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
err := meta.ParseRepo("repo", metaDB, storeController, log)
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("json.Unmarshal errors", func() {
|
|
|
|
imageStore.GetIndexContentFn = func(repo string) ([]byte, error) {
|
|
|
|
return []byte("Invalid JSON"), nil
|
|
|
|
}
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
err := meta.ParseRepo("repo", metaDB, storeController, log)
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
2023-11-08 16:37:52 -05:00
|
|
|
Convey("resetRepoReferences errors", func() {
|
|
|
|
imageStore.GetIndexContentFn = func(repo string) ([]byte, error) {
|
|
|
|
return []byte("{}"), nil
|
|
|
|
}
|
|
|
|
metaDB.ResetRepoReferencesFn = func(repo string) error { return ErrTestError }
|
|
|
|
err := meta.ParseRepo("repo", metaDB, storeController, log)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("zcommon.IsReferrersTag", func() {
|
|
|
|
imageStore.GetIndexContentFn = func(repo string) ([]byte, error) {
|
|
|
|
return getIndexBlob(ispec.Index{
|
|
|
|
Manifests: []ispec.Descriptor{
|
|
|
|
{
|
|
|
|
MediaType: ispec.MediaTypeImageManifest,
|
|
|
|
Digest: godigest.FromString("digest"),
|
|
|
|
Annotations: map[string]string{
|
|
|
|
ispec.AnnotationRefName: "sha256-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
err := meta.ParseRepo("repo", metaDB, storeController, log)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("imageStore.GetImageManifest errors", func() {
|
|
|
|
imageStore.GetIndexContentFn = func(repo string) ([]byte, error) {
|
|
|
|
return getIndexBlob(ispec.Index{
|
|
|
|
Manifests: []ispec.Descriptor{
|
|
|
|
{
|
|
|
|
MediaType: ispec.MediaTypeImageManifest,
|
|
|
|
Digest: godigest.FromString("digest"),
|
|
|
|
Annotations: map[string]string{
|
|
|
|
ispec.AnnotationRefName: "tag",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
imageStore.GetImageManifestFn = func(repo, reference string) ([]byte, godigest.Digest, string, error) {
|
|
|
|
return nil, "", "", ErrTestError
|
|
|
|
}
|
|
|
|
err := meta.ParseRepo("repo", metaDB, storeController, log)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
Convey("manifestMetaIsPresent true", func() {
|
|
|
|
indexContent := ispec.Index{
|
|
|
|
Manifests: []ispec.Descriptor{
|
|
|
|
{
|
|
|
|
Digest: godigest.FromString("manifest1"),
|
|
|
|
MediaType: ispec.MediaTypeImageManifest,
|
|
|
|
Annotations: map[string]string{
|
|
|
|
ispec.AnnotationRefName: "tag1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
indexBlob, err := json.Marshal(indexContent)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
imageStore.GetIndexContentFn = func(repo string) ([]byte, error) {
|
|
|
|
return indexBlob, nil
|
|
|
|
}
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
Convey("metaDB.SetRepoReference", func() {
|
2023-11-01 11:16:18 -05:00
|
|
|
metaDB.SetRepoReferenceFn = func(ctx context.Context, repo, reference string, imageMeta mTypes.ImageMeta) error {
|
2023-01-09 15:37:44 -05:00
|
|
|
return ErrTestError
|
|
|
|
}
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
err = meta.ParseRepo("repo", metaDB, storeController, log)
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2023-11-08 16:37:52 -05:00
|
|
|
|
|
|
|
image := CreateRandomImage()
|
|
|
|
|
|
|
|
Convey("SetImageMetaFromInput errors", t, func() {
|
|
|
|
mockImageStore := mocks.MockedImageStore{}
|
|
|
|
mockedMetaDB := mocks.MetaDBMock{}
|
|
|
|
log := log.NewLogger("debug", "")
|
|
|
|
|
|
|
|
Convey("Image Manifest errors", func() {
|
|
|
|
Convey("Get Config blob error", func() {
|
|
|
|
mockImageStore.GetBlobContentFn = func(repo string, digest godigest.Digest) ([]byte, error) {
|
|
|
|
return []byte{}, ErrTestError
|
|
|
|
}
|
|
|
|
|
|
|
|
err := meta.SetImageMetaFromInput(ctx, "repo", "tag", ispec.MediaTypeImageManifest, image.Digest(),
|
|
|
|
image.ManifestDescriptor.Data, mockImageStore, mockedMetaDB, log)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
Convey("Unmarshal config blob error", func() {
|
|
|
|
mockImageStore.GetBlobContentFn = func(repo string, digest godigest.Digest) ([]byte, error) {
|
|
|
|
return []byte("bad-blob"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := meta.SetImageMetaFromInput(ctx, "repo", "tag", ispec.MediaTypeImageManifest, image.Digest(),
|
|
|
|
image.ManifestDescriptor.Data, mockImageStore, mockedMetaDB, log)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
Convey("Is Signature", func() {
|
|
|
|
image := CreateDefaultImage()
|
|
|
|
mediaType := ispec.MediaTypeImageManifest
|
|
|
|
// it has more than 1 layer
|
|
|
|
badNotationSignature := CreateImageWith().RandomLayers(2, 10).EmptyConfig().Subject(image.DescriptorRef()).
|
|
|
|
ArtifactType(zcommon.ArtifactTypeNotation).Build()
|
|
|
|
goodNotationSignature := CreateMockNotationSignature(image.DescriptorRef())
|
|
|
|
|
|
|
|
Convey("GetSignatureLayersInfo errors", func() {
|
|
|
|
err := meta.SetImageMetaFromInput(ctx, "repo", "tag", mediaType, badNotationSignature.Digest(),
|
|
|
|
badNotationSignature.ManifestDescriptor.Data, mockImageStore, mockedMetaDB, log)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
Convey("UpdateSignaturesValidity errors", func() {
|
2023-11-24 03:40:10 -05:00
|
|
|
mockedMetaDB.UpdateSignaturesValidityFn = func(ctx context.Context, repo string,
|
|
|
|
manifestDigest godigest.Digest,
|
|
|
|
) error {
|
2023-11-08 16:37:52 -05:00
|
|
|
return ErrTestError
|
|
|
|
}
|
|
|
|
err := meta.SetImageMetaFromInput(ctx, "repo", "tag", mediaType, goodNotationSignature.Digest(),
|
|
|
|
goodNotationSignature.ManifestDescriptor.Data, mockImageStore, mockedMetaDB, log)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
Convey("Image Index errors", func() {
|
|
|
|
Convey("Unmarshal error", func() {
|
|
|
|
err := meta.SetImageMetaFromInput(ctx, "repo", "tag", ispec.MediaTypeImageIndex, "",
|
|
|
|
[]byte("bad-json"), mockImageStore, mockedMetaDB, log)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func getIndexBlob(index ispec.Index) []byte {
|
|
|
|
index.MediaType = ispec.MediaTypeImageIndex
|
|
|
|
|
|
|
|
blob, err := json.Marshal(index)
|
|
|
|
if err != nil {
|
|
|
|
panic("image index should always be marshable")
|
|
|
|
}
|
|
|
|
|
|
|
|
return blob
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-05-15 04:02:23 -05:00
|
|
|
func TestParseStorageWithBoltDB(t *testing.T) {
|
2023-01-09 15:37:44 -05:00
|
|
|
Convey("Boltdb", t, func() {
|
|
|
|
rootDir := t.TempDir()
|
2023-11-16 13:39:27 -05:00
|
|
|
log := log.NewLogger("debug", "/dev/null")
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
boltDB, err := boltdb.GetBoltDriver(boltdb.DBParameters{
|
2023-03-23 13:08:11 -05:00
|
|
|
RootDir: rootDir,
|
|
|
|
})
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-11-16 13:39:27 -05:00
|
|
|
metaDB, err := boltdb.New(boltDB, log)
|
2023-03-28 12:20:09 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-11-16 13:39:27 -05:00
|
|
|
RunParseStorageTests(rootDir, metaDB, log)
|
2023-03-23 13:08:11 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseStorageDynamoWrapper(t *testing.T) {
|
2023-10-05 06:34:50 -05:00
|
|
|
tskip.SkipDynamo(t)
|
2023-03-23 13:08:11 -05:00
|
|
|
|
|
|
|
Convey("Dynamodb", t, func() {
|
|
|
|
rootDir := t.TempDir()
|
2023-11-16 13:39:27 -05:00
|
|
|
log := log.NewLogger("debug", "/dev/null")
|
2023-03-23 13:08:11 -05:00
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
params := dynamodb.DBDriverParameters{
|
2023-10-30 15:06:04 -05:00
|
|
|
Endpoint: os.Getenv("DYNAMODBMOCK_ENDPOINT"),
|
|
|
|
Region: "us-east-2",
|
|
|
|
RepoMetaTablename: "RepoMetadataTable",
|
|
|
|
RepoBlobsInfoTablename: "RepoBlobsInfoTablename",
|
|
|
|
ImageMetaTablename: "ImageMetaTablename",
|
|
|
|
UserDataTablename: "UserDataTable",
|
|
|
|
APIKeyTablename: "ApiKeyTable",
|
|
|
|
VersionTablename: "Version",
|
2023-03-28 12:20:09 -05:00
|
|
|
}
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
dynamoClient, err := dynamodb.GetDynamoClient(params)
|
2023-03-28 12:20:09 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-11-16 13:39:27 -05:00
|
|
|
dynamoWrapper, err := dynamodb.New(dynamoClient, params, log)
|
2023-03-23 13:08:11 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
err = dynamoWrapper.ResetTable(dynamoWrapper.RepoMetaTablename)
|
2023-03-23 13:08:11 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
err = dynamoWrapper.ResetTable(dynamoWrapper.RepoBlobsTablename)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = dynamoWrapper.ResetTable(dynamoWrapper.ImageMetaTablename)
|
2023-03-23 13:08:11 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-11-16 13:39:27 -05:00
|
|
|
RunParseStorageTests(rootDir, dynamoWrapper, log)
|
2023-03-23 13:08:11 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-11-16 13:39:27 -05:00
|
|
|
func RunParseStorageTests(rootDir string, metaDB mTypes.MetaDB, log log.Logger) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2023-05-15 04:02:23 -05:00
|
|
|
Convey("Test with simple case", func() {
|
2023-09-22 13:51:20 -05:00
|
|
|
imageStore := local.NewImageStore(rootDir, false, false,
|
2023-11-16 13:39:27 -05:00
|
|
|
log, monitoring.NewMetricsServer(false, log), nil, nil)
|
2023-01-09 15:37:44 -05:00
|
|
|
|
|
|
|
storeController := storage.StoreController{DefaultStore: imageStore}
|
|
|
|
manifests := []ispec.Manifest{}
|
|
|
|
for i := 0; i < 3; i++ {
|
2023-11-16 13:39:27 -05:00
|
|
|
image := CreateRandomImage() //nolint:staticcheck
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-11-16 13:39:27 -05:00
|
|
|
manifests = append(manifests, image.Manifest)
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-11-16 13:39:27 -05:00
|
|
|
err := WriteImageToFileSystem(
|
|
|
|
image, repo, fmt.Sprintf("tag%d", i), storeController)
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add fake signature for tag1
|
2023-09-27 13:34:48 -05:00
|
|
|
signatureTag, err := signature.GetCosignSignatureTagForManifest(manifests[1])
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
manifestBlob, err := json.Marshal(manifests[1])
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
signedManifestDigest := godigest.FromBytes(manifestBlob)
|
|
|
|
|
2024-01-17 13:20:07 -05:00
|
|
|
image := CreateRandomImage()
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2024-01-17 13:20:07 -05:00
|
|
|
err = WriteImageToFileSystem(image, repo, signatureTag, storeController)
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
// remove tag2 from index.json
|
|
|
|
indexPath := path.Join(rootDir, repo, "index.json")
|
|
|
|
indexFile, err := os.Open(indexPath)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
buf, err := io.ReadAll(indexFile)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
var index ispec.Index
|
|
|
|
if err = json.Unmarshal(buf, &index); err == nil {
|
|
|
|
for _, manifest := range index.Manifests {
|
|
|
|
if val, ok := manifest.Annotations[ispec.AnnotationRefName]; ok && val == "tag2" {
|
|
|
|
delete(manifest.Annotations, ispec.AnnotationRefName)
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf, err = json.Marshal(index)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = os.WriteFile(indexPath, buf, 0o600)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-11-16 13:39:27 -05:00
|
|
|
err = meta.ParseStorage(metaDB, storeController, log) //nolint: contextcheck
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-11-16 13:39:27 -05:00
|
|
|
repos, err := metaDB.GetMultipleRepoMeta(ctx,
|
2023-10-30 15:06:04 -05:00
|
|
|
func(repoMeta mTypes.RepoMeta) bool { return true })
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(len(repos), ShouldEqual, 1)
|
|
|
|
So(len(repos[0].Tags), ShouldEqual, 2)
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
for tag, descriptor := range repos[0].Tags {
|
|
|
|
imageManifestData, err := metaDB.GetFullImageMeta(ctx, repo, tag)
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
if descriptor.Digest == signedManifestDigest.String() {
|
2023-10-30 15:06:04 -05:00
|
|
|
So(imageManifestData.Signatures, ShouldNotBeEmpty)
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-05-15 04:02:23 -05:00
|
|
|
Convey("Accept orphan signatures", func() {
|
2023-09-22 13:51:20 -05:00
|
|
|
imageStore := local.NewImageStore(rootDir, false, false,
|
2023-11-16 13:39:27 -05:00
|
|
|
log, monitoring.NewMetricsServer(false, log), nil, nil)
|
2023-01-09 15:37:44 -05:00
|
|
|
|
|
|
|
storeController := storage.StoreController{DefaultStore: imageStore}
|
2024-01-17 13:20:07 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
// add an image
|
2024-01-17 13:20:07 -05:00
|
|
|
image := CreateRandomImage()
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2024-01-17 13:20:07 -05:00
|
|
|
err := WriteImageToFileSystem(image, repo, "tag1", storeController)
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
// add mock cosign signature without pushing the signed image
|
2024-01-17 13:20:07 -05:00
|
|
|
image = CreateRandomImage()
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-09-27 13:34:48 -05:00
|
|
|
signatureTag, err := signature.GetCosignSignatureTagForManifest(image.Manifest)
|
2023-05-15 04:02:23 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2024-01-17 13:20:07 -05:00
|
|
|
missingImageDigest := image.ManifestDescriptor.Digest
|
2023-01-09 15:37:44 -05:00
|
|
|
|
|
|
|
// get the body of the signature
|
2024-01-17 13:20:07 -05:00
|
|
|
signature := CreateRandomImage()
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2024-01-17 13:20:07 -05:00
|
|
|
err = WriteImageToFileSystem(signature, repo, signatureTag, storeController)
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-11-16 13:39:27 -05:00
|
|
|
err = meta.ParseStorage(metaDB, storeController, log) //nolint: contextcheck
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-11-16 13:39:27 -05:00
|
|
|
repos, err := metaDB.GetMultipleRepoMeta(ctx,
|
2023-10-30 15:06:04 -05:00
|
|
|
func(repoMeta mTypes.RepoMeta) bool { return true })
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-05-15 04:02:23 -05:00
|
|
|
for _, desc := range repos[0].Tags {
|
|
|
|
So(desc.Digest, ShouldNotResemble, missingImageDigest.String())
|
|
|
|
}
|
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
So(len(repos), ShouldEqual, 1)
|
|
|
|
So(repos[0].Tags, ShouldContainKey, "tag1")
|
|
|
|
So(repos[0].Tags, ShouldNotContainKey, signatureTag)
|
2023-05-15 04:02:23 -05:00
|
|
|
So(repos[0].Signatures, ShouldContainKey, missingImageDigest.String())
|
2023-01-09 15:37:44 -05:00
|
|
|
})
|
|
|
|
|
2023-03-23 13:08:11 -05:00
|
|
|
Convey("Check statistics after load", func() {
|
2023-09-22 13:51:20 -05:00
|
|
|
imageStore := local.NewImageStore(rootDir, false, false,
|
2023-11-16 13:39:27 -05:00
|
|
|
log, monitoring.NewMetricsServer(false, log), nil, nil)
|
2023-01-09 15:37:44 -05:00
|
|
|
|
|
|
|
storeController := storage.StoreController{DefaultStore: imageStore}
|
2023-03-23 13:08:11 -05:00
|
|
|
// add an image
|
2023-10-30 15:06:04 -05:00
|
|
|
image := CreateRandomImage() //nolint:staticcheck
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
err := WriteImageToFileSystem(image, repo, "tag", storeController)
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-11-16 13:39:27 -05:00
|
|
|
err = metaDB.SetRepoReference(ctx, repo, "tag", image.AsImageMeta())
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
err = metaDB.IncrementRepoStars(repo)
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
2023-11-01 11:16:18 -05:00
|
|
|
err = metaDB.UpdateStatsOnDownload(repo, "tag")
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
2023-11-01 11:16:18 -05:00
|
|
|
err = metaDB.UpdateStatsOnDownload(repo, "tag")
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
2023-11-01 11:16:18 -05:00
|
|
|
err = metaDB.UpdateStatsOnDownload(repo, "tag")
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-11-16 13:39:27 -05:00
|
|
|
repoMeta, err := metaDB.GetRepoMeta(ctx, repo)
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
So(repoMeta.Statistics[image.DigestStr()].DownloadCount, ShouldEqual, 3)
|
|
|
|
So(repoMeta.StarCount, ShouldEqual, 1)
|
2023-11-01 11:16:18 -05:00
|
|
|
So(time.Now(), ShouldHappenAfter, repoMeta.Statistics[image.DigestStr()].LastPullTimestamp)
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-11-16 13:39:27 -05:00
|
|
|
err = meta.ParseStorage(metaDB, storeController, log) //nolint: contextcheck
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-11-16 13:39:27 -05:00
|
|
|
repoMeta, err = metaDB.GetRepoMeta(ctx, repo)
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
So(repoMeta.Statistics[image.DigestStr()].DownloadCount, ShouldEqual, 3)
|
|
|
|
So(repoMeta.StarCount, ShouldEqual, 1)
|
2023-03-10 13:37:29 -05:00
|
|
|
})
|
2023-11-09 12:32:56 -05:00
|
|
|
|
|
|
|
// make sure pushTimestamp is always populated to not interfere with retention logic
|
|
|
|
Convey("Always update pushTimestamp if its value is 0(time.Time{})", func() {
|
|
|
|
imageStore := local.NewImageStore(rootDir, false, false,
|
2023-11-16 13:39:27 -05:00
|
|
|
log, monitoring.NewMetricsServer(false, log), nil, nil)
|
2023-11-09 12:32:56 -05:00
|
|
|
|
|
|
|
storeController := storage.StoreController{DefaultStore: imageStore}
|
|
|
|
// add an image
|
|
|
|
image := CreateRandomImage() //nolint:staticcheck
|
|
|
|
|
|
|
|
err := WriteImageToFileSystem(image, repo, "tag", storeController)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-11-16 13:39:27 -05:00
|
|
|
err = metaDB.SetRepoReference(ctx, repo, "tag", image.AsImageMeta())
|
2023-11-09 12:32:56 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = metaDB.UpdateStatsOnDownload(repo, "tag")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-11-16 13:39:27 -05:00
|
|
|
repoMeta, err := metaDB.GetRepoMeta(ctx, repo)
|
2023-11-09 12:32:56 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(repoMeta.Statistics[image.DigestStr()].DownloadCount, ShouldEqual, 1)
|
|
|
|
So(time.Now(), ShouldHappenAfter, repoMeta.Statistics[image.DigestStr()].LastPullTimestamp)
|
|
|
|
So(time.Now(), ShouldHappenAfter, repoMeta.Statistics[image.DigestStr()].PushTimestamp)
|
|
|
|
|
|
|
|
// update statistics (simulate that a metaDB has statistics, but pushTimestamp is 0)
|
|
|
|
stats := repoMeta.Statistics[image.DigestStr()]
|
|
|
|
oldPushTimestamp := stats.PushTimestamp
|
|
|
|
stats.PushTimestamp = time.Time{}
|
|
|
|
repoMeta.Statistics[image.DigestStr()] = stats
|
|
|
|
|
|
|
|
err = metaDB.SetRepoMeta(repo, repoMeta)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
// metaDB should detect that pushTimestamp is 0 and update it.
|
2023-11-16 13:39:27 -05:00
|
|
|
err = meta.ParseStorage(metaDB, storeController, log) //nolint: contextcheck
|
2023-11-09 12:32:56 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-11-16 13:39:27 -05:00
|
|
|
repoMeta, err = metaDB.GetRepoMeta(ctx, repo)
|
2023-11-09 12:32:56 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(repoMeta.Statistics[image.DigestStr()].DownloadCount, ShouldEqual, 1)
|
|
|
|
So(repoMeta.DownloadCount, ShouldEqual, 1)
|
|
|
|
So(repoMeta.Statistics[image.DigestStr()].PushTimestamp, ShouldHappenAfter, oldPushTimestamp)
|
|
|
|
})
|
2023-11-16 13:39:27 -05:00
|
|
|
|
|
|
|
Convey("Parse 2 times and check correct update of the metaDB for modified and deleted repos", func() {
|
|
|
|
storeController := ociutils.GetDefaultStoreController(rootDir, log)
|
|
|
|
|
|
|
|
notModifiedRepo := "not-modified-repo"
|
|
|
|
modifiedAddImageRepo := "modified-add-image-repo"
|
|
|
|
modifiedRemoveImageRepo := "modified-remove-image-repo"
|
|
|
|
deletedRepo := "deleted-repo"
|
|
|
|
addedRepo := "added-repo"
|
|
|
|
tag := "tag"
|
|
|
|
tag2 := "tag2"
|
|
|
|
newTag := "newTag"
|
|
|
|
|
|
|
|
image := CreateRandomImage()
|
|
|
|
|
|
|
|
err := WriteImageToFileSystem(image, notModifiedRepo, tag, storeController)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = WriteImageToFileSystem(image, modifiedAddImageRepo, tag, storeController)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = WriteImageToFileSystem(image, modifiedRemoveImageRepo, tag, storeController)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = WriteImageToFileSystem(image, modifiedRemoveImageRepo, tag2, storeController)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = WriteImageToFileSystem(image, deletedRepo, tag, storeController)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = meta.ParseStorage(metaDB, storeController, log) //nolint: contextcheck
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
repoMetaList, err := metaDB.SearchRepos(ctx, "")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(repoMetaList), ShouldEqual, 4)
|
|
|
|
|
|
|
|
repoNames := tcommon.AccumulateField(repoMetaList, func(rm mTypes.RepoMeta) string { return rm.Name })
|
|
|
|
So(repoNames, ShouldContain, notModifiedRepo)
|
|
|
|
So(repoNames, ShouldContain, modifiedAddImageRepo)
|
|
|
|
So(repoNames, ShouldContain, modifiedRemoveImageRepo)
|
|
|
|
So(repoNames, ShouldContain, deletedRepo)
|
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
|
|
// Update the storage
|
|
|
|
|
|
|
|
err = WriteImageToFileSystem(image, modifiedAddImageRepo, newTag, storeController)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = storeController.GetDefaultImageStore().DeleteImageManifest(modifiedRemoveImageRepo, tag2, false)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = os.RemoveAll(filepath.Join(rootDir, deletedRepo))
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = WriteImageToFileSystem(image, addedRepo, tag, storeController)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
// Parse again
|
|
|
|
err = meta.ParseStorage(metaDB, storeController, log) //nolint: contextcheck
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
repoMetaList, err = metaDB.SearchRepos(ctx, "")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(repoMetaList), ShouldEqual, 4)
|
|
|
|
|
|
|
|
repoNames = tcommon.AccumulateField(repoMetaList, func(rm mTypes.RepoMeta) string { return rm.Name })
|
|
|
|
So(repoNames, ShouldContain, notModifiedRepo)
|
|
|
|
So(repoNames, ShouldContain, modifiedAddImageRepo)
|
|
|
|
So(repoNames, ShouldContain, modifiedRemoveImageRepo)
|
|
|
|
So(repoNames, ShouldNotContain, deletedRepo)
|
|
|
|
So(repoNames, ShouldContain, addedRepo)
|
|
|
|
|
|
|
|
repoMeta, err := metaDB.GetRepoMeta(ctx, modifiedAddImageRepo)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(repoMeta.Tags, ShouldContainKey, tag)
|
|
|
|
So(repoMeta.Tags, ShouldContainKey, newTag)
|
|
|
|
|
|
|
|
repoMeta, err = metaDB.GetRepoMeta(ctx, modifiedRemoveImageRepo)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(repoMeta.Tags, ShouldContainKey, tag)
|
|
|
|
So(repoMeta.Tags, ShouldNotContainKey, tag2)
|
|
|
|
})
|
2023-03-10 13:37:29 -05:00
|
|
|
}
|
|
|
|
|
2023-05-24 11:46:16 -05:00
|
|
|
func TestGetSignatureLayersInfo(t *testing.T) {
|
|
|
|
Convey("wrong signature type", t, func() {
|
2023-07-18 12:27:26 -05:00
|
|
|
layers, err := meta.GetSignatureLayersInfo("repo", "tag", "123", "wrong signature type", []byte{},
|
2023-05-24 11:46:16 -05:00
|
|
|
nil, log.NewLogger("debug", ""))
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(layers, ShouldBeEmpty)
|
|
|
|
})
|
|
|
|
|
2023-10-12 20:45:20 -05:00
|
|
|
Convey("notation index", t, func() {
|
|
|
|
notationIndex := ispec.Index{
|
|
|
|
MediaType: ispec.MediaTypeImageIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
notationIndexBlob, err := json.Marshal(notationIndex)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
layers, err := meta.GetSignatureLayersInfo("repo", "tag", "123", zcommon.NotationSignature, notationIndexBlob,
|
|
|
|
nil, log.NewLogger("debug", ""))
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(layers, ShouldBeEmpty)
|
|
|
|
})
|
|
|
|
|
2023-11-08 16:37:52 -05:00
|
|
|
Convey("GetBlobContent errors", t, func() {
|
|
|
|
mockImageStore := mocks.MockedImageStore{}
|
|
|
|
mockImageStore.GetBlobContentFn = func(repo string, digest godigest.Digest) ([]byte, error) {
|
|
|
|
return nil, ErrTestError
|
|
|
|
}
|
|
|
|
image := CreateRandomImage()
|
|
|
|
|
|
|
|
layers, err := meta.GetSignatureLayersInfo("repo", "tag", "123", zcommon.CosignSignature,
|
|
|
|
image.ManifestDescriptor.Data, mockImageStore, log.NewLogger("debug", ""))
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(layers, ShouldBeEmpty)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("notation len(manifestContent.Layers) != 1", t, func() {
|
|
|
|
mockImageStore := mocks.MockedImageStore{}
|
|
|
|
image := CreateImageWith().RandomLayers(3, 10).RandomConfig().Build()
|
|
|
|
|
|
|
|
layers, err := meta.GetSignatureLayersInfo("repo", "tag", "123", zcommon.NotationSignature,
|
|
|
|
image.ManifestDescriptor.Data, mockImageStore, log.NewLogger("debug", ""))
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(layers, ShouldBeEmpty)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("notation GetBlobContent errors", t, func() {
|
|
|
|
mockImageStore := mocks.MockedImageStore{}
|
|
|
|
mockImageStore.GetBlobContentFn = func(repo string, digest godigest.Digest) ([]byte, error) {
|
|
|
|
return nil, ErrTestError
|
|
|
|
}
|
|
|
|
image := CreateImageWith().RandomLayers(1, 10).RandomConfig().Build()
|
|
|
|
|
|
|
|
layers, err := meta.GetSignatureLayersInfo("repo", "tag", "123", zcommon.NotationSignature,
|
|
|
|
image.ManifestDescriptor.Data, mockImageStore, log.NewLogger("debug", ""))
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(layers, ShouldBeEmpty)
|
|
|
|
})
|
|
|
|
|
2023-05-24 11:46:16 -05:00
|
|
|
Convey("error while unmarshaling manifest content", t, func() {
|
2023-08-19 00:52:03 -05:00
|
|
|
_, err := meta.GetSignatureLayersInfo("repo", "tag", "123", zcommon.CosignSignature, []byte("bad manifest"),
|
2023-05-24 11:46:16 -05:00
|
|
|
nil, log.NewLogger("debug", ""))
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2023-08-19 00:52:03 -05:00
|
|
|
_, err = meta.GetSignatureLayersInfo("repo", "tag", "123", zcommon.NotationSignature, []byte("bad manifest"),
|
2023-05-24 11:46:16 -05:00
|
|
|
nil, log.NewLogger("debug", ""))
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
}
|