mirror of
https://github.com/SecurityBrewery/catalyst.git
synced 2026-05-05 00:47:51 +02:00
refactor: improve setup and maintainability (#1067)
This commit is contained in:
@@ -0,0 +1,224 @@
|
||||
package fakedata
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/brianvoe/gofakeit/v7"
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"github.com/pocketbase/pocketbase/daos"
|
||||
"github.com/pocketbase/pocketbase/models"
|
||||
"github.com/pocketbase/pocketbase/tools/security"
|
||||
|
||||
"github.com/SecurityBrewery/catalyst/migrations"
|
||||
)
|
||||
|
||||
const (
|
||||
minimumUserCount = 1
|
||||
minimumTicketCount = 1
|
||||
)
|
||||
|
||||
func Generate(app *pocketbase.PocketBase, userCount, ticketCount int) error {
|
||||
if userCount < minimumUserCount {
|
||||
userCount = minimumUserCount
|
||||
}
|
||||
|
||||
if ticketCount < minimumTicketCount {
|
||||
ticketCount = minimumTicketCount
|
||||
}
|
||||
|
||||
types, err := app.Dao().FindRecordsByExpr(migrations.TypeCollectionName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
users := userRecords(app.Dao(), userCount)
|
||||
tickets := ticketRecords(app.Dao(), users, types, ticketCount)
|
||||
webhooks := webhookRecords(app.Dao())
|
||||
|
||||
for _, records := range [][]*models.Record{users, tickets, webhooks} {
|
||||
for _, record := range records {
|
||||
if err := app.Dao().SaveRecord(record); err != nil {
|
||||
app.Logger().Error(err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func userRecords(dao *daos.Dao, count int) []*models.Record {
|
||||
collection, err := dao.FindCollectionByNameOrId(migrations.UserCollectionName)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var records []*models.Record
|
||||
|
||||
// create the test user
|
||||
if _, err := dao.FindRecordById(migrations.UserCollectionName, "u_test"); err != nil {
|
||||
record := models.NewRecord(collection)
|
||||
record.SetId("u_test")
|
||||
_ = record.SetUsername("u_test")
|
||||
_ = record.SetPassword("1234567890")
|
||||
record.Set("name", gofakeit.Name())
|
||||
record.Set("email", "user@catalyst-soar.com")
|
||||
_ = record.SetVerified(true)
|
||||
|
||||
records = append(records, record)
|
||||
}
|
||||
|
||||
for range count - 1 {
|
||||
record := models.NewRecord(collection)
|
||||
record.SetId("u_" + security.PseudorandomString(10))
|
||||
_ = record.SetUsername("u_" + security.RandomStringWithAlphabet(5, "123456789"))
|
||||
_ = record.SetPassword("1234567890")
|
||||
record.Set("name", gofakeit.Name())
|
||||
record.Set("email", gofakeit.Username()+"@catalyst-soar.com")
|
||||
_ = record.SetVerified(true)
|
||||
|
||||
records = append(records, record)
|
||||
}
|
||||
|
||||
return records
|
||||
}
|
||||
|
||||
func ticketRecords(dao *daos.Dao, users, types []*models.Record, count int) []*models.Record {
|
||||
collection, err := dao.FindCollectionByNameOrId(migrations.TicketCollectionName)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var records []*models.Record
|
||||
|
||||
created := time.Now()
|
||||
number := gofakeit.Number(200*count, 300*count)
|
||||
|
||||
for range count {
|
||||
number -= gofakeit.Number(100, 200)
|
||||
created = created.Add(time.Duration(-gofakeit.Number(13, 37)) * time.Hour)
|
||||
|
||||
record := models.NewRecord(collection)
|
||||
record.SetId("t_" + security.PseudorandomString(10))
|
||||
|
||||
updated := gofakeit.DateRange(created, time.Now())
|
||||
|
||||
ticketType := random(types)
|
||||
|
||||
record.Set("created", created.Format("2006-01-02T15:04:05Z"))
|
||||
record.Set("updated", updated.Format("2006-01-02T15:04:05Z"))
|
||||
|
||||
record.Set("name", fmt.Sprintf("%s-%d", strings.ToUpper(ticketType.GetString("singular")), number))
|
||||
record.Set("type", ticketType.GetId())
|
||||
record.Set("description", fakeTicketDescription())
|
||||
record.Set("open", gofakeit.Bool())
|
||||
record.Set("schema", `{"type":"object","properties":{"tlp":{"title":"TLP","type":"string"}}}`)
|
||||
record.Set("state", `{"tlp":"AMBER"}`)
|
||||
record.Set("owner", random(users).GetId())
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
return records
|
||||
}
|
||||
|
||||
func webhookRecords(dao *daos.Dao) []*models.Record {
|
||||
collection, err := dao.FindCollectionByNameOrId(migrations.WebhookCollectionName)
|
||||
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")
|
||||
|
||||
return []*models.Record{record}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package fakedata
|
||||
|
||||
import "github.com/brianvoe/gofakeit/v7"
|
||||
|
||||
func fakeTicketDescription() string {
|
||||
return random([]string{
|
||||
"Unauthorized access attempt detected in the main server room.",
|
||||
"Multiple failed login attempts from an unknown IP address.",
|
||||
"Suspicious file download flagged by antivirus software.",
|
||||
"User account locked due to repeated incorrect password entries.",
|
||||
"Unusual network activity observed on the internal firewall.",
|
||||
"Phishing email reported by several employees.",
|
||||
"Sensitive data transfer detected outside the approved hours.",
|
||||
"Malware infection found on a workstation in the finance department.",
|
||||
"Unauthorized device connected to the company network.",
|
||||
"Brute-force attack attempt on the admin account detected.",
|
||||
"Security patch required for vulnerability in outdated software.",
|
||||
"External IP address attempting to probe network ports.",
|
||||
"Suspicious behavior detected by user in HR department.",
|
||||
"Unauthorized software installation on company laptop.",
|
||||
"Access control system malfunction at the main entrance.",
|
||||
"DDoS attack detected on company web server.",
|
||||
"Unusual outbound traffic to a known malicious domain.",
|
||||
"Potential insider threat flagged by behavior analysis tool.",
|
||||
"Compromised credentials detected on dark web.",
|
||||
"Encryption key rotation required for compliance with security policy.",
|
||||
})
|
||||
}
|
||||
|
||||
func fakeTicketComment() string {
|
||||
return random([]string{
|
||||
"Ticket opened by user.",
|
||||
"Initial investigation started.",
|
||||
"Further analysis required.",
|
||||
"Escalated to security team.",
|
||||
"Action taken to mitigate risk.",
|
||||
"Resolution in progress.",
|
||||
"User notified of incident.",
|
||||
"Security incident confirmed.",
|
||||
"Containment measures implemented.",
|
||||
"Root cause analysis underway.",
|
||||
"Forensic investigation initiated.",
|
||||
"Data breach confirmed.",
|
||||
"Incident response team activated.",
|
||||
"Legal counsel consulted.",
|
||||
"Public relations notified.",
|
||||
"Regulatory authorities informed.",
|
||||
"Compensation plan developed.",
|
||||
"Press release drafted.",
|
||||
"Media monitoring in progress.",
|
||||
"Post-incident review scheduled.",
|
||||
})
|
||||
}
|
||||
|
||||
func fakeTicketTimelineMessage() string {
|
||||
return random([]string{
|
||||
"Initial investigation started.",
|
||||
"Further analysis required.",
|
||||
"Escalated to security team.",
|
||||
"Action taken to mitigate risk.",
|
||||
"Resolution in progress.",
|
||||
"User notified of incident.",
|
||||
"Security incident confirmed.",
|
||||
"Containment measures implemented.",
|
||||
"Root cause analysis underway.",
|
||||
"Forensic investigation initiated.",
|
||||
"Data breach confirmed.",
|
||||
"Incident response team activated.",
|
||||
"Legal counsel consulted.",
|
||||
"Public relations notified.",
|
||||
"Regulatory authorities informed.",
|
||||
"Compensation plan developed.",
|
||||
"Press release drafted.",
|
||||
"Media monitoring in progress.",
|
||||
"Post-incident review scheduled.",
|
||||
})
|
||||
}
|
||||
|
||||
func fakeTicketTask() string {
|
||||
return random([]string{
|
||||
"Interview witnesses.",
|
||||
"Review security camera footage.",
|
||||
"Analyze network traffic logs.",
|
||||
"Scan for malware on affected systems.",
|
||||
"Check for unauthorized software installations.",
|
||||
"Conduct vulnerability assessment.",
|
||||
"Implement security patch.",
|
||||
"Change firewall rules.",
|
||||
"Reset compromised credentials.",
|
||||
"Isolate infected systems.",
|
||||
"Monitor for further suspicious activity.",
|
||||
"Coordinate with law enforcement.",
|
||||
"Notify affected customers.",
|
||||
"Prepare incident report.",
|
||||
"Update security policies.",
|
||||
"Train employees on security best practices.",
|
||||
"Conduct post-incident review.",
|
||||
"Implement lessons learned.",
|
||||
"Improve incident response procedures.",
|
||||
"Enhance security awareness program.",
|
||||
})
|
||||
}
|
||||
|
||||
func random[T any](e []T) T {
|
||||
return e[gofakeit.IntN(len(e))]
|
||||
}
|
||||
Reference in New Issue
Block a user