Implement bounded Windows Event Log jobs and verify contracts on cloud Linux
Rust development slice / linux (push) Failing after 7s
Rust development slice / linux (push) Failing after 7s
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user