Refacotring

This commit is contained in:
Kota Kanbe
2017-05-05 09:50:59 +09:00
committed by kota kanbe
parent 17a4e532c1
commit a2c364f9eb
8 changed files with 175 additions and 130 deletions

View File

@@ -21,13 +21,14 @@ func NewDebian() Debian {
}
// FillCveInfoFromOvalDB returns scan result after updating CVE info by OVAL
func (o Debian) FillCveInfoFromOvalDB(r *models.ScanResult) (*models.ScanResult, error) {
util.Log.Debugf("open oval-dictionary db (%s)", config.Conf.OvalDBType)
func (o Debian) FillCveInfoFromOvalDB(r *models.ScanResult) error {
ovalconf.Conf.DBType = config.Conf.OvalDBType
ovalconf.Conf.DBPath = config.Conf.OvalDBPath
util.Log.Infof("open oval-dictionary db (%s): %s",
config.Conf.OvalDBType, config.Conf.OvalDBPath)
if err := db.OpenDB(); err != nil {
return nil, fmt.Errorf("Failed to open OVAL DB. err: %s", err)
return fmt.Errorf("Failed to open OVAL DB. err: %s", err)
}
var d db.OvalDB
@@ -40,27 +41,27 @@ func (o Debian) FillCveInfoFromOvalDB(r *models.ScanResult) (*models.ScanResult,
for _, pack := range r.Packages {
definitions, err := d.GetByPackName(r.Release, pack.Name)
if err != nil {
return nil, fmt.Errorf("Failed to get Debian OVAL info by package name: %v", err)
return fmt.Errorf("Failed to get Debian OVAL info by package name: %v", err)
}
for _, definition := range definitions {
for _, def := range definitions {
current, _ := ver.NewVersion(pack.Version)
for _, p := range definition.AffectedPacks {
for _, p := range def.AffectedPacks {
if pack.Name != p.Name {
continue
}
affected, _ := ver.NewVersion(p.Version)
if current.LessThan(affected) {
r = o.fillOvalInfo(r, &definition)
o.fillOvalInfo(r, &def)
}
}
}
}
return r, nil
return nil
}
func (o Debian) fillOvalInfo(r *models.ScanResult, definition *ovalmodels.Definition) *models.ScanResult {
func (o Debian) fillOvalInfo(r *models.ScanResult, definition *ovalmodels.Definition) {
ovalContent := *o.convertToModel(definition)
ovalContent.Type = models.CveContentType(r.Family)
ovalContent.Type = models.NewCveContentType(r.Family)
vinfo, ok := r.ScannedCves.Get(definition.Debian.CveID)
if !ok {
util.Log.Infof("%s is newly detected by OVAL",
@@ -72,7 +73,7 @@ func (o Debian) fillOvalInfo(r *models.ScanResult, definition *ovalmodels.Defini
CveContents: []models.CveContent{ovalContent},
}
} else {
if _, ok := vinfo.CveContents.Get(models.CveContentType(r.Family)); !ok {
if _, ok := vinfo.CveContents.Get(models.NewCveContentType(r.Family)); !ok {
util.Log.Infof("%s is also detected by OVAL", definition.Debian.CveID)
} else {
util.Log.Infof("%s will be updated by OVAL", definition.Debian.CveID)
@@ -83,7 +84,6 @@ func (o Debian) fillOvalInfo(r *models.ScanResult, definition *ovalmodels.Defini
vinfo.CveContents.Upsert(ovalContent)
}
r.ScannedCves.Upsert(vinfo)
return r
}
func (o Debian) convertToModel(def *ovalmodels.Definition) *models.CveContent {

View File

@@ -7,7 +7,7 @@ import (
// Client is the interface of OVAL client.
type Client interface {
FillCveInfoFromOvalDB(r *models.ScanResult) (*models.ScanResult, error)
FillCveInfoFromOvalDB(r *models.ScanResult) error
}
func getPackageInfoList(r *models.ScanResult, d *ovalmodels.Definition) models.PackageInfoList {

View File

@@ -23,14 +23,14 @@ func NewRedhat() Redhat {
}
// FillCveInfoFromOvalDB returns scan result after updating CVE info by OVAL
func (o Redhat) FillCveInfoFromOvalDB(r *models.ScanResult) (*models.ScanResult, error) {
util.Log.Debugf("open oval-dictionary db (%s)", config.Conf.OvalDBType)
func (o Redhat) FillCveInfoFromOvalDB(r *models.ScanResult) error {
ovalconf.Conf.DBType = config.Conf.OvalDBType
ovalconf.Conf.DBPath = config.Conf.OvalDBPath
util.Log.Infof("open oval-dictionary db (%s): %s",
config.Conf.OvalDBType, config.Conf.OvalDBPath)
if err := db.OpenDB(); err != nil {
return nil, fmt.Errorf("Failed to open OVAL DB. err: %s", err)
return fmt.Errorf("Failed to open OVAL DB. err: %s", err)
}
d := db.NewRedHat()
@@ -38,7 +38,7 @@ func (o Redhat) FillCveInfoFromOvalDB(r *models.ScanResult) (*models.ScanResult,
for _, pack := range r.Packages {
definitions, err := d.GetByPackName(r.Release, pack.Name)
if err != nil {
return nil, fmt.Errorf("Failed to get RedHat OVAL info by package name: %v", err)
return fmt.Errorf("Failed to get RedHat OVAL info by package name: %v", err)
}
for _, definition := range definitions {
current, _ := ver.NewVersion(fmt.Sprintf("%s-%s", pack.Version, pack.Release))
@@ -48,15 +48,15 @@ func (o Redhat) FillCveInfoFromOvalDB(r *models.ScanResult) (*models.ScanResult,
}
affected, _ := ver.NewVersion(p.Version)
if current.LessThan(affected) {
r = o.fillOvalInfo(r, &definition)
o.fillOvalInfo(r, &definition)
}
}
}
}
return r, nil
return nil
}
func (o Redhat) fillOvalInfo(r *models.ScanResult, definition *ovalmodels.Definition) *models.ScanResult {
func (o Redhat) fillOvalInfo(r *models.ScanResult, definition *ovalmodels.Definition) {
for _, cve := range definition.Advisory.Cves {
ovalContent := *o.convertToModel(cve.CveID, definition)
vinfo, ok := r.ScannedCves.Get(cve.CveID)
@@ -81,7 +81,6 @@ func (o Redhat) fillOvalInfo(r *models.ScanResult, definition *ovalmodels.Defini
}
r.ScannedCves.Upsert(vinfo)
}
return r
}
func (o Redhat) convertToModel(cveID string, def *ovalmodels.Definition) *models.CveContent {