Skip to content

Commit d0cb0db

Browse files
authored
Merge pull request #151 from kubescape/FixByNP
add func IsFixedByNetworkPolicy
2 parents 579a594 + 3633bc3 commit d0cb0db

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

reporthandling/datastructuresmethods.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import (
1212
"golang.org/x/exp/slices"
1313
)
1414

15-
const ActionRequiredAttribute string = "actionRequired"
15+
const (
16+
ActionRequiredAttribute string = "actionRequired"
17+
ControlAttributeKeyIsFixedByNetworkPolicy string = "isFixedByNetworkPolicy"
18+
)
1619

1720
// ==============================================================================================
1821
// ========================== PostureReport =====================================================
@@ -443,6 +446,19 @@ func (control *Control) GetControlTypeTags() []string {
443446
return []string{}
444447
}
445448

449+
// returns true if control has attribute "isFixedByNetworkPolicy" and its value is true
450+
func (control *Control) IsFixedByNetworkPolicy() bool {
451+
if control.Attributes == nil {
452+
return false
453+
}
454+
if v, exist := control.Attributes[ControlAttributeKeyIsFixedByNetworkPolicy]; exist {
455+
if isFixedByNetworkPolicy, ok := v.(bool); ok {
456+
return isFixedByNetworkPolicy
457+
}
458+
}
459+
return false
460+
}
461+
446462
func (control *Control) SupportSmartRemediation() bool {
447463
typeTags := control.GetControlTypeTags()
448464
return slices.Contains(typeTags, v1alpha1.ControlTypeTagSmartRemediation)

reporthandling/datastructuresmethods_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,22 @@ func TestControl_GetControlTypeTags(t *testing.T) {
192192
assert.NoError(t, err, err)
193193
assert.Equal(t, []string{}, missingAttributeControl.GetControlTypeTags())
194194
}
195+
196+
func TestControl_IsFixedByNetworkPolicy(t *testing.T) {
197+
validControlJsonNoAttributes := `{"name":"TEST","description":"","remediation":"","rulesNames":["CVE-2022-0185"],"id":"C-0079","long_description":"","test":"","controlID":"C-0079","baseScore":4,"example":""}`
198+
var validControl Control
199+
err := json.Unmarshal([]byte(validControlJsonNoAttributes), &validControl)
200+
assert.NoError(t, err, err)
201+
assert.False(t, validControl.IsFixedByNetworkPolicy())
202+
203+
validControlJson := `{"name":"TEST","attributes":{"controlTypeTags":["security","compliance"],"isFixedByNetworkPolicy":true, "attackTracks":[{"attackTrack": "network","categories": ["Eavesdropping","Spoofing"]}]},"description":"","remediation":"","rulesNames":["CVE-2022-0185"],"id":"C-0079","long_description":"","test":"","controlID":"C-0079","baseScore":4,"example":""}`
204+
err = json.Unmarshal([]byte(validControlJson), &validControl)
205+
assert.NoError(t, err, err)
206+
assert.True(t, validControl.IsFixedByNetworkPolicy())
207+
208+
missingAttributeControlJson := `{"name":"TEST","attributes":{"controlTypeTags":["security","compliance"]},"description":"","remediation":"","rulesNames":["CVE-2022-0185"],"id":"C-0079","long_description":"","test":"","controlID":"C-0079","baseScore":4,"example":""}`
209+
var missingAttributeControl Control
210+
err = json.Unmarshal([]byte(missingAttributeControlJson), &missingAttributeControl)
211+
assert.NoError(t, err, err)
212+
assert.False(t, missingAttributeControl.IsFixedByNetworkPolicy())
213+
}

0 commit comments

Comments
 (0)