Skip to content

Commit 3c918a5

Browse files
authored
refactor(vulnfeeds): consolidate models (#4650)
moved CVE / NVD models into the models dir bc they share some things
1 parent c2bfb64 commit 3c918a5

31 files changed

Lines changed: 694 additions & 661 deletions

File tree

vulnfeeds/cmd/combine-to-osv/main.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import (
1111
"log/slog"
1212
"os"
1313
"path/filepath"
14-
"strings"
15-
1614
"slices"
15+
"strings"
1716

1817
"cloud.google.com/go/storage"
1918
"github.com/google/osv/vulnfeeds/cves"
19+
"github.com/google/osv/vulnfeeds/models"
2020
"github.com/google/osv/vulnfeeds/upload"
2121
"github.com/google/osv/vulnfeeds/utility/logger"
2222
"github.com/ossf/osv-schema/bindings/go/osvschema"
@@ -138,8 +138,8 @@ func listBucketObjects(bucketName string, prefix string) ([]string, error) {
138138
// The function returns a map of CVE IDs to their corresponding Vulnerability objects.
139139
// Files that are not ".json" files, directories, or files ending in ".metrics.json" are skipped.
140140
// The function will log warnings for files that fail to open or decode, and will terminate if it fails to walk the directory.
141-
func loadOSV(osvPath string) map[cves.CVEID]*osvschema.Vulnerability {
142-
allVulns := make(map[cves.CVEID]*osvschema.Vulnerability)
141+
func loadOSV(osvPath string) map[models.CVEID]*osvschema.Vulnerability {
142+
allVulns := make(map[models.CVEID]*osvschema.Vulnerability)
143143
logger.Info("Loading OSV records", slog.String("path", osvPath))
144144
err := filepath.WalkDir(osvPath, func(path string, d fs.DirEntry, err error) error {
145145
if err != nil {
@@ -161,7 +161,7 @@ func loadOSV(osvPath string) map[cves.CVEID]*osvschema.Vulnerability {
161161
logger.Error("Failed to decode, skipping", slog.String("file", path), slog.Any("err", decodeErr))
162162
return nil
163163
}
164-
allVulns[cves.CVEID(vuln.GetId())] = &vuln
164+
allVulns[models.CVEID(vuln.GetId())] = &vuln
165165

166166
return nil
167167
})
@@ -174,8 +174,8 @@ func loadOSV(osvPath string) map[cves.CVEID]*osvschema.Vulnerability {
174174
}
175175

176176
// combineIntoOSV creates OSV entry by combining loaded CVEs from NVD and PackageInfo information from security advisories.
177-
func combineIntoOSV(cve5osv map[cves.CVEID]*osvschema.Vulnerability, nvdosv map[cves.CVEID]*osvschema.Vulnerability, mandatoryCVEIDs []string) map[cves.CVEID]*osvschema.Vulnerability {
178-
osvRecords := make(map[cves.CVEID]*osvschema.Vulnerability)
177+
func combineIntoOSV(cve5osv map[models.CVEID]*osvschema.Vulnerability, nvdosv map[models.CVEID]*osvschema.Vulnerability, mandatoryCVEIDs []string) map[models.CVEID]*osvschema.Vulnerability {
178+
osvRecords := make(map[models.CVEID]*osvschema.Vulnerability)
179179

180180
// Iterate through CVEs from security advisories (cve5) as the base
181181
for cveID, cve5 := range cve5osv {

vulnfeeds/cmd/combine-to-osv/main_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
"github.com/google/go-cmp/cmp"
1010
"github.com/google/go-cmp/cmp/cmpopts"
11-
"github.com/google/osv/vulnfeeds/cves"
11+
"github.com/google/osv/vulnfeeds/models"
1212
"github.com/ossf/osv-schema/bindings/go/osvschema"
1313
"google.golang.org/protobuf/testing/protocmp"
1414
"google.golang.org/protobuf/types/known/timestamppb"
@@ -35,7 +35,7 @@ func TestCombineIntoOSV(t *testing.T) {
3535

3636
cve5osv := loadOSV(cve5Path)
3737
nvdosv := loadOSV(nvdPath)
38-
nvdosvCopy := make(map[cves.CVEID]*osvschema.Vulnerability)
38+
nvdosvCopy := make(map[models.CVEID]*osvschema.Vulnerability)
3939
for k, v := range nvdosv {
4040
nvdosvCopy[k] = v
4141
}

vulnfeeds/cmd/converters/alpine/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"strings"
1616
"time"
1717

18-
"github.com/google/osv/vulnfeeds/cves"
1918
"github.com/google/osv/vulnfeeds/models"
2019
"github.com/google/osv/vulnfeeds/upload"
2120
"github.com/google/osv/vulnfeeds/utility/logger"
@@ -138,7 +137,7 @@ func getAlpineSecDBData() map[string][]VersionAndPkg {
138137
}
139138

140139
// generateAlpineOSV generates the generic PackageInfo package from the information given by alpine advisory
141-
func generateAlpineOSV(allAlpineSecDb map[string][]VersionAndPkg, allCVEs map[cves.CVEID]cves.Vulnerability) (osvVulnerabilities []*vulns.Vulnerability) {
140+
func generateAlpineOSV(allAlpineSecDb map[string][]VersionAndPkg, allCVEs map[models.CVEID]models.Vulnerability) (osvVulnerabilities []*vulns.Vulnerability) {
142141
cveIDs := make([]string, 0, len(allAlpineSecDb))
143142
for cveID := range allAlpineSecDb {
144143
cveIDs = append(cveIDs, cveID)
@@ -157,7 +156,7 @@ func generateAlpineOSV(allAlpineSecDb map[string][]VersionAndPkg, allCVEs map[cv
157156

158157
return verPkgs[i].Ver < verPkgs[j].Ver
159158
})
160-
cve, ok := allCVEs[cves.CVEID(cveID)]
159+
cve, ok := allCVEs[models.CVEID(cveID)]
161160
var published time.Time
162161
var details string
163162
if ok {

vulnfeeds/cmd/converters/cve/cve5/bulk-converter/main.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ import (
1414
"sync"
1515
"time"
1616

17+
"github.com/google/osv/vulnfeeds/conversion"
1718
"github.com/google/osv/vulnfeeds/cvelist2osv"
18-
"github.com/google/osv/vulnfeeds/cves"
19+
"github.com/google/osv/vulnfeeds/models"
1920
"github.com/google/osv/vulnfeeds/utility/logger"
2021
)
2122

@@ -34,7 +35,7 @@ func main() {
3435
flag.Parse()
3536
logger.InitGlobalLogger()
3637

37-
logger.Info("Commencing Linux CVE to OSV conversion run")
38+
logger.Info("Commencing CVE to OSV conversion run")
3839
if err := os.MkdirAll(*localOutputDir, 0755); err != nil {
3940
logger.Fatal("Failed to create local output directory", slog.Any("err", err))
4041
}
@@ -103,7 +104,7 @@ func worker(wg *sync.WaitGroup, jobs <-chan string, outDir string, cnas []string
103104
continue
104105
}
105106

106-
var cve cves.CVE5
107+
var cve models.CVE5
107108
if err := json.Unmarshal(data, &cve); err != nil {
108109
logger.Info("Failed to unmarshal JSON", slog.String("path", path), slog.Any("err", err))
109110
continue
@@ -115,8 +116,8 @@ func worker(wg *sync.WaitGroup, jobs <-chan string, outDir string, cnas []string
115116
cveID := cve.Metadata.CVEID
116117
logger.Info("Processing "+string(cveID), slog.String("cve", string(cveID)))
117118

118-
osvFile, errCVE := cvelist2osv.CreateOSVFile(cveID, outDir)
119-
metricsFile, errMetrics := cvelist2osv.CreateMetricsFile(cveID, outDir)
119+
osvFile, errCVE := conversion.CreateOSVFile(cveID, outDir)
120+
metricsFile, errMetrics := conversion.CreateMetricsFile(cveID, outDir)
120121
if errCVE != nil || errMetrics != nil {
121122
logger.Fatal("File failed to be created for CVE", slog.String("cve", string(cveID)))
122123
}

vulnfeeds/cmd/converters/cve/cve5/single-converter/main.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import (
77
"log/slog"
88
"os"
99

10+
"github.com/google/osv/vulnfeeds/conversion"
1011
"github.com/google/osv/vulnfeeds/cvelist2osv"
11-
"github.com/google/osv/vulnfeeds/cves"
12+
"github.com/google/osv/vulnfeeds/models"
1213
"github.com/google/osv/vulnfeeds/utility/logger"
1314
)
1415

@@ -28,7 +29,7 @@ func main() {
2829
logger.Fatal("Failed to open file", slog.Any("err", err))
2930
}
3031

31-
var cve cves.CVE5
32+
var cve models.CVE5
3233
if err = json.Unmarshal(data, &cve); err != nil {
3334
logger.Fatal("Failed to parse CVEList CVE JSON", slog.Any("err", err))
3435
}
@@ -44,8 +45,8 @@ func main() {
4445
}
4546
// create the files
4647

47-
osvFile, errCVE := cvelist2osv.CreateOSVFile(cveID, outDir)
48-
metricsFile, errMetrics := cvelist2osv.CreateMetricsFile(cveID, outDir)
48+
osvFile, errCVE := conversion.CreateOSVFile(cveID, outDir)
49+
metricsFile, errMetrics := conversion.CreateMetricsFile(cveID, outDir)
4950
if errCVE != nil || errMetrics != nil {
5051
logger.Fatal("File failed to be created for CVE", slog.String("cve", string(cveID)))
5152
}

0 commit comments

Comments
 (0)