build: add build tags to create customizable binaries

This commit is contained in:
Shivam Mishra
2020-10-14 14:47:20 -07:00
parent 17dce7e63b
commit 46beb30fc1
27 changed files with 213 additions and 92 deletions
+20
View File
@@ -0,0 +1,20 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = [
"config.go",
"extension.go",
"minimal.go",
],
importpath = "github.com/anuvu/zot/pkg/extensions",
visibility = ["//visibility:public"],
deps = [
"//pkg/extensions/search:go_default_library",
"//pkg/extensions/search/cve:go_default_library",
"//pkg/log:go_default_library",
"//pkg/storage:go_default_library",
"@com_github_99designs_gqlgen//graphql/handler:go_default_library",
"@com_github_gorilla_mux//:go_default_library",
],
)
+16
View File
@@ -0,0 +1,16 @@
package extensions
import "time"
type ExtensionConfig struct {
Search *SearchConfig
}
type SearchConfig struct {
// CVE search
CVE *CVEConfig
}
type CVEConfig struct {
UpdateInterval time.Duration // should be 2 hours or more, if not specified default be kept as 24 hours
}
+61
View File
@@ -0,0 +1,61 @@
// +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 ...
func DownloadTrivyDB(dbDir string, log log.Logger, updateInterval time.Duration) error {
for {
log.Info().Msg("Updating the CVE database")
err := cveinfo.UpdateCVEDb(dbDir, log)
if err != nil {
return err
}
log.Info().Str("Db update completed, next update scheduled after", updateInterval.String()).Msg("")
time.Sleep(updateInterval)
}
}
func EnableExtension(extension *ExtensionConfig, log log.Logger, rootDir string) {
if extension != nil && extension.Search != nil &&
extension.Search.CVE != nil {
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() {
err := DownloadTrivyDB(rootDir, log,
extension.Search.CVE.UpdateInterval)
if err != nil {
panic(err)
}
}()
} else {
log.Info().Msg("Cve config not provided, skipping cve update")
}
}
func SetupRoutes(router *mux.Router, rootDir string, imgStore *storage.ImageStore, log log.Logger) {
resConfig := search.GetResolverConfig(rootDir, log, imgStore)
router.PathPrefix("/query").Methods("GET", "POST").
Handler(gqlHandler.NewDefaultServer(search.NewExecutableSchema(resConfig)))
}
+23
View File
@@ -0,0 +1,23 @@
// +build minimal
package extensions
import (
"time"
"github.com/anuvu/zot/pkg/log"
"github.com/anuvu/zot/pkg/storage"
"github.com/gorilla/mux"
)
// DownloadTrivyDB ...
func DownloadTrivyDB(dbDir string, log log.Logger, updateInterval time.Duration) error {
return nil
}
func EnableExtension(extension *ExtensionConfig, log log.Logger, rootDir string) {
log.Info().Msg("given zot binary doesn't support any extensions, please build zot full binary for this feature")
}
func SetupRoutes(router *mux.Router, rootDir string, imgStore *storage.ImageStore, log log.Logger) {
}
+1
View File
@@ -30,6 +30,7 @@ go_test(
embed = [":go_default_library"],
deps = [
"//pkg/api:go_default_library",
"//pkg/extensions:go_default_library",
"//pkg/log:go_default_library",
"@com_github_opencontainers_image_spec//specs-go/v1:go_default_library",
"@com_github_smartystreets_goconvey//convey:go_default_library",
+4 -6
View File
@@ -13,6 +13,7 @@ import (
"time"
"github.com/anuvu/zot/pkg/api"
ext "github.com/anuvu/zot/pkg/extensions"
cveinfo "github.com/anuvu/zot/pkg/extensions/search/cve"
"github.com/anuvu/zot/pkg/log"
ispec "github.com/opencontainers/image-spec/specs-go/v1"
@@ -461,7 +462,6 @@ func TestImageTag(t *testing.T) {
func TestCVESearch(t *testing.T) {
Convey("Test image vulenrability scanning", t, func() {
updateDuration, _ := time.ParseDuration("1h")
expectedDuration, _ := time.ParseDuration("2h")
config := api.NewConfig()
config.HTTP.Port = SecurePort1
htpasswdPath := makeHtpasswdFile()
@@ -475,13 +475,13 @@ func TestCVESearch(t *testing.T) {
c := api.NewController(config)
defer os.RemoveAll(dbDir)
c.Config.Storage.RootDirectory = dbDir
cveConfig := &api.CVEConfig{
cveConfig := &ext.CVEConfig{
UpdateInterval: updateDuration,
}
searchConfig := &api.SearchConfig{
searchConfig := &ext.SearchConfig{
CVE: cveConfig,
}
c.Config.Extensions = &api.ExtensionConfig{
c.Config.Extensions = &ext.ExtensionConfig{
Search: searchConfig,
}
go func() {
@@ -508,8 +508,6 @@ func TestCVESearch(t *testing.T) {
_ = c.Server.Shutdown(ctx)
}()
So(c.Config.Extensions.Search.CVE.UpdateInterval, ShouldEqual, expectedDuration)
// without creds, should get access error
resp, err := resty.R().Get(BaseURL1 + "/v2/")
So(err, ShouldBeNil)