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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
use crossbeam_channel::{Receiver, Sender, TryRecvError};
use std::borrow::Cow;
use usiem::components::command::{
    CommandDefinition, CommandError, SiemCommandCall, SiemCommandResponse, SiemFunctionType,
};
use usiem::components::common::{SiemComponentCapabilities, SiemMessage, UserRole};
use usiem::components::SiemComponent;
use usiem::events::schema::FieldSchema;
use usiem::events::SiemLog;

pub mod proxy;

use proxy::{SqliteProxy, SqliteProxyOptions};

pub struct SqliteDatastore {
    /// 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>,
    local_chnl_log_snd: Sender<SiemLog>,
    local_chnl_log_rcv: Receiver<SiemLog>,
    storage: Option<Box<dyn usiem::components::common::SiemComponentStateStorage>>,
    schema: FieldSchema,
    id: u64,
    store_location: String,
    sqlite_options: SqliteProxyOptions,
    connections: SqliteProxy,
}

impl SiemComponent for SqliteDatastore {
    fn set_id(&mut self, id: u64) {
        self.id = id;
    }

    fn local_channel(&self) -> Sender<SiemMessage> {
        self.local_chnl_snd.clone()
    }

    fn set_log_channel(
        &mut self,
        sender: Sender<usiem::events::SiemLog>,
        receiver: Receiver<usiem::events::SiemLog>,
    ) {
        self.local_chnl_log_snd = sender;
        self.local_chnl_log_rcv = receiver;
    }

    fn set_kernel_sender(&mut self, sender: Sender<SiemMessage>) {
        self.kernel_sender = sender;
    }

    fn run(&mut self) {
        let mut log_size = 0;
        let mut last_commit = 0;
        loop {
            let now = chrono::Utc::now().timestamp_millis();
            match self.local_chnl_rcv.try_recv() {
                Ok(msg) => match msg {
                    SiemMessage::Command(comm_info, cmd) => match cmd {
                        SiemCommandCall::STOP_COMPONENT(_n) => {
                            let mut count = 0;
                            loop {
                                let exit = self.connections.close();
                                count += 1;
                                if exit {
                                    return;
                                }
                                if count > 5 {
                                    println!("Cannot save all logs!!");
                                    return;
                                }
                            }
                        }
                        SiemCommandCall::LOG_QUERY(query) => {
                            match &query.query_id {
                                Some(_) => {
                                    // Query over query or retrieve query info
                                    match self.connections.get_query_result(&query) {
                                        Ok((completed, log_list)) => {
                                            if completed {
                                                let _ =
                                                    self.kernel_sender.send(SiemMessage::Response(
                                                        comm_info,
                                                        SiemCommandResponse::LOG_QUERY(
                                                            query.clone(),
                                                            Ok(log_list),
                                                        ),
                                                    ));
                                            } else {
                                                let _ =
                                                    self.kernel_sender.send(SiemMessage::Response(
                                                        comm_info,
                                                        SiemCommandResponse::LOG_QUERY(
                                                            query.clone(),
                                                            Ok(vec![]),
                                                        ),
                                                    ));
                                            }
                                        }
                                        Err(err) => {
                                            let _ = self.kernel_sender.send(SiemMessage::Response(
                                                comm_info,
                                                SiemCommandResponse::LOG_QUERY(
                                                    query,
                                                    Err(CommandError::SyntaxError(Cow::Owned(
                                                        err.to_string(),
                                                    ))),
                                                ),
                                            ));
                                        }
                                    };
                                }
                                None => {
                                    match self.connections.start_query(&query) {
                                        Ok(query_res) => {
                                            let _ = self.kernel_sender.send(SiemMessage::Response(
                                                comm_info,
                                                SiemCommandResponse::LOG_QUERY(
                                                    query_res,
                                                    Ok(vec![]),
                                                ),
                                            ));
                                        }
                                        Err(err) => {
                                            let _ = self.kernel_sender.send(SiemMessage::Response(
                                                comm_info,
                                                SiemCommandResponse::LOG_QUERY(
                                                    query,
                                                    Err(CommandError::SyntaxError(Cow::Owned(
                                                        err.to_string(),
                                                    ))),
                                                ),
                                            ));
                                        }
                                    };
                                }
                            };
                        }
                        _ => {}
                    },
                    SiemMessage::Log(log) => {
                        log_size += 1;
                        self.connections.ingest_log(log);
                    }
                    _ => {}
                },
                Err(e) => match e {
                    TryRecvError::Empty => {}
                    TryRecvError::Disconnected => return,
                },
            }
            for _ in 0..100 {
                match self.local_chnl_log_rcv.try_recv() {
                    Ok(log) => {
                        log_size += 1;
                        self.connections.ingest_log(log);
                    }
                    Err(e) => match e {
                        TryRecvError::Empty => break,
                        TryRecvError::Disconnected => return,
                    },
                }
            }
            if now > last_commit + self.sqlite_options.commit_time
                || log_size > self.sqlite_options.commit_size
            {
                self.connections.commit();
                last_commit = now;
            }
            match self.connections.continue_queries() {
                Ok(_) => {}
                Err(e) => {
                    panic!("{:?}", e.to_string())
                }
            }
        }
    }

