Feature/filter record by eventid#94 (#95)
* add function to get event id from rootnode. * refactoring #76 * maybe fix bug. * before test * fix source files. * cargo fmt --all * add threadnum parameter
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
extern crate regex;
|
||||
|
||||
use std::vec;
|
||||
|
||||
use crate::detections::utils;
|
||||
|
||||
use regex::Regex;
|
||||
@@ -121,6 +123,40 @@ impl RuleNode {
|
||||
|
||||
return selection.unwrap().select(event_record);
|
||||
}
|
||||
|
||||
pub fn get_event_ids(&self) -> Vec<i64> {
|
||||
let selection = self
|
||||
.detection
|
||||
.as_ref()
|
||||
.and_then(|detection| detection.selection.as_ref());
|
||||
if selection.is_none() {
|
||||
return vec![];
|
||||
}
|
||||
|
||||
return selection
|
||||
.unwrap()
|
||||
.get_leaf_nodes()
|
||||
.iter()
|
||||
.filter(|node| {
|
||||
// alias.txtのevent_keyに一致するかどうか
|
||||
let key = utils::get_event_id_key();
|
||||
if node.get_key() == key {
|
||||
return true;
|
||||
}
|
||||
|
||||
// alias.txtのaliasに一致するかどうか
|
||||
let alias = utils::get_alias(&key);
|
||||
if alias.is_none() {
|
||||
return false;
|
||||
} else {
|
||||
return node.get_key() == alias.unwrap();
|
||||
}
|
||||
})
|
||||
.filter_map(|node| {
|
||||
return node.select_value.as_i64();
|
||||
})
|
||||
.collect();
|
||||
}
|
||||
}
|
||||
|
||||
// Ruleファイルのdetectionを表すノード
|
||||
@@ -142,6 +178,7 @@ impl DetectionNode {
|
||||
trait SelectionNode {
|
||||
fn select(&self, event_record: &Value) -> bool;
|
||||
fn init(&mut self) -> Result<(), Vec<String>>;
|
||||
fn get_leaf_nodes(&self) -> Vec<&LeafSelectionNode>;
|
||||
}
|
||||
|
||||
// detection - selection配下でAND条件を表すノード
|
||||
@@ -192,6 +229,22 @@ impl SelectionNode for AndSelectionNode {
|
||||
return Result::Err(err_msgs);
|
||||
}
|
||||
}
|
||||
|
||||
fn get_leaf_nodes(&self) -> Vec<&LeafSelectionNode> {
|
||||
let mut ret = vec![];
|
||||
|
||||
self.child_nodes
|
||||
.iter()
|
||||
.map(|child| {
|
||||
return child.get_leaf_nodes();
|
||||
})
|
||||
.flatten()
|
||||
.for_each(|descendant| {
|
||||
ret.push(descendant);
|
||||
});
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
// detection - selection配下でOr条件を表すノード
|
||||
@@ -242,6 +295,22 @@ impl SelectionNode for OrSelectionNode {
|
||||
return Result::Err(err_msgs);
|
||||
}
|
||||
}
|
||||
|
||||
fn get_leaf_nodes(&self) -> Vec<&LeafSelectionNode> {
|
||||
let mut ret = vec![];
|
||||
|
||||
self.child_nodes
|
||||
.iter()
|
||||
.map(|child| {
|
||||
return child.get_leaf_nodes();
|
||||
})
|
||||
.flatten()
|
||||
.for_each(|descendant| {
|
||||
ret.push(descendant);
|
||||
});
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
// detection - selection配下の末端ノード
|
||||
@@ -262,13 +331,21 @@ impl LeafSelectionNode {
|
||||
};
|
||||
}
|
||||
|
||||
fn get_key(&self) -> String {
|
||||
if self.key_list.is_empty() {
|
||||
return String::default();
|
||||
}
|
||||
|
||||
return self.key_list[0].to_string();
|
||||
}
|
||||
|
||||
// JSON形式のEventJSONから値を取得する関数 aliasも考慮されている。
|
||||
fn get_event_value<'a>(&self, event_value: &'a Value) -> Option<&'a Value> {
|
||||
if self.key_list.is_empty() {
|
||||
return Option::None;
|
||||
}
|
||||
|
||||
return utils::get_event_value(&self.key_list[0].to_string(), event_value);
|
||||
return utils::get_event_value(&self.get_key(), event_value);
|
||||
}
|
||||
|
||||
// LeafMatcherの一覧を取得する。
|
||||
@@ -375,6 +452,10 @@ impl SelectionNode for LeafSelectionNode {
|
||||
.unwrap()
|
||||
.init(&match_key_list, &self.select_value);
|
||||
}
|
||||
|
||||
fn get_leaf_nodes(&self) -> Vec<&LeafSelectionNode> {
|
||||
return vec![&self];
|
||||
}
|
||||
}
|
||||
|
||||
// 末端ノードがEventLogの値を比較するロジックを表す。
|
||||
|
||||
Reference in New Issue
Block a user