@@ -8,6 +8,7 @@ package syslogprocessor
8
8
import (
9
9
"context"
10
10
"encoding/json"
11
+ "fmt"
11
12
"testing"
12
13
13
14
"github.com/stretchr/testify/assert"
@@ -31,7 +32,6 @@ func TestSyslogProcessor(t *testing.T) {
31
32
name : "csv nginx app protect syslog message" ,
32
33
body : `<130>Aug 22 03:28:35 ip-172-16-0-213 ASM:N/A,80,127.0.0.1,false,GET,nms_app_protect_default_policy,HTTP,blocked,0,N/A,N/A::N/A,{High Accuracy Signatures;Cross Site Scripting Signatures}::{High Accuracy Signatures; Cross Site Scripting Signatures},56064,N/A,5377540117854870581,N/A,5,1-localhost:1-/,N/A,REJECTED,SECURITY_WAF_VIOLATION,Illegal meta character in URL::Attack signature detected::Violation Rating Threat detected::Bot Client Detected,<?xml version='1.0' encoding='UTF-8'?><BAD_MSG><violation_masks><block>414000000200c00-3a03030c30000072-8000000000000000-0</block><alarm>475f0ffcbbd0fea-befbf35cb000007e-f400000000000000-0</alarm><learn>0-0-0-0</learn><staging>0-0-0-0</staging></violation_masks><request-violations><violation><viol_index>42</viol_index><viol_name>VIOL_ATTACK_SIGNATURE</viol_name><context>url</context><sig_data><sig_id>200000099</sig_id><blocking_mask>3</blocking_mask><kw_data><buffer>Lzw+PHNjcmlwdD4=</buffer><offset>3</offset><length>7</length></kw_data></sig_data><sig_data><sig_id>200000093</sig_id><blocking_mask>3</blocking_mask><kw_data><buffer>Lzw+PHNjcmlwdD4=</buffer><offset>4</offset><length>7</length></kw_data></sig_data></violation><violation><viol_index>26</viol_index><viol_name>VIOL_URL_METACHAR</viol_name><uri>Lzw+PHNjcmlwdD4=</uri><metachar_index>60</metachar_index><wildcard_entity>*</wildcard_entity><staging>0</staging></violation><violation><viol_index>26</viol_index><viol_name>VIOL_URL_METACHAR</viol_name><uri>Lzw+PHNjcmlwdD4=</uri><metachar_index>62</metachar_index><wildcard_entity>*</wildcard_entity><staging>0</staging></violation><violation><viol_index>122</viol_index><viol_name>VIOL_BOT_CLIENT</viol_name></violation><violation><viol_index>93</viol_index><viol_name>VIOL_RATING_THREAT</viol_name></violation></request-violations></BAD_MSG>,curl,HTTP Library,N/A,N/A,Untrusted Bot,N/A,N/A,HTTP/1.1,/<><script>,GET /<><script> HTTP/1.1\\r\\nHost: localhost\\r\\nUser-Agent: curl/7.81.0\\r\\nAccept: */*\\r\\n\\r\\n` ,
33
34
expectAttrs : map [string ]string {
34
- "syslog.hostname" : "ip-172-16-0-213" ,
35
35
"syslog.appname" : "ASM" ,
36
36
"app_protect.policy_name" : "nms_app_protect_default_policy" ,
37
37
"app_protect.support_id" : "5377540117854870581" ,
@@ -44,8 +44,7 @@ func TestSyslogProcessor(t *testing.T) {
44
44
name : "simple valid syslog message" ,
45
45
body : "<34>Oct 11 22:14:15 mymachine su: 'su root' failed for lonvick on /dev/pts/8" ,
46
46
expectAttrs : map [string ]string {
47
- "syslog.hostname" : "mymachine" ,
48
- "syslog.appname" : "su" ,
47
+ "syslog.appname" : "su" ,
49
48
},
50
49
expectRecords : 1 ,
51
50
},
@@ -159,16 +158,14 @@ func TestSyslogProcessor(t *testing.T) {
159
158
160
159
func TestSyslogProcessorFailure (t * testing.T ) {
161
160
testCases := []struct {
162
- expectAttrs map [string ]string
163
- body any
164
161
name string
165
- expectJSON string
162
+ body any
166
163
expectRecords int
167
164
}{
168
165
{
169
166
name : "invalid syslog message" ,
170
167
body : "not a syslog line" ,
171
- expectRecords : 1 ,
168
+ expectRecords : 0 ,
172
169
},
173
170
}
174
171
@@ -177,31 +174,35 @@ func TestSyslogProcessorFailure(t *testing.T) {
177
174
ctx := context .Background ()
178
175
settings := processortest .NewNopSettings (processortest .NopType )
179
176
settings .Logger = zap .NewNop ()
180
-
181
177
logs := plog .NewLogs ()
182
- lr := logs .ResourceLogs ().AppendEmpty ().ScopeLogs ().AppendEmpty ().LogRecords ().AppendEmpty ()
178
+ logRecord := logs .ResourceLogs ().
179
+ AppendEmpty ().
180
+ ScopeLogs ().
181
+ AppendEmpty ().
182
+ LogRecords ().
183
+ AppendEmpty ()
184
+
183
185
switch v := tc .body .(type ) {
184
186
case string :
185
- lr .Body ().SetStr (v )
187
+ logRecord .Body ().SetStr (v )
186
188
case int :
187
- lr .Body ().SetInt (int64 (v ))
189
+ logRecord .Body ().SetInt (int64 (v ))
188
190
case []byte :
189
- lr .Body ().SetEmptyBytes ().FromRaw (v )
191
+ logRecord .Body ().SetEmptyBytes ().FromRaw (v )
190
192
}
191
193
194
+ // Create sink and processor.
192
195
sink := & consumertest.LogsSink {}
193
- p := newSyslogProcessor (sink , settings )
194
- require .NoError (t , p .Start (ctx , nil ))
196
+ processor := newSyslogProcessor (sink , settings )
195
197
196
- err := p .ConsumeLogs (ctx , logs )
198
+ require .NoError (t , processor .Start (ctx , nil ))
199
+ err := processor .ConsumeLogs (ctx , logs )
200
+ fmt .Println (err )
197
201
require .Error (t , err )
198
202
199
- if tc .expectRecords == 0 {
200
- assert .Equal (t , 0 , sink .LogRecordCount (), "no logs should be produced" )
201
- require .NoError (t , p .Shutdown (ctx ))
203
+ assert .Equal (t , tc .expectRecords , sink .LogRecordCount (), "unexpected number of logs produced" )
202
204
203
- return
204
- }
205
+ require .NoError (t , processor .Shutdown (ctx ))
205
206
})
206
207
}
207
208
}
0 commit comments