    fn set_storage(&mut self, conn: Box<dyn usiem::components::common::SiemComponentStateStorage>) {
        self.storage = Some(conn);
    }

    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 indexing 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 search_logs = CommandDefinition::new(
            SiemFunctionType::LOG_QUERY,
            Cow::Borrowed("Search for logs"),
            Cow::Borrowed("Search for logs"),
            UserRole::Analyst,
        );
        commands.push(search_logs);

        SiemComponentCapabilities::new(
            Cow::Borrowed("SQlite datastore"),
            Cow::Borrowed("Store logs using a sqlite"),
            Cow::Borrowed(""), // No HTML
            datasets,
            commands,
            vec![],
            vec![]
        )
    }

    fn duplicate(&self) -> Box<dyn SiemComponent> {
        let (local_chnl_snd, local_chnl_rcv) = crossbeam_channel::bounded(1000);
        let clone = SqliteDatastore {
            kernel_sender: self.kernel_sender.clone(),
            local_chnl_snd,
            local_chnl_rcv,
            local_chnl_log_rcv: self.local_chnl_log_rcv.clone(),
            local_chnl_log_snd: self.local_chnl_log_snd.clone(),
            store_location: self.store_location.clone(),
            id: self.id,
            schema: self.schema.clone(),
            storage: self.storage.clone(),
            sqlite_options: self.sqlite_options.clone(),
            connections: self.connections.clone(),
        };
        Box::from(clone)
    }

    fn set_datasets(&mut self, _datasets: Vec<usiem::components::dataset::SiemDataset>) {
        //
    }
}

