Merge pull request #1494 from cbrom/core_cautils_datastructuremethods_tests

Code Improvement on isRuleKubescapeVersionCompatible function
This commit is contained in:
Matthias Bertschy
2023-11-23 09:01:46 +01:00
committed by GitHub
2 changed files with 14 additions and 14 deletions
+13 -13
View File
@@ -59,25 +59,25 @@ func (policies *Policies) Set(frameworks []reporthandling.Framework, version str
// returns true only if rule doesn't have the "until" attribute
func isRuleKubescapeVersionCompatible(attributes map[string]interface{}, version string) bool {
if from, ok := attributes["useFromKubescapeVersion"]; ok && from != nil {
if version != "" {
if sfrom, ok := from.(string); ok {
if semver.Compare(version, sfrom) == -1 {
return false
}
} else {
switch sfrom := from.(type) {
case string:
if version != "" && semver.Compare(version, sfrom) == -1 {
return false
}
}
}
if until, ok := attributes["useUntilKubescapeVersion"]; ok && until != nil {
if version == "" {
default:
// Handle case where useFromKubescapeVersion is not a string
return false
}
if suntil, ok := until.(string); ok {
if semver.Compare(version, suntil) >= 0 {
}
if until, ok := attributes["useUntilKubescapeVersion"]; ok && until != nil {
switch suntil := until.(type) {
case string:
if version == "" || semver.Compare(version, suntil) >= 0 {
return false
}
} else {
default:
// Handle case where useUntilKubescapeVersion is not a string
return false
}
}
+1 -1
View File
@@ -31,7 +31,7 @@ func TestIsRuleKubescapeVersionCompatible(t *testing.T) {
// should not crash when the value of useUntilKubescapeVersion is not a string
buildNumberMock := "v1.0.135"
assert.False(t, isRuleKubescapeVersionCompatible(rule_invalid_from.Attributes, buildNumberMock))
assert.False(t, isRuleKubescapeVersionCompatible(rule_invalid_from.Attributes, buildNumberMock))
assert.False(t, isRuleKubescapeVersionCompatible(rule_invalid_until.Attributes, buildNumberMock))
// should use only rules that don't have "until"
buildNumberMock = ""
assert.False(t, isRuleKubescapeVersionCompatible(rule_v1_0_131.Attributes, buildNumberMock))