1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
use std::borrow::Cow;
use std::collections::{BTreeMap, BTreeSet};
use usiem::components::command::{
    CommandDefinition, SiemCommandCall, SiemCommandHeader, SiemCommandResponse, SiemFunctionType,
};
use usiem::components::command_types::ParserDefinition;
use usiem::components::dataset::holder::DatasetHolder;
use usiem::components::parsing::{LogParser, LogParsingError};
use usiem::components::common::{
    SiemComponentCapabilities, SiemComponentStateStorage, SiemMessage, UserRole,
};
use usiem::components::SiemComponent;
use usiem::crossbeam_channel::TryRecvError;
use usiem::crossbeam_channel::{Receiver, Sender};
use usiem::events::SiemLog;

#[derive(Clone)]
pub struct BasicParserComponent {
    /// Send actions to the kernel
    kernel_sender: Sender<SiemMessage>,
    /// Receive actions from other components or the kernel
    local_chnl_rcv: Receiver<SiemMessage>,
    /// Send actions to this components
    local_chnl_snd: Sender<SiemMessage>,
    /// Receive logs
    log_receiver: Receiver<SiemLog>,
    log_sender: Sender<SiemLog>,
    conn: Option<Box<dyn SiemComponentStateStorage>>,
    parsers: Vec<Box<dyn LogParser>>,
    datasets : DatasetHolder,
    id: u64,
}

impl BasicParserComponent {
    pub fn new() -> BasicParserComponent {
        let (kernel_sender, _receiver) = usiem::crossbeam_channel::bounded(1000);
        let (local_chnl_snd, local_chnl_rcv) = usiem::crossbeam_channel::unbounded();
        let (log_sender, log_receiver) = usiem::crossbeam_channel::unbounded();
        return BasicParserComponent {
            kernel_sender,
            local_chnl_rcv,
            local_chnl_snd,
            log_receiver,
            log_sender,
            parsers: Vec::new(),
            conn: None,
            datasets : DatasetHolder::from_datasets(vec![]),
            id: 0,
        };
    }
    pub fn add_parser(&mut self, parser: Box<dyn LogParser>) {
        self.parsers.push(parser);
    }

    fn list_parsers(&self, header: &SiemCommandHeader) -> SiemMessage {
        let content2 = self
            .parsers
            .iter()
            .map(|x| ParserDefinition {
                name: x.name().to_string(),
                description: x.description().to_string(),
            })
            .collect::<Vec<ParserDefinition>>();
        SiemMessage::Response(
            SiemCommandHeader {
                comm_id: header.comm_id,
                comp_id: header.comp_id,
                user: header.user.clone(),
            },
            SiemCommandResponse::LIST_PARSERS(Ok(content2)),
        )
    }

