-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement expanding cves by regex
RHINENG-13545
- Loading branch information
Showing
3 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |