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:
@@ -13,6 +13,12 @@ pub fn socket_for(
|
||||
general: Option<OsString>,
|
||||
apt: Option<OsString>,
|
||||
) -> Result<PathBuf> {
|
||||
if job.action.full_maintenance() {
|
||||
return Ok(PathBuf::from(
|
||||
std::env::var_os("RMM_FULL_MAINTENANCE_HELPER_SOCKET")
|
||||
.context("FULL_MAINTENANCE_SOCKET_NOT_CONFIGURED")?,
|
||||
));
|
||||
}
|
||||
select_socket(provider::is_apt_action(job), general, apt)
|
||||
}
|
||||
fn select_socket(
|
||||
@@ -216,6 +222,23 @@ pub fn invoke(binding: &Binding, job: &JobPayload, request: &Request) -> Result<
|
||||
rmm_privileged_helper::invoke(&socket, request, job.max_runtime_seconds)
|
||||
}
|
||||
|
||||
pub fn invoke_with_control(
|
||||
binding: &Binding,
|
||||
job: &JobPayload,
|
||||
request: &Request,
|
||||
observe: impl FnMut() -> Result<()>,
|
||||
) -> Result<Outcome> {
|
||||
if !job.action.full_maintenance() {
|
||||
return invoke(binding, job, request);
|
||||
}
|
||||
ensure!(
|
||||
crate::helper_sync::general_supported(binding, job),
|
||||
"FULL_MAINTENANCE_POLICY_REQUIRED"
|
||||
);
|
||||
let socket = socket_for(job, None, None)?;
|
||||
rmm_privileged_helper::invoke_controlled(&socket, request, job.max_runtime_seconds, observe)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -354,6 +377,7 @@ mod tests {
|
||||
arguments: vec![],
|
||||
expected_stdout: None,
|
||||
windows_execution_profile: None,
|
||||
privileged_script_profile: None,
|
||||
},
|
||||
execution_identity: "root".into(),
|
||||
not_before: now,
|
||||
|
||||
@@ -9,6 +9,7 @@ struct Endpoint {
|
||||
socket: PathBuf,
|
||||
profile: String,
|
||||
hash: String,
|
||||
binary_hash: Option<String>,
|
||||
}
|
||||
#[derive(Clone)]
|
||||
pub struct Helpers {
|
||||
@@ -39,24 +40,44 @@ fn map(s: &root::Snapshot) -> reconcile::HelperSnapshot {
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
fn general(binding: &Binding) -> Result<control::Response> {
|
||||
let socket = std::env::var_os("RMM_HELPER_SOCKET").context("NO_GENERAL_HELPER")?;
|
||||
let path = std::env::var_os("RMM_HELPER_POLICY_FILE").context("GENERAL_POLICY_PIN_REQUIRED")?;
|
||||
fn general(binding: &Binding, full: bool) -> Result<control::Response> {
|
||||
let (socket_name, pin_name, profile) = if full {
|
||||
(
|
||||
"RMM_FULL_MAINTENANCE_HELPER_SOCKET",
|
||||
"RMM_FULL_MAINTENANCE_POLICY_FILE",
|
||||
"full_maintenance_v1",
|
||||
)
|
||||
} else {
|
||||
("RMM_HELPER_SOCKET", "RMM_HELPER_POLICY_FILE", "general")
|
||||
};
|
||||
let socket = std::env::var_os(socket_name).context("NO_GENERAL_HELPER")?;
|
||||
let path = std::env::var_os(pin_name).context("GENERAL_POLICY_PIN_REQUIRED")?;
|
||||
let pin = control::GeneralPin::read(Path::new(&path), binding)?;
|
||||
ensure!(pin.profile == profile, "HELPER_PROFILE_ROUTE_MISMATCH");
|
||||
control::general_probe(Path::new(&socket), binding, &pin)
|
||||
}
|
||||
impl Helpers {
|
||||
pub fn configured(binding: &Binding) -> Result<Self> {
|
||||
let mut endpoints = vec![];
|
||||
if let Some(socket) = std::env::var_os("RMM_HELPER_SOCKET") {
|
||||
let path = std::env::var_os("RMM_HELPER_POLICY_FILE")
|
||||
.context("GENERAL_POLICY_PIN_REQUIRED")?;
|
||||
let pin = control::GeneralPin::read(Path::new(&path), binding)?;
|
||||
endpoints.push(Endpoint {
|
||||
socket: socket.into(),
|
||||
profile: "general".into(),
|
||||
hash: pin.policy_sha256,
|
||||
});
|
||||
for (socket_name, pin_name, profile) in [
|
||||
("RMM_HELPER_SOCKET", "RMM_HELPER_POLICY_FILE", "general"),
|
||||
(
|
||||
"RMM_FULL_MAINTENANCE_HELPER_SOCKET",
|
||||
"RMM_FULL_MAINTENANCE_POLICY_FILE",
|
||||
"full_maintenance_v1",
|
||||
),
|
||||
] {
|
||||
if let Some(socket) = std::env::var_os(socket_name) {
|
||||
let path = std::env::var_os(pin_name).context("GENERAL_POLICY_PIN_REQUIRED")?;
|
||||
let pin = control::GeneralPin::read(Path::new(&path), binding)?;
|
||||
ensure!(pin.profile == profile, "HELPER_PROFILE_ROUTE_MISMATCH");
|
||||
endpoints.push(Endpoint {
|
||||
socket: socket.into(),
|
||||
profile: profile.into(),
|
||||
hash: pin.policy_sha256,
|
||||
binary_hash: pin.helper_binary_sha256,
|
||||
});
|
||||
}
|
||||
}
|
||||
if let Some(socket) = std::env::var_os("RMM_APT_HELPER_SOCKET") {
|
||||
ensure!(
|
||||
@@ -70,6 +91,7 @@ impl Helpers {
|
||||
socket: socket.into(),
|
||||
profile: "apt_only".into(),
|
||||
hash: pin.provider_stack_sha256,
|
||||
binary_hash: None,
|
||||
});
|
||||
}
|
||||
Ok(Self {
|
||||
@@ -79,7 +101,9 @@ impl Helpers {
|
||||
}
|
||||
fn validate(&self, e: &Endpoint, r: &control::Response) -> Result<()> {
|
||||
ensure!(
|
||||
r.profile == e.profile && r.policy_sha256 == e.hash,
|
||||
r.profile == e.profile
|
||||
&& r.policy_sha256 == e.hash
|
||||
&& r.helper_binary_sha256 == e.binary_hash,
|
||||
"HELPER_PIN_PROFILE_MISMATCH"
|
||||
);
|
||||
Ok(())
|
||||
@@ -153,13 +177,39 @@ impl Helpers {
|
||||
}
|
||||
}
|
||||
pub fn general_capabilities(binding: &Binding) -> Vec<String> {
|
||||
let mut caps = profile_capabilities(binding, false);
|
||||
caps.extend(profile_capabilities(binding, true));
|
||||
caps.sort();
|
||||
caps.dedup();
|
||||
caps
|
||||
}
|
||||
fn profile_capabilities(binding: &Binding, full: bool) -> Vec<String> {
|
||||
(|| -> Result<Vec<String>> {
|
||||
let response = general(binding)?;
|
||||
let response = general(binding, full)?;
|
||||
let policy = response.policy.context("NO_GENERAL_POLICY")?;
|
||||
let mut caps = vec![
|
||||
"helper.root.v1".into(),
|
||||
format!("helper.policy.sha256:{}", response.policy_sha256),
|
||||
];
|
||||
if full {
|
||||
ensure!(
|
||||
policy.schema_version == 2
|
||||
&& policy.profile == "full_maintenance_v1"
|
||||
&& policy.allowed_script_profiles.as_deref()
|
||||
== Some(&[rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1]),
|
||||
"FULL_MAINTENANCE_POLICY_REQUIRED"
|
||||
);
|
||||
let binary = policy
|
||||
.helper_binary_sha256
|
||||
.as_ref()
|
||||
.context("FULL_MAINTENANCE_BINARY_PIN_REQUIRED")?;
|
||||
ensure!(
|
||||
response.helper_binary_sha256.as_ref() == Some(binary),
|
||||
"FULL_MAINTENANCE_BINARY_PROOF_REQUIRED"
|
||||
);
|
||||
caps.push(format!("helper.binary.sha256:{binary}"));
|
||||
caps.push(rmm_domain::jobs::FULL_MAINTENANCE_CAPABILITY.into());
|
||||
}
|
||||
let mut actions = vec![];
|
||||
if policy.allow_root_scripts && rmm_agent_linux::actions::Interpreter::PosixSh.available() {
|
||||
actions.push("run_script");
|
||||
@@ -180,10 +230,22 @@ pub fn general_capabilities(binding: &Binding) -> Vec<String> {
|
||||
}
|
||||
pub fn general_supported(binding: &Binding, job: &rmm_domain::jobs::JobPayload) -> bool {
|
||||
(|| -> Result<bool> {
|
||||
let p = general(binding)?.policy.context("NO_GENERAL_POLICY")?;
|
||||
let p = general(binding, job.action.full_maintenance())?
|
||||
.policy
|
||||
.context("NO_GENERAL_POLICY")?;
|
||||
Ok(match &job.action {
|
||||
rmm_domain::jobs::Action::RunScript { interpreter, .. } => {
|
||||
p.allow_root_scripts
|
||||
&& job.action.valid_execution_identity(&job.execution_identity)
|
||||
&& job
|
||||
.action
|
||||
.valid_job_limits(job.max_runtime_seconds, job.max_output_bytes)
|
||||
&& (!job.action.full_maintenance()
|
||||
|| p.allowed_script_profiles.as_ref().is_some_and(|v| {
|
||||
v.contains(
|
||||
&rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1,
|
||||
)
|
||||
}))
|
||||
&& match interpreter.as_str() {
|
||||
"posix_sh" => rmm_agent_linux::actions::Interpreter::PosixSh.available(),
|
||||
"bash" => rmm_agent_linux::actions::Interpreter::Bash.available(),
|
||||
|
||||
@@ -50,6 +50,7 @@ struct Agent<C: reconcile::SyncClock = reconcile::NativeClock> {
|
||||
inventory_tx: tokio::sync::mpsc::Sender<Result<(Vec<u8>, i64)>>,
|
||||
inventory_rx: tokio::sync::mpsc::Receiver<Result<(Vec<u8>, i64)>>,
|
||||
inventory_inflight: bool,
|
||||
running_cancels: std::collections::HashMap<(String, String), Arc<AtomicBool>>,
|
||||
sync: Option<reconcile::ClientSync>,
|
||||
sync_clock: C,
|
||||
helpers: Option<helper_sync::Helpers>,
|
||||
@@ -61,6 +62,7 @@ struct Agent<C: reconcile::SyncClock = reconcile::NativeClock> {
|
||||
}
|
||||
#[derive(Default)]
|
||||
struct EffectFence {
|
||||
running_generation: AtomicU64,
|
||||
generation: AtomicU64,
|
||||
deadline: AtomicI64,
|
||||
root: AtomicBool,
|
||||
@@ -80,6 +82,14 @@ fn report_closed_gate(error: &anyhow::Error) {
|
||||
}
|
||||
impl EffectFence {
|
||||
fn close(&self) {
|
||||
self.running_generation.store(0, Ordering::SeqCst);
|
||||
self.close_starts();
|
||||
}
|
||||
// A signed Sync/start lease authorizes NEW starts. Its natural expiry
|
||||
// cannot retroactively stop an approved bounded process (spec22.3).
|
||||
// Running effects instead observe their exact cancellation and current
|
||||
// connection generation; transport/signed refusal closes both fences.
|
||||
fn close_starts(&self) {
|
||||
self.generation.store(0, Ordering::SeqCst);
|
||||
self.root.store(false, Ordering::SeqCst);
|
||||
}
|
||||
@@ -117,7 +127,7 @@ impl<C: reconcile::SyncClock> Agent<C> {
|
||||
})
|
||||
}
|
||||
async fn start_sync(&mut self, wire: &wire_channel::Handle, generation: u64) -> Result<()> {
|
||||
self.fence.close();
|
||||
self.fence.close_starts();
|
||||
let helpers = self.helpers.clone();
|
||||
let snapshot = tokio::task::spawn_blocking(move || match helpers {
|
||||
Some(h) => h.snapshot(generation),
|
||||
@@ -182,6 +192,9 @@ impl<C: reconcile::SyncClock> Agent<C> {
|
||||
.as_ref()
|
||||
.and_then(|s| s.validity_deadline())
|
||||
.context("SIGNED_LEASE_REQUIRED")?;
|
||||
self.fence
|
||||
.running_generation
|
||||
.store(generation, Ordering::SeqCst);
|
||||
self.fence
|
||||
.deadline
|
||||
.store(deadline.timestamp_millis(), Ordering::SeqCst);
|
||||
@@ -189,6 +202,9 @@ impl<C: reconcile::SyncClock> Agent<C> {
|
||||
.root
|
||||
.store(self.ready(generation, true), Ordering::SeqCst);
|
||||
self.fence.generation.store(generation, Ordering::SeqCst);
|
||||
} else {
|
||||
// A valid signed refusal is a fence too, not just parse/I/O errors.
|
||||
self.fence.close();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -322,6 +338,11 @@ impl<C: reconcile::SyncClock> Agent<C> {
|
||||
}));
|
||||
jobs::event(&tx, &envelope)?;
|
||||
tx.commit()?;
|
||||
let cancellation = Arc::new(AtomicBool::new(false));
|
||||
self.running_cancels.insert(
|
||||
(offer.job_id.clone(), offer.attempt_id.clone()),
|
||||
cancellation.clone(),
|
||||
);
|
||||
let completed = self.completed_tx.clone();
|
||||
let clock = self.clock.clone();
|
||||
let fence = self.fence.clone();
|
||||
@@ -334,7 +355,16 @@ impl<C: reconcile::SyncClock> Agent<C> {
|
||||
{
|
||||
jobs::no_start("failed", "START_WINDOW_OR_CLOCK_INVALID_BEFORE_EFFECT")
|
||||
} else if job.execution_identity == "root" {
|
||||
apt::invoke(&binding, &job, &request).unwrap_or_else(|_| {
|
||||
apt::invoke_with_control(&binding, &job, &request, || {
|
||||
ensure!(
|
||||
!cancellation.load(Ordering::SeqCst)
|
||||
&& fence.running_generation.load(Ordering::SeqCst) == generation
|
||||
&& clock.trusted(),
|
||||
"SCRIPT_RUNNING_CANCEL_OR_FENCE"
|
||||
);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap_or_else(|_| {
|
||||
rmm_privileged_helper::Outcome::unknown("HELPER_TRANSPORT_EFFECT_UNKNOWN")
|
||||
})
|
||||
} else {
|
||||
@@ -537,6 +567,7 @@ impl<C: reconcile::SyncClock> Agent<C> {
|
||||
rusqlite::params![cancel.job_id,cancel.attempt_id], |r| Ok((r.get(0)?,r.get(1)?,r.get(2)?))).optional()?
|
||||
};
|
||||
if let Some((bytes,state,grant)) = row {
|
||||
if let Some(flag) = self.running_cancels.get(&(cancel.job_id.clone(),cancel.attempt_id.clone())) { flag.store(true,Ordering::SeqCst); }
|
||||
self.db.execute("UPDATE job_attempts SET cancel_requested=1 WHERE job=?1 AND attempt=?2", rusqlite::params![cancel.job_id,cancel.attempt_id])?;
|
||||
if state == "accepted" { self.record(generation, &JobOffer::decode(bytes.as_slice())?, &grant, jobs::no_start("cancelled", "CANCELLED_BEFORE_START"))?; }
|
||||
// Running script cancellation is not confirmed by an
|
||||
@@ -549,6 +580,7 @@ impl<C: reconcile::SyncClock> Agent<C> {
|
||||
}
|
||||
}
|
||||
Some((offer, grant, outcome)) = self.completed_rx.recv() => {
|
||||
self.running_cancels.remove(&(offer.job_id.clone(),offer.attempt_id.clone()));
|
||||
// First preserve the original completion without unlocking.
|
||||
// A startup can replay it if the journal transaction is lost.
|
||||
let state = outcome.state.clone();
|
||||
@@ -807,6 +839,7 @@ async fn main() -> Result<()> {
|
||||
inventory_tx,
|
||||
inventory_rx,
|
||||
inventory_inflight: false,
|
||||
running_cancels: std::collections::HashMap::new(),
|
||||
sync,
|
||||
sync_clock: reconcile::NativeClock::default(),
|
||||
helpers,
|
||||
|
||||
@@ -51,6 +51,7 @@ fn new_process_persists_interruption_before_helper_io_without_unlock_or_repeat()
|
||||
arguments: vec![],
|
||||
expected_stdout: Some("fixture only; never executed\n".into()),
|
||||
windows_execution_profile: None,
|
||||
privileged_script_profile: None,
|
||||
},
|
||||
execution_identity: "root".into(),
|
||||
not_before: now,
|
||||
@@ -139,6 +140,7 @@ fn new_process_persists_interruption_before_helper_io_without_unlock_or_repeat()
|
||||
inventory_tx: itx,
|
||||
inventory_rx: irx,
|
||||
inventory_inflight: false,
|
||||
running_cancels: std::collections::HashMap::new(),
|
||||
sync: Some(reconcile::ClientSync::new(binding.clone())),
|
||||
sync_clock: Clock(true),
|
||||
helpers: None,
|
||||
@@ -525,6 +527,7 @@ async fn run(mode: wire_channel::Mode, trusted: bool, forged: bool, rounds: u64)
|
||||
inventory_tx: itx,
|
||||
inventory_rx: irx,
|
||||
inventory_inflight: false,
|
||||
running_cancels: std::collections::HashMap::new(),
|
||||
sync: Some(reconcile::ClientSync::new(binding)),
|
||||
sync_clock: Clock(trusted),
|
||||
helpers: None,
|
||||
|
||||
@@ -595,6 +595,7 @@ fn script() -> Action {
|
||||
arguments: vec![],
|
||||
expected_stdout: Some("hello".into()),
|
||||
windows_execution_profile: None,
|
||||
privileged_script_profile: None,
|
||||
}
|
||||
}
|
||||
fn success() -> ReconciledOutcome {
|
||||
|
||||
Reference in New Issue
Block a user