2022-10-10 15:05:55 +03:00
//go:build search
// +build search
2020-10-14 14:47:20 -07:00
package extensions
import (
"time"
gqlHandler "github.com/99designs/gqlgen/graphql/handler"
2021-10-15 18:05:00 +03:00
"github.com/gorilla/mux"
2022-02-24 12:31:36 -08:00
distext "github.com/opencontainers/distribution-spec/specs-go/v1/extensions"
2022-10-20 19:39:20 +03:00
2021-12-04 03:50:58 +00:00
"zotregistry.io/zot/pkg/api/config"
2022-02-24 12:31:36 -08:00
"zotregistry.io/zot/pkg/api/constants"
2021-12-04 03:50:58 +00:00
"zotregistry.io/zot/pkg/extensions/search"
cveinfo "zotregistry.io/zot/pkg/extensions/search/cve"
2022-07-15 11:10:51 +00:00
"zotregistry.io/zot/pkg/extensions/search/gql_generated"
2021-12-04 03:50:58 +00:00
"zotregistry.io/zot/pkg/log"
"zotregistry.io/zot/pkg/storage"
2020-10-14 14:47:20 -07:00
)
2022-09-28 21:39:54 +03:00
// We need this object to be a singleton as read/writes in the CVE DB may
// occur at any time via DB downloads as well as during scanning.
// The library doesn't seem to handle concurrency very well internally.
2022-10-05 13:21:14 +03:00
var cveInfo cveinfo . CveInfo //nolint:gochecknoglobals
2022-09-28 21:39:54 +03:00
func EnableSearchExtension ( config * config . Config , log log . Logger , storeController storage . StoreController ) {
2021-12-28 15:29:30 +02:00
if config . Extensions . Search != nil && * config . Extensions . Search . Enable && config . Extensions . Search . CVE != nil {
2020-10-14 14:47:20 -07:00
defaultUpdateInterval , _ := time . ParseDuration ( "2h" )
2021-06-08 23:11:18 +03:00
if config . Extensions . Search . CVE . UpdateInterval < defaultUpdateInterval {
config . Extensions . Search . CVE . UpdateInterval = defaultUpdateInterval
2020-10-14 14:47:20 -07:00
2022-03-21 17:37:23 +00:00
log . Warn ( ) . Msg ( "CVE update interval set to too-short interval < 2h, changing update duration to 2 hours and continuing." ) //nolint:lll // gofumpt conflicts with lll
2020-10-14 14:47:20 -07:00
}
2022-09-28 21:39:54 +03:00
cveInfo = cveinfo . NewCVEInfo ( storeController , log )
2020-10-14 14:47:20 -07:00
go func ( ) {
2022-09-28 21:39:54 +03:00
err := downloadTrivyDB ( log , config . Extensions . Search . CVE . UpdateInterval )
2020-10-14 14:47:20 -07:00
if err != nil {
2021-06-08 21:37:31 +03:00
log . Error ( ) . Err ( err ) . Msg ( "error while downloading TrivyDB" )
2020-10-14 14:47:20 -07:00
}
} ( )
} else {
2020-10-22 17:31:16 -07:00
log . Info ( ) . Msg ( "CVE config not provided, skipping CVE update" )
2020-10-14 14:47:20 -07:00
}
2022-04-27 09:00:20 +03:00
}
2021-06-08 23:11:18 +03:00
2022-09-28 21:39:54 +03:00
func downloadTrivyDB ( log log . Logger , updateInterval time . Duration ) error {
2022-04-27 09:00:20 +03:00
for {
log . Info ( ) . Msg ( "updating the CVE database" )
2021-10-28 12:10:01 +03:00
2022-09-28 21:39:54 +03:00
err := cveInfo . UpdateDB ( )
2022-04-27 09:00:20 +03:00
if err != nil {
return err
2021-10-28 12:10:01 +03:00
}
2022-04-27 09:00:20 +03:00
log . Info ( ) . Str ( "DB update completed, next update scheduled after" , updateInterval . String ( ) ) . Msg ( "" )
time . Sleep ( updateInterval )
2021-06-08 23:11:18 +03:00
}
2020-10-14 14:47:20 -07:00
}
2022-04-27 09:00:20 +03:00
func SetupSearchRoutes ( config * config . Config , router * mux . Router , storeController storage . StoreController ,
2022-09-29 23:28:39 +03:00
log log . Logger ,
2022-04-27 09:00:20 +03:00
) {
log . Info ( ) . Msg ( "setting up search routes" )
2022-03-04 09:37:06 +02:00
2022-04-27 09:00:20 +03:00
if config . Extensions . Search != nil && * config . Extensions . Search . Enable {
2022-07-15 11:10:51 +00:00
var resConfig gql_generated . Config
2022-03-04 09:37:06 +02:00
2022-04-27 09:00:20 +03:00
if config . Extensions . Search . CVE != nil {
2022-09-28 21:39:54 +03:00
// cveinfo should already be initialized by this time
// as EnableSearchExtension is supposed to be called earlier, but let's be sure
if cveInfo == nil {
cveInfo = cveinfo . NewCVEInfo ( storeController , log )
}
resConfig = search . GetResolverConfig ( log , storeController , cveInfo )
2022-05-10 01:30:11 +03:00
} else {
2022-09-28 21:39:54 +03:00
resConfig = search . GetResolverConfig ( log , storeController , nil )
2022-05-10 01:30:11 +03:00
}
2022-04-27 09:00:20 +03:00
2022-10-19 06:46:06 +03:00
graphqlPrefix := router . PathPrefix ( constants . FullSearchPrefix ) . Methods ( "OPTIONS" , "GET" , "POST" )
2022-08-16 11:57:09 +03:00
graphqlPrefix . Handler ( gqlHandler . NewDefaultServer ( gql_generated . NewExecutableSchema ( resConfig ) ) )
2022-03-04 09:37:06 +02:00
}
}
2022-05-23 19:22:52 +00:00
func getExtension ( name , url , description string , endpoints [ ] string ) distext . Extension {
2022-02-24 12:31:36 -08:00
return distext . Extension {
Name : name ,
URL : url ,
Description : description ,
2022-05-23 19:22:52 +00:00
Endpoints : endpoints ,
2022-02-24 12:31:36 -08:00
}
}
func GetExtensions ( config * config . Config ) distext . ExtensionList {
extensionList := distext . ExtensionList { }
extensions := make ( [ ] distext . Extension , 0 )
if config . Extensions != nil && config . Extensions . Search != nil {
2022-10-19 06:46:06 +03:00
endpoints := [ ] string { constants . FullSearchPrefix }
2022-05-25 17:49:22 +00:00
searchExt := getExtension ( "_zot" ,
2022-10-11 19:01:59 +03:00
"https://github.com/project-zot/zot/blob/" + config . ReleaseTag + "/pkg/extensions/_zot.md" ,
"zot registry extensions" ,
2022-05-25 17:49:22 +00:00
endpoints )
2022-02-24 12:31:36 -08:00
extensions = append ( extensions , searchExt )
}
extensionList . Extensions = extensions
return extensionList
}