Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: updating cve severity #37

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions internal/sql/repository/CveStoreRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type CveStoreRepository interface {
FindByCveNames(names []string) ([]*CveStore, error)
FindByName(name string) (*CveStore, error)
Update(model *CveStore) error
UpdateInBatch(models []*CveStore, tx *pg.Tx) error
}

type CveStoreRepositoryImpl struct {
Expand Down Expand Up @@ -75,3 +76,13 @@ func (impl CveStoreRepositoryImpl) Update(team *CveStore) error {
err := impl.dbConnection.Update(team)
return err
}

func (impl CveStoreRepositoryImpl) UpdateInBatch(models []*CveStore, tx *pg.Tx) error {
for _, val := range models {
err := tx.Update(val)
if err != nil {
return err
}
}
return nil
}
39 changes: 33 additions & 6 deletions pkg/security/ImageScanService.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ func (impl *ImageScanServiceImpl) ConvertEndStepOutputAndSaveVulnerabilities(ste

allCvesMap := make([]*repository.CveStore, 0, len(vulnerabilities))
cvesToBeSaved := make([]*repository.CveStore, 0, len(vulnerabilities))
cvesToBeUpdated := make([]*repository.CveStore, 0, len(vulnerabilities))
uniqueVulnerabilityMap := make(map[string]*bean.ImageScanOutputObject)
allCvesNames := make([]string, 0, len(vulnerabilities))
for _, vul := range vulnerabilities {
Expand Down Expand Up @@ -423,10 +424,13 @@ func (impl *ImageScanServiceImpl) ConvertEndStepOutputAndSaveVulnerabilities(ste
}
for _, vul := range uniqueVulnerabilityMap {
var cve *repository.CveStore
if val, ok := allSavedCvesMap[vul.Name]; ok {
cve = val
var hasCVEInfoChanged bool
existingCve, ok := allSavedCvesMap[vul.Name]
if ok {
hasCVEInfoChanged = checkIfCveInfoHasChanged(existingCve, vul)
cve = existingCve
}
if cve == nil {
if cve == nil || hasCVEInfoChanged {
cve = &repository.CveStore{
Name: vul.Name,
Package: vul.Package,
Expand All @@ -436,11 +440,17 @@ func (impl *ImageScanServiceImpl) ConvertEndStepOutputAndSaveVulnerabilities(ste
lowerCaseSeverity := bean.ConvertToLowerCase(vul.Severity)
cve.Severity = bean.ConvertToSeverityUtility(lowerCaseSeverity)
cve.StandardSeverity = bean.ConvertToStandardSeverityUtility(lowerCaseSeverity)
cve.CreatedOn = time.Now()
cve.CreatedBy = userId
cve.UpdatedOn = time.Now()
cve.UpdatedBy = userId
cvesToBeSaved = append(cvesToBeSaved, cve)
if hasCVEInfoChanged {
cve.CreatedOn = existingCve.CreatedOn
cve.CreatedBy = existingCve.CreatedBy
cvesToBeUpdated = append(cvesToBeUpdated, cve)
} else {
cve.CreatedOn = time.Now()
cve.CreatedBy = userId
cvesToBeSaved = append(cvesToBeSaved, cve)
}
}
allCvesMap = append(allCvesMap, cve)
}
Expand Down Expand Up @@ -468,6 +478,13 @@ func (impl *ImageScanServiceImpl) ConvertEndStepOutputAndSaveVulnerabilities(ste
return err
}
}
if len(cvesToBeUpdated) > 0 {
err = impl.cveStoreRepository.UpdateInBatch(cvesToBeUpdated, tx)
if err != nil {
impl.logger.Errorw("error in updating cves in batch", "err", err)
return err
}
}
if len(imageScanExecutionResults) > 0 {
err = impl.scanResultRepository.SaveInBatch(imageScanExecutionResults, tx)
if err != nil {
Expand All @@ -483,6 +500,16 @@ func (impl *ImageScanServiceImpl) ConvertEndStepOutputAndSaveVulnerabilities(ste
return nil
}

func checkIfCveInfoHasChanged(oldCve *repository.CveStore, newCve *bean.ImageScanOutputObject) bool {
lowerCaseSeverity := bean.ConvertToLowerCase(newCve.Severity)
severityInDevtron := bean.ConvertToSeverityUtility(lowerCaseSeverity)
// check if some info (like severity) has changed in cve
if newCve.Package != oldCve.Package || newCve.FixedInVersion != oldCve.FixedVersion || severityInDevtron != oldCve.Severity {
return true
}
return false
}

func isV1Template(resultDescriptorTemplate string) bool {
var mappings []bean.Mapping
err := json.Unmarshal([]byte(resultDescriptorTemplate), &mappings)
Expand Down