Skip to content

Commit d7f95c3

Browse files
wip on manual step integration
1 parent 09420b7 commit d7f95c3

File tree

4 files changed

+124
-10
lines changed

4 files changed

+124
-10
lines changed

backend/soarca/helper.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package soarca
22

33
import (
4+
"bytes"
45
"context"
56
"encoding/json"
67
"fmt"
@@ -14,7 +15,6 @@ const (
1415
)
1516

1617
func fetchToJson(client *http.Client, url string, target interface{}) error {
17-
1818
ctx, cancel := context.WithTimeout(context.Background(), timeout*time.Millisecond)
1919
defer cancel()
2020

@@ -58,3 +58,37 @@ func fetch(ctx context.Context, client *http.Client, url string) ([]byte, error)
5858

5959
return body, nil
6060
}
61+
62+
func postJson(client *http.Client, url string, payload interface{}) error {
63+
ctx, cancel := context.WithTimeout(context.Background(), timeout*time.Millisecond)
64+
defer cancel()
65+
66+
return postJsonWithContext(ctx, client, url, payload)
67+
}
68+
69+
func postJsonWithContext(ctx context.Context, client *http.Client, url string, payload interface{}) error {
70+
jsonData, err := json.Marshal(payload)
71+
if err != nil {
72+
return fmt.Errorf("failed to marshal payload: %w", err)
73+
}
74+
75+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBuffer(jsonData))
76+
if err != nil {
77+
return fmt.Errorf("failed to create request: %w", err)
78+
}
79+
80+
req.Header.Set("Content-Type", "application/json")
81+
82+
response, err := client.Do(req)
83+
if err != nil {
84+
return fmt.Errorf("failed to make POST request: %w", err)
85+
}
86+
defer response.Body.Close()
87+
88+
if response.StatusCode != http.StatusOK {
89+
body, _ := io.ReadAll(response.Body)
90+
return fmt.Errorf("unexpected status code: %d, body: %s", response.StatusCode, string(body))
91+
}
92+
93+
return nil
94+
}

backend/soarca/manual.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package soarca
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
models "soarca-gui/models/manual"
8+
)
9+
10+
const (
11+
manualPath = "/manual"
12+
)
13+
14+
type Manual struct {
15+
Host string
16+
client *http.Client
17+
}
18+
19+
func NewManual(host string, client *http.Client) *Manual {
20+
return &Manual{Host: host, client: client}
21+
}
22+
23+
func (m *Manual) GetManualActions() ([]models.ManualAction, error) {
24+
url := fmt.Sprintf("%s%s", m.Host, manualPath)
25+
var actions []models.ManualAction
26+
err := fetchToJson(m.client, url, &actions)
27+
if err != nil {
28+
return nil, fmt.Errorf("failed to fetch manual actions: %w", err)
29+
}
30+
return actions, nil
31+
}
32+
33+
func (m *Manual) GetManualActionByIDs(executionID, stepID string) (*models.ManualAction, error) {
34+
url := fmt.Sprintf("%s%s/%s/%s", m.Host, manualPath, executionID, stepID)
35+
var action models.ManualAction
36+
err := fetchToJson(m.client, url, &action)
37+
if err != nil {
38+
return nil, fmt.Errorf("failed to fetch manual action: %w", err)
39+
}
40+
return &action, nil
41+
}
42+
43+
func (m *Manual) ContinueManualAction(request models.ManualContinueRequest) error {
44+
url := fmt.Sprintf("%s%s/continue", m.Host, manualPath)
45+
46+
jsonData, err := json.Marshal(request)
47+
if err != nil {
48+
return fmt.Errorf("failed to marshal continue request: %w", err)
49+
}
50+
51+
err = postJson(url, &request)
52+
if err != nil {
53+
return nil, fmt.Errorf("failed to fetch manual action: %w", err)
54+
}
55+
56+
if resp.StatusCode != http.StatusOK {
57+
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
58+
}
59+
60+
return nil
61+
}

handlers/reporting.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package handlers
33
import (
44
"errors"
55
"net/http"
6-
76
"soarca-gui/backend"
87
"soarca-gui/models/reporter"
98
"soarca-gui/utils"
@@ -28,14 +27,12 @@ func (r *reportingHandler) ReportingIndexHandler(context *gin.Context) {
2827
}
2928

3029
func (r *reportingHandler) ReportingCardSectionHandler(context *gin.Context) {
31-
3230
reports, err := r.reporter.GetReports()
33-
3431
if err != nil {
3532
metrics := []cards.ReportingCardData{
36-
cards.ReportingCardData{Type: cards.Unkown},
37-
cards.ReportingCardData{Type: cards.Unkown},
38-
cards.ReportingCardData{Type: cards.Unkown},
33+
{Type: cards.Unkown},
34+
{Type: cards.Unkown},
35+
{Type: cards.Unkown},
3936
}
4037
render := utils.NewTempl(context, http.StatusOK, cards.ReportingMetricCards(metrics))
4138
context.Render(http.StatusInternalServerError, render)
@@ -46,9 +43,9 @@ func (r *reportingHandler) ReportingCardSectionHandler(context *gin.Context) {
4643
failedCount := countStatusType("failed", reports)
4744

4845
metrics := []cards.ReportingCardData{
49-
cards.ReportingCardData{Type: cards.Succes, Value: succesCount},
50-
cards.ReportingCardData{Type: cards.Ongoing, Value: ongoingCount},
51-
cards.ReportingCardData{Type: cards.Failed, Value: failedCount},
46+
{Type: cards.Succes, Value: succesCount},
47+
{Type: cards.Ongoing, Value: ongoingCount},
48+
{Type: cards.Failed, Value: failedCount},
5249
}
5350
render := utils.NewTempl(context, http.StatusOK, cards.ReportingMetricCards(metrics))
5451
context.Render(http.StatusOK, render)

models/manual/manual.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package manual
2+
3+
type ManualAction struct {
4+
ExecutionStatus string `json:"execution-status"`
5+
ExecutionID string `json:"execution_id"`
6+
PlaybookID string `json:"playbook_id"`
7+
StepID string `json:"step_id"`
8+
Description string `json:"description"`
9+
Command string `json:"command"`
10+
CommandIsBase64 bool `json:"command_is_base64"`
11+
Targets map[string]interface{} `json:"targets"`
12+
OutArgs map[string]interface{} `json:"out_args"`
13+
}
14+
15+
type ManualContinueRequest struct {
16+
ExecutionStatus string `json:"execution-status"`
17+
ExecutionID string `json:"execution_id"`
18+
PlaybookID string `json:"playbook_id"`
19+
StepID string `json:"step_id"`
20+
ResponseStatus string `json:"response_status"`
21+
ResponseOutArgs map[string]interface{} `json:"response_out_args"`
22+
}

0 commit comments

Comments
 (0)