Skip to content

Commit

Permalink
fix: implement various cves endpoint fixes
Browse files Browse the repository at this point in the history
RHINENG-13545
  • Loading branch information
Dugowitch authored and psegedy committed Dec 2, 2024
1 parent ee2d2cd commit 197a925
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 23 deletions.
20 changes: 13 additions & 7 deletions vmaas/cves.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ type Cves struct {
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")
return nil, errors.Wrap(ErrProcessingInput, "cve_list must contain at least one item")
}
cves, err := utils.TryExpandRegexPattern(cves, c.CveDetail)
if err != nil {
return nil, errors.Wrap(ErrProcessingInput, "invalid regex pattern")
}
cves = utils.TryExpandRegexPattern(cves, c.CveDetail)
slices.Sort(cves)
return cves, nil
}
Expand All @@ -43,13 +46,13 @@ func filterInputCves(c *Cache, cves []string, req *CvesRequest) []string {
continue
}

if req.ModifiedSince != nil && cveDetail.ModifiedDate != nil {
if cveDetail.ModifiedDate.Before(*req.ModifiedSince) {
if req.ModifiedSince != nil {
if cveDetail.ModifiedDate == nil || cveDetail.ModifiedDate.Before(*req.ModifiedSince) {
continue
}
}
if req.PublishedSince != nil && cveDetail.PublishedDate != nil {
if cveDetail.PublishedDate.Before(*req.PublishedSince) {
if req.PublishedSince != nil {
if cveDetail.PublishedDate == nil || cveDetail.PublishedDate.Before(*req.PublishedSince) {
continue
}
}
Expand All @@ -68,6 +71,9 @@ func (c *Cache) loadCveDetails(cves []string) CveDetails {
binPackages, sourcePackages := c.packageIDs2Nevras(cveDetail.PkgIDs)
cveDetail.Packages = binPackages
cveDetail.SourcePackages = sourcePackages
if cveDetail.CWEs == nil {
cveDetail.CWEs = []string{}
}
cveDetails[cve] = cveDetail
}
return cveDetails
Expand All @@ -76,7 +82,7 @@ func (c *Cache) loadCveDetails(cves []string) CveDetails {
func (req *CvesRequest) cves(c *Cache) (*Cves, error) { // TODO: implement opts
cves, err := req.getSortedCves(c)
if err != nil {
return nil, err
return &Cves{}, err
}

cves = filterInputCves(c, cves, req)
Expand Down
6 changes: 2 additions & 4 deletions vmaas/cves_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,12 @@ func TestFilterInputCves(t *testing.T) {
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])
assert.Equal(t, 0, len(filteredIDs))

// 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])
assert.Equal(t, 0, len(filteredIDs))
}

func TestLoadCveDetails(t *testing.T) {
Expand Down
5 changes: 4 additions & 1 deletion vmaas/errata.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ func (req *ErrataRequest) getSortedErrata(c *Cache) ([]string, error) {
if len(req.Errata) == 0 {
return nil, errors.New("errata_list must contain at least one item")
}
errata := utils.TryExpandRegexPattern(req.Errata, c.ErratumDetails)
errata, err := utils.TryExpandRegexPattern(req.Errata, c.ErratumDetails)
if err != nil {
return nil, errors.Wrap(ErrProcessingInput, "invalid regex pattern")
}
slices.Sort(errata)
return errata, nil
}
Expand Down
19 changes: 12 additions & 7 deletions vmaas/utils/expand_regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,28 @@ import (

// TryExpandRegexPattern treats the item in a single-label slice like a regex pattern
// and returns all matching labels from dataByLabels, otherwise it returns inLabels.
func TryExpandRegexPattern[T any](inLabels []string, dataByLabels map[string]T) []string {
func TryExpandRegexPattern[T any](inLabels []string, dataByLabels map[string]T) ([]string, error) {
if len(inLabels) != 1 {
return inLabels
return inLabels, nil
}

pattern := inLabels[0]

// Check pattern before adding ^ and $.
// For example, go implementation errors out on `*`, but doesn't on `^*$`.
_, err := regexp.Compile(pattern)
if err != nil {
return nil, err
}

if !strings.HasPrefix(pattern, "^") {
pattern = "^" + pattern
}
if !strings.HasSuffix(pattern, "$") {
pattern += "$"
}

re, err := regexp.Compile(pattern)
if err != nil {
return inLabels
}
re := regexp.MustCompile(pattern)

outLabels := make([]string, 0, len(dataByLabels))
for label := range dataByLabels {
Expand All @@ -32,5 +37,5 @@ func TryExpandRegexPattern[T any](inLabels []string, dataByLabels map[string]T)
outLabels = append(outLabels, label)
}
}
return outLabels
return outLabels, nil
}
12 changes: 8 additions & 4 deletions vmaas/utils/expand_regex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@ func TestTryExpandRegexPattern(t *testing.T) {
}

// empty slice
outLabels := TryExpandRegexPattern([]string{}, labelDetails)
outLabels, _ := TryExpandRegexPattern([]string{}, labelDetails)
assert.Equal(t, 0, len(outLabels))

// with a single lable that is not a regex pattern
outLabels = TryExpandRegexPattern(inLabels[0:1], labelDetails)
outLabels, _ = TryExpandRegexPattern(inLabels[0:1], labelDetails)
assert.Equal(t, inLabels[0], outLabels[0])

// more labels in inLabels
outLabels = TryExpandRegexPattern(inLabels, labelDetails)
outLabels, _ = TryExpandRegexPattern(inLabels, labelDetails)
assert.Equal(t, len(inLabels), len(outLabels))

// with regex
outLabels = TryExpandRegexPattern(regexLabel, labelDetails)
outLabels, _ = TryExpandRegexPattern(regexLabel, labelDetails)
assert.Equal(t, 2, len(outLabels))

// invalid regex
_, err := TryExpandRegexPattern([]string{"*"}, labelDetails)
assert.Error(t, err)
}

0 comments on commit 197a925

Please sign in to comment.