feat: deploy platform inventory and signed APT retry client compatibility
Rust development slice / linux (push) Failing after 6s
Rust development slice / linux (push) Failing after 6s
This commit is contained in:
@@ -15,6 +15,20 @@ struct Observation {
|
||||
data: Value,
|
||||
}
|
||||
pub fn validate(bytes: &[u8], now: DateTime<Utc>) -> Result<Value, &'static str> {
|
||||
validate_for_scope(bytes, now, "linux.base")
|
||||
}
|
||||
/// Snapshot scope is supplied by the authenticated ingestion path, never inferred
|
||||
/// from an arbitrary observation's label. Windows does not manufacture inodes.
|
||||
pub fn validate_for_scope(
|
||||
bytes: &[u8],
|
||||
now: DateTime<Utc>,
|
||||
scope: &str,
|
||||
) -> Result<Value, &'static str> {
|
||||
let prefix = match scope {
|
||||
"linux.base" => "linux.",
|
||||
"windows.base" => "windows.",
|
||||
_ => return Err("UNSUPPORTED_INVENTORY_SCOPE"),
|
||||
};
|
||||
if bytes.len() > 512 * 1024 {
|
||||
return Err("INVENTORY_SIZE");
|
||||
}
|
||||
@@ -23,10 +37,36 @@ pub fn validate(bytes: &[u8], now: DateTime<Utc>) -> Result<Value, &'static str>
|
||||
let observations = base.remove("observations");
|
||||
let started = base.remove("collection_started_at");
|
||||
let completed = base.remove("collection_completed_at");
|
||||
let inventory: crate::Inventory =
|
||||
serde_json::from_value(Value::Object(base)).map_err(|_| "INVALID_BASE_INVENTORY")?;
|
||||
if !inventory.validate() {
|
||||
return Err("INVALID_BASE_INVENTORY");
|
||||
if scope == "windows.base" {
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct WindowsInventory {
|
||||
os: String,
|
||||
hostname: String,
|
||||
architecture: String,
|
||||
root_total_bytes: u64,
|
||||
root_available_bytes: u64,
|
||||
root_available_inodes: Option<u64>,
|
||||
}
|
||||
let inventory: WindowsInventory =
|
||||
serde_json::from_value(Value::Object(base)).map_err(|_| "INVALID_BASE_INVENTORY")?;
|
||||
if !inventory.os.starts_with("Windows")
|
||||
|| inventory.os.len() > 4096
|
||||
|| inventory.hostname.is_empty()
|
||||
|| inventory.hostname.len() > 255
|
||||
|| inventory.architecture != "x86_64"
|
||||
|| inventory.root_total_bytes == 0
|
||||
|| inventory.root_available_bytes > inventory.root_total_bytes
|
||||
|| inventory.root_available_inodes.is_some()
|
||||
{
|
||||
return Err("INVALID_WINDOWS_BASE_INVENTORY");
|
||||
}
|
||||
} else {
|
||||
let inventory: crate::Inventory =
|
||||
serde_json::from_value(Value::Object(base)).map_err(|_| "INVALID_BASE_INVENTORY")?;
|
||||
if !inventory.validate() {
|
||||
return Err("INVALID_BASE_INVENTORY");
|
||||
}
|
||||
}
|
||||
if let Some(observations) = observations {
|
||||
let started: DateTime<Utc> =
|
||||
@@ -57,7 +97,7 @@ pub fn validate(bytes: &[u8], now: DateTime<Utc>) -> Result<Value, &'static str>
|
||||
}
|
||||
let o: Observation =
|
||||
serde_json::from_value(item.clone()).map_err(|_| "INVALID_OBSERVATION")?;
|
||||
if !o.scope.starts_with("linux.")
|
||||
if !o.scope.starts_with(prefix)
|
||||
|| o.scope.len() > 64
|
||||
|| !o
|
||||
.scope
|
||||
@@ -114,4 +154,27 @@ mod tests {
|
||||
unknown["tenant_id"] = json!("forged");
|
||||
assert!(validate(&serde_json::to_vec(&unknown).unwrap(), now).is_err());
|
||||
}
|
||||
#[test]
|
||||
fn windows_scope_does_not_weaken_linux_or_accept_platform_spoofing() {
|
||||
let now = Utc::now();
|
||||
let mut v = base();
|
||||
v["os"] = json!("Windows 11 Pro");
|
||||
v["root_available_inodes"] = Value::Null;
|
||||
let bytes = serde_json::to_vec(&v).unwrap();
|
||||
assert!(validate(&bytes, now).is_err());
|
||||
assert!(validate_for_scope(&bytes, now, "windows.base").is_ok());
|
||||
assert!(validate_for_scope(&bytes, now, "arbitrary.base").is_err());
|
||||
v["root_available_inodes"] = json!(0);
|
||||
assert!(validate_for_scope(&serde_json::to_vec(&v).unwrap(), now, "windows.base").is_err());
|
||||
v["root_available_inodes"] = Value::Null;
|
||||
v["collection_started_at"] = json!(now);
|
||||
v["collection_completed_at"] = json!(now);
|
||||
v["observations"] = json!([{"scope":"linux.os","source_id":"windows.native","observed_at":now,"quality":"valid","completeness":"partial","truncated":false,"error_code":null,"data":{}}]);
|
||||
assert_eq!(
|
||||
validate_for_scope(&serde_json::to_vec(&v).unwrap(), now, "windows.base").unwrap_err(),
|
||||
"INVALID_OBSERVATION_SCOPE"
|
||||
);
|
||||
v["observations"][0]["scope"] = json!("windows.os");
|
||||
assert!(validate_for_scope(&serde_json::to_vec(&v).unwrap(), now, "windows.base").is_ok());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user