//! Separate deny-only opt-in. Legacy Root/SCM/MSI/script policies grant no update. use crate::jobs::Error; use axum::http::StatusCode; use chrono::{DateTime, Utc}; use rmm_domain::{ agent_update::{ResolvedAgentUpdate, digest}, jobs::{Action, JobPayload}, }; use serde::Deserialize; use sqlx::{Postgres, Transaction}; use uuid::Uuid; #[derive(Deserialize)] #[serde(deny_unknown_fields)] struct Policy { schema_version: u32, profile: String, expires_at: DateTime, scopes: Vec, } #[derive(Deserialize)] #[serde(deny_unknown_fields)] struct Scope { tenant_id: Uuid, asset_id: Uuid, agent_id: Uuid, enrollment_generation: u64, assignment_version: u64, principals: Vec, helper_policy_sha256: String, helper_binary_sha256: String, updater_binary_sha256: String, updater_policy_sha256: String, keyring_generation: u64, keyring_sha256: String, health_public_key_hex: String, health_key_pin_sha256: String, allowed_manifest_sha256: Vec, allowed_predecessor_record_sha256: Vec, acceptance_evidence_id: String, } impl Scope { fn valid(&self) -> bool { !self.tenant_id.is_nil() && !self.asset_id.is_nil() && !self.agent_id.is_nil() && self.enrollment_generation > 0 && self.assignment_version > 0 && self.keyring_generation > 0 && !self.principals.is_empty() && self.principals.len() <= 16 && self .principals .iter() .all(|p| !p.is_empty() && p.len() <= 256) && [ &self.helper_policy_sha256, &self.helper_binary_sha256, &self.updater_binary_sha256, &self.updater_policy_sha256, &self.keyring_sha256, &self.health_public_key_hex, &self.health_key_pin_sha256, ] .iter() .all(|h| digest(h)) && [ &self.allowed_manifest_sha256, &self.allowed_predecessor_record_sha256, ] .iter() .all(|list| { !list.is_empty() && list.len() <= 16 && list.iter().all(|h| digest(h)) && list.iter().collect::>().len() == list.len() }) && !self.acceptance_evidence_id.trim().is_empty() && self.acceptance_evidence_id.len() <= 512 } fn permits( &self, principal: &str, job: &JobPayload, update: &ResolvedAgentUpdate, caps: &[String], ) -> bool { if !self.valid() || !update.validate() || !job.action.available_for(&job.execution_identity, caps) || !job .action .valid_job_limits(job.max_runtime_seconds, job.max_output_bytes) || update.execution_identity() != Some(job.execution_identity.as_str()) || self.tenant_id != job.tenant_id || self.asset_id != job.asset_id || self.agent_id != job.agent_id || self.enrollment_generation != job.enrollment_generation || self.assignment_version != job.assignment_version || !self.principals.iter().any(|p| p == principal) || self.helper_binary_sha256 != update.predecessor.helper_sha256 || self.updater_binary_sha256 != update.updater.binary_sha256 || self.updater_policy_sha256 != update.updater.policy_sha256 || self.keyring_generation != update.updater.keyring_generation || self.keyring_sha256 != update.updater.keyring_sha256 || self.health_public_key_hex != update.updater.health_public_key_hex || self.health_key_pin_sha256 != update.updater.health_key_pin_sha256 || !self .allowed_manifest_sha256 .contains(&update.manifest_sha256) || !self .allowed_predecessor_record_sha256 .contains(&update.predecessor.installed_record_sha256) { return false; } let helper = match job.execution_identity.as_str() { "root" => "helper.root.v1", "system" => "helper.system.v1", _ => return false, }; [ "update_agent".into(), "agent.update.apply/1".into(), "rmm.sync.v1".into(), helper.into(), format!("{}:update_agent", job.execution_identity), format!("helper.policy.sha256:{}", self.helper_policy_sha256), format!("helper.binary.sha256:{}", self.helper_binary_sha256), format!("updater.binary.sha256:{}", self.updater_binary_sha256), format!("updater.policy.sha256:{}", self.updater_policy_sha256), ] .iter() .all(|needed| caps.contains(needed)) } } pub async fn authorize( tx: &mut Transaction<'_, Postgres>, principal: &str, job: &JobPayload, ) -> Result<(), Error> { let Action::UpdateAgent { update } = &job.action else { return Err(Error(StatusCode::FORBIDDEN, "AGENT_UPDATE_POLICY_DENIED")); }; let identity = crate::root_policy::execution_permission(&job.execution_identity) .ok_or(Error(StatusCode::FORBIDDEN, "EXECUTION_IDENTITY_DENIED"))?; crate::permissions::require(tx, principal, identity).await?; crate::permissions::require(tx, principal, "updates:apply").await?; if std::env::var("RMM_PROFILE").as_deref() != Ok("development") { return Err(Error(StatusCode::CONFLICT, "AGENT_UPDATE_NOT_CONFIGURED")); } let path = std::env::var("RMM_AGENT_UPDATE_POLICY_FILE") .map_err(|_| Error(StatusCode::CONFLICT, "AGENT_UPDATE_NOT_CONFIGURED"))?; let raw = crate::agent_updates::protected_file(std::path::Path::new(&path), 65536)?; let policy: Policy = serde_json::from_slice(&raw) .map_err(|_| Error(StatusCode::CONFLICT, "AGENT_UPDATE_POLICY_INVALID"))?; if policy.schema_version != 1 || policy.profile != "development" || policy.expires_at <= Utc::now() || policy.scopes.is_empty() || policy.scopes.len() > 64 || policy.scopes.iter().any(|s| !s.valid()) { return Err(Error(StatusCode::CONFLICT, "AGENT_UPDATE_POLICY_DENIED")); } 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 = serde_json::from_value(caps)?; if !policy .scopes .iter() .any(|scope| scope.permits(principal, job, update, &caps)) { return Err(Error(StatusCode::FORBIDDEN, "AGENT_UPDATE_POLICY_DENIED")); } crate::agent_updates::check_update(tx, update).await?; crate::agent_updates::validate_current_target(tx, job).await?; Ok(()) }