Skip to content

Commit b6bb38e

Browse files
committed
refactor: simplify body creation in ElasticSearch function and remove unnecessary debug logs
1 parent 6706492 commit b6bb38e

2 files changed

Lines changed: 12 additions & 24 deletions

File tree

soc-ai/elastic/alerts.go

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,7 @@ type AlertCorrelation struct {
7474
}
7575

7676
func GetRelatedAlerts() ([]schema.Alert, error) {
77-
// Debug log
78-
utils.Logger.Info("Getting historical alerts from Elasticsearch")
79-
80-
result, err := ElasticSearch(configurations.ALERT_INDEX_PATTERN, "*", "*")
77+
result, err := ElasticSearch(configurations.ALERT_INDEX_PATTERN, "", "")
8178
if err != nil {
8279
return nil, fmt.Errorf("error getting historical alerts: %v", err)
8380
}
@@ -92,9 +89,6 @@ func GetRelatedAlerts() ([]schema.Alert, error) {
9289
}
9390

9491
func FindRelatedAlerts(currentAlert schema.Alert) (*AlertCorrelation, error) {
95-
// Debug log
96-
utils.Logger.Info("Finding related alerts for alert %s", currentAlert.ID)
97-
9892
correlation := &AlertCorrelation{
9993
CurrentAlert: currentAlert,
10094
RelatedAlerts: []schema.Alert{},
@@ -106,8 +100,6 @@ func FindRelatedAlerts(currentAlert schema.Alert) (*AlertCorrelation, error) {
106100
return nil, err
107101
}
108102

109-
utils.Logger.Info("Found %d historical alerts to analyze", len(historicalResponses))
110-
111103
var alertIDs []string
112104
for _, resp := range historicalResponses {
113105
alertIDs = append(alertIDs, resp.ID)
@@ -138,43 +130,36 @@ func FindRelatedAlerts(currentAlert schema.Alert) (*AlertCorrelation, error) {
138130
}
139131

140132
func isAlertRelated(current, historical schema.Alert) bool {
141-
utils.Logger.Info("Checking relation between alerts - Current: %s, Historical: %s", current.ID, historical.ID)
133+
if current.ID == historical.ID {
134+
return false
135+
}
142136

143137
if current.Destination.IP != "" && current.Destination.IP == historical.Destination.IP {
144-
utils.Logger.Info("Match found: Destination IP %s", current.Destination.IP)
145138
return true
146139
}
147140
if current.Destination.Port != 0 && current.Destination.Port == historical.Destination.Port {
148-
utils.Logger.Info("Match found: Destination Port %d", current.Destination.Port)
149141
return true
150142
}
151143
if current.Destination.Host != "" && current.Destination.Host == historical.Destination.Host {
152-
utils.Logger.Info("Match found: Destination Host %s", current.Destination.Host)
153144
return true
154145
}
155146
if current.Destination.User != "" && current.Destination.User == historical.Destination.User {
156-
utils.Logger.Info("Match found: Destination User %s", current.Destination.User)
157147
return true
158148
}
159149

160150
if current.Source.IP != "" && current.Source.IP == historical.Source.IP {
161-
utils.Logger.Info("Match found: Source IP %s", current.Source.IP)
162151
return true
163152
}
164153
if current.Source.Port != 0 && current.Source.Port == historical.Source.Port {
165-
utils.Logger.Info("Match found: Source Port %d", current.Source.Port)
166154
return true
167155
}
168156
if current.Source.Host != "" && current.Source.Host == historical.Source.Host {
169-
utils.Logger.Info("Match found: Source Host %s", current.Source.Host)
170157
return true
171158
}
172159
if current.Source.User != "" && current.Source.User == historical.Source.User {
173-
utils.Logger.Info("Match found: Source User %s", current.Source.User)
174160
return true
175161
}
176162

177-
utils.Logger.Info("No match found between alerts %s and %s", current.ID, historical.ID)
178163
return false
179164
}
180165

soc-ai/elastic/index.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,15 @@ func ElasticSearch(index, field, value string) ([]byte, error) {
4444
"Utm-Internal-Key": configurations.GetInternalKey(),
4545
}
4646

47-
body := schema.SearchDetailsRequest{{Field: field, Operator: "IS", Value: value}}
48-
bodyBytes, err := json.Marshal(body)
49-
if err != nil {
50-
return nil, fmt.Errorf("error marshalling body: %v", err)
47+
var bodyBytes []byte
48+
var err error
49+
if field != "" && value != "" {
50+
body := schema.SearchDetailsRequest{{Field: field, Operator: "IS", Value: value}}
51+
bodyBytes, err = json.Marshal(body)
52+
if err != nil {
53+
return nil, fmt.Errorf("error marshalling body: %v", err)
54+
}
5155
}
52-
5356
resp, statusCode, err := utils.DoReq(url, bodyBytes, "POST", headers, configurations.HTTP_TIMEOUT)
5457
if err != nil || statusCode != http.StatusOK {
5558
return nil, fmt.Errorf("error while doing request for get Alert Details: %v: %s", err, string(resp))

0 commit comments

Comments
 (0)