Bind approved Windows PowerShell process policy and prove Linux-first contracts
Rust development slice / linux (push) Failing after 6s
Rust development slice / linux (push) Failing after 6s
This commit is contained in:
@@ -518,6 +518,7 @@ mod tests {
|
||||
script_utf8: "printf hello\\n".into(),
|
||||
arguments: vec![],
|
||||
expected_stdout: None,
|
||||
windows_execution_profile: None,
|
||||
},
|
||||
execution_identity: "agent_service".into(),
|
||||
not_before: c.issued_at,
|
||||
|
||||
@@ -191,6 +191,7 @@ pub async fn event(
|
||||
ensure!(r.stdout == expected.as_bytes(), "expected output mismatch");
|
||||
}
|
||||
}
|
||||
script_profile_evidence(&payload.action, &evidence)?;
|
||||
if matches!(payload.action, Action::Reboot { .. }) {
|
||||
crate::synchronization::verify_reboot_success(
|
||||
tx, tenant, agent, e, &payload, r, &evidence,
|
||||
@@ -224,6 +225,74 @@ pub async fn event(
|
||||
.await?;
|
||||
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 {
|
||||
script_utf8,
|
||||
windows_execution_profile: Some(WindowsExecutionProfile::PowerShell51RemoteSigned),
|
||||
..
|
||||
} = action
|
||||
{
|
||||
ensure!(
|
||||
evidence.get("windows_execution_profile")
|
||||
== Some(&json!("powershell_51_remote_signed"))
|
||||
&& evidence.get("execution_policy")
|
||||
== Some(&json!("process_remote_signed_requested"))
|
||||
&& evidence.get("execution_policy_requested") == Some(&json!("RemoteSigned"))
|
||||
&& evidence.get("execution_policy_scope") == Some(&json!("process"))
|
||||
&& evidence.get("script_sha256") == Some(&json!(hash(script_utf8.as_bytes()))),
|
||||
"requested Windows process profile evidence mismatch"
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod script_profile_tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn requested_profile_success_evidence_is_exact_without_effective_policy_claim() {
|
||||
let mut action = Action::RunScript {
|
||||
interpreter: "powershell".into(),
|
||||
script_utf8: "Write-Output 'hi'\r\n".into(),
|
||||
arguments: vec![],
|
||||
expected_stdout: None,
|
||||
windows_execution_profile: Some(WindowsExecutionProfile::PowerShell51RemoteSigned),
|
||||
};
|
||||
let evidence = json!({"windows_execution_profile":"powershell_51_remote_signed",
|
||||
"execution_policy":"process_remote_signed_requested", "execution_policy_requested":"RemoteSigned",
|
||||
"execution_policy_scope":"process", "script_sha256":hash(b"Write-Output 'hi'\r\n")});
|
||||
script_profile_evidence(&action, &evidence).unwrap();
|
||||
for field in [
|
||||
"windows_execution_profile",
|
||||
"execution_policy",
|
||||
"execution_policy_requested",
|
||||
"execution_policy_scope",
|
||||
"script_sha256",
|
||||
] {
|
||||
let mut changed = evidence.clone();
|
||||
changed.as_object_mut().unwrap().remove(field);
|
||||
assert!(
|
||||
script_profile_evidence(&action, &changed).is_err(),
|
||||
"{field}"
|
||||
);
|
||||
changed[field] = json!("different");
|
||||
assert!(
|
||||
script_profile_evidence(&action, &changed).is_err(),
|
||||
"{field}"
|
||||
);
|
||||
}
|
||||
if let Action::RunScript {
|
||||
windows_execution_profile,
|
||||
..
|
||||
} = &mut action
|
||||
{
|
||||
*windows_execution_profile = None;
|
||||
}
|
||||
script_profile_evidence(&action, &json!({})).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
async fn locked_job(
|
||||
tx: &mut Transaction<'_, Postgres>,
|
||||
agent: Uuid,
|
||||
|
||||
+154
-1
@@ -151,6 +151,12 @@ pub struct Variant {
|
||||
interpreter: String,
|
||||
encoding: String,
|
||||
script_utf8: String,
|
||||
#[serde(
|
||||
default,
|
||||
deserialize_with = "rmm_domain::jobs::deserialize_windows_execution_profile",
|
||||
skip_serializing_if = "Option::is_none"
|
||||
)]
|
||||
windows_execution_profile: Option<WindowsExecutionProfile>,
|
||||
}
|
||||
#[derive(Clone, Deserialize, Serialize, schemars::JsonSchema)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
@@ -213,6 +219,7 @@ impl PlanRequest {
|
||||
script_utf8: v.script_utf8.clone(),
|
||||
arguments: p.arguments.clone().unwrap_or_default(),
|
||||
expected_stdout: p.expected_stdout.clone(),
|
||||
windows_execution_profile: v.windows_execution_profile,
|
||||
}
|
||||
}
|
||||
"refresh_inventory" => Action::RefreshInventory {
|
||||
@@ -310,6 +317,7 @@ fn validate_variants(variants: &[Variant]) -> Result<()> {
|
||||
script_utf8: v.script_utf8.clone(),
|
||||
arguments: vec![],
|
||||
expected_stdout: None,
|
||||
windows_execution_profile: v.windows_execution_profile,
|
||||
};
|
||||
if v.encoding != "utf-8"
|
||||
|| action.script_platform() != Some(v.os_family.as_str())
|
||||
@@ -336,12 +344,21 @@ fn target_action(input: &PlanInput, caps: &[String]) -> Result<Action> {
|
||||
script_utf8,
|
||||
arguments,
|
||||
expected_stdout,
|
||||
windows_execution_profile,
|
||||
} = &input.action
|
||||
else {
|
||||
return Err(fail("VALIDATION_ERROR"));
|
||||
};
|
||||
let first = &input.script_variants[0];
|
||||
if interpreter != &first.interpreter || script_utf8 != &first.script_utf8 {
|
||||
if interpreter != &first.interpreter
|
||||
|| script_utf8 != &first.script_utf8
|
||||
|| windows_execution_profile != &first.windows_execution_profile
|
||||
|| (input.execution_identity != "agent_service"
|
||||
&& input
|
||||
.script_variants
|
||||
.iter()
|
||||
.any(|v| v.windows_execution_profile.is_some()))
|
||||
{
|
||||
return Err(fail("VALIDATION_ERROR"));
|
||||
}
|
||||
let platform = match (
|
||||
@@ -362,6 +379,7 @@ fn target_action(input: &PlanInput, caps: &[String]) -> Result<Action> {
|
||||
script_utf8: v.script_utf8.clone(),
|
||||
arguments: arguments.clone(),
|
||||
expected_stdout: expected_stdout.clone(),
|
||||
windows_execution_profile: v.windows_execution_profile,
|
||||
})
|
||||
}
|
||||
async fn policy(tx: &mut Transaction<'_, Postgres>) -> Result<()> {
|
||||
@@ -1035,6 +1053,72 @@ mod windows_contract_tests {
|
||||
i.script_variants.remove(1);
|
||||
assert!(target_action(&i, &["windows.base/1".into()]).is_err());
|
||||
}
|
||||
#[test]
|
||||
fn remote_signed_fixed_variants_bind_profile_without_linux_inheritance() {
|
||||
let mut i = input();
|
||||
let legacy_linux = encode(&i.action).unwrap();
|
||||
i.script_variants[1].windows_execution_profile =
|
||||
Some(WindowsExecutionProfile::PowerShell51RemoteSigned);
|
||||
assert_eq!(
|
||||
encode(&target_action(&i, &["linux.base/1".into()]).unwrap()).unwrap(),
|
||||
legacy_linux
|
||||
);
|
||||
let windows = target_action(&i, &["windows.base/1".into()]).unwrap();
|
||||
assert!(matches!(
|
||||
windows,
|
||||
Action::RunScript {
|
||||
windows_execution_profile: Some(WindowsExecutionProfile::PowerShell51RemoteSigned),
|
||||
..
|
||||
}
|
||||
));
|
||||
assert!(!windows.available_for(
|
||||
"agent_service",
|
||||
&[
|
||||
"windows.base/1".into(),
|
||||
"rmm.sync.v1".into(),
|
||||
"run_script".into(),
|
||||
"script.powershell/1".into()
|
||||
]
|
||||
));
|
||||
for identity in ["root", "system"] {
|
||||
i.execution_identity = identity.into();
|
||||
assert!(target_action(&i, &["linux.base/1".into()]).is_err());
|
||||
}
|
||||
i.execution_identity = "agent_service".into();
|
||||
// Even the first Action must match the optional profile byte-for-byte.
|
||||
i.script_variants.reverse();
|
||||
i.action = windows;
|
||||
assert!(target_action(&i, &["windows.base/1".into()]).is_ok());
|
||||
if let Action::RunScript {
|
||||
windows_execution_profile,
|
||||
..
|
||||
} = &mut i.action
|
||||
{
|
||||
*windows_execution_profile = None;
|
||||
}
|
||||
assert!(target_action(&i, &["windows.base/1".into()]).is_err());
|
||||
i.script_variants[1].windows_execution_profile =
|
||||
Some(WindowsExecutionProfile::PowerShell51RemoteSigned);
|
||||
assert!(validate_variants(&i.script_variants).is_err());
|
||||
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());
|
||||
assert_eq!(serde_json::to_value(inherited).unwrap(), legacy);
|
||||
for profile in [
|
||||
Value::Null,
|
||||
json!(false),
|
||||
json!(1),
|
||||
json!([]),
|
||||
json!({}),
|
||||
json!({"powershell_51_remote_signed":null}),
|
||||
json!("bypass"),
|
||||
json!("powershell_7_remote_signed"),
|
||||
] {
|
||||
assert!(serde_json::from_value::<Variant>(json!({"os_family":"windows", "interpreter":"powershell", "encoding":"utf-8", "script_utf8":"hi", "windows_execution_profile":profile})).is_err());
|
||||
assert!(serde_json::from_value::<WebParameters>(json!({"execution_identity":"agent_service", "timeout_seconds":60, "max_output_bytes":4096, "windows_execution_profile":profile})).is_err());
|
||||
}
|
||||
assert!(serde_json::from_value::<WebParameters>(json!({"execution_identity":"agent_service", "timeout_seconds":60, "max_output_bytes":4096, "windows_execution_profile":"powershell_51_remote_signed"})).is_err());
|
||||
}
|
||||
#[tokio::test]
|
||||
#[ignore = "requires dedicated isolated PostgreSQL; tests/windows_plan_database.py"]
|
||||
async fn windows_plan_caps_persisted_database() {
|
||||
@@ -1175,6 +1259,75 @@ mod windows_contract_tests {
|
||||
"PLAN_HASH_MISMATCH"
|
||||
);
|
||||
tx.rollback().await.unwrap();
|
||||
// A process-profile plan needs the additional current capability. Its
|
||||
// presence is rechecked at submit/approval just like interpreter support.
|
||||
let mut profiled = request.clone();
|
||||
profiled.script_variants[1].windows_execution_profile =
|
||||
Some(WindowsExecutionProfile::PowerShell51RemoteSigned);
|
||||
assert_eq!(
|
||||
plan(&state, &principal, tenant, profiled.clone())
|
||||
.await
|
||||
.unwrap_err()
|
||||
.1,
|
||||
"UNSUPPORTED_CAPABILITY"
|
||||
);
|
||||
let mut profile_caps = windows_caps.as_array().unwrap().clone();
|
||||
profile_caps.push(json!("script.powershell.process_remote_signed/1"));
|
||||
sqlx::query("UPDATE agent_installations SET capabilities=$2 WHERE agent_id=$1")
|
||||
.bind(agents[1])
|
||||
.bind(json!(profile_caps))
|
||||
.execute(&admin)
|
||||
.await
|
||||
.unwrap();
|
||||
let view = plan(&state, &principal, tenant, profiled).await.unwrap();
|
||||
let profile_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(profile_id)
|
||||
.fetch_one(&admin)
|
||||
.await
|
||||
.unwrap();
|
||||
let profile_bound: PlanPayload = serde_json::from_slice(&bytes).unwrap();
|
||||
let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant)
|
||||
.await
|
||||
.unwrap();
|
||||
recheck(&mut tx, &profile_bound).await.unwrap();
|
||||
tx.rollback().await.unwrap();
|
||||
let mut changed = profile_bound.clone();
|
||||
let windows_job = changed
|
||||
.jobs
|
||||
.iter_mut()
|
||||
.find(|j| j.payload.asset_id == assets[1])
|
||||
.unwrap();
|
||||
if let Action::RunScript {
|
||||
windows_execution_profile,
|
||||
..
|
||||
} = &mut windows_job.payload.action
|
||||
{
|
||||
*windows_execution_profile = None;
|
||||
}
|
||||
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();
|
||||
sqlx::query("UPDATE agent_installations SET capabilities=$2 WHERE agent_id=$1")
|
||||
.bind(agents[1])
|
||||
.bind(&windows_caps)
|
||||
.execute(&admin)
|
||||
.await
|
||||
.unwrap();
|
||||
let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
recheck(&mut tx, &profile_bound).await.unwrap_err().1,
|
||||
"UNSUPPORTED_CAPABILITY"
|
||||
);
|
||||
tx.rollback().await.unwrap();
|
||||
request.script_variants.clear();
|
||||
assert_eq!(
|
||||
plan(&state, &principal, tenant, request.clone())
|
||||
|
||||
@@ -356,6 +356,7 @@ mod tests {
|
||||
script_utf8: "true\n".into(),
|
||||
arguments: vec![],
|
||||
expected_stdout: None,
|
||||
windows_execution_profile: None,
|
||||
},
|
||||
execution_identity: "root".into(),
|
||||
not_before: now,
|
||||
|
||||
@@ -393,6 +393,7 @@ mod tests {
|
||||
script_utf8: "Write-Output 'hello'".into(),
|
||||
arguments: vec![],
|
||||
expected_stdout: None,
|
||||
windows_execution_profile: None,
|
||||
};
|
||||
assert!(!policy.permits("selected", &job, &caps, now));
|
||||
job.action = Action::SetServiceState {
|
||||
|
||||
Reference in New Issue
Block a user