Skip to content

Commit 14217a4

Browse files
committed
Update log writing with correct use of 'catcher' in the correlation engine.
1 parent 582609f commit 14217a4

File tree

16 files changed

+149
-112
lines changed

16 files changed

+149
-112
lines changed

correlation/api/newLogHandler.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package api
33
import (
44
"encoding/json"
55
"fmt"
6-
"github.com/utmstack/UTMStack/correlation/ti"
76
"io"
8-
"log"
97
"net/http"
108
"time"
119

10+
"github.com/threatwinds/go-sdk/catcher"
11+
"github.com/utmstack/UTMStack/correlation/ti"
12+
1213
"github.com/gin-gonic/gin"
1314
"github.com/google/uuid"
1415
"github.com/tidwall/gjson"
@@ -29,7 +30,7 @@ func NewLog(c *gin.Context) {
2930
if err != nil {
3031
response["status"] = "error"
3132
response["error"] = fmt.Sprintf("%v", err)
32-
log.Println(response["error"])
33+
catcher.Error("Failed to read request body", err, map[string]any{"status": http.StatusBadRequest})
3334
c.JSON(http.StatusBadRequest, response)
3435
return
3536
}
@@ -38,7 +39,7 @@ func NewLog(c *gin.Context) {
3839
if err := json.Unmarshal(body, &lo); err != nil {
3940
response["status"] = "error"
4041
response["error"] = fmt.Sprintf("%v", err)
41-
log.Println(response["error"])
42+
catcher.Error("Failed to read request body", err, map[string]any{"status": http.StatusBadRequest})
4243
c.JSON(http.StatusBadRequest, response)
4344
return
4445
}
@@ -69,7 +70,10 @@ func NewLog(c *gin.Context) {
6970
!gjson.Get(l, "dataSource").Exists() {
7071
response["status"] = "error"
7172
response["error"] = "The log doesn't have the required fields. Please be sure that you are sending the @timestamp in RFC3339Nano format, the dataType that could be windows, linux, iis, macos, ... and the dataSource that could be the Hostname or IP of the log source."
72-
log.Printf("%s LOG: %s", response["error"], l)
73+
catcher.Error("Log validation failed - missing required fields", nil, map[string]any{
74+
"status": http.StatusBadRequest,
75+
"log_sample": l,
76+
})
7377
c.JSON(http.StatusBadRequest, response)
7478
return
7579
}

correlation/cache/cache.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package cache
22

33
import (
4-
"log"
54
"runtime"
65
"sync"
76
"time"
87

8+
"github.com/threatwinds/go-sdk/catcher"
99
"github.com/tidwall/gjson"
1010
"github.com/utmstack/UTMStack/correlation/rules"
1111
"github.com/utmstack/UTMStack/correlation/utils"
@@ -19,10 +19,10 @@ var storage []string
1919

2020
func Status() {
2121
for {
22-
log.Printf("Logs in cache: %v", len(storage))
22+
catcher.Info("Logs in cache", map[string]any{"count": len(storage)})
2323
if len(storage) != 0 {
2424
est := gjson.Get(storage[0], "@timestamp").String()
25-
log.Printf("Old document in cache: %s", est)
25+
catcher.Info("Old document in cache", map[string]any{"timestamp": est})
2626
}
2727
time.Sleep(60 * time.Second)
2828
}
@@ -47,7 +47,7 @@ func Search(allOf []rules.AllOf, oneOf []rules.OneOf, seconds int64) []string {
4747
est := gjson.Get(storage[i], "@timestamp").String()
4848
eit, err := time.Parse(time.RFC3339Nano, est)
4949
if err != nil {
50-
log.Printf("Could not parse @timestamp: %v", err)
50+
catcher.Error("Could not parse @timestamp:", err, nil)
5151
continue
5252
}
5353
if eit.Unix() < ait {
@@ -85,7 +85,7 @@ var logs = make(chan string, bufferSize)
8585

8686
func AddToCache(l string) {
8787
if len(logs) == bufferSize {
88-
log.Printf("Buffer is full, you could be lossing events")
88+
catcher.Info("Buffer is full, you could be lossing events", nil)
8989
return
9090
}
9191
logs <- l
@@ -116,7 +116,7 @@ func Clean() {
116116
old := gjson.Get(storage[0], "@timestamp").String()
117117
oldTime, err := time.Parse(time.RFC3339Nano, old)
118118
if err != nil {
119-
log.Printf("Could not parse old log timestamp. Cleaning up")
119+
catcher.Error("Could not parse old log timestamp. Cleaning up", err, nil)
120120
clean = true
121121
}
122122

correlation/correlation/finder.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"bytes"
55
"fmt"
66
"html/template"
7-
"log"
87
"time"
98

9+
"github.com/threatwinds/go-sdk/catcher"
1010
"github.com/utmstack/UTMStack/correlation/cache"
1111
"github.com/utmstack/UTMStack/correlation/rules"
1212
"github.com/utmstack/UTMStack/correlation/search"
@@ -15,13 +15,13 @@ import (
1515

1616
func Finder(rule rules.Rule) {
1717
if len(rule.DataTypes) == 0 {
18-
log.Printf("Disabling rule '%s', because dataTypes is empty", rule.Name)
18+
catcher.Info("Disabling rule, because dataTypes is empty", map[string]any{"name": rule.Name})
1919
return
2020
}
2121

2222
sleep, err := time.ParseDuration(fmt.Sprintf("%ds", rule.Frequency))
2323
if err != nil {
24-
log.Printf("Disabling rule '%s', because of error: '%v", rule.Name, err)
24+
catcher.Error("Disabling rule", err, map[string]any{"name": rule.Name})
2525
return
2626
}
2727

@@ -52,15 +52,15 @@ func Finder(rule rules.Rule) {
5252
continue
5353
}
5454

55-
log.Printf("Executing rule: %s", rule.Name)
55+
catcher.Info("Executing rule", map[string]any{"name": rule.Name})
5656

5757
if len(rule.Cache) != 0 {
5858
findInCache(rule)
5959
} else if len(rule.Search) != 0 {
6060
findInSearch(rule)
6161
}
6262

63-
log.Printf("Execution of rule '%s' finished", rule.Name)
63+
catcher.Info("Execution of rule finished", map[string]any{"name": rule.Name})
6464

6565
switch sleep {
6666
case 0:
@@ -84,7 +84,7 @@ func findInSearch(rule rules.Rule) {
8484
t := template.Must(template.New("query").Parse(query.Query))
8585
err := t.Execute(&q, fields)
8686
if err != nil {
87-
log.Printf("Error while trying to process the query %v of the rule %s: %v", step+1, rule.Name, err)
87+
catcher.Error("Error while trying to process the query", err, map[string]any{"step": step + 1, "rule": rule.Name})
8888
} else {
8989
l := search.Search(q.String())
9090
processResponse(l, rule, query.Save, &tmpLogs, len(rule.Search), step, query.MinCount)
@@ -108,7 +108,7 @@ func findInCache(rule rules.Rule) {
108108
t := template.Must(template.New("allOf").Parse(allOf.Value))
109109
err := t.Execute(&value, fields)
110110
if err != nil {
111-
log.Printf("Error while trying to process the query %v of the rule %s: %v", step+1, rule.Name, err)
111+
catcher.Error("Error while trying to process the query", err, map[string]any{"step": step + 1, "rule": rule.Name})
112112
} else {
113113
allOfList = append(allOfList, rules.AllOf{Field: allOf.Field, Operator: allOf.Operator, Value: value.String()})
114114
}
@@ -120,7 +120,7 @@ func findInCache(rule rules.Rule) {
120120
t := template.Must(template.New("oneOf").Parse(oneOf.Value))
121121
err := t.Execute(&value, fields)
122122
if err != nil {
123-
log.Printf("Error while trying to process the query %v of the rule %s: %v", step+1, rule.Name, err)
123+
catcher.Error("Error while trying to process the query", err, map[string]any{"step": step + 1, "rule": rule.Name})
124124
} else {
125125
oneOfList = append(oneOfList, rules.OneOf{Field: oneOf.Field, Operator: oneOf.Operator, Value: value.String()})
126126
}

correlation/correlation/reporter.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ package correlation
22

33
import (
44
"encoding/json"
5+
"strconv"
6+
"strings"
7+
"time"
8+
59
"github.com/google/uuid"
610
"github.com/levigross/grequests"
11+
"github.com/threatwinds/go-sdk/catcher"
712
"github.com/utmstack/UTMStack/correlation/geo"
813
"github.com/utmstack/UTMStack/correlation/search"
914
"github.com/utmstack/UTMStack/correlation/utils"
10-
"log"
11-
"strconv"
12-
"strings"
13-
"time"
1415
)
1516

1617
type Host struct {
@@ -100,7 +101,7 @@ func Alert(name, severity, description, solution, category, tactic string, refer
100101
}
101102
}
102103

103-
log.Printf("Reporting alert: %s", name)
104+
catcher.Info("Reporting alert", map[string]any{"name": name})
104105

105106
if !UpdateAlert(name, severity, fields) {
106107
NewAlert(name, severity, description, solution, category, tactic, reference, dataType, dataSource,
@@ -113,7 +114,7 @@ func UpdateAlert(name, severity string, details map[string]string) bool {
113114

114115
index, err := search.IndexBuilder("alert", time.Now().UTC().Format(time.RFC3339Nano))
115116
if err != nil {
116-
log.Printf("Could not build index name: %v", err)
117+
catcher.Error("Could not build index name", err, nil)
117118
return true
118119
}
119120

@@ -208,7 +209,7 @@ func UpdateAlert(name, severity string, details map[string]string) bool {
208209
JSON: request,
209210
})
210211
if err != nil {
211-
log.Printf("Could not check existent alert: %v", err)
212+
catcher.Error("Could not check existent alert", err, nil)
212213
return false
213214
}
214215

@@ -221,7 +222,7 @@ func UpdateAlert(name, severity string, details map[string]string) bool {
221222
err = json.Unmarshal([]byte(resultStr), &resultObj)
222223

223224
if err != nil {
224-
log.Printf("Could not check existent alert: %v", err)
225+
catcher.Error("Could not check existent alert", err, nil)
225226
return false
226227
}
227228

@@ -242,7 +243,7 @@ func UpdateAlert(name, severity string, details map[string]string) bool {
242243
},
243244
})
244245
if err != nil {
245-
log.Printf("Could not update existent alert: %v", err)
246+
catcher.Error("Could not update existent alert", err, nil)
246247
return false
247248
}
248249

@@ -362,13 +363,13 @@ func NewAlert(name, severity, description, solution, category, tactic string, re
362363
url := cnf.Elasticsearch + "/" + index + "/_doc"
363364
_, err := utils.DoPost(url, "application/json", body)
364365
if err != nil {
365-
log.Printf("Could not send alert to Elasticsearch: %v", err)
366+
catcher.Error("Could not send alert to Elasticsearch", err, nil)
366367
}
367368
} else {
368-
log.Printf("Could not build index name: %v", err)
369+
catcher.Error("Could not build index name", err, nil)
369370
}
370371
} else {
371-
log.Printf("Could not encode alert in JSON: %v", err)
372+
catcher.Error("Could not encode alert in JSON", err, nil)
372373
}
373374
time.Sleep(3 * time.Second)
374375
}

correlation/geo/bases.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package geo
22

33
import (
4-
"github.com/utmstack/UTMStack/correlation/utils"
5-
"log"
64
"net"
75
"path/filepath"
86
"strconv"
7+
8+
"github.com/threatwinds/go-sdk/catcher"
9+
"github.com/utmstack/UTMStack/correlation/utils"
910
)
1011

1112
func Load() {
12-
log.Printf("Loading GeoIP databases")
13+
catcher.Info("Loading GeoIP databases", nil)
1314

1415
var files = []string{
1516
"asn-blocks-v4.csv",
@@ -38,10 +39,10 @@ func Load() {
3839
}
3940
}
4041

41-
log.Printf("asnBlocks rows: %v", len(asnBlocks))
42-
log.Printf("cityBlocks rows: %v", len(cityBlocks))
43-
log.Printf("cityLocations rows: %v", len(cityLocations))
44-
log.Printf("GeoIP databases loaded")
42+
catcher.Info("asnBlocks rows", map[string]any{"count": len(asnBlocks)})
43+
catcher.Info("cityBlocks rows", map[string]any{"count": len(cityBlocks)})
44+
catcher.Info("cityLocations rows", map[string]any{"count": len(cityLocations)})
45+
catcher.Info("GeoIP databases loaded", map[string]any{})
4546
}
4647

4748
func populateASNBlocks(csv [][]string) {
@@ -51,13 +52,13 @@ func populateASNBlocks(csv [][]string) {
5152
}
5253
_, n, err := net.ParseCIDR(line[0])
5354
if err != nil {
54-
log.Printf("Could not get CIDR in populateASNBlocks: %v", err)
55+
catcher.Error("Could not get CIDR in populateASNBlocks", err, nil)
5556
continue
5657
}
5758

5859
asn, err := strconv.Atoi(line[1])
5960
if err != nil {
60-
log.Printf("Could not get ASN in populateASNBlocks: %v", err)
61+
catcher.Error("Could not get ASN in populateASNBlocks", err, nil)
6162
continue
6263
}
6364

@@ -78,7 +79,7 @@ func populateCityBlocks(csv [][]string) {
7879
}
7980
_, n, err := net.ParseCIDR(line[0])
8081
if err != nil {
81-
log.Printf("Could not parse CIDR in populateCityBlocks: %v", err)
82+
catcher.Error("Could not parse CIDR in populateCityBlocks", err, nil)
8283
continue
8384
}
8485

@@ -88,13 +89,13 @@ func populateCityBlocks(csv [][]string) {
8889

8990
geonameID, err := strconv.Atoi(line[1])
9091
if err != nil {
91-
log.Printf("Could not parse geonameID in populateCityBlocks: %v", err)
92+
catcher.Error("Could not parse geonameID in populateCityBlocks", err, nil)
9293
continue
9394
}
9495

9596
isAnonymousProxy, err := strconv.Atoi(line[4])
9697
if err != nil {
97-
log.Printf("Could not parse isAnonymousProxy in populateCityBlocks: %v", err)
98+
catcher.Error("Could not parse isAnonymousProxy in populateCityBlocks", err, nil)
9899
continue
99100
}
100101

@@ -105,7 +106,7 @@ func populateCityBlocks(csv [][]string) {
105106

106107
isSatelliteProvider, err := strconv.Atoi(line[5])
107108
if err != nil {
108-
log.Printf("Could not parse isSatelliteProvider in populateCityBlocks: %v", err)
109+
catcher.Error("Could not parse isSatelliteProvider in populateCityBlocks", err, nil)
109110
continue
110111
}
111112

@@ -116,19 +117,19 @@ func populateCityBlocks(csv [][]string) {
116117

117118
latitude, err := strconv.ParseFloat(line[7], 64)
118119
if err != nil {
119-
log.Printf("Could not parse latitude in populateCityBlocks: %v", err)
120+
catcher.Error("Could not parse latitude in populateCityBlocks", err, nil)
120121
continue
121122
}
122123

123124
longitude, err := strconv.ParseFloat(line[8], 64)
124125
if err != nil {
125-
log.Printf("Could not parse longitude in populateCityBlocks: %v", err)
126+
catcher.Error("Could not parse longitude in populateCityBlocks", err, nil)
126127
continue
127128
}
128129

129130
accuracyRadius, err := strconv.Atoi(line[9])
130131
if err != nil {
131-
log.Printf("Could not parse accuracyRadius in populateCityBlocks: %v", err)
132+
catcher.Error("Could not parse accuracyRadius in populateCityBlocks", err, nil)
132133
continue
133134
}
134135

@@ -153,13 +154,13 @@ func populateCityLocations(csv [][]string) {
153154
}
154155
geonameID, err := strconv.Atoi(line[0])
155156
if err != nil {
156-
log.Printf("Could not parse geonameID in populateCityLocations: %v", err)
157+
catcher.Error("Could not parse geonameID in populateCityLocations", err, nil)
157158
continue
158159
}
159160

160161
isInEuropeanUnion, err := strconv.Atoi(line[13])
161162
if err != nil {
162-
log.Printf("Could not parse isInEuropeanUnion in populateCityLocations: %v", err)
163+
catcher.Error("Could not parse isInEuropeanUnion in populateCityLocations", err, nil)
163164
continue
164165
}
165166

0 commit comments

Comments
 (0)