Skip to content

Commit 7b24b84

Browse files
Added reporting to the condition executor
1 parent c16260d commit 7b24b84

File tree

2 files changed

+71
-7
lines changed

2 files changed

+71
-7
lines changed

internal/executors/condition/condition.go

+21-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package condition
22

33
import (
44
"errors"
5+
"fmt"
56
"reflect"
7+
"soarca/internal/reporter"
68
"soarca/logger"
79
"soarca/models/cacao"
810
"soarca/models/execution"
@@ -16,8 +18,10 @@ func init() {
1618
log = logger.Logger(component, logger.Info, "", logger.Json)
1719
}
1820

19-
func New(comparison comparison.IComparison) *Executor {
20-
return &Executor{comparison: comparison}
21+
func New(comparison comparison.IComparison,
22+
reporter reporter.IStepReporter) *Executor {
23+
return &Executor{comparison: comparison,
24+
reporter: reporter}
2125
}
2226

2327
type IExecuter interface {
@@ -27,6 +31,7 @@ type IExecuter interface {
2731

2832
type Executor struct {
2933
comparison comparison.IComparison
34+
reporter reporter.IStepReporter
3035
}
3136

3237
func (executor *Executor) Execute(meta execution.Metadata, step cacao.Step, variables cacao.Variables) (string, bool, error) {
@@ -37,22 +42,35 @@ func (executor *Executor) Execute(meta execution.Metadata, step cacao.Step, vari
3742
return step.OnFailure, false, err
3843
}
3944

45+
executor.reporter.ReportStepStart(meta.ExecutionId, step, variables)
46+
4047
result, err := executor.comparison.Evaluate(step.Condition, variables)
48+
49+
// We are reporting early to not have double reporting
50+
executor.reporter.ReportStepEnd(meta.ExecutionId,
51+
step,
52+
variables,
53+
err)
54+
4155
if err != nil {
4256
log.Error(err)
4357
return "", false, err
4458
}
4559

60+
log.Debug("the result was: ", fmt.Sprint(result))
61+
4662
if result {
4763
if step.OnTrue != "" {
48-
log.Trace("")
64+
log.Trace("will return on true step ", step.OnTrue)
4965
return step.OnTrue, true, nil
5066
}
5167
} else {
5268
if step.OnFalse != "" {
69+
log.Trace("will return on false step ", step.OnFalse)
5370
return step.OnFalse, true, nil
5471
}
5572
}
73+
log.Trace("will return on completion step ", step.OnCompletion)
5674

5775
return step.OnCompletion, false, nil
5876
}

test/unittest/executor/condition/condition_executor_test.go

+50-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package condition_test
22

33
import (
4+
"errors"
45
"soarca/internal/executors/condition"
56
"soarca/models/cacao"
67
"soarca/models/execution"
8+
"soarca/test/unittest/mocks/mock_reporter"
79
mock_stix "soarca/test/unittest/mocks/mock_utils/stix"
810
"testing"
911

@@ -13,8 +15,9 @@ import (
1315

1416
func TestExecuteConditionTrue(t *testing.T) {
1517
mock_stix := new(mock_stix.MockStix)
18+
mock_reporter := new(mock_reporter.Mock_Reporter)
1619

17-
conditionExecutior := condition.New(mock_stix)
20+
conditionExecutior := condition.New(mock_stix, mock_reporter)
1821

1922
executionId := uuid.New()
2023

@@ -28,19 +31,25 @@ func TestExecuteConditionTrue(t *testing.T) {
2831
OnFalse: "4"}
2932
vars := cacao.NewVariables()
3033

34+
mock_reporter.On("ReportStepStart", executionId, step, vars)
3135
mock_stix.On("Evaluate", "a = a", vars).Return(true, nil)
36+
mock_reporter.On("ReportStepEnd", executionId, step, vars, nil)
3237

3338
nextStepId, goToBranch, err := conditionExecutior.Execute(meta, step, vars)
3439
assert.Equal(t, nil, err)
3540
assert.Equal(t, true, goToBranch)
3641
assert.Equal(t, "3", nextStepId)
3742

43+
mock_reporter.AssertExpectations(t)
44+
mock_stix.AssertExpectations(t)
45+
3846
}
3947

4048
func TestExecuteConditionFalse(t *testing.T) {
4149
mock_stix := new(mock_stix.MockStix)
50+
mock_reporter := new(mock_reporter.Mock_Reporter)
4251

43-
conditionExecutior := condition.New(mock_stix)
52+
conditionExecutior := condition.New(mock_stix, mock_reporter)
4453

4554
executionId := uuid.New()
4655

@@ -54,11 +63,48 @@ func TestExecuteConditionFalse(t *testing.T) {
5463
OnFalse: "4"}
5564
vars := cacao.NewVariables()
5665

57-
mock_stix.On("Evaluate", "a = a", vars).Return(true, nil)
66+
mock_reporter.On("ReportStepStart", executionId, step, vars)
67+
mock_stix.On("Evaluate", "a = a", vars).Return(false, nil)
68+
mock_reporter.On("ReportStepEnd", executionId, step, vars, nil)
5869

5970
nextStepId, goToBranch, err := conditionExecutior.Execute(meta, step, vars)
6071
assert.Equal(t, nil, err)
6172
assert.Equal(t, true, goToBranch)
62-
assert.Equal(t, "3", nextStepId)
73+
assert.Equal(t, "4", nextStepId)
74+
75+
mock_reporter.AssertExpectations(t)
76+
mock_stix.AssertExpectations(t)
77+
}
78+
79+
func TestExecuteConditionError(t *testing.T) {
80+
mock_stix := new(mock_stix.MockStix)
81+
mock_reporter := new(mock_reporter.Mock_Reporter)
82+
83+
conditionExecutior := condition.New(mock_stix, mock_reporter)
84+
85+
executionId := uuid.New()
86+
87+
meta := execution.Metadata{ExecutionId: executionId,
88+
PlaybookId: "1",
89+
StepId: "2"}
90+
91+
step := cacao.Step{Type: cacao.StepTypeIfCondition,
92+
Condition: "a = a",
93+
OnTrue: "3",
94+
OnFalse: "4"}
95+
vars := cacao.NewVariables()
96+
97+
evaluationError := errors.New("some ds error")
98+
99+
mock_reporter.On("ReportStepStart", executionId, step, vars)
100+
mock_stix.On("Evaluate", "a = a", vars).Return(false, evaluationError)
101+
mock_reporter.On("ReportStepEnd", executionId, step, vars, evaluationError)
102+
103+
nextStepId, goToBranch, err := conditionExecutior.Execute(meta, step, vars)
104+
assert.Equal(t, evaluationError, err)
105+
assert.Equal(t, false, goToBranch)
106+
assert.Equal(t, "", nextStepId)
63107

108+
mock_reporter.AssertExpectations(t)
109+
mock_stix.AssertExpectations(t)
64110
}

0 commit comments

Comments
 (0)