Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 112 additions & 12 deletions aw-query/src/datatype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,26 @@ impl TryFrom<&DataType> for Rule {
if rtype == "none" {
Ok(Self::None)
} else if rtype == "regex" {
let ignore_case_val = match obj.get("ignore_case") {
Some(case_val) => case_val,
None => &DataType::Bool(false),
};
let ignore_case = match ignore_case_val {
DataType::Bool(b) => b,
_ => {
return Err(QueryError::InvalidFunctionParameters(
"the ignore_case field of the regex rule is not a bool".to_string(),
))
}
};
// Option B for aw-webui#939: per-field patterns on the existing
// `regex` type instead of a new rule type. A `fields` rule carries
// no `regex` of its own, so a server that predates it either rejects
// the rule or (with an empty `regex`) matches nothing for it, rather
// than misclassifying events.
if obj.contains_key("fields") {
return parse_fields_rule(obj, *ignore_case);
}
let regex_val = match obj.get("regex") {
Some(regex_val) => regex_val,
None => {
Expand All @@ -443,18 +463,6 @@ impl TryFrom<&DataType> for Rule {
))
}
};
let ignore_case_val = match obj.get("ignore_case") {
Some(case_val) => case_val,
None => &DataType::Bool(false),
};
let ignore_case = match ignore_case_val {
DataType::Bool(b) => b,
_ => {
return Err(QueryError::InvalidFunctionParameters(
"the ignore_case field of the regex rule is not a bool".to_string(),
))
}
};
let select_keys = match obj.get("select_keys") {
Some(DataType::List(keys)) => {
let mut select_keys = Vec::with_capacity(keys.len());
Expand Down Expand Up @@ -492,3 +500,95 @@ impl TryFrom<&DataType> for Rule {
}
}
}

/// Parse a `regex` rule that carries a per-field `fields` map (Option B for
/// aw-webui#939), for example:
///
/// ```text
/// { "type": "regex", "fields": { "app": "mstsc", "title": "office" } }
/// ```
///
/// Every named field must be present, a string, and match its own pattern
/// (unanchored, like a plain `regex`). `regex` and `select_keys` are rejected
/// when set: silently ignoring them would turn a rule that older servers read
/// as a single-pattern rule into something else. An empty `regex`/`select_keys`
/// counts as unset, matching aw-core's Python `Rule` and keeping such a rule
/// fail-closed on servers that predate the field map.
fn parse_fields_rule(
obj: &HashMap<String, DataType>,
ignore_case: bool,
) -> Result<Rule, QueryError> {
match obj.get("regex") {
None => {}
Some(DataType::String(s)) if s.is_empty() => {}
Some(DataType::String(_)) => {
return Err(QueryError::InvalidFunctionParameters(
"a regex rule with 'fields' must not also carry a 'regex' pattern \
(an empty string counts as unset)"
.to_string(),
))
}
Some(_) => {
return Err(QueryError::InvalidFunctionParameters(
"the regex field of the regex rule is not a string".to_string(),
))
}
}
match obj.get("select_keys") {
None => {}
Some(DataType::List(keys)) if keys.is_empty() => {}
Some(DataType::List(_)) => {
return Err(QueryError::InvalidFunctionParameters(
"a regex rule with 'fields' must not also carry 'select_keys' \
(an empty list counts as unset)"
.to_string(),
))
}
Some(_) => {
return Err(QueryError::InvalidFunctionParameters(
"the select_keys field of the regex rule is not a list".to_string(),
))
}
}
let fields = match obj.get("fields") {
Some(DataType::Dict(fields)) => fields,
Some(_) => {
return Err(QueryError::InvalidFunctionParameters(
"the fields of the regex rule is not a dict".to_string(),
))
}
None => {
return Err(QueryError::InvalidFunctionParameters(
"a regex rule with 'fields' is missing the 'fields' map".to_string(),
))
}
};
if fields.is_empty() {
return Err(QueryError::InvalidFunctionParameters(
"the fields of the regex rule must not be empty".to_string(),
));
}
let mut patterns = HashMap::with_capacity(fields.len());
for (field, pattern) in fields {
let pattern = match pattern {
DataType::String(pattern) if !pattern.is_empty() => pattern.clone(),
DataType::String(_) => {
return Err(QueryError::InvalidFunctionParameters(format!(
"the pattern for field '{field}' in the regex rule is empty"
)))
}
_ => {
return Err(QueryError::InvalidFunctionParameters(format!(
"the pattern for field '{field}' in the regex rule is not a string"
)))
}
};
patterns.insert(field.clone(), pattern);
}
match RegexRule::from_fields(patterns, ignore_case) {
Ok(rule) => Ok(Rule::Regex(rule)),
Err(err) => Err(QueryError::RegexCompileError(format!(
"Failed to compile a pattern in the 'fields' map of the regex rule: {err:?}"
))),
}
}
120 changes: 120 additions & 0 deletions aw-query/tests/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,126 @@ mod query_tests {
assert_eq!(tags, &serde_json::json!(vec!["title-match"]));
}

#[test]
fn test_tag_fields_map() {
// Option B for ActivityWatch/aw-webui#939: per-field patterns on the
// existing `regex` rule, ANDed. mstsc and winbox show the same host in
// their titles, so only app AND title distinguish them.
let ds = setup_datastore_with_bucket();
let interval = TimeInterval::new_from_string(TIME_INTERVAL).unwrap();

let event = |app: &str, title: &str| Event {
id: None,
timestamp: chrono::Utc::now(),
duration: Duration::seconds(1),
data: json_map! { "app": json!(app), "title": json!(title) },
};
ds.insert_events(
BUCKET_ID,
&[
event("mstsc.exe", "office.example.com - Remote Desktop"),
event("winbox64.exe", "admin@office.example.com"),
event("firefox", "office.example.com"),
],
)
.unwrap();

let code = format!(
r#"
events = query_bucket("{}");
events = tag(events, [["rdp", {{ "type": "regex", "fields": {{ "app": "mstsc.exe", "title": "office.example" }} }}], ["winbox", {{ "type": "regex", "fields": {{ "app": "winbox", "title": "office.example" }} }}]]);
return events;"#,
BUCKET_ID
);
let result: DataType = aw_query::query(&code, &interval, &ds).unwrap();
let events: Vec<Event> = Vec::try_from(&result).unwrap();
assert_eq!(events.len(), 3);

// `query_bucket` returns events newest-first, so key by app instead of
// relying on insertion order.
let tags_by_app: std::collections::HashMap<String, serde_json::Value> = events
.iter()
.map(|e| {
(
e.data.get("app").unwrap().as_str().unwrap().to_string(),
e.data.get("$tags").cloned().unwrap(),
)
})
.collect();
assert_eq!(tags_by_app["mstsc.exe"], serde_json::json!(vec!["rdp"]));
assert_eq!(
tags_by_app["winbox64.exe"],
serde_json::json!(vec!["winbox"])
);
assert_eq!(
tags_by_app["firefox"],
serde_json::json!(Vec::<String>::new())
);

// Empty `regex`/`select_keys` count as unset, so a config that serializes
// them next to `fields` still parses (and stays fail-closed on servers
// that predate the field map, which read the empty regex as no match).
let code = format!(
r#"
events = query_bucket("{}");
events = tag(events, [["rdp", {{ "type": "regex", "regex": "", "select_keys": [], "fields": {{ "app": "mstsc" }} }}]]);
return events;"#,
BUCKET_ID
);
let result: DataType = aw_query::query(&code, &interval, &ds).unwrap();
let events: Vec<Event> = Vec::try_from(&result).unwrap();
let rdp_tags: Vec<_> = events
.iter()
.filter(|e| e.data.get("app").unwrap().as_str() == Some("mstsc.exe"))
.map(|e| e.data.get("$tags").cloned().unwrap())
.collect();
assert_eq!(rdp_tags, vec![serde_json::json!(vec!["rdp"])]);
}

#[test]
fn test_rule_parsing_fields_map() {
let ds = setup_datastore_populated();
let interval = TimeInterval::new_from_string(TIME_INTERVAL).unwrap();

// A fields rule may not smuggle in a single pattern: older servers read
// only `regex`, so silently ignoring it would change what the rule means.
for member in [r#""regex": "value""#, r#""select_keys": ["app"]"#] {
let code = format!(
r#"
events = [];
events = tag(events, [["t", {{ "type": "regex", {member}, "fields": {{ "app": "value" }} }}]]);
return events;"#
);
let res = aw_query::query(&code, &interval, &ds);
assert_err_type!(res, QueryError::InvalidFunctionParameters(_));
}

// Malformed or empty field maps are rejected rather than matching all.
for fields in [
r#""fields": []"#,
r#""fields": {}"#,
r#""fields": { "app": 5 }"#,
r#""fields": { "app": "" }"#,
] {
let code = format!(
r#"
events = [];
events = tag(events, [["t", {{ "type": "regex", {fields} }}]]);
return events;"#
);
let res = aw_query::query(&code, &interval, &ds);
assert_err_type!(res, QueryError::InvalidFunctionParameters(_));
}

// An uncompilable per-field pattern is reported as a regex error.
let code = r#"
events = [];
events = tag(events, [["t", { "type": "regex", "fields": { "app": "!#¤%&/(=" } }]]);
return events;"#;
let res = aw_query::query(code, &interval, &ds);
assert_err_type!(res, QueryError::RegexCompileError(_));
}

#[test]
fn test_rule_parsing() {
let ds = setup_datastore_populated();
Expand Down
Loading
Loading