Authorize scoped MCP script requests on opted-in managed devices
Rust development slice / linux (push) Failing after 16s
Rust development slice / linux (push) Failing after 16s
This commit is contained in:
@@ -93,9 +93,26 @@ fn invalid() -> Error {
|
||||
Error(StatusCode::CONFLICT, "MANAGED_SCRIPT_POLICY_INVALID")
|
||||
}
|
||||
|
||||
// Only a managed device scope can opt in. The authenticated principal remains
|
||||
// the token ID for authorization, the plan record and audit.
|
||||
const SCOPED_MCP: &str = "mcp:scoped";
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
enum HelperBinding {
|
||||
LocalPins,
|
||||
VerifiedInstalledRelease,
|
||||
}
|
||||
|
||||
impl Policy {
|
||||
fn permits(&self, principal: &str, job: &JobPayload, caps: &[String]) -> bool {
|
||||
if !matches!(job.action, Action::RunScript { .. })
|
||||
fn permits(
|
||||
&self,
|
||||
principal: &str,
|
||||
job: &JobPayload,
|
||||
caps: &[String],
|
||||
binding: HelperBinding,
|
||||
) -> bool {
|
||||
if principal == SCOPED_MCP
|
||||
|| !matches!(job.action, Action::RunScript { .. })
|
||||
|| !job.action.full_maintenance()
|
||||
|| !job.action.validate()
|
||||
|| !job.action.available_for(&job.execution_identity, caps)
|
||||
@@ -105,17 +122,23 @@ impl Policy {
|
||||
{
|
||||
return false;
|
||||
}
|
||||
match job.execution_identity.as_str() {
|
||||
let permits_subject = |subject: &str| match job.execution_identity.as_str() {
|
||||
"root" => self
|
||||
.linux
|
||||
.iter()
|
||||
.any(|scope| scope.permits(principal, job, caps, None)),
|
||||
.any(|scope| scope.permits(subject, job, caps, None)),
|
||||
"system" => self
|
||||
.windows
|
||||
.iter()
|
||||
.any(|scope| scope.permits(principal, job, caps, false, true, false)),
|
||||
.any(|scope| scope.permits(subject, job, caps, false, true, false)),
|
||||
_ => false,
|
||||
}
|
||||
};
|
||||
permits_subject(principal)
|
||||
|| (matches!(binding, HelperBinding::VerifiedInstalledRelease)
|
||||
&& principal.strip_prefix("mcp:").is_some_and(|id| {
|
||||
Uuid::parse_str(id).is_ok_and(|uuid| !uuid.is_nil() && uuid.to_string() == id)
|
||||
})
|
||||
&& permits_subject(SCOPED_MCP))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,12 +213,11 @@ pub(crate) async fn permits(
|
||||
{
|
||||
return Err(invalid());
|
||||
}
|
||||
let direct = policy.permits(principal, job, caps);
|
||||
if !caps
|
||||
.iter()
|
||||
.any(|cap| cap.starts_with("script.helper.release:"))
|
||||
{
|
||||
return Ok(direct);
|
||||
return Ok(policy.permits(principal, job, caps, HelperBinding::LocalPins));
|
||||
}
|
||||
{
|
||||
let release = Uuid::parse_str(single_value(caps, "script.helper.release:")?)
|
||||
@@ -222,7 +244,12 @@ pub(crate) async fn permits(
|
||||
}
|
||||
})?;
|
||||
check_pair(&manifest, version.as_deref(), &job.execution_identity, caps)?;
|
||||
if direct {
|
||||
if policy.permits(
|
||||
principal,
|
||||
job,
|
||||
caps,
|
||||
HelperBinding::VerifiedInstalledRelease,
|
||||
) {
|
||||
return Ok(true);
|
||||
}
|
||||
let current_policy = single_value(caps, "helper.policy.sha256:")?;
|
||||
@@ -241,7 +268,12 @@ pub(crate) async fn permits(
|
||||
});
|
||||
pinned.push(format!("helper.binary.sha256:{}", anchor.helper_sha256));
|
||||
pinned.push(format!("helper.policy.sha256:{original}"));
|
||||
if policy.permits(principal, job, &pinned) {
|
||||
if policy.permits(
|
||||
principal,
|
||||
job,
|
||||
&pinned,
|
||||
HelperBinding::VerifiedInstalledRelease,
|
||||
) {
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
@@ -253,6 +285,117 @@ pub(crate) async fn permits(
|
||||
mod tests {
|
||||
use super::*;
|
||||
use rmm_domain::agent_update::ArtifactDescriptor;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn scoped_mcp_requires_a_device_opt_in_and_the_same_pinned_script_scope() {
|
||||
let token = "mcp:12345678-abcd-4321-b123-123456789abc";
|
||||
let verified = HelperBinding::VerifiedInstalledRelease;
|
||||
let local = HelperBinding::LocalPins;
|
||||
for (platform, identity, interpreter, script) in [
|
||||
("linux", "root", "posix_sh", "true\n"),
|
||||
("windows", "system", "powershell", "Write-Output 'ok'\r\n"),
|
||||
] {
|
||||
let mut job = JobPayload {
|
||||
schema_version: 1,
|
||||
tenant_id: Uuid::new_v4(),
|
||||
asset_id: Uuid::new_v4(),
|
||||
agent_id: Uuid::new_v4(),
|
||||
enrollment_generation: 1,
|
||||
assignment_version: 1,
|
||||
job_id: Uuid::new_v4(),
|
||||
attempt_id: Uuid::new_v4(),
|
||||
action: Action::RunScript {
|
||||
interpreter: interpreter.into(),
|
||||
script_utf8: script.into(),
|
||||
arguments: vec![],
|
||||
expected_stdout: None,
|
||||
windows_execution_profile: None,
|
||||
privileged_script_profile: Some(
|
||||
rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1,
|
||||
),
|
||||
},
|
||||
execution_identity: identity.into(),
|
||||
not_before: chrono::Utc::now(),
|
||||
start_before: chrono::Utc::now() + chrono::Duration::minutes(5),
|
||||
max_runtime_seconds: 30,
|
||||
max_output_bytes: 4096,
|
||||
};
|
||||
let binary = "ab".repeat(32);
|
||||
let local_policy = "cd".repeat(32);
|
||||
let mut caps = vec![
|
||||
format!("{platform}.base/1"),
|
||||
format!("helper.{identity}.v1"),
|
||||
format!("{identity}:run_script"),
|
||||
"run_script".into(),
|
||||
"rmm.sync.v1".into(),
|
||||
FULL_MAINTENANCE_CAPABILITY.into(),
|
||||
format!("helper.policy.sha256:{local_policy}"),
|
||||
format!("helper.binary.sha256:{binary}"),
|
||||
];
|
||||
let mut scope = json!({
|
||||
"tenant_id": job.tenant_id, "asset_id": job.asset_id, "agent_id": job.agent_id,
|
||||
"enrollment_generation": 1, "assignment_version": 1, "principals": ["local:reviewer"],
|
||||
"helper_policy_sha256": local_policy, "helper_binary_sha256": binary,
|
||||
"acceptance_evidence_id": Uuid::new_v4(), "allowed_services": [],
|
||||
"allowed_script_profiles": ["full_maintenance_v1"]
|
||||
});
|
||||
if platform == "linux" {
|
||||
scope["allow_root_scripts"] = json!(true);
|
||||
scope["allow_reboot"] = json!(false);
|
||||
} else {
|
||||
scope["max_runtime_seconds"] = json!(30);
|
||||
scope["allowed_script_sha256"] = json!([rmm_domain::jobs::hash(script.as_bytes())]);
|
||||
caps.push("script.powershell/1".into());
|
||||
}
|
||||
let mut config = json!({"schema_version": 1, "linux": [], "windows": []});
|
||||
config[platform] = json!([scope]);
|
||||
let policy: Policy = serde_json::from_value(config.clone()).unwrap();
|
||||
assert!(policy.permits("local:reviewer", &job, &caps, verified));
|
||||
assert!(!policy.permits(token, &job, &caps, verified));
|
||||
|
||||
config[platform][0]["principals"] = json!(["local:reviewer", SCOPED_MCP]);
|
||||
let policy: Policy = serde_json::from_value(config).unwrap();
|
||||
assert!(policy.permits(token, &job, &caps, verified), "{platform}");
|
||||
assert!(!policy.permits(token, &job, &caps, local));
|
||||
assert!(policy.permits("local:reviewer", &job, &caps, local));
|
||||
assert!(policy.permits("local:reviewer", &job, &caps, verified));
|
||||
for other in [
|
||||
SCOPED_MCP,
|
||||
"local:other",
|
||||
"mcp:not-a-uuid",
|
||||
"mcp:00000000-0000-0000-0000-000000000000",
|
||||
"mcp:12345678ABCD4321B123123456789ABC",
|
||||
] {
|
||||
assert!(
|
||||
!policy.permits(other, &job, &caps, verified),
|
||||
"{platform}: {other}"
|
||||
);
|
||||
}
|
||||
let mut without_binary = caps.clone();
|
||||
without_binary.retain(|cap| !cap.starts_with("helper.binary.sha256:"));
|
||||
assert!(!policy.permits(token, &job, &without_binary, verified));
|
||||
job.assignment_version += 1;
|
||||
assert!(!policy.permits(token, &job, &caps, verified));
|
||||
if platform == "windows" {
|
||||
job.assignment_version -= 1;
|
||||
if let Action::RunScript { arguments, .. } = &mut job.action {
|
||||
arguments.push("other".into());
|
||||
}
|
||||
assert!(!policy.permits(token, &job, &caps, verified));
|
||||
if let Action::RunScript {
|
||||
script_utf8,
|
||||
arguments,
|
||||
..
|
||||
} = &mut job.action
|
||||
{
|
||||
arguments.clear();
|
||||
*script_utf8 = "Write-Output 'changed'\r\n".into();
|
||||
}
|
||||
assert!(!policy.permits(token, &job, &caps, verified));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn signed_helper_is_bound_to_platform_reported_version_and_unambiguous_local_pins() {
|
||||
|
||||
Reference in New Issue
Block a user