Integrate the currently deployed server functionality with the reviewed WebUI
This commit is contained in:
@@ -27,6 +27,7 @@ mod oidc;
|
||||
mod pagination;
|
||||
mod permissions;
|
||||
mod root_policy;
|
||||
mod managed_scripts;
|
||||
mod scheduler;
|
||||
mod script_library;
|
||||
mod synchronization;
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
//! Permanent authorization ceilings for installed, pinned script helpers.
|
||||
//! Plan submission, interactive approval and signed start grants are unchanged.
|
||||
use crate::{jobs::Error, root_policy::RootScope, windows_policy::SystemScope};
|
||||
use axum::http::StatusCode;
|
||||
use rmm_domain::jobs::{Action, JobPayload};
|
||||
use serde::Deserialize;
|
||||
use std::{fs, os::unix::fs::MetadataExt, path::Path};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Policy {
|
||||
schema_version: u8,
|
||||
linux: Vec<RootScope>,
|
||||
windows: Vec<SystemScope>,
|
||||
}
|
||||
|
||||
fn invalid() -> Error {
|
||||
Error(StatusCode::CONFLICT, "MANAGED_SCRIPT_POLICY_INVALID")
|
||||
}
|
||||
|
||||
impl Policy {
|
||||
fn permits(&self, principal: &str, job: &JobPayload, caps: &[String]) -> bool {
|
||||
if !matches!(job.action, Action::RunScript { .. })
|
||||
|| !job.action.full_maintenance()
|
||||
|| !job.action.validate()
|
||||
|| !job.action.available_for(&job.execution_identity, caps)
|
||||
|| !job.action.valid_job_limits(job.max_runtime_seconds, job.max_output_bytes)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
match job.execution_identity.as_str() {
|
||||
"root" => self.linux.iter().any(|scope| scope.permits(principal, job, caps, None)),
|
||||
"system" => self.windows.iter().any(|scope| scope.permits(principal, job, caps, false, true, false)),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn permits(principal: &str, job: &JobPayload, caps: &[String]) -> Result<bool, Error> {
|
||||
if !job.action.full_maintenance() {
|
||||
return Ok(false);
|
||||
}
|
||||
let Some(path) = std::env::var_os("RMM_MANAGED_SCRIPT_POLICY_FILE") else {
|
||||
return Ok(false);
|
||||
};
|
||||
let key = std::env::var_os("RMM_JOB_KEY_FILE").ok_or_else(invalid)?;
|
||||
let signer = fs::symlink_metadata(key).map_err(|_| invalid())?;
|
||||
if !signer.is_file() {
|
||||
return Err(invalid());
|
||||
}
|
||||
let bytes = crate::windows_policy::protected_bytes(Path::new(&path), signer.uid())
|
||||
.map_err(|_| invalid())?;
|
||||
let policy: Policy = serde_json::from_slice(&bytes).map_err(|_| invalid())?;
|
||||
if policy.schema_version != 1
|
||||
|| policy.linux.len() + policy.windows.len() > 64
|
||||
|| !policy.windows.iter().all(SystemScope::valid)
|
||||
{
|
||||
return Err(invalid());
|
||||
}
|
||||
Ok(policy.permits(principal, job, caps))
|
||||
}
|
||||
@@ -126,7 +126,7 @@ fn tools() -> Vec<Tool> {
|
||||
),
|
||||
tool::<PlanScript>(
|
||||
"scripts.plan",
|
||||
"Plan the selected immutable script revision against fixed assets. Submit the returned plan for WebUI approval via operations.submit.",
|
||||
"Plan the selected immutable script revision against fixed assets. Set input.execution_identity to select agent_service, root on Linux or system on Windows. Omitting it uses the revision default. Submit the plan for WebUI approval via operations.submit.",
|
||||
),
|
||||
tool::<Devices>(
|
||||
"updates.list_releases",
|
||||
|
||||
@@ -20,7 +20,7 @@ pub struct RootPolicy {
|
||||
}
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct RootScope {
|
||||
pub(crate) struct RootScope {
|
||||
tenant_id: Uuid,
|
||||
asset_id: Uuid,
|
||||
agent_id: Uuid,
|
||||
@@ -158,7 +158,7 @@ impl RootScope {
|
||||
&& self.enrollment_generation == job.enrollment_generation
|
||||
&& self.assignment_version == job.assignment_version
|
||||
}
|
||||
fn permits(
|
||||
pub(crate) fn permits(
|
||||
&self,
|
||||
principal: &str,
|
||||
job: &JobPayload,
|
||||
@@ -338,6 +338,11 @@ pub async fn authorize(
|
||||
for permission in required_permissions(job, false) {
|
||||
crate::permissions::require(tx, principal, permission).await?;
|
||||
}
|
||||
let caps:serde_json::Value=sqlx::query_scalar("SELECT capabilities FROM agent_installations WHERE agent_id=$1 AND asset_id=$2 AND revoked_at IS NULL AND enrollment_generation=$3 AND assignment_version=$4").bind(job.agent_id).bind(job.asset_id).bind(i64::try_from(job.enrollment_generation).map_err(|_|Error(StatusCode::CONFLICT,"BINDING_MISMATCH"))?).bind(i64::try_from(job.assignment_version).map_err(|_|Error(StatusCode::CONFLICT,"BINDING_MISMATCH"))?).fetch_optional(&mut **tx).await?.ok_or(Error(StatusCode::CONFLICT,"AUTHORIZATION_REVOKED"))?;
|
||||
let caps: Vec<String> = serde_json::from_value(caps)?;
|
||||
if crate::managed_scripts::permits(principal, job, &caps)? {
|
||||
return Ok(());
|
||||
}
|
||||
let policy = load()?;
|
||||
let global_log_units = if matches!(job.action, Action::QueryLogs { .. }) {
|
||||
parse_log_units(std::env::var("RMM_ROOT_LOG_UNITS").ok().as_deref())?
|
||||
@@ -356,8 +361,6 @@ pub async fn authorize(
|
||||
crate::permissions::require(tx, principal, "logs:read_sensitive").await?;
|
||||
}
|
||||
}
|
||||
let caps:serde_json::Value=sqlx::query_scalar("SELECT capabilities FROM agent_installations WHERE agent_id=$1 AND asset_id=$2 AND revoked_at IS NULL AND enrollment_generation=$3 AND assignment_version=$4").bind(job.agent_id).bind(job.asset_id).bind(i64::try_from(job.enrollment_generation).map_err(|_|Error(StatusCode::CONFLICT,"BINDING_MISMATCH"))?).bind(i64::try_from(job.assignment_version).map_err(|_|Error(StatusCode::CONFLICT,"BINDING_MISMATCH"))?).fetch_optional(&mut **tx).await?.ok_or(Error(StatusCode::CONFLICT,"AUTHORIZATION_REVOKED"))?;
|
||||
let caps: Vec<String> = serde_json::from_value(caps)?;
|
||||
if !policy
|
||||
.scopes
|
||||
.iter()
|
||||
|
||||
@@ -38,6 +38,7 @@ pub struct Selection {
|
||||
pub reason: String,
|
||||
pub max_runtime_seconds: u32,
|
||||
pub max_output_bytes: u32,
|
||||
pub execution_identity: Option<String>,
|
||||
}
|
||||
|
||||
fn script(interpreter: &str, identity: &str, body: String) -> Result<Action, Error> {
|
||||
@@ -169,7 +170,9 @@ pub async fn plan(
|
||||
version: row.get("version"),
|
||||
script_sha256: hash(body.as_bytes()),
|
||||
};
|
||||
let identity: String = row.get("execution_identity");
|
||||
let identity = input
|
||||
.execution_identity
|
||||
.unwrap_or_else(|| row.get("execution_identity"));
|
||||
let action = script(row.get("interpreter"), &identity, body)?;
|
||||
let plan = PlanInput {
|
||||
targets: input.targets,
|
||||
@@ -299,6 +302,7 @@ pub(crate) mod tests {
|
||||
reason: "standard diagnostic library contract".into(),
|
||||
max_runtime_seconds: 120,
|
||||
max_output_bytes: 65536,
|
||||
execution_identity: None,
|
||||
};
|
||||
let planned = plan(s, p, tenant, id, selection).await;
|
||||
if identity == "system" {
|
||||
@@ -394,6 +398,7 @@ pub(crate) mod tests {
|
||||
reason: "isolated library contract".into(),
|
||||
max_runtime_seconds: 60,
|
||||
max_output_bytes: 4096,
|
||||
execution_identity: None,
|
||||
};
|
||||
assert_eq!(
|
||||
plan(s, p, tenant, id, selection(windows))
|
||||
|
||||
@@ -25,7 +25,7 @@ struct SystemPolicy {
|
||||
}
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct SystemScope {
|
||||
pub(crate) struct SystemScope {
|
||||
tenant_id: Uuid,
|
||||
asset_id: Uuid,
|
||||
agent_id: Uuid,
|
||||
@@ -76,7 +76,7 @@ fn fixture_service(name: &str) -> bool {
|
||||
.all(|c| c.is_ascii_alphanumeric() || b"_-".contains(&c))
|
||||
}
|
||||
impl SystemScope {
|
||||
fn valid(&self) -> bool {
|
||||
pub(crate) fn valid(&self) -> bool {
|
||||
!self.tenant_id.is_nil()
|
||||
&& !self.asset_id.is_nil()
|
||||
&& !self.agent_id.is_nil()
|
||||
@@ -127,7 +127,7 @@ impl SystemScope {
|
||||
&& rmm_domain::jobs::valid_windows_update_limit(self.windows_update_max_runtime_seconds)
|
||||
&& (1..=120).contains(&self.max_runtime_seconds)
|
||||
}
|
||||
fn permits(
|
||||
pub(crate) fn permits(
|
||||
&self,
|
||||
principal: &str,
|
||||
job: &JobPayload,
|
||||
@@ -293,7 +293,7 @@ fn protected_parent_chain(path: &Path, owner: u32) -> Result<(), Error> {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn protected_bytes(path: &Path, owner: u32) -> Result<Vec<u8>, Error> {
|
||||
pub(crate) fn protected_bytes(path: &Path, owner: u32) -> Result<Vec<u8>, Error> {
|
||||
no_symlink_components(path)?;
|
||||
protected_parent_chain(path, owner)?;
|
||||
let before = fs::symlink_metadata(path).map_err(|_| disabled())?;
|
||||
@@ -374,9 +374,12 @@ pub async fn authorize(
|
||||
if matches!(job.action, Action::RunScript { .. }) {
|
||||
crate::permissions::require(tx, principal, "scripts:execute").await?;
|
||||
}
|
||||
let policy = load()?;
|
||||
let caps:serde_json::Value=sqlx::query_scalar("SELECT capabilities FROM agent_installations WHERE tenant_id=$1 AND agent_id=$2 AND asset_id=$3 AND revoked_at IS NULL AND enrollment_generation=$4 AND assignment_version=$5").bind(job.tenant_id).bind(job.agent_id).bind(job.asset_id).bind(i64::try_from(job.enrollment_generation).map_err(|_|Error(StatusCode::CONFLICT,"BINDING_MISMATCH"))?).bind(i64::try_from(job.assignment_version).map_err(|_|Error(StatusCode::CONFLICT,"BINDING_MISMATCH"))?).fetch_optional(&mut **tx).await?.ok_or(Error(StatusCode::CONFLICT,"AUTHORIZATION_REVOKED"))?;
|
||||
let capabilities: Vec<String> = serde_json::from_value(caps)?;
|
||||
if crate::managed_scripts::permits(principal, job, &capabilities)? {
|
||||
return Ok(());
|
||||
}
|
||||
let policy = load()?;
|
||||
if !policy.permits(principal, job, &capabilities, Utc::now()) {
|
||||
return Err(Error(
|
||||
StatusCode::FORBIDDEN,
|
||||
|
||||
Reference in New Issue
Block a user