mirror of
https://github.com/project-zot/zot.git
synced 2026-06-15 20:07:55 +08:00
implement scrub to check manifest/blob integrity
Signed-off-by: Andreea-Lupu <andreealupu1470@yahoo.com>
This commit is contained in:
committed by
Ramkumar Chinchani
parent
914cf5c356
commit
c61c3836db
@@ -1,6 +1,9 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
glob "github.com/bmatcuk/doublestar/v4"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
@@ -80,6 +83,49 @@ func NewRootCmd() *cobra.Command {
|
||||
},
|
||||
}
|
||||
|
||||
// "scrub"
|
||||
scrubCmd := &cobra.Command{
|
||||
Use: "scrub <config>",
|
||||
Aliases: []string{"scrub"},
|
||||
Short: "`scrub` checks manifest/blob integrity",
|
||||
Long: "`scrub` checks manifest/blob integrity",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
configuration := config.New()
|
||||
|
||||
if len(args) > 0 {
|
||||
LoadConfiguration(configuration, args[0])
|
||||
} else {
|
||||
if err := cmd.Usage(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// checking if the server is already running
|
||||
response, err := http.Get(fmt.Sprintf("http://%s:%s/v2", configuration.HTTP.Address, configuration.HTTP.Port))
|
||||
|
||||
if err == nil {
|
||||
response.Body.Close()
|
||||
log.Info().Msg("The server is running, in order to perform the scrub command the server should be shut down")
|
||||
panic("Error: server is running")
|
||||
} else {
|
||||
// server is down
|
||||
c := api.NewController(configuration)
|
||||
|
||||
if err := c.InitImageStore(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
result, err := c.StoreController.CheckAllBlobsIntegrity()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
result.PrintScrubResults(cmd.OutOrStdout())
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
verifyCmd := &cobra.Command{
|
||||
Use: "verify <config>",
|
||||
Aliases: []string{"verify"},
|
||||
@@ -137,6 +183,7 @@ func NewRootCmd() *cobra.Command {
|
||||
}
|
||||
|
||||
rootCmd.AddCommand(serveCmd)
|
||||
rootCmd.AddCommand(scrubCmd)
|
||||
rootCmd.AddCommand(gcCmd)
|
||||
rootCmd.AddCommand(verifyCmd)
|
||||
|
||||
|
||||
@@ -1,15 +1,23 @@
|
||||
package cli_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"gopkg.in/resty.v1"
|
||||
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/spf13/viper"
|
||||
"zotregistry.io/zot/pkg/api"
|
||||
"zotregistry.io/zot/pkg/api/config"
|
||||
"zotregistry.io/zot/pkg/cli"
|
||||
. "zotregistry.io/zot/test"
|
||||
)
|
||||
|
||||
func TestUsage(t *testing.T) {
|
||||
@@ -189,3 +197,183 @@ func TestGC(t *testing.T) {
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestScrub(t *testing.T) {
|
||||
oldArgs := os.Args
|
||||
|
||||
defer func() { os.Args = oldArgs }()
|
||||
|
||||
Convey("Test scrub help", t, func(c C) {
|
||||
os.Args = []string{"cli_test", "scrub", "-h"}
|
||||
err := cli.NewRootCmd().Execute()
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("Test scrub config", t, func(c C) {
|
||||
Convey("non-existent config", func(c C) {
|
||||
os.Args = []string{"cli_test", "scrub", path.Join(os.TempDir(), "/x.yaml")}
|
||||
So(func() { _ = cli.NewRootCmd().Execute() }, ShouldPanic)
|
||||
})
|
||||
|
||||
Convey("unknown config", func(c C) {
|
||||
os.Args = []string{"cli_test", "scrub", path.Join(os.TempDir(), "/x")}
|
||||
So(func() { _ = cli.NewRootCmd().Execute() }, ShouldPanic)
|
||||
})
|
||||
|
||||
Convey("bad config", func(c C) {
|
||||
tmpfile, err := ioutil.TempFile("", "zot-test*.json")
|
||||
So(err, ShouldBeNil)
|
||||
defer os.Remove(tmpfile.Name()) // clean up
|
||||
content := []byte(`{"log":{}}`)
|
||||
_, err = tmpfile.Write(content)
|
||||
So(err, ShouldBeNil)
|
||||
err = tmpfile.Close()
|
||||
So(err, ShouldBeNil)
|
||||
os.Args = []string{"cli_test", "scrub", tmpfile.Name()}
|
||||
So(func() { _ = cli.NewRootCmd().Execute() }, ShouldPanic)
|
||||
})
|
||||
|
||||
Convey("server is running", func(c C) {
|
||||
port := GetFreePort()
|
||||
config := config.New()
|
||||
config.HTTP.Port = port
|
||||
controller := api.NewController(config)
|
||||
|
||||
dir, err := ioutil.TempDir("", "scrub-test")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
controller.Config.Storage.RootDirectory = dir
|
||||
go func(controller *api.Controller) {
|
||||
// this blocks
|
||||
if err := controller.Run(); err != nil {
|
||||
return
|
||||
}
|
||||
}(controller)
|
||||
// wait till ready
|
||||
for {
|
||||
_, err := resty.R().Get(fmt.Sprintf("http://127.0.0.1:%s", port))
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
|
||||
tmpfile, err := ioutil.TempFile("", "zot-test*.json")
|
||||
So(err, ShouldBeNil)
|
||||
defer os.Remove(tmpfile.Name()) // clean up
|
||||
content := []byte(fmt.Sprintf(`{
|
||||
"storage": {
|
||||
"rootDirectory": "%s"
|
||||
},
|
||||
"http": {
|
||||
"port": %s
|
||||
},
|
||||
"log": {
|
||||
"level": "debug"
|
||||
}
|
||||
}
|
||||
`, dir, port))
|
||||
_, err = tmpfile.Write(content)
|
||||
So(err, ShouldBeNil)
|
||||
err = tmpfile.Close()
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
os.Args = []string{"cli_test", "scrub", tmpfile.Name()}
|
||||
So(func() { _ = cli.NewRootCmd().Execute() }, ShouldPanic)
|
||||
|
||||
defer func(controller *api.Controller) {
|
||||
ctx := context.Background()
|
||||
_ = controller.Server.Shutdown(ctx)
|
||||
}(controller)
|
||||
})
|
||||
|
||||
Convey("no image store provided", func(c C) {
|
||||
port := GetFreePort()
|
||||
|
||||
tmpfile, err := ioutil.TempFile("", "zot-test*.json")
|
||||
So(err, ShouldBeNil)
|
||||
defer os.Remove(tmpfile.Name()) // clean up
|
||||
content := []byte(fmt.Sprintf(`{
|
||||
"storage": {
|
||||
"rootDirectory": ""
|
||||
},
|
||||
"http": {
|
||||
"port": %s
|
||||
},
|
||||
"log": {
|
||||
"level": "debug"
|
||||
}
|
||||
}
|
||||
`, port))
|
||||
_, err = tmpfile.Write(content)
|
||||
So(err, ShouldBeNil)
|
||||
err = tmpfile.Close()
|
||||
So(err, ShouldBeNil)
|
||||
os.Args = []string{"cli_test", "scrub", tmpfile.Name()}
|
||||
So(func() { _ = cli.NewRootCmd().Execute() }, ShouldPanic)
|
||||
})
|
||||
|
||||
Convey("bad index.json", func(c C) {
|
||||
port := GetFreePort()
|
||||
|
||||
dir, err := ioutil.TempDir("", "scrub-test")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
repoName := "badIndex"
|
||||
|
||||
repo, err := ioutil.TempDir(dir, repoName)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(fmt.Sprintf("%s/blobs", repo), 0755); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if _, err = os.Stat(fmt.Sprintf("%s/oci-layout", repo)); err != nil {
|
||||
content := []byte(`{"imageLayoutVersion": "1.0.0"}`)
|
||||
if err = ioutil.WriteFile(fmt.Sprintf("%s/oci-layout", repo), content, 0600); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
if _, err = os.Stat(fmt.Sprintf("%s/index.json", repo)); err != nil {
|
||||
content := []byte(`not a JSON content`)
|
||||
if err = ioutil.WriteFile(fmt.Sprintf("%s/index.json", repo), content, 0600); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
tmpfile, err := ioutil.TempFile("", "zot-test*.json")
|
||||
So(err, ShouldBeNil)
|
||||
defer os.Remove(tmpfile.Name()) // clean up
|
||||
content := []byte(fmt.Sprintf(`{
|
||||
"storage": {
|
||||
"rootDirectory": "%s"
|
||||
},
|
||||
"http": {
|
||||
"port": %s
|
||||
},
|
||||
"log": {
|
||||
"level": "debug"
|
||||
}
|
||||
}
|
||||
`, dir, port))
|
||||
_, err = tmpfile.Write(content)
|
||||
So(err, ShouldBeNil)
|
||||
err = tmpfile.Close()
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
os.Args = []string{"cli_test", "scrub", tmpfile.Name()}
|
||||
So(func() { _ = cli.NewRootCmd().Execute() }, ShouldPanic)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user