Qualify tenant-scoped bootstrap rotation and fresh Windows scripting

This commit is contained in:
2026-11-17 16:19:08 +01:00
parent 81fcd9c8f8
commit fbd24b6d75
26 changed files with 877 additions and 118 deletions
+63 -5
View File
@@ -36,6 +36,8 @@ struct Config {
transport: String,
#[serde(default)]
job_binding_file: Option<PathBuf>,
#[serde(default)]
localservice_time_policy: Option<PathBuf>,
// 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<Self> {
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<fs::File>,
identity: EnrollmentResponse,
ca: Vec<u8>,
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<bool> {
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."