Skip to content

Commit 8334eca

Browse files
authored
Support checks vuls (kubescape#119)
* support GetSubstepsWithVulnerabilities * minor * minor fix
1 parent c09ae47 commit 8334eca

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

reporthandling/attacktrack/v1alpha1/attacktrackmethods.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,25 @@ func (at *AttackTrack) Iterator() IAttackTrackIterator {
5757
}
5858
}
5959

60+
// GetSubstepsWithVulnerabilities returns a list of substeps names that check for vulnerabilities
61+
func (at *AttackTrack) GetSubstepsWithVulnerabilities() []string {
62+
var substepNames []string
63+
64+
var traverse func(step AttackTrackStep)
65+
traverse = func(step AttackTrackStep) {
66+
if step.DoesCheckVulnerabilities() {
67+
substepNames = append(substepNames, step.Name)
68+
}
69+
for _, substep := range step.SubSteps {
70+
traverse(substep)
71+
}
72+
}
73+
74+
traverse(at.Spec.Data)
75+
76+
return substepNames
77+
}
78+
6079
func (iter *AttackTrackIterator) HasNext() bool {
6180
return !iter.stack.IsEmpty()
6281
}

reporthandling/attacktrack/v1alpha1/attacktrackmethods_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,3 +685,50 @@ func TestFilterNodesWithControls(t *testing.T) {
685685
})
686686
}
687687
}
688+
689+
func TestGetSubstepsWithVulnerabilities(t *testing.T) {
690+
// Create an AttackTrack object with substeps having different values for ChecksVulnerabilities
691+
attackTrack := AttackTrack{
692+
ApiVersion: "v1",
693+
Kind: "AttackTrack",
694+
Metadata: map[string]interface{}{},
695+
Spec: AttackTrackSpecification{
696+
Version: "1.0",
697+
Description: "Example attack track",
698+
Data: AttackTrackStep{
699+
Name: "Step 1",
700+
Description: "First step",
701+
ChecksVulnerabilities: true,
702+
SubSteps: []AttackTrackStep{
703+
{
704+
Name: "Substep 1.1",
705+
Description: "Substep 1.1 description",
706+
ChecksVulnerabilities: true,
707+
},
708+
{
709+
Name: "Substep 1.2",
710+
Description: "Substep 1.2 description",
711+
ChecksVulnerabilities: false,
712+
},
713+
},
714+
},
715+
},
716+
}
717+
718+
// Call the method being tested
719+
substepNames := attackTrack.GetSubstepsWithVulnerabilities()
720+
721+
// Define the expected substep names with ChecksVulnerabilities set to true
722+
expectedSubstepNames := []string{"Step 1", "Substep 1.1"}
723+
724+
// Check if the returned substep names match the expected substep names
725+
if len(substepNames) != len(expectedSubstepNames) {
726+
t.Errorf("Unexpected number of substep names. Expected: %d, Got: %d", len(expectedSubstepNames), len(substepNames))
727+
}
728+
729+
for i, name := range substepNames {
730+
if name != expectedSubstepNames[i] {
731+
t.Errorf("Mismatched substep name. Expected: %s, Got: %s", expectedSubstepNames[i], name)
732+
}
733+
}
734+
}

reporthandling/attacktrack/v1alpha1/attacktrackmocks.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,26 @@ func (at AttackTrackMock) Iterator() IAttackTrackIterator {
136136
}
137137
}
138138

139+
// GetSubstepsWithVulnerabilities returns a list of substeps names that check for vulnerabilities
140+
func (at AttackTrackMock) GetSubstepsWithVulnerabilities() []string {
141+
var substepNames []string
142+
143+
var traverse func(step AttackTrackStep)
144+
traverse = func(step AttackTrackStep) {
145+
if step.DoesCheckVulnerabilities() {
146+
substepNames = append(substepNames, step.Name)
147+
}
148+
for _, substep := range step.SubSteps {
149+
traverse(substep)
150+
}
151+
}
152+
153+
t := at.Spec.Data.(*AttackTrackStep)
154+
traverse(*t)
155+
156+
return substepNames
157+
}
158+
139159
type MockAttackTrackSpecification struct {
140160
Version string `json:"version,omitempty"`
141161
Description string `json:"description,omitempty"`

reporthandling/attacktrack/v1alpha1/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type IAttackTrack interface {
99
GetData() IAttackTrackStep
1010
Iterator() IAttackTrackIterator
1111
IsValid() bool
12+
GetSubstepsWithVulnerabilities() []string
1213
}
1314

1415
// A step in an attack track

0 commit comments

Comments
 (0)