diff --git a/commands/server.go b/commands/server.go index e21907d3..24921cde 100644 --- a/commands/server.go +++ b/commands/server.go @@ -208,7 +208,26 @@ func (p *ServerCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{} } } - http.Handle("/vuls", server.VulsHandler{}) + dbclient, locked, err := report.NewDBClient(report.DBClientConf{ + CveDictCnf: c.Conf.CveDict, + OvalDictCnf: c.Conf.OvalDict, + GostCnf: c.Conf.Gost, + ExploitCnf: c.Conf.Exploit, + DebugSQL: c.Conf.DebugSQL, + }) + if locked { + util.Log.Errorf("SQLite3 is locked. Close other DB connections and try again: %+v", err) + return subcommands.ExitFailure + } + + if err != nil { + util.Log.Errorf("Failed to init DB Clients. err: %+v", err) + return subcommands.ExitFailure + } + + defer dbclient.CloseDB() + + http.Handle("/vuls", server.VulsHandler{DBclient: *dbclient}) http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "ok") }) diff --git a/go.mod b/go.mod index 8bd7653f..5a7d6966 100644 --- a/go.mod +++ b/go.mod @@ -37,7 +37,7 @@ require ( github.com/knqyf263/trivy v0.1.4 github.com/kotakanbe/go-cve-dictionary v0.0.0-20190327053454-5fe52611f0b8 github.com/kotakanbe/go-pingscanner v0.1.0 - github.com/kotakanbe/goval-dictionary v0.1.4 + github.com/kotakanbe/goval-dictionary v0.2.0 github.com/kotakanbe/logrus-prefixed-formatter v0.0.0-20180123152602-928f7356cb96 github.com/lusis/go-slackbot v0.0.0-20180109053408-401027ccfef5 // indirect github.com/lusis/slack-test v0.0.0-20190426140909-c40012f20018 // indirect diff --git a/go.sum b/go.sum index 412f68c5..2e87128a 100644 --- a/go.sum +++ b/go.sum @@ -261,8 +261,8 @@ github.com/kotakanbe/go-cve-dictionary v0.0.0-20190327053454-5fe52611f0b8 h1:0zo github.com/kotakanbe/go-cve-dictionary v0.0.0-20190327053454-5fe52611f0b8/go.mod h1:CNVaCVSeqjxCFQm93uCWPT8mR+a0514XHiiBJx9yrkQ= github.com/kotakanbe/go-pingscanner v0.1.0 h1:VG4/9l0i8WeToXclj7bIGoAZAu7a07Z3qmQiIfU0gT0= github.com/kotakanbe/go-pingscanner v0.1.0/go.mod h1:/761QZzuZFcfN8h/1QuawUA+pKukp3qcNj5mxJCOiAk= -github.com/kotakanbe/goval-dictionary v0.1.4 h1:X0B9fCb9ogaVvHfJCvJwyOLNWiAHdkDD9tQA3GtuLGw= -github.com/kotakanbe/goval-dictionary v0.1.4/go.mod h1:VupP39J8370MdBkmvQQVmuYf98VrcQzhiGo+UiNW4rs= +github.com/kotakanbe/goval-dictionary v0.2.0 h1:Yq2F4ee+oLUWRGOzuptV1v5mIq43mahYPbVENocBlyI= +github.com/kotakanbe/goval-dictionary v0.2.0/go.mod h1:VupP39J8370MdBkmvQQVmuYf98VrcQzhiGo+UiNW4rs= github.com/kotakanbe/logrus-prefixed-formatter v0.0.0-20180123152602-928f7356cb96 h1:xNVK0mQJdQjw+QYeaMM4G6fvucWr8rTGGIhlPakx1wU= github.com/kotakanbe/logrus-prefixed-formatter v0.0.0-20180123152602-928f7356cb96/go.mod h1:ljq48H1V+0Vh0u7ucA3LjR4AfkAeCpxrf7LaaCk8Vmo= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= diff --git a/oval/redhat.go b/oval/redhat.go index 3218385c..115b80ff 100644 --- a/oval/redhat.go +++ b/oval/redhat.go @@ -152,7 +152,7 @@ func (o RedHatBase) update(r *models.ScanResult, defPacks defPacks) (nCVEs int) func (o RedHatBase) convertToDistroAdvisory(def *ovalmodels.Definition) *models.DistroAdvisory { advisoryID := def.Title - if o.family == config.RedHat || o.family == config.CentOS { + if (o.family == config.RedHat || o.family == config.CentOS) && len(advisoryID) > 0 { ss := strings.Fields(def.Title) advisoryID = strings.TrimSuffix(ss[0], ":") } diff --git a/oval/util.go b/oval/util.go index 4e42cd4f..53cc6a9b 100644 --- a/oval/util.go +++ b/oval/util.go @@ -238,7 +238,7 @@ func getDefsByPackNameFromOvalDB(driver db.DB, r *models.ScanResult) (relatedDef } for _, req := range requests { - definitions, err := driver.GetByPackName(r.Release, req.packName, req.arch) + definitions, err := driver.GetByPackName(r.Family, r.Release, req.packName, req.arch) if err != nil { return relatedDefs, xerrors.Errorf("Failed to get %s OVAL info by package: %#v, err: %w", r.Family, req, err) } diff --git a/server/server.go b/server/server.go index 8a62493f..34c63893 100644 --- a/server/server.go +++ b/server/server.go @@ -35,6 +35,7 @@ import ( // VulsHandler is used for vuls server mode type VulsHandler struct { + DBclient report.DBClient } func (h VulsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -69,26 +70,7 @@ func (h VulsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - dbclient, locked, err := report.NewDBClient(report.DBClientConf{ - CveDictCnf: c.Conf.CveDict, - OvalDictCnf: c.Conf.OvalDict, - GostCnf: c.Conf.Gost, - ExploitCnf: c.Conf.Exploit, - DebugSQL: c.Conf.DebugSQL, - }) - if locked { - util.Log.Errorf("SQLite3 is locked. Close other DB connections and try again: %+v", err) - return - } - - if err != nil { - util.Log.Errorf("Failed to init DB Clients. err: %+v", err) - return - } - - defer dbclient.CloseDB() - - if err := report.FillCveInfo(*dbclient, &result, []string{}, true); err != nil { + if err := report.FillCveInfo(h.DBclient, &result, []string{}, true); err != nil { util.Log.Error(err) http.Error(w, err.Error(), http.StatusServiceUnavailable) return