mirror of
https://github.com/SecurityBrewery/catalyst.git
synced 2026-02-07 22:03:32 +01:00
feat: improve python actions (#1083)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package fakedata
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -50,14 +51,12 @@ func Records(app core.App, userCount int, ticketCount int) ([]*models.Record, er
|
||||
|
||||
users := userRecords(app.Dao(), userCount)
|
||||
tickets := ticketRecords(app.Dao(), users, types, ticketCount)
|
||||
webhooks := webhookRecords(app.Dao())
|
||||
reactions := reactionRecords(app.Dao())
|
||||
|
||||
var records []*models.Record
|
||||
records = append(records, users...)
|
||||
records = append(records, types...)
|
||||
records = append(records, tickets...)
|
||||
records = append(records, webhooks...)
|
||||
records = append(records, reactions...)
|
||||
|
||||
return records, nil
|
||||
@@ -69,7 +68,7 @@ func userRecords(dao *daos.Dao, count int) []*models.Record {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var records []*models.Record
|
||||
records := make([]*models.Record, 0, count)
|
||||
|
||||
// create the test user
|
||||
if _, err := dao.FindRecordById(migrations.UserCollectionName, "u_test"); err != nil {
|
||||
@@ -105,7 +104,7 @@ func ticketRecords(dao *daos.Dao, users, types []*models.Record, count int) []*m
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var records []*models.Record
|
||||
records := make([]*models.Record, 0, count)
|
||||
|
||||
created := time.Now()
|
||||
number := gofakeit.Number(200*count, 300*count)
|
||||
@@ -135,114 +134,168 @@ func ticketRecords(dao *daos.Dao, users, types []*models.Record, count int) []*m
|
||||
records = append(records, record)
|
||||
|
||||
// Add comments
|
||||
for range gofakeit.IntN(5) {
|
||||
commentCollection, err := dao.FindCollectionByNameOrId(migrations.CommentCollectionName)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
commentCreated := gofakeit.DateRange(created, time.Now())
|
||||
commentUpdated := gofakeit.DateRange(commentCreated, time.Now())
|
||||
|
||||
commentRecord := models.NewRecord(commentCollection)
|
||||
commentRecord.SetId("c_" + security.PseudorandomString(10))
|
||||
commentRecord.Set("created", commentCreated.Format("2006-01-02T15:04:05Z"))
|
||||
commentRecord.Set("updated", commentUpdated.Format("2006-01-02T15:04:05Z"))
|
||||
commentRecord.Set("ticket", record.GetId())
|
||||
commentRecord.Set("author", random(users).GetId())
|
||||
commentRecord.Set("message", fakeTicketComment())
|
||||
|
||||
records = append(records, commentRecord)
|
||||
}
|
||||
|
||||
// Add timeline
|
||||
for range gofakeit.IntN(5) {
|
||||
timelineCollection, err := dao.FindCollectionByNameOrId(migrations.TimelineCollectionName)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
timelineCreated := gofakeit.DateRange(created, time.Now())
|
||||
timelineUpdated := gofakeit.DateRange(timelineCreated, time.Now())
|
||||
|
||||
timelineRecord := models.NewRecord(timelineCollection)
|
||||
timelineRecord.SetId("tl_" + security.PseudorandomString(10))
|
||||
timelineRecord.Set("created", timelineCreated.Format("2006-01-02T15:04:05Z"))
|
||||
timelineRecord.Set("updated", timelineUpdated.Format("2006-01-02T15:04:05Z"))
|
||||
timelineRecord.Set("ticket", record.GetId())
|
||||
timelineRecord.Set("time", gofakeit.DateRange(created, time.Now()).Format("2006-01-02T15:04:05Z"))
|
||||
timelineRecord.Set("message", fakeTicketTimelineMessage())
|
||||
|
||||
records = append(records, timelineRecord)
|
||||
}
|
||||
|
||||
// Add tasks
|
||||
for range gofakeit.IntN(5) {
|
||||
taskCollection, err := dao.FindCollectionByNameOrId(migrations.TaskCollectionName)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
taskCreated := gofakeit.DateRange(created, time.Now())
|
||||
taskUpdated := gofakeit.DateRange(taskCreated, time.Now())
|
||||
|
||||
taskRecord := models.NewRecord(taskCollection)
|
||||
taskRecord.SetId("ts_" + security.PseudorandomString(10))
|
||||
taskRecord.Set("created", taskCreated.Format("2006-01-02T15:04:05Z"))
|
||||
taskRecord.Set("updated", taskUpdated.Format("2006-01-02T15:04:05Z"))
|
||||
taskRecord.Set("ticket", record.GetId())
|
||||
taskRecord.Set("name", fakeTicketTask())
|
||||
taskRecord.Set("open", gofakeit.Bool())
|
||||
taskRecord.Set("owner", random(users).GetId())
|
||||
|
||||
records = append(records, taskRecord)
|
||||
}
|
||||
|
||||
// Add links
|
||||
for range gofakeit.IntN(5) {
|
||||
linkCollection, err := dao.FindCollectionByNameOrId(migrations.LinkCollectionName)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
linkCreated := gofakeit.DateRange(created, time.Now())
|
||||
linkUpdated := gofakeit.DateRange(linkCreated, time.Now())
|
||||
|
||||
linkRecord := models.NewRecord(linkCollection)
|
||||
linkRecord.SetId("l_" + security.PseudorandomString(10))
|
||||
linkRecord.Set("created", linkCreated.Format("2006-01-02T15:04:05Z"))
|
||||
linkRecord.Set("updated", linkUpdated.Format("2006-01-02T15:04:05Z"))
|
||||
linkRecord.Set("ticket", record.GetId())
|
||||
linkRecord.Set("url", gofakeit.URL())
|
||||
linkRecord.Set("name", random([]string{"Blog", "Forum", "Wiki", "Documentation"}))
|
||||
|
||||
records = append(records, linkRecord)
|
||||
}
|
||||
records = append(records, commentRecords(dao, users, created, record)...)
|
||||
records = append(records, timelineRecords(dao, created, record)...)
|
||||
records = append(records, taskRecords(dao, users, created, record)...)
|
||||
records = append(records, linkRecords(dao, created, record)...)
|
||||
}
|
||||
|
||||
return records
|
||||
}
|
||||
|
||||
func webhookRecords(dao *daos.Dao) []*models.Record {
|
||||
collection, err := dao.FindCollectionByNameOrId(migrations.WebhookCollectionName)
|
||||
func commentRecords(dao *daos.Dao, users []*models.Record, created time.Time, record *models.Record) []*models.Record {
|
||||
commentCollection, err := dao.FindCollectionByNameOrId(migrations.CommentCollectionName)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
record := models.NewRecord(collection)
|
||||
record.SetId("w_" + security.PseudorandomString(10))
|
||||
record.Set("name", "Test Webhook")
|
||||
record.Set("collection", "tickets")
|
||||
record.Set("destination", "http://localhost:8080/webhook")
|
||||
records := make([]*models.Record, 0, 5)
|
||||
|
||||
return []*models.Record{record}
|
||||
for range gofakeit.IntN(5) {
|
||||
commentCreated := gofakeit.DateRange(created, time.Now())
|
||||
commentUpdated := gofakeit.DateRange(commentCreated, time.Now())
|
||||
|
||||
commentRecord := models.NewRecord(commentCollection)
|
||||
commentRecord.SetId("c_" + security.PseudorandomString(10))
|
||||
commentRecord.Set("created", commentCreated.Format("2006-01-02T15:04:05Z"))
|
||||
commentRecord.Set("updated", commentUpdated.Format("2006-01-02T15:04:05Z"))
|
||||
commentRecord.Set("ticket", record.GetId())
|
||||
commentRecord.Set("author", random(users).GetId())
|
||||
commentRecord.Set("message", fakeTicketComment())
|
||||
|
||||
records = append(records, commentRecord)
|
||||
}
|
||||
|
||||
return records
|
||||
}
|
||||
|
||||
func timelineRecords(dao *daos.Dao, created time.Time, record *models.Record) []*models.Record {
|
||||
timelineCollection, err := dao.FindCollectionByNameOrId(migrations.TimelineCollectionName)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
records := make([]*models.Record, 0, 5)
|
||||
|
||||
for range gofakeit.IntN(5) {
|
||||
timelineCreated := gofakeit.DateRange(created, time.Now())
|
||||
timelineUpdated := gofakeit.DateRange(timelineCreated, time.Now())
|
||||
|
||||
timelineRecord := models.NewRecord(timelineCollection)
|
||||
timelineRecord.SetId("tl_" + security.PseudorandomString(10))
|
||||
timelineRecord.Set("created", timelineCreated.Format("2006-01-02T15:04:05Z"))
|
||||
timelineRecord.Set("updated", timelineUpdated.Format("2006-01-02T15:04:05Z"))
|
||||
timelineRecord.Set("ticket", record.GetId())
|
||||
timelineRecord.Set("time", gofakeit.DateRange(created, time.Now()).Format("2006-01-02T15:04:05Z"))
|
||||
timelineRecord.Set("message", fakeTicketTimelineMessage())
|
||||
|
||||
records = append(records, timelineRecord)
|
||||
}
|
||||
|
||||
return records
|
||||
}
|
||||
|
||||
func taskRecords(dao *daos.Dao, users []*models.Record, created time.Time, record *models.Record) []*models.Record {
|
||||
taskCollection, err := dao.FindCollectionByNameOrId(migrations.TaskCollectionName)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
records := make([]*models.Record, 0, 5)
|
||||
|
||||
for range gofakeit.IntN(5) {
|
||||
taskCreated := gofakeit.DateRange(created, time.Now())
|
||||
taskUpdated := gofakeit.DateRange(taskCreated, time.Now())
|
||||
|
||||
taskRecord := models.NewRecord(taskCollection)
|
||||
taskRecord.SetId("ts_" + security.PseudorandomString(10))
|
||||
taskRecord.Set("created", taskCreated.Format("2006-01-02T15:04:05Z"))
|
||||
taskRecord.Set("updated", taskUpdated.Format("2006-01-02T15:04:05Z"))
|
||||
taskRecord.Set("ticket", record.GetId())
|
||||
taskRecord.Set("name", fakeTicketTask())
|
||||
taskRecord.Set("open", gofakeit.Bool())
|
||||
taskRecord.Set("owner", random(users).GetId())
|
||||
|
||||
records = append(records, taskRecord)
|
||||
}
|
||||
|
||||
return records
|
||||
}
|
||||
|
||||
func linkRecords(dao *daos.Dao, created time.Time, record *models.Record) []*models.Record {
|
||||
linkCollection, err := dao.FindCollectionByNameOrId(migrations.LinkCollectionName)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
records := make([]*models.Record, 0, 5)
|
||||
|
||||
for range gofakeit.IntN(5) {
|
||||
linkCreated := gofakeit.DateRange(created, time.Now())
|
||||
linkUpdated := gofakeit.DateRange(linkCreated, time.Now())
|
||||
|
||||
linkRecord := models.NewRecord(linkCollection)
|
||||
linkRecord.SetId("l_" + security.PseudorandomString(10))
|
||||
linkRecord.Set("created", linkCreated.Format("2006-01-02T15:04:05Z"))
|
||||
linkRecord.Set("updated", linkUpdated.Format("2006-01-02T15:04:05Z"))
|
||||
linkRecord.Set("ticket", record.GetId())
|
||||
linkRecord.Set("url", gofakeit.URL())
|
||||
linkRecord.Set("name", random([]string{"Blog", "Forum", "Wiki", "Documentation"}))
|
||||
|
||||
records = append(records, linkRecord)
|
||||
}
|
||||
|
||||
return records
|
||||
}
|
||||
|
||||
const alertIngestPy = `import sys
|
||||
import json
|
||||
import random
|
||||
import os
|
||||
|
||||
from pocketbase import PocketBase
|
||||
|
||||
# Parse the event from the webhook payload
|
||||
event = json.loads(sys.argv[1])
|
||||
body = json.loads(event["body"])
|
||||
|
||||
# Connect to the PocketBase server
|
||||
client = PocketBase('http://127.0.0.1:8090')
|
||||
client.auth_store.save(token=os.environ["CATALYST_TOKEN"])
|
||||
|
||||
# Create a new ticket
|
||||
client.collection("tickets").create({
|
||||
"name": body["name"],
|
||||
"type": "alert",
|
||||
"open": True,
|
||||
})`
|
||||
|
||||
const assignTicketsPy = `import sys
|
||||
import json
|
||||
import random
|
||||
import os
|
||||
|
||||
from pocketbase import PocketBase
|
||||
|
||||
# Parse the ticket from the input
|
||||
ticket = json.loads(sys.argv[1])
|
||||
|
||||
# Connect to the PocketBase server
|
||||
client = PocketBase('http://127.0.0.1:8090')
|
||||
client.auth_store.save(token=os.environ["CATALYST_TOKEN"])
|
||||
|
||||
# Get a random user
|
||||
users = client.collection("users").get_list(1, 200)
|
||||
random_user = random.choice(users.items)
|
||||
|
||||
# Assign the ticket to the random user
|
||||
client.collection("tickets").update(ticket["record"]["id"], {
|
||||
"owner": random_user.id,
|
||||
})`
|
||||
|
||||
const (
|
||||
triggerWebhook = `{"token":"1234567890","path":"webhook"}`
|
||||
reactionPython = `{"requirements":"requests","script":"import sys\n\nprint(sys.argv[1])"}`
|
||||
triggerHook = `{"collections":["tickets","comments"],"events":["create","update","delete"]}`
|
||||
reactionWebhook = `{"headers":["Content-Type: application/json"],"url":"http://localhost:8080/webhook"}`
|
||||
triggerWebhook = `{"token":"1234567890","path":"webhook"}`
|
||||
triggerHook = `{"collections":["tickets"],"events":["create"]}`
|
||||
)
|
||||
|
||||
func reactionRecords(dao *daos.Dao) []*models.Record {
|
||||
@@ -253,23 +306,39 @@ func reactionRecords(dao *daos.Dao) []*models.Record {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
alertIngestActionData, err := json.Marshal(map[string]interface{}{
|
||||
"requirements": "pocketbase",
|
||||
"script": alertIngestPy,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
record := models.NewRecord(collection)
|
||||
record.SetId("w_" + security.PseudorandomString(10))
|
||||
record.Set("name", "Test Reaction")
|
||||
record.Set("trigger", "webhook")
|
||||
record.Set("triggerdata", triggerWebhook)
|
||||
record.Set("action", "python")
|
||||
record.Set("actiondata", reactionPython)
|
||||
record.Set("actiondata", string(alertIngestActionData))
|
||||
|
||||
records = append(records, record)
|
||||
|
||||
assignTicketsActionData, err := json.Marshal(map[string]interface{}{
|
||||
"requirements": "pocketbase",
|
||||
"script": assignTicketsPy,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
record = models.NewRecord(collection)
|
||||
record.SetId("w_" + security.PseudorandomString(10))
|
||||
record.Set("name", "Test Reaction 2")
|
||||
record.Set("trigger", "hook")
|
||||
record.Set("triggerdata", triggerHook)
|
||||
record.Set("action", "webhook")
|
||||
record.Set("actiondata", reactionWebhook)
|
||||
record.Set("action", "python")
|
||||
record.Set("actiondata", string(assignTicketsActionData))
|
||||
|
||||
records = append(records, record)
|
||||
|
||||
|
||||
@@ -11,7 +11,9 @@ import (
|
||||
)
|
||||
|
||||
func Test_records(t *testing.T) {
|
||||
app, cleanup := catalystTesting.App(t)
|
||||
t.Parallel()
|
||||
|
||||
app, _, cleanup := catalystTesting.App(t)
|
||||
defer cleanup()
|
||||
|
||||
got, err := fakedata.Records(app, 2, 2)
|
||||
@@ -21,7 +23,9 @@ func Test_records(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGenerate(t *testing.T) {
|
||||
app, cleanup := catalystTesting.App(t)
|
||||
t.Parallel()
|
||||
|
||||
app, _, cleanup := catalystTesting.App(t)
|
||||
defer cleanup()
|
||||
|
||||
err := fakedata.Generate(app, 0, 0)
|
||||
|
||||
@@ -7,22 +7,32 @@ import (
|
||||
)
|
||||
|
||||
func Test_fakeTicketComment(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert.NotEmpty(t, fakeTicketComment())
|
||||
}
|
||||
|
||||
func Test_fakeTicketDescription(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert.NotEmpty(t, fakeTicketDescription())
|
||||
}
|
||||
|
||||
func Test_fakeTicketTask(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert.NotEmpty(t, fakeTicketTask())
|
||||
}
|
||||
|
||||
func Test_fakeTicketTimelineMessage(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert.NotEmpty(t, fakeTicketTimelineMessage())
|
||||
}
|
||||
|
||||
func Test_random(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
type args[T any] struct {
|
||||
e []T
|
||||
}
|
||||
@@ -40,6 +50,8 @@ func Test_random(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
got := random(tt.args.e)
|
||||
|
||||
assert.Contains(t, tt.args.e, got)
|
||||
|
||||
Reference in New Issue
Block a user