    fn parse_log<'a>(
        &'a self,
        origin_parser_map: &mut BTreeMap<String, Vec<&'a Box<dyn LogParser>>>,
        log: SiemLog,
    ) {
        let mut empty = vec![];
        let origin : String = log.origin().to_string();
        let selected_parsers = match origin_parser_map.get_mut(&origin) {
            Some(vc) => vc,
            None => &mut empty,
        };
        let mut tried_parsers = BTreeSet::new();
        let mut log = log;
        // Direct search
        for parser in &(*selected_parsers) {
            log = match parser.parse_log(log, &self.datasets) {
                Ok(lg) => {
                    let _ = self.log_sender.send(lg);
                    return;
                }
                Err(e) => match e {
                    LogParsingError::NoValidParser(lg) => lg,
                    LogParsingError::ParserError(lg, error) => {
                        let r = self
                            .kernel_sender
                            .send(SiemMessage::Notification(self.id, Cow::Owned(error)));
                        match r {
                            Err(_e) => {}
                            _ => {}
                        };
                        let _ = self.log_sender.send(lg);
                        return;
                    }
                    LogParsingError::NotImplemented(lg) => {
                        let _ = self.log_sender.send(lg);
                        return;
                    }
                    LogParsingError::FormatError(lg, _) => {
                        let _ = self.log_sender.send(lg);
                        return;
                    }
                },
            };
            tried_parsers.insert(parser.name());
        }
        // No direct parser found, try indirect
        for parser in &self.parsers {
            if !tried_parsers.contains(parser.name()) {
                log = match parser.parse_log(log, &self.datasets) {
                    Ok(lg) => {

                        if !origin_parser_map.contains_key(&origin) {
                            origin_parser_map.insert(origin.clone(), vec![&parser]);
                        }else{
                            let _ : Option<String> = origin_parser_map.get_mut(&origin).and_then(|x| {x.push(&parser); None});
                        }
                        let _ = self.log_sender.send(lg);
                        return;
                    }
                    Err(e) => match e {
                        LogParsingError::NoValidParser(lg) => lg,
                        LogParsingError::ParserError(lg, error) => {
                            if !origin_parser_map.contains_key(&origin) {
                                origin_parser_map.insert(origin.clone(), vec![&parser]);
                            }else{
                                let _ : Option<String> = origin_parser_map.get_mut(&origin).and_then(|x| {x.push(&parser); None});
                            }
                            let r = self
                                .kernel_sender
                                .send(SiemMessage::Notification(self.id, Cow::Owned(error)));
                            match r {
                                Err(_e) => {}
                                _ => {}
                            };
                            let _ = self.log_sender.send(lg);
                            return;
                        }
                        LogParsingError::NotImplemented(lg) => {
                            if !origin_parser_map.contains_key(&origin) {
                                origin_parser_map.insert(origin.clone(), vec![&parser]);
                            }else{
                                let _ : Option<String> = origin_parser_map.get_mut(&origin).and_then(|x| {x.push(&parser); None});
                            }
                            let _ = self.log_sender.send(lg);
                            return;
                        }
                        LogParsingError::FormatError(lg, _error) => {
                            if !origin_parser_map.contains_key(&origin) {
                                origin_parser_map.insert(origin.clone(), vec![&parser]);
                            }else{
                                let _ : Option<String> = origin_parser_map.get_mut(&origin).and_then(|x| {x.push(&parser); None});
                            }
                            let _ = self.log_sender.send(lg);
                            return;
                        }
                    },
                };
            }
        }
        let _ = self.log_sender.send(log);
    }
}

impl SiemComponent for BasicParserComponent {
    fn id(&self) -> u64 {
        return self.id;
    }
    fn set_id(&mut self, id: u64) {
        self.id = id;
    }
    fn name(&self) -> &str {
        "BasicParserExecutor"
    }
    fn local_channel(&self) -> Sender<SiemMessage> {
        self.local_chnl_snd.clone()
    }
    fn set_log_channel(&mut self, log_sender: Sender<SiemLog>, receiver: Receiver<SiemLog>) {
        self.log_receiver = receiver;
        self.log_sender = log_sender;
    }
    fn set_kernel_sender(&mut self, sender: Sender<SiemMessage>) {
        self.kernel_sender = sender;
    }
    fn duplicate(&self) -> Box<dyn SiemComponent> {
        return Box::new(self.clone());
    }
    fn set_datasets(&mut self, datasets: DatasetHolder) {
        self.datasets = datasets;
    }

    /// Execute the logic of this component in an infinite loop. Must be stopped using Commands sent using the channel.
    fn run(&mut self) {
        let kernel_channel = self.kernel_sender.clone();
        let local_chnl_rcv = self.local_chnl_rcv.clone();
        let mut origin_parser_map: BTreeMap<String, Vec<&Box<dyn LogParser>>> = BTreeMap::new();
        loop {
            let rcv_action = local_chnl_rcv.try_recv();
            match rcv_action {
                Ok(msg) => match msg {
                    SiemMessage::Command(hdr, cmd) => match cmd {
                        SiemCommandCall::STOP_COMPONENT(_name) => return,
                        SiemCommandCall::LIST_PARSERS(_pagination) => {
                            let _ = kernel_channel.send(self.list_parsers(&hdr));
                        }
                        _ => {}
                    },
                    SiemMessage::Log(msg) => {
                        self.parse_log(&mut origin_parser_map, msg);
                    }
                    _ => {}
                },
                Err(e) => match e {
                    TryRecvError::Empty => {
                        std::thread::sleep(std::time::Duration::from_millis(10));
                    }
                    TryRecvError::Disconnected => return,
                },
            }

            let rcv_log = (&self.log_receiver).try_recv();
            match rcv_log {
                Ok(log) => {
                    self.parse_log(&mut origin_parser_map, log);
                }
                Err(e) => match e {
                    TryRecvError::Empty => {
                        continue;
                    }
                    TryRecvError::Disconnected => return,
                },
            }
        }
    }

