Provision the release-bound Windows maintenance helper on enrolled development devices

This commit is contained in:
2026-11-17 16:35:01 +01:00
parent a799272d91
commit 863dcb04b1
9 changed files with 215 additions and 34 deletions
+67 -12
View File
@@ -95,8 +95,55 @@ fn protected(path: &Path, limit: usize) -> Result<Vec<u8>> {
);
Ok(fs::read(path)?)
}
// The active agent pins its trust/configuration directory. Windows permits
// creating a child there, but rejects a rename into this pinned directory.
// Keep all transactional records in their own administrator-controlled child.
fn journal_dir() -> PathBuf {
data("release-state")
}
fn record_path(name: &str) -> PathBuf {
journal_dir().join(name)
}
fn prepare_journal() -> Result<()> {
job_binding::check_admin_readonly(&data("config.json"))?;
let dir = journal_dir();
if !dir.try_exists()? {
fs::create_dir(&dir)?;
}
native::private_path(&dir, true)?;
// Copy original protected bytes once, preserving any interrupted transition.
// Record selection thereafter uses only the new directory.
for name in [
ACTIVE,
INTENT,
"rejected-release.json",
"release-status.json",
] {
let previous = data(name);
let current = record_path(name);
if !current.try_exists()? && previous.try_exists()? {
let bytes = protected(&previous, 8192)?;
let temporary = dir.join(format!(".migrate-{}", Uuid::new_v4()));
write_new(&temporary, &bytes)?;
let moved = fs::rename(&temporary, current);
if moved.is_err() {
let _ = fs::remove_file(&temporary);
}
moved?;
}
// ACTIVE doubles as the immutable baseline for older installers. An
// old intent must never be resurrected once its recovery is complete.
if name != ACTIVE && previous.try_exists()? && record_path(name).try_exists()? {
fs::remove_file(previous)?;
}
}
Ok(())
}
fn read_record<T: serde::de::DeserializeOwned>(name: &str) -> Result<T> {
Ok(serde_json::from_slice(&protected(&data(name), 8192)?)?)
Ok(serde_json::from_slice(&protected(
&record_path(name),
8192,
)?)?)
}
fn write_new(path: &Path, bytes: &[u8]) -> Result<()> {
let mut file = fs::File::create_new(path)?;
@@ -108,11 +155,11 @@ fn write_record<T: Serialize>(name: &str, value: &T) -> Result<()> {
use windows_sys::Win32::Storage::FileSystem::{
MOVEFILE_REPLACE_EXISTING, MOVEFILE_WRITE_THROUGH, MoveFileExW,
};
let tmp = data(&format!(".release-{}", Uuid::new_v4()));
let tmp = record_path(&format!(".release-{}", Uuid::new_v4()));
let wide = |p: &OsStr| p.encode_wide().chain(Some(0)).collect::<Vec<_>>();
let result = (|| -> Result<()> {
write_new(&tmp, &serde_json::to_vec(value)?)?;
let (from, to) = (wide(tmp.as_os_str()), wide(data(name).as_os_str()));
let (from, to) = (wide(tmp.as_os_str()), wide(record_path(name).as_os_str()));
if unsafe {
MoveFileExW(
from.as_ptr(),
@@ -294,7 +341,7 @@ async fn rollback(t: &Transition) -> Result<()> {
local_manifest(&t.old)?;
select(t.new.release_id, t.old.release_id)?;
write_record("rejected-release.json", &t.new)?;
fs::remove_file(data(INTENT))?;
fs::remove_file(record_path(INTENT))?;
Ok(())
}
@@ -371,7 +418,7 @@ async fn cycle() -> Result<()> {
|| config.localservice_time_policy.as_ref() == Some(&data("job-time.json")),
"job-enabled pair requires coordinated helper handoff"
);
if data(INTENT).try_exists()? {
if record_path(INTENT).try_exists()? {
let _job_gate = if config.enable_jobs {
let Some(file) = exclusive_job_gate()? else {
return Ok(());
@@ -383,7 +430,7 @@ async fn cycle() -> Result<()> {
let t: Transition = read_record(INTENT)?;
if active.release_id == t.new.release_id {
fs::remove_file(data(INTENT))?;
fs::remove_file(record_path(INTENT))?;
} else {
ensure!(
active.release_id == t.old.release_id,
@@ -422,7 +469,7 @@ async fn cycle() -> Result<()> {
);
return Ok(());
}
if data("rejected-release.json").try_exists()? {
if record_path("rejected-release.json").try_exists()? {
let rejected: Installed = read_record("rejected-release.json")?;
ensure!(
candidate.release_sequence > rejected.release_sequence,
@@ -456,7 +503,7 @@ async fn cycle() -> Result<()> {
&& h.last_contact_at.is_some_and(|v| v >= start)
{
write_record(ACTIVE, &t.new)?;
fs::remove_file(data(INTENT))?;
fs::remove_file(record_path(INTENT))?;
return Ok(());
}
tokio::time::sleep(Duration::from_secs(5)).await;
@@ -472,13 +519,21 @@ pub fn dispatch() -> Result<()> {
fn run(stop: &'static AtomicBool) -> Result<()> {
let runtime = tokio::runtime::Runtime::new()?;
while !stop.load(Ordering::SeqCst) {
let result = runtime.block_on(cycle());
write_record(
let result = prepare_journal().and_then(|()| runtime.block_on(cycle()));
let succeeded = result.is_ok();
let status_saved = write_record(
"release-status.json",
&serde_json::json!({"observed_at": chrono::Utc::now(),
"status": if result.is_ok() { "ok" } else { "error" }, "detail": result.err().map(|e| e.to_string())}),
)?;
for _ in 0..300 {
);
// A temporary file lock during status publication must not terminate
// the SCM service. A bounded retry also covers enrollment startup.
let retry_seconds = if succeeded && status_saved.is_ok() {
300
} else {
15
};
for _ in 0..retry_seconds {
if stop.load(Ordering::SeqCst) {
break;
}