Skip to content

Commit 51a9933

Browse files
Feature/17 openc2 capability (#21)
Co-authored-by: Maarten de Kruijf <[email protected]>
1 parent 7ae126b commit 51a9933

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package openc2
2+
3+
import (
4+
"reflect"
5+
6+
"soarca/logger"
7+
"soarca/models/cacao"
8+
"soarca/models/execution"
9+
"soarca/utils/http"
10+
)
11+
12+
type OpenC2Capability struct {
13+
httpRequest http.IHttpRequest
14+
}
15+
16+
type Empty struct{}
17+
18+
const (
19+
openc2ResultVariableName = "__soarca_openc2_http_result__"
20+
openc2capabilityName = "soarca-openc2-http"
21+
)
22+
23+
var (
24+
component = reflect.TypeOf(Empty{}).PkgPath()
25+
log *logger.Log
26+
)
27+
28+
func init() {
29+
log = logger.Logger(component, logger.Info, "", logger.Json)
30+
}
31+
32+
func New(httpRequest http.IHttpRequest) *OpenC2Capability {
33+
return &OpenC2Capability{httpRequest: httpRequest}
34+
}
35+
36+
func (OpenC2Capability *OpenC2Capability) GetType() string {
37+
return openc2capabilityName
38+
}
39+
40+
func (OpenC2Capability *OpenC2Capability) Execute(
41+
metadata execution.Metadata,
42+
command cacao.Command,
43+
authentication cacao.AuthenticationInformation,
44+
target cacao.AgentTarget,
45+
variables cacao.Variables,
46+
) (cacao.Variables, error) {
47+
log.Trace(metadata.ExecutionId)
48+
49+
httpOptions := http.HttpOptions{
50+
Command: &command,
51+
Target: &target,
52+
Auth: &authentication,
53+
}
54+
response, err := OpenC2Capability.httpRequest.Request(httpOptions)
55+
if err != nil {
56+
log.Error(err)
57+
return cacao.NewVariables(), err
58+
}
59+
60+
results := cacao.NewVariables(cacao.Variable{Name: openc2ResultVariableName, Value: string(response)})
61+
log.Trace("Finished openc2 execution, will return the variables: ", results)
62+
return results, nil
63+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package openc2_test
2+
3+
import (
4+
"testing"
5+
6+
openc2 "soarca/internal/capability/openc2"
7+
"soarca/models/cacao"
8+
"soarca/models/execution"
9+
mockRequest "soarca/test/unittest/mocks/mock_utils/http"
10+
"soarca/utils/http"
11+
12+
assert "github.com/go-playground/assert/v2"
13+
"github.com/google/uuid"
14+
)
15+
16+
func TestOpenC2Request(t *testing.T) {
17+
mockHttp := &mockRequest.MockHttpRequest{}
18+
openc2 := openc2.New(mockHttp)
19+
20+
authId, _ := uuid.Parse("6aa7b810-9dad-11d1-81b4-00c04fd430c8")
21+
executionId, _ := uuid.Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
22+
playbookId, _ := uuid.Parse("d09351a2-a075-40c8-8054-0b7c423db83f")
23+
stepId, _ := uuid.Parse("81eff59f-d084-4324-9e0a-59e353dbd28f")
24+
25+
target := cacao.AgentTarget{
26+
HttpUrl: "https://soarca.tno.nl",
27+
AuthInfoIdentifier: authId.String(),
28+
}
29+
30+
auth := cacao.AuthenticationInformation{
31+
ID: authId.String(),
32+
Type: "oauth2",
33+
Token: "this-is-a-test",
34+
}
35+
36+
command := cacao.Command{
37+
Type: "http-api",
38+
Command: "POST / HTTP/1.1",
39+
Headers: map[string]string{"accept": "application/json"},
40+
}
41+
42+
cacaoVariable := cacao.Variable{
43+
Type: "string",
44+
Name: "test request building",
45+
Value: "",
46+
}
47+
48+
metadata := execution.Metadata{
49+
ExecutionId: executionId,
50+
PlaybookId: playbookId.String(),
51+
StepId: stepId.String(),
52+
}
53+
54+
httpOptions := http.HttpOptions{
55+
Command: &command,
56+
Target: &target,
57+
Auth: &auth,
58+
}
59+
60+
payload := "test payload"
61+
62+
payloadBytes := []byte(payload)
63+
64+
mockHttp.On("Request", httpOptions).Return(payloadBytes, nil)
65+
66+
results, err := openc2.Execute(
67+
metadata,
68+
command,
69+
auth,
70+
target,
71+
cacao.NewVariables(cacaoVariable))
72+
if err != nil {
73+
t.Log(err)
74+
t.Fail()
75+
}
76+
t.Log(results)
77+
assert.Equal(t, results["__soarca_openc2_http_result__"].Value, payload)
78+
}

0 commit comments

Comments
 (0)