    /// Allow to store information about this component like the state or conigurations.
    fn set_storage(&mut self, conn: Box<dyn SiemComponentStateStorage>) {
        self.conn = Some(conn);
    }

    /// Capabilities and actions that can be performed on this component
    fn capabilities(&self) -> SiemComponentCapabilities {
        let datasets = Vec::new();
        let mut commands = Vec::new();

        let stop_component = CommandDefinition::new(SiemFunctionType::STOP_COMPONENT,Cow::Borrowed("Stop BasicParser") ,Cow::Borrowed("This allows stopping all Basic Parser components.\nUse only when really needed, like when there is a bug in the parsing process.") , UserRole::Administrator);
        commands.push(stop_component);
        let start_component = CommandDefinition::new(
            SiemFunctionType::START_COMPONENT, // Must be added by default by the KERNEL and only used by him
            Cow::Borrowed("Start Basic Parser"),
            Cow::Borrowed("This allows processing logs."),
            UserRole::Administrator,
        );
        commands.push(start_component);

        let list_parsers = CommandDefinition::new(
            SiemFunctionType::OTHER(
                Cow::Borrowed("LIST_PARSERS"),
                std::collections::BTreeMap::new(),
            ), // Must be added by default by the KERNEL and only used by him
            Cow::Borrowed("List log parsers"),
            Cow::Borrowed("List all parsers in this component."),
            UserRole::Administrator,
        );
        commands.push(list_parsers);

        SiemComponentCapabilities::new(
            Cow::Borrowed("BasicParser"),
            Cow::Borrowed("Parse logs using multiple diferent parsers"),
            Cow::Borrowed(""), // No HTML
            datasets,
            commands,
            vec![],
            vec![],
        )
    }
}

#[cfg(test)]
mod parser_test {
    use super::BasicParserComponent;
    use usiem::components::command::{SiemCommandHeader, SiemCommandCall, SiemCommandResponse, Pagination};
    use usiem::components::common::{ SiemMessage};
    use usiem::components::dataset::holder::DatasetHolder;
    use usiem::components::parsing::{LogGenerator, LogParser, LogParsingError};
    use usiem::components::SiemComponent;
    use usiem::events::field::{SiemField};
    use usiem::events::schema::FieldSchema;
    use usiem::events::SiemLog;

    use lazy_static::lazy_static;

    lazy_static! {
        static ref BASIC_SCHEMA: FieldSchema = FieldSchema::new();
    }

    struct DummyLogGenerator {}

    impl LogGenerator for DummyLogGenerator {
        fn log(&self) -> String {
            "This is a dummy log".to_string()
        }

        fn weight(&self) -> u8 {
            1
        }
    }

    #[derive(Clone)]
    struct DummyParserTextDUMMY {}

    impl LogParser for DummyParserTextDUMMY {
        fn parse_log(&self, mut log: SiemLog, _datasets : &DatasetHolder) -> Result<SiemLog, LogParsingError> {
            if !log.message().contains("DUMMY") {
                return Err(LogParsingError::NoValidParser(log))
            }
            log.add_field("parser", SiemField::from_str("DummyParserTextDUMMY"));
            Ok(log)
        }
        fn name(&self) -> &str {
            "DummyParserTextDUMMY"
        }
        fn description(&self) -> &str {
            "This is a dummy that parsers if contains DUMMY in text"
        }
        fn schema(&self) -> &'static FieldSchema {
            return &BASIC_SCHEMA;
        }

