Skip to content

Commit 0b21ce3

Browse files
committed
refactor: clean up functions a bit more
1 parent 274ea18 commit 0b21ce3

2 files changed

Lines changed: 79 additions & 69 deletions

File tree

pkg/osvscanner/configs.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package osvscanner
2+
3+
import (
4+
"github.com/google/osv-scanner/v2/internal/config"
5+
"github.com/google/osv-scanner/v2/pkg/models"
6+
"github.com/ossf/osv-schema/bindings/go/osvschema"
7+
)
8+
9+
func addVulnConfigIgnores(vulnResults *models.VulnerabilityResults, manager *config.Manager) {
10+
configVulns := make(map[string][]*osvschema.Vulnerability)
11+
configPaths := make(map[string]config.Config)
12+
13+
for _, pkgSrc := range vulnResults.Results {
14+
c := manager.Get(pkgSrc.Source.Path)
15+
16+
// skip the default config
17+
if c.LoadPath == "" {
18+
continue
19+
}
20+
21+
configPaths[c.LoadPath] = c
22+
23+
for _, pkgVulns := range pkgSrc.Packages {
24+
configVulns[c.LoadPath] = append(configVulns[c.LoadPath], pkgVulns.Vulnerabilities...)
25+
}
26+
}
27+
28+
// update each config to ignore all the vulnerabilities
29+
// found across all packages that are using that config
30+
for p, vulns := range configVulns {
31+
c := configPaths[p]
32+
33+
c.IgnoreVulns(vulns)
34+
}
35+
}
36+
37+
func removeAllUnusedConfigIgnores(manager *config.Manager) {
38+
if manager.OverrideConfig != nil {
39+
manager.OverrideConfig.RemoveUnusedIgnores()
40+
}
41+
42+
for _, c := range manager.ConfigMap {
43+
// skip the default config
44+
if c.LoadPath == "" {
45+
continue
46+
}
47+
48+
c.RemoveUnusedIgnores()
49+
}
50+
}
51+
52+
func saveAllConfigs(manager *config.Manager) error {
53+
if manager.OverrideConfig != nil {
54+
err := manager.OverrideConfig.Save()
55+
if err != nil {
56+
return err
57+
}
58+
}
59+
60+
for _, c := range manager.ConfigMap {
61+
// skip the default config
62+
if c.LoadPath == "" {
63+
continue
64+
}
65+
66+
err := c.Save()
67+
if err != nil {
68+
return err
69+
}
70+
}
71+
72+
return nil
73+
}

pkg/osvscanner/osvscanner.go

Lines changed: 6 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737
"github.com/google/osv-scanner/v2/pkg/models"
3838
"github.com/google/osv-scanner/v2/pkg/osvscanner/internal/imagehelpers"
3939
"github.com/ossf/osv-schema/bindings/go/osvconstants"
40-
"github.com/ossf/osv-schema/bindings/go/osvschema"
4140
"osv.dev/bindings/go/osvdev"
4241
)
4342

@@ -422,11 +421,7 @@ func finalizeScanResult(scanResult results.ScanResults, actions ScannerActions)
422421
// - c: filtering removes vulns from results, so need to account for that
423422
if actions.UpdateConfigIgnores == "all" {
424423
// todo: add output about having ignored vulns
425-
err := updateConfigs(&vulnerabilityResults, &scanResult.ConfigManager)
426-
427-
if err != nil {
428-
return models.VulnerabilityResults{}, err
429-
}
424+
addVulnConfigIgnores(&vulnerabilityResults, &scanResult.ConfigManager)
430425
}
431426

432427
filtered := filterResults(&vulnerabilityResults, &scanResult.ConfigManager, actions.ShowAllPackages)
@@ -440,7 +435,11 @@ func finalizeScanResult(scanResult results.ScanResults, actions ScannerActions)
440435

441436
if actions.UpdateConfigIgnores == "unused" {
442437
// todo: add output about having ignored vulns
443-
err := updateConfigsToRemoveUnusedIgnores(&scanResult.ConfigManager)
438+
removeAllUnusedConfigIgnores(&scanResult.ConfigManager)
439+
}
440+
441+
if actions.UpdateConfigIgnores != "" && actions.UpdateConfigIgnores != "none" {
442+
err := saveAllConfigs(&scanResult.ConfigManager)
444443

445444
if err != nil {
446445
return models.VulnerabilityResults{}, err
@@ -463,68 +462,6 @@ func finalizeScanResult(scanResult results.ScanResults, actions ScannerActions)
463462
return vulnerabilityResults, determineReturnErr(vulnerabilityResults, actions.ShowAllVulns)
464463
}
465464

466-
func updateConfigs(vulnResults *models.VulnerabilityResults, configManager *config.Manager) error {
467-
configVulns := make(map[string][]*osvschema.Vulnerability)
468-
configPaths := make(map[string]config.Config)
469-
470-
for _, pkgSrc := range vulnResults.Results {
471-
c := configManager.Get(pkgSrc.Source.Path)
472-
473-
// skip the default config
474-
if c.LoadPath == "" {
475-
continue
476-
}
477-
478-
configPaths[c.LoadPath] = c
479-
480-
for _, pkgVulns := range pkgSrc.Packages {
481-
configVulns[c.LoadPath] = append(configVulns[c.LoadPath], pkgVulns.Vulnerabilities...)
482-
}
483-
}
484-
485-
// update each config to ignore all the vulnerabilities
486-
// found across all packages that are using that config
487-
for p, vulns := range configVulns {
488-
c := configPaths[p]
489-
490-
c.IgnoreVulns(vulns)
491-
492-
err := c.Save()
493-
if err != nil {
494-
return err
495-
}
496-
}
497-
498-
return nil
499-
}
500-
501-
func updateConfigsToRemoveUnusedIgnores(configManager *config.Manager) error {
502-
if configManager.OverrideConfig != nil {
503-
configManager.OverrideConfig.RemoveUnusedIgnores()
504-
505-
err := configManager.OverrideConfig.Save()
506-
if err != nil {
507-
return err
508-
}
509-
}
510-
511-
for _, c := range configManager.ConfigMap {
512-
// skip the default config
513-
if c.LoadPath == "" {
514-
continue
515-
}
516-
517-
c.RemoveUnusedIgnores()
518-
519-
err := c.Save()
520-
if err != nil {
521-
return err
522-
}
523-
}
524-
525-
return nil
526-
}
527-
528465
func buildLicenseSummary(scanResult *results.ScanResults) []models.LicenseCount {
529466
var licenseSummary []models.LicenseCount
530467

0 commit comments

Comments
 (0)