2021-10-15 10:05:00 -05:00
|
|
|
//go:build extended
|
2020-10-14 16:47:20 -05:00
|
|
|
// +build extended
|
|
|
|
|
|
|
|
package extensions
|
|
|
|
|
|
|
|
import (
|
2022-02-10 09:17:49 -05:00
|
|
|
"context"
|
2021-12-02 12:45:26 -05:00
|
|
|
goSync "sync"
|
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"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/api/config"
|
|
|
|
"zotregistry.io/zot/pkg/extensions/search"
|
|
|
|
cveinfo "zotregistry.io/zot/pkg/extensions/search/cve"
|
|
|
|
"zotregistry.io/zot/pkg/extensions/sync"
|
|
|
|
"zotregistry.io/zot/pkg/log"
|
|
|
|
"zotregistry.io/zot/pkg/storage"
|
2020-10-14 16:47:20 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
func EnableExtensions(config *config.Config, log log.Logger, rootDir string) {
|
2021-12-28 08:29:30 -05:00
|
|
|
if config.Extensions.Search != nil && *config.Extensions.Search.Enable && config.Extensions.Search.CVE != nil {
|
2020-10-14 16:47:20 -05:00
|
|
|
defaultUpdateInterval, _ := time.ParseDuration("2h")
|
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
if config.Extensions.Search.CVE.UpdateInterval < defaultUpdateInterval {
|
|
|
|
config.Extensions.Search.CVE.UpdateInterval = defaultUpdateInterval
|
2020-10-14 16:47:20 -05:00
|
|
|
|
2021-12-28 08:29:30 -05:00
|
|
|
log.Warn().Msg("CVE update interval set to too-short interval < 2h, changing update duration to 2 hours and continuing.") // nolint: lll
|
2020-10-14 16:47:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2020-10-22 19:31:16 -05:00
|
|
|
err := downloadTrivyDB(rootDir, log,
|
2021-06-08 15:11:18 -05:00
|
|
|
config.Extensions.Search.CVE.UpdateInterval)
|
2020-10-14 16:47:20 -05:00
|
|
|
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
|
|
|
}
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2021-10-28 04:10:01 -05:00
|
|
|
if config.Extensions.Metrics != nil &&
|
2021-12-28 08:29:30 -05:00
|
|
|
*config.Extensions.Metrics.Enable &&
|
2021-10-28 04:10:01 -05:00
|
|
|
config.Extensions.Metrics.Prometheus != nil {
|
|
|
|
if config.Extensions.Metrics.Prometheus.Path == "" {
|
|
|
|
config.Extensions.Metrics.Prometheus.Path = "/metrics"
|
|
|
|
|
|
|
|
log.Warn().Msg("Prometheus instrumentation Path not set, changing to '/metrics'.")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Info().Msg("Metrics config not provided, skipping Metrics config update")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnableSyncExtension enables sync extension.
|
2022-02-10 09:17:49 -05:00
|
|
|
func EnableSyncExtension(ctx context.Context, config *config.Config, wg *goSync.WaitGroup,
|
2021-12-02 12:45:26 -05:00
|
|
|
storeController storage.StoreController, log log.Logger) {
|
2021-12-28 08:29:30 -05:00
|
|
|
if config.Extensions.Sync != nil && *config.Extensions.Sync.Enable {
|
2022-02-10 09:17:49 -05:00
|
|
|
if err := sync.Run(ctx, *config.Extensions.Sync, storeController, wg, log); err != nil {
|
2021-06-08 15:11:18 -05:00
|
|
|
log.Error().Err(err).Msg("Error encountered while setting up syncing")
|
|
|
|
}
|
|
|
|
} else {
|
2022-02-10 09:17:49 -05:00
|
|
|
log.Info().Msg("Sync registries config not provided or disabled, skipping sync")
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
2020-10-14 16:47:20 -05:00
|
|
|
}
|
|
|
|
|
2020-10-22 19:31:16 -05:00
|
|
|
// SetupRoutes ...
|
2021-06-08 15:11:18 -05:00
|
|
|
func SetupRoutes(config *config.Config, router *mux.Router, storeController storage.StoreController,
|
2021-10-28 04:10:01 -05:00
|
|
|
l log.Logger) {
|
|
|
|
// fork a new zerolog child to avoid data race
|
|
|
|
log := log.Logger{Logger: l.With().Caller().Timestamp().Logger()}
|
2020-10-22 19:31:16 -05:00
|
|
|
log.Info().Msg("setting up extensions routes")
|
2021-06-08 13:37:31 -05:00
|
|
|
|
2021-12-28 08:29:30 -05:00
|
|
|
if config.Extensions.Search != nil && *config.Extensions.Search.Enable {
|
2021-01-25 13:04:03 -05:00
|
|
|
var resConfig search.Config
|
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
if config.Extensions.Search.CVE != nil {
|
2021-01-25 13:04:03 -05:00
|
|
|
resConfig = search.GetResolverConfig(log, storeController, true)
|
|
|
|
} else {
|
|
|
|
resConfig = search.GetResolverConfig(log, storeController, false)
|
|
|
|
}
|
|
|
|
|
2022-02-15 20:15:13 -05:00
|
|
|
router.PathPrefix("/query").Methods("GET", "POST", "OPTIONS").
|
2021-06-08 13:37:31 -05:00
|
|
|
Handler(gqlHandler.NewDefaultServer(search.NewExecutableSchema(resConfig)))
|
|
|
|
}
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2021-12-28 08:29:30 -05:00
|
|
|
if config.Extensions.Metrics != nil && *config.Extensions.Metrics.Enable {
|
2021-10-15 10:05:00 -05:00
|
|
|
router.PathPrefix(config.Extensions.Metrics.Prometheus.Path).
|
|
|
|
Handler(promhttp.Handler())
|
|
|
|
}
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// SyncOneImage syncs one image.
|
2021-12-02 12:45:26 -05:00
|
|
|
func SyncOneImage(config *config.Config, storeController storage.StoreController,
|
2022-01-10 11:06:12 -05:00
|
|
|
repoName, reference string, isArtifact bool, log log.Logger) error {
|
2021-06-08 15:11:18 -05:00
|
|
|
log.Info().Msgf("syncing image %s:%s", repoName, reference)
|
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
err := sync.OneImage(*config.Extensions.Sync, storeController, repoName, reference, isArtifact, log)
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2021-10-28 04:10:01 -05:00
|
|
|
return err
|
2020-10-14 16:47:20 -05:00
|
|
|
}
|