From d96ab483a4d829d648ec3b2653ec31c9300f750d Mon Sep 17 00:00:00 2001 From: cbrom Date: Wed, 22 Nov 2023 19:27:52 +0300 Subject: [PATCH 1/2] code improvement on type assertion for useFromKubescapeVersion and useUntilKubescapeVersion Signed-off-by: cbrom --- core/cautils/datastructuresmethods.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) 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 } } From 12056f4cad67c79e8eb1a927c09a4b9f117bb703 Mon Sep 17 00:00:00 2001 From: cbrom Date: Wed, 22 Nov 2023 19:28:30 +0300 Subject: [PATCH 2/2] fixed a test where it would run 1 test case twice Signed-off-by: cbrom --- core/cautils/versioncheck_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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))