diff --git a/models/api/reporter.go b/models/api/reporter.go index 7d4e588..e8f19ac 100644 --- a/models/api/reporter.go +++ b/models/api/reporter.go @@ -2,6 +2,7 @@ package api import ( "errors" + "fmt" "soarca/models/cacao" cache_model "soarca/models/cache" ) @@ -11,6 +12,9 @@ type Status uint8 // Reporter model adapted from https://github.com/cyentific-rni/workflow-status/blob/main/README.md const ( + ReportLevelPlaybook = "playbook" + ReportLevelStep = "step" + SuccessfullyExecuted = "successfully_executed" Failed = "failed" Ongoing = "ongoing" @@ -19,6 +23,15 @@ const ( TimeoutError = "timeout_error" ExceptionConditionError = "exception_condition_error" AwaitUserInput = "await_user_input" + + SuccessfullyExecutedText = "%s execution completed successfully" + FailedText = "something went wrong in the execution of this %s" + OngoingText = "this %s is currently being executed" + ServerSideErrorText = "there was a server-side problem with the execution of this %s" + ClientSideErrorText = "something in the data provided for this %s raised an issue" + TimeoutErrorText = "the execution of this %s timed out" + ExceptionConditionErrorText = "the execution of this %s raised a playbook exception" + AwaitUserInputText = "waiting for users to provide input for the %s execution" ) type PlaybookExecutionReport struct { @@ -72,3 +85,29 @@ func CacheStatusEnum2String(status cache_model.Status) (string, error) { return "", errors.New("unable to read execution information status") } } + +func GetCacheStatusText(status string, level string) (string, error) { + if level != ReportLevelPlaybook && level != ReportLevelStep { + return "", errors.New("invalid reporting level provided. use either 'playbook' or 'step'") + } + switch status { + case SuccessfullyExecuted: + return fmt.Sprintf(SuccessfullyExecutedText, level), nil + case Failed: + return fmt.Sprintf(FailedText, level), nil + case Ongoing: + return fmt.Sprintf(OngoingText, level), nil + case ServerSideError: + return fmt.Sprintf(ServerSideErrorText, level), nil + case ClientSideError: + return fmt.Sprintf(ClientSideErrorText, level), nil + case TimeoutError: + return fmt.Sprintf(TimeoutErrorText, level), nil + case ExceptionConditionError: + return fmt.Sprintf(ExceptionConditionErrorText, level), nil + case AwaitUserInput: + return fmt.Sprintf(AwaitUserInputText, level), nil + default: + return "", errors.New("unable to read execution information status") + } +} diff --git a/routes/reporter/reporter_parser.go b/routes/reporter/reporter_parser.go index 323e496..eb2e54a 100644 --- a/routes/reporter/reporter_parser.go +++ b/routes/reporter/reporter_parser.go @@ -12,7 +12,7 @@ func parseCachePlaybookEntry(cacheEntry cache_model.ExecutionEntry) (api_model.P if err != nil { return api_model.PlaybookExecutionReport{}, err } - + playbookStatusText, err := api_model.GetCacheStatusText(playbookStatus, api_model.ReportLevelPlaybook) playbookErrorStr := "" if cacheEntry.PlaybookResult != nil { playbookErrorStr = cacheEntry.PlaybookResult.Error() @@ -30,7 +30,7 @@ func parseCachePlaybookEntry(cacheEntry cache_model.ExecutionEntry) (api_model.P Started: cacheEntry.Started.String(), Ended: cacheEntry.Ended.String(), Status: playbookStatus, - StatusText: playbookErrorStr, + StatusText: playbookStatusText, Error: playbookErrorStr, StepResults: stepResults, RequestInterval: defaultRequestInterval, @@ -43,6 +43,7 @@ func parseCacheStepEntries(cacheStepEntries map[string]cache_model.StepResult) ( for stepId, stepEntry := range cacheStepEntries { stepStatus, err := api_model.CacheStatusEnum2String(stepEntry.Status) + stepStatusText, err := api_model.GetCacheStatusText(stepStatus, api_model.ReportLevelStep) if err != nil { return map[string]api_model.StepExecutionReport{}, err } @@ -64,7 +65,7 @@ func parseCacheStepEntries(cacheStepEntries map[string]cache_model.StepResult) ( Started: stepEntry.Started.String(), Ended: stepEntry.Ended.String(), Status: stepStatus, - StatusText: stepErrorStr, + StatusText: stepStatusText, ExecutedBy: "soarca", CommandsB64: stepEntry.CommandsB64, Error: stepErrorStr, diff --git a/test/unittest/routes/reporter_api/reporter_api_invocation_test.go b/test/unittest/routes/reporter_api/reporter_api_invocation_test.go index 8217319..d1ea77c 100644 --- a/test/unittest/routes/reporter_api/reporter_api_invocation_test.go +++ b/test/unittest/routes/reporter_api/reporter_api_invocation_test.go @@ -101,15 +101,15 @@ func TestGetExecutionReportInvocation(t *testing.T) { "Started":"2014-11-12 11:45:26.371 +0000 UTC", "Ended":"0001-01-01 00:00:00 +0000 UTC", "Status":"ongoing", - "StatusText":"", + "StatusText":"this playbook is currently being executed", "StepResults":{ "action--test":{ "ExecutionId":"6ba7b810-9dad-11d1-80b4-00c04fd430c0", - "StepId":"action--test", - "Started":"2014-11-12 11:45:26.371 +0000 UTC", - "Ended":"2014-11-12 11:45:26.371 +0000 UTC", - "Status":"successfully_executed", - "StatusText":"", + "StepId": "action--test", + "Started": "2014-11-12 11:45:26.371 +0000 UTC", + "Ended": "2014-11-12 11:45:26.371 +0000 UTC", + "Status": "successfully_executed", + "StatusText": "step execution completed successfully", "Error":"", "Variables":{ "var1":{ diff --git a/test/unittest/routes/reporter_api/reporter_api_test.go b/test/unittest/routes/reporter_api/reporter_api_test.go index 02b5743..3d63904 100644 --- a/test/unittest/routes/reporter_api/reporter_api_test.go +++ b/test/unittest/routes/reporter_api/reporter_api_test.go @@ -259,7 +259,7 @@ func TestGetExecutionReport(t *testing.T) { "Started":"2014-11-12 11:45:26.371 +0000 UTC", "Ended":"0001-01-01 00:00:00 +0000 UTC", "Status":"ongoing", - "StatusText":"", + "StatusText":"this playbook is currently being executed", "StepResults":{ "action--test":{ "ExecutionId":"6ba7b810-9dad-11d1-80b4-00c04fd430c0", @@ -267,7 +267,7 @@ func TestGetExecutionReport(t *testing.T) { "Started":"2014-11-12 11:45:26.371 +0000 UTC", "Ended":"2014-11-12 11:45:26.371 +0000 UTC", "Status":"successfully_executed", - "StatusText":"", + "StatusText": "step execution completed successfully", "Error":"", "Variables":{ "var1":{