feat: add approval bound full maintenance script console and executors
Rust development slice / linux (push) Failing after 6s
Rust development slice / linux (push) Failing after 6s
This commit is contained in:
+240
-3
@@ -48,6 +48,61 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
/// Explicit authority for a separately qualified, fully privileged script.
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, schemars::JsonSchema)]
|
||||
pub enum PrivilegedScriptProfile {
|
||||
#[serde(rename = "full_maintenance_v1")]
|
||||
FullMaintenanceV1,
|
||||
}
|
||||
pub const FULL_MAINTENANCE_CAPABILITY: &str = "script.full_maintenance/1";
|
||||
pub fn deserialize_privileged_script_profile<'de, D>(
|
||||
d: D,
|
||||
) -> Result<Option<PrivilegedScriptProfile>, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
let value = String::deserialize(d)?;
|
||||
match value.as_str() {
|
||||
"full_maintenance_v1" => Ok(Some(PrivilegedScriptProfile::FullMaintenanceV1)),
|
||||
_ => Err(serde::de::Error::unknown_variant(
|
||||
&value,
|
||||
&["full_maintenance_v1"],
|
||||
)),
|
||||
}
|
||||
}
|
||||
/// Optional metadata pins preserve old codecs when missing, reject null or
|
||||
/// noncanonical digests when explicitly supplied.
|
||||
pub fn deserialize_optional_sha256<'de, D>(d: D) -> Result<Option<String>, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
let value = String::deserialize(d)?;
|
||||
if value.len() != 64
|
||||
|| !value
|
||||
.bytes()
|
||||
.all(|b| b.is_ascii_digit() || b"abcdef".contains(&b))
|
||||
{
|
||||
return Err(serde::de::Error::custom("lowercase SHA256 required"));
|
||||
}
|
||||
Ok(Some(value))
|
||||
}
|
||||
|
||||
/// Policy lists are strict when present; omission is deny, never a wildcard.
|
||||
pub fn deserialize_script_profiles<'de, D>(
|
||||
d: D,
|
||||
) -> Result<Option<Vec<PrivilegedScriptProfile>>, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
let values = Vec::<String>::deserialize(d)?;
|
||||
if values.len() != 1 || values[0] != "full_maintenance_v1" {
|
||||
return Err(serde::de::Error::custom(
|
||||
"exact full_maintenance_v1 profile required",
|
||||
));
|
||||
}
|
||||
Ok(Some(vec![PrivilegedScriptProfile::FullMaintenanceV1]))
|
||||
}
|
||||
|
||||
/// Missing preserves Linux codecs; explicit null/nonobject is never a manifest.
|
||||
pub fn deserialize_windows_msi_manifest<'de, D>(
|
||||
deserializer: D,
|
||||
@@ -74,6 +129,13 @@ pub enum Action {
|
||||
)]
|
||||
#[schemars(with = "WindowsExecutionProfile")]
|
||||
windows_execution_profile: Option<WindowsExecutionProfile>,
|
||||
#[serde(
|
||||
default,
|
||||
deserialize_with = "deserialize_privileged_script_profile",
|
||||
skip_serializing_if = "Option::is_none"
|
||||
)]
|
||||
#[schemars(with = "PrivilegedScriptProfile")]
|
||||
privileged_script_profile: Option<PrivilegedScriptProfile>,
|
||||
},
|
||||
RefreshInventory {
|
||||
scope: String,
|
||||
@@ -160,6 +222,7 @@ impl Action {
|
||||
arguments,
|
||||
expected_stdout,
|
||||
windows_execution_profile,
|
||||
..
|
||||
} => {
|
||||
matches!(interpreter.as_str(), "posix_sh" | "bash" | "powershell")
|
||||
&& (windows_execution_profile.is_none() || interpreter == "powershell")
|
||||
@@ -296,6 +359,26 @@ impl Action {
|
||||
(matches!(self,Self::SetServiceState{state,..} if matches!(state.as_str(),"running"|"stopped"))
|
||||
&& self.validate())
|
||||
|| (identity == "system" && self.windows_msi().is_some() && self.validate())
|
||||
|| (identity == "system"
|
||||
&& self.full_maintenance()
|
||||
&& self.script_platform() == Some("windows")
|
||||
&& self.validate())
|
||||
}
|
||||
pub fn full_maintenance(&self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
Self::RunScript {
|
||||
privileged_script_profile: Some(PrivilegedScriptProfile::FullMaintenanceV1),
|
||||
..
|
||||
}
|
||||
)
|
||||
}
|
||||
pub fn valid_execution_identity(&self, identity: &str) -> bool {
|
||||
!self.full_maintenance()
|
||||
|| matches!(
|
||||
(identity, self.script_platform()),
|
||||
("root", Some("linux")) | ("system", Some("windows"))
|
||||
)
|
||||
}
|
||||
pub fn is_windows_event_log_fixture(&self) -> bool {
|
||||
matches!(self, Self::QueryLogs { unit, .. } if unit == WINDOWS_EVENT_LOG_FIXTURE_SOURCE)
|
||||
@@ -304,6 +387,14 @@ impl Action {
|
||||
/// 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)
|
||||
&& (!self.full_maintenance()
|
||||
|| (output_bytes <= 65536
|
||||
&& runtime_seconds
|
||||
<= if self.script_platform() == Some("windows") {
|
||||
120
|
||||
} else {
|
||||
300
|
||||
}))
|
||||
&& (self.windows_msi().is_none()
|
||||
|| ((10..=120).contains(&runtime_seconds)
|
||||
&& (1024..=65536).contains(&output_bytes)))
|
||||
@@ -325,11 +416,23 @@ impl Action {
|
||||
}
|
||||
/// Shared plan/revalidation/start gate. Passive Windows never implies job support.
|
||||
pub fn available_for(&self, identity: &str, capabilities: &[String]) -> bool {
|
||||
if identity == "system" && !self.valid_helper_denial_shape(identity) {
|
||||
if !self.valid_execution_identity(identity)
|
||||
|| (identity == "system" && !self.valid_helper_denial_shape(identity))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
let has = |cap: &str| capabilities.iter().any(|c| c == cap);
|
||||
if !has(self.name()) {
|
||||
if !has(self.name())
|
||||
|| (self.full_maintenance()
|
||||
&& !(has(FULL_MAINTENANCE_CAPABILITY)
|
||||
&& has("rmm.sync.v1")
|
||||
&& has(&format!("{}:run_script", identity))
|
||||
&& has(if identity == "root" {
|
||||
"helper.root.v1"
|
||||
} else {
|
||||
"helper.system.v1"
|
||||
})))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
match self {
|
||||
@@ -338,7 +441,7 @@ impl Action {
|
||||
windows_execution_profile,
|
||||
..
|
||||
} if interpreter == "powershell" => {
|
||||
identity == "agent_service"
|
||||
(identity == "agent_service" || (identity == "system" && self.full_maintenance()))
|
||||
&& has("windows.base/1")
|
||||
&& !has("linux.base/1")
|
||||
&& has("rmm.sync.v1")
|
||||
@@ -643,6 +746,7 @@ mod tests {
|
||||
arguments: vec![],
|
||||
expected_stdout: None,
|
||||
windows_execution_profile: None,
|
||||
privileged_script_profile: None,
|
||||
};
|
||||
let mut all = caps;
|
||||
all.extend(["run_script".into(), "script.powershell/1".into()]);
|
||||
@@ -653,6 +757,7 @@ mod tests {
|
||||
arguments: vec![],
|
||||
expected_stdout: None,
|
||||
windows_execution_profile: None,
|
||||
privileged_script_profile: None,
|
||||
};
|
||||
let reboot = Action::Reboot {
|
||||
reason: "fixed test".into(),
|
||||
@@ -675,6 +780,7 @@ mod tests {
|
||||
arguments: vec![],
|
||||
expected_stdout: Some("Hello\r\n".into()),
|
||||
windows_execution_profile: None,
|
||||
privileged_script_profile: None,
|
||||
};
|
||||
let caps: Vec<String> = [
|
||||
"windows.base/1",
|
||||
@@ -744,6 +850,136 @@ mod tests {
|
||||
));
|
||||
}
|
||||
#[test]
|
||||
fn full_maintenance_is_explicit_strict_identity_and_capability_bound() {
|
||||
let legacy = br#"{"action":"run_script","interpreter":"posix_sh","script_utf8":"id\n","arguments":[],"expected_stdout":null}"#;
|
||||
let mut action: Action = serde_json::from_slice(legacy).unwrap();
|
||||
assert_eq!(encode(&action).unwrap(), legacy);
|
||||
for value in [
|
||||
serde_json::Value::Null,
|
||||
serde_json::json!({"FullMaintenanceV1":null}),
|
||||
serde_json::json!("latest"),
|
||||
serde_json::json!(true),
|
||||
] {
|
||||
let mut input: serde_json::Value = serde_json::from_slice(legacy).unwrap();
|
||||
input["privileged_script_profile"] = value;
|
||||
assert!(serde_json::from_value::<Action>(input).is_err());
|
||||
}
|
||||
if let Action::RunScript {
|
||||
privileged_script_profile,
|
||||
..
|
||||
} = &mut action
|
||||
{
|
||||
*privileged_script_profile = Some(PrivilegedScriptProfile::FullMaintenanceV1);
|
||||
}
|
||||
assert!(action.validate());
|
||||
assert!(action.valid_execution_identity("root"));
|
||||
assert!(!action.valid_execution_identity("agent_service"));
|
||||
assert!(!action.valid_execution_identity("system"));
|
||||
assert!(action.valid_job_limits(300, 65536));
|
||||
assert!(!action.valid_job_limits(301, 65536));
|
||||
assert!(!action.valid_job_limits(300, 65537));
|
||||
let caps: Vec<String> = [
|
||||
"linux.base/1",
|
||||
"rmm.sync.v1",
|
||||
"helper.root.v1",
|
||||
"run_script",
|
||||
"root:run_script",
|
||||
FULL_MAINTENANCE_CAPABILITY,
|
||||
]
|
||||
.into_iter()
|
||||
.map(str::to_owned)
|
||||
.collect();
|
||||
assert!(action.available_for("root", &caps));
|
||||
for missing in &caps {
|
||||
assert!(
|
||||
!action.available_for(
|
||||
"root",
|
||||
&caps
|
||||
.iter()
|
||||
.filter(|c| *c != missing)
|
||||
.cloned()
|
||||
.collect::<Vec<_>>()
|
||||
)
|
||||
);
|
||||
}
|
||||
assert!(!action.available_for("agent_service", &caps));
|
||||
if let Action::RunScript {
|
||||
interpreter,
|
||||
script_utf8,
|
||||
windows_execution_profile,
|
||||
..
|
||||
} = &mut action
|
||||
{
|
||||
*interpreter = "powershell".into();
|
||||
*script_utf8 = "Write-Output 'hi'\r\n".into();
|
||||
*windows_execution_profile = Some(WindowsExecutionProfile::PowerShell51RemoteSigned);
|
||||
}
|
||||
assert!(action.valid_helper_denial_shape("system"));
|
||||
assert!(!action.valid_helper_denial_shape("root"));
|
||||
assert!(action.valid_job_limits(120, 65536));
|
||||
assert!(!action.valid_job_limits(121, 65536));
|
||||
let caps: Vec<String> = [
|
||||
"windows.base/1",
|
||||
"rmm.sync.v1",
|
||||
"helper.system.v1",
|
||||
"run_script",
|
||||
"system:run_script",
|
||||
"script.powershell/1",
|
||||
"script.powershell.process_remote_signed/1",
|
||||
FULL_MAINTENANCE_CAPABILITY,
|
||||
]
|
||||
.into_iter()
|
||||
.map(str::to_owned)
|
||||
.collect();
|
||||
assert!(action.available_for("system", &caps));
|
||||
for missing in &caps {
|
||||
assert!(
|
||||
!action.available_for(
|
||||
"system",
|
||||
&caps
|
||||
.iter()
|
||||
.filter(|c| *c != missing)
|
||||
.cloned()
|
||||
.collect::<Vec<_>>()
|
||||
)
|
||||
);
|
||||
}
|
||||
assert!(!action.available_for("root", &caps));
|
||||
}
|
||||
#[test]
|
||||
fn script_console_golden_plans_use_actual_rust_codec() {
|
||||
let cases: Vec<serde_json::Value> = serde_json::from_str(include_str!(
|
||||
"../../../tests/script_console/codec-golden.json"
|
||||
))
|
||||
.unwrap();
|
||||
assert_eq!(cases.len(), 3);
|
||||
for case in cases {
|
||||
let bytes: Vec<u8> =
|
||||
serde_json::from_value(case["plan_payload_bytes"].clone()).unwrap();
|
||||
let plan: PlanPayload = serde_json::from_slice(&bytes).unwrap();
|
||||
assert_eq!(encode(&plan).unwrap(), bytes);
|
||||
assert_eq!(hash(&bytes), case["plan_hash"]);
|
||||
let job = &plan.jobs[0];
|
||||
let job_bytes = encode(&job.payload).unwrap();
|
||||
assert_eq!(
|
||||
serde_json::to_value(&job_bytes).unwrap(),
|
||||
case["job_payload_bytes"]
|
||||
);
|
||||
assert_eq!(hash(&job_bytes), job.job_payload_hash);
|
||||
assert_eq!(job.job_payload_hash, case["job_payload_hash"]);
|
||||
assert!(job.payload.action.validate());
|
||||
assert!(
|
||||
job.payload
|
||||
.action
|
||||
.valid_execution_identity(&job.payload.execution_identity)
|
||||
);
|
||||
let Action::RunScript { script_utf8, .. } = &job.payload.action else {
|
||||
panic!("script fixture")
|
||||
};
|
||||
assert_eq!(hash(script_utf8.as_bytes()), case["script_sha256"]);
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn codec_golden_and_exact_script_bytes() {
|
||||
let a = Action::RunScript {
|
||||
interpreter: "posix_sh".into(),
|
||||
@@ -751,6 +987,7 @@ mod tests {
|
||||
arguments: vec![],
|
||||
expected_stdout: None,
|
||||
windows_execution_profile: None,
|
||||
privileged_script_profile: None,
|
||||
};
|
||||
assert_eq!(
|
||||
String::from_utf8(encode(&a).unwrap()).unwrap(),
|
||||
|
||||
Reference in New Issue
Block a user