Add gated agent update planning, preserved handoff and Linux worker ports
Rust development slice / linux (push) Failing after 6s
Rust development slice / linux (push) Failing after 6s
This commit is contained in:
@@ -0,0 +1,197 @@
|
||||
//! Exact fixed target/stage proofs, captured before atomic consumption and
|
||||
//! independently reread under the existing lock before each native operation.
|
||||
use anyhow::{Context, Result, ensure};
|
||||
use rmm_domain::jobs::hash;
|
||||
use rmm_updater_handoff::{FileRole, Handoff};
|
||||
use rmm_updater_linux::{
|
||||
Budget,
|
||||
files::{Directory, FileProof, Role, Staged},
|
||||
};
|
||||
use serde::Serialize;
|
||||
use std::{ffi::OsStr, path::Path};
|
||||
use uuid::Uuid;
|
||||
|
||||
const ROLES: [Role; 4] = [Role::Agent, Role::Helper, Role::FullConfig, Role::FullPin];
|
||||
fn label(role: Role) -> &'static str {
|
||||
match role {
|
||||
Role::Agent => "agent",
|
||||
Role::Helper => "helper",
|
||||
Role::FullConfig => "full-config",
|
||||
Role::FullPin => "full-pin",
|
||||
}
|
||||
}
|
||||
fn role(role: FileRole) -> Result<Role> {
|
||||
match role {
|
||||
FileRole::AgentBinary => Ok(Role::Agent),
|
||||
FileRole::HelperBinary => Ok(Role::Helper),
|
||||
FileRole::LinuxFullConfig => Ok(Role::FullConfig),
|
||||
FileRole::LinuxFullPin => Ok(Role::FullPin),
|
||||
_ => anyhow::bail!("UPDATE_LINUX_FILE_ROLE"),
|
||||
}
|
||||
}
|
||||
#[derive(Serialize)]
|
||||
struct Proof {
|
||||
role: Role,
|
||||
original: FileProof,
|
||||
staged: FileProof,
|
||||
}
|
||||
pub struct FileWitnesses {
|
||||
files: Vec<Proof>,
|
||||
nonce: Uuid,
|
||||
sha256: String,
|
||||
}
|
||||
impl FileWitnesses {
|
||||
/// Exactly Agent, Helper, Fullconfig, Fullpin, all fully staged. Reopening
|
||||
/// each fixed stage name also rejects a mislabeled `Staged` object.
|
||||
pub fn capture(stages: &[Staged; 4], nonce: Uuid, budget: &Budget) -> Result<Self> {
|
||||
ensure!(!nonce.is_nil(), "UPDATE_STAGE_NONCE");
|
||||
let mut files = Vec::new();
|
||||
for (role, stage) in ROLES.iter().copied().zip(stages) {
|
||||
budget.check()?;
|
||||
let path = Path::new(role.target());
|
||||
let directory =
|
||||
Directory::open(path.parent().context("UPDATE_TARGET_PARENT")?, 0, false)?;
|
||||
let original = directory.inspect(
|
||||
path.file_name().context("UPDATE_TARGET_NAME")?,
|
||||
0,
|
||||
256 * 1024 * 1024,
|
||||
)?;
|
||||
directory.file(
|
||||
OsStr::new(&format!(".rmm-update-{nonce}-{}.stage", label(role))),
|
||||
stage.proof(),
|
||||
256 * 1024 * 1024,
|
||||
)?;
|
||||
ensure!(
|
||||
stage.proof().identity.links == 1
|
||||
&& stage.proof().identity.uid == original.proof().identity.uid
|
||||
&& stage.proof().identity.gid == original.proof().identity.gid
|
||||
&& stage.proof().identity.mode == original.proof().identity.mode,
|
||||
"UPDATE_STAGE_METADATA_CHANGED"
|
||||
);
|
||||
files.push(Proof {
|
||||
role,
|
||||
original: original.proof().clone(),
|
||||
staged: stage.proof().clone(),
|
||||
});
|
||||
}
|
||||
let digest = hash(&serde_json::to_vec(&files)?);
|
||||
Ok(Self {
|
||||
files,
|
||||
nonce,
|
||||
sha256: digest,
|
||||
})
|
||||
}
|
||||
pub fn sha256(&self) -> &str {
|
||||
&self.sha256
|
||||
}
|
||||
pub fn bind(&self, handoff: &Handoff) -> Result<()> {
|
||||
ensure!(
|
||||
handoff.files.len() == 4
|
||||
&& handoff.owner_nonce == self.nonce
|
||||
&& handoff.staged_files_sha256 == self.sha256,
|
||||
"UPDATE_FILESET_BINDING"
|
||||
);
|
||||
for (file, plan) in self.files.iter().zip(&handoff.files) {
|
||||
ensure!(
|
||||
plan.role == file.role.handoff_role()
|
||||
&& plan.old_sha256 == file.original.sha256
|
||||
&& plan.new_sha256 == file.staged.sha256,
|
||||
"UPDATE_FILESET_PLAN_CHANGED"
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
pub fn verify_original_and_stage(&self, which: FileRole, budget: &Budget) -> Result<()> {
|
||||
let file = self.file(which)?;
|
||||
budget.check()?;
|
||||
let path = Path::new(file.role.target());
|
||||
let dir = Directory::open(path.parent().context("UPDATE_TARGET_PARENT")?, 0, false)?;
|
||||
dir.file(
|
||||
path.file_name().context("UPDATE_TARGET_NAME")?,
|
||||
&file.original,
|
||||
256 * 1024 * 1024,
|
||||
)?;
|
||||
dir.file(
|
||||
OsStr::new(&format!(
|
||||
".rmm-update-{}-{}.stage",
|
||||
self.nonce,
|
||||
label(file.role)
|
||||
)),
|
||||
&file.staged,
|
||||
256 * 1024 * 1024,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
pub fn verify_replaced(&self, which: FileRole, budget: &Budget) -> Result<()> {
|
||||
let file = self.file(which)?;
|
||||
budget.check()?;
|
||||
let path = Path::new(file.role.target());
|
||||
let dir = Directory::open(path.parent().context("UPDATE_TARGET_PARENT")?, 0, false)?;
|
||||
dir.file(
|
||||
path.file_name().context("UPDATE_TARGET_NAME")?,
|
||||
&file.staged,
|
||||
256 * 1024 * 1024,
|
||||
)?;
|
||||
dir.file(
|
||||
OsStr::new(&format!(
|
||||
".rmm-update-{}-{}.previous",
|
||||
self.nonce,
|
||||
label(file.role)
|
||||
)),
|
||||
&file.original,
|
||||
256 * 1024 * 1024,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
pub fn verify_saved(&self, which: FileRole, budget: &Budget) -> Result<()> {
|
||||
let file = self.file(which)?;
|
||||
budget.check()?;
|
||||
let path = Path::new(file.role.target());
|
||||
let dir = Directory::open(path.parent().context("UPDATE_TARGET_PARENT")?, 0, false)?;
|
||||
dir.file(
|
||||
OsStr::new(&format!(
|
||||
".rmm-update-{}-{}.previous",
|
||||
self.nonce,
|
||||
label(file.role)
|
||||
)),
|
||||
&file.original,
|
||||
256 * 1024 * 1024,
|
||||
)?;
|
||||
dir.file(
|
||||
OsStr::new(&format!(
|
||||
".rmm-update-{}-{}.stage",
|
||||
self.nonce,
|
||||
label(file.role)
|
||||
)),
|
||||
&file.staged,
|
||||
256 * 1024 * 1024,
|
||||
)?;
|
||||
ensure!(
|
||||
std::fs::symlink_metadata(path)
|
||||
.is_err_and(|e| e.kind() == std::io::ErrorKind::NotFound),
|
||||
"UPDATE_PUBLISH_TARGET_NOT_ABSENT"
|
||||
);
|
||||
dir.verify()
|
||||
}
|
||||
pub fn native_observation_sha256(&self, which: FileRole, replaced: bool) -> Result<String> {
|
||||
let file = self.file(which)?;
|
||||
Ok(hash(&serde_json::to_vec(if replaced {
|
||||
&file.staged
|
||||
} else {
|
||||
&file.original
|
||||
})?))
|
||||
}
|
||||
pub fn verify_pair(&self, budget: &Budget) -> Result<()> {
|
||||
for file in &self.files {
|
||||
self.verify_replaced(file.role.handoff_role(), budget)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn file(&self, which: FileRole) -> Result<&Proof> {
|
||||
let role = role(which)?;
|
||||
self.files
|
||||
.iter()
|
||||
.find(|f| f.role == role)
|
||||
.context("UPDATE_FILE_NOT_BOUND")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user