Skip to content

Commit

Permalink
Merge pull request #620 from Security-Onion-Solutions/cogburn/sync-si…
Browse files Browse the repository at this point in the history
…gma-custom

Sync Local Rules
  • Loading branch information
coreyogburn authored Aug 26, 2024
2 parents cc10f21 + 2308b47 commit b04f49d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
49 changes: 48 additions & 1 deletion server/modules/elastalert/elastalert.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ func (e *ElastAlertEngine) Sync(logger *log.Entry, forceSync bool) error {
var errMap map[string]error

// If the system is Airgap, load the sigma packages from disk.
// else, not Airgap, downoad the sigma packages.
// else, not Airgap, download the sigma packages.
if e.airgapEnabled {
zips, errMap = e.loadSigmaPackagesFromDisk()
} else {
Expand Down Expand Up @@ -675,6 +675,53 @@ func (e *ElastAlertEngine) Sync(logger *log.Entry, forceSync bool) error {
return detections.ErrSyncFailed
}

localrules, err := e.srv.Detectionstore.GetAllDetections(e.srv.Context, model.WithEngine(model.EngineNameElastAlert), model.WithCommunity(false))
if err != nil {
if errors.Is(err, detections.ErrModuleStopped) {
return err
}

logger.WithError(err).Error("unable to get local detections")

if e.notify {
e.srv.Host.Broadcast("detection-sync", "detections", server.SyncStatus{
Engine: model.EngineNameElastAlert,
Status: "error",
})
}

return detections.ErrSyncFailed
}

if len(localrules) > 0 {
local := make([]*model.Detection, 0, len(localrules))
for _, det := range localrules {
local = append(local, det)
}

errMapLocal, err := e.SyncLocalDetections(e.srv.Context, local)
if err != nil {
if errors.Is(err, detections.ErrModuleStopped) {
return err
}

logger.WithError(err).Error("unable to sync local detections")

if e.notify {
e.srv.Host.Broadcast("detection-sync", "detections", server.SyncStatus{
Engine: model.EngineNameElastAlert,
Status: "error",
})
}

return detections.ErrSyncFailed
}

for publicID, err := range errMapLocal {
errMap[publicID] = errors.New(err)
}
}

detections.WriteStateFile(e.IOManager, e.StateFilePath)

if len(errMap) > 0 {
Expand Down
12 changes: 12 additions & 0 deletions server/modules/elastalert/elastalert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,18 @@ func TestSyncChanges(t *testing.T) {
}).Times(3)
auditm.EXPECT().Close(gomock.Any()).Return(nil)
auditm.EXPECT().Stats().Return(esutil.BulkIndexerStats{})
// SyncLocalDetections
detStore.EXPECT().GetAllDetections(gomock.Any(), gomock.Any()).Return(map[string]*model.Detection{
SimpleRule2SID: {
PublicID: SimpleRule2SID,
},
}, nil)
iom.EXPECT().ReadDir("elastAlertRulesFolder").Return([]fs.DirEntry{
&handmock.MockDirEntry{
Filename: SimpleRule2SID + ".yml",
},
}, nil) // IndexExistingRules
iom.EXPECT().DeleteFile("elastAlertRulesFolder/" + SimpleRule2SID + ".yml").Return(nil)
iom.EXPECT().WriteFile("stateFilePath", gomock.Any(), fs.FileMode(0644)).Return(nil) // WriteStateFile
iom.EXPECT().WriteFile("rulesFingerprintFile", gomock.Any(), fs.FileMode(0644)).Return(nil) // WriteFingerprintFile
// regenNeeded
Expand Down

0 comments on commit b04f49d

Please sign in to comment.