Skip to content

Commit

Permalink
change reporter api resutls model
Browse files Browse the repository at this point in the history
  • Loading branch information
lucamrgs committed May 15, 2024
1 parent 867f444 commit 841c51a
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 125 deletions.
1 change: 0 additions & 1 deletion docs/content/en/docs/core-components/api-reporter.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ Response data model:
|ended |timestamp |string |The time at which the execution of the playbook ended (if so)
|status |execution-status-enum |string |The current [status](#execution-stataus) of the execution
|status_text |explanation |string |A natural language explanation of the current status or related info
|error |error |string |Error raised along the execution of the playbook at execution level
|step_results |step_results |dictionary |Map of step-id to related [step execution data](#step-execution-data)
|request_interval |seconds |integer |Suggests the polling interval for the next request (default suggested is 5 seconds).

Expand Down
2 changes: 1 addition & 1 deletion internal/reporter/downstream_reporter/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (cacheReporter *Cache) ReportWorkflowEnd(executionId uuid.UUID, playbook ca
}

if workflowError != nil {
executionEntry.PlaybookResult = workflowError
executionEntry.Error = workflowError
executionEntry.Status = cache_report.Failed
} else {
executionEntry.Status = cache_report.SuccessfullyExecuted
Expand Down
41 changes: 20 additions & 21 deletions models/api/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"soarca/models/cacao"
cache_model "soarca/models/cache"
"time"
)

type Status uint8
Expand Down Expand Up @@ -35,30 +36,28 @@ const (
)

type PlaybookExecutionReport struct {
Type string
ExecutionId string
PlaybookId string
Started string
Ended string
Status string
StatusText string
StepResults map[string]StepExecutionReport
Error string
RequestInterval int
Type string `bson:"type" json:"type"`
ExecutionId string `bson:"execution_id" json:"execution_id"`
PlaybookId string `bson:"playbook_id" json:"playbook_id"`
Started time.Time `bson:"started" json:"started"`
Ended time.Time `bson:"ended" json:"ended"`
Status string `bson:"status" json:"status"`
StatusText string `bson:"status_text" json:"status_text"`
StepResults map[string]StepExecutionReport `bson:"step_results" json:"step_results"`
RequestInterval int `bson:"request_interval" json:"request_interval"`
}

type StepExecutionReport struct {
ExecutionId string
StepId string
Started string
Ended string
Status string
StatusText string
ExecutedBy string
CommandsB64 []string
Error string
Variables map[string]cacao.Variable
AutomatedExecution string
ExecutionId string `bson:"execution_id" json:"execution_id"`
StepId string `bson:"step_id" json:"step_id"`
Started time.Time `bson:"started" json:"started"`
Ended time.Time `bson:"ended" json:"ended"`
Status string `bson:"status" json:"status"`
StatusText string `bson:"status_text" json:"status_text"`
ExecutedBy string `bson:"executed_by" json:"executed_by"`
CommandsB64 []string `bson:"commands_b64" json:"commands_b64"`
Variables map[string]cacao.Variable `bson:"variables" json:"variables"`
AutomatedExecution bool `bson:"automated_execution" json:"automated_execution"`
// Make sure we can have a playbookID for playbook actions, and also
// the execution ID for the invoked playbook
}
Expand Down
14 changes: 7 additions & 7 deletions models/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ const (
)

type ExecutionEntry struct {
ExecutionId uuid.UUID
PlaybookId string
Started time.Time
Ended time.Time
StepResults map[string]StepResult
PlaybookResult error
Status Status
ExecutionId uuid.UUID
PlaybookId string
Started time.Time
Ended time.Time
StepResults map[string]StepResult
Error error
Status Status
}

type StepResult struct {
Expand Down
7 changes: 0 additions & 7 deletions routes/playbook/playbook_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ import (
"github.com/gin-gonic/gin"
)

// TODOs
// Add all command types to cacao model
// Change api parsing model to actual data types and add json bson strings for formatting
// Refactor reporter parsing in getting info from cache via now using new model
// Remove copy of cache entries and just pass the objects
// Remove Error from step cache report

// A PlaybookController implements the playbook API endpoints is dependent on a database.
type playbookController struct {
playbookRepo playbookrepository.IPlaybookRepository
Expand Down
5 changes: 5 additions & 0 deletions routes/reporter/reporter_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import (
"soarca/logger"
)

// TODOs
// Change api parsing model to actual data types and add json bson strings for formatting
// Refactor reporter parsing in getting info from cache via now using new model
// Remove copy of cache entries and just pass the objects

var log *logger.Log

type Empty struct{}
Expand Down
29 changes: 10 additions & 19 deletions routes/reporter/reporter_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ 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)
if err != nil {
return api_model.PlaybookExecutionReport{}, err
}
playbookErrorStr := ""
if cacheEntry.PlaybookResult != nil {
playbookErrorStr = cacheEntry.PlaybookResult.Error()
if cacheEntry.Error != nil {
playbookStatusText = playbookStatusText + " - error: " + cacheEntry.Error.Error()
}

stepResults, err := parseCacheStepEntries(cacheEntry.StepResults)
Expand All @@ -30,11 +30,10 @@ func parseCachePlaybookEntry(cacheEntry cache_model.ExecutionEntry) (api_model.P
Type: "execution_status",
ExecutionId: cacheEntry.ExecutionId.String(),
PlaybookId: cacheEntry.PlaybookId,
Started: cacheEntry.Started.String(),
Ended: cacheEntry.Ended.String(),
Started: cacheEntry.Started,
Ended: cacheEntry.Ended,
Status: playbookStatus,
StatusText: playbookStatusText,
Error: playbookErrorStr,
StepResults: stepResults,
RequestInterval: defaultRequestInterval,
}
Expand All @@ -54,29 +53,21 @@ func parseCacheStepEntries(cacheStepEntries map[string]cache_model.StepResult) (
return map[string]api_model.StepExecutionReport{}, err
}

stepError := stepEntry.Error
stepErrorStr := ""
if stepError != nil {
stepErrorStr = stepError.Error()
}

automatedExecution := "true"
if !stepEntry.IsAutomated {
automatedExecution = "false"
if stepEntry.Error != nil {
stepStatusText = stepStatusText + " - error: " + stepEntry.Error.Error()
}

parsedEntries[stepId] = api_model.StepExecutionReport{
ExecutionId: stepEntry.ExecutionId.String(),
StepId: stepEntry.StepId,
Started: stepEntry.Started.String(),
Ended: stepEntry.Ended.String(),
Started: stepEntry.Started,
Ended: stepEntry.Ended,
Status: stepStatus,
StatusText: stepStatusText,
ExecutedBy: "soarca",
CommandsB64: stepEntry.CommandsB64,
Error: stepErrorStr,
Variables: stepEntry.Variables,
AutomatedExecution: automatedExecution,
AutomatedExecution: stepEntry.IsAutomated,
}
}
return parsedEntries, nil
Expand Down
42 changes: 21 additions & 21 deletions test/unittest/reporters/downstream_reporter/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ func TestReportWorkflowStartFirst(t *testing.T) {
expectedEnded, _ := time.Parse(layout, "0001-01-01T00:00:00Z")
expectedExecutions := []cache_model.ExecutionEntry{
{
ExecutionId: executionId0,
PlaybookId: "test",
Started: expectedStarted,
Ended: expectedEnded,
StepResults: map[string]cache_model.StepResult{},
PlaybookResult: nil,
Status: 2,
ExecutionId: executionId0,
PlaybookId: "test",
Started: expectedStarted,
Ended: expectedEnded,
StepResults: map[string]cache_model.StepResult{},
Error: nil,
Status: 2,
},
}

Expand Down Expand Up @@ -207,13 +207,13 @@ func TestReportWorkflowStartFifo(t *testing.T) {
for _, executionId := range executionIds[:len(executionIds)-1] {
t.Log(executionId)
entry := cache_model.ExecutionEntry{
ExecutionId: executionId,
PlaybookId: "test",
Started: expectedStarted,
Ended: expectedEnded,
StepResults: map[string]cache_model.StepResult{},
PlaybookResult: nil,
Status: 2,
ExecutionId: executionId,
PlaybookId: "test",
Started: expectedStarted,
Ended: expectedEnded,
StepResults: map[string]cache_model.StepResult{},
Error: nil,
Status: 2,
}
expectedExecutionsFull = append(expectedExecutionsFull, entry)
}
Expand All @@ -222,13 +222,13 @@ func TestReportWorkflowStartFifo(t *testing.T) {
for _, executionId := range executionIds[1:] {
t.Log(executionId)
entry := cache_model.ExecutionEntry{
ExecutionId: executionId,
PlaybookId: "test",
Started: expectedStarted,
Ended: expectedEnded,
StepResults: map[string]cache_model.StepResult{},
PlaybookResult: nil,
Status: 2,
ExecutionId: executionId,
PlaybookId: "test",
Started: expectedStarted,
Ended: expectedEnded,
StepResults: map[string]cache_model.StepResult{},
Error: nil,
Status: 2,
}
expectedExecutionsFifo = append(expectedExecutionsFifo, entry)
}
Expand Down
38 changes: 18 additions & 20 deletions test/unittest/routes/reporter_api/reporter_api_invocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,36 +95,34 @@ func TestGetExecutionReportInvocation(t *testing.T) {
app.ServeHTTP(recorder, request)

expectedResponse := `{
"Type":"execution_status",
"ExecutionId":"6ba7b810-9dad-11d1-80b4-00c04fd430c0",
"PlaybookId":"test",
"Started":"2014-11-12 11:45:26.371 +0000 UTC",
"Ended":"0001-01-01 00:00:00 +0000 UTC",
"Status":"ongoing",
"StatusText":"this playbook is currently being executed",
"StepResults":{
"type":"execution_status",
"execution_id":"6ba7b810-9dad-11d1-80b4-00c04fd430c0",
"playbook_id":"test",
"started":"2014-11-12T11:45:26.371Z",
"ended":"0001-01-01T00:00:00Z",
"status":"ongoing",
"status_text":"this playbook is currently being executed",
"step_results":{
"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": "step execution completed successfully",
"Error":"",
"execution_id":"6ba7b810-9dad-11d1-80b4-00c04fd430c0",
"step_id": "action--test",
"started": "2014-11-12T11:45:26.371Z",
"ended": "2014-11-12T11:45:26.371Z",
"status": "successfully_executed",
"status_text": "step execution completed successfully",
"Variables":{
"var1":{
"type":"string",
"name":"var1",
"value":"testing"
}
},
"CommandsB64" : [],
"AutomatedExecution" : "true",
"ExecutedBy" : "soarca"
"commands_b64" : [],
"automated_execution" : true,
"executed_by" : "soarca"
}
},
"Error":"",
"RequestInterval":5
"request_interval":5
}`
expectedResponseData := api_model.PlaybookExecutionReport{}
err = json.Unmarshal([]byte(expectedResponse), &expectedResponseData)
Expand Down
Loading

0 comments on commit 841c51a

Please sign in to comment.