//! Additive JSON observation mapping inside the existing snapshot payload. This //! does not introduce a protobuf tag or a fabricated receive time. use anyhow::{Result, ensure}; use rmm_domain::inventory_document::LINUX_SOFTWARE_OBSERVATION_MAX_BYTES; use serde_json::{Value, json}; fn truncate_rows(observation: &mut Value) -> bool { let Some(rows) = observation["data"].as_array_mut().filter(|v| !v.is_empty()) else { return false; }; rows.truncate(rows.len() / 2); observation["truncated"] = json!(true); observation["completeness"] = json!("partial"); observation["error_code"] = json!("WIRE_SCOPE_LIMIT"); true } pub fn observations(inventory: &crate::inventory::LinuxInventory) -> Result> { let value = serde_json::to_value(inventory)?; let mut result = Vec::new(); for (name, observation) in value .as_object() .ok_or_else(|| anyhow::anyhow!("inventory object"))? { let mut observation = observation.clone(); observation["scope"] = json!(format!("linux.{name}")); // Bound each scope independently. Never drop unsupported/error metadata // or imply that absent/truncated rows were deleted from a device. let max_bytes = if name == "software" { LINUX_SOFTWARE_OBSERVATION_MAX_BYTES } else { 32768 }; while serde_json::to_vec(&observation)?.len() > max_bytes { if !truncate_rows(&mut observation) { observation["truncated"] = json!(true); observation["completeness"] = json!("partial"); observation["error_code"] = json!("WIRE_SCOPE_LIMIT"); observation["data"] = Value::Null; observation["quality"] = json!("unknown"); break; } } result.push(observation); } Ok(result) } pub fn snapshot(extended: bool) -> Result { let started = chrono::Utc::now(); let mut base = serde_json::to_value(crate::collect()?)?; if extended { base["observations"] = serde_json::to_value(observations(&crate::inventory::collect_native())?)?; base["collection_started_at"] = json!(started); base["collection_completed_at"] = json!(chrono::Utc::now()); } Ok(base) } pub fn encode_snapshot(mut inventory: Value) -> Result> { let mut bytes = serde_json::to_vec(&inventory)?; // Preserve the other scopes when a large package list fills the snapshot. while bytes.len() > 512 * 1024 { let software = inventory .get_mut("observations") .and_then(Value::as_array_mut) .and_then(|scopes| scopes.iter_mut().find(|scope| scope["scope"] == "linux.software")); ensure!(software.is_some_and(truncate_rows), "INVENTORY_WIRE_LIMIT"); bytes = serde_json::to_vec(&inventory)?; } Ok(bytes) } #[cfg(test)] mod tests { use super::*; #[test] fn native_wire_scopes_preserve_quality_time_and_unsupported() { let native = crate::inventory::collect_native(); let mapped = observations(&native).unwrap(); assert_eq!(mapped.len(), 12); let smart = mapped.iter().find(|v| v["scope"] == "linux.smart").unwrap(); assert_eq!(smart["quality"], "unsupported"); assert!(smart["data"].is_null()); assert_eq!(smart["completeness"], "unknown"); for observation in &mapped { assert!( chrono::DateTime::parse_from_rfc3339(observation["observed_at"].as_str().unwrap()) .is_ok() ); assert!(!observation["source_id"].as_str().unwrap().is_empty()); assert!(observation.get("received_at").is_none()); let limit = if observation["scope"] == "linux.software" { LINUX_SOFTWARE_OBSERVATION_MAX_BYTES } else { 32768 }; assert!(serde_json::to_vec(observation).unwrap().len() <= limit); } let base = snapshot(false).unwrap(); assert!(serde_json::from_value::(base).is_ok()); } #[test] fn scope_limit_retains_error_metadata_and_marks_partial() { let mut native = crate::inventory::collect_native(); native.software.data = Some( (0..2000) .map(|_| crate::inventory::SoftwarePackage { name: "x".repeat(128), version: "1.0".into(), architecture: "amd64".into(), status: "installed".into(), }) .collect(), ); let mapped = observations(&native).unwrap(); let software = mapped .iter() .find(|v| v["scope"] == "linux.software") .unwrap(); assert_eq!(software["truncated"], true); assert_eq!(software["completeness"], "partial"); } #[test] fn snapshot_limit_shrinks_software_and_preserves_other_scopes() { let network = json!({"scope":"linux.interfaces","data":"x".repeat(280 * 1024)}); let data = vec!["x".repeat(1024); 240]; let snapshot = json!({"observations":[ network, {"scope":"linux.software","completeness":"complete","data":data}, ]}); let bytes = encode_snapshot(snapshot).unwrap(); let bounded: Value = serde_json::from_slice(&bytes).unwrap(); assert_eq!(bounded["observations"][0], network); assert_eq!(bounded["observations"][1]["completeness"], "partial"); assert_eq!( bounded["observations"][1]["data"].as_array().unwrap().len(), 120 ); } }