-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/116 add if condition executor #138
Changes from all commits
9cf8443
cc0048d
89cb1fe
99909e4
f8c15b3
8f1d877
a10feea
a2c0529
56afa63
9e2e31d
6fbe289
9f91cb4
79d360d
1428042
c16260d
7b24b84
bd9df0d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package condition | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"reflect" | ||
"soarca/internal/reporter" | ||
"soarca/logger" | ||
"soarca/models/cacao" | ||
"soarca/models/execution" | ||
"soarca/utils/stix/expression/comparison" | ||
) | ||
|
||
var component = reflect.TypeOf(Executor{}).PkgPath() | ||
var log *logger.Log | ||
|
||
func init() { | ||
log = logger.Logger(component, logger.Info, "", logger.Json) | ||
} | ||
|
||
func New(comparison comparison.IComparison, | ||
reporter reporter.IStepReporter) *Executor { | ||
return &Executor{comparison: comparison, | ||
reporter: reporter} | ||
} | ||
|
||
type IExecuter interface { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just for clarity - perhaps it would be clearer to rename this interface to something like IIfConditionExecuter? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to keep it short because it's already in the package condition |
||
Execute(metadata execution.Metadata, | ||
step cacao.Step, variables cacao.Variables) (string, bool, error) | ||
} | ||
|
||
type Executor struct { | ||
comparison comparison.IComparison | ||
reporter reporter.IStepReporter | ||
} | ||
|
||
func (executor *Executor) Execute(meta execution.Metadata, step cacao.Step, variables cacao.Variables) (string, bool, error) { | ||
|
||
MaartendeKruijf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if step.Type != cacao.StepTypeIfCondition { | ||
err := errors.New("the provided step type is not compatible with this executor") | ||
log.Error(err) | ||
return step.OnFailure, false, err | ||
} | ||
|
||
executor.reporter.ReportStepStart(meta.ExecutionId, step, variables) | ||
|
||
result, err := executor.comparison.Evaluate(step.Condition, variables) | ||
|
||
// We are reporting early to not have double reporting | ||
executor.reporter.ReportStepEnd(meta.ExecutionId, | ||
step, | ||
variables, | ||
err) | ||
|
||
if err != nil { | ||
log.Error(err) | ||
return "", false, err | ||
} | ||
|
||
log.Debug("the result was: ", fmt.Sprint(result)) | ||
|
||
if result { | ||
if step.OnTrue != "" { | ||
log.Trace("will return on true step ", step.OnTrue) | ||
return step.OnTrue, true, nil | ||
} | ||
} else { | ||
if step.OnFalse != "" { | ||
MaartendeKruijf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
log.Trace("will return on false step ", step.OnFalse) | ||
return step.OnFalse, true, nil | ||
} | ||
} | ||
log.Trace("will return on completion step ", step.OnCompletion) | ||
|
||
return step.OnCompletion, false, nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is probably never invoked, right? I would believe that the "on completion" step handling should be performed at decomposer level There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is invoked when on_true or on_false are not set but the condition is valid en evaluates to true or false respectively
MaartendeKruijf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If on_completion is handled at decomposer level, then I think its invocation should be performed here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are not branching if the on_true or on_false are not taken but for consistency sake we return the onCompletionId