2022-10-10 07:05:55 -05:00
|
|
|
//go:build search
|
|
|
|
// +build search
|
2020-10-14 16:47:20 -05:00
|
|
|
|
|
|
|
package extensions
|
|
|
|
|
|
|
|
import (
|
2023-02-27 14:25:47 -05:00
|
|
|
"net/http"
|
2020-10-14 16:47:20 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
gqlHandler "github.com/99designs/gqlgen/graphql/handler"
|
2021-10-15 10:05:00 -05:00
|
|
|
"github.com/gorilla/mux"
|
2022-10-20 11:39:20 -05:00
|
|
|
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/api/config"
|
2022-02-24 15:31:36 -05:00
|
|
|
"zotregistry.io/zot/pkg/api/constants"
|
2023-05-25 13:44:54 -05:00
|
|
|
zcommon "zotregistry.io/zot/pkg/common"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/extensions/search"
|
|
|
|
cveinfo "zotregistry.io/zot/pkg/extensions/search/cve"
|
2022-07-15 06:10:51 -05:00
|
|
|
"zotregistry.io/zot/pkg/extensions/search/gql_generated"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/log"
|
2023-07-18 12:27:26 -05:00
|
|
|
mTypes "zotregistry.io/zot/pkg/meta/types"
|
2023-03-02 12:43:54 -05:00
|
|
|
"zotregistry.io/zot/pkg/scheduler"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/storage"
|
2020-10-14 16:47:20 -05:00
|
|
|
)
|
|
|
|
|
2023-09-22 13:49:17 -05:00
|
|
|
const scanInterval = 15 * time.Minute
|
2023-03-02 12:43:54 -05:00
|
|
|
|
2023-09-22 13:49:17 -05:00
|
|
|
type CveScanner cveinfo.Scanner
|
2022-09-28 13:39:54 -05:00
|
|
|
|
2023-05-25 06:46:52 -05:00
|
|
|
func IsBuiltWithSearchExtension() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-09-22 13:49:17 -05:00
|
|
|
func GetCveScanner(conf *config.Config, storeController storage.StoreController,
|
2023-07-18 12:27:26 -05:00
|
|
|
metaDB mTypes.MetaDB, log log.Logger,
|
2023-09-22 13:49:17 -05:00
|
|
|
) CveScanner {
|
|
|
|
if !conf.IsCveScanningEnabled() {
|
2023-02-10 00:04:52 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-22 13:49:17 -05:00
|
|
|
dbRepository := conf.Extensions.Search.CVE.Trivy.DBRepository
|
|
|
|
javaDBRepository := conf.Extensions.Search.CVE.Trivy.JavaDBRepository
|
2023-02-10 00:04:52 -05:00
|
|
|
|
2023-09-22 13:49:17 -05:00
|
|
|
return cveinfo.NewScanner(storeController, metaDB, dbRepository, javaDBRepository, log)
|
2023-02-10 00:04:52 -05:00
|
|
|
}
|
|
|
|
|
2023-09-22 13:49:17 -05:00
|
|
|
func EnableSearchExtension(conf *config.Config, storeController storage.StoreController,
|
|
|
|
metaDB mTypes.MetaDB, taskScheduler *scheduler.Scheduler, cveScanner CveScanner, log log.Logger,
|
2023-01-09 15:37:44 -05:00
|
|
|
) {
|
2023-09-22 13:49:17 -05:00
|
|
|
if conf.IsCveScanningEnabled() {
|
|
|
|
updateInterval := conf.Extensions.Search.CVE.UpdateInterval
|
2023-03-02 12:43:54 -05:00
|
|
|
|
2023-09-22 13:49:17 -05:00
|
|
|
downloadTrivyDB(updateInterval, taskScheduler, cveScanner, log)
|
|
|
|
startScanner(scanInterval, metaDB, taskScheduler, cveScanner, log)
|
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
|
|
|
}
|
2022-04-27 01:00:20 -05:00
|
|
|
}
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2023-09-22 13:49:17 -05:00
|
|
|
func downloadTrivyDB(interval time.Duration, sch *scheduler.Scheduler, cveScanner CveScanner, log log.Logger) {
|
|
|
|
generator := cveinfo.NewDBUpdateTaskGenerator(interval, cveScanner, log)
|
2021-10-28 04:10:01 -05:00
|
|
|
|
2023-05-31 16:37:46 -05:00
|
|
|
log.Info().Msg("Submitting CVE DB update scheduler")
|
2023-03-02 12:43:54 -05:00
|
|
|
sch.SubmitGenerator(generator, interval, scheduler.HighPriority)
|
|
|
|
}
|
2021-10-28 04:10:01 -05:00
|
|
|
|
2023-09-22 13:49:17 -05:00
|
|
|
func startScanner(interval time.Duration, metaDB mTypes.MetaDB, sch *scheduler.Scheduler,
|
|
|
|
cveScanner CveScanner, log log.Logger,
|
|
|
|
) {
|
|
|
|
generator := cveinfo.NewScanTaskGenerator(metaDB, cveScanner, log)
|
2023-03-02 12:43:54 -05:00
|
|
|
|
2023-09-22 13:49:17 -05:00
|
|
|
log.Info().Msg("Submitting CVE scan scheduler")
|
|
|
|
sch.SubmitGenerator(generator, interval, scheduler.MediumPriority)
|
2020-10-14 16:47:20 -05:00
|
|
|
}
|
|
|
|
|
2023-08-02 13:58:34 -05:00
|
|
|
func SetupSearchRoutes(conf *config.Config, router *mux.Router, storeController storage.StoreController,
|
2023-09-22 13:49:17 -05:00
|
|
|
metaDB mTypes.MetaDB, cveScanner CveScanner, log log.Logger,
|
2022-04-27 01:00:20 -05:00
|
|
|
) {
|
2023-08-02 13:58:34 -05:00
|
|
|
if !conf.IsSearchEnabled() {
|
|
|
|
log.Info().Msg("skip enabling the search route as the config prerequisites are not met")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-27 01:00:20 -05:00
|
|
|
log.Info().Msg("setting up search routes")
|
2022-03-04 02:37:06 -05:00
|
|
|
|
2023-09-22 13:49:17 -05:00
|
|
|
var cveInfo cveinfo.CveInfo
|
|
|
|
if conf.IsCveScanningEnabled() {
|
|
|
|
cveInfo = cveinfo.NewCVEInfo(cveScanner, metaDB, log)
|
|
|
|
} else {
|
|
|
|
cveInfo = nil
|
|
|
|
}
|
|
|
|
|
2023-08-02 13:58:34 -05:00
|
|
|
resConfig := search.GetResolverConfig(log, storeController, metaDB, cveInfo)
|
2022-04-27 01:00:20 -05:00
|
|
|
|
2023-08-02 13:58:34 -05:00
|
|
|
allowedMethods := zcommon.AllowedMethods(http.MethodGet, http.MethodPost)
|
2023-05-11 08:05:14 -05:00
|
|
|
|
2023-08-02 13:58:34 -05:00
|
|
|
extRouter := router.PathPrefix(constants.ExtSearchPrefix).Subrouter()
|
|
|
|
extRouter.Use(zcommon.CORSHeadersMiddleware(conf.HTTP.AllowOrigin))
|
|
|
|
extRouter.Use(zcommon.ACHeadersMiddleware(conf, allowedMethods...))
|
|
|
|
extRouter.Use(zcommon.AddExtensionSecurityHeaders())
|
|
|
|
extRouter.Methods(allowedMethods...).
|
|
|
|
Handler(gqlHandler.NewDefaultServer(gql_generated.NewExecutableSchema(resConfig)))
|
|
|
|
|
|
|
|
log.Info().Msg("finished setting up search routes")
|
2023-05-11 08:05:14 -05:00
|
|
|
}
|