100 lines
3.5 KiB
Rust
100 lines
3.5 KiB
Rust
use anyhow::{Result, ensure};
|
|
use std::{
|
|
fs::{self, OpenOptions},
|
|
io::Write,
|
|
os::unix::fs::{OpenOptionsExt, PermissionsExt},
|
|
path::PathBuf,
|
|
};
|
|
use uuid::Uuid;
|
|
pub trait ArtifactStore {
|
|
fn put(&self, tenant: Uuid, bytes: &[u8]) -> Result<String>;
|
|
fn get(&self, tenant: Uuid, hash: &str) -> Result<Vec<u8>>;
|
|
}
|
|
pub struct LocalArtifacts {
|
|
root: PathBuf,
|
|
}
|
|
impl LocalArtifacts {
|
|
pub fn new(profile: &str, root: PathBuf) -> Result<Self> {
|
|
ensure!(
|
|
profile == "development",
|
|
"local artifact adapter is development-only; production not configured"
|
|
);
|
|
Ok(Self { root })
|
|
}
|
|
fn path(&self, tenant: Uuid, hash: &str) -> Result<PathBuf> {
|
|
ensure!(
|
|
hash.len() == 64 && hash.bytes().all(|c| c.is_ascii_hexdigit()),
|
|
"invalid digest"
|
|
);
|
|
Ok(self.root.join(tenant.to_string()).join(hash))
|
|
}
|
|
}
|
|
impl ArtifactStore for LocalArtifacts {
|
|
fn put(&self, tenant: Uuid, bytes: &[u8]) -> Result<String> {
|
|
ensure!(!bytes.is_empty() && bytes.len() <= 262144, "artifact bound");
|
|
let hash = rmm_domain::jobs::hash(bytes);
|
|
let path = self.path(tenant, &hash)?;
|
|
let parent = path
|
|
.parent()
|
|
.ok_or_else(|| anyhow::anyhow!("artifact parent missing"))?;
|
|
fs::create_dir_all(parent)?;
|
|
fs::set_permissions(parent, fs::Permissions::from_mode(0o700))?;
|
|
// A crash cannot expose partially written bytes as a hash-verified object.
|
|
let temporary = parent.join(format!(".{}", Uuid::new_v4()));
|
|
let mut file = OpenOptions::new()
|
|
.create_new(true)
|
|
.write(true)
|
|
.mode(0o600)
|
|
.open(&temporary)?;
|
|
file.write_all(bytes)?;
|
|
file.sync_all()?;
|
|
let linked = fs::hard_link(&temporary, &path);
|
|
fs::remove_file(&temporary)?;
|
|
if let Err(e) = linked {
|
|
ensure!(
|
|
e.kind() == std::io::ErrorKind::AlreadyExists,
|
|
"artifact link failed"
|
|
);
|
|
ensure!(
|
|
self.get(tenant, &hash)? == bytes,
|
|
"immutable object conflict"
|
|
);
|
|
}
|
|
fs::File::open(parent)?.sync_all()?;
|
|
Ok(hash)
|
|
}
|
|
fn get(&self, tenant: Uuid, hash: &str) -> Result<Vec<u8>> {
|
|
let path = self.path(tenant, hash)?;
|
|
let meta = fs::symlink_metadata(&path)?;
|
|
ensure!(
|
|
meta.is_file() && meta.len() <= 262144,
|
|
"invalid artifact object"
|
|
);
|
|
let bytes = fs::read(path)?;
|
|
ensure!(
|
|
rmm_domain::jobs::hash(&bytes) == hash,
|
|
"artifact integrity failure"
|
|
);
|
|
Ok(bytes)
|
|
}
|
|
}
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
#[test]
|
|
fn immutable_tenant_objects_and_production_denial() {
|
|
let dir = PathBuf::from(format!("/tmp/opencode/rmm-artifact-{}", Uuid::new_v4()));
|
|
assert!(LocalArtifacts::new("production", dir.clone()).is_err());
|
|
let store = LocalArtifacts::new("development", dir.clone()).unwrap();
|
|
let tenant = Uuid::new_v4();
|
|
let hash = store.put(tenant, b"exact\r\n").unwrap();
|
|
assert_eq!(store.put(tenant, b"exact\r\n").unwrap(), hash);
|
|
assert_eq!(store.get(tenant, &hash).unwrap(), b"exact\r\n");
|
|
assert!(store.get(Uuid::new_v4(), &hash).is_err());
|
|
assert!(store.get(tenant, "../escape").is_err());
|
|
fs::write(store.path(tenant, &hash).unwrap(), b"modified").unwrap();
|
|
assert!(store.get(tenant, &hash).is_err());
|
|
fs::remove_dir_all(dir).unwrap();
|
|
}
|
|
}
|