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,285 @@
|
||||
//! Separate bounded binary store. Existing 256KiB package artifacts are unchanged.
|
||||
//! A completed object is published only after complete length/hash verification.
|
||||
use anyhow::{Result, ensure};
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::{
|
||||
fs::{self, File, OpenOptions},
|
||||
io::{Read, Seek, SeekFrom, Write},
|
||||
os::unix::fs::{MetadataExt, OpenOptionsExt, PermissionsExt},
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
pub const MAX_CHUNK_BYTES: u32 = 512 * 1024;
|
||||
pub struct UpdateArtifacts {
|
||||
root: PathBuf,
|
||||
owner: u32,
|
||||
}
|
||||
pub struct Staging {
|
||||
file: File,
|
||||
temporary: PathBuf,
|
||||
final_path: PathBuf,
|
||||
parent: PathBuf,
|
||||
expected_hash: String,
|
||||
expected_size: u64,
|
||||
written: u64,
|
||||
hasher: Sha256,
|
||||
}
|
||||
fn descriptor(hash: &str, bytes: u64) -> bool {
|
||||
rmm_domain::agent_update::digest(hash) && (1..=rmm_updater::MAX_ARTIFACT_BYTES).contains(&bytes)
|
||||
}
|
||||
fn directory(path: &Path, owner: u32) -> Result<()> {
|
||||
let m = fs::symlink_metadata(path)?;
|
||||
ensure!(
|
||||
m.is_dir() && m.uid() == owner && m.mode() & 0o777 == 0o700,
|
||||
"private update store directory"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
fn regular(file: &File, owner: u32, expected_size: u64) -> Result<fs::Metadata> {
|
||||
let m = file.metadata()?;
|
||||
ensure!(
|
||||
m.is_file()
|
||||
&& m.uid() == owner
|
||||
&& m.mode() & 0o777 == 0o600
|
||||
&& m.nlink() == 1
|
||||
&& m.len() == expected_size,
|
||||
"immutable update object metadata"
|
||||
);
|
||||
Ok(m)
|
||||
}
|
||||
impl UpdateArtifacts {
|
||||
pub fn new(profile: &str, root: PathBuf, owner: u32) -> Result<Self> {
|
||||
ensure!(
|
||||
profile == "development",
|
||||
"update object adapter is development-only"
|
||||
);
|
||||
match fs::create_dir(&root) {
|
||||
Ok(()) => fs::set_permissions(&root, fs::Permissions::from_mode(0o700))?,
|
||||
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {}
|
||||
Err(e) => return Err(e.into()),
|
||||
}
|
||||
directory(&root, owner)?;
|
||||
Ok(Self { root, owner })
|
||||
}
|
||||
fn parent(&self, tenant: Uuid) -> Result<PathBuf> {
|
||||
ensure!(!tenant.is_nil(), "update tenant identity");
|
||||
directory(&self.root, self.owner)?;
|
||||
let parent = self.root.join(tenant.to_string());
|
||||
match fs::create_dir(&parent) {
|
||||
Ok(()) => {
|
||||
fs::set_permissions(&parent, fs::Permissions::from_mode(0o700))?;
|
||||
File::open(&self.root)?.sync_all()?;
|
||||
}
|
||||
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {}
|
||||
Err(e) => return Err(e.into()),
|
||||
}
|
||||
directory(&parent, self.owner)?;
|
||||
Ok(parent)
|
||||
}
|
||||
pub fn begin(&self, tenant: Uuid, hash: &str, bytes: u64) -> Result<Staging> {
|
||||
ensure!(descriptor(hash, bytes), "bounded update object descriptor");
|
||||
let parent = self.parent(tenant)?;
|
||||
let temporary = parent.join(format!(".staging-{}", Uuid::new_v4()));
|
||||
let file = OpenOptions::new()
|
||||
.create_new(true)
|
||||
.write(true)
|
||||
.mode(0o600)
|
||||
.custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC)
|
||||
.open(&temporary)?;
|
||||
Ok(Staging {
|
||||
file,
|
||||
temporary,
|
||||
final_path: parent.join(hash),
|
||||
parent,
|
||||
expected_hash: hash.into(),
|
||||
expected_size: bytes,
|
||||
written: 0,
|
||||
hasher: Sha256::new(),
|
||||
})
|
||||
}
|
||||
fn verified_file(&self, tenant: Uuid, hash: &str, bytes: u64) -> Result<File> {
|
||||
ensure!(descriptor(hash, bytes), "bounded update object descriptor");
|
||||
let parent = self.parent(tenant)?;
|
||||
let mut file = OpenOptions::new()
|
||||
.read(true)
|
||||
.custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC)
|
||||
.open(parent.join(hash))?;
|
||||
let before = regular(&file, self.owner, bytes)?;
|
||||
let mut hasher = Sha256::new();
|
||||
let mut buffer = [0_u8; 32768];
|
||||
let mut seen = 0;
|
||||
loop {
|
||||
let n = file.read(&mut buffer)?;
|
||||
if n == 0 {
|
||||
break;
|
||||
}
|
||||
seen += n as u64;
|
||||
ensure!(seen <= bytes, "update object additional bytes");
|
||||
hasher.update(&buffer[..n]);
|
||||
}
|
||||
let after = regular(&file, self.owner, bytes)?;
|
||||
ensure!(
|
||||
seen == bytes
|
||||
&& hex::encode(hasher.finalize()) == hash
|
||||
&& (
|
||||
before.dev(),
|
||||
before.ino(),
|
||||
before.ctime(),
|
||||
before.ctime_nsec(),
|
||||
before.mtime(),
|
||||
before.mtime_nsec()
|
||||
) == (
|
||||
after.dev(),
|
||||
after.ino(),
|
||||
after.ctime(),
|
||||
after.ctime_nsec(),
|
||||
after.mtime(),
|
||||
after.mtime_nsec()
|
||||
),
|
||||
"complete immutable update object integrity"
|
||||
);
|
||||
file.seek(SeekFrom::Start(0))?;
|
||||
Ok(file)
|
||||
}
|
||||
pub fn verify(&self, tenant: Uuid, hash: &str, bytes: u64) -> Result<()> {
|
||||
self.verified_file(tenant, hash, bytes).map(|_| ())
|
||||
}
|
||||
pub fn read_chunk(
|
||||
&self,
|
||||
tenant: Uuid,
|
||||
hash: &str,
|
||||
bytes: u64,
|
||||
offset: u64,
|
||||
length: u32,
|
||||
) -> Result<Vec<u8>> {
|
||||
ensure!(
|
||||
offset < bytes && (1..=MAX_CHUNK_BYTES).contains(&length),
|
||||
"bounded update range"
|
||||
);
|
||||
let mut file = self.verified_file(tenant, hash, bytes)?;
|
||||
let before = regular(&file, self.owner, bytes)?;
|
||||
file.seek(SeekFrom::Start(offset))?;
|
||||
let mut chunk = vec![0; (bytes - offset).min(length as u64) as usize];
|
||||
file.read_exact(&mut chunk)?;
|
||||
let after = regular(&file, self.owner, bytes)?;
|
||||
ensure!(
|
||||
(
|
||||
before.dev(),
|
||||
before.ino(),
|
||||
before.ctime(),
|
||||
before.ctime_nsec(),
|
||||
before.mtime(),
|
||||
before.mtime_nsec()
|
||||
) == (
|
||||
after.dev(),
|
||||
after.ino(),
|
||||
after.ctime(),
|
||||
after.ctime_nsec(),
|
||||
after.mtime(),
|
||||
after.mtime_nsec()
|
||||
),
|
||||
"update object changed during bounded range"
|
||||
);
|
||||
Ok(chunk)
|
||||
}
|
||||
}
|
||||
impl Staging {
|
||||
pub fn append(&mut self, chunk: &[u8]) -> Result<()> {
|
||||
ensure!(
|
||||
!chunk.is_empty()
|
||||
&& chunk.len() <= MAX_CHUNK_BYTES as usize
|
||||
&& self.written + chunk.len() as u64 <= self.expected_size,
|
||||
"bounded update import chunk"
|
||||
);
|
||||
self.file.write_all(chunk)?;
|
||||
self.hasher.update(chunk);
|
||||
self.written += chunk.len() as u64;
|
||||
Ok(())
|
||||
}
|
||||
pub fn finish(mut self, store: &UpdateArtifacts, tenant: Uuid) -> Result<()> {
|
||||
ensure!(
|
||||
self.written == self.expected_size
|
||||
&& hex::encode(self.hasher.clone().finalize()) == self.expected_hash,
|
||||
"complete update import hash/length"
|
||||
);
|
||||
self.file.flush()?;
|
||||
self.file.sync_all()?;
|
||||
if let Err(e) = fs::hard_link(&self.temporary, &self.final_path) {
|
||||
ensure!(
|
||||
e.kind() == std::io::ErrorKind::AlreadyExists,
|
||||
"update publication failed"
|
||||
);
|
||||
store.verify(tenant, &self.expected_hash, self.expected_size)?;
|
||||
}
|
||||
fs::remove_file(&self.temporary)?;
|
||||
File::open(&self.parent)?.sync_all()?;
|
||||
store.verify(tenant, &self.expected_hash, self.expected_size)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
impl Drop for Staging {
|
||||
fn drop(&mut self) {
|
||||
// Only this create-once temporary object, never a published object/job.
|
||||
let _ = fs::remove_file(&self.temporary);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn large_update_stream_preserves_legacy_bound_and_denies_corruption_or_foreign_tenant() {
|
||||
let root = std::env::temp_dir().join(format!("rmm-update-store-{}", Uuid::new_v4()));
|
||||
fs::create_dir(&root).unwrap();
|
||||
fs::set_permissions(&root, fs::Permissions::from_mode(0o700)).unwrap();
|
||||
let owner = fs::metadata(&root).unwrap().uid();
|
||||
let store = UpdateArtifacts::new("development", root.clone(), owner).unwrap();
|
||||
assert!(UpdateArtifacts::new("production", root.clone(), owner).is_err());
|
||||
let tenant = Uuid::new_v4();
|
||||
let bytes = vec![37_u8; 300_007];
|
||||
let hash = rmm_domain::jobs::hash(&bytes);
|
||||
let mut staging = store.begin(tenant, &hash, bytes.len() as u64).unwrap();
|
||||
for chunk in bytes.chunks(32768) {
|
||||
staging.append(chunk).unwrap();
|
||||
}
|
||||
staging.finish(&store, tenant).unwrap();
|
||||
assert_eq!(
|
||||
store
|
||||
.read_chunk(tenant, &hash, bytes.len() as u64, 299_999, 16)
|
||||
.unwrap(),
|
||||
bytes[299_999..]
|
||||
);
|
||||
assert!(
|
||||
store
|
||||
.read_chunk(Uuid::new_v4(), &hash, bytes.len() as u64, 0, 32)
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
store
|
||||
.read_chunk(tenant, &hash, bytes.len() as u64, bytes.len() as u64, 1)
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
store
|
||||
.read_chunk(tenant, &hash, bytes.len() as u64, 0, MAX_CHUNK_BYTES + 1)
|
||||
.is_err()
|
||||
);
|
||||
assert!(store.begin(tenant, "../escape", 1).is_err());
|
||||
// An incomplete object is never published, including a clean early EOF.
|
||||
let truncated = rmm_domain::jobs::hash(b"truncated");
|
||||
let mut short = store.begin(tenant, &truncated, 9).unwrap();
|
||||
short.append(b"trunc").unwrap();
|
||||
assert!(short.finish(&store, tenant).is_err());
|
||||
assert!(!root.join(tenant.to_string()).join(truncated).exists());
|
||||
let object = root.join(tenant.to_string()).join(&hash);
|
||||
fs::write(&object, vec![38_u8; bytes.len()]).unwrap();
|
||||
assert!(store.verify(tenant, &hash, bytes.len() as u64).is_err());
|
||||
// Existing small package storage retains its own 256KiB boundary.
|
||||
use crate::artifact_store::ArtifactStore;
|
||||
let legacy =
|
||||
crate::artifact_store::LocalArtifacts::new("development", root.join("legacy")).unwrap();
|
||||
assert!(legacy.put(tenant, &bytes).is_err());
|
||||
fs::remove_dir_all(root).unwrap();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user