Skip to content
Draft
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
35 changes: 35 additions & 0 deletions backend/soarca/helper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package soarca

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -61,3 +62,37 @@ func fetch(ctx context.Context, client *http.Client, url string, modifyRequest f

return body, nil
}

func postJson(client *http.Client, url string, payload interface{}) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout*time.Millisecond)
defer cancel()

return postJsonWithContext(ctx, client, url, payload)
}

func postJsonWithContext(ctx context.Context, client *http.Client, url string, payload interface{}) error {
jsonData, err := json.Marshal(payload)
if err != nil {
return fmt.Errorf("failed to marshal payload: %w", err)
}

req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBuffer(jsonData))
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}

req.Header.Set("Content-Type", "application/json")

response, err := client.Do(req)
if err != nil {
return fmt.Errorf("failed to make POST request: %w", err)
}
defer response.Body.Close()

if response.StatusCode != http.StatusOK {
body, _ := io.ReadAll(response.Body)
return fmt.Errorf("unexpected status code: %d, body: %s", response.StatusCode, string(body))
}

return nil
}
61 changes: 61 additions & 0 deletions backend/soarca/manual.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package soarca

import (
"encoding/json"
"fmt"
"net/http"
models "soarca-gui/models/manual"
)

const (
manualPath = "/manual"
)

type Manual struct {
Host string
client *http.Client
}

func NewManual(host string, client *http.Client) *Manual {
return &Manual{Host: host, client: client}
}

func (m *Manual) GetManualActions() ([]models.ManualAction, error) {
url := fmt.Sprintf("%s%s", m.Host, manualPath)
var actions []models.ManualAction
err := fetchToJson(m.client, url, &actions)

Check failure on line 26 in backend/soarca/manual.go

View workflow job for this annotation

GitHub Actions / Run ci-tests

not enough arguments in call to fetchToJson
if err != nil {
return nil, fmt.Errorf("failed to fetch manual actions: %w", err)
}
return actions, nil
}

func (m *Manual) GetManualActionByIDs(executionID, stepID string) (*models.ManualAction, error) {
url := fmt.Sprintf("%s%s/%s/%s", m.Host, manualPath, executionID, stepID)
var action models.ManualAction
err := fetchToJson(m.client, url, &action)

Check failure on line 36 in backend/soarca/manual.go

View workflow job for this annotation

GitHub Actions / Run ci-tests

not enough arguments in call to fetchToJson
if err != nil {
return nil, fmt.Errorf("failed to fetch manual action: %w", err)
}
return &action, nil
}

func (m *Manual) ContinueManualAction(request models.ManualContinueRequest) error {
url := fmt.Sprintf("%s%s/continue", m.Host, manualPath)

jsonData, err := json.Marshal(request)

Check failure on line 46 in backend/soarca/manual.go

View workflow job for this annotation

GitHub Actions / Run ci-tests

declared and not used: jsonData
if err != nil {
return fmt.Errorf("failed to marshal continue request: %w", err)
}

err = postJson(url, &request)

Check failure on line 51 in backend/soarca/manual.go

View workflow job for this annotation

GitHub Actions / Run ci-tests

not enough arguments in call to postJson
if err != nil {
return nil, fmt.Errorf("failed to fetch manual action: %w", err)

Check failure on line 53 in backend/soarca/manual.go

View workflow job for this annotation

GitHub Actions / Run ci-tests

too many return values
}

if resp.StatusCode != http.StatusOK {

Check failure on line 56 in backend/soarca/manual.go

View workflow job for this annotation

GitHub Actions / Run ci-tests

undefined: resp
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)

Check failure on line 57 in backend/soarca/manual.go

View workflow job for this annotation

GitHub Actions / Run ci-tests

undefined: resp
}

return nil
}
22 changes: 22 additions & 0 deletions models/manual/manual.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package manual

type ManualAction struct {
ExecutionStatus string `json:"execution-status"`
ExecutionID string `json:"execution_id"`
PlaybookID string `json:"playbook_id"`
StepID string `json:"step_id"`
Description string `json:"description"`
Command string `json:"command"`
CommandIsBase64 bool `json:"command_is_base64"`
Targets map[string]interface{} `json:"targets"`
OutArgs map[string]interface{} `json:"out_args"`
}

type ManualContinueRequest struct {
ExecutionStatus string `json:"execution-status"`
ExecutionID string `json:"execution_id"`
PlaybookID string `json:"playbook_id"`
StepID string `json:"step_id"`
ResponseStatus string `json:"response_status"`
ResponseOutArgs map[string]interface{} `json:"response_out_args"`
}
26 changes: 16 additions & 10 deletions routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,37 @@ import (
"github.com/gin-gonic/gin"
)

const requiredGroupPermission = "soarca_admin"

func Setup(app *gin.Engine) {
app.GET("/404-page", handlers.ErrorPage)
app.NoRoute(func(ctx *gin.Context) {
ctx.Redirect(http.StatusTemporaryRedirect, "/404-page")
})

authEnabled, _ := strconv.ParseBool(utils.GetEnv("AUTH_ENABLED", "false"))

reporter := soarca.NewReport(utils.GetEnv("SOARCA_URI", "http://localhost:8080"), &http.Client{}, authEnabled)
status := soarca.NewStatus(utils.GetEnv("SOARCA_URI", "http://localhost:8080"), &http.Client{}, authEnabled)

auth, err := gauth.New(gauth.OIDCRedirectConfig())
authHandler := handlers.NewOIDCAuthHandler(auth)
if err != nil {
log.Fatal("could not configure oidc redirect config: ", err)
}
var auth *gauth.Authenticator
var authHandler *handlers.OIDCAuthHandler
var err error

publicRoutes := app.Group("/")
protectedRoutes := app.Group("/")
protectedRoutes.Use(auth.LoadAuthContext())

PublicRoutes(publicRoutes, authEnabled, authHandler)
if authEnabled {
auth, err = gauth.New(gauth.OIDCRedirectConfig())
if err != nil {
log.Fatal("could not configure oidc redirect config: ", err)
}
authHandler = handlers.NewOIDCAuthHandler(auth)
PublicRoutes(publicRoutes, authEnabled, authHandler)
protectedRoutes.Use(auth.LoadAuthContext())
protectedRoutes.Use(auth.Middleware([]string{requiredGroupPermission}))
}

protectedRoutes.Use(auth.Middleware([]string{"soarca_admin"}))
DashboardRoutes(protectedRoutes, authHandler)

ReportingRoutes(reporter, protectedRoutes, authEnabled)
StatusRoutes(status, protectedRoutes, authEnabled)
SettingsRoutes(protectedRoutes)
Expand Down
Loading