Add gated agent update planning, preserved handoff and Linux worker ports
Rust development slice / linux (push) Failing after 6s
Rust development slice / linux (push) Failing after 6s
This commit is contained in:
@@ -24,6 +24,25 @@ pub fn validate_for_scope(
|
||||
now: DateTime<Utc>,
|
||||
scope: &str,
|
||||
) -> Result<Value, &'static str> {
|
||||
if scope == "agent.update" {
|
||||
if bytes.len() > 65536 {
|
||||
return Err("AGENT_UPDATE_INVENTORY_SIZE");
|
||||
}
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct UpdateInventory {
|
||||
agent_update: crate::agent_update::AgentUpdateObservation,
|
||||
}
|
||||
let update: UpdateInventory =
|
||||
serde_json::from_slice(bytes).map_err(|_| "INVALID_AGENT_UPDATE_OBSERVATION")?;
|
||||
if !update.agent_update.validate()
|
||||
|| update.agent_update.observed_at - now > chrono::Duration::seconds(45)
|
||||
|| now - update.agent_update.observed_at > chrono::Duration::hours(72)
|
||||
{
|
||||
return Err("INVALID_AGENT_UPDATE_OBSERVATION");
|
||||
}
|
||||
return serde_json::from_slice(bytes).map_err(|_| "INVALID_AGENT_UPDATE_OBSERVATION");
|
||||
}
|
||||
let prefix = match scope {
|
||||
"linux.base" => "linux.",
|
||||
"windows.base" => "windows.",
|
||||
@@ -34,6 +53,31 @@ pub fn validate_for_scope(
|
||||
}
|
||||
let value: Value = serde_json::from_slice(bytes).map_err(|_| "INVALID_INVENTORY_JSON")?;
|
||||
let mut base = value.as_object().cloned().ok_or("INVENTORY_OBJECT")?;
|
||||
if let Some(update) = base.remove("agent_update") {
|
||||
// Decode the original bytes as well: Value alone would discard duplicate
|
||||
// fields before the strict new metadata DTO could reject them.
|
||||
#[derive(Deserialize)]
|
||||
struct UpdateField {
|
||||
agent_update: crate::agent_update::AgentUpdateObservation,
|
||||
}
|
||||
let typed: UpdateField =
|
||||
serde_json::from_slice(bytes).map_err(|_| "INVALID_AGENT_UPDATE_OBSERVATION")?;
|
||||
if serde_json::to_value(&typed.agent_update)
|
||||
.map_err(|_| "INVALID_AGENT_UPDATE_OBSERVATION")?
|
||||
!= update
|
||||
|| !typed.agent_update.validate()
|
||||
|| typed.agent_update.platform
|
||||
!= if scope == "linux.base" {
|
||||
crate::agent_update::Platform::LinuxX64
|
||||
} else {
|
||||
crate::agent_update::Platform::WindowsX64
|
||||
}
|
||||
|| typed.agent_update.observed_at - now > chrono::Duration::seconds(45)
|
||||
|| now - typed.agent_update.observed_at > chrono::Duration::hours(72)
|
||||
{
|
||||
return Err("INVALID_AGENT_UPDATE_OBSERVATION");
|
||||
}
|
||||
}
|
||||
let observations = base.remove("observations");
|
||||
let started = base.remove("collection_started_at");
|
||||
let completed = base.remove("collection_completed_at");
|
||||
@@ -231,3 +275,51 @@ mod tests {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod agent_update_tests {
|
||||
use super::*;
|
||||
use crate::agent_update::{AgentUpdateObservation, Platform};
|
||||
#[test]
|
||||
fn update_scope_is_separate_bounded_strict_and_never_restamps_base_inventory() {
|
||||
let now = Utc::now();
|
||||
let observation = AgentUpdateObservation::not_configured(Platform::LinuxX64, now);
|
||||
let value = serde_json::json!({"agent_update":observation});
|
||||
let bytes = serde_json::to_vec(&value).unwrap();
|
||||
assert_eq!(
|
||||
validate_for_scope(&bytes, now, "agent.update").unwrap(),
|
||||
value
|
||||
);
|
||||
assert!(validate_for_scope(&bytes, now, "linux.base").is_err());
|
||||
let object = serde_json::to_string(&observation).unwrap();
|
||||
let duplicate = format!("{{\"agent_update\":{object},\"agent_update\":{object}}}");
|
||||
assert!(validate_for_scope(duplicate.as_bytes(), now, "agent.update").is_err());
|
||||
let mut false_valid = value.clone();
|
||||
false_valid["agent_update"]["quality"] = serde_json::json!("valid");
|
||||
assert!(
|
||||
validate_for_scope(
|
||||
&serde_json::to_vec(&false_valid).unwrap(),
|
||||
now,
|
||||
"agent.update"
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
let mut mixed = value;
|
||||
mixed["hostname"] = serde_json::json!("pretend-new-base");
|
||||
assert!(
|
||||
validate_for_scope(&serde_json::to_vec(&mixed).unwrap(), now, "agent.update").is_err()
|
||||
);
|
||||
let future = AgentUpdateObservation::not_configured(
|
||||
Platform::LinuxX64,
|
||||
now + chrono::Duration::seconds(46),
|
||||
);
|
||||
assert!(
|
||||
validate_for_scope(
|
||||
&serde_json::to_vec(&serde_json::json!({"agent_update":future})).unwrap(),
|
||||
now,
|
||||
"agent.update"
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user