Skip to content
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/92 add playbook action step typ to executor #108

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"soarca/internal/capability/ssh"
"soarca/internal/decomposer"
"soarca/internal/executors/action"
"soarca/internal/executors/playbook_action"
"soarca/internal/fin/protocol"
"soarca/internal/guid"
"soarca/logger"
Expand Down Expand Up @@ -67,8 +68,9 @@ func (controller *Controller) NewDecomposer() decomposer.IDecomposer {
}

actionExecutor := action.New(capabilities)
playbookActionExecutor := playbook_action.New(controller, controller)
guid := new(guid.Guid)
decompose := decomposer.New(actionExecutor, guid)
decompose := decomposer.New(actionExecutor, playbookActionExecutor, guid)
return decompose
}

Expand Down
39 changes: 21 additions & 18 deletions internal/decomposer/decomposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"reflect"

"soarca/internal/executors"
"soarca/internal/executors/action"
"soarca/internal/guid"
"soarca/logger"
Expand Down Expand Up @@ -35,22 +36,21 @@ func init() {
log = logger.Logger(component, logger.Info, "", logger.Json)
}

func New(actionExecutor action.IExecuter, guid guid.IGuid) *Decomposer {
instance := Decomposer{}
if instance.actionExecutor == nil {
instance.actionExecutor = actionExecutor
}
if instance.guid == nil {
instance.guid = guid
}
return &instance
func New(actionExecutor action.IExecuter,
playbookActionExecutor executors.IPlaybookExecuter,
guid guid.IGuid) *Decomposer {

return &Decomposer{actionExecutor: actionExecutor,
playbookActionExecutor: playbookActionExecutor,
guid: guid}
}

type Decomposer struct {
playbook cacao.Playbook
details ExecutionDetails
actionExecutor action.IExecuter
guid guid.IGuid
playbook cacao.Playbook
details ExecutionDetails
actionExecutor action.IExecuter
playbookActionExecutor executors.IPlaybookExecuter
guid guid.IGuid
}

// Execute a Playbook
Expand Down Expand Up @@ -131,6 +131,12 @@ func (decomposer *Decomposer) ExecuteStep(step cacao.Step, scopeVariables cacao.
variables.Merge(scopeVariables)
variables.Merge(step.StepVariables)

metadata := execution.Metadata{
ExecutionId: decomposer.details.ExecutionId,
PlaybookId: decomposer.details.PlaybookId,
StepId: step.ID,
}

switch step.Type {
case cacao.StepTypeAction:
actionMetadata := action.PlaybookStepMetadata{
Expand All @@ -140,12 +146,9 @@ func (decomposer *Decomposer) ExecuteStep(step cacao.Step, scopeVariables cacao.
Agent: decomposer.playbook.AgentDefinitions[step.Agent],
Variables: variables,
}
metadata := execution.Metadata{
ExecutionId: decomposer.details.ExecutionId,
PlaybookId: decomposer.details.PlaybookId,
StepId: step.ID,
}
return decomposer.actionExecutor.Execute(metadata, actionMetadata)
case cacao.StepTypePlaybookAction:
return decomposer.playbookActionExecutor.Execute(metadata, step, variables)
default:
// NOTE: This currently silently handles unknown step types. Should we return an error instead?
return cacao.NewVariables(), nil
Expand Down
12 changes: 12 additions & 0 deletions internal/executors/executors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package executors

import (
"soarca/models/cacao"
"soarca/models/execution"
)

type IPlaybookExecuter interface {
Execute(execution.Metadata,
cacao.Step,
cacao.Variables) (cacao.Variables, error)
}
61 changes: 61 additions & 0 deletions internal/executors/playbook_action/playbook_action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package playbook_action

import (
"errors"
"fmt"
"reflect"
"soarca/internal/controller/database"
"soarca/internal/controller/decomposer_controller"
"soarca/logger"
"soarca/models/cacao"
"soarca/models/execution"
)

type PlaybookAction struct {
decomposerController decomposer_controller.IController
databaseController database.IController
}

var component = reflect.TypeOf(PlaybookAction{}).PkgPath()
var log *logger.Log

func init() {
log = logger.Logger(component, logger.Info, "", logger.Json)
}

func New(controller decomposer_controller.IController,
database database.IController) *PlaybookAction {
return &PlaybookAction{decomposerController: controller, databaseController: database}
}

func (playbookAction *PlaybookAction) Execute(metadata execution.Metadata,
step cacao.Step,
variables cacao.Variables) (cacao.Variables, error) {
log.Trace(metadata.ExecutionId)

if step.Type != cacao.StepTypePlaybookAction {
err := errors.New(fmt.Sprint("step type is not of type ", cacao.StepTypePlaybookAction))
log.Error(err)
return cacao.NewVariables(), err
}

playbookRepo := playbookAction.databaseController.GetDatabaseInstance()
decomposer := playbookAction.decomposerController.NewDecomposer()

playbook, err := playbookRepo.Read(step.PlaybookID)
if err != nil {
log.Error("failed loading the playbook from the repository in playbook action")
return cacao.NewVariables(), err
}

playbook.PlaybookVariables.Merge(variables)

details, err := decomposer.Execute(playbook)
if err != nil {
err = errors.New(fmt.Sprint("execution of playbook failed with error: ", err))
log.Error(err)
return cacao.NewVariables(), err
}
return details.Variables, nil

}
2 changes: 1 addition & 1 deletion models/cacao/cacao.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const (
StepTypeEnd = "end"
StepTypeStart = "start"
StepTypeAction = "action"
StepTypePlaybook = "playbook-action"
StepTypePlaybookAction = "playbook-action"
StepTypeParallel = "parallel"
StepTypeIfCondition = "if-condition"
StepTypeWhileCondition = "while-condition"
Expand Down
80 changes: 76 additions & 4 deletions test/unittest/decomposer/decomposer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"soarca/models/cacao"
"soarca/models/execution"
"soarca/test/unittest/mocks/mock_executor"
mock_playbook_action_executor "soarca/test/unittest/mocks/mock_executor/playbook_action"
"soarca/test/unittest/mocks/mock_guid"

"github.com/go-playground/assert/v2"
Expand All @@ -18,6 +19,7 @@ import (

func TestExecutePlaybook(t *testing.T) {
mock_action_executor := new(mock_executor.Mock_Action_Executor)
mock_playbook_action_executor := new(mock_playbook_action_executor.Mock_PlaybookActionExecutor)
uuid_mock := new(mock_guid.Mock_Guid)

expectedCommand := cacao.Command{
Expand All @@ -31,7 +33,9 @@ func TestExecutePlaybook(t *testing.T) {
Value: "testing",
}

decomposer := decomposer.New(mock_action_executor, uuid_mock)
decomposer := decomposer.New(mock_action_executor,
mock_playbook_action_executor,
uuid_mock)

step1 := cacao.Step{
Type: "action",
Expand Down Expand Up @@ -107,6 +111,7 @@ func TestExecutePlaybook(t *testing.T) {

func TestExecutePlaybookMultiStep(t *testing.T) {
mock_action_executor := new(mock_executor.Mock_Action_Executor)
mock_playbook_action_executor := new(mock_playbook_action_executor.Mock_PlaybookActionExecutor)
uuid_mock := new(mock_guid.Mock_Guid)

expectedCommand := cacao.Command{
Expand All @@ -131,7 +136,9 @@ func TestExecutePlaybookMultiStep(t *testing.T) {
Value: "testing2",
}

decomposer := decomposer.New(mock_action_executor, uuid_mock)
decomposer := decomposer.New(mock_action_executor,
mock_playbook_action_executor,
uuid_mock)

step1 := cacao.Step{
Type: "action",
Expand Down Expand Up @@ -245,6 +252,7 @@ Test with an Empty OnCompletion will result in not executing the step.
*/
func TestExecuteEmptyMultiStep(t *testing.T) {
mock_action_executor2 := new(mock_executor.Mock_Action_Executor)
mock_playbook_action_executor2 := new(mock_playbook_action_executor.Mock_PlaybookActionExecutor)
uuid_mock2 := new(mock_guid.Mock_Guid)

expectedCommand := cacao.Command{
Expand All @@ -268,7 +276,9 @@ func TestExecuteEmptyMultiStep(t *testing.T) {
Name: "soarca-ssh",
}

decomposer2 := decomposer.New(mock_action_executor2, uuid_mock2)
decomposer2 := decomposer.New(mock_action_executor2,
mock_playbook_action_executor2,
uuid_mock2)

step1 := cacao.Step{
Type: "ssh",
Expand Down Expand Up @@ -307,6 +317,7 @@ Test with an not occuring on completion id will result in not executing the step
*/
func TestExecuteIllegalMultiStep(t *testing.T) {
mock_action_executor2 := new(mock_executor.Mock_Action_Executor)
mock_playbook_action_executor2 := new(mock_playbook_action_executor.Mock_PlaybookActionExecutor)
uuid_mock2 := new(mock_guid.Mock_Guid)

expectedCommand := cacao.Command{
Expand All @@ -320,7 +331,9 @@ func TestExecuteIllegalMultiStep(t *testing.T) {
Value: "testing",
}

decomposer2 := decomposer.New(mock_action_executor2, uuid_mock2)
decomposer2 := decomposer.New(mock_action_executor2,
mock_playbook_action_executor2,
uuid_mock2)

step1 := cacao.Step{
Type: "action",
Expand Down Expand Up @@ -351,3 +364,62 @@ func TestExecuteIllegalMultiStep(t *testing.T) {
assert.Equal(t, returnedId.ExecutionId, id)
mock_action_executor2.AssertExpectations(t)
}

func TestExecutePlaybookAction(t *testing.T) {
mock_action_executor := new(mock_executor.Mock_Action_Executor)
mock_playbook_action_executor := new(mock_playbook_action_executor.Mock_PlaybookActionExecutor)
uuid_mock := new(mock_guid.Mock_Guid)

expectedVariables := cacao.Variable{
Type: "string",
Name: "var1",
Value: "testing",
}

decomposer := decomposer.New(mock_action_executor,
mock_playbook_action_executor,
uuid_mock)

step1 := cacao.Step{
Type: "playbook-action",
ID: "playbook-action--test",
Name: "ssh-tests",
StepVariables: cacao.NewVariables(expectedVariables),
PlaybookID: "playbook--1",
OnCompletion: "end--test",
}

end := cacao.Step{
Type: "end",
ID: "end--test",
Name: "end step",
}

playbook := cacao.Playbook{
ID: "test",
Type: "test",
Name: "playbook-test",
WorkflowStart: step1.ID,
Workflow: map[string]cacao.Step{step1.ID: step1, end.ID: end},
}

executionId, _ := uuid.Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
metaStep1 := execution.Metadata{ExecutionId: executionId, PlaybookId: "test", StepId: step1.ID}

uuid_mock.On("New").Return(executionId)

mock_playbook_action_executor.On("Execute",
metaStep1,
step1,
cacao.NewVariables(expectedVariables)).Return(cacao.NewVariables(cacao.Variable{Name: "return", Value: "value"}), nil)

details, err := decomposer.Execute(playbook)
uuid_mock.AssertExpectations(t)
fmt.Println(err)
assert.Equal(t, err, nil)
assert.Equal(t, details.ExecutionId, executionId)
mock_action_executor.AssertExpectations(t)
value, found := details.Variables.Find("return")
assert.Equal(t, found, true)
assert.Equal(t, value.Value, "value")
}
Loading