        fn generator(&self) -> Box<dyn LogGenerator> {
            return Box::new(DummyLogGenerator {});
        }
    }
    #[derive(Clone)]
    struct DummyParserALL {}

    impl LogParser for DummyParserALL {
        fn parse_log(&self, mut log: SiemLog, _datasets : &DatasetHolder) -> Result<SiemLog, LogParsingError> {
            log.add_field("parser", SiemField::from_str("DummyParserALL"));
            Ok(log)
        }
        fn name(&self) -> &str {
            "DummyParserALL"
        }
        fn description(&self) -> &str {
            "This is a dummy parser that always parses logs"
        }
        fn schema(&self) -> &'static FieldSchema {
            return &BASIC_SCHEMA;
        }

        fn generator(&self) -> Box<dyn LogGenerator> {
            return Box::new(DummyLogGenerator {});
        }
    }

    #[test]
    fn test_parser() {
        let (log_to_component, log_receiver) = usiem::crossbeam_channel::unbounded();
        let (log_sender, next_log_receiver) = usiem::crossbeam_channel::unbounded();

        let parser1 = DummyParserTextDUMMY {};
        let parser2 = DummyParserALL {};

        let mut parser = BasicParserComponent::new();
        let component_channel = parser.local_channel();
        parser.add_parser(Box::from(parser1));
        parser.add_parser(Box::from(parser2));
        parser.set_log_channel(log_sender, log_receiver);

        let log1 = SiemLog::new(
            "This is a DUMY log for DummyParserTextDUMMY",
            0,
            "localhost1",
        );
        let log2 = SiemLog::new(
            "This is NOT a DUmmY log for DummyParserTextDUmmY",
            0,
            "localhost2",
        );
        let _r = log_to_component.send(log1);
        let _r = log_to_component.send(log2);

        std::thread::spawn(move || {
            std::thread::sleep(std::time::Duration::from_millis(200));
            // STOP parser component to finish testing
            let _r = component_channel.send(SiemMessage::Command(
                SiemCommandHeader {comm_id : 0, comp_id : 0, user : "Superuser".to_string()},
                SiemCommandCall::STOP_COMPONENT("BasicParserComponent".to_string()),
            ));
        });
        parser.run();

        if let Ok(log) =  next_log_receiver.recv() {
            assert_eq!(
                log.field("parser"),
                Some(&SiemField::from_str("DummyParserTextDUMMY"))
            );
        }else{
            panic!("Must be received")
        }
        if let Ok(log) =  next_log_receiver.recv() {
            assert_eq!(
                log.field("parser"),
                Some(&SiemField::from_str("DummyParserALL"))
            );
        }else{
            panic!("Must be received")
        }
    }

    #[test]
    fn test_list_parsers() {
        let (kernel_sender, kernel_receiver) = usiem::crossbeam_channel::unbounded();

        let parser1 = DummyParserTextDUMMY {};
        let parser2 = DummyParserALL {};

        let mut parser = BasicParserComponent::new();
        let component_channel = parser.local_channel();
        parser.add_parser(Box::from(parser1));
        parser.add_parser(Box::from(parser2));
        parser.set_kernel_sender(kernel_sender);

        std::thread::spawn(move || {
            let _r = component_channel.send(SiemMessage::Command(
                SiemCommandHeader {comm_id : 0, comp_id : 0, user : "Superuser".to_string()},
                SiemCommandCall::LIST_PARSERS(Pagination {
                    offset : 0,
                    limit : 1000
                })
            ));
            std::thread::sleep(std::time::Duration::from_millis(200));
            // STOP parser component to finish testing
            let _r = component_channel.send(SiemMessage::Command(
                SiemCommandHeader {comm_id : 0, comp_id : 0, user : "Superuser".to_string()},
                SiemCommandCall::STOP_COMPONENT("BasicParserComponent".to_string()),
            ));
        });
        parser.run();

        let response = kernel_receiver.recv();
        if let Ok(SiemMessage::Response(_,SiemCommandResponse::LIST_PARSERS(Ok(res)))) = response {
            assert_eq!(2, res.len());
            assert_eq!("DummyParserTextDUMMY", res.get(0).unwrap().name);
            assert_eq!("DummyParserALL", res.get(1).unwrap().name);
        }else{
            panic!("Must not be error")
        }
    }
}