2022-04-27 01:00:20 -05:00
|
|
|
//go:build scrub
|
|
|
|
// +build scrub
|
2022-03-04 02:37:06 -05:00
|
|
|
|
|
|
|
package scrub_test
|
|
|
|
|
|
|
|
import (
|
2023-09-05 11:48:56 -05:00
|
|
|
"context"
|
2022-03-04 02:37:06 -05:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
2022-10-20 11:39:20 -05:00
|
|
|
|
2024-01-31 23:34:07 -05:00
|
|
|
"zotregistry.dev/zot/pkg/api"
|
|
|
|
"zotregistry.dev/zot/pkg/api/config"
|
|
|
|
extconf "zotregistry.dev/zot/pkg/extensions/config"
|
|
|
|
"zotregistry.dev/zot/pkg/extensions/monitoring"
|
|
|
|
"zotregistry.dev/zot/pkg/extensions/scrub"
|
|
|
|
"zotregistry.dev/zot/pkg/log"
|
|
|
|
"zotregistry.dev/zot/pkg/storage"
|
|
|
|
"zotregistry.dev/zot/pkg/storage/cache"
|
|
|
|
"zotregistry.dev/zot/pkg/storage/local"
|
|
|
|
test "zotregistry.dev/zot/pkg/test/common"
|
|
|
|
. "zotregistry.dev/zot/pkg/test/image-utils"
|
|
|
|
ociutils "zotregistry.dev/zot/pkg/test/oci-utils"
|
2022-03-04 02:37:06 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
repoName = "test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestScrubExtension(t *testing.T) {
|
|
|
|
Convey("Blobs integrity not affected", t, func(c C) {
|
|
|
|
port := test.GetFreePort()
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
logFile, err := os.CreateTemp("", "zot-log*.txt")
|
2022-03-04 02:37:06 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
defer os.Remove(logFile.Name()) // clean up
|
|
|
|
|
|
|
|
conf := config.New()
|
|
|
|
conf.HTTP.Port = port
|
|
|
|
|
|
|
|
dir := t.TempDir()
|
2022-09-23 00:27:56 -05:00
|
|
|
subdir := t.TempDir()
|
2022-03-04 02:37:06 -05:00
|
|
|
|
|
|
|
conf.Storage.RootDirectory = dir
|
2023-04-07 11:49:24 -05:00
|
|
|
conf.Storage.Dedupe = false
|
2023-08-07 14:55:19 -05:00
|
|
|
conf.Storage.GC = false
|
2023-04-07 11:49:24 -05:00
|
|
|
|
2022-09-23 00:27:56 -05:00
|
|
|
substore := config.StorageConfig{RootDirectory: subdir}
|
|
|
|
conf.Storage.SubPaths = map[string]config.StorageConfig{"/a": substore}
|
2022-03-04 02:37:06 -05:00
|
|
|
conf.Log.Output = logFile.Name()
|
2022-09-27 20:06:50 -05:00
|
|
|
trueValue := true
|
2022-03-04 02:37:06 -05:00
|
|
|
scrubConfig := &extconf.ScrubConfig{
|
2022-10-21 07:33:54 -05:00
|
|
|
BaseConfig: extconf.BaseConfig{Enable: &trueValue},
|
|
|
|
Interval: 2,
|
2022-03-04 02:37:06 -05:00
|
|
|
}
|
|
|
|
conf.Extensions = &extconf.ExtensionConfig{
|
|
|
|
Scrub: scrubConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctlr := api.NewController(conf)
|
|
|
|
|
2023-09-27 13:34:48 -05:00
|
|
|
srcStorageCtlr := ociutils.GetDefaultStoreController(dir, log.NewLogger("debug", ""))
|
|
|
|
err = WriteImageToFileSystem(CreateDefaultVulnerableImage(), repoName, "0.0.1", srcStorageCtlr)
|
2023-08-18 03:46:11 -05:00
|
|
|
So(err, ShouldBeNil)
|
2022-03-04 02:37:06 -05:00
|
|
|
|
2023-01-19 11:54:05 -05:00
|
|
|
cm := test.NewControllerManager(ctlr)
|
|
|
|
cm.StartAndWait(port)
|
|
|
|
defer cm.StopServer()
|
2022-03-04 02:37:06 -05:00
|
|
|
|
2023-12-11 13:00:34 -05:00
|
|
|
found, err := test.ReadLogFileAndSearchString(logFile.Name(), "blobs/manifest ok", 60*time.Second)
|
|
|
|
So(found, ShouldBeTrue)
|
2022-03-04 02:37:06 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Blobs integrity affected", t, func(c C) {
|
|
|
|
port := test.GetFreePort()
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
logFile, err := os.CreateTemp("", "zot-log*.txt")
|
2022-03-04 02:37:06 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
defer os.Remove(logFile.Name()) // clean up
|
|
|
|
|
|
|
|
conf := config.New()
|
|
|
|
conf.HTTP.Port = port
|
|
|
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
conf.Storage.RootDirectory = dir
|
2023-04-07 11:49:24 -05:00
|
|
|
conf.Storage.Dedupe = false
|
2023-08-07 14:55:19 -05:00
|
|
|
conf.Storage.GC = false
|
2023-04-07 11:49:24 -05:00
|
|
|
|
2022-03-04 02:37:06 -05:00
|
|
|
conf.Log.Output = logFile.Name()
|
2022-09-27 20:06:50 -05:00
|
|
|
trueValue := true
|
2022-03-04 02:37:06 -05:00
|
|
|
scrubConfig := &extconf.ScrubConfig{
|
2022-10-21 07:33:54 -05:00
|
|
|
BaseConfig: extconf.BaseConfig{Enable: &trueValue},
|
|
|
|
Interval: 2,
|
2022-03-04 02:37:06 -05:00
|
|
|
}
|
|
|
|
conf.Extensions = &extconf.ExtensionConfig{
|
|
|
|
Scrub: scrubConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctlr := api.NewController(conf)
|
|
|
|
|
2023-09-27 13:34:48 -05:00
|
|
|
srcStorageCtlr := ociutils.GetDefaultStoreController(dir, log.NewLogger("debug", ""))
|
2023-09-15 11:53:15 -05:00
|
|
|
image := CreateDefaultVulnerableImage()
|
2023-09-27 13:34:48 -05:00
|
|
|
err = WriteImageToFileSystem(image, repoName, "0.0.1", srcStorageCtlr)
|
2023-08-18 03:46:11 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2024-01-25 14:12:21 -05:00
|
|
|
layerDigest := image.Manifest.Layers[0].Digest
|
2022-03-04 02:37:06 -05:00
|
|
|
|
2024-01-25 14:12:21 -05:00
|
|
|
err = os.Remove(path.Join(dir, repoName, "blobs/sha256", layerDigest.Encoded()))
|
2022-03-04 02:37:06 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-01-19 11:54:05 -05:00
|
|
|
cm := test.NewControllerManager(ctlr)
|
|
|
|
cm.StartAndWait(port)
|
|
|
|
defer cm.StopServer()
|
2022-03-04 02:37:06 -05:00
|
|
|
|
2023-12-11 13:00:34 -05:00
|
|
|
found, err := test.ReadLogFileAndSearchString(logFile.Name(), "blobs/manifest affected", 60*time.Second)
|
|
|
|
So(found, ShouldBeTrue)
|
2022-03-04 02:37:06 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
2022-09-23 00:27:56 -05:00
|
|
|
Convey("Generator error - not enough permissions to access root directory", t, func(c C) {
|
2022-03-04 02:37:06 -05:00
|
|
|
port := test.GetFreePort()
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
logFile, err := os.CreateTemp("", "zot-log*.txt")
|
2022-03-04 02:37:06 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
defer os.Remove(logFile.Name()) // clean up
|
|
|
|
|
|
|
|
conf := config.New()
|
|
|
|
conf.HTTP.Port = port
|
|
|
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
conf.Storage.RootDirectory = dir
|
2023-04-07 11:49:24 -05:00
|
|
|
conf.Storage.Dedupe = false
|
2023-08-07 14:55:19 -05:00
|
|
|
conf.Storage.GC = false
|
2023-04-07 11:49:24 -05:00
|
|
|
|
2022-03-04 02:37:06 -05:00
|
|
|
conf.Log.Output = logFile.Name()
|
2022-09-27 20:06:50 -05:00
|
|
|
trueValue := true
|
2022-03-04 02:37:06 -05:00
|
|
|
scrubConfig := &extconf.ScrubConfig{
|
2022-10-21 07:33:54 -05:00
|
|
|
BaseConfig: extconf.BaseConfig{Enable: &trueValue},
|
|
|
|
Interval: 2,
|
2022-03-04 02:37:06 -05:00
|
|
|
}
|
|
|
|
conf.Extensions = &extconf.ExtensionConfig{
|
|
|
|
Scrub: scrubConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctlr := api.NewController(conf)
|
|
|
|
|
2023-09-27 13:34:48 -05:00
|
|
|
srcStorageCtlr := ociutils.GetDefaultStoreController(dir, log.NewLogger("debug", ""))
|
2023-09-15 11:53:15 -05:00
|
|
|
image := CreateDefaultVulnerableImage()
|
2023-08-18 03:46:11 -05:00
|
|
|
|
2023-09-27 13:34:48 -05:00
|
|
|
err = WriteImageToFileSystem(image, repoName, "0.0.1", srcStorageCtlr)
|
2023-08-18 03:46:11 -05:00
|
|
|
So(err, ShouldBeNil)
|
2022-03-04 02:37:06 -05:00
|
|
|
|
|
|
|
So(os.Chmod(path.Join(dir, repoName), 0o000), ShouldBeNil)
|
|
|
|
|
2023-01-19 11:54:05 -05:00
|
|
|
cm := test.NewControllerManager(ctlr)
|
|
|
|
cm.StartAndWait(port)
|
|
|
|
defer cm.StopServer()
|
2022-03-04 02:37:06 -05:00
|
|
|
|
2023-12-11 13:00:34 -05:00
|
|
|
found, err := test.ReadLogFileAndSearchString(logFile.Name(), "failed to execute generator", 60*time.Second)
|
|
|
|
So(found, ShouldBeTrue)
|
2022-03-04 02:37:06 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(os.Chmod(path.Join(dir, repoName), 0o755), ShouldBeNil)
|
|
|
|
})
|
|
|
|
}
|
2022-05-09 17:30:11 -05:00
|
|
|
|
|
|
|
func TestRunScrubRepo(t *testing.T) {
|
|
|
|
Convey("Blobs integrity not affected", t, func(c C) {
|
2022-09-02 07:56:02 -05:00
|
|
|
logFile, err := os.CreateTemp("", "zot-log*.txt")
|
2022-05-09 17:30:11 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
defer os.Remove(logFile.Name()) // clean up
|
|
|
|
|
2022-06-24 08:08:47 -05:00
|
|
|
conf := config.New()
|
|
|
|
conf.Extensions = &extconf.ExtensionConfig{}
|
|
|
|
conf.Extensions.Lint = &extconf.LintConfig{}
|
|
|
|
|
2022-05-09 17:30:11 -05:00
|
|
|
dir := t.TempDir()
|
|
|
|
log := log.NewLogger("debug", logFile.Name())
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
2023-09-22 13:51:20 -05:00
|
|
|
imgStore := local.NewImageStore(dir, true,
|
2022-11-02 17:53:08 -05:00
|
|
|
true, log, metrics, nil, cacheDriver)
|
2022-05-09 17:30:11 -05:00
|
|
|
|
2023-09-27 13:34:48 -05:00
|
|
|
srcStorageCtlr := ociutils.GetDefaultStoreController(dir, log)
|
2023-09-15 11:53:15 -05:00
|
|
|
image := CreateDefaultVulnerableImage()
|
2023-08-18 03:46:11 -05:00
|
|
|
|
2023-09-27 13:34:48 -05:00
|
|
|
err = WriteImageToFileSystem(image, repoName, "0.0.1", srcStorageCtlr)
|
2023-08-18 03:46:11 -05:00
|
|
|
So(err, ShouldBeNil)
|
2022-05-09 17:30:11 -05:00
|
|
|
|
2023-09-05 11:48:56 -05:00
|
|
|
err = scrub.RunScrubRepo(context.Background(), imgStore, repoName, log)
|
2022-09-23 00:27:56 -05:00
|
|
|
So(err, ShouldBeNil)
|
2022-05-09 17:30:11 -05:00
|
|
|
|
|
|
|
data, err := os.ReadFile(logFile.Name())
|
|
|
|
So(err, ShouldBeNil)
|
2023-12-08 03:05:02 -05:00
|
|
|
So(string(data), ShouldContainSubstring, "blobs/manifest ok")
|
2022-05-09 17:30:11 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Blobs integrity affected", t, func(c C) {
|
2022-09-02 07:56:02 -05:00
|
|
|
logFile, err := os.CreateTemp("", "zot-log*.txt")
|
2022-05-09 17:30:11 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
defer os.Remove(logFile.Name()) // clean up
|
|
|
|
|
2022-06-24 08:08:47 -05:00
|
|
|
conf := config.New()
|
|
|
|
|
|
|
|
conf.Extensions = &extconf.ExtensionConfig{}
|
|
|
|
conf.Extensions.Lint = &extconf.LintConfig{}
|
|
|
|
|
2022-05-09 17:30:11 -05:00
|
|
|
dir := t.TempDir()
|
|
|
|
log := log.NewLogger("debug", logFile.Name())
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
2023-09-22 13:51:20 -05:00
|
|
|
imgStore := local.NewImageStore(dir, true,
|
2022-11-02 17:53:08 -05:00
|
|
|
true, log, metrics, nil, cacheDriver)
|
2022-05-09 17:30:11 -05:00
|
|
|
|
2023-09-27 13:34:48 -05:00
|
|
|
srcStorageCtlr := ociutils.GetDefaultStoreController(dir, log)
|
2023-09-15 11:53:15 -05:00
|
|
|
image := CreateDefaultVulnerableImage()
|
2023-08-18 03:46:11 -05:00
|
|
|
|
2023-09-27 13:34:48 -05:00
|
|
|
err = WriteImageToFileSystem(image, repoName, "0.0.1", srcStorageCtlr)
|
2023-08-18 03:46:11 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2024-01-25 14:12:21 -05:00
|
|
|
layerDigest := image.Manifest.Layers[0].Digest
|
2022-05-09 17:30:11 -05:00
|
|
|
|
2024-01-25 14:12:21 -05:00
|
|
|
err = os.Remove(path.Join(dir, repoName, "blobs/sha256", layerDigest.Encoded()))
|
2022-05-09 17:30:11 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-09-05 11:48:56 -05:00
|
|
|
err = scrub.RunScrubRepo(context.Background(), imgStore, repoName, log)
|
2022-09-23 00:27:56 -05:00
|
|
|
So(err, ShouldBeNil)
|
2022-05-09 17:30:11 -05:00
|
|
|
|
|
|
|
data, err := os.ReadFile(logFile.Name())
|
|
|
|
So(err, ShouldBeNil)
|
2023-12-08 03:05:02 -05:00
|
|
|
So(string(data), ShouldContainSubstring, "blobs/manifest affected")
|
2022-05-09 17:30:11 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("CheckRepo error - not enough permissions to access root directory", t, func(c C) {
|
2022-09-02 07:56:02 -05:00
|
|
|
logFile, err := os.CreateTemp("", "zot-log*.txt")
|
2022-05-09 17:30:11 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
defer os.Remove(logFile.Name()) // clean up
|
|
|
|
|
2022-06-24 08:08:47 -05:00
|
|
|
conf := config.New()
|
|
|
|
conf.Extensions = &extconf.ExtensionConfig{}
|
|
|
|
conf.Extensions.Lint = &extconf.LintConfig{}
|
|
|
|
|
2022-05-09 17:30:11 -05:00
|
|
|
dir := t.TempDir()
|
|
|
|
log := log.NewLogger("debug", logFile.Name())
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-11-02 17:53:08 -05:00
|
|
|
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{
|
|
|
|
RootDir: dir,
|
|
|
|
Name: "cache",
|
|
|
|
UseRelPaths: true,
|
|
|
|
}, log)
|
2023-09-22 13:51:20 -05:00
|
|
|
imgStore := local.NewImageStore(dir, true, true, log, metrics, nil, cacheDriver)
|
2022-05-09 17:30:11 -05:00
|
|
|
|
2023-09-27 13:34:48 -05:00
|
|
|
srcStorageCtlr := ociutils.GetDefaultStoreController(dir, log)
|
2023-09-15 11:53:15 -05:00
|
|
|
image := CreateDefaultVulnerableImage()
|
2023-08-18 03:46:11 -05:00
|
|
|
|
2023-09-27 13:34:48 -05:00
|
|
|
err = WriteImageToFileSystem(image, repoName, "0.0.1", srcStorageCtlr)
|
2023-08-18 03:46:11 -05:00
|
|
|
So(err, ShouldBeNil)
|
2022-05-09 17:30:11 -05:00
|
|
|
|
|
|
|
So(os.Chmod(path.Join(dir, repoName), 0o000), ShouldBeNil)
|
|
|
|
|
2023-09-05 11:48:56 -05:00
|
|
|
err = scrub.RunScrubRepo(context.Background(), imgStore, repoName, log)
|
2022-09-23 00:27:56 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
2022-05-09 17:30:11 -05:00
|
|
|
|
|
|
|
data, err := os.ReadFile(logFile.Name())
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(string(data), ShouldContainSubstring,
|
2024-07-29 12:32:51 -05:00
|
|
|
"failed to run scrub for "+imgStore.RootDir())
|
2022-05-09 17:30:11 -05:00
|
|
|
So(os.Chmod(path.Join(dir, repoName), 0o755), ShouldBeNil)
|
|
|
|
})
|
|
|
|
}
|