mirror of
https://github.com/project-zot/zot.git
synced 2026-06-16 04:17:55 +08:00
refactor(scrub): replace umoci logic in scrub implementation (#1845)
- implement scrub also for S3 storage by replacing umoci - change scrub implementation for ImageIndex - take the `Subject` into consideration when running scrub - remove test code relying on the umoci library. Since we started relying on images in test/data, and we create our own images using go code we can obtain digests by other means. (cherry picked from commit 489d4e2d23c1b4e48799283f8281024bbef6123f) Signed-off-by: Andrei Aaron <aaaron@luxoft.com> Signed-off-by: Andreea-Lupu <andreealupu1470@yahoo.com>
This commit is contained in:
@@ -35,7 +35,6 @@ import (
|
||||
godigest "github.com/opencontainers/go-digest"
|
||||
"github.com/opencontainers/image-spec/specs-go"
|
||||
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
"github.com/opencontainers/umoci"
|
||||
"github.com/phayes/freeport"
|
||||
"github.com/project-zot/mockoidc"
|
||||
"github.com/sigstore/cosign/v2/cmd/cosign/cli/generate"
|
||||
@@ -72,29 +71,6 @@ var (
|
||||
|
||||
var NotationPathLock = new(sync.Mutex) //nolint: gochecknoglobals
|
||||
|
||||
// which: manifest, config, layer
|
||||
func GetTestBlobDigest(image, which string) godigest.Digest {
|
||||
prePath := "../test/data"
|
||||
|
||||
for _, err := os.Stat(prePath); err != nil; _, err = os.Stat(prePath) {
|
||||
prePath = "../" + prePath
|
||||
}
|
||||
|
||||
imgPath := path.Join(prePath, image)
|
||||
manifest, config, layer := GetOciLayoutDigests(imgPath)
|
||||
|
||||
switch which {
|
||||
case "manifest":
|
||||
return manifest
|
||||
case "config":
|
||||
return config
|
||||
case "layer":
|
||||
return layer
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func GetFreePort() string {
|
||||
port, err := freeport.GetFreePort()
|
||||
if err != nil {
|
||||
@@ -434,57 +410,6 @@ func GetRandomImageConfig() ([]byte, godigest.Digest) {
|
||||
return configBlobContent, configBlobDigestRaw
|
||||
}
|
||||
|
||||
func GetOciLayoutDigests(imagePath string) (godigest.Digest, godigest.Digest, godigest.Digest) {
|
||||
var (
|
||||
manifestDigest godigest.Digest
|
||||
configDigest godigest.Digest
|
||||
layerDigest godigest.Digest
|
||||
)
|
||||
|
||||
oci, err := umoci.OpenLayout(imagePath)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("error opening layout at '%s' : %w", imagePath, err))
|
||||
}
|
||||
|
||||
defer oci.Close()
|
||||
|
||||
ctxUmoci := context.Background()
|
||||
|
||||
index, err := oci.GetIndex(ctxUmoci)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for _, manifest := range index.Manifests {
|
||||
manifestDigest = manifest.Digest
|
||||
|
||||
manifestBlob, err := oci.GetBlob(ctxUmoci, manifest.Digest)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
manifestBuf, err := io.ReadAll(manifestBlob)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var manifest ispec.Manifest
|
||||
|
||||
err = json.Unmarshal(manifestBuf, &manifest)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
configDigest = manifest.Config.Digest
|
||||
|
||||
for _, layer := range manifest.Layers {
|
||||
layerDigest = layer.Digest
|
||||
}
|
||||
}
|
||||
|
||||
return manifestDigest, configDigest, layerDigest
|
||||
}
|
||||
|
||||
// Deprecated: Should use the new functions starting with "Create".
|
||||
func GetImageComponents(layerSize int) (ispec.Image, [][]byte, ispec.Manifest, error) {
|
||||
config := ispec.Image{
|
||||
|
||||
@@ -22,7 +22,6 @@ import (
|
||||
|
||||
"zotregistry.io/zot/pkg/api"
|
||||
"zotregistry.io/zot/pkg/api/config"
|
||||
"zotregistry.io/zot/pkg/log"
|
||||
"zotregistry.io/zot/pkg/storage"
|
||||
"zotregistry.io/zot/pkg/test"
|
||||
. "zotregistry.io/zot/pkg/test/image-utils"
|
||||
@@ -125,64 +124,6 @@ func TestCopyFiles(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetOciLayoutDigests(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
|
||||
Convey("image path is wrong", t, func() {
|
||||
So(func() { _, _, _ = test.GetOciLayoutDigests("inexistent-image") }, ShouldPanic)
|
||||
})
|
||||
|
||||
Convey("no permissions when getting index", t, func() {
|
||||
storageCtlr := test.GetDefaultStoreController(dir, log.NewLogger("debug", ""))
|
||||
image := CreateDefaultImage()
|
||||
|
||||
err := test.WriteImageToFileSystem(image, "test-index", "0.0.1", storageCtlr)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
err = os.Chmod(path.Join(dir, "test-index", "index.json"), 0o000)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
So(func() { _, _, _ = test.GetOciLayoutDigests(path.Join(dir, "test-index")) }, ShouldPanic)
|
||||
|
||||
err = os.Chmod(path.Join(dir, "test-index", "index.json"), 0o755)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
|
||||
Convey("can't access manifest digest", t, func() {
|
||||
storageCtlr := test.GetDefaultStoreController(dir, log.NewLogger("debug", ""))
|
||||
image := CreateDefaultImage()
|
||||
|
||||
err := test.WriteImageToFileSystem(image, "test-manifest", "0.0.1", storageCtlr)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
buf, err := os.ReadFile(path.Join(dir, "test-manifest", "index.json"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var index ispec.Index
|
||||
if err := json.Unmarshal(buf, &index); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = os.Chmod(path.Join(dir, "test-manifest", "blobs/sha256", index.Manifests[0].Digest.Encoded()), 0o000)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
So(func() { _, _, _ = test.GetOciLayoutDigests(path.Join(dir, "test-manifest")) }, ShouldPanic)
|
||||
|
||||
err = os.Chmod(path.Join(dir, "test-manifest", "blobs/sha256", index.Manifests[0].Digest.Encoded()), 0o755)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetImageComponents(t *testing.T) {
|
||||
Convey("Inject failures for unreachable lines", t, func() {
|
||||
injected := inject.InjectFailure(0)
|
||||
|
||||
@@ -33,8 +33,6 @@ import (
|
||||
var ErrTestError = fmt.Errorf("testError")
|
||||
|
||||
func TestBaseOciLayoutUtils(t *testing.T) {
|
||||
manifestDigest := GetTestBlobDigest("zot-test", "config").String()
|
||||
|
||||
Convey("GetImageManifestSize fail", t, func() {
|
||||
mockStoreController := mocks.MockedImageStore{
|
||||
GetBlobContentFn: func(repo string, digest godigest.Digest) ([]byte, error) {
|
||||
@@ -64,6 +62,9 @@ func TestBaseOciLayoutUtils(t *testing.T) {
|
||||
})
|
||||
|
||||
Convey("GetImageConfigSize: config GetBlobContent fail", t, func() {
|
||||
image := CreateRandomImage()
|
||||
manifestDigest := image.ConfigDescriptor.Digest.String()
|
||||
|
||||
mockStoreController := mocks.MockedImageStore{
|
||||
GetBlobContentFn: func(repo string, digest godigest.Digest) ([]byte, error) {
|
||||
if digest.String() == manifestDigest {
|
||||
@@ -83,7 +84,7 @@ func TestBaseOciLayoutUtils(t *testing.T) {
|
||||
"layers": [
|
||||
{
|
||||
"mediaType": "application/vnd.oci.image.layer.v1.tar+gzip",
|
||||
"digest": "` + GetTestBlobDigest("zot-test", "layer").String() + `",
|
||||
"digest": "` + image.Manifest.Layers[0].Digest.String() + `",
|
||||
"size": 76097157
|
||||
}
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user