|
| 1 | +package parser |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/NextronSystems/jsonlog" |
| 8 | + "github.com/NextronSystems/jsonlog/thorlog/common" |
| 9 | + thorlogv1 "github.com/NextronSystems/jsonlog/thorlog/v1" |
| 10 | + thorlogv2 "github.com/NextronSystems/jsonlog/thorlog/v2" |
| 11 | + "github.com/NextronSystems/jsonlog/thorlog/v3" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +func mustTime(s string) time.Time { |
| 17 | + t, err := time.Parse(time.RFC3339, s) |
| 18 | + if err != nil { |
| 19 | + panic(err) |
| 20 | + } |
| 21 | + return t |
| 22 | +} |
| 23 | + |
| 24 | +func TestParseEvent(t *testing.T) { |
| 25 | + for _, testcase := range []struct { |
| 26 | + name string |
| 27 | + rawEvent string |
| 28 | + expected thorlog.Event |
| 29 | + }{ |
| 30 | + { |
| 31 | + "JsonV1Logline", |
| 32 | + `{"time":"2024-09-24T12:35:41Z","hostname":"host","level":"Info","module":"Startup","message":"Sigma Database: r2024-07-17-48-g5c4f599e3","scanid":"S-NtvqbRLWbf8","log_version":"v1.0.0"}`, |
| 33 | + &thorlogv1.Event{ |
| 34 | + LogEventMetadata: thorlogv1.Metadata{ |
| 35 | + Time: mustTime("2024-09-24T12:35:41Z"), |
| 36 | + Lvl: common.Info, |
| 37 | + Mod: "Startup", |
| 38 | + ScanID: "S-NtvqbRLWbf8", |
| 39 | + GenID: "", |
| 40 | + Source: "host", |
| 41 | + }, |
| 42 | + Data: thorlogv1.Fields{ |
| 43 | + {Key: "message", Value: "Sigma Database: r2024-07-17-48-g5c4f599e3"}, |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + "JsonV2", |
| 49 | + `{"time":"2024-09-24T12:37:04Z","hostname":"host","level":"Info","module":"Hosts","message":"Starting module","scanid":"S-kgKxYyJFQd0","log_version":"v2.0.0"}`, |
| 50 | + &thorlogv2.Event{ |
| 51 | + LogEventMetadata: thorlogv2.Metadata{ |
| 52 | + Time: mustTime("2024-09-24T12:37:04Z"), |
| 53 | + Lvl: common.Info, |
| 54 | + Mod: "Hosts", |
| 55 | + ScanID: "S-kgKxYyJFQd0", |
| 56 | + GenID: "", |
| 57 | + Source: "host", |
| 58 | + }, |
| 59 | + Data: thorlogv2.Fields{ |
| 60 | + {Key: "message", Value: "Starting module"}, |
| 61 | + }, |
| 62 | + EventVersion: "v2.0.0", |
| 63 | + }, |
| 64 | + }, |
| 65 | + { |
| 66 | + "JsonV2WithArray", |
| 67 | + `{"time":"2024-09-24T12:37:04Z","hostname":"host","level":"Info","module":"Hosts","message":"something","tags":["abc","def","ghi"],"scanid":"S-kgKxYyJFQd0","log_version":"v2.0.0"}`, |
| 68 | + &thorlogv2.Event{ |
| 69 | + LogEventMetadata: thorlogv2.Metadata{ |
| 70 | + Time: mustTime("2024-09-24T12:37:04Z"), |
| 71 | + Lvl: common.Info, |
| 72 | + Mod: "Hosts", |
| 73 | + ScanID: "S-kgKxYyJFQd0", |
| 74 | + GenID: "", |
| 75 | + Source: "host", |
| 76 | + }, |
| 77 | + Data: thorlogv2.Fields{ |
| 78 | + {Key: "message", Value: "something"}, |
| 79 | + {Key: "tags", Value: []any{"abc", "def", "ghi"}}, |
| 80 | + }, |
| 81 | + EventVersion: "v2.0.0", |
| 82 | + }, |
| 83 | + }, |
| 84 | + { |
| 85 | + "JsonV2LoglineWithNested", |
| 86 | + `{"time":"2024-09-24T12:37:04Z","hostname":"host","level":"Info","module":"Hosts","message":"something","reasons":[{"reason":"r1","rulename":"rn1"},{"reason":"r2","rulename":"rn2"}],"scanid":"S-kgKxYyJFQd0","log_version":"v2.0.0"}`, |
| 87 | + &thorlogv2.Event{ |
| 88 | + LogEventMetadata: thorlogv2.Metadata{ |
| 89 | + Time: mustTime("2024-09-24T12:37:04Z"), |
| 90 | + Lvl: common.Info, |
| 91 | + Mod: "Hosts", |
| 92 | + ScanID: "S-kgKxYyJFQd0", |
| 93 | + GenID: "", |
| 94 | + Source: "host", |
| 95 | + }, |
| 96 | + Data: thorlogv2.Fields{ |
| 97 | + {Key: "message", Value: "something"}, |
| 98 | + {Key: "reasons", Value: []any{ |
| 99 | + thorlogv2.Fields{ |
| 100 | + {Key: "reason", Value: "r1"}, |
| 101 | + {Key: "rulename", Value: "rn1"}, |
| 102 | + }, |
| 103 | + thorlogv2.Fields{ |
| 104 | + {Key: "reason", Value: "r2"}, |
| 105 | + {Key: "rulename", Value: "rn2"}, |
| 106 | + }, |
| 107 | + }}, |
| 108 | + }, |
| 109 | + EventVersion: "v2.0.0", |
| 110 | + }, |
| 111 | + }, |
| 112 | + { |
| 113 | + "JsonV3Message", |
| 114 | + `{"message":"Starting module","type":"THOR message","meta":{"time":"2024-09-24T14:18:46.190394329+02:00","level":"Info","module":"Hosts","scan_id":"S-UBNfBD4xE8s","event_id":"","hostname":"host"},"fields":{},"log_version":"v3.0.0"}`, |
| 115 | + &thorlog.Message{ |
| 116 | + ObjectHeader: jsonlog.ObjectHeader{ |
| 117 | + Type: "THOR message", |
| 118 | + }, |
| 119 | + Meta: thorlog.LogEventMetadata{ |
| 120 | + Time: mustTime("2024-09-24T14:18:46.190394329+02:00"), |
| 121 | + Lvl: common.Info, |
| 122 | + Mod: "Hosts", |
| 123 | + ScanID: "S-UBNfBD4xE8s", |
| 124 | + GenID: "", |
| 125 | + Source: "host", |
| 126 | + }, |
| 127 | + Text: "Starting module", |
| 128 | + LogVersion: "v3.0.0", |
| 129 | + }, |
| 130 | + }, |
| 131 | + { |
| 132 | + "JsonV3Finding", |
| 133 | + `{"type":"THOR finding","meta":{"time":"2024-09-24T14:18:46.190394329+02:00","level":"Alert","module":"Test","scan_id":"abdc","event_id":"abdas","hostname":"aserarsd"},"message":"This is a test finding","subject":{"type":"file","path":"path/to/file"},"score":70,"reasons":[{"type":"reason","summary":"Reason 1","signature":{"score":70,"ref":null,"origin":"internal","kind":""},"matched":null}],"reason_count":0,"context":[{"object":{"type":"At Job"},"relation":"","unique":false}],"log_version":"v3"}`, |
| 134 | + &thorlog.Finding{ |
| 135 | + ObjectHeader: jsonlog.ObjectHeader{ |
| 136 | + Type: "THOR finding", |
| 137 | + }, |
| 138 | + Meta: thorlog.LogEventMetadata{ |
| 139 | + Time: mustTime("2024-09-24T14:18:46.190394329+02:00"), |
| 140 | + Lvl: common.Alert, |
| 141 | + Mod: "Test", |
| 142 | + ScanID: "abdc", |
| 143 | + GenID: "abdas", |
| 144 | + Source: "aserarsd", |
| 145 | + }, |
| 146 | + Text: "This is a test finding", |
| 147 | + Subject: &thorlog.File{ |
| 148 | + ObjectHeader: jsonlog.ObjectHeader{ |
| 149 | + Type: "file", |
| 150 | + }, |
| 151 | + Path: "path/to/file", |
| 152 | + }, |
| 153 | + Score: 70, |
| 154 | + Reasons: []thorlog.Reason{ |
| 155 | + { |
| 156 | + ObjectHeader: jsonlog.ObjectHeader{ |
| 157 | + Type: "reason", |
| 158 | + }, |
| 159 | + Summary: "Reason 1", |
| 160 | + Signature: thorlog.Signature{ |
| 161 | + Score: 70, |
| 162 | + Type: thorlog.Internal, |
| 163 | + }, |
| 164 | + }, |
| 165 | + }, |
| 166 | + ReasonCount: 0, |
| 167 | + EventContext: thorlog.Context{ |
| 168 | + { |
| 169 | + Object: &thorlog.AtJob{ |
| 170 | + ObjectHeader: jsonlog.ObjectHeader{ |
| 171 | + Type: "At Job", |
| 172 | + }, |
| 173 | + }, |
| 174 | + }, |
| 175 | + }, |
| 176 | + LogVersion: "v3.0.0", |
| 177 | + }, |
| 178 | + }, |
| 179 | + } { |
| 180 | + t.Run(testcase.name, func(t *testing.T) { |
| 181 | + event, err := ParseEvent([]byte(testcase.rawEvent)) |
| 182 | + require.NoError(t, err) |
| 183 | + assert.Equal(t, testcase.expected, event) |
| 184 | + }) |
| 185 | + } |
| 186 | +} |
0 commit comments