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:
+135
-1
@@ -14,6 +14,13 @@ pub fn encode<T: Serialize>(value: &T) -> Result<Vec<u8>, serde_json::Error> {
|
||||
serde_json::to_vec(value)
|
||||
}
|
||||
|
||||
/// One explicitly qualified Event Log source, not an arbitrary channel/XPath.
|
||||
pub const WINDOWS_EVENT_LOG_FIXTURE_SOURCE: &str =
|
||||
"windows.eventlog.application.rustdiscovery-mvp-test";
|
||||
pub const WINDOWS_EVENT_LOG_FIXTURE_CAPABILITY: &str = "logs.windows.eventlog.fixture/1";
|
||||
pub const WINDOWS_EVENT_LOG_FIXTURE_CHANNEL: &str = "Application";
|
||||
pub const WINDOWS_EVENT_LOG_FIXTURE_PROVIDER: &str = "RustDiscovery-MVP-Test";
|
||||
|
||||
/// Fixed per-job Windows PowerShell 5.1 option, never a machine/GPO policy.
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, schemars::JsonSchema)]
|
||||
pub enum WindowsExecutionProfile {
|
||||
@@ -54,6 +61,7 @@ pub enum Action {
|
||||
deserialize_with = "deserialize_windows_execution_profile",
|
||||
skip_serializing_if = "Option::is_none"
|
||||
)]
|
||||
#[schemars(with = "WindowsExecutionProfile")]
|
||||
windows_execution_profile: Option<WindowsExecutionProfile>,
|
||||
},
|
||||
RefreshInventory {
|
||||
@@ -160,7 +168,8 @@ impl Action {
|
||||
} => {
|
||||
unit(u)
|
||||
&& since < until
|
||||
&& (*until - *since).num_hours() <= 24
|
||||
&& *until - *since <= chrono::Duration::hours(24)
|
||||
&& (u != WINDOWS_EVENT_LOG_FIXTURE_SOURCE || since.timestamp() >= 0)
|
||||
&& *max_lines > 0
|
||||
&& *max_lines <= 1000
|
||||
}
|
||||
@@ -234,6 +243,18 @@ impl Action {
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn is_windows_event_log_fixture(&self) -> bool {
|
||||
matches!(self, Self::QueryLogs { unit, .. } if unit == WINDOWS_EVENT_LOG_FIXTURE_SOURCE)
|
||||
}
|
||||
/// The outer process budget includes authenticated worker startup/cleanup;
|
||||
/// its inner Event Log calls retain their own cooperative <=5s deadline.
|
||||
pub fn valid_job_limits(&self, runtime_seconds: u32, output_bytes: u32) -> bool {
|
||||
(1..=3600).contains(&runtime_seconds)
|
||||
&& (1..=262144).contains(&output_bytes)
|
||||
&& (!self.is_windows_event_log_fixture()
|
||||
|| ((10..=120).contains(&runtime_seconds)
|
||||
&& (1024..=65536).contains(&output_bytes)))
|
||||
}
|
||||
/// Interpreter identity is part of the immutable action, never inferred at execution.
|
||||
pub fn script_platform(&self) -> Option<&'static str> {
|
||||
match self {
|
||||
@@ -283,6 +304,16 @@ impl Action {
|
||||
&& !(has("linux.base/1") && has("windows.base/1"))
|
||||
&& (scope != "windows.base" || has("rmm.sync.v1"))
|
||||
}
|
||||
Self::QueryLogs { unit, .. }
|
||||
if unit == WINDOWS_EVENT_LOG_FIXTURE_SOURCE || has("windows.base/1") =>
|
||||
{
|
||||
identity == "agent_service"
|
||||
&& unit == WINDOWS_EVENT_LOG_FIXTURE_SOURCE
|
||||
&& has("windows.base/1")
|
||||
&& !has("linux.base/1")
|
||||
&& has("rmm.sync.v1")
|
||||
&& has(WINDOWS_EVENT_LOG_FIXTURE_CAPABILITY)
|
||||
}
|
||||
Self::SetServiceState { state, .. }
|
||||
if identity == "system" || has("windows.base/1") =>
|
||||
{
|
||||
@@ -664,6 +695,28 @@ mod tests {
|
||||
}
|
||||
#[test]
|
||||
fn windows_remote_signed_profile_is_optional_exact_and_capability_bound() {
|
||||
let schema = schemars::schema_for!(Action).to_value();
|
||||
let script_schema = schema["oneOf"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.find(|variant| {
|
||||
variant["properties"]
|
||||
.get("windows_execution_profile")
|
||||
.is_some()
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
script_schema["properties"]["windows_execution_profile"],
|
||||
serde_json::json!({"$ref":"#/$defs/WindowsExecutionProfile"})
|
||||
);
|
||||
assert!(
|
||||
!script_schema["required"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.contains(&serde_json::json!("windows_execution_profile"))
|
||||
);
|
||||
assert_eq!(schema["$defs"]["WindowsExecutionProfile"]["type"], "string");
|
||||
let legacy = br#"{"action":"run_script","interpreter":"powershell","script_utf8":"Write-Output 'hi'\r\n","arguments":[],"expected_stdout":null}"#;
|
||||
let mut action: Action = serde_json::from_slice(legacy).unwrap();
|
||||
assert_eq!(encode(&action).unwrap(), legacy);
|
||||
@@ -740,6 +793,87 @@ mod tests {
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn windows_event_log_fixture_scope_limits_and_exact_24h_preserve_linux_bytes() {
|
||||
let since = DateTime::parse_from_rfc3339("2026-11-14T00:00:00Z")
|
||||
.unwrap()
|
||||
.with_timezone(&Utc);
|
||||
let mut action = Action::QueryLogs {
|
||||
unit: WINDOWS_EVENT_LOG_FIXTURE_SOURCE.into(),
|
||||
since,
|
||||
until: since + chrono::Duration::hours(24),
|
||||
max_lines: 1000,
|
||||
};
|
||||
let caps: Vec<String> = [
|
||||
"windows.base/1",
|
||||
"rmm.sync.v1",
|
||||
"query_logs",
|
||||
WINDOWS_EVENT_LOG_FIXTURE_CAPABILITY,
|
||||
]
|
||||
.into_iter()
|
||||
.map(str::to_string)
|
||||
.collect();
|
||||
assert!(action.validate());
|
||||
assert!(action.available_for("agent_service", &caps));
|
||||
assert!(action.valid_job_limits(10, 1024));
|
||||
assert!(action.valid_job_limits(120, 65536));
|
||||
for (runtime, bytes) in [(9, 1024), (121, 1024), (10, 1023), (10, 65537)] {
|
||||
assert!(!action.valid_job_limits(runtime, bytes));
|
||||
}
|
||||
for i in 0..caps.len() {
|
||||
let mut short = caps.clone();
|
||||
short.remove(i);
|
||||
assert!(!action.available_for("agent_service", &short));
|
||||
}
|
||||
for identity in ["root", "system", "SYSTEM"] {
|
||||
assert!(!action.available_for(identity, &caps));
|
||||
}
|
||||
let mut mixed = caps.clone();
|
||||
mixed.push("linux.base/1".into());
|
||||
assert!(!action.available_for("agent_service", &mixed));
|
||||
assert!(!action.available_for("root", &["linux.base/1".into(), "query_logs".into()]));
|
||||
for unit in [
|
||||
"Application",
|
||||
"Security",
|
||||
"windows.eventlog.application.other",
|
||||
] {
|
||||
let mut wrong = action.clone();
|
||||
if let Action::QueryLogs { unit: u, .. } = &mut wrong {
|
||||
*u = unit.into();
|
||||
}
|
||||
assert!(!wrong.available_for("agent_service", &caps));
|
||||
}
|
||||
if let Action::QueryLogs { until, .. } = &mut action {
|
||||
*until += chrono::Duration::nanoseconds(1);
|
||||
}
|
||||
assert!(!action.validate());
|
||||
let linux = Action::QueryLogs {
|
||||
unit: "rmm-test.service".into(),
|
||||
since,
|
||||
until: since + chrono::Duration::hours(24),
|
||||
max_lines: 1000,
|
||||
};
|
||||
assert_eq!(
|
||||
String::from_utf8(encode(&linux).unwrap()).unwrap(),
|
||||
r#"{"action":"query_logs","unit":"rmm-test.service","since":"2026-11-14T00:00:00Z","until":"2026-11-15T00:00:00Z","max_lines":1000}"#
|
||||
);
|
||||
assert!(linux.validate());
|
||||
for identity in ["root", "agent_service"] {
|
||||
assert!(linux.available_for(identity, &["linux.base/1".into(), "query_logs".into()]));
|
||||
}
|
||||
let mut too_long = linux;
|
||||
if let Action::QueryLogs { until, .. } = &mut too_long {
|
||||
*until += chrono::Duration::minutes(59);
|
||||
}
|
||||
assert!(!too_long.validate());
|
||||
let pre_epoch = Action::QueryLogs {
|
||||
unit: WINDOWS_EVENT_LOG_FIXTURE_SOURCE.into(),
|
||||
since: DateTime::from_timestamp(-1, 0).unwrap(),
|
||||
until: DateTime::from_timestamp(0, 0).unwrap(),
|
||||
max_lines: 1,
|
||||
};
|
||||
assert!(!pre_epoch.validate());
|
||||
}
|
||||
#[test]
|
||||
fn no_terminal_replay_or_unknown_restart() {
|
||||
assert!(!JobState::Succeeded.allows(JobState::Running));
|
||||
assert!(!JobState::Unknown.allows(JobState::Running));
|
||||
|
||||
Reference in New Issue
Block a user