diff --git a/crates/agent-windows/src/bin/rmm-updater-windows.rs b/crates/agent-windows/src/bin/rmm-updater-windows.rs index 60ac0ae..8741a32 100644 --- a/crates/agent-windows/src/bin/rmm-updater-windows.rs +++ b/crates/agent-windows/src/bin/rmm-updater-windows.rs @@ -7,6 +7,10 @@ fn main() -> anyhow::Result<()> { Some(flag) if flag == "--release-service" && args.next().is_none() => { rmm_agent_windows::release_service::dispatch() } + Some(flag) if flag == "--release-capabilities" && args.next().is_none() => { + println!("release-job-lock/1"); + Ok(()) + } Some(flag) if flag == "--verify-bootstrap" => { let release = rmm_updater::verify_bootstrap_files( rmm_agent_windows::release_platform::VERIFY, diff --git a/crates/agent-windows/src/helper_ipc.rs b/crates/agent-windows/src/helper_ipc.rs index 5d34732..db21a97 100644 --- a/crates/agent-windows/src/helper_ipc.rs +++ b/crates/agent-windows/src/helper_ipc.rs @@ -1432,7 +1432,8 @@ impl Server { /// Blocking SDK call; run on the agent's bounded blocking worker, never heartbeat reactor. pub fn execution_timeout(action: &rmm_domain::jobs::Action, seconds: u32) -> Result { ensure!( - (1..=action.windows_system_runtime_limit(120, Some(rmm_domain::jobs::WINDOWS_UPDATE_MAX_SECONDS))).contains(&seconds), + (1..=action.windows_system_runtime_limit(120, Some(rmm_domain::jobs::WINDOWS_UPDATE_MAX_SECONDS))) + .contains(&seconds), "SYSTEM_EXECUTION_TIMEOUT_BOUND" ); Ok(Duration::from_secs(u64::from(seconds) + 5)) @@ -1455,9 +1456,12 @@ fn exchange_fixed(pin: &Pin, call: &Call, timeout: Duration, time_only: bool) -> anyhow::bail!("HELPER_CALL_TIMEOUT_BOUND"); }; let job = pin.binding.job(&request.job_bytes)?; - ensure!(job.execution_identity == "system" && job.action.is_windows_update_template() - && execution_timeout(&job.action, job.max_runtime_seconds)? == timeout, - "HELPER_CALL_TIMEOUT_BOUND"); + ensure!( + job.execution_identity == "system" + && job.action.is_windows_update_template() + && execution_timeout(&job.action, job.max_runtime_seconds)? == timeout, + "HELPER_CALL_TIMEOUT_BOUND" + ); } else { ensure!(timeout >= Duration::from_secs(1), "HELPER_CALL_TIMEOUT_BOUND"); } diff --git a/crates/agent-windows/src/helper_system.rs b/crates/agent-windows/src/helper_system.rs index 0aa86ed..6371f19 100644 --- a/crates/agent-windows/src/helper_system.rs +++ b/crates/agent-windows/src/helper_system.rs @@ -116,8 +116,9 @@ impl Settings { .action .valid_job_limits(job.max_runtime_seconds, job.max_output_bytes) && job.max_runtime_seconds <= job.action.windows_system_runtime_limit(self.max_runtime_seconds, self.windows_update_max_runtime_seconds) - && self.allowed_script_profiles.as_deref() + && (self.allowed_script_profiles.as_deref() == Some(&[rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1]) + || (self.windows_update_max_runtime_seconds.is_some() && job.action.is_windows_update_template())) } fn permits_reboot(&self, job: &JobPayload) -> bool { self.schema_version == 4 @@ -146,7 +147,8 @@ impl Settings { |v| v == [rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1] ) && (self.schema_version == 4 || !self.allow_reboot) - && rmm_domain::jobs::valid_windows_update_limit(self.windows_update_max_runtime_seconds, self.allowed_script_profiles.as_deref()) + && (self.schema_version >= 3 || self.windows_update_max_runtime_seconds.is_none()) + && rmm_domain::jobs::valid_windows_update_limit(self.windows_update_max_runtime_seconds) && (1..=120).contains(&self.max_runtime_seconds) && self.allowed_services.len() <= 16, "SYSTEM_POLICY_PROFILE" diff --git a/crates/agent-windows/src/release_service.rs b/crates/agent-windows/src/release_service.rs index a7bc8f2..1025d29 100644 --- a/crates/agent-windows/src/release_service.rs +++ b/crates/agent-windows/src/release_service.rs @@ -52,6 +52,8 @@ struct Endpoint { ca_file: PathBuf, #[serde(default)] enable_jobs: bool, + #[serde(default)] + localservice_time_policy: Option, } #[derive(Deserialize)] struct Health { @@ -349,16 +351,36 @@ async fn stage(device: &Device, release: &Published, active: &Installed) -> Resu result } +fn exclusive_job_gate() -> Result> { + use std::os::windows::fs::OpenOptionsExt; + let path = data("release-job.lock"); + job_binding::check_admin_readonly(&path)?; + match fs::OpenOptions::new().read(true).share_mode(0).open(path) { + Ok(file) => Ok(Some(file)), + Err(error) if error.raw_os_error() == Some(32) => Ok(None), + Err(error) => Err(error.into()), + } +} + async fn cycle() -> Result<()> { let active: Installed = read_record(ACTIVE)?; let config: Endpoint = serde_json::from_slice(&protected(&data("config.json"), 32768)?)?; - // Installed passive pairs only. A separately provisioned SYSTEM helper and - // privileged time/profile pins need the existing coordinated handoff. + // Additional SYSTEM helper pins need their separately coordinated handoff. ensure!( - !config.enable_jobs, + !config.enable_jobs + || config.localservice_time_policy.as_ref() == Some(&data("job-time.json")), "job-enabled pair requires coordinated helper handoff" ); if data(INTENT).try_exists()? { + let _job_gate = if config.enable_jobs { + let Some(file) = exclusive_job_gate()? else { + return Ok(()); + }; + Some(file) + } else { + None + }; + let t: Transition = read_record(INTENT)?; if active.release_id == t.new.release_id { fs::remove_file(data(INTENT))?; @@ -414,6 +436,14 @@ async fn cycle() -> Result<()> { "current agent version not confirmed" ); let new = stage(&device, &release, &active).await?; + let _job_gate = if config.enable_jobs { + let Some(file) = exclusive_job_gate()? else { + return Ok(()); + }; + Some(file) + } else { + None + }; let t = Transition { old: active, new }; write_record(INTENT, &t)?; let start = chrono::Utc::now(); diff --git a/crates/agent-windows/src/release_service_switch.ps1 b/crates/agent-windows/src/release_service_switch.ps1 index aca8772..9771599 100644 --- a/crates/agent-windows/src/release_service_switch.ps1 +++ b/crates/agent-windows/src/release_service_switch.ps1 @@ -9,7 +9,10 @@ $from = Get-Image 'FROM_UUID' $to = Get-Image 'TO_UUID' $service = Get-CimInstance -ClassName Win32_Service -Filter "Name='RmmDiscoveryWindows'" $config = Get-Content -LiteralPath $configPath -Raw | ConvertFrom-Json -if ($config.PSObject.Properties['enable_jobs'] -and $config.enable_jobs) { throw 'Job-enabled agent requires a coordinated handoff.' } +if ($config.PSObject.Properties['enable_jobs'] -and $config.enable_jobs) { + if (-not $config.PSObject.Properties['localservice_time_policy'] -or + $config.localservice_time_policy -cne (Join-Path (Split-Path $configPath) 'job-time.json')) { throw 'Job-enabled pair requires a coordinated helper handoff.' } +} if (-not $service -or $service.StartName -ine 'NT AUTHORITY\LocalService' -or $service.PathName -cnotin @($from,$to) -or (Get-Service 'RmmDiscoverySystemHelper' -ErrorAction SilentlyContinue)) { throw 'Unexpected service configuration.' } if ($service.PathName -cne $to) { diff --git a/crates/agent-windows/src/runtime.rs b/crates/agent-windows/src/runtime.rs index 22e3c48..dd97444 100644 --- a/crates/agent-windows/src/runtime.rs +++ b/crates/agent-windows/src/runtime.rs @@ -36,6 +36,8 @@ struct Config { transport: String, #[serde(default)] job_binding_file: Option, + #[serde(default)] + localservice_time_policy: Option, // Explicit protected opt-in only; this flag never qualifies a clock or pins. #[serde(default)] enable_jobs: bool, @@ -88,13 +90,20 @@ fn rotation_file_id(path: &Path) -> Result<[u32; 3]> { impl Config { fn profile_enabled(&self, profile: rmm_domain::jobs::WindowsExecutionProfile) -> bool { self.enable_jobs - && self.job_binding_file.is_some() + && (self.job_binding_file.is_some() || self.localservice_time_policy.is_some()) && cfg!(feature = "system-helper") && self.windows_execution_profile == Some(profile) } fn read(path: &Path) -> Result { let raw = protected_read(path, 32768)?; let config: Self = serde_json::from_slice(&raw)?; + if let Some(policy) = &config.localservice_time_policy { + let parent = path.parent().context("CONFIG_PARENT")?; + ensure!(policy == &parent.join("job-time.json") + && config.job_binding_file.is_none() + && config.profile_enabled(rmm_domain::jobs::WindowsExecutionProfile::PowerShell51RemoteSigned) + && !config.windows_echo_probe_only, "INVALID_ENROLLED_LOCALSERVICE_CONFIGURATION"); + } if config.windows_echo_probe_only { ensure!( config.profile_enabled( @@ -593,6 +602,7 @@ impl HelperControl { struct Agent { config: Config, reported_version: String, + release_job_guard: Option, identity: EnrollmentResponse, ca: Vec, journal: Journal, @@ -802,12 +812,14 @@ impl Agent { &identity.certificate_pem, &identity.private_key_pem, )?; - let helper = rmm_agent_windows::helper_runtime::Client::discover( + let helper = if self.config.localservice_time_policy.is_some() { + None + } else { rmm_agent_windows::helper_runtime::Client::discover( &self.identity.agent_id, &self.identity.asset_id, self.binding.as_ref(), self.helper_lane.clone(), - )?; + )? }; let clock = self.clock.native.source(); let stop = Arc::new(AtomicBool::new(false)); let mut tasks = Vec::new(); @@ -828,6 +840,7 @@ impl Agent { .chain(helper_receipt.iter()) .cloned() .collect(); + let installed_localservice = self.config.localservice_time_policy.is_some(); tasks.push(tokio::spawn(async move { let mut own_report = RefreshReport::default(); while !own_stop.load(Ordering::SeqCst) { @@ -857,7 +870,12 @@ impl Agent { let _ = own_clock.close_generation(generation); } own_report.record(&own_state, "agent", &result); - tokio::time::sleep(mode.interval(result.is_ok())).await; + let interval = if installed_localservice && result.is_ok() { + Duration::from_secs(5) + } else { + mode.interval(result.is_ok()) + }; + tokio::time::sleep(interval).await; } let _ = own_clock.close_generation(generation); })); @@ -1359,7 +1377,32 @@ impl Agent { })); Ok(false) } + fn reserve_release_gate(&mut self) -> Result { + let Some(policy) = &self.config.localservice_time_policy else { return Ok(true) }; + if self.release_job_guard.is_some() { return Ok(true) } + use std::os::windows::fs::OpenOptionsExt; + use windows_sys::Win32::Storage::FileSystem::FILE_SHARE_READ; + let path = policy.with_file_name("release-job.lock"); + rmm_agent_windows::job_binding::check_admin_readonly(&path)?; + match fs::OpenOptions::new().read(true).share_mode(FILE_SHARE_READ).open(path) { + Ok(file) => { self.release_job_guard = Some(file); Ok(true) } + Err(error) if error.raw_os_error() == Some(32) => Ok(false), + Err(error) => Err(error.into()), + } + } + fn release_idle_gate(&mut self) -> Result<()> { + if self.release_job_guard.is_some() && self.running.is_none() && self.deferred_permit.is_none() { + let idle: bool = self.journal.connection().query_row( + "SELECT NOT EXISTS(SELECT 1 FROM job_attempts WHERE state IN ('requesting','running','verifying','awaiting_reboot'))", + [], |row| row.get(0))?; + if idle { self.release_job_guard.take(); } + } + Ok(()) + } fn supported_job(&self, job: &rmm_domain::jobs::JobPayload) -> bool { + if self.config.localservice_time_policy.is_some() && job.execution_identity != "agent_service" { + return false; + } if self.config.windows_echo_probe_only && !self .config @@ -2577,7 +2620,7 @@ impl Agent { let prefetched=self.prefetch_update(generation,&offer,&job).await?; #[cfg(not(feature="system-helper"))] let prefetched=true; - if prefetched { + if prefetched && self.reserve_release_gate()? { jobs::reserve_request(self.journal.connection(),&offer)?; let request=self.envelope(generation,Payload::ExecutionPermitRequest(ExecutionPermitRequest{job_id:offer.job_id,attempt_id:offer.attempt_id,job_payload_hash:offer.job_payload_hash,assignment_version:job.assignment_version}))?; channel.send(&request).await?; @@ -2632,6 +2675,7 @@ impl Agent { } } } + self.release_idle_gate()?; if readiness_at.elapsed() >= Duration::from_secs(5) { readiness_at = Instant::now(); if self @@ -2832,6 +2876,10 @@ fn service_run(stop: &'static AtomicBool) -> Result<()> { journal.pin_identity(&identity.agent_id)?; let boot = native::boot_id().await?; let binding=config.job_binding_file.as_deref().map(readonly_binding).transpose()?; + #[cfg(feature = "system-helper")] + let binding = if let Some(policy) = &config.localservice_time_policy { + Some(rmm_agent_windows::signed_clock::enrolled_binding(policy, &identity)?) + } else { binding }; if let Some(binding)=&binding { ensure!(binding.agent_id.to_string()==identity.agent_id && binding.asset_id.to_string()==identity.asset_id,"ENROLLMENT_JOB_BINDING_MISMATCH"); native::private_path(&config.state_directory.join("jobs"),true)?; @@ -2845,6 +2893,11 @@ fn service_run(stop: &'static AtomicBool) -> Result<()> { for child in fs::read_dir(&config.state_directory)? { let child=child?;native::private_path(&child.path(),child.file_type()?.is_dir())?; } let (ledger,ledger_file)=match &binding {Some(binding)=>{let (db,file)=open_ledger(&config.state_directory,binding)?;(Some(db),Some(file))},None=>(None,None)}; let clock=JobClock::new(config.enable_jobs); + #[cfg(feature = "system-helper")] + let clock = if let Some(policy) = &config.localservice_time_policy { + let binding = binding.clone().context("JOB_BINDING_REQUIRED")?; + JobClock { enabled: true, native: rmm_agent_windows::signed_clock::RuntimeClock::enrolled_local_service(binding,policy.clone()).for_generation(0) } + } else { clock }; let mut config=config; if config.enable_jobs { config.echo_scope=rmm_agent_windows::echo_probe::installed(journal.connection(),binding.as_ref().context("JOB_BINDING_REQUIRED")?)?; @@ -2864,6 +2917,7 @@ fn service_run(stop: &'static AtomicBool) -> Result<()> { let mut agent = Agent { config, reported_version: installed_version()?, + release_job_guard: None, identity, ca, journal, @@ -2954,6 +3008,10 @@ pub fn main() -> Result<()> { ensure!(args.len() == 2, "EVENT_LOG_WORKER_ARGUMENTS"); return rmm_agent_windows::event_log_job::worker(Path::new(&args[1])); } + if args == ["--release-capabilities"] { + println!("enrolled-localservice/1"); + return Ok(()); + } if args == ["--help"] { println!( "Development Windows service. --service --config ABSOLUTE_PATH. Jobs disabled by default; require qualified clock, provisioned signing binding, signed Sync and WebUI-authorized grant. No approval bypass." diff --git a/crates/agent-windows/src/signed_clock.rs b/crates/agent-windows/src/signed_clock.rs index ba6396c..8d99992 100644 --- a/crates/agent-windows/src/signed_clock.rs +++ b/crates/agent-windows/src/signed_clock.rs @@ -176,6 +176,7 @@ impl UpdaterConfiguration { enum CaptureMode { Physical { base_error_ns: u64 }, TrustedOsActiveGuest, + EnrolledLocalService, } fn digest(value: &str) -> bool { value.len() == 64 @@ -476,7 +477,9 @@ fn bracket_sample( // This is a software bracket in trusted nominal OS units, not a // physical accuracy bound. Checked multiplication by100 is exact, so // there is no conversion rounding remainder or invented base constant. - CaptureMode::TrustedOsActiveGuest => width.max(unbiased_width), + CaptureMode::TrustedOsActiveGuest | CaptureMode::EnrolledLocalService => { + width.max(unbiased_width) + } }; ensure!( capture_error_ns <= rmm_time_attestation::MAX_CAPTURE_ERROR_NS, @@ -778,14 +781,58 @@ mod installed { challenge: Challenge, } struct HeldEvidence { + received_ticks: Sample, evidence: VerifiedEvidence, epoch: u64, } /// Own-process role proof, protected held configuration, private challenge /// and native timers. No constructor accepts agent-reported helper times. + enum ClockOwner { + Service(Box), + EnrolledLocalService, + } + + #[derive(serde::Deserialize)] + #[serde(deny_unknown_fields)] + struct LocalPolicy { + schema_version: u32, + key_generation: u64, + server_instance: Uuid, + public_keys_hex: BTreeMap, + tenant_id: Uuid, + job_signing_public_key_hex: String, + } + pub fn enrolled_binding( + policy: &Path, + registration: &rmm_protocol::EnrollmentResponse, + ) -> Result { + crate::job_binding::check_admin_readonly(policy)?; + ensure!( + std::fs::metadata(policy)?.len() <= MAX_CONFIG_BYTES as u64, + "TIME_NATIVE_CONFIG_SIZE" + ); + let p: LocalPolicy = serde_json::from_slice(&std::fs::read(policy)?)?; + ensure!( + p.schema_version == 1 && !p.tenant_id.is_nil() && digest(&p.job_signing_public_key_hex), + "INSTALLED_JOB_TRUST" + ); + let binding = rmm_agent_core::auth::Binding { + tenant_id: p.tenant_id, + agent_id: Uuid::parse_str(®istration.agent_id)?, + asset_id: Uuid::parse_str(®istration.asset_id)?, + enrollment_generation: 1, + assignment_version: 1, + public_key_hex: p.job_signing_public_key_hex, + }; + ensure!( + !binding.agent_id.is_nil() && !binding.asset_id.is_nil(), + "INSTALLED_DEVICE_IDENTITY" + ); + Ok(binding) + } pub struct SignedClock { verifier: Verifier, - own_process: PeerProof, + owner: ClockOwner, files: Vec, config_sha256: String, boot_id: Uuid, @@ -891,7 +938,7 @@ mod installed { files.push(config_file); let mut value = Self { verifier: Verifier::new(context, pins)?, - own_process, + owner: ClockOwner::Service(Box::new(own_process)), files, config_sha256: hash(&raw), boot_id, @@ -910,6 +957,88 @@ mod installed { failure_fence.armed = false; Ok(value) } + /// An enrolled LocalService never supplies time evidence for a helper. + /// Its public broker pins and job binding are installed by the HTTPS + /// bootstrap; every usable reading still needs a current signed challenge. + async fn open_local_service( + generation: u64, + binding: &rmm_agent_core::auth::Binding, + policy_file: &Path, + ) -> Result { + let mut failure_fence = ConstructorFailureFence { armed: true }; + crate::native::require_local_service()?; + let readers = [crate::native::service_sid()?]; + let parent = PinnedFile::open( + policy_file.parent().context("TIME_POLICY_PARENT")?, + true, + &readers, + &[], + false, + )?; + let mut config_file = PinnedFile::open(policy_file, false, &readers, &[], false)?; + let mut raw = Vec::new(); + (&mut config_file.file) + .take(MAX_CONFIG_BYTES as u64 + 1) + .read_to_end(&mut raw)?; + ensure!(raw.len() <= MAX_CONFIG_BYTES, "TIME_NATIVE_CONFIG_SIZE"); + let p: LocalPolicy = serde_json::from_slice(&raw)?; + ensure!( + p.schema_version == 1 + && !p.public_keys_hex.is_empty() + && p.public_keys_hex.len() <= 8, + "TIME_LOCAL_SERVICE_POLICY" + ); + let keys = p + .public_keys_hex + .into_iter() + .map(|(id, key)| Ok((id, independent_time_key(&key, &binding.public_key_hex)?))) + .collect::>>()?; + ensure!( + p.tenant_id == binding.tenant_id + && p.job_signing_public_key_hex == binding.public_key_hex, + "TIME_POLICY_BINDING" + ); + let pins = Pins::new(p.key_generation, p.server_instance, keys)?; + let boot_id = Uuid::parse_str(&crate::native::boot_id().await?)?; + let context = Context { + tenant_id: binding.tenant_id, + asset_id: binding.asset_id, + agent_id: binding.agent_id, + enrollment_generation: binding.enrollment_generation, + assignment_generation: binding.assignment_version, + connection_generation: generation, + windows_boot_id: boot_id, + verifier_incarnation: *INCARNATION.get_or_init(Uuid::new_v4), + role: Role::Agent, + binding_schema: 1, + helper_image_sha256: None, + updater_image_sha256: None, + }; + let hash = hash(&raw); + let mut clock = Self { + verifier: Verifier::new(context, pins)?, + owner: ClockOwner::EnrolledLocalService, + files: vec![parent, config_file], + config_sha256: hash.clone(), + boot_id, + connection_generation: generation, + capture_mode: CaptureMode::EnrolledLocalService, + pending: None, + latest: None, + own_updater_configuration: None, + }; + clock.recheck()?; + { + let mut state = process() + .lock() + .map_err(|_| anyhow::anyhow!("TIME_NATIVE_STATE_POISONED"))?; + state.accept_configuration(p.key_generation, &hash)?; + state.accept_connection(generation)?; + } + observed(boot_id, clock.capture_mode)?; + failure_fence.armed = false; + Ok(clock) + } /// Stable-updater-only process proof and separately protected profile. /// It uses its OWN current SCM process/image/token and challenge. pub async fn open_updater(connection_generation: u64) -> Result { @@ -1011,7 +1140,7 @@ mod installed { let capture_mode = CaptureMode::TrustedOsActiveGuest; let mut value = Self { verifier: Verifier::new(context, pins)?, - own_process, + owner: ClockOwner::Service(Box::new(own_process)), files: vec![root, policy_file, config_file], config_sha256: hash(&raw), boot_id, @@ -1032,7 +1161,10 @@ mod installed { } fn recheck(&mut self) -> Result<()> { let result = (|| { - self.own_process.check()?; + match &mut self.owner { + ClockOwner::Service(process) => process.check()?, + ClockOwner::EnrolledLocalService => crate::native::require_local_service()?, + } if let Some((pin, expected)) = &self.own_updater_configuration { ensure!( crate::update_native::services::updater_configuration_sha256(pin)? @@ -1141,6 +1273,7 @@ mod installed { state.active = None; self.latest = Some(HeldEvidence { evidence, + received_ticks: received, epoch: pending.epoch, }); let comparison = Comparison { @@ -1179,6 +1312,25 @@ mod installed { self.recheck()?; let (sample, current_epoch) = observed(self.boot_id, self.capture_mode)?; ensure!(epoch == current_epoch, "TIME_NATIVE_EVIDENCE_INVALIDATED"); + if self.capture_mode == CaptureMode::EnrolledLocalService { + let previous = self + .latest + .as_ref() + .context("TIME_NATIVE_NO_EVIDENCE")? + .received_ticks; + let wall = sample + .interrupt_ns + .checked_sub(previous.interrupt_ns) + .context("TIME_INTERRUPT_RESET")?; + let awake = sample + .unbiased_ns + .checked_sub(previous.unbiased_ns) + .context("TIME_AWAKE_RESET")?; + ensure!( + wall <= 15 * SECOND_NS && wall.abs_diff(awake) <= 10_000_000, + "TIME_LOCAL_SERVICE_STALE_OR_SUSPENDED" + ); + } let state = process() .lock() .map_err(|_| anyhow::anyhow!("TIME_NATIVE_STATE_POISONED"))?; @@ -1233,6 +1385,7 @@ mod installed { #[derive(Clone)] pub struct RuntimeClock { role: Role, + local_policy: Option<(rmm_agent_core::auth::Binding, std::path::PathBuf)>, state: Arc>>, } #[derive(Clone)] @@ -1244,9 +1397,18 @@ mod installed { pub fn new(role: Role) -> Self { Self { role, + local_policy: None, state: Arc::new(Mutex::new(None)), } } + pub fn enrolled_local_service( + binding: rmm_agent_core::auth::Binding, + policy: std::path::PathBuf, + ) -> Self { + let mut value = Self::new(Role::Agent); + value.local_policy = Some((binding, policy)); + value + } pub fn for_generation(&self, generation: u64) -> BoundClock { BoundClock { source: self.clone(), @@ -1261,7 +1423,12 @@ mod installed { pub async fn open_connection(&self, generation: u64) -> Result<()> { ensure!(generation > 0, "TIME_RUNTIME_GENERATION"); self.close()?; - let clock = SignedClock::open(self.role, generation).await?; + let clock = match &self.local_policy { + Some((binding, policy)) => { + SignedClock::open_local_service(generation, binding, policy).await? + } + None => SignedClock::open(self.role, generation).await?, + }; let mut state = self .state .lock() @@ -1378,6 +1545,7 @@ mod installed { #[cfg(all(windows, feature = "system-helper"))] pub use installed::{ BoundClock, CONFIG_PATH, Comparison, RuntimeClock, SignedClock, UPDATER_CONFIG_PATH, + enrolled_binding, }; #[cfg(test)] diff --git a/crates/agent-windows/src/system_capabilities.rs b/crates/agent-windows/src/system_capabilities.rs index 8115b97..a73f4bc 100644 --- a/crates/agent-windows/src/system_capabilities.rs +++ b/crates/agent-windows/src/system_capabilities.rs @@ -80,19 +80,13 @@ pub fn qualified( for action in q.actions { match action.as_str() { "run_script" => { - ensure!( - settings.schema_version >= 3 - && settings.allowed_script_profiles.as_deref() - == Some(&[ - rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1 - ]) - && crate::actions::powershell().is_ok(), - "system script qualification policy" - ); - if settings.windows_update_max_runtime_seconds.is_some() { - caps.push(rmm_domain::jobs::WINDOWS_UPDATE_LONG_CAPABILITY.to_owned()); - } - caps.extend(["system:run_script", "script.full_maintenance/1"].map(str::to_string)); + let general = settings.allowed_script_profiles.as_deref() == + Some(&[rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1]); + let windows_update = settings.windows_update_max_runtime_seconds.is_some(); + ensure!(settings.schema_version >= 3 && (general || windows_update), "system script qualification policy"); + caps.push("system:run_script".into()); + if general { caps.push(rmm_domain::jobs::FULL_MAINTENANCE_CAPABILITY.to_owned()); } + if windows_update { caps.push(rmm_domain::jobs::WINDOWS_UPDATE_LONG_CAPABILITY.to_owned()); } } "set_service_state" => { ensure!( diff --git a/crates/agent-windows/src/system_script.rs b/crates/agent-windows/src/system_script.rs index 929bc57..197934c 100644 --- a/crates/agent-windows/src/system_script.rs +++ b/crates/agent-windows/src/system_script.rs @@ -121,7 +121,12 @@ pub(crate) fn execute( .as_ref() .is_none_or(|s| output.stdout == s.as_bytes()); let interrupted = output.interrupted || began.elapsed() >= timeout || observe(false).is_err(); - let success = !interrupted && output.exit_code == Some(0) && expected && !output.truncated; + let windows_update = job.action.is_windows_update_template(); + let reboot = if windows_update && !interrupted && !output.truncated { + rmm_domain::jobs::windows_update_reboot_report(&output.stdout) + } else { None }; + let success = !interrupted && output.exit_code == Some(0) && expected && !output.truncated + && (!windows_update || reboot.is_some()); let mut evidence = serde_json::json!({"postconditions_passed":success,"interpreter":"powershell", "script_sha256":hash(script_utf8.as_bytes()),"privileged_script_profile":"full_maintenance_v1", "execution_sid":"S-1-5-18","output_encoding":"raw_native_bytes","comparison":"exact_utf8_bytes", @@ -133,6 +138,9 @@ pub(crate) fn execute( evidence["execution_policy_requested"] = "RemoteSigned".into(); evidence["execution_policy_scope"] = "process".into(); } + if windows_update { + evidence["windows_update_reboot_required"] = serde_json::json!(reboot); + } Ok(Outcome { state: if interrupted { "unknown" @@ -146,13 +154,15 @@ pub(crate) fn execute( stderr: output.stderr, exit_code: output.exit_code, output_truncated: output.truncated, - reboot_required: false, + reboot_required: reboot.unwrap_or(false), error_code: if success { "" } else if interrupted { "SYSTEM_SCRIPT_INTERRUPTED_EFFECT_UNKNOWN" } else if output.truncated { "OUTPUT_LIMIT_REACHED" + } else if windows_update && output.exit_code == Some(0) && reboot.is_none() { + "WINDOWS_UPDATE_SUMMARY_MISSING" } else if !expected { "STDOUT_POSTCONDITION_FAILED" } else { diff --git a/crates/agent-windows/src/update_native/transition.rs b/crates/agent-windows/src/update_native/transition.rs index c23eb2a..d30b3a4 100644 --- a/crates/agent-windows/src/update_native/transition.rs +++ b/crates/agent-windows/src/update_native/transition.rs @@ -215,7 +215,8 @@ pub fn render(input: Inputs<'_>) -> Result { ensure!( matches!(settings.schema_version, 1..=4) && (settings.schema_version == 4 || !settings.allow_reboot) - && rmm_domain::jobs::valid_windows_update_limit(settings.windows_update_max_runtime_seconds, settings.allowed_script_profiles.as_deref()) + && (settings.schema_version >= 3 || settings.windows_update_max_runtime_seconds.is_none()) + && rmm_domain::jobs::valid_windows_update_limit(settings.windows_update_max_runtime_seconds) && (1..=120).contains(&settings.max_runtime_seconds) && settings.allowed_services.len() <= 16 && (settings.schema_version >= 2 || settings.allowed_msi_recipe_sha256.is_none()) diff --git a/crates/domain/src/jobs.rs b/crates/domain/src/jobs.rs index 787de98..d1c3e74 100644 --- a/crates/domain/src/jobs.rs +++ b/crates/domain/src/jobs.rs @@ -60,14 +60,8 @@ pub const WINDOWS_UPDATE_SCRIPT_SHA256: &str = "0fa5f513a06b6cf902a3f9ac1989987642c181eedd6c305b3912771bd10f2ebf"; pub const WINDOWS_UPDATE_MAX_SECONDS: u32 = 3600; pub const WINDOWS_UPDATE_LONG_CAPABILITY: &str = "script.windows_update.long/1"; -pub fn valid_windows_update_limit( - limit: Option, - profiles: Option<&[PrivilegedScriptProfile]>, -) -> bool { - limit.is_none_or(|seconds| { - (121..=WINDOWS_UPDATE_MAX_SECONDS).contains(&seconds) - && profiles == Some(&[PrivilegedScriptProfile::FullMaintenanceV1][..]) - }) +pub fn valid_windows_update_limit(limit: Option) -> bool { + limit.is_none_or(|seconds| (121..=WINDOWS_UPDATE_MAX_SECONDS).contains(&seconds)) } /// Only the built-in template's final ASCII JSON object describes the host's /// restart state. Earlier lines can include per-update results and native titles. @@ -489,7 +483,7 @@ impl Action { let has = |cap: &str| capabilities.iter().any(|c| c == cap); if !has(self.name()) || (self.full_maintenance() - && !(has(FULL_MAINTENANCE_CAPABILITY) + && !((has(FULL_MAINTENANCE_CAPABILITY) || (self.is_windows_update_template() && has(WINDOWS_UPDATE_LONG_CAPABILITY))) && has("rmm.sync.v1") && has(&format!("{}:run_script", identity)) && has(if identity == "root" { @@ -843,7 +837,7 @@ mod tests { None ); assert_eq!(windows_update_reboot_report(b"partial"), None); - assert!(!valid_windows_update_limit(Some(3600), None)); + assert!(!valid_windows_update_limit(Some(3601))); } #[test] fn windows_system_scm_is_explicit_and_preserves_linux_service_bytes() { diff --git a/crates/server/src/agent_updates.rs b/crates/server/src/agent_updates.rs index d9b5baf..fb30b02 100644 --- a/crates/server/src/agent_updates.rs +++ b/crates/server/src/agent_updates.rs @@ -238,7 +238,7 @@ fn verify_envelope_with_keyring( keyring_generation: config.generation, protocol_major: 1, state_schema: 1, - updater_version: 1, + updater_version: if binding.is_none() && decoded.manifest.platform == Platform::WindowsGnuX64 { 2 } else { 1 }, minimum_release_sequence: 0, active_release: None, }; diff --git a/crates/server/src/bootstrap/install.ps1 b/crates/server/src/bootstrap/install.ps1 index bae6951..f8d31dd 100644 --- a/crates/server/src/bootstrap/install.ps1 +++ b/crates/server/src/bootstrap/install.ps1 @@ -123,7 +123,7 @@ try { Set-Text $ring ($meta.keyring | ConvertTo-Json -Depth 16 -Compress) Set-Text $envelope ($meta.release_envelope | ConvertTo-Json -Depth 16 -Compress) $updater = Join-Path $appStage 'rmm-updater-windows.exe' - Get-Download ('/agent/v1/bootstrap/updater' + $query) $updater $meta.updater.bytes $true + Get-Download ('/agent/v1/bootstrap/updater' + $query + '&sha256=' + $meta.updater.sha256) $updater $meta.updater.bytes $true if ((Get-Hash $updater) -cne $meta.updater.sha256) { throw 'Bootstrap updater digest mismatch.' } $roles = @('agent', 'helper') for ($i = 0; $i -lt $roles.Count; $i++) { @@ -145,6 +145,25 @@ try { $ca = Join-Path $data 'server-ca.pem' Set-Text (Join-Path $dataStage 'server-ca.pem') $meta.ca_pem $config = [ordered]@{server_name=$gateway.DnsSafeHost; connect_address=$endpoint.ToString(); ca_file=$ca; state_directory=$state; enrollment_token_file=$tokenPath; transport='https'} + $localJobs = $verified.minimum_updater_version -ge 2 + if ($localJobs) { + $features = & $agentPath --release-capabilities + if ($LASTEXITCODE -ne 0 -or $features -cne 'enrolled-localservice/1') { throw 'Signed agent lacks its required LocalService profile.' } + $features = & $updater --release-capabilities + if ($LASTEXITCODE -ne 0 -or $features -cne 'release-job-lock/1') { throw 'Stable updater lacks coordinated job draining.' } + $trust = $meta.windows_time_trust + if (-not $trust -or $meta.job_signing_public_key_hex -cnotmatch '^[0-9a-f]{64}$' -or + $trust.key_generation -lt 1 -or @($trust.public_keys_hex.PSObject.Properties).Count -notin (1..8)) { throw 'Missing enrolled Windows job trust.' } + foreach ($key in $trust.public_keys_hex.PSObject.Properties) { + if ($key.Value -cnotmatch '^[0-9a-f]{64}$' -or $key.Value -ceq $meta.job_signing_public_key_hex) { throw 'Invalid independent time signing key.' } + } + $policy = [ordered]@{schema_version=1; tenant_id=$tenant; job_signing_public_key_hex=$meta.job_signing_public_key_hex; key_generation=$trust.key_generation; server_instance=(Get-Id $trust.server_instance); public_keys_hex=$trust.public_keys_hex} + Set-Text (Join-Path $dataStage 'job-time.json') ($policy | ConvertTo-Json -Depth 8 -Compress) + Set-Text (Join-Path $dataStage 'release-job.lock') '' + $config['enable_jobs'] = $true + $config['localservice_time_policy'] = Join-Path $data 'job-time.json' + $config['windows_execution_profile'] = 'powershell_51_remote_signed' + } Set-Text (Join-Path $dataStage 'config.json') ($config | ConvertTo-Json -Compress) $baseline = [ordered]@{schema_version=1; tenant_id=$tenant; release_id=$releaseId; release_sequence=$verified.release_sequence; manifest_sha256=(Get-Hash (Join-Path $images 'manifest.json'))} Set-Text (Join-Path $dataStage 'installed-release.json') ($baseline | ConvertTo-Json -Compress) @@ -165,6 +184,7 @@ try { Set-Acl -LiteralPath $app -AclObject (Get-DirectoryAcl $sid 'ReadAndExecute') Set-Acl -LiteralPath $data -AclObject (Get-DirectoryAcl $sid 'ReadAndExecute') [void][IO.Directory]::CreateDirectory($state, (Get-DirectoryAcl $sid 'Modify')) + if ($localJobs) { [void][IO.Directory]::CreateDirectory((Join-Path $state 'jobs'), (Get-DirectoryAcl $sid 'Modify')) } # Agent-side private-path checks authenticate the owner as well as the DACL. foreach ($root in @($app, $data)) { foreach ($item in @(Get-ChildItem -LiteralPath $root -Recurse -Force)) { diff --git a/crates/server/src/bootstrap/lifecycle.py b/crates/server/src/bootstrap/lifecycle.py index 92aa66c..0fe6a65 100644 --- a/crates/server/src/bootstrap/lifecycle.py +++ b/crates/server/src/bootstrap/lifecycle.py @@ -117,7 +117,10 @@ def device_get(route, size=None, digest=None): def device_idle(): # The root worker holds the admission lock before consulting this journal. with sqlite3.connect(f'file:{STATE}/journal.sqlite?mode=ro', uri=True) as conn: - busy, = conn.execute("SELECT EXISTS(SELECT 1 FROM job_attempts WHERE state IN ('requesting','running','verifying'))").fetchone() + busy, = conn.execute( + "SELECT EXISTS(SELECT 1 FROM job_attempts " + "WHERE state IN ('requesting','running','verifying','unknown','awaiting_reboot'))" + ).fetchone() print('busy' if busy else 'idle') @@ -248,7 +251,7 @@ def enable(): else: save(path, data.encode()) service('daemon-reload') - service('enable', '--now', SERVICE, TIMER) + service('enable', '--now', SERVICE) def enrollment_token(path): @@ -279,6 +282,7 @@ def install(base, token_file): save_token(ROOT, token, pwd.getpwnam('rmm-discovery').pw_gid) enable() require(healthy(read(ROOT / 'current/manifest.json')['version'], 0), 'awaiting authenticated device heartbeat') + service('enable', '--now', TIMER) (ROOT / 'first.token').unlink(missing_ok=True) return require(not STATE.exists() and not any((UNIT_DIR/name).exists() for name in units()), @@ -302,7 +306,7 @@ def install(base, token_file): save(staging / 'config.json', {'bootstrap': base, 'gateway': origin(doc['gateway_origin']), 'tenant': canonical_id(doc['tenant_id'])}) updater = staging / 'updater' - remote((base, token), '/agent/v1/bootstrap/updater?platform=' + PLATFORM, updater, doc['updater']) + remote((base, token), '/agent/v1/bootstrap/updater?platform=' + PLATFORM + '&sha256=' + doc['updater']['sha256'], updater, doc['updater']) updater.chmod(0o755) releases = staging / 'releases' releases.mkdir() @@ -318,6 +322,9 @@ def install(base, token_file): os.rename(staging, ROOT) enable() require(healthy(manifest['version'], 0), 'installed, awaiting authenticated device heartbeat') + # OnBootSec may already be in the past on a running machine. The first + # timer check can run immediately once the device has reported its platform. + service('enable', '--now', TIMER) (ROOT / 'first.token').unlink(missing_ok=True) diff --git a/crates/server/src/bootstrap/mod.rs b/crates/server/src/bootstrap/mod.rs index 1460ae6..ef70540 100644 --- a/crates/server/src/bootstrap/mod.rs +++ b/crates/server/src/bootstrap/mod.rs @@ -9,14 +9,41 @@ struct Config { updater_sha256: String, updater_bytes: u64, } -fn config(platform: Platform) -> Result { +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct Pin { sha256: String, bytes: u64 } +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct Previous { pin: Pin, expires_at: i64 } +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct TenantPins { current: Pin, previous: Vec } +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +pub(super) struct UpdaterQuery { platform: Platform, sha256: Option } + +fn config(platform: Platform, tenant: Uuid, wanted: Option<&str>) -> Result { let slot = match platform { Platform::LinuxX64 => "RMM_AGENT_BOOTSTRAP_LINUX_FILE", Platform::WindowsX64 => "RMM_AGENT_BOOTSTRAP_WINDOWS_FILE", Platform::WindowsGnuX64 => "RMM_AGENT_BOOTSTRAP_WINDOWS_GNU_FILE", }; let path = std::env::var(slot).map_err(|_| fail("BOOTSTRAP_NOT_CONFIGURED"))?; - let config: Config = serde_json::from_slice(&protected_file(FsPath::new(&path), 8192)?)?; + let path = FsPath::new(&path); + let mut config: Config = serde_json::from_slice(&protected_file(path, 8192)?)?; + let scoped = path.with_extension(format!("{tenant}.json")); + if scoped.try_exists().map_err(|_| fail("BOOTSTRAP_CONFIGURATION_INVALID"))? { + let pins: TenantPins = serde_json::from_slice(&protected_file(&scoped, 32768)?)?; + let selected = match wanted { + Some(digest) if pins.current.sha256 != digest => pins.previous.into_iter() + .find(|p| p.pin.sha256 == digest && p.expires_at > chrono::Utc::now().timestamp()) + .map(|p| p.pin).ok_or_else(missing)?, + _ => pins.current, + }; + config.updater_sha256 = selected.sha256; + config.updater_bytes = selected.bytes; + } + if wanted.is_some_and(|digest| digest != config.updater_sha256) { return Err(missing()); } let url = url::Url::parse(&config.gateway_origin).map_err(|_| invalid())?; if url.scheme() != "https" || url.domain().is_none() @@ -143,7 +170,7 @@ pub(super) async fn current( Query(q): Query, ) -> Result { let t = token_tenant(&s, &headers).await?; - let c = config(q.platform)?; + let c = config(q.platform, t, None)?; let mut tx = rmm_persistence::tenant_tx(&s.pool, t).await?; let release: Uuid = sqlx::query_scalar("SELECT release_id FROM agent_update_releases WHERE platform=$1 ORDER BY release_sequence DESC LIMIT 1") .bind(platform_name(q.platform)).fetch_optional(&mut *tx).await?.ok_or_else(missing)?; @@ -187,9 +214,9 @@ pub(super) async fn artifact( pub(super) async fn updater( State(s): State, headers: HeaderMap, - Query(q): Query, + Query(q): Query, ) -> Result { let tenant = token_tenant(&s, &headers).await?; - let c = config(q.platform)?; + let c = config(q.platform, tenant, q.sha256.as_deref())?; verified_binary(tenant, c.updater_sha256, c.updater_bytes).await } diff --git a/crates/server/src/windows_policy.rs b/crates/server/src/windows_policy.rs index 0daf138..78c668a 100644 --- a/crates/server/src/windows_policy.rs +++ b/crates/server/src/windows_policy.rs @@ -124,10 +124,7 @@ impl SystemScope { .allowed_script_profiles .as_deref() .is_none_or(|v| v == [rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1]) - && rmm_domain::jobs::valid_windows_update_limit( - self.windows_update_max_runtime_seconds, - self.allowed_script_profiles.as_deref(), - ) + && rmm_domain::jobs::valid_windows_update_limit(self.windows_update_max_runtime_seconds) && (1..=120).contains(&self.max_runtime_seconds) } fn permits( @@ -184,11 +181,11 @@ impl SystemScope { Action::RunScript { .. } => { allow_scripts && job.action.full_maintenance() - && self.allowed_script_profiles.as_ref().is_some_and(|v| { + && (self.allowed_script_profiles.as_ref().is_some_and(|v| { v.contains( &rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1, ) - }) + }) || (self.windows_update_max_runtime_seconds.is_some() && job.action.is_windows_update_template())) && job.action.validate() && job .action diff --git a/crates/update-history/src/lib.rs b/crates/update-history/src/lib.rs index ea55d36..fb8bb9e 100644 --- a/crates/update-history/src/lib.rs +++ b/crates/update-history/src/lib.rs @@ -221,6 +221,7 @@ fn current_keyring(file: &UpdateKeyringFile) -> Result { match key.platform { Platform::LinuxX64 => ReleasePlatform::LinuxX64, Platform::WindowsX64 => ReleasePlatform::WindowsX64, + Platform::WindowsGnuX64 => ReleasePlatform::WindowsGnuX64, }, key.first_sequence, key.last_sequence, @@ -309,6 +310,7 @@ fn verify_record_release( let platform = match record.platform { Platform::LinuxX64 => ReleasePlatform::LinuxX64, Platform::WindowsX64 => ReleasePlatform::WindowsX64, + Platform::WindowsGnuX64 => ReleasePlatform::WindowsGnuX64, }; let envelope: &ReleaseEnvelope = &record.release_envelope; verify_release( @@ -402,7 +404,7 @@ fn verify_update( job.execution_identity == match record.platform { Platform::LinuxX64 => "root", - Platform::WindowsX64 => "system", + Platform::WindowsX64 | Platform::WindowsGnuX64 => "system", } && update.release_envelope == record.release_envelope && agent_update::handoff_matches_job(&header, &job) diff --git a/crates/updater-worker/src/main.rs b/crates/updater-worker/src/main.rs index 9ced5b1..735ef85 100644 --- a/crates/updater-worker/src/main.rs +++ b/crates/updater-worker/src/main.rs @@ -44,7 +44,8 @@ mod linux { pub fn run() -> Result<()> { let args: Vec<_> = std::env::args_os().skip(1).collect(); if args.first().is_some_and(|arg| arg == "--verify-bootstrap") { - let release = rmm_updater::verify_bootstrap_files(rmm_updater::Platform::LinuxX64, &args[1..])?; + let release = + rmm_updater::verify_bootstrap_files(rmm_updater::Platform::LinuxX64, &args[1..])?; println!("{}", serde_json::to_string(release.manifest())?); return Ok(()); } diff --git a/crates/updater/src/bootstrap.rs b/crates/updater/src/bootstrap.rs index 71b523e..64d7bc2 100644 --- a/crates/updater/src/bootstrap.rs +++ b/crates/updater/src/bootstrap.rs @@ -1,8 +1,16 @@ //! Local verification for the separately distributed stable bootstrap updater. -use crate::{ArtifactRole, Platform, SignedRelease, TrustedContext, TrustedUpdateKey, UpdateKeyring, VerifiedRelease}; +use crate::{ + ArtifactRole, Platform, SignedRelease, TrustedContext, TrustedUpdateKey, UpdateKeyring, + VerifiedRelease, +}; use anyhow::{Context, Result, ensure}; use rmm_domain::agent_update::{Platform as WirePlatform, ReleaseEnvelope, UpdateKeyringFile}; -use std::{ffi::OsString, fs::File, io::Read, time::{SystemTime, UNIX_EPOCH}}; +use std::{ + ffi::OsString, + fs::File, + io::Read, + time::{SystemTime, UNIX_EPOCH}, +}; fn bounded(path: &OsString, limit: u64) -> Result> { let mut bytes = Vec::new(); @@ -19,26 +27,53 @@ pub fn verify_bootstrap_files(platform: Platform, args: &[OsString]) -> Result Platform::LinuxX64, - WirePlatform::WindowsX64 => Platform::WindowsX64, - WirePlatform::WindowsGnuX64 => Platform::WindowsGnuX64, - }; - let raw = hex::decode(key.public_key_hex)?.try_into().ok().context("Ed25519 public key length")?; - TrustedUpdateKey::new(raw, platform, key.first_sequence, key.last_sequence, key.revoked) - }).collect::>>()?; + let keys = config + .keys + .into_iter() + .map(|key| { + let platform = match key.platform { + WirePlatform::LinuxX64 => Platform::LinuxX64, + WirePlatform::WindowsX64 => Platform::WindowsX64, + WirePlatform::WindowsGnuX64 => Platform::WindowsGnuX64, + }; + let raw = hex::decode(key.public_key_hex)? + .try_into() + .ok() + .context("Ed25519 public key length")?; + TrustedUpdateKey::new( + raw, + platform, + key.first_sequence, + key.last_sequence, + key.revoked, + ) + }) + .collect::>>()?; let ring = UpdateKeyring::new(config.generation, keys)?; let envelope: ReleaseEnvelope = serde_json::from_slice(&bounded(envelope, 32768)?)?; - let decoded = envelope.decode().map_err(|_| anyhow::anyhow!("invalid release envelope"))?; + let decoded = envelope + .decode() + .map_err(|_| anyhow::anyhow!("invalid release envelope"))?; let context = TrustedContext { - platform, now_utc_seconds: SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs(), - keyring_generation: config.generation, protocol_major: 1, state_schema: 1, - updater_version: 1, minimum_release_sequence: 0, active_release: None, + platform, + now_utc_seconds: SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs(), + keyring_generation: config.generation, + protocol_major: 1, + state_schema: 1, + updater_version: if platform == Platform::WindowsGnuX64 { 2 } else { 1 }, + minimum_release_sequence: 0, + active_release: None, }; - let release = crate::verify_release(SignedRelease {schema_version: envelope.schema_version, - key_id: &envelope.key_id, payload_bytes: &decoded.payload_bytes, - signature: &decoded.signature}, &ring, &context)?; + let release = crate::verify_release( + SignedRelease { + schema_version: envelope.schema_version, + key_id: &envelope.key_id, + payload_bytes: &decoded.payload_bytes, + signature: &decoded.signature, + }, + &ring, + &context, + )?; release.verify_artifact(ArtifactRole::Agent, File::open(agent)?)?; release.verify_artifact(ArtifactRole::Helper, File::open(helper)?)?; Ok(release) diff --git a/tests/linux_release_bootstrap.py b/tests/linux_release_bootstrap.py index e2cf653..bc56e8a 100644 --- a/tests/linux_release_bootstrap.py +++ b/tests/linux_release_bootstrap.py @@ -1,7 +1,9 @@ """Retry coverage for the HTTPS Linux bootstrap without installing on the test host.""" -from contextlib import ExitStack +from contextlib import ExitStack, redirect_stdout from importlib.util import module_from_spec, spec_from_file_location +import io import json +import sqlite3 from pathlib import Path import tempfile import unittest @@ -29,12 +31,26 @@ class EnrollmentRetry(unittest.TestCase): for field, value in [('ROOT',root),('STATE',self.state)]: self.stack.enter_context(patch.object(lifecycle,field,value)) self.enable = self.stack.enter_context(patch.object(lifecycle,'enable')) + self.service = self.stack.enter_context(patch.object(lifecycle,'service')) self.healthy = self.stack.enter_context(patch.object(lifecycle,'healthy',return_value=True)) self.save = self.stack.enter_context(patch.object(lifecycle,'save_token')) self.stack.enter_context(patch.object(lifecycle.pwd,'getpwnam')) self.token = folder / 'retry.token' self.token.write_text('0' * 64) + def test_unresolved_effects_delay_release_without_blocking_closed_jobs(self): + with sqlite3.connect(self.state / 'journal.sqlite') as db: + db.execute('CREATE TABLE job_attempts (state TEXT)') + for state, expected in [('running','busy'), ('unknown','busy'), ('awaiting_reboot','busy'), + ('closed_unknown','idle'), ('accepted','idle'), ('succeeded','idle')]: + with self.subTest(state=state): + db.execute('DELETE FROM job_attempts') + db.execute('INSERT INTO job_attempts VALUES(?)', (state,)) + db.commit() + with redirect_stdout(io.StringIO()) as output: + lifecycle.device_idle() + self.assertEqual(output.getvalue().strip(), expected) + def bootstrap_tenant(self, tenant): def answer(source, route, target): self.assertEqual(source, (self.base,'0' * 64)) @@ -57,6 +73,17 @@ class EnrollmentRetry(unittest.TestCase): self.save.assert_called_once() self.healthy.assert_called_once_with('1.0',0) + def test_timer_starts_only_after_confirmed_first_connection(self): + self.bootstrap_tenant(self.tenant) + self.healthy.return_value = False + with self.assertRaisesRegex(ValueError, 'awaiting authenticated device heartbeat'): + lifecycle.install(self.base,self.token) + self.enable.assert_called_once() + self.service.assert_not_called() + self.healthy.return_value = True + lifecycle.install(self.base,self.token) + self.service.assert_called_once_with('enable','--now',lifecycle.TIMER) + def test_token_for_another_tenant_cannot_replace_initial_token(self): self.bootstrap_tenant('22222222-2222-4222-8222-222222222222') with self.assertRaisesRegex(ValueError,'different enrollment tenant'): diff --git a/tests/release_bootstrap_gateway.py b/tests/release_bootstrap_gateway.py new file mode 100644 index 0000000..868a6a6 --- /dev/null +++ b/tests/release_bootstrap_gateway.py @@ -0,0 +1,229 @@ +"""Exercise release publication and fresh enrollment on disposable loopback TLS/PostgreSQL.""" +import argparse +import base64 +from contextlib import ExitStack +import hashlib +import http.client +from http.cookies import SimpleCookie +import json +import os +from pathlib import Path +import secrets +import shutil +import socket +import ssl +import subprocess +import sys +import tempfile +import time +from types import SimpleNamespace +from urllib.parse import urlencode +import uuid + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tools')) +import agent_release as release + +TENANT = '11111111-1111-4111-8111-111111111111' + + +def run(*cmd, data=None): + return subprocess.run(cmd, input=data, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True).stdout + + +def test(binary, directory, stack): + db = 'rmm-bootstrap-' + uuid.uuid4().hex[:12] + password = secrets.token_hex(24) + run('docker','run','-d','--name',db,'-e','POSTGRES_PASSWORD=' + password,'-p','127.0.0.1::5432','postgres:17.6-bookworm') + stack.callback(run,'docker','rm','-f',db) + for _ in range(80): + if b'init process complete' in run('docker','logs',db) and subprocess.run( + ['docker','exec',db,'pg_isready','-U','postgres'],stdout=subprocess.DEVNULL).returncode == 0: + break + time.sleep(.25) + port = run('docker','port',db,'5432/tcp').decode().strip().split(':')[-1] + def sql(role, database, script): + return run('docker','exec','-i',db,'psql','-XAtq','-v','ON_ERROR_STOP=1','-U',role,'-d',database,data=script.encode()).decode().strip() + sql('postgres','postgres', f"""CREATE ROLE rmm_migrator LOGIN NOSUPERUSER NOBYPASSRLS PASSWORD '{password}'; + CREATE ROLE rmm_app LOGIN NOSUPERUSER NOBYPASSRLS PASSWORD '{password}'; + CREATE DATABASE rmm OWNER rmm_migrator;""") + sql('postgres','rmm','ALTER SCHEMA public OWNER TO rmm_migrator; REVOKE CREATE ON SCHEMA public FROM PUBLIC;') + for migration in sorted((ROOT/'migrations').glob('*.sql')): + sql('rmm_migrator','rmm',migration.read_text()) + assert sql('rmm_migrator','rmm',"SELECT NOT rolsuper AND NOT rolbypassrls FROM pg_roles WHERE rolname=current_user") == 't' + def permissions(): + return sql('rmm_app','rmm',f"""BEGIN; SET LOCAL app.tenant_id='{TENANT}'; + SELECT ('devices:enroll'=ANY(permissions))::int FROM memberships WHERE principal='simulated-testprincipal-a'; COMMIT;""") + assert permissions() == '1', 'migration 0025 must grant devices:enroll through tenant RLS' + print('Migrations and enrollment permission verified with NOSUPERUSER NOBYPASSRLS migration owner') + + artifacts = directory/'bin' + artifacts.mkdir() + files = ('/bin/true','/bin/false','/usr/bin/env') + state = directory/'release' + def make_release(sequence): + for name, system_file in zip(release.FILES[release.LINUX], files): + shutil.copyfile(system_file,artifacts/name) + helper=artifacts/release.FILES[release.LINUX][1] + with helper.open('ab') as f: f.write(bytes([sequence])) + hashes={p.name:hashlib.sha256(p.read_bytes()).hexdigest() for p in artifacts.iterdir()} + receipt=directory/'receipt.json' + receipt.write_text(json.dumps({'sha256':hashes,'target':release.LINUX})) + release.prepare(SimpleNamespace(artifacts=artifacts,build_receipt=receipt,service_uid=os.getuid(),state=state, + sequence=sequence,version='test.'+str(sequence),gateway='https://localhost',days=1)) + env=json.loads((state/'prepared/linux'/str(sequence)/'envelope.json').read_bytes()) + return env, json.loads(base64.urlsafe_b64decode(env['payload_bytes']+'===')) + a, ma = make_release(1) + tls=directory/'tls' + tls.mkdir() + run('openssl','req','-x509','-newkey','ec','-pkeyopt','ec_paramgen_curve:prime256v1','-nodes','-days','1', + '-subj','/CN=Test CA','-addext','basicConstraints=critical,CA:TRUE','-keyout',str(tls/'ca.key'),'-out',str(tls/'ca.pem')) + def csr(name): + run('openssl','req','-new','-newkey','ec','-pkeyopt','ec_paramgen_curve:prime256v1','-nodes','-subj','/CN='+name, + '-keyout',str(tls/(name+'.key')),'-out',str(tls/(name+'.csr'))) + csr('server') + extensions=tls/'san' + extensions.write_text('subjectAltName=DNS:localhost\nextendedKeyUsage=serverAuth\n') + run('openssl','x509','-req','-in',str(tls/'server.csr'),'-CA',str(tls/'ca.pem'),'-CAkey',str(tls/'ca.key'), + '-CAcreateserial','-days','1','-extfile',str(extensions),'-out',str(tls/'server.pem')) + csr('device') + with socket.socket() as s: + s.bind(('127.0.0.1',0)); http_port=s.getsockname()[1] + origin='https://localhost:'+str(http_port) + env=dict(os.environ,DATABASE_URL=f'postgres://rmm_app:{password}@127.0.0.1:{port}/rmm', + RMM_PROFILE='development',RMM_DEV_AUTH='true',RMM_DEV_USERNAME='acceptance',RMM_DEV_PASSWORD=secrets.token_hex(24), + RMM_VIEWER_A=secrets.token_hex(24),RMM_VIEWER_B=secrets.token_hex(24),RMM_SERVER_ROLE='all', + RMM_LISTEN_ADDR=f'127.0.0.1:{http_port}',RMM_UI_ORIGIN=origin,RMM_TLS_DIR=str(tls), + RMM_JOB_KEY_FILE=str(directory/'job-keys/key.pem'),RMM_AGENT_UPDATE_ARTIFACT_DIR=str(state/'objects'), + RMM_AGENT_UPDATE_LINUX_KEYRING_FILE=str(state/'config/linux-keyring.json'), + RMM_AGENT_BOOTSTRAP_LINUX_FILE=str(state/'config/linux-bootstrap.json'),RMM_BOOTSTRAP_ORIGIN=origin) + log=stack.enter_context((directory/'server.log').open('w+')) + server=subprocess.Popen([binary],cwd=directory,env=env,stdout=log,stderr=log) + def stop_server(): + if server.poll() is None: + server.terminate() + server.wait(timeout=15) + stack.callback(stop_server) + ctx=ssl.create_default_context(cafile=str(tls/'ca.pem')) + def ask(method,path,body=None,headers=None,status=200): + conn=http.client.HTTPSConnection('localhost',http_port,context=ctx,timeout=10) + try: + headers=dict(headers or {}) + if isinstance(body,dict): + body=json.dumps(body).encode();headers['Content-Type']='application/json' + conn.request(method,path,body=body,headers=headers) + response=conn.getresponse();raw=response.read() + expected=(status,) if isinstance(status,int) else status + assert response.status in expected, (method,path.split('?')[0],response.status,expected) + return response.headers, json.loads(raw) if response.getheader('content-type','').startswith('application/json') else raw + finally: + conn.close() + for _ in range(70): + if server.poll() is not None: + log.seek(0); raise RuntimeError(log.read()) + try: + ask('GET','/healthz') + break + except OSError: + time.sleep(.1) + hdr,_=ask('POST','/auth/dev/login',{'username':'acceptance','password':env['RMM_DEV_PASSWORD']},{'Origin':origin}) + cookie=SimpleCookie(hdr['set-cookie'])['__Host-rmm_session'].value + auth={'Cookie':'__Host-rmm_session='+cookie,'Origin':origin} + _,session=ask('GET','/auth/session',headers=auth) + auth['X-CSRF-Token']=session['csrf_token'] + path='/api/v1/tenants/'+TENANT + def upload(blob, data, status=200): + query=urlencode({'artifact_id':blob['artifact_id'],'sha256':blob['sha256'],'size_bytes':blob['bytes']}) + ask('POST',path+'/agent-update-artifacts?'+query,data,{**auth,'Content-Type':'application/octet-stream'},status) + def stable_blob(data): + return {'artifact_id':str(uuid.uuid4()),'sha256':hashlib.sha256(data).hexdigest(),'bytes':len(data)} + original_worker=(artifacts/'rmm-agent-updater').read_bytes() + original=stable_blob(original_worker) + upload(original,original_worker) + def send_pair(manifest): + for name,blob in zip(release.FILES[release.LINUX],manifest['artifacts']): + upload(blob,(artifacts/name).read_bytes()) + send_pair(ma) + ask('POST',path+'/agent-releases',a,auth) + ask('POST',path+'/agent-releases',a,auth) + b,mb=make_release(2) + upload(mb['artifacts'][0],(artifacts/'rmm-agent').read_bytes()) + ask('POST',path+'/agent-releases',b,auth,404) + partial=mb['artifacts'][1] + upload(partial,(artifacts/'rmm-privileged-helper').read_bytes()[:-1],400) + ask('POST',path+'/agent-releases',b,auth,404) + h,token=ask('POST',path+'/enrollment-tokens',{},auth) + assert 'no-store' in h['Cache-Control'] + bootstrap='/agent/v1/bootstrap/current?platform='+release.LINUX + credential={'Authorization':'Enrollment '+token['token']} + _,active=ask('GET',bootstrap,headers=credential) + assert active['manifest']['release_id']==ma['release_id'] and active['job_signing_public_key_hex'] + assert active['tenant_id']==TENANT + ask('GET',bootstrap,headers={'Authorization':'Enrollment '+'0'*64},status=401) + blob=ma['artifacts'][0] + download=f"/agent/v1/bootstrap/releases/{ma['release_id']}/artifacts/{blob['artifact_id']}?platform={release.LINUX}" + _,raw=ask('GET',download,headers=credential) + assert hashlib.sha256(raw).hexdigest()==blob['sha256'] + def updater(digest=None, status=200): + return ask('GET','/agent/v1/bootstrap/updater?platform='+release.LINUX+('&sha256='+digest if digest else ''), + headers=credential,status=status)[1] + assert updater()==original_worker + replacement_worker=original_worker+b'test-new-worker' + new=stable_blob(replacement_worker) + pin={'sha256':new['sha256'],'bytes':new['bytes']} + try: + release.rotate_pin(state,'linux',TENANT,original['sha256'],pin) + raise AssertionError('missing upload changed pointer') + except FileNotFoundError: + pass + assert updater()==original_worker + upload(new,replacement_worker) + try: + release.rotate_pin(state,'linux',TENANT,'0'*64,pin) + raise AssertionError('unexpected predecessor accepted') + except ValueError: + pass + assert updater()==original_worker + release.rotate_pin(state,'linux',TENANT,original['sha256'],pin) + release.rotate_pin(state,'linux',TENANT,original['sha256'],pin) + assert updater()==replacement_worker and updater(original['sha256'])==original_worker + assert ask('GET',bootstrap,headers=credential)[1]['updater']==pin + updater('0'*64,404) + assert json.loads((state/'config/linux-bootstrap.json').read_bytes())['updater_sha256']==original['sha256'] + # Set up B's already published default worker. This fixture has only one + # interactive dev principal (tenant A), so seed the independent tenant directly. + tenant_b='22222222-2222-4222-8222-222222222222' + token_b=secrets.token_hex(32) + sql('rmm_app','rmm',f"BEGIN; SET LOCAL app.tenant_id='{tenant_b}'; SELECT issue_enrollment_token('{hashlib.sha256(token_b.encode()).hexdigest()}'); COMMIT;") + b_store=state/'objects'/tenant_b + b_store.mkdir(mode=0o700) + shutil.copy2(state/'objects'/TENANT/original['sha256'],b_store/original['sha256']) + b_auth={'Authorization':'Enrollment '+token_b} + assert ask('GET','/agent/v1/bootstrap/updater?platform='+release.LINUX,headers=b_auth)[1]==original_worker + ask('GET','/agent/v1/bootstrap/updater?platform='+release.LINUX+'&sha256='+new['sha256'],headers=b_auth,status=404) + print('tenant worker rotation rejects missing object and stale predecessor; default and in-flight downloads survive') + install={'token':token['token'],'csr_pem':(tls/'device.csr').read_text()} + _,enrolled=ask('POST','/agent/v1/enroll',install) + assert enrolled['certificate_pem'] + ask('POST','/agent/v1/enroll',install,status=401) + ask('GET',bootstrap,headers=credential,status=401) + ask('GET',download,headers=credential,status=401) + ask('GET','/agent/v1/releases/current?platform='+release.LINUX,status=401) + cert = tls/'issued-device.pem' + cert.write_text(enrolled['certificate_pem']) + ctx.load_cert_chain(str(cert),str(tls/'device.key')) + _,trust=ask('GET','/agent/v1/releases/device-trust') + assert trust['job_signing_public_key_hex'] == active['job_signing_public_key_hex'] + upload(partial,(artifacts/'rmm-privileged-helper').read_bytes()) + ask('POST',path+'/agent-releases',b,auth) + print('A stays current after missing/truncated B; B publishes only after complete retry') + print('Unknown token, second enrollment, post-consumption metadata/object fetch and absent mTLS rejected') + + +if __name__ == '__main__': + parser=argparse.ArgumentParser(description=__doc__) + parser.add_argument('--server-binary',required=True,type=Path) + args=parser.parse_args() + with ExitStack() as cleanup: + folder=Path(cleanup.enter_context(tempfile.TemporaryDirectory())) + test(args.server_binary.resolve(),folder,cleanup) diff --git a/tools/agent_release.py b/tools/agent_release.py index fd003ac..bb3253f 100755 --- a/tools/agent_release.py +++ b/tools/agent_release.py @@ -2,6 +2,7 @@ """Prepare an Ed25519 agent release on the signing host and publish through the catalog API.""" import argparse import base64 +import fcntl import getpass import hashlib import http.client @@ -50,6 +51,22 @@ def fingerprint(path): return digest.hexdigest(), size +def verify_native_image(path, target, size): + with path.open('rb') as binary: + header = binary.read(64) + check(len(header) == 64, 'truncated binary header: ' + path.name) + if target == LINUX: + check(header[:6] == b'\x7fELF\x02\x01' and header[16:18] in (b'\x02\x00', b'\x03\x00') + and header[18:20] == b'\x3e\x00', 'Linux x86-64 ELF image required: ' + path.name) + else: + pe = int.from_bytes(header[60:64], 'little') + check(header[:2] == b'MZ' and 64 <= pe <= size - 26, 'Windows PE image required: ' + path.name) + binary.seek(pe) + coff = binary.read(26) + check(coff[:6] == b'PE\0\0\x64\x86' and coff[24:26] == b'\x0b\x02', + 'Windows x86-64 PE32+ image required: ' + path.name) + + def binaries(folder, receipt): record = json.loads(receipt.read_text(encoding='utf-8-sig')) if 'target' in record and 'sha256' in record: @@ -65,6 +82,7 @@ def binaries(folder, receipt): for name in FILES[target]: path = folder / name digest, size = fingerprint(path) + verify_native_image(path, target, size) check(digest == hashes[name], 'file differs from build receipt: ' + name) result.append({'artifact_id': str(uuid.uuid5(uuid.NAMESPACE_OID, target + '/' + name + '/' + digest)), 'sha256': digest, 'bytes': size}) @@ -108,11 +126,22 @@ def https(value): def prepare(args): + provision_folder(args.state) + provision_folder(args.state/'private') + with (args.state/'private'/'signing.lock').open('a') as lock: + fcntl.flock(lock, fcntl.LOCK_EX) + prepare_locked(args) + + +def prepare_locked(args): https(args.gateway) check(0 < args.sequence < 2**63 and re.fullmatch('[A-Za-z0-9.+_-]{1,64}', args.version), 'invalid release version/sequence') + minimum = getattr(args, 'minimum_updater_version', 1) + check(minimum in (1,2), 'unknown minimum updater version') check(0 < args.days <= 3650, 'invalid validity in days') platform, blobs = binaries(args.artifacts, args.build_receipt) label = {LINUX:'linux', WINDOWS:'windows', WINDOWS_GNU:'windows-gnu'}[platform] + check(minimum == 1 or platform == WINDOWS_GNU, 'version 2 currently applies to the GNU Windows worker') provision_folder(args.state) config, private = args.state / 'config', args.state / 'private' provision_folder(config, args.service_uid) @@ -146,13 +175,15 @@ def prepare(args): prepared = args.state / 'prepared' / label / str(args.sequence) provision_folder(prepared.parent) if prepared.exists(): - check(json.loads((prepared / 'inputs.json').read_bytes()) == {'version': args.version, 'blobs': blobs}, 'sequence already bound') + prior = json.loads((prepared / 'inputs.json').read_bytes()) + check(prior.get('version') == args.version and prior.get('blobs') == blobs + and prior.get('minimum_updater_version', 1) == minimum, 'sequence already bound') print(prepared / 'envelope.json') return now = int(time.time()) manifest = {'schema_version': 1, 'release_id': str(uuid.uuid4()), 'release_sequence': args.sequence, 'version': args.version, 'platform': platform, 'published_at': now, 'expires_at': now + args.days * 86400, - 'protocol_major': 1, 'state_schema': 1, 'minimum_updater_version': 1, + 'protocol_major': 1, 'state_schema': 1, 'minimum_updater_version': minimum, 'artifacts': [dict(role=role, **blob) for role, blob in zip(('agent', 'helper'), blobs[:2])]} raw = encoded(manifest) with tempfile.TemporaryDirectory(dir=prepared.parent) as tmpdir: @@ -168,7 +199,7 @@ def prepare(args): output.mkdir() env = {'schema_version': 1, 'key_id': hashlib.sha256(pub).hexdigest(), 'payload_bytes': b64(raw), 'signature': b64(sig)} save_once(output / 'envelope.json', encoded(env)) - save_once(output / 'inputs.json', encoded({'version': args.version, 'blobs': blobs})) + save_once(output / 'inputs.json', encoded({'version': args.version, 'blobs': blobs, 'minimum_updater_version': minimum})) output.rename(prepared) print(prepared / 'envelope.json') @@ -197,12 +228,59 @@ class Publisher: conn.close() +def rotate_pin(state, label, tenant, expected, new_pin): + tenant = str(uuid.UUID(tenant)) + check(re.fullmatch('[0-9a-f]{64}', expected), 'expected predecessor SHA-256 required') + directory = state/'config' + default = directory/(label + '-bootstrap.json') + path = default.with_suffix('.' + tenant + '.json') + # The already completed upload attests the API read and stored the object. + # Independently reopen the actual tenant object before changing the pin. + sha, size = fingerprint(state/'objects'/tenant/new_pin['sha256']) + check(new_pin == {'sha256':sha,'bytes':size}, 'uploaded stable updater differs') + with (state/'private'/'bootstrap-rotation.lock').open('a') as lock: + fcntl.flock(lock, fcntl.LOCK_EX) + if path.exists(): + pins = json.loads(path.read_bytes()) + else: + baseline = json.loads(default.read_bytes()) + pins = {'current':{'sha256':baseline['updater_sha256'],'bytes':baseline['updater_bytes']},'previous':[]} + now = int(time.time()) + if pins['current'] == new_pin: + prior = pins['previous'][-1]['pin']['sha256'] if pins['previous'] else None + check(expected in (prior, new_pin['sha256']), 'bootstrap predecessor changed') + return + check(pins['current']['sha256'] == expected, 'bootstrap predecessor changed') + previous = [item for item in pins['previous'] if item['expires_at'] > now + and item['pin'] not in (new_pin,pins['current'])] + previous.append({'pin':pins['current'],'expires_at':now+900}) + raw = encoded({'current':new_pin,'previous':previous}) + check(len(raw) <= 32768, 'too many concurrent bootstrap rotations') + with tempfile.NamedTemporaryFile(dir=directory,delete=False) as f: + try: + f.write(raw) + os.fchmod(f.fileno(),0o600) + os.fchown(f.fileno(),default.stat().st_uid,default.stat().st_gid) + f.flush() + os.fsync(f.fileno()) + os.replace(f.name,path) + finally: + Path(f.name).unlink(missing_ok=True) + fd = os.open(directory,os.O_DIRECTORY) + try: + os.fsync(fd) + finally: + os.close(fd) + + def publish(args): platform, blobs = binaries(args.artifacts, args.build_receipt) env = json.loads(args.envelope.read_bytes()) manifest = json.loads(base64.urlsafe_b64decode(env['payload_bytes'] + '===')) check(manifest['platform'] == platform and manifest['artifacts'] == [dict(role=role, **blob) for role, blob in zip(('agent', 'helper'), blobs[:2])], 'input files differ from release envelope') + check(bool(args.rotate_bootstrap_from) == bool(args.bootstrap_state), 'rotation needs previous SHA and local signing state') + check(not (args.rotate_bootstrap_from and args.bootstrap_worker), 'a rotation uses the new receipt-bound stable worker') client = Publisher(args.origin, args.ca) if args.dev_login_file: path = args.dev_login_file @@ -222,6 +300,7 @@ def publish(args): if args.bootstrap_worker: paths[2] = args.bootstrap_worker sha, size = fingerprint(paths[2]) + verify_native_image(paths[2], platform, size) name = FILES[platform][2] blobs[2] = {'artifact_id': str(uuid.uuid5(uuid.NAMESPACE_OID, platform + '/' + name + '/' + sha)), 'sha256': sha, 'bytes': size} @@ -231,6 +310,11 @@ def publish(args): result = client.request('POST', path + '/agent-update-artifacts?' + query, data, {'Content-Type': 'application/octet-stream', 'Content-Length': str(blob['bytes'])}) check(result['sha256'] == blob['sha256'] and result['size_bytes'] == blob['bytes'], 'artifact receipt differs') + if args.rotate_bootstrap_from: + stable = blobs[2] + label = {LINUX:'linux', WINDOWS:'windows', WINDOWS_GNU:'windows-gnu'}[platform] + rotate_pin(args.bootstrap_state,label,tenant,args.rotate_bootstrap_from, + {'sha256':stable['sha256'],'bytes':stable['bytes']}) result = client.request('POST', path + '/agent-releases', encoded(env), {'Content-Type': 'application/json'}) check(result['release_envelope'] == env, 'published envelope changed') print(json.dumps({'release_id': result['release_id'], 'version': manifest['version'], 'platform': platform, @@ -253,9 +337,12 @@ def main(): cmd.add_argument('--sequence', type=int, required=True) cmd.add_argument('--version', required=True) cmd.add_argument('--days', type=int, default=90) + cmd.add_argument('--minimum-updater-version',type=int,default=1) cmd.add_argument('--service-uid', type=int, default=os.getuid()) else: cmd.add_argument('--origin', required=True) + cmd.add_argument('--bootstrap-state',type=Path,help='Local signing host state for an explicit stable updater rotation') + cmd.add_argument('--rotate-bootstrap-from',help='Compare-and-swap the tenant pin from this SHA-256 after verified upload') cmd.add_argument('--bootstrap-worker', type=Path, help='Pinned initial stable updater when publishing subsequent code pairs') cmd.add_argument('--ca', type=Path) cmd.add_argument('--tenant', required=True) diff --git a/tools/dev_agent_release.py b/tools/dev_agent_release.py index a0b5153..6712920 100755 --- a/tools/dev_agent_release.py +++ b/tools/dev_agent_release.py @@ -20,6 +20,7 @@ def on_server(options): from pathlib import Path import shutil import subprocess + import uuid import tempfile os.umask(0o077) @@ -49,7 +50,7 @@ def on_server(options): raise ValueError('sequence already contains different build: ' + name) inputs = ['--artifacts', str(build), '--build-receipt', str(build / options['receipt'])] subprocess.run(['python3', '-I', str(tool), 'prepare', *inputs, '--state', str(state), '--gateway', options['gateway'], - '--service-uid','10001','--sequence',str(options['sequence']),'--version',options['version']],check=True) + '--service-uid','10001','--minimum-updater-version',str(options['minimum']),'--sequence',str(options['sequence']),'--version',options['version']],check=True) if options['label'].startswith('windows'): import re import uuid @@ -91,10 +92,20 @@ def on_server(options): with tempfile.NamedTemporaryFile(mode='w', dir=state) as auth: json.dump({'username':env['RMM_DEV_USERNAME'],'password':env['RMM_DEV_PASSWORD']},auth) auth.flush() + label=options['label'] + tenant=str(uuid.UUID(options['tenant'])) + pin=(state/'config'/(label+'-bootstrap.json')).with_suffix('.'+tenant+'.json') + if options['rotate']: + selection=['--rotate-bootstrap-from',options['rotate'],'--bootstrap-state',str(state)] + elif pin.is_file(): + sha=json.loads(pin.read_bytes())['current']['sha256'] + selection=['--bootstrap-worker',str(state/'objects'/tenant/sha)] + else: + selection=['--bootstrap-worker',str(state/'bootstrap-workers'/label)] subprocess.run(['python3','-I',str(tool),'publish',*inputs,'--tenant',options['tenant'], '--envelope',str(state/'prepared'/options['label']/str(options['sequence'])/'envelope.json'), '--origin','https://localhost:18443','--ca','/opt/rust-discovery/devstate/api-pki/ca.pem', - '--dev-login-file',auth.name,'--bootstrap-worker',str(state/'bootstrap-workers'/options['label'])],check=True) + '--dev-login-file',auth.name,*selection],check=True) def main(): import inspect @@ -103,6 +114,8 @@ def main(): parser.add_argument('--build-receipt',type=Path,required=True) parser.add_argument('--sequence',type=int,required=True) parser.add_argument('--version',required=True) + parser.add_argument('--minimum-updater-version',type=int,default=1) + parser.add_argument('--rotate-bootstrap-from',help='Rotate only this tenant from the given previous worker SHA-256') parser.add_argument('--gateway',default='https://discovery.ease-technologies.de') parser.add_argument('--tenant',default='11111111-1111-4111-8111-111111111111') parser.add_argument('--prepare-only',action='store_true') @@ -114,7 +127,7 @@ def main(): raise ValueError('build receipt and binary names must differ') params={'files':{f.name:hashlib.sha256(f.read_bytes()).hexdigest() for f in files},'tool':hashlib.sha256(tool.read_bytes()).hexdigest(), 'receipt':args.build_receipt.name,'sequence':args.sequence,'version':args.version,'tenant':args.tenant, - 'gateway':args.gateway,'prepare_only':args.prepare_only,'label':{agent_release.LINUX:'linux', + 'minimum':args.minimum_updater_version,'rotate':args.rotate_bootstrap_from,'gateway':args.gateway,'prepare_only':args.prepare_only,'label':{agent_release.LINUX:'linux', agent_release.WINDOWS:'windows',agent_release.WINDOWS_GNU:'windows-gnu'}[platform]} subprocess.run([sys.executable,str(ROOT/'tools/dev_stage.py'),'server',*map(str,files),str(tool)],check=True) command=inspect.getsource(on_server)+'\non_server('+repr(params)+')\n' diff --git a/tools/dev_deploy_server.py b/tools/dev_deploy_server.py index 0ec1d0b..1b8e42e 100644 --- a/tools/dev_deploy_server.py +++ b/tools/dev_deploy_server.py @@ -7,7 +7,6 @@ import json from pathlib import Path import re import subprocess -import sys def sha(data): @@ -29,6 +28,39 @@ def write_atomic(path, data, mode=0o644): tmp.unlink(missing_ok=True) +def pack_web(dist): + if not (dist / 'index.html').is_file(): + raise ValueError('A built WebUI directory with index.html is required') + files = {} + for file in dist.rglob('*'): + if file.is_symlink(): + raise ValueError('Web build contains a symlink') + if file.is_file(): + files[file.relative_to(dist).as_posix()] = base64.b64encode(file.read_bytes()).decode() + return files + + +def stage_web(dist, files): + if 'index.html' not in files: + raise ValueError('Web build has no index.html') + for name, encoded in files.items(): + path = dist / name + if Path(name).is_absolute() or '..' in Path(name).parts or path.resolve() != path: + raise ValueError('Invalid WebUI asset path') + if name != 'index.html': + write_atomic(path, base64.b64decode(encoded, validate=True)) + return base64.b64decode(files['index.html'], validate=True) + + +def publish_index(root, dist, index): + write_atomic(dist / 'index.html', index) + actual = subprocess.check_output(['curl','--noproxy','*','--max-time','10','--cacert', + str(root/'devstate/api-pki/ca.pem'),'--silent','--fail','https://localhost:18443/']) + if actual != index: + raise RuntimeError('Served index differs') + return sha(actual) + + def web_files(revision): """Build the exact Git revision using the local locked frontend dependencies.""" import tempfile @@ -40,16 +72,14 @@ def web_files(revision): subprocess.run(['tar','-xf',archive,'-C',work],check=True) (work/'web/node_modules').symlink_to(root/'web/node_modules',target_is_directory=True) subprocess.run(['npm','run','build'],cwd=work/'web',check=True) - dist = work/'web/dist' - return {str(file.relative_to(dist)):base64.b64encode(file.read_bytes()).decode() - for file in dist.rglob('*') if file.is_file()} + return pack_web(work / 'web/dist') def activate(payload): import fcntl import time revision = payload['revision'] - if not re.fullmatch('[0-9a-f]{40}', revision): + if not payload['web_only'] and not re.fullmatch('[0-9a-f]{40}', revision): raise ValueError('Invalid Git revision') def run(*command): return subprocess.check_output(list(command),text=True).strip() @@ -58,10 +88,19 @@ def activate(payload): root = Path('/opt/rust-discovery') work = root/'devstate/mvp-deploy' work.mkdir(exist_ok=True,mode=0o700) - role_dir = work/revision - role_dir.mkdir(exist_ok=True,mode=0o700) with (work/'deployment.lock').open('a') as lock: fcntl.flock(lock,fcntl.LOCK_EX) + active = inspect('api') + dist = next(Path(m['Source']) for m in active['Mounts'] if m['Destination'] == '/web') + if dist != root / 'web/dist' or active['Config']['Labels']['com.docker.compose.project'] != 'rust-discovery-server': + raise RuntimeError('Unexpected development WebUI deployment') + index = stage_web(dist, payload['web']) + if payload['web_only']: + print(json.dumps(dict(revision=revision, web_index_sha256=publish_index(root, dist, index), + api_container=active['Id'], web_only=True))) + return + role_dir = work / revision + role_dir.mkdir(exist_ok=True, mode=0o700) matches = list((Path.home()/'rmm-dev-build/server-arm64/artifacts'/revision).glob('*/manifest.json')) if len(matches) != 1: raise RuntimeError('Expected one ARM64 build manifest for this revision') @@ -70,7 +109,6 @@ def activate(payload): if (manifest['revision'] != revision or manifest['target'] != 'aarch64-unknown-linux-gnu' or sha(binary) != manifest['sha256']['rmm-server'] or sha(matches[0].read_bytes()) != matches[0].parent.name): raise RuntimeError('Server build digest mismatch') - active = inspect('api') if inspect('gateway')['Image'] != active['Image']: raise RuntimeError('API and Gateway have different base images') labels = active['Config']['Labels'] @@ -111,13 +149,6 @@ def activate(payload): raise RuntimeError('Dev admin/gateway boundary changed') if config['postgres'].get('ports'): raise RuntimeError('Unexpected public database') - dist=next(Path(m['Source']) for m in active['Mounts'] if m['Destination']=='/web') - assets={name:base64.b64decode(data,validate=True) for name,data in payload['web'].items()} - if 'index.html' not in assets or any(Path(n).is_absolute() or '..' in Path(n).parts for n in assets): - raise ValueError('Invalid WebUI asset set') - for name,data in assets.items(): - if name!='index.html': - write_atomic(dist/name,data) run(*compose,'run','--no-deps','-T','--rm','migrate') sql = ['docker','exec','rust-discovery-server-postgres-1','psql','-X','-qAt','-v','ON_ERROR_STOP=1','-U','postgres','-d','rmm','-c'] recorded = dict(line.split('|') for line in run(*sql,"SELECT version,encode(checksum,'hex') FROM _sqlx_migrations WHERE success").splitlines()) @@ -136,12 +167,7 @@ def activate(payload): live=run('docker','exec','rust-discovery-server-'+role+'-1','sha256sum','/usr/local/bin/rmm-server').split()[0] if live!=sha(binary): raise RuntimeError(role+' running digest differs') - write_atomic(dist/'index.html',assets['index.html']) - index=subprocess.check_output(['curl','--noproxy','*','--cacert',str(root/'devstate/api-pki/ca.pem'), - '--silent','--fail','https://localhost:18443/']) - if index!=assets['index.html']: - raise RuntimeError('Served index differs') - print(json.dumps(dict(revision=revision,server_sha256=sha(binary),web_index_sha256=sha(index), + print(json.dumps(dict(revision=revision,server_sha256=sha(binary),web_index_sha256=publish_index(root, dist, index), image=inspect('api')['Image'],api_health='ok',gateway_health='ok'))) @@ -150,13 +176,20 @@ def main(): import inspect import shlex parser=argparse.ArgumentParser(description=__doc__) - parser.add_argument('--ref',required=True) + parser.add_argument('--ref', help='Exact Git revision for the server and WebUI') + parser.add_argument('--web-only', action='store_true', help='Only activate built web assets') + parser.add_argument('--web-dist', type=Path, help='Use an existing WebUI build with --web-only') parser.add_argument('--overlay',action='append',default=[],help='Deploy and use this compose overlay from the selected Git revision') args=parser.parse_args() - revision=subprocess.check_output(['git','rev-parse','--verify','--end-of-options',args.ref+'^{commit}'],text=True).strip() - web=web_files(revision) + if args.web_dist and (not args.web_only or args.ref) or args.web_only and args.overlay: + parser.error('Choose --web-only with either --web-dist or --ref and no compose overlay') + if not args.web_dist and not args.ref: + parser.error('--ref or --web-only --web-dist is required') + revision = None if args.web_dist else subprocess.check_output( + ['git','rev-parse','--verify','--end-of-options',args.ref+'^{commit}'],text=True).strip() + web = pack_web(args.web_dist.resolve(strict=True)) if args.web_dist else web_files(revision) source=Path(inspect.getfile(activate)).read_text().rsplit('\nif __name__ ==', 1)[0] - names=subprocess.check_output(['git','ls-tree','--name-only','-r',revision,'migrations'],text=True).splitlines() + names=subprocess.check_output(['git','ls-tree','--name-only','-r',revision,'migrations'],text=True).splitlines() if not args.web_only else [] migrations={str(int(Path(n).name.split('_',1)[0])):hashlib.sha384(subprocess.check_output(['git','show',revision+':'+n])).hexdigest() for n in names if n.endswith('.sql')} overlays = {} @@ -165,7 +198,7 @@ def main(): parser.error('Expected a server overlay filename from deploy/') raw = subprocess.check_output(['git', 'show', revision + ':deploy/' + name]) overlays[name] = base64.b64encode(raw).decode() - payload=json.dumps(dict(revision=revision,web=web,overlays=overlays,migrations=migrations)) + payload=json.dumps(dict(revision=revision,web=web,web_only=args.web_only,overlays=overlays,migrations=migrations)) cmd=source+'\nactivate(json.loads('+repr(payload)+'))\n' subprocess.run(ssh_prefix('server')+[shlex.join(['python3','-I','-'])],input=cmd,text=True,check=True) diff --git a/web/src/console-output.test.tsx b/web/src/console-output.test.tsx index 0479e28..6f74ac1 100644 --- a/web/src/console-output.test.tsx +++ b/web/src/console-output.test.tsx @@ -1,3 +1,4 @@ +import { windowsUpdateScriptHash } from './script-console'; import { expect, it, vi } from 'vitest'; import { renderToStaticMarkup } from 'react-dom/server'; import { ConsoleOutput, outputBytes, outputText } from './console-output'; @@ -25,3 +26,14 @@ it('distinguishes no result, empty bytes, invalid encoding, invalid bytes and mi const bytes = outputBytes([239, 187, 191, 65, 13, 10]); expect(outputText(bytes).text).toBe('\\ufeffA\\u000d\n'); expect(Array.from(bytes)).toEqual([239, 187, 191, 65, 13, 10]); }); + +it('shows unknown restart state for an incomplete Windows Update summary', () => { + function rendered(hash: string) { + const result = { stdout: [], stderr: [], reboot_required: false, evidence: { + script_sha256: hash, windows_update_reboot_required: null + }}; + return renderToStaticMarkup(); + } + expect(rendered(windowsUpdateScriptHash)).toContain('Neustartbedarf: unknown'); + expect(rendered('ordinary script')).toContain('Neustartbedarf: false'); +}); diff --git a/web/src/console-output.tsx b/web/src/console-output.tsx index fd87325..92fd0b2 100644 --- a/web/src/console-output.tsx +++ b/web/src/console-output.tsx @@ -1,3 +1,4 @@ +import { windowsUpdateScriptHash } from './script-console'; import { record, RecordValue, stringField } from './api'; /** The wire format carries bytes, not an encoding declaration. Never guess a @@ -45,8 +46,8 @@ export function ConsoleOutput({ job }: { job: RecordValue }) { function Result({ value }: { value: unknown }) { try { const result = record(value, 'Jobergebnis'); - const evidence = result.evidence && typeof result.evidence === 'object' ? record(result.evidence) : {}; - const reboot = Object.hasOwn(evidence, 'windows_update_reboot_required') ? evidence.windows_update_reboot_required : result.reboot_required; + const evidence = result.evidence && typeof result.evidence === 'object' && !Array.isArray(result.evidence) ? record(result.evidence) : {}; + const reboot = evidence.script_sha256 === windowsUpdateScriptHash && Object.hasOwn(evidence, 'windows_update_reboot_required') ? evidence.windows_update_reboot_required : result.reboot_required; return <>

Exitcode: {typeof result.exit_code === 'number' && Number.isInteger(result.exit_code) ? result.exit_code : 'unknown / nicht gemeldet'} · Fehlercode: {typeof result.error_code === 'string' && result.error_code ? result.error_code : 'nicht gemeldet'}

Ausgabe begrenzt: {typeof result.output_truncated === 'boolean' ? String(result.output_truncated) : 'unknown'}. {result.output_truncated === true ? 'Die gespeicherten Bytes sind nur ein Ausschnitt.' : result.output_truncated !== false ? 'Vollständigkeit ist nicht bestätigt.' : 'Keine Trunkierung gemeldet.'}

Neustartbedarf: {typeof reboot === 'boolean' ? String(reboot) : 'unknown'}. Ein Exitcode 0 bestätigt Prozessabschluss; fachliche Nachbedingungen stehen im vollständigen Ergebnis.