impl SqliteDatastore {
    pub fn new(schema: FieldSchema, store_location: String, options: SqliteProxyOptions) -> Self {
        let (local_chnl_snd, local_chnl_rcv) = crossbeam_channel::bounded(1000);
        let (local_chnl_log_snd, local_chnl_log_rcv) = crossbeam_channel::bounded(1000);
        let (kernel_sender, _) = crossbeam_channel::bounded(1);
        let proxy = SqliteProxy::new(options.clone(), schema.clone(), store_location.clone());
        SqliteDatastore {
            kernel_sender,
            local_chnl_snd,
            local_chnl_rcv,
            local_chnl_log_rcv,
            local_chnl_log_snd,
            store_location: store_location,
            id: 0,
            schema: schema,
            storage: None,
            sqlite_options: options,
            connections: proxy,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use usiem::components::command::{QueryInfo, SiemCommandHeader};
    use usiem::events::auth::{AuthEvent, AuthLoginType, LoginOutcome, RemoteLogin};
    use usiem::events::{get_default_schema, SiemEvent};

    #[test]
    fn test_sqlite_log_ingestion() {
        let mut comp = SqliteDatastore::new(
            get_default_schema(),
            ".".to_string(),
            SqliteProxyOptions::default(),
        );
        let local_chan = comp.local_channel();
        let (local_chnl_log_snd, local_chnl_log_rcv) = crossbeam_channel::bounded(5000);
        comp.set_log_channel(local_chnl_log_snd.clone(), local_chnl_log_rcv.clone());

        std::thread::spawn(move || comp.run());

        for i in 1..12_000 {
            let mut log = SiemLog::new("This is a log example ..............111111111111111111111111222222222222222222222223333333333333333333333", chrono::Utc::now().timestamp_millis(), "localhost");
            log.set_category(Cow::Borrowed("Authentication"));
            log.set_product(Cow::Borrowed("MagicDevice001"));
            log.set_tenant(Cow::Borrowed("Default"));
            log.set_service(Cow::Borrowed("sshd"));
            log.set_vendor(Cow::Borrowed("MagicDevices"));
            log.set_event(SiemEvent::Auth(AuthEvent {
                hostname: Cow::Borrowed("hostname1"),
                outcome: LoginOutcome::FAIL,
                login_type: AuthLoginType::Remote(RemoteLogin {
                    domain: Cow::Borrowed("CNMS"),
                    source_address: Cow::Borrowed("10.10.10.10"),
                    user_name: Cow::Borrowed("cancamusa"),
                }),
            }));
            let _ = local_chnl_log_snd.send(log);
        }
        std::thread::sleep(std::time::Duration::from_secs(10));
        let _ = local_chan.send(SiemMessage::Command(
            SiemCommandHeader {
                user: String::from("None"),
                comp_id: 0,
                comm_id: 0,
            },
            SiemCommandCall::STOP_COMPONENT("Stop!!".to_string()),
        ));
    }

    #[test]
    fn test_sqlite_with_query() {
        let mut comp = SqliteDatastore::new(
            get_default_schema(),
            ".".to_string(),
            SqliteProxyOptions {
                commit_size: 10_000,
                commit_time: 5_000,
                mmap_size: 32_000_000,
            },
        );
        let local_chan = comp.local_channel();
        let (local_chnl_log_snd, local_chnl_log_rcv) = crossbeam_channel::bounded(50_000);
        let (kernel_snd, kernel_rcv) = crossbeam_channel::bounded(5000);
        comp.set_log_channel(local_chnl_log_snd.clone(), local_chnl_log_rcv.clone());
        comp.set_kernel_sender(kernel_snd);

        std::thread::spawn(move || comp.run());

        for i in 1..100_000 {
            let mut log = SiemLog::new("This is a log example ..............111111111111111111111111222222222222222222222223333333333333333333333", i, "localhost");
            log.set_category(Cow::Borrowed("Authentication"));
            log.set_product(Cow::Borrowed("MagicDevice001"));
            log.set_tenant(Cow::Borrowed("Default"));
            log.set_service(Cow::Borrowed("sshd"));
            log.set_vendor(Cow::Borrowed("MagicDevices"));
            log.set_event_created(0);
            log.set_event(SiemEvent::Auth(AuthEvent {
                hostname: Cow::Borrowed("hostname1"),
                outcome: LoginOutcome::FAIL,
                login_type: AuthLoginType::Remote(RemoteLogin {
                    domain: Cow::Borrowed("CNMS"),
                    source_address: Cow::Borrowed("10.10.10.10"),
                    user_name: Cow::Borrowed("cancamusa"),
                }),
            }));
            let _ = local_chnl_log_snd.send(log);
        }
        std::thread::sleep(std::time::Duration::from_secs(6));
        let ttl = chrono::Utc::now().timestamp_millis() + 100_000;
        let _ = local_chan.send(SiemMessage::Command(
            SiemCommandHeader {
                user: String::from("None"),
                comm_id: 1,
                comp_id: 1,
            },
            SiemCommandCall::LOG_QUERY(QueryInfo {
                user: String::from("None"),
                is_native: true,
                query_id: None,
                from: 0,
                to: 1000,
                limit: 10_000,
                offset: 0,
                ttl,
                query: "category='Authentication'".to_string(),
                fields: vec![],
            }),
        ));
        std::thread::sleep(std::time::Duration::from_secs(2));
        let query_id;
        match kernel_rcv.try_recv() {
            Ok(msg) => {
                match msg {
                    SiemMessage::Response(_, resp) => {
                        match resp {
                            // Todo: improve in the CORE Package...
                            SiemCommandResponse::LOG_QUERY(query, res) => {
                                println!("{:?}", query.query);
                                query_id = query.query_id.unwrap_or(String::new());
                                match res {
                                    Ok(logs) => {
                                        if logs.len() != 0 {
                                            panic!("Log size must be 0");
                                        }
                                    }
                                    Err(e) => {
                                        println!("{:?}", e);
                                        panic!("Not expected response");
                                    }
                                }
                            }
                            _ => {
                                panic!("Not expected response");
                            }
                        }
                    }
                    _ => {
                        panic!("Not expected response");
                    }
                }
            }
            Err(e) => {
                panic!("{:?}", e);
            }
        }
        std::thread::sleep(std::time::Duration::from_secs(2));
        let ttl = chrono::Utc::now().timestamp_millis() + 10_000;
        let _ = local_chan.send(SiemMessage::Command(
            SiemCommandHeader {
                user: String::from("None"),
                comm_id: 1,
                comp_id: 1,
            },
            SiemCommandCall::LOG_QUERY(QueryInfo {
                user: String::from("None"),
                is_native: true,
                query_id: Some(query_id),
                from: 0,
                to: 1000,
                limit: 100000,
                offset: 0,
                ttl,
                query: "category='Authentication'".to_string(),
                fields: vec![],
            }),
        ));
        std::thread::sleep(std::time::Duration::from_secs(4));
        match kernel_rcv.try_recv() {
            Ok(msg) => {
                match msg {
                    SiemMessage::Response(_, resp) => {
                        match resp {
                            // Todo: improve in the CORE Package...
                            SiemCommandResponse::LOG_QUERY(_query_info, res) => match res {
                                Ok(logs) => {
                                    if logs.len() == 0 {
                                        panic!("No logs returned");
                                    }
                                    println!("Returned logs: {}", logs.len());
                                    for log in logs {
                                        assert_eq!(log.get("message").unwrap().to_string(), "This is a log example ..............111111111111111111111111222222222222222222222223333333333333333333333");
                                    }
                                }
                                Err(e) => {
                                    println!("{:?}", e);
                                    panic!("Not expected response");
                                }
                            },
                            _ => {
                                panic!("Not expected response");
                            }
                        }
                    }
                    _ => {
                        panic!("Not expected response");
                    }
                }
            }
            Err(_e) => {
                panic!("No response!!!")
            }
        }
        std::thread::sleep(std::time::Duration::from_secs(2));
        let _ = local_chan.send(SiemMessage::Command(
            SiemCommandHeader {
                user: String::from("None"),
                comm_id: 0,
                comp_id: 0,
            },
            SiemCommandCall::STOP_COMPONENT("Stop!!".to_string()),
        ));
    }
}