Skip to content

Commit

Permalink
feat: implement expanding cves by regex
Browse files Browse the repository at this point in the history
RHINENG-13545
  • Loading branch information
Dugowitch authored and psegedy committed Oct 31, 2024
1 parent ad285fb commit ecb2e42
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion vmaas/cves.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (req *CvesRequest) getSortedCves(c *Cache) ([]string, error) {
if len(cves) == 0 {
return nil, errors.New("cve_list must contain at least one item")
}
// TODO: implement expanding by regex
cves = utils.TryExpandRegexPattern(cves, c.CveDetail)
slices.Sort(cves)
return cves, nil
}
Expand Down
36 changes: 36 additions & 0 deletions vmaas/utils/expand_regex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package utils

import (
"regexp"
"strings"
)

// 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 {
if len(inLabels) != 1 {
return inLabels
}

pattern := inLabels[0]
if !strings.HasPrefix(pattern, "^") {
pattern = "^" + pattern
}
if !strings.HasSuffix(pattern, "$") {
pattern += "$"
}

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

outLabels := make([]string, 0, len(dataByLabels))
for label := range dataByLabels {
matched := re.Match([]byte(label))
if matched {
outLabels = append(outLabels, label)
}
}
return outLabels
}
33 changes: 33 additions & 0 deletions vmaas/utils/expand_regex_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package utils

import (
"testing"

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

func TestTryExpandRegexPattern(t *testing.T) {
regexLabel := []string{`CVE-2024-1\d+`}
inLabels := []string{"CVE-2024-1234", "CVE-2024-21345"}
labelDetails := map[string]int{
"CVE-2024-1234": 0,
"CVE-2024-12345": 0,
"CVE-2024-21345": 0,
}

// empty slice
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)
assert.Equal(t, inLabels[0], outLabels[0])

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

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

0 comments on commit ecb2e42

Please sign in to comment.