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"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
godigest "github.com/opencontainers/go-digest"
|
|
|
|
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
|
|
|
|
zerr "zotregistry.io/zot/errors"
|
2023-08-19 00:52:03 -05:00
|
|
|
zcommon "zotregistry.io/zot/pkg/common"
|
2023-01-09 15:37:44 -05:00
|
|
|
"zotregistry.io/zot/pkg/extensions/monitoring"
|
|
|
|
"zotregistry.io/zot/pkg/log"
|
2023-07-18 12:27:26 -05:00
|
|
|
"zotregistry.io/zot/pkg/meta"
|
|
|
|
"zotregistry.io/zot/pkg/meta/boltdb"
|
|
|
|
"zotregistry.io/zot/pkg/meta/dynamodb"
|
|
|
|
mTypes "zotregistry.io/zot/pkg/meta/types"
|
2023-01-09 15:37:44 -05:00
|
|
|
"zotregistry.io/zot/pkg/storage"
|
|
|
|
"zotregistry.io/zot/pkg/storage/local"
|
2023-05-26 13:08:19 -05:00
|
|
|
storageTypes "zotregistry.io/zot/pkg/storage/types"
|
2023-01-09 15:37:44 -05:00
|
|
|
"zotregistry.io/zot/pkg/test"
|
|
|
|
"zotregistry.io/zot/pkg/test/mocks"
|
|
|
|
)
|
|
|
|
|
|
|
|
const repo = "repo"
|
|
|
|
|
2023-03-23 13:08:11 -05:00
|
|
|
func TestParseStorageErrors(t *testing.T) {
|
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-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
|
|
|
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-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)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("resetRepoMetaTags errors", func() {
|
|
|
|
imageStore.GetIndexContentFn = func(repo string) ([]byte, error) {
|
|
|
|
return []byte("{}"), nil
|
|
|
|
}
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
Convey("metaDB.GetRepoMeta errors", func() {
|
|
|
|
metaDB.GetRepoMetaFn = func(repo string) (mTypes.RepoMetadata, error) {
|
|
|
|
return mTypes.RepoMetadata{}, ErrTestError
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
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("isManifestMetaPresent errors", 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.GetManifestMeta errors", func() {
|
|
|
|
metaDB.GetManifestMetaFn = func(repo string, manifestDigest godigest.Digest) (mTypes.ManifestMetadata, error) {
|
|
|
|
return mTypes.ManifestMetadata{}, ErrTestError
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
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("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() {
|
|
|
|
metaDB.SetRepoReferenceFn = func(repo, tag string, manifestDigest godigest.Digest, mediaType string) 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)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("manifestMetaIsPresent false", 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
|
|
|
metaDB.GetManifestMetaFn = func(repo string, manifestDigest godigest.Digest) (mTypes.ManifestMetadata, error) {
|
|
|
|
return mTypes.ManifestMetadata{}, zerr.ErrManifestMetaNotFound
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Convey("GetImageManifest errors", func() {
|
|
|
|
imageStore.GetImageManifestFn = func(repo, reference string) ([]byte, godigest.Digest, string, 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("CheckIsImageSignature errors", func() {
|
|
|
|
// CheckIsImageSignature will fail because of a invalid json
|
|
|
|
imageStore.GetImageManifestFn = func(repo, reference string) ([]byte, godigest.Digest, string, 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)
|
|
|
|
})
|
|
|
|
Convey("CheckIsImageSignature -> not signature", func() {
|
|
|
|
manifestContent := ispec.Manifest{}
|
|
|
|
manifestBlob, err := json.Marshal(manifestContent)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
imageStore.GetImageManifestFn = func(repo, reference string) ([]byte, godigest.Digest, string, error) {
|
|
|
|
return manifestBlob, "", "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
Convey("imgStore.GetBlobContent errors", func() {
|
|
|
|
imageStore.GetBlobContentFn = func(repo string, digest godigest.Digest) ([]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("CheckIsImageSignature -> is signature", func() {
|
2023-05-10 12:15:33 -05:00
|
|
|
manifestContent := ispec.Manifest{
|
2023-03-10 13:37:29 -05:00
|
|
|
Subject: &ispec.Descriptor{
|
2023-01-09 15:37:44 -05:00
|
|
|
Digest: "123",
|
|
|
|
},
|
2023-03-10 13:37:29 -05:00
|
|
|
ArtifactType: "application/vnd.cncf.notary.signature",
|
2023-05-24 11:46:16 -05:00
|
|
|
Layers: []ispec.Descriptor{{MediaType: ispec.MediaTypeImageLayer}},
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
2023-03-10 13:37:29 -05:00
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
manifestBlob, err := json.Marshal(manifestContent)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
imageStore.GetImageManifestFn = func(repo, reference string) ([]byte, godigest.Digest, string, error) {
|
|
|
|
return manifestBlob, "", "", nil
|
|
|
|
}
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
metaDB.AddManifestSignatureFn = func(repo string, signedManifestDigest godigest.Digest,
|
|
|
|
sm mTypes.SignatureMetadata,
|
2023-01-09 15:37:44 -05:00
|
|
|
) error {
|
|
|
|
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-05-24 11:46:16 -05:00
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
metaDB.AddManifestSignatureFn = func(repo string, signedManifestDigest godigest.Digest,
|
|
|
|
sm mTypes.SignatureMetadata,
|
2023-05-24 11:46:16 -05:00
|
|
|
) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
metaDB.UpdateSignaturesValidityFn = func(repo string, signedManifestDigest godigest.Digest,
|
2023-05-24 11:46:16 -05:00
|
|
|
) error {
|
|
|
|
return ErrTestError
|
|
|
|
}
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
err = meta.ParseRepo("repo", metaDB, storeController, log)
|
2023-05-24 11:46:16 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("GetSignatureLayersInfo errors", func() {
|
|
|
|
// get notation signature layers info
|
|
|
|
badNotationManifestContent := ispec.Manifest{
|
|
|
|
Subject: &ispec.Descriptor{
|
|
|
|
Digest: "123",
|
|
|
|
},
|
|
|
|
ArtifactType: "application/vnd.cncf.notary.signature",
|
|
|
|
}
|
|
|
|
|
|
|
|
badNotationManifestBlob, err := json.Marshal(badNotationManifestContent)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
imageStore.GetImageManifestFn = func(repo, reference string) ([]byte, godigest.Digest, string, error) {
|
|
|
|
return badNotationManifestBlob, "", "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// wrong number of layers of notation manifest
|
2023-07-18 12:27:26 -05:00
|
|
|
err = meta.ParseRepo("repo", metaDB, storeController, log)
|
2023-05-24 11:46:16 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
|
|
|
notationManifestContent := ispec.Manifest{
|
|
|
|
Subject: &ispec.Descriptor{
|
|
|
|
Digest: "123",
|
|
|
|
},
|
|
|
|
ArtifactType: "application/vnd.cncf.notary.signature",
|
|
|
|
Layers: []ispec.Descriptor{{MediaType: ispec.MediaTypeImageLayer}},
|
|
|
|
}
|
|
|
|
|
|
|
|
notationManifestBlob, err := json.Marshal(notationManifestContent)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
imageStore.GetImageManifestFn = func(repo, reference string) ([]byte, godigest.Digest, string, error) {
|
|
|
|
return notationManifestBlob, "", "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
imageStore.GetBlobContentFn = func(repo string, digest godigest.Digest) ([]byte, error) {
|
|
|
|
return []byte{}, ErrTestError
|
|
|
|
}
|
|
|
|
|
|
|
|
// unable to get layer content
|
2023-07-18 12:27:26 -05:00
|
|
|
err = meta.ParseRepo("repo", metaDB, storeController, log)
|
2023-05-24 11:46:16 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2023-07-28 09:53:46 -05:00
|
|
|
_, _, cosignManifestContent, _ := test.GetRandomImageComponents(10) //nolint:staticcheck
|
|
|
|
_, _, signedManifest, _ := test.GetRandomImageComponents(10) //nolint:staticcheck
|
2023-05-24 11:46:16 -05:00
|
|
|
signatureTag, err := test.GetCosignSignatureTagForManifest(signedManifest)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
cosignManifestContent.Annotations = map[string]string{ispec.AnnotationRefName: signatureTag}
|
|
|
|
|
|
|
|
cosignManifestBlob, err := json.Marshal(cosignManifestContent)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
imageStore.GetImageManifestFn = func(repo, reference string) ([]byte, godigest.Digest, string, error) {
|
|
|
|
return cosignManifestBlob, "", "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
indexContent := ispec.Index{
|
|
|
|
Manifests: []ispec.Descriptor{
|
|
|
|
{
|
|
|
|
Digest: godigest.FromString("cosignSig"),
|
|
|
|
MediaType: ispec.MediaTypeImageManifest,
|
|
|
|
Annotations: map[string]string{
|
|
|
|
ispec.AnnotationRefName: signatureTag,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
indexBlob, err := json.Marshal(indexContent)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
imageStore.GetIndexContentFn = func(repo string) ([]byte, error) {
|
|
|
|
return indexBlob, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// unable to get layer content
|
2023-07-18 12:27:26 -05:00
|
|
|
err = meta.ParseRepo("repo", metaDB, storeController, log)
|
2023-05-24 11:46:16 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
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-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-07-18 12:27:26 -05:00
|
|
|
metaDB, err := boltdb.New(boltDB, log.NewLogger("debug", ""))
|
2023-03-28 12:20:09 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
RunParseStorageTests(rootDir, metaDB)
|
2023-03-23 13:08:11 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseStorageDynamoWrapper(t *testing.T) {
|
|
|
|
skipIt(t)
|
|
|
|
|
|
|
|
Convey("Dynamodb", t, func() {
|
|
|
|
rootDir := t.TempDir()
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
params := dynamodb.DBDriverParameters{
|
2023-03-23 13:08:11 -05:00
|
|
|
Endpoint: os.Getenv("DYNAMODBMOCK_ENDPOINT"),
|
|
|
|
Region: "us-east-2",
|
|
|
|
RepoMetaTablename: "RepoMetadataTable",
|
|
|
|
ManifestDataTablename: "ManifestDataTable",
|
|
|
|
IndexDataTablename: "IndexDataTable",
|
2023-04-24 13:13:15 -05:00
|
|
|
UserDataTablename: "UserDataTable",
|
2023-07-07 11:27:10 -05:00
|
|
|
APIKeyTablename: "ApiKeyTable",
|
2023-03-23 13:08:11 -05:00
|
|
|
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-07-18 12:27:26 -05:00
|
|
|
dynamoWrapper, err := dynamodb.New(dynamoClient, params, log.NewLogger("debug", ""))
|
2023-03-23 13:08:11 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = dynamoWrapper.ResetManifestDataTable()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = dynamoWrapper.ResetRepoMetaTable()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
RunParseStorageTests(rootDir, dynamoWrapper)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
func RunParseStorageTests(rootDir string, metaDB mTypes.MetaDB) {
|
2023-05-15 04:02:23 -05:00
|
|
|
Convey("Test with simple case", func() {
|
2023-09-01 12:54:39 -05:00
|
|
|
imageStore := local.NewImageStore(rootDir, false, false, 0, 0, false, false,
|
2023-01-09 15:37:44 -05:00
|
|
|
log.NewLogger("debug", ""), monitoring.NewMetricsServer(false, log.NewLogger("debug", "")), nil, nil)
|
|
|
|
|
|
|
|
storeController := storage.StoreController{DefaultStore: imageStore}
|
|
|
|
manifests := []ispec.Manifest{}
|
|
|
|
for i := 0; i < 3; i++ {
|
2023-07-28 09:53:46 -05:00
|
|
|
config, layers, manifest, err := test.GetRandomImageComponents(100) //nolint:staticcheck
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
manifests = append(manifests, manifest)
|
|
|
|
|
|
|
|
err = test.WriteImageToFileSystem(
|
|
|
|
test.Image{
|
2023-07-28 09:53:46 -05:00
|
|
|
Config: config,
|
|
|
|
Layers: layers,
|
|
|
|
Manifest: manifest,
|
|
|
|
}, repo, fmt.Sprintf("tag%d", i), storeController)
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add fake signature for tag1
|
|
|
|
signatureTag, err := test.GetCosignSignatureTagForManifest(manifests[1])
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
manifestBlob, err := json.Marshal(manifests[1])
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
signedManifestDigest := godigest.FromBytes(manifestBlob)
|
|
|
|
|
2023-07-28 09:53:46 -05:00
|
|
|
config, layers, manifest, err := test.GetRandomImageComponents(100) //nolint:staticcheck
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = test.WriteImageToFileSystem(
|
|
|
|
test.Image{
|
2023-07-28 09:53:46 -05:00
|
|
|
Config: config,
|
|
|
|
Layers: layers,
|
|
|
|
Manifest: manifest,
|
|
|
|
}, 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-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, ShouldBeNil)
|
|
|
|
|
2023-07-31 14:16:09 -05:00
|
|
|
repos, err := metaDB.GetMultipleRepoMeta(context.Background(),
|
|
|
|
func(repoMeta mTypes.RepoMetadata) 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)
|
|
|
|
|
|
|
|
for _, descriptor := range repos[0].Tags {
|
2023-07-18 12:27:26 -05:00
|
|
|
manifestMeta, err := metaDB.GetManifestMeta(repo, godigest.Digest(descriptor.Digest))
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(manifestMeta.ManifestBlob, ShouldNotBeNil)
|
|
|
|
So(manifestMeta.ConfigBlob, ShouldNotBeNil)
|
|
|
|
|
|
|
|
if descriptor.Digest == signedManifestDigest.String() {
|
2023-03-23 13:08:11 -05:00
|
|
|
So(manifestMeta.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-01 12:54:39 -05:00
|
|
|
imageStore := local.NewImageStore(rootDir, false, false, 0, 0, false, false,
|
2023-01-09 15:37:44 -05:00
|
|
|
log.NewLogger("debug", ""), monitoring.NewMetricsServer(false, log.NewLogger("debug", "")), nil, nil)
|
|
|
|
|
|
|
|
storeController := storage.StoreController{DefaultStore: imageStore}
|
|
|
|
// add an image
|
2023-07-28 09:53:46 -05:00
|
|
|
config, layers, manifest, err := test.GetRandomImageComponents(100) //nolint:staticcheck
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = test.WriteImageToFileSystem(
|
|
|
|
test.Image{
|
2023-07-28 09:53:46 -05:00
|
|
|
Config: config,
|
|
|
|
Layers: layers,
|
|
|
|
Manifest: manifest,
|
|
|
|
}, repo, "tag1", storeController)
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
// add mock cosign signature without pushing the signed image
|
2023-07-28 09:53:46 -05:00
|
|
|
image, err := test.GetRandomImage() //nolint:staticcheck
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-05-15 04:02:23 -05:00
|
|
|
signatureTag, err := test.GetCosignSignatureTagForManifest(image.Manifest)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-07-26 05:08:04 -05:00
|
|
|
missingImageDigest := image.Digest()
|
2023-01-09 15:37:44 -05:00
|
|
|
|
|
|
|
// get the body of the signature
|
2023-07-28 09:53:46 -05:00
|
|
|
config, layers, manifest, err = test.GetRandomImageComponents(100) //nolint:staticcheck
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
err = test.WriteImageToFileSystem(
|
|
|
|
test.Image{
|
2023-07-28 09:53:46 -05:00
|
|
|
Config: config,
|
|
|
|
Layers: layers,
|
|
|
|
Manifest: manifest,
|
|
|
|
}, repo, signatureTag, storeController)
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
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, ShouldBeNil)
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
repos, err := metaDB.GetMultipleRepoMeta(
|
2023-01-09 15:37:44 -05:00
|
|
|
context.Background(),
|
2023-07-18 12:27:26 -05:00
|
|
|
func(repoMeta mTypes.RepoMetadata) 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-01 12:54:39 -05:00
|
|
|
imageStore := local.NewImageStore(rootDir, false, false, 0, 0, false, false,
|
2023-01-09 15:37:44 -05:00
|
|
|
log.NewLogger("debug", ""), monitoring.NewMetricsServer(false, log.NewLogger("debug", "")), nil, nil)
|
|
|
|
|
|
|
|
storeController := storage.StoreController{DefaultStore: imageStore}
|
2023-03-23 13:08:11 -05:00
|
|
|
// add an image
|
2023-07-28 09:53:46 -05:00
|
|
|
image, err := test.GetRandomImage() //nolint:staticcheck
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-07-26 05:08:04 -05:00
|
|
|
manifestDigest := image.Digest()
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-07-28 09:53:46 -05:00
|
|
|
err = test.WriteImageToFileSystem(image, repo, "tag", storeController)
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
err = metaDB.SetRepoReference(repo, "tag", manifestDigest, ispec.MediaTypeImageManifest)
|
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-07-18 12:27:26 -05:00
|
|
|
err = metaDB.IncrementImageDownloads(repo, "tag")
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
2023-07-18 12:27:26 -05:00
|
|
|
err = metaDB.IncrementImageDownloads(repo, "tag")
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
2023-07-18 12:27:26 -05:00
|
|
|
err = metaDB.IncrementImageDownloads(repo, "tag")
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
repoMeta, err := metaDB.GetRepoMeta(repo)
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-03-23 13:08:11 -05:00
|
|
|
So(repoMeta.Statistics[manifestDigest.String()].DownloadCount, ShouldEqual, 3)
|
|
|
|
So(repoMeta.Stars, ShouldEqual, 1)
|
2023-01-09 15:37:44 -05:00
|
|
|
|
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, ShouldBeNil)
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
repoMeta, err = metaDB.GetRepoMeta(repo)
|
2023-01-09 15:37:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2023-03-23 13:08:11 -05:00
|
|
|
So(repoMeta.Statistics[manifestDigest.String()].DownloadCount, ShouldEqual, 3)
|
|
|
|
So(repoMeta.Stars, ShouldEqual, 1)
|
2023-01-09 15:37:44 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
func TestGetReferredInfo(t *testing.T) {
|
|
|
|
Convey("GetReferredInfo error", t, func() {
|
|
|
|
_, _, _, err := meta.GetReferredInfo([]byte("bad json"), "digest", ispec.MediaTypeImageManifest)
|
2023-03-10 13:37:29 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
2023-07-05 11:42:16 -05:00
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
_, _, _, err = meta.GetReferredInfo([]byte("bad json"), "digest", ispec.MediaTypeImageIndex)
|
2023-07-05 11:42:16 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
2023-03-10 13:37:29 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
func skipIt(t *testing.T) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
if os.Getenv("S3MOCK_ENDPOINT") == "" {
|
|
|
|
t.Skip("Skipping testing without AWS S3 mock server")
|
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|