Implement bounded Windows Event Log jobs and verify contracts on cloud Linux
Rust development slice / linux (push) Failing after 7s

This commit is contained in:
2026-11-14 10:48:05 +01:00
parent d532ff0ffd
commit 5656431ebf
21 changed files with 2296 additions and 136 deletions
+162
View File
@@ -192,6 +192,7 @@ pub async fn event(
}
}
script_profile_evidence(&payload.action, &evidence)?;
windows_event_log_success_evidence(&payload, r, &evidence)?;
if matches!(payload.action, Action::Reboot { .. }) {
crate::synchronization::verify_reboot_success(
tx, tenant, agent, e, &payload, r, &evidence,
@@ -225,6 +226,70 @@ pub async fn event(
.await?;
Ok(())
}
fn windows_event_log_success_evidence(
job: &JobPayload,
result: &JobResult,
evidence: &Value,
) -> Result<()> {
if !job.action.is_windows_event_log_fixture() {
return Ok(());
}
let Action::QueryLogs {
unit,
since,
until,
max_lines,
} = &job.action
else {
anyhow::bail!("fixed event log action required")
};
let observed_since: chrono::DateTime<Utc> = serde_json::from_value(
evidence
.get("start_utc")
.cloned()
.context("event log start missing")?,
)?;
let observed_until: chrono::DateTime<Utc> = serde_json::from_value(
evidence
.get("end_utc")
.cloned()
.context("event log end missing")?,
)?;
let complete = evidence.get("completeness") == Some(&json!("complete"))
&& evidence.get("truncated") == Some(&json!(false))
&& evidence.get("error_code") == Some(&Value::Null);
let partial = evidence.get("completeness") == Some(&json!("partial"))
&& evidence.get("truncated") == Some(&json!(true))
&& matches!(
evidence.get("error_code").and_then(Value::as_str),
Some("EVENT_LOG_EVENT_LIMIT" | "EVENT_LOG_BYTE_LIMIT" | "EVENT_LOG_EVENT_SIZE_LIMIT")
);
ensure!(
!result.reboot_required
&& evidence.get("truncated") == Some(&json!(result.output_truncated))
&& job.execution_identity == "agent_service"
&& job.action.validate()
&& job
.action
.valid_job_limits(job.max_runtime_seconds, job.max_output_bytes)
&& evidence.get("source_id") == Some(&json!(unit))
&& evidence.get("channel") == Some(&json!(WINDOWS_EVENT_LOG_FIXTURE_CHANNEL))
&& evidence.get("provider") == Some(&json!(WINDOWS_EVENT_LOG_FIXTURE_PROVIDER))
&& observed_since == *since
&& observed_until == *until
&& evidence.get("max_events") == Some(&json!(max_lines))
&& evidence.get("max_output_bytes") == Some(&json!(job.max_output_bytes))
&& evidence.get("quality") == Some(&json!("valid"))
&& evidence.get("win32_error") == Some(&Value::Null)
&& evidence.get("bookmark_updated") == Some(&json!(false))
&& evidence.get("process_tree_bounded") == Some(&json!(true))
&& evidence.get("execution_sid") == Some(&json!("S-1-5-19"))
&& (complete || partial),
"fixed Windows Event Log evidence mismatch"
);
Ok(())
}
// The profile records the requested process option, never an effective GPO policy.
fn script_profile_evidence(action: &Action, evidence: &Value) -> Result<()> {
if let Action::RunScript {
@@ -251,6 +316,94 @@ fn script_profile_evidence(action: &Action, evidence: &Value) -> Result<()> {
mod script_profile_tests {
use super::*;
#[test]
fn windows_event_log_success_binds_request_and_keeps_limit_partial_honest() {
let id = Uuid::new_v4();
let now = Utc::now();
let job = JobPayload {
schema_version: 1,
tenant_id: id,
asset_id: id,
agent_id: id,
enrollment_generation: 1,
assignment_version: 1,
job_id: id,
attempt_id: id,
action: Action::QueryLogs {
unit: WINDOWS_EVENT_LOG_FIXTURE_SOURCE.into(),
since: now - Duration::hours(1),
until: now,
max_lines: 1000,
},
execution_identity: "agent_service".into(),
not_before: now,
start_before: now + Duration::minutes(1),
max_runtime_seconds: 30,
max_output_bytes: 4096,
};
let evidence = json!({"source_id":WINDOWS_EVENT_LOG_FIXTURE_SOURCE,"channel":"Application","provider":"RustDiscovery-MVP-Test","start_utc":now-Duration::hours(1),"end_utc":now,"max_events":1000,"max_output_bytes":4096,"quality":"valid","completeness":"complete","truncated":false,"error_code":null,"win32_error":null,"bookmark_updated":false,"process_tree_bounded":true,"execution_sid":"S-1-5-19"});
let result = JobResult::default();
windows_event_log_success_evidence(&job, &result, &evidence).unwrap();
for conflicting in [
JobResult {
reboot_required: true,
..Default::default()
},
JobResult {
output_truncated: true,
..Default::default()
},
] {
assert!(windows_event_log_success_evidence(&job, &conflicting, &evidence).is_err());
}
for field in [
"source_id",
"channel",
"provider",
"start_utc",
"end_utc",
"max_events",
"max_output_bytes",
"quality",
"win32_error",
"bookmark_updated",
"process_tree_bounded",
"execution_sid",
] {
let mut bad = evidence.clone();
bad[field] = json!("different");
assert!(
windows_event_log_success_evidence(&job, &result, &bad).is_err(),
"{field}"
);
}
for code in [
"EVENT_LOG_EVENT_LIMIT",
"EVENT_LOG_BYTE_LIMIT",
"EVENT_LOG_EVENT_SIZE_LIMIT",
] {
let mut partial = evidence.clone();
partial["completeness"] = json!("partial");
partial["truncated"] = json!(true);
partial["error_code"] = json!(code);
let partial_result = JobResult {
output_truncated: true,
..Default::default()
};
windows_event_log_success_evidence(&job, &partial_result, &partial).unwrap();
assert!(windows_event_log_success_evidence(&job, &result, &partial).is_err());
partial["error_code"] = json!("EVENT_LOG_TIMEOUT");
assert!(windows_event_log_success_evidence(&job, &partial_result, &partial).is_err());
partial["quality"] = json!("permission_denied");
assert!(windows_event_log_success_evidence(&job, &partial_result, &partial).is_err());
}
let mut permission_denied = evidence.clone();
permission_denied["win32_error"] = json!(5);
assert!(windows_event_log_success_evidence(&job, &result, &permission_denied).is_err());
let mut false_complete = evidence.clone();
false_complete["truncated"] = json!(true);
assert!(windows_event_log_success_evidence(&job, &result, &false_complete).is_err());
}
#[test]
fn requested_profile_success_evidence_is_exact_without_effective_policy_claim() {
let mut action = Action::RunScript {
interpreter: "powershell".into(),
@@ -383,6 +536,12 @@ pub async fn permit(
return Ok(denied("BINDING_MISMATCH"));
}
let now = Utc::now();
if !payload
.action
.valid_job_limits(payload.max_runtime_seconds, payload.max_output_bytes)
{
return Ok(denied("INVALID_JOB_LIMITS"));
}
if !payload.action.validate() {
return Ok(denied("APT_MANIFEST_REQUIRED"));
}
@@ -580,6 +739,9 @@ pub async fn next(
if !payload
.action
.available_for(&payload.execution_identity, &caps)
|| !payload
.action
.valid_job_limits(payload.max_runtime_seconds, payload.max_output_bytes)
{
tx.commit().await?;
return Ok(None);
+160
View File
@@ -156,6 +156,7 @@ pub struct Variant {
deserialize_with = "rmm_domain::jobs::deserialize_windows_execution_profile",
skip_serializing_if = "Option::is_none"
)]
#[schemars(with = "WindowsExecutionProfile")]
windows_execution_profile: Option<WindowsExecutionProfile>,
}
#[derive(Clone, Deserialize, Serialize, schemars::JsonSchema)]
@@ -471,6 +472,9 @@ pub async fn plan(
let row = &rows[0];
let caps = capabilities(row.get("capabilities"))?;
let action = target_action(&input, &caps)?;
if !action.valid_job_limits(input.max_runtime_seconds, input.max_output_bytes) {
return Err(fail("VALIDATION_ERROR"));
}
if !action.available_for(&input.execution_identity, &caps) {
return Err(fail("UNSUPPORTED_CAPABILITY"));
}
@@ -598,6 +602,12 @@ async fn recheck(tx: &mut Transaction<'_, Postgres>, plan: &PlanPayload) -> Resu
}
let mut references = Vec::new();
for job in &plan.jobs {
if !job.payload.action.valid_job_limits(
job.payload.max_runtime_seconds,
job.payload.max_output_bytes,
) {
return Err(fail("VALIDATION_ERROR"));
}
if !job.payload.action.validate() {
return Err(Error(StatusCode::CONFLICT, "APT_MANIFEST_REQUIRED"));
}
@@ -1022,6 +1032,7 @@ async fn operations_http(
#[cfg(test)]
mod windows_contract_tests {
use super::*;
use chrono::Duration;
fn input() -> PlanInput {
let tenant = Uuid::new_v4();
let now = Utc::now();
@@ -1054,6 +1065,33 @@ mod windows_contract_tests {
assert!(target_action(&i, &["windows.base/1".into()]).is_err());
}
#[test]
fn fixed_windows_log_web_contract_denies_extra_source_parameters() {
let tenant = Uuid::new_v4();
let now = Utc::now();
let value = json!({"tenant_id":tenant,"action":"query_logs","targets":{"asset_ids":[Uuid::new_v4()]},"parameters":{"execution_identity":"agent_service","timeout_seconds":30,"max_output_bytes":4096,"unit":WINDOWS_EVENT_LOG_FIXTURE_SOURCE,"since":now-Duration::hours(1),"until":now,"max_lines":1000},"schedule":{"not_before":now,"start_before":now+Duration::minutes(10),"offline_policy":"wait_until_deadline"},"rollout":{"max_parallel":1,"halt_after_failures":1},"reason":"fixed source"});
let input = serde_json::from_value::<PlanRequest>(value.clone())
.unwrap()
.resolve(tenant)
.unwrap();
assert!(input.action.is_windows_event_log_fixture());
assert!(input.action.validate());
assert!(
input
.action
.valid_job_limits(input.max_runtime_seconds, input.max_output_bytes)
);
assert!(input.script_variants.is_empty());
for field in ["channel", "provider", "xpath", "bookmark"] {
let mut bad = value.clone();
bad["parameters"][field] = json!("arbitrary");
assert!(
serde_json::from_value::<PlanRequest>(bad)
.map(|request| request.resolve(tenant).is_err())
.unwrap_or(true)
);
}
}
#[test]
fn remote_signed_fixed_variants_bind_profile_without_linux_inheritance() {
let mut i = input();
let legacy_linux = encode(&i.action).unwrap();
@@ -1100,6 +1138,18 @@ mod windows_contract_tests {
i.script_variants[1].windows_execution_profile =
Some(WindowsExecutionProfile::PowerShell51RemoteSigned);
assert!(validate_variants(&i.script_variants).is_err());
let schema = schemars::schema_for!(Variant).to_value();
assert_eq!(
schema["properties"]["windows_execution_profile"],
json!({"$ref":"#/$defs/WindowsExecutionProfile"})
);
assert!(
!schema["required"]
.as_array()
.unwrap()
.contains(&json!("windows_execution_profile"))
);
assert_eq!(schema["$defs"]["WindowsExecutionProfile"]["type"], "string");
let legacy = json!({"os_family":"windows", "interpreter":"powershell", "encoding":"utf-8", "script_utf8":"hi"});
let inherited: Variant = serde_json::from_value(legacy.clone()).unwrap();
assert!(inherited.windows_execution_profile.is_none());
@@ -1351,6 +1401,116 @@ mod windows_contract_tests {
.unwrap()
.is_none()
);
let now = Utc::now();
let mut logs = input();
logs.targets.asset_ids = vec![assets[1]];
logs.script_variants.clear();
logs.max_runtime_seconds = 30;
logs.action = Action::QueryLogs {
unit: WINDOWS_EVENT_LOG_FIXTURE_SOURCE.into(),
since: now - Duration::hours(1),
until: now,
max_lines: 1000,
};
assert_eq!(
plan(&state, &principal, tenant, logs.clone())
.await
.unwrap_err()
.1,
"FORBIDDEN"
);
sqlx::query("UPDATE memberships SET permissions=array_append(permissions,'logs:read') WHERE principal=$1").bind(&principal.name).execute(&admin).await.unwrap();
assert_eq!(
plan(&state, &principal, tenant, logs.clone())
.await
.unwrap_err()
.1,
"UNSUPPORTED_CAPABILITY"
);
let mut log_caps = windows_caps.as_array().unwrap().clone();
log_caps.extend([
json!("query_logs"),
json!(WINDOWS_EVENT_LOG_FIXTURE_CAPABILITY),
]);
sqlx::query("UPDATE agent_installations SET capabilities=$2 WHERE agent_id=$1")
.bind(agents[1])
.bind(json!(log_caps))
.execute(&admin)
.await
.unwrap();
let view = plan(&state, &principal, tenant, logs).await.unwrap();
let log_id = Uuid::parse_str(view["plan_id"].as_str().unwrap()).unwrap();
let bytes: Vec<u8> =
sqlx::query_scalar("SELECT payload FROM operation_plans WHERE plan_id=$1")
.bind(log_id)
.fetch_one(&admin)
.await
.unwrap();
let log_bound: PlanPayload = serde_json::from_slice(&bytes).unwrap();
let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant)
.await
.unwrap();
recheck(&mut tx, &log_bound).await.unwrap();
tx.rollback().await.unwrap();
let pending = submit(
&state,
&principal,
tenant,
log_id,
Submit {
idempotency_key: "fixed-event-log".into(),
},
)
.await
.unwrap();
assert_eq!(pending["approval_state"], "pending_approval");
assert_eq!(pending["job_count"], 0);
let mut changed = log_bound.clone();
if let Action::QueryLogs { max_lines, .. } = &mut changed.jobs[0].payload.action {
*max_lines = 999;
}
let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant)
.await
.unwrap();
assert_eq!(
recheck(&mut tx, &changed).await.unwrap_err().1,
"PLAN_HASH_MISMATCH"
);
tx.rollback().await.unwrap();
let mut changed = log_bound.clone();
changed.jobs[0].payload.max_output_bytes = 65537;
let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant)
.await
.unwrap();
assert_eq!(
recheck(&mut tx, &changed).await.unwrap_err().1,
"VALIDATION_ERROR"
);
tx.rollback().await.unwrap();
log_caps.retain(|cap| cap != WINDOWS_EVENT_LOG_FIXTURE_CAPABILITY);
sqlx::query("UPDATE agent_installations SET capabilities=$2 WHERE agent_id=$1")
.bind(agents[1])
.bind(json!(log_caps))
.execute(&admin)
.await
.unwrap();
let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant)
.await
.unwrap();
assert_eq!(
recheck(&mut tx, &log_bound).await.unwrap_err().1,
"UNSUPPORTED_CAPABILITY"
);
tx.rollback().await.unwrap();
sqlx::query("UPDATE memberships SET permissions=array_remove(permissions,'logs:read') WHERE principal=$1").bind(&principal.name).execute(&admin).await.unwrap();
let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant)
.await
.unwrap();
assert_eq!(
recheck(&mut tx, &log_bound).await.unwrap_err().1,
"FORBIDDEN"
);
tx.rollback().await.unwrap();
let counts:(i64,i64,i64)=sqlx::query_as("SELECT (SELECT count(*) FROM jobs WHERE tenant_id=$1),(SELECT count(*) FROM outbox WHERE tenant_id=$1),(SELECT count(*) FROM execution_permits WHERE tenant_id=$1)").bind(tenant).fetch_one(&admin).await.unwrap();
assert_eq!(counts, (0, 0, 0));
let after: Vec<u8> =