2020-10-14 16:47:20 -05:00
|
|
|
// +build extended
|
|
|
|
|
|
|
|
package extensions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/anuvu/zot/pkg/extensions/search"
|
|
|
|
"github.com/anuvu/zot/pkg/storage"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
gqlHandler "github.com/99designs/gqlgen/graphql/handler"
|
|
|
|
cveinfo "github.com/anuvu/zot/pkg/extensions/search/cve"
|
|
|
|
|
|
|
|
"github.com/anuvu/zot/pkg/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DownloadTrivyDB ...
|
2020-10-22 19:31:16 -05:00
|
|
|
func downloadTrivyDB(dbDir string, log log.Logger, updateInterval time.Duration) error {
|
2020-10-14 16:47:20 -05:00
|
|
|
for {
|
2020-10-22 19:31:16 -05:00
|
|
|
log.Info().Msg("updating the CVE database")
|
2020-10-14 16:47:20 -05:00
|
|
|
|
|
|
|
err := cveinfo.UpdateCVEDb(dbDir, log)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-22 19:31:16 -05:00
|
|
|
log.Info().Str("DB update completed, next update scheduled after", updateInterval.String()).Msg("")
|
2020-10-14 16:47:20 -05:00
|
|
|
|
|
|
|
time.Sleep(updateInterval)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 19:31:16 -05:00
|
|
|
// EnableExtensions ...
|
|
|
|
func EnableExtensions(extension *ExtensionConfig, log log.Logger, rootDir string) {
|
2021-06-08 13:37:31 -05:00
|
|
|
if extension.Search != nil && extension.Search.Enable && extension.Search.CVE != nil {
|
2020-10-14 16:47:20 -05:00
|
|
|
defaultUpdateInterval, _ := time.ParseDuration("2h")
|
|
|
|
|
|
|
|
if extension.Search.CVE.UpdateInterval < defaultUpdateInterval {
|
|
|
|
extension.Search.CVE.UpdateInterval = defaultUpdateInterval
|
|
|
|
|
|
|
|
log.Warn().Msg("CVE update interval set to too-short interval <= 1, changing update duration to 2 hours and continuing.") // nolint: lll
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2020-10-22 19:31:16 -05:00
|
|
|
err := downloadTrivyDB(rootDir, log,
|
2020-10-14 16:47:20 -05:00
|
|
|
extension.Search.CVE.UpdateInterval)
|
|
|
|
if err != nil {
|
2021-06-08 13:37:31 -05:00
|
|
|
log.Error().Err(err).Msg("error while downloading TrivyDB")
|
2020-10-14 16:47:20 -05:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
} else {
|
2020-10-22 19:31:16 -05:00
|
|
|
log.Info().Msg("CVE config not provided, skipping CVE update")
|
2020-10-14 16:47:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 19:31:16 -05:00
|
|
|
// SetupRoutes ...
|
2021-06-08 13:37:31 -05:00
|
|
|
func SetupRoutes(extension *ExtensionConfig, router *mux.Router, storeController storage.StoreController,
|
|
|
|
log log.Logger) {
|
2020-10-22 19:31:16 -05:00
|
|
|
log.Info().Msg("setting up extensions routes")
|
2021-06-08 13:37:31 -05:00
|
|
|
|
|
|
|
if extension.Search != nil && extension.Search.Enable {
|
2021-01-25 13:04:03 -05:00
|
|
|
var resConfig search.Config
|
|
|
|
|
|
|
|
if extension.Search.CVE != nil {
|
|
|
|
resConfig = search.GetResolverConfig(log, storeController, true)
|
|
|
|
} else {
|
|
|
|
resConfig = search.GetResolverConfig(log, storeController, false)
|
|
|
|
}
|
|
|
|
|
2021-06-08 13:37:31 -05:00
|
|
|
router.PathPrefix("/query").Methods("GET", "POST").
|
|
|
|
Handler(gqlHandler.NewDefaultServer(search.NewExecutableSchema(resConfig)))
|
|
|
|
}
|
2020-10-14 16:47:20 -05:00
|
|
|
}
|