0
Fork 0
mirror of https://github.com/project-zot/zot.git synced 2025-01-13 22:50:38 -05:00

Add an 'enable' flag in the server configuration to enable gql-based searches

"extensions": {
        "search": {
            "enable": true
        }
    }
This commit is contained in:
Andrei Aaron 2021-06-08 21:37:31 +03:00 committed by Ramkumar Chinchani
parent c1dd7878e4
commit 792e82cbdf
10 changed files with 69 additions and 13 deletions

View file

@ -12,6 +12,7 @@
}, },
"extensions": { "extensions": {
"search": { "search": {
"enable": true,
"cve": { "cve": {
"updateInterval": "24h" "updateInterval": "24h"
} }

View file

@ -29,6 +29,7 @@
}, },
"extensions": { "extensions": {
"search": { "search": {
"enable": true,
"cve": { "cve": {
"updateInterval": "24h" "updateInterval": "24h"
} }

View file

@ -91,7 +91,7 @@ func (rh *RouteHandler) SetupRoutes() {
rh.c.Router.PathPrefix("/swagger/v2/").Methods("GET").Handler(httpSwagger.WrapHandler) rh.c.Router.PathPrefix("/swagger/v2/").Methods("GET").Handler(httpSwagger.WrapHandler)
// Setup Extensions Routes // Setup Extensions Routes
if rh.c.Config != nil && rh.c.Config.Extensions != nil { if rh.c.Config != nil && rh.c.Config.Extensions != nil {
ext.SetupRoutes(rh.c.Router, rh.c.StoreController, rh.c.Log) ext.SetupRoutes(rh.c.Config.Extensions, rh.c.Router, rh.c.StoreController, rh.c.Log)
} }
} }

View file

@ -308,7 +308,8 @@ func TestServerCVEResponse(t *testing.T) {
UpdateInterval: 2, UpdateInterval: 2,
} }
searchConfig := &ext.SearchConfig{ searchConfig := &ext.SearchConfig{
CVE: cveConfig, CVE: cveConfig,
Enable: true,
} }
c.Config.Extensions = &ext.ExtensionConfig{ c.Config.Extensions = &ext.ExtensionConfig{
Search: searchConfig, Search: searchConfig,

View file

@ -287,7 +287,7 @@ func TestServerResponse(t *testing.T) {
config := api.NewConfig() config := api.NewConfig()
config.HTTP.Port = port config.HTTP.Port = port
config.Extensions = &extensions.ExtensionConfig{ config.Extensions = &extensions.ExtensionConfig{
Search: &extensions.SearchConfig{}, Search: &extensions.SearchConfig{Enable: true},
} }
c := api.NewController(config) c := api.NewController(config)
dir, err := ioutil.TempDir("", "oci-repo-test") dir, err := ioutil.TempDir("", "oci-repo-test")

View file

@ -8,7 +8,8 @@ type ExtensionConfig struct {
type SearchConfig struct { type SearchConfig struct {
// CVE search // CVE search
CVE *CVEConfig CVE *CVEConfig
Enable bool
} }
type CVEConfig struct { type CVEConfig struct {

View file

@ -33,7 +33,7 @@ func downloadTrivyDB(dbDir string, log log.Logger, updateInterval time.Duration)
// EnableExtensions ... // EnableExtensions ...
func EnableExtensions(extension *ExtensionConfig, log log.Logger, rootDir string) { func EnableExtensions(extension *ExtensionConfig, log log.Logger, rootDir string) {
if extension.Search != nil && extension.Search.CVE != nil { if extension.Search != nil && extension.Search.Enable && extension.Search.CVE != nil {
defaultUpdateInterval, _ := time.ParseDuration("2h") defaultUpdateInterval, _ := time.ParseDuration("2h")
if extension.Search.CVE.UpdateInterval < defaultUpdateInterval { if extension.Search.CVE.UpdateInterval < defaultUpdateInterval {
@ -46,7 +46,7 @@ func EnableExtensions(extension *ExtensionConfig, log log.Logger, rootDir string
err := downloadTrivyDB(rootDir, log, err := downloadTrivyDB(rootDir, log,
extension.Search.CVE.UpdateInterval) extension.Search.CVE.UpdateInterval)
if err != nil { if err != nil {
panic(err) log.Error().Err(err).Msg("error while downloading TrivyDB")
} }
}() }()
} else { } else {
@ -55,9 +55,13 @@ func EnableExtensions(extension *ExtensionConfig, log log.Logger, rootDir string
} }
// SetupRoutes ... // SetupRoutes ...
func SetupRoutes(router *mux.Router, storeController storage.StoreController, log log.Logger) { func SetupRoutes(extension *ExtensionConfig, router *mux.Router, storeController storage.StoreController,
log log.Logger) {
log.Info().Msg("setting up extensions routes") log.Info().Msg("setting up extensions routes")
resConfig := search.GetResolverConfig(log, storeController)
router.PathPrefix("/query").Methods("GET", "POST"). if extension.Search != nil && extension.Search.Enable {
Handler(gqlHandler.NewDefaultServer(search.NewExecutableSchema(resConfig))) resConfig := search.GetResolverConfig(log, storeController)
router.PathPrefix("/query").Methods("GET", "POST").
Handler(gqlHandler.NewDefaultServer(search.NewExecutableSchema(resConfig)))
}
} }

View file

@ -21,6 +21,6 @@ func EnableExtensions(extension *ExtensionConfig, log log.Logger, rootDir string
} }
// SetupRoutes ... // SetupRoutes ...
func SetupRoutes(router *mux.Router, storeController storage.StoreController, log log.Logger) { func SetupRoutes(extension *ExtensionConfig, router *mux.Router, storeController storage.StoreController, log log.Logger) {
log.Warn().Msg("skipping setting up extensions routes because given zot binary doesn't support any extensions, please build zot full binary for this feature") log.Warn().Msg("skipping setting up extensions routes because given zot binary doesn't support any extensions, please build zot full binary for this feature")
} }

View file

@ -547,7 +547,8 @@ func TestCVESearch(t *testing.T) {
UpdateInterval: updateDuration, UpdateInterval: updateDuration,
} }
searchConfig := &ext.SearchConfig{ searchConfig := &ext.SearchConfig{
CVE: cveConfig, CVE: cveConfig,
Enable: true,
} }
c.Config.Extensions = &ext.ExtensionConfig{ c.Config.Extensions = &ext.ExtensionConfig{
Search: searchConfig, Search: searchConfig,

View file

@ -169,7 +169,7 @@ func TestDigestSearchHTTP(t *testing.T) {
config.HTTP.Port = Port1 config.HTTP.Port = Port1
config.Storage.RootDirectory = rootDir config.Storage.RootDirectory = rootDir
config.Extensions = &ext.ExtensionConfig{ config.Extensions = &ext.ExtensionConfig{
Search: &ext.SearchConfig{}, Search: &ext.SearchConfig{Enable: true},
} }
c := api.NewController(config) c := api.NewController(config)
@ -288,3 +288,50 @@ func TestDigestSearchHTTP(t *testing.T) {
So(len(responseStruct.Errors), ShouldEqual, 1) So(len(responseStruct.Errors), ShouldEqual, 1)
}) })
} }
func TestDigestSearchDisabled(t *testing.T) {
Convey("Test disabling image search", t, func() {
dir, err := ioutil.TempDir("", "digest_test")
So(err, ShouldBeNil)
config := api.NewConfig()
config.HTTP.Port = Port1
config.Storage.RootDirectory = dir
config.Extensions = &ext.ExtensionConfig{
Search: &ext.SearchConfig{Enable: false},
}
c := api.NewController(config)
go func() {
// this blocks
if err := c.Run(); err != nil {
return
}
}()
// wait till ready
for {
_, err := resty.R().Get(BaseURL1)
if err == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
// shut down server
defer func() {
ctx := context.Background()
_ = c.Server.Shutdown(ctx)
}()
resp, err := resty.R().Get(BaseURL1 + "/v2/")
So(resp, ShouldNotBeNil)
So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, 200)
resp, err = resty.R().Get(BaseURL1 + "/query")
So(resp, ShouldNotBeNil)
So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, 404)
})
}