use serde_json::{Value, json};
pub const OCSF_VERSION: &str = "1.3.0";
const DISCOVERY: u32 = 5;
const SOFTWARE_INVENTORY_INFO: u32 = 5020;
const DEVICE_CONFIG_STATE: u32 = 5002;
const COLLECT: u32 = 2;
const INFORMATIONAL: u32 = 1;
const HIGH: u32 = 4;
pub fn events(doc: &Value) -> Vec<Value> {
let time = doc.get("generated_ms").cloned().unwrap_or(json!(0));
let device = device(doc);
let metadata = json!({
"version": OCSF_VERSION,
"product": {
"name": "OpenMoat",
"vendor_name": "OpenMoat",
"version": doc.get("openmoat_version").cloned().unwrap_or(Value::Null),
},
"log_name": "openmoat-inventory",
});
let mut out = Vec::new();
for agent in doc
.get("agents")
.and_then(Value::as_array)
.into_iter()
.flatten()
.filter(|a| a.get("present") == Some(&Value::Bool(true)))
{
let version = agent
.get("version")
.and_then(Value::as_str)
.unwrap_or("unknown");
let name = agent.get("name").cloned().unwrap_or(Value::Null);
let level = agent
.pointer("/protection/level")
.and_then(Value::as_str)
.unwrap_or("unknown");
out.push(json!({
"class_uid": SOFTWARE_INVENTORY_INFO,
"class_name": "Software Inventory Info",
"category_uid": DISCOVERY,
"category_name": "Discovery",
"activity_id": COLLECT,
"activity_name": "Collect",
"type_uid": SOFTWARE_INVENTORY_INFO * 100 + COLLECT,
"severity_id": INFORMATIONAL,
"time": time,
"metadata": metadata,
"device": device,
"package": {
"name": agent.get("id").cloned().unwrap_or(Value::Null),
"version": version,
},
"product": { "name": name },
"message": format!("{} {version}: {level}", agent["id"].as_str().unwrap_or_default()),
"unmapped": { "openmoat": agent },
}));
}
let drifted = doc.pointer("/lock/intact") != Some(&Value::Bool(true))
|| doc.pointer("/policy/error").is_some_and(|e| !e.is_null());
let unmapped: serde_json::Map<String, Value> = [
"policy",
"lock",
"audit",
"foreign_hooks",
"mcp_servers",
"mcp_proxied",
"run_tiers",
]
.into_iter()
.filter_map(|key| doc.get(key).map(|v| (key.to_owned(), v.clone())))
.collect();
out.push(json!({
"class_uid": DEVICE_CONFIG_STATE,
"class_name": "Device Config State",
"category_uid": DISCOVERY,
"category_name": "Discovery",
"activity_id": COLLECT,
"activity_name": "Collect",
"type_uid": DEVICE_CONFIG_STATE * 100 + COLLECT,
"severity_id": if drifted { HIGH } else { INFORMATIONAL },
"time": time,
"metadata": metadata,
"device": device,
"message": if drifted {
"OpenMoat's lock or policy is not intact: every agent call is denied"
} else {
"OpenMoat's lock and policy are intact"
},
"unmapped": { "openmoat": unmapped },
}));
out
}
fn device(doc: &Value) -> Value {
let os = doc
.pointer("/machine/os")
.and_then(Value::as_str)
.unwrap_or_default();
let name = doc
.pointer("/machine/name")
.and_then(Value::as_str)
.unwrap_or("unnamed");
json!({
"type_id": 0,
"type": "Unknown",
"name": name,
"os": { "name": os, "type_id": os_type(os) },
})
}
fn os_type(os: &str) -> u32 {
match os {
"windows" => 100,
"linux" => 200,
"macos" => 300,
_ => 0,
}
}