mirror of
https://github.com/project-zot/zot.git
synced 2024-12-23 22:27:35 -05:00
77149aa85c
BREAKING CHANGE: The functionality provided by the mgmt endpoint has beed redesigned - see details below BREAKING CHANGE: The API keys endpoint has been moved - see details below BREAKING CHANGE: The mgmt extension config has been removed - endpoint is now enabled by having both the search and the ui extensions enabled BREAKING CHANGE: The API keys configuration has been moved from extensions to http>auth>apikey mgmt and imagetrust extensions: - separate the _zot/ext/mgmt into 3 separate endpoints: _zot/ext/auth, _zot/ext/notation, _zot/ext/cosign - signature verification logic is in a separate `imagetrust` extension - better hanling or errors in case of signature uploads: logging and error codes (more 400 and less 500 errors) - add authz on signature uploads (and add a new middleware in common for this purpose) - remove the mgmt extension configuration - it is now enabled if the UI and the search extensions are enabled userprefs estension: - userprefs are enabled if both search and ui extensions are enabled (as opposed to just search) apikey extension is removed and logic moved into the api folder - Move apikeys code out of pkg/extensions and into pkg/api - Remove apikey configuration options from the extensions configuration and move it inside the http auth section - remove the build label apikeys other changes: - move most of the logic adding handlers to the extensions endpoints out of routes.go and into the extensions files. - add warnings in case the users are still using configurations with the obsolete settings for mgmt and api keys - add a new function in the extension package which could be a single point of starting backgroud tasks for all extensions - more clear methods for verifying specific extensions are enabled - fix http methods paired with the UI handlers - rebuild swagger docs Signed-off-by: Andrei Aaron <aaaron@luxoft.com>
182 lines
4.9 KiB
Go
182 lines
4.9 KiB
Go
//go:build search
|
|
// +build search
|
|
|
|
package extensions
|
|
|
|
import (
|
|
"net/http"
|
|
"sync"
|
|
"time"
|
|
|
|
gqlHandler "github.com/99designs/gqlgen/graphql/handler"
|
|
"github.com/gorilla/mux"
|
|
|
|
"zotregistry.io/zot/pkg/api/config"
|
|
"zotregistry.io/zot/pkg/api/constants"
|
|
zcommon "zotregistry.io/zot/pkg/common"
|
|
"zotregistry.io/zot/pkg/extensions/search"
|
|
cveinfo "zotregistry.io/zot/pkg/extensions/search/cve"
|
|
"zotregistry.io/zot/pkg/extensions/search/gql_generated"
|
|
"zotregistry.io/zot/pkg/log"
|
|
mTypes "zotregistry.io/zot/pkg/meta/types"
|
|
"zotregistry.io/zot/pkg/scheduler"
|
|
"zotregistry.io/zot/pkg/storage"
|
|
)
|
|
|
|
type (
|
|
CveInfo cveinfo.CveInfo
|
|
state int
|
|
)
|
|
|
|
const (
|
|
pending state = iota
|
|
running
|
|
done
|
|
)
|
|
|
|
func IsBuiltWithSearchExtension() bool {
|
|
return true
|
|
}
|
|
|
|
func GetCVEInfo(config *config.Config, storeController storage.StoreController,
|
|
metaDB mTypes.MetaDB, log log.Logger,
|
|
) CveInfo {
|
|
if config.Extensions.Search == nil || !*config.Extensions.Search.Enable || config.Extensions.Search.CVE == nil {
|
|
return nil
|
|
}
|
|
|
|
dbRepository := config.Extensions.Search.CVE.Trivy.DBRepository
|
|
javaDBRepository := config.Extensions.Search.CVE.Trivy.JavaDBRepository
|
|
|
|
return cveinfo.NewCVEInfo(storeController, metaDB, dbRepository, javaDBRepository, log)
|
|
}
|
|
|
|
func EnableSearchExtension(config *config.Config, storeController storage.StoreController,
|
|
metaDB mTypes.MetaDB, taskScheduler *scheduler.Scheduler, cveInfo CveInfo, log log.Logger,
|
|
) {
|
|
if config.Extensions.Search != nil && *config.Extensions.Search.Enable && config.Extensions.Search.CVE != nil {
|
|
updateInterval := config.Extensions.Search.CVE.UpdateInterval
|
|
|
|
downloadTrivyDB(updateInterval, taskScheduler, cveInfo, log)
|
|
} else {
|
|
log.Info().Msg("CVE config not provided, skipping CVE update")
|
|
}
|
|
}
|
|
|
|
func downloadTrivyDB(interval time.Duration, sch *scheduler.Scheduler, cveInfo CveInfo, log log.Logger) {
|
|
generator := NewTrivyTaskGenerator(interval, cveInfo, log)
|
|
|
|
log.Info().Msg("Submitting CVE DB update scheduler")
|
|
sch.SubmitGenerator(generator, interval, scheduler.HighPriority)
|
|
}
|
|
|
|
func NewTrivyTaskGenerator(interval time.Duration, cveInfo CveInfo, log log.Logger) *TrivyTaskGenerator {
|
|
generator := &TrivyTaskGenerator{interval, cveInfo, log, pending, 0, time.Now(), &sync.Mutex{}}
|
|
|
|
return generator
|
|
}
|
|
|
|
type TrivyTaskGenerator struct {
|
|
interval time.Duration
|
|
cveInfo CveInfo
|
|
log log.Logger
|
|
status state
|
|
waitTime time.Duration
|
|
lastTaskTime time.Time
|
|
lock *sync.Mutex
|
|
}
|
|
|
|
func (gen *TrivyTaskGenerator) Next() (scheduler.Task, error) {
|
|
var newTask scheduler.Task
|
|
|
|
gen.lock.Lock()
|
|
|
|
if gen.status == pending && time.Since(gen.lastTaskTime) >= gen.waitTime {
|
|
newTask = newTrivyTask(gen.interval, gen.cveInfo, gen, gen.log)
|
|
gen.status = running
|
|
}
|
|
gen.lock.Unlock()
|
|
|
|
return newTask, nil
|
|
}
|
|
|
|
func (gen *TrivyTaskGenerator) IsDone() bool {
|
|
gen.lock.Lock()
|
|
status := gen.status
|
|
gen.lock.Unlock()
|
|
|
|
return status == done
|
|
}
|
|
|
|
func (gen *TrivyTaskGenerator) Reset() {
|
|
gen.lock.Lock()
|
|
gen.status = pending
|
|
gen.waitTime = 0
|
|
gen.lock.Unlock()
|
|
}
|
|
|
|
type trivyTask struct {
|
|
interval time.Duration
|
|
cveInfo cveinfo.CveInfo
|
|
generator *TrivyTaskGenerator
|
|
log log.Logger
|
|
}
|
|
|
|
func newTrivyTask(interval time.Duration, cveInfo cveinfo.CveInfo,
|
|
generator *TrivyTaskGenerator, log log.Logger,
|
|
) *trivyTask {
|
|
return &trivyTask{interval, cveInfo, generator, log}
|
|
}
|
|
|
|
func (trivyT *trivyTask) DoWork() error {
|
|
trivyT.log.Info().Msg("updating the CVE database")
|
|
|
|
err := trivyT.cveInfo.UpdateDB()
|
|
if err != nil {
|
|
trivyT.generator.lock.Lock()
|
|
trivyT.generator.status = pending
|
|
|
|
if trivyT.generator.waitTime == 0 {
|
|
trivyT.generator.waitTime = time.Second
|
|
}
|
|
|
|
trivyT.generator.waitTime *= 2
|
|
trivyT.generator.lastTaskTime = time.Now()
|
|
trivyT.generator.lock.Unlock()
|
|
|
|
return err
|
|
}
|
|
|
|
trivyT.generator.lock.Lock()
|
|
trivyT.generator.lastTaskTime = time.Now()
|
|
trivyT.generator.status = done
|
|
trivyT.generator.lock.Unlock()
|
|
trivyT.log.Info().Str("DB update completed, next update scheduled after", trivyT.interval.String()).Msg("")
|
|
|
|
return nil
|
|
}
|
|
|
|
func SetupSearchRoutes(conf *config.Config, router *mux.Router, storeController storage.StoreController,
|
|
metaDB mTypes.MetaDB, cveInfo CveInfo, log log.Logger,
|
|
) {
|
|
if !conf.IsSearchEnabled() {
|
|
log.Info().Msg("skip enabling the search route as the config prerequisites are not met")
|
|
|
|
return
|
|
}
|
|
|
|
log.Info().Msg("setting up search routes")
|
|
|
|
resConfig := search.GetResolverConfig(log, storeController, metaDB, cveInfo)
|
|
|
|
allowedMethods := zcommon.AllowedMethods(http.MethodGet, http.MethodPost)
|
|
|
|
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")
|
|
}
|