Skip to content

Commit

Permalink
Merge pull request #110 from kubescape/fix-SubStatusConfiguration
Browse files Browse the repository at this point in the history
fix configuration status
  • Loading branch information
YiscahLevySilas1 authored Jun 11, 2023
2 parents e68640b + fdda434 commit 9f1742e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
12 changes: 6 additions & 6 deletions reporthandling/results/v1/resourcesresults/controls.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (control *ResourceAssociatedControl) SetStatus(c reporthandling.Control) {
}

// If the control type is configuration and the configuration is not set, the status is skipped and the sub status is configuration
if actionRequired == apis.SubStatusConfiguration && controlMissingConfiguration(control) {
if actionRequired == apis.SubStatusConfiguration && controlMissingAllConfigurations(control) {
status = apis.StatusSkipped
subStatus = apis.SubStatusConfiguration
statusInfo = string(apis.SubStatusConfigurationInfo)
Expand All @@ -120,17 +120,17 @@ func (control *ResourceAssociatedControl) ListRules() []ResourceAssociatedRule {
return control.ResourceAssociatedRules
}

// controlMissingConfiguration return true if the control is missing configuration
func controlMissingConfiguration(control *ResourceAssociatedControl) bool {
// controlMissingAllConfiguration returns true if all control configurations are missing
func controlMissingAllConfigurations(control *ResourceAssociatedControl) bool {
for _, rule := range control.ResourceAssociatedRules {
if len(rule.ControlConfigurations) == 0 {
return true
}
for _, configuration := range rule.ControlConfigurations {
if len(configuration) == 0 {
return true
if len(configuration) != 0 {
return false
}
}
}
return false
return true
}
70 changes: 70 additions & 0 deletions reporthandling/results/v1/resourcesresults/controls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,73 @@ func Test_GetStatusAndSubStatus_NewResourceAssociatedControls(t *testing.T) {
assert.Equal(t, controlIdToExpectedSubStatus[control.ControlID], string(control.GetSubStatus()))
}
}

func TestControlMissingAllConfigurations(t *testing.T) {
tests := []struct {
name string
control *ResourceAssociatedControl
want bool
}{
{
name: "TestControlNoConfigurations",
control: &ResourceAssociatedControl{
ResourceAssociatedRules: []ResourceAssociatedRule{
{
ControlConfigurations: map[string][]string{},
},
},
},
want: true,
}, {
name: "TestControlOneEmptyConfiguration",
control: &ResourceAssociatedControl{
ResourceAssociatedRules: []ResourceAssociatedRule{
{
ControlConfigurations: map[string][]string{
"EmptyConfiguration": {},
},
},
},
},
want: true,
},
{
name: "TestControlOneNonEmptyConfiguration",
control: &ResourceAssociatedControl{
ResourceAssociatedRules: []ResourceAssociatedRule{
{
ControlConfigurations: map[string][]string{
"NonEmptyConfiguration": {
"key", "value",
},
},
},
},
},
want: false,
},
{
name: "TestControlMultipleConfigurations",
control: &ResourceAssociatedControl{
ResourceAssociatedRules: []ResourceAssociatedRule{
{
ControlConfigurations: map[string][]string{
"EmptyConfiguration": {},
"NonEmptyConfiguration": {
"key", "value",
},
},
},
},
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := controlMissingAllConfigurations(tt.control); got != tt.want {
t.Errorf("Control.missingAllConfigurations() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 9f1742e

Please sign in to comment.