diff --git a/core/cautils/datastructuresmethods.go b/core/cautils/datastructuresmethods.go index c9b4b56c..98c5f159 100644 --- a/core/cautils/datastructuresmethods.go +++ b/core/cautils/datastructuresmethods.go @@ -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 } } diff --git a/core/cautils/versioncheck_test.go b/core/cautils/versioncheck_test.go index fa8b5265..8e0c3835 100644 --- a/core/cautils/versioncheck_test.go +++ b/core/cautils/versioncheck_test.go @@ -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))