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

rewrite vmaas cves endpoint in go #78

Merged
merged 5 commits into from
Oct 31, 2024
Merged
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
39 changes: 39 additions & 0 deletions vmaas/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,42 @@ func ShouldReload(c *Cache, latestDumpEndpoint string) bool {
utils.LogDebug("latest", latest, "exported", exported, "Cache reload not needed")
return false
}

func (c *Cache) errataIDs2Names(eids []int) []string {
names := make([]string, 0, len(eids))
for _, eid := range eids {
names = append(names, c.ErratumID2Name[ErratumID(eid)])
}
return names
}

func (c *Cache) pkgDetail2Nevra(pkgDetail PackageDetail) string {
evr := c.ID2Evr[pkgDetail.EvrID]
nevra := utils.Nevra{
Name: c.ID2Packagename[pkgDetail.NameID],
Epoch: evr.Epoch,
Version: evr.Version,
Release: evr.Release,
Arch: c.ID2Arch[pkgDetail.ArchID],
}
return nevra.String()
}

func (c *Cache) packageIDs2Nevras(pkgIDs []int) ([]string, []string) {
binPackages := make([]string, 0, len(pkgIDs))
sourcePackages := make([]string, 0, len(pkgIDs))
sourceArchID := c.Arch2ID["src"]
for _, pkgID := range pkgIDs {
pkgDetail := c.PackageDetails[PkgID(pkgID)]
nevra := c.pkgDetail2Nevra(pkgDetail)
if nevra == "" {
continue
}
if pkgDetail.ArchID == sourceArchID {
sourcePackages = append(sourcePackages, nevra)
} else {
binPackages = append(binPackages, nevra)
}
}
return binPackages, sourcePackages
}
78 changes: 78 additions & 0 deletions vmaas/cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package vmaas

import (
"testing"
"time"

"github.com/redhatinsights/vmaas-lib/vmaas/utils"
"github.com/stretchr/testify/assert"
)

func TestErrataIDs2Names(t *testing.T) {
c := mockCache()
errataNames := c.errataIDs2Names([]int{1, 2})
assert.Equal(t, 2, len(errataNames))
}

func TestPkgDetail2Nevra(t *testing.T) {
c := mockCache()
pkgDetail := c.PackageDetails[PkgID(1)]
nevra := c.pkgDetail2Nevra(pkgDetail)
assert.Equal(t, "kernel-1:1-1.x86_64", nevra)
}

func TestPackageIDs2Nevras(t *testing.T) {
c := mockCache()
binPackages, sourcePackages := c.packageIDs2Nevras([]int{1, 3})
assert.Equal(t, 1, len(binPackages))
assert.Equal(t, 1, len(sourcePackages))
assert.Equal(t, "kernel-1:1-1.x86_64", binPackages[0])
assert.Equal(t, "kernel-devel-1:1-1.src", sourcePackages[0])
}

func mockCache() *Cache {
modifiedDate, _ := time.Parse(time.RFC3339, "2024-10-03T11:44:00+02:00")
publishedDate, _ := time.Parse(time.RFC3339, "2024-10-03T11:44:00+02:00")
return &Cache{
ID2Packagename: map[NameID]string{1: "kernel", 2: "kernel-devel"},

ID2Evr: map[EvrID]utils.Evr{
1: {Epoch: 1, Version: "1", Release: "1"},
2: {Epoch: 0, Version: "2", Release: "2"},
},

Arch2ID: map[string]ArchID{
"x86_64": 1,
"src": 2,
},
ID2Arch: map[ArchID]string{
1: "x86_64",
2: "src",
},

PackageDetails: map[PkgID]PackageDetail{
1: {NameID: 1, EvrID: 1, ArchID: 1}, // kernel-1:1-1
2: {NameID: 1, EvrID: 2, ArchID: 1}, // kernel-0:2-2
3: {NameID: 2, EvrID: 1, ArchID: 2}, // kernel-devel-1:1-1
},

ErratumID2Name: map[ErratumID]string{
1: "RHSA-2024:0042",
2: "RHSA-2024:1111",
},

CveDetail: map[string]CveDetail{
"CVE-2024-21345": {
Source: "Red Hat",
ModifiedDate: &modifiedDate,
PublishedDate: &publishedDate,
},
"CVE-2024-1234": {
ErrataIDs: []int{1, 2},
},
"CVE-2024-1111111": {},
},

DBChange: DBChange{LastChange: "2024-10-02T16:08:00+02:00"},
}
}
91 changes: 91 additions & 0 deletions vmaas/cves.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package vmaas

import (
"slices"

"github.com/pkg/errors"
"github.com/redhatinsights/vmaas-lib/vmaas/utils"
)

type CveDetails map[string]CveDetail

type Cves struct {
Cves CveDetails `json:"cve_list"`
LastChange string `json:"last_change"`
utils.PaginationDetails
}

func (req *CvesRequest) getSortedCves(c *Cache) ([]string, error) {
cves := req.Cves
if len(cves) == 0 {
return nil, errors.New("cve_list must contain at least one item")
}
cves = utils.TryExpandRegexPattern(cves, c.CveDetail)
slices.Sort(cves)
return cves, nil
}

func filterInputCves(c *Cache, cves []string, req *CvesRequest) []string {
filteredIDs := make([]string, 0, len(cves))
for _, cve := range cves {
if cve == "" {
continue
}
cveDetail, found := c.CveDetail[cve]
if !found {
continue
}
if req.RHOnly && cveDetail.Source != "Red Hat" {
continue
}
if req.AreErrataAssociated && len(cveDetail.ErrataIDs) == 0 {
// FIXME: also check CSAF
continue
}

if req.ModifiedSince != nil && cveDetail.ModifiedDate != nil {
if cveDetail.ModifiedDate.Before(*req.ModifiedSince) {
continue
}
}
if req.PublishedSince != nil && cveDetail.PublishedDate != nil {
if cveDetail.PublishedDate.Before(*req.PublishedSince) {
continue
}
}

filteredIDs = append(filteredIDs, cve)
}
return filteredIDs
}

func (c *Cache) loadCveDetails(cves []string) CveDetails {
cveDetails := make(CveDetails, len(cves))
for _, cve := range cves {
cveDetail := c.CveDetail[cve]
cveDetail.Name = cve
cveDetail.Errata = c.errataIDs2Names(cveDetail.ErrataIDs)
binPackages, sourcePackages := c.packageIDs2Nevras(cveDetail.PkgIDs)
cveDetail.Packages = binPackages
cveDetail.SourcePackages = sourcePackages
cveDetails[cve] = cveDetail
}
return cveDetails
}

func (req *CvesRequest) cves(c *Cache) (*Cves, error) { // TODO: implement opts
cves, err := req.getSortedCves(c)
if err != nil {
return nil, err
}

cves = filterInputCves(c, cves, req)
cves, paginationDetails := utils.Paginate(cves, req.PageNumber, req.PageSize)

res := Cves{
Cves: c.loadCveDetails(cves),
LastChange: c.DBChange.LastChange,
PaginationDetails: paginationDetails,
}
return &res, nil
}
88 changes: 88 additions & 0 deletions vmaas/cves_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package vmaas

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestGetSortedCves(t *testing.T) {
req := mockCvesRequest()
reqWithoutReq := &CvesRequest{}
c := mockCache()

cves, err := req.getSortedCves(c)
assert.NoError(t, err)
assert.Equal(t, "CVE-2024-1111111", cves[0])
assert.Equal(t, "CVE-2024-1234", cves[1])
assert.Equal(t, "CVE-2024-21345", cves[2])

_, err = reqWithoutReq.getSortedCves(c)
assert.Error(t, err)
}

func TestFilterInputCves(t *testing.T) {
cves := []string{"CVE-2024-1234", "CVE-2024-21345", ""}
c := mockCache()

// usual case
filteredIDs := filterInputCves(c, cves, &CvesRequest{})
assert.Equal(t, 2, len(filteredIDs))

// RHOnly
req := &CvesRequest{RHOnly: true}
filteredIDs = filterInputCves(c, cves, req)
assert.Equal(t, 1, len(filteredIDs))
assert.Equal(t, "CVE-2024-21345", filteredIDs[0])

// With some errata associated only
req = &CvesRequest{AreErrataAssociated: true}
filteredIDs = filterInputCves(c, cves, req)
assert.Equal(t, 1, len(filteredIDs))
assert.Equal(t, "CVE-2024-1234", filteredIDs[0])

// With modified date before req.ModifiedSince
testTime, _ := time.Parse(time.RFC3339, "2024-10-03T15:01:01Z")
req = &CvesRequest{ModifiedSince: &testTime}
filteredIDs = filterInputCves(c, cves, req)
assert.Equal(t, 1, len(filteredIDs))
assert.Equal(t, "CVE-2024-1234", filteredIDs[0])

// With published date before req.PublishedSince
req = &CvesRequest{PublishedSince: &testTime}
filteredIDs = filterInputCves(c, cves, req)
assert.Equal(t, 1, len(filteredIDs))
assert.Equal(t, "CVE-2024-1234", filteredIDs[0])
}

func TestLoadCveDetails(t *testing.T) {
c := mockCache()
cve := "CVE-2024-1111111"
cvePropertiesMap := c.loadCveDetails([]string{cve})
assert.Equal(t, 1, len(cvePropertiesMap))
assert.Equal(t, cve, cvePropertiesMap[cve].Name)
}

func TestCves(t *testing.T) {
req := &CvesRequest{}
c := mockCache()

// empty cve list
_, err := req.cves(c)
assert.Error(t, err)
}

func mockCvesRequest() *CvesRequest {
modifiedSince, _ := time.Parse(time.RFC3339, "2024-10-02T16:08:00+02:00")
publishedSince, _ := time.Parse(time.RFC3339, "2024-10-02T16:08:00+02:00")
return &CvesRequest{
Cves: []string{"CVE-2024-21345", "CVE-2024-1234", "CVE-2024-1111111"},
ModifiedSince: &modifiedSince,
PublishedSince: &publishedSince,
RHOnly: false,
AreErrataAssociated: false,
PageNumber: 1,
PageSize: 5000,
}
}
22 changes: 15 additions & 7 deletions vmaas/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,28 +505,36 @@ func loadErrata(c *Cache) {
c.ErratumID2Name = erratumID2Name
}

//nolint:lll
func loadCves(c *Cache) {
defer utils.TimeTrack(time.Now(), "CveDetail")

cveID2cwes := loadInt2Strings("cve_cwe", "cve_id,cwe", "cveID2cwes")
cveID2pkg := loadInt2Ints("cve_pkg", "cve_id,pkg_id", "cveID2pkg")
cve2eid := loadString2Ints("errata_cve", "cve,errata_id", "cve2eid")

rows := getAllRows("cve_detail", "*")
rows := getAllRows("cve_detail", "id, name, COALESCE(redhat_url, ''), COALESCE(secondary_url, ''), COALESCE(cvss3_score, ''), COALESCE(cvss3_metrics, ''), impact, COALESCE(published_date, ''), COALESCE(modified_date, ''), COALESCE(iava, ''), description, COALESCE(cvss2_score, ''), COALESCE(cvss2_metrics, ''), source")
cnt := getCount("cve_detail", "*")
cveDetails := make(map[string]CveDetail, cnt)
cveNames := make(map[int]string, cnt)
var cveID int
var cveName string
var publishedDateStr, modifiedDateStr string
for rows.Next() {
var det CveDetail
err := rows.Scan(&cveID, &cveName, &det.RedHatURL, &det.SecondaryURL, &det.Cvss3Score, &det.Cvss3Metrics,
&det.Impact, &det.PublishedDate, &det.ModifiedDate, &det.Iava, &det.Description, &det.Cvss2Score,
err := rows.Scan(&cveID, &det.Name, &det.RedHatURL, &det.SecondaryURL, &det.Cvss3Score, &det.Cvss3Metrics,
&det.Impact, &publishedDateStr, &modifiedDateStr, &det.Iava, &det.Description, &det.Cvss2Score,
&det.Cvss2Metrics, &det.Source)
if err != nil {
panic(err)
}

if publishedDate, err := time.Parse(time.RFC3339, publishedDateStr); err == nil {
det.PublishedDate = &publishedDate
}
if modifiedDate, err := time.Parse(time.RFC3339, modifiedDateStr); err == nil {
det.ModifiedDate = &modifiedDate
}

cwes, ok := cveID2cwes[cveID]
sort.Strings(cwes)
if ok {
Expand All @@ -538,12 +546,12 @@ func loadCves(c *Cache) {
det.PkgIDs = pkgs
}

eids, ok := cve2eid[cveName]
eids, ok := cve2eid[det.Name]
if ok {
det.ErrataIDs = eids
}
cveDetails[cveName] = det
cveNames[cveID] = cveName
cveDetails[det.Name] = det
cveNames[cveID] = det.Name
}
c.CveDetail = cveDetails
c.CveNames = cveNames
Expand Down
Loading
Loading