Skip to content

Commit

Permalink
implement status text
Browse files Browse the repository at this point in the history
  • Loading branch information
lucamrgs committed May 14, 2024
1 parent 988f428 commit 79b66f1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
39 changes: 39 additions & 0 deletions models/api/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"errors"
"fmt"
"soarca/models/cacao"
cache_model "soarca/models/cache"
)
Expand All @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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")
}
}
7 changes: 4 additions & 3 deletions routes/reporter/reporter_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
Expand All @@ -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
}
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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":{
Expand Down
4 changes: 2 additions & 2 deletions test/unittest/routes/reporter_api/reporter_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,15 @@ 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",
"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":"",
"StatusText": "step execution completed successfully",
"Error":"",
"Variables":{
"var1":{
Expand Down

0 comments on commit 79b66f1

Please sign in to comment.