use std::ffi::{OsStr, OsString};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use askama::Template;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use crate::ContainerPath;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ManagedTool {
ApplyPatch,
Ripgrep,
AstGrep,
Jq,
}
impl ManagedTool {
pub const ALL: [Self; 4] = [Self::ApplyPatch, Self::Ripgrep, Self::AstGrep, Self::Jq];
#[must_use]
pub const fn command(self) -> &'static str {
match self {
Self::ApplyPatch => "apply_patch",
Self::Ripgrep => "rg",
Self::AstGrep => "ast-grep",
Self::Jq => "jq",
}
}
pub(crate) fn executable_name(self, target: &ToolTarget) -> String {
executable_name(self.command(), target)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum HostProgram {
RepoSearch,
}
impl HostProgram {
pub const ALL: [Self; 1] = [Self::RepoSearch];
#[must_use]
pub const fn command(self) -> &'static str {
match self {
Self::RepoSearch => "search",
}
}
pub(crate) fn executable_name(self, target: &ToolTarget) -> String {
executable_name(self.command(), target)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DistributedTool {
Command(ManagedTool),
Host(HostProgram),
}
impl From<ManagedTool> for DistributedTool {
fn from(tool: ManagedTool) -> Self {
Self::Command(tool)
}
}
impl From<HostProgram> for DistributedTool {
fn from(program: HostProgram) -> Self {
Self::Host(program)
}
}
fn executable_name(command: &str, target: &ToolTarget) -> String {
if target.os == "windows" {
format!("{command}.exe")
} else {
command.to_owned()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ToolTarget {
os: String,
arch: String,
}
impl ToolTarget {
pub fn native() -> Result<Self, ToolRuntimeError> {
Self::from_probe(std::env::consts::OS, std::env::consts::ARCH)
}
pub fn from_probe(os: &str, arch: &str) -> Result<Self, ToolRuntimeError> {
let normalized_os = match os.trim() {
"Linux" | "linux" => "linux",
"Darwin" | "macos" => "macos",
"Windows_NT" | "windows" => "windows",
_ => {
return Err(ToolRuntimeError::UnsupportedTarget {
os: os.into(),
arch: arch.into(),
});
}
};
let normalized_arch = match arch.trim() {
"arm64" | "aarch64" => "aarch64",
"amd64" | "x86_64" => "x86_64",
_ => {
return Err(ToolRuntimeError::UnsupportedTarget {
os: os.into(),
arch: arch.into(),
});
}
};
Ok(Self {
os: normalized_os.into(),
arch: normalized_arch.into(),
})
}
#[must_use]
pub fn os(&self) -> &str {
&self.os
}
#[must_use]
pub fn arch(&self) -> &str {
&self.arch
}
}
#[derive(Debug, Clone)]
pub struct ToolBinary {
pub(crate) tool: ManagedTool,
version: semver::Version,
pub(crate) sha256: [u8; 32],
pub(crate) bytes: Arc<[u8]>,
}
impl ToolBinary {
pub fn verify(
tool: ManagedTool,
version: &str,
expected: &[u8; 32],
bytes: Vec<u8>,
) -> Result<Self, ToolRuntimeError> {
let (version, bytes) = verify_executable(tool.into(), version, expected, bytes)?;
Ok(Self {
tool,
version,
sha256: *expected,
bytes,
})
}
}
fn verify_executable(
tool: DistributedTool,
version: &str,
expected: &[u8; 32],
bytes: Vec<u8>,
) -> Result<(semver::Version, Arc<[u8]>), ToolRuntimeError> {
let version = semver::Version::parse(version)?;
if bytes.len() > 64 * 1024 * 1024 {
return Err(ToolRuntimeError::ArtifactTooLarge(tool));
}
if digest(&bytes) != *expected {
return Err(ToolRuntimeError::Integrity { tool });
}
Ok((version, bytes.into()))
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HostProgramPath(PathBuf);
impl HostProgramPath {
#[must_use]
pub fn path(&self) -> &Path {
&self.0
}
}
impl HostProgramPath {
pub(crate) fn cached(
root: &Path,
program: HostProgram,
target: &ToolTarget,
sha256: &[u8; 32],
) -> Result<Option<Self>, ToolRuntimeError> {
let path = Self::location(root, program, target, sha256)?;
Ok(Self::is_prepared(&path, sha256)?.then_some(Self(path)))
}
pub(crate) fn write(
root: &Path,
program: HostProgram,
target: &ToolTarget,
sha256: &[u8; 32],
bytes: &[u8],
) -> Result<Self, ToolRuntimeError> {
let path = Self::location(root, program, target, sha256)?;
let directory = path.parent().ok_or(ToolRuntimeError::InvalidArchive)?;
std::fs::create_dir_all(directory)?;
let mut stage = tempfile::NamedTempFile::new_in(directory)?;
std::io::Write::write_all(&mut stage, bytes)?;
drop(stage.persist(&path).map_err(|error| error.error)?);
set_executable_readonly(&path)?;
if Self::is_prepared(&path, sha256)? {
Ok(Self(path))
} else {
Err(ToolRuntimeError::InvalidCache(path))
}
}
fn location(
root: &Path,
program: HostProgram,
target: &ToolTarget,
sha256: &[u8; 32],
) -> Result<PathBuf, ToolRuntimeError> {
std::fs::create_dir_all(root)?;
Ok(std::fs::canonicalize(root)?
.join(hex::encode(sha256))
.join(program.executable_name(target)))
}
fn is_prepared(path: &Path, sha256: &[u8; 32]) -> Result<bool, ToolRuntimeError> {
let metadata = match std::fs::symlink_metadata(path) {
Ok(metadata) => metadata,
Err(error) => {
return match cache_entry_error(path, error) {
ToolRuntimeError::InvalidCache(_) => Ok(false),
error => Err(error),
};
}
};
if !metadata.file_type().is_file() || digest(&std::fs::read(path)?) != *sha256 {
return Ok(false);
}
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mode = metadata.permissions().mode();
Ok(mode & 0o222 == 0 && mode & 0o111 != 0)
}
#[cfg(not(unix))]
Ok(metadata.permissions().readonly())
}
}
#[derive(Debug, Clone)]
pub struct ToolBundle {
target: ToolTarget,
pub(crate) binaries: Arc<[ToolBinary]>,
}
impl ToolBundle {
pub fn new(
target: ToolTarget,
mut binaries: Vec<ToolBinary>,
) -> Result<Self, ToolRuntimeError> {
binaries.sort_by_key(|binary| binary.tool);
for pair in binaries.windows(2) {
if pair[0].tool == pair[1].tool {
return Err(ToolRuntimeError::DuplicateTool(pair[0].tool));
}
}
for tool in ManagedTool::ALL {
if !binaries.iter().any(|binary| binary.tool == tool) {
return Err(ToolRuntimeError::MissingTool(tool));
}
}
Ok(Self {
target,
binaries: binaries.into(),
})
}
#[must_use]
pub const fn target(&self) -> &ToolTarget {
&self.target
}
#[must_use]
pub fn catalog(&self) -> ToolCatalog {
ToolCatalog {
tools: self
.binaries
.iter()
.map(|binary| ToolInfo {
command: binary.tool.command(),
version: binary.version.clone(),
})
.collect(),
}
}
#[cfg(test)]
fn binary(&self, tool: ManagedTool) -> &ToolBinary {
self.binaries
.iter()
.find(|binary| binary.tool == tool)
.expect("bundle is complete")
}
pub(crate) fn cache_key(&self) -> Result<String, ToolRuntimeError> {
#[derive(Serialize)]
struct Identity<'a> {
target: &'a ToolTarget,
tools: Vec<(&'a str, &'a semver::Version, &'a [u8; 32])>,
}
let identity = Identity {
target: &self.target,
tools: self
.binaries
.iter()
.map(|binary| (binary.tool.command(), &binary.version, &binary.sha256))
.collect(),
};
Ok(hex::encode(digest(&serde_json::to_vec(&identity)?)))
}
pub async fn prepare_local(
&self,
root: &Path,
) -> Result<PreparedTools<PathBuf>, ToolRuntimeError> {
if self.target != ToolTarget::native()? {
return Err(ToolRuntimeError::TargetMismatch);
}
let bundle = self.clone();
let root = root.to_path_buf();
blocking::unblock(move || {
let key = bundle.cache_key()?;
bundle.prepare_local_sync(&root, &key)
})
.await
}
pub(crate) fn prepare_local_sync(
&self,
root: &Path,
key: &str,
) -> Result<PreparedTools<PathBuf>, ToolRuntimeError> {
std::fs::create_dir_all(root)?;
let root = std::fs::canonicalize(root)?;
let destination = root.join(key);
if let Some(prepared) = self.reusable(&destination)? {
return Ok(prepared);
}
let lock_path = root.join(format!(".{key}.lock"));
let mut lock = fd_lock::RwLock::new(
std::fs::OpenOptions::new()
.create(true)
.truncate(false)
.write(true)
.open(&lock_path)?,
);
let _guard = lock.write()?;
if let Some(prepared) = self.reusable(&destination)? {
return Ok(prepared);
}
let stage = self.stage_scratch(&root)?;
let discarded = if destination.try_exists()? {
let discarded = tempfile::Builder::new()
.prefix(".invalid-")
.tempdir_in(&root)?;
std::fs::rename(&destination, discarded.path().join("runtime"))?;
Some(discarded)
} else {
None
};
match std::fs::rename(stage.path(), &destination) {
Ok(()) => {}
Err(error) if destination.try_exists()? => {
tracing::debug!(%error, "another initializer activated the tool bundle");
}
Err(error) => return Err(error.into()),
}
if let Some(discarded) = discarded {
discarded.close()?;
}
self.verify_prepared(&destination)
}
fn reusable(
&self,
destination: &Path,
) -> Result<Option<PreparedTools<PathBuf>>, ToolRuntimeError> {
if !destination.try_exists()? {
return Ok(None);
}
match self.verify_prepared(destination) {
Ok(prepared) => Ok(Some(prepared)),
Err(
error @ (ToolRuntimeError::InvalidCache(_) | ToolRuntimeError::Integrity { .. }),
) => {
tracing::warn!(
path = %destination.display(),
%error,
"prepared tool runtime is invalid and will be replaced"
);
Ok(None)
}
Err(error) => Err(error),
}
}
fn stage_scratch(&self, root: &Path) -> Result<tempfile::TempDir, ToolRuntimeError> {
let stage = tempfile::Builder::new()
.prefix(".initializing-")
.tempdir_in(root)?;
let bin = stage.path().join("bin");
std::fs::create_dir(&bin)?;
for binary in self.binaries.iter() {
let path = bin.join(binary.tool.executable_name(&self.target));
std::fs::write(&path, &binary.bytes)?;
set_executable_readonly(&path)?;
}
Ok(stage)
}
fn verify_prepared(
&self,
destination: &Path,
) -> Result<PreparedTools<PathBuf>, ToolRuntimeError> {
let bin_dir = destination.join("bin");
for binary in self.binaries.iter() {
let path = bin_dir.join(binary.tool.executable_name(&self.target));
let metadata = std::fs::symlink_metadata(&path)
.map_err(|error| cache_entry_error(&path, error))?;
if !metadata.file_type().is_file() {
return Err(ToolRuntimeError::InvalidCache(path));
}
let bytes = std::fs::read(&path).map_err(|error| cache_entry_error(&path, error))?;
if digest(&bytes) != binary.sha256 {
return Err(ToolRuntimeError::Integrity {
tool: binary.tool.into(),
});
}
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mode = metadata.permissions().mode();
if mode & 0o222 != 0 || mode & 0o111 == 0 {
return Err(ToolRuntimeError::InvalidCache(path));
}
}
#[cfg(not(unix))]
if !metadata.permissions().readonly() {
return Err(ToolRuntimeError::InvalidCache(path));
}
}
Ok(PreparedTools {
bin_dir,
catalog: self.catalog(),
})
}
}
#[derive(Debug, Clone)]
pub struct ImageTool {
pub tool: ManagedTool,
pub version: semver::Version,
pub archive_sha256: [u8; 32],
}
#[derive(Debug, Clone)]
pub struct ImageTools {
target: ToolTarget,
pub(crate) tools: Vec<ImageTool>,
}
impl ImageTools {
pub(crate) const fn new(target: ToolTarget, tools: Vec<ImageTool>) -> Self {
Self { target, tools }
}
pub(crate) fn cache_key(&self) -> Result<String, ToolRuntimeError> {
#[derive(Serialize)]
struct Identity<'a> {
target: &'a ToolTarget,
tools: Vec<(&'a str, &'a semver::Version, &'a [u8; 32])>,
}
let identity = Identity {
target: &self.target,
tools: self
.tools
.iter()
.map(|tool| (tool.tool.command(), &tool.version, &tool.archive_sha256))
.collect(),
};
Ok(hex::encode(digest(&serde_json::to_vec(&identity)?)))
}
pub fn container_directory(&self) -> Result<ContainerPath, ToolRuntimeError> {
Ok(ContainerPath::root()
.join("opt/aither/tools")
.join(self.cache_key()?))
}
pub(crate) fn container_bin_dir(&self) -> Result<ContainerPath, ToolRuntimeError> {
Ok(self.container_directory()?.join("bin"))
}
#[must_use]
pub fn catalog(&self) -> ToolCatalog {
ToolCatalog {
tools: self
.tools
.iter()
.map(|tool| ToolInfo {
command: tool.tool.command(),
version: tool.version.clone(),
})
.collect(),
}
}
}
pub fn digest(bytes: &[u8]) -> [u8; 32] {
Sha256::digest(bytes).into()
}
fn cache_entry_error(path: &Path, error: std::io::Error) -> ToolRuntimeError {
match error.kind() {
std::io::ErrorKind::NotFound | std::io::ErrorKind::NotADirectory => {
ToolRuntimeError::InvalidCache(path.to_path_buf())
}
_ => ToolRuntimeError::Io(error),
}
}
fn set_executable_readonly(path: &Path) -> Result<(), std::io::Error> {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o555))
}
#[cfg(not(unix))]
{
let mut permissions = std::fs::metadata(path)?.permissions();
permissions.set_readonly(true);
std::fs::set_permissions(path, permissions)
}
}
#[derive(Debug, Clone)]
pub struct PreparedTools<D> {
pub(crate) bin_dir: D,
pub(crate) catalog: ToolCatalog,
}
impl<D> PreparedTools<D> {
#[must_use]
pub const fn bin_dir(&self) -> &D {
&self.bin_dir
}
#[must_use]
pub const fn catalog(&self) -> &ToolCatalog {
&self.catalog
}
}
impl PreparedTools<PathBuf> {
pub(crate) async fn verify_local_versions<E: executor_core::Executor + Clone + 'static>(
&self,
executor: E,
) -> Result<(), ToolRuntimeError> {
use futures::{StreamExt, TryStreamExt};
let scratch = blocking::unblock(tempfile::tempdir).await?;
let config = heel::SandboxConfig::builder()
.working_dir(
std::fs::canonicalize(scratch.path())
.unwrap_or_else(|_| scratch.path().to_path_buf()),
)
.grant(
heel::ResolvedPath::new(std::fs::canonicalize(&self.bin_dir)?)
.map_err(|error| ToolRuntimeError::SshBootstrap(error.to_string()))?,
heel::Access::READ | heel::Access::EXEC,
)
.build();
let sandbox = std::sync::Arc::new(
heel::Sandbox::with_config_and_executor(config, executor)
.await
.map_err(|error| ToolRuntimeError::SshBootstrap(error.to_string()))?,
);
let directory = self.bin_dir.clone();
futures::stream::iter(self.catalog.tools.clone())
.map(|info| {
let sandbox = sandbox.clone();
let program = directory
.join(info.command)
.with_extension(std::env::consts::EXE_EXTENSION);
async move {
let program = program
.to_str()
.ok_or_else(|| ToolRuntimeError::InvalidCache(program.clone()))?;
let command = sandbox.command(program).arg("--version");
let output = futures_lite::future::or(
async {
command
.output()
.await
.map_err(|error| ToolRuntimeError::SshBootstrap(error.to_string()))
},
async {
async_io::Timer::after(std::time::Duration::from_secs(30)).await;
Err(ToolRuntimeError::InitializationTimeout)
},
)
.await?;
if !output.status.success() {
return Err(ToolRuntimeError::VersionMismatch(info.command.into()));
}
info.verify_version(&String::from_utf8(output.stdout)?)
}
})
.buffer_unordered(ManagedTool::ALL.len())
.try_collect::<Vec<_>>()
.await?;
Ok(())
}
#[doc = "Rejects writable host grants that expose the managed executable directory."]
#[doc = "# Errors"]
#[doc = "Returns path resolution errors or a writable-directory conflict."]
pub async fn ensure_readonly<'a>(
&self,
paths: impl IntoIterator<Item = &'a PathBuf> + Send,
) -> Result<(), ToolRuntimeError> {
use futures::{StreamExt, TryStreamExt};
let paths = paths.into_iter().cloned().collect::<Vec<_>>();
let bin_dir = async_fs::canonicalize(&self.bin_dir).await?;
futures::stream::iter(paths)
.map(|path| {
let bin_dir = &bin_dir;
async move {
let canonical = async_fs::canonicalize(path).await?;
if bin_dir.starts_with(&canonical) || canonical.starts_with(bin_dir) {
return Err(ToolRuntimeError::WritableToolDirectory(canonical));
}
Ok(())
}
})
.buffer_unordered(4)
.try_collect::<Vec<_>>()
.await?;
Ok(())
}
pub fn prepend_path(&self, existing: Option<&OsStr>) -> Result<OsString, ToolRuntimeError> {
let existing = existing.map(std::env::split_paths).into_iter().flatten();
Ok(std::env::join_paths(
std::iter::once(self.bin_dir.clone()).chain(existing),
)?)
}
}
#[derive(Template)]
#[template(path = "tool_path.sh", escape = "none")]
struct ToolPathTemplate<'a> {
bin_dir: std::borrow::Cow<'a, str>,
script: &'a str,
}
impl PreparedTools<PathBuf> {
pub(crate) fn script_bin_dir(&self) -> Result<&str, ToolRuntimeError> {
self.bin_dir
.to_str()
.ok_or_else(|| ToolRuntimeError::InvalidCache(self.bin_dir.clone()))
}
pub(crate) fn wrap_script(&self, script: &str) -> Result<String, ToolRuntimeError> {
render_tool_path(self.script_bin_dir()?, script)
}
}
impl PreparedTools<ContainerPath> {
pub(crate) fn wrap_script(&self, script: &str) -> Result<String, ToolRuntimeError> {
render_tool_path(self.bin_dir.as_str(), script)
}
}
fn render_tool_path(bin_dir: &str, script: &str) -> Result<String, ToolRuntimeError> {
Ok(ToolPathTemplate {
bin_dir: shell_words::quote(bin_dir),
script,
}
.render()?)
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ToolCatalog {
tools: Vec<ToolInfo>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct ToolInfo {
command: &'static str,
version: semver::Version,
}
impl ToolCatalog {
pub(crate) fn verify_versions(&self, output: &str) -> Result<(), ToolRuntimeError> {
let reported = output.split_terminator('\0').collect::<Vec<_>>();
if reported.len() != self.tools.len() {
return Err(ToolRuntimeError::InvalidProbe);
}
for (info, output) in self.tools.iter().zip(reported) {
info.verify_version(output)?;
}
Ok(())
}
}
impl ToolInfo {
fn verify_version(&self, output: &str) -> Result<(), ToolRuntimeError> {
let reported = output
.lines()
.next()
.and_then(|line| {
line.strip_prefix("jq-")
.or_else(|| line.split_whitespace().nth(1))
})
.and_then(|version| semver::Version::parse(version).ok());
if reported.as_ref() != Some(&self.version) {
return Err(ToolRuntimeError::VersionMismatch(self.command.into()));
}
Ok(())
}
}
#[derive(Template)]
#[template(path = "prepared_tools.txt", escape = "none")]
struct ToolCatalogTemplate<'a> {
tools: &'a [ToolInfo],
}
impl ToolCatalogTemplate<'_> {
fn has(&self, command: &str) -> bool {
self.tools.iter().any(|tool| tool.command == command)
}
}
impl ToolCatalog {
pub fn render(&self) -> Result<String, ToolRuntimeError> {
Ok(ToolCatalogTemplate { tools: &self.tools }.render()?)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PreparedEnvironment {
HomeBackend,
Container,
SshServers,
}
impl std::fmt::Display for PreparedEnvironment {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str(match self {
Self::HomeBackend => "the home execution backend",
Self::Container => "the container runtime",
Self::SshServers => "the SSH server set",
})
}
}
#[derive(Debug, thiserror::Error)]
pub enum ToolRuntimeError {
#[error("container runtime is not configured")]
MissingContainerRuntime,
#[error("prepared tool catalogs differ between environments")]
CatalogMismatch,
#[error("SSH runtime initialization failed: {0}")]
SshBootstrap(String),
#[error("managed tool installation denied for {0}")]
InstallDenied(String),
#[error("tool installation authorization failed: {0}")]
Authorization(crate::SshAuthorizationError),
#[error("tool initialization interrupted")]
InitializationInterrupted,
#[error("tool initialization timed out")]
InitializationTimeout,
#[error("invalid tool environment probe output")]
InvalidProbe,
#[error("tool initialization command failed ({status:?}): {stderr}")]
InitializationCommand {
status: Option<i32>,
stderr: String,
},
#[error("container tool initialization failed: {0}")]
Container(String),
#[error(transparent)]
Utf8(#[from] std::string::FromUtf8Error),
#[error("tool runtime is not initialized for {0}")]
NotInitialized(String),
#[error("{0} changed after tool preparation")]
EnvironmentChanged(PreparedEnvironment),
#[error("managed tool directory overlaps a writable sandbox path: {0}")]
WritableToolDirectory(PathBuf),
#[error("unsupported tool target: {os}/{arch}")]
UnsupportedTarget {
os: String,
arch: String,
},
#[error("prepared tool failed its version probe: {0}")]
VersionMismatch(String),
#[error("container image lacks verified baseline tools at {directory}: {defect}")]
InvalidImageTools {
directory: ContainerPath,
defect: ImageToolDefect,
},
#[error("required tool is missing: {0:?}")]
MissingTool(ManagedTool),
#[error("duplicate managed tool: {0:?}")]
DuplicateTool(ManagedTool),
#[error("host program `{}` has no artifact for {os}/{arch}", .program.command())]
MissingHostProgram {
program: HostProgram,
os: String,
arch: String,
},
#[error("host program `{}` has several artifacts for {os}/{arch}", .program.command())]
DuplicateHostProgram {
program: HostProgram,
os: String,
arch: String,
},
#[error("tool artifact failed SHA-256 verification: {tool:?}")]
Integrity {
tool: DistributedTool,
},
#[error("tool artifact exceeds the size limit: {0:?}")]
ArtifactTooLarge(DistributedTool),
#[error("invalid prepared tool cache entry: {0}")]
InvalidCache(PathBuf),
#[error("tool artifact platform does not match the execution environment")]
TargetMismatch,
#[error(transparent)]
Io(#[from] std::io::Error),
#[error(transparent)]
Version(#[from] semver::Error),
#[error(transparent)]
Path(#[from] std::env::JoinPathsError),
#[error(transparent)]
Manifest(#[from] serde_json::Error),
#[error("{0:?} requires a local file or credential-free HTTPS artifact source")]
ArtifactSource(DistributedTool),
#[error("tool artifact download failed with HTTP status {0}")]
DownloadStatus(u16),
#[error(transparent)]
Download(#[from] zenwave::Error),
#[error(transparent)]
Digest(#[from] hex::FromHexError),
#[error("invalid executable archive member")]
InvalidArchive,
#[error(transparent)]
Zip(#[from] zip::result::ZipError),
#[error(transparent)]
Template(#[from] askama::Error),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ImageToolDefect {
MissingDirectory,
MissingBinary(String),
}
impl std::fmt::Display for ImageToolDefect {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::MissingDirectory => write!(f, "missing bin directory"),
Self::MissingBinary(command) => {
write!(f, "missing or non-regular executable `{command}`")
}
}
}
}
#[cfg(test)]
pub mod tests {
use super::*;
use std::ffi::OsStr;
#[test]
fn artifact_digest_matches_known_sha256_vector() {
assert_eq!(
hex::encode(digest(b"abc")),
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
);
}
pub fn bundle() -> ToolBundle {
bundle_pinned("1.0.0")
}
pub fn bundle_pinned(version: &str) -> ToolBundle {
let binaries = ManagedTool::ALL
.into_iter()
.map(|tool| {
let bytes =
format!("#!/bin/sh\nprintf '%s\\n' '{} 1.0.0'\n", tool.command()).into_bytes();
ToolBinary::verify(tool, version, &digest(&bytes), bytes).unwrap()
})
.collect();
ToolBundle::new(ToolTarget::native().unwrap(), binaries).unwrap()
}
pub fn image_tools() -> ImageTools {
image_tools_pinned("1.0.0")
}
pub fn image_tools_pinned(version: &str) -> ImageTools {
let tools = ManagedTool::ALL
.into_iter()
.map(|tool| {
let bytes = format!("#!/bin/sh\nprintf '%s\\n' '{} {version}'\n", tool.command());
ImageTool {
tool,
version: semver::Version::parse(version).unwrap(),
archive_sha256: digest(bytes.as_bytes()),
}
})
.collect();
ImageTools::new(ToolTarget::native().unwrap(), tools)
}
pub fn container_tools(tools: &ImageTools) -> PreparedTools<ContainerPath> {
PreparedTools {
bin_dir: tools.container_bin_dir().unwrap(),
catalog: tools.catalog(),
}
}
#[test]
fn container_tool_paths_are_posix_on_every_host() {
let tools = image_tools();
let key = tools.cache_key().unwrap();
assert_eq!(
tools.container_directory().unwrap().as_str(),
format!("/opt/aither/tools/{key}")
);
assert_eq!(
tools.container_bin_dir().unwrap().as_str(),
format!("/opt/aither/tools/{key}/bin")
);
let script = container_tools(&tools).wrap_script("rg --version").unwrap();
assert!(
script.starts_with(&format!("PATH=/opt/aither/tools/{key}/bin")),
"{script}"
);
}
#[test]
fn missing_baseline_tool_is_an_initialization_error() {
let mut binaries = bundle().binaries.to_vec();
binaries.pop();
assert!(matches!(
ToolBundle::new(ToolTarget::native().unwrap(), binaries),
Err(ToolRuntimeError::MissingTool(ManagedTool::Jq))
));
}
#[test]
fn incorrect_digest_is_rejected_before_installation() {
assert!(matches!(
ToolBinary::verify(ManagedTool::Ripgrep, "1.0.0", &[0; 32], b"corrupt".to_vec()),
Err(ToolRuntimeError::Integrity {
tool: DistributedTool::Command(ManagedTool::Ripgrep)
})
));
}
#[tokio::test]
async fn initialize_outside_workspace_and_reuse_verified_content() {
let cache = tempfile::tempdir().unwrap();
let bundle = bundle();
let first = bundle.prepare_local(cache.path()).await.unwrap();
let second = bundle.prepare_local(cache.path()).await.unwrap();
assert_eq!(first.bin_dir(), second.bin_dir());
assert_eq!(first.catalog(), second.catalog());
for tool in ManagedTool::ALL {
let path = first.bin_dir().join(tool.executable_name(&bundle.target));
assert_eq!(
async_fs::read(path).await.unwrap().as_slice(),
bundle.binary(tool).bytes.as_ref()
);
}
let existing = [
cache.path().join("system tools"),
cache.path().join("project tools"),
];
let original_path = std::env::join_paths(&existing).unwrap();
let path = first.prepend_path(Some(&original_path)).unwrap();
let entries = std::env::split_paths(&path).collect::<Vec<_>>();
assert_eq!(entries[0], *first.bin_dir());
assert_eq!(entries[1..], existing);
}
#[tokio::test]
async fn concurrent_initialization_never_exposes_partial_tools() {
let cache = tempfile::tempdir().unwrap();
let bundle = bundle();
let (left, right) = futures_lite::future::zip(
bundle.prepare_local(cache.path()),
bundle.prepare_local(cache.path()),
)
.await;
let left = left.unwrap();
let right = right.unwrap();
assert_eq!(left.bin_dir(), right.bin_dir());
for tool in ManagedTool::ALL {
assert!(
left.bin_dir()
.join(tool.executable_name(&bundle.target))
.is_file()
);
}
}
#[tokio::test]
async fn managed_tools_cannot_overlap_a_writable_grant() {
let cache = tempfile::tempdir().unwrap();
let workspace = tempfile::tempdir().unwrap();
let ready = bundle().prepare_local(cache.path()).await.unwrap();
ready
.ensure_readonly([&workspace.path().to_path_buf()])
.await
.unwrap();
let result = ready.ensure_readonly([&cache.path().to_path_buf()]).await;
assert!(matches!(
result,
Err(ToolRuntimeError::WritableToolDirectory(_))
));
}
#[tokio::test]
async fn writable_grant_to_one_managed_executable_is_rejected() {
let cache = tempfile::tempdir().unwrap();
let bundle = bundle();
let ready = bundle.prepare_local(cache.path()).await.unwrap();
let executable = ready
.bin_dir()
.join(ManagedTool::Ripgrep.executable_name(&bundle.target));
assert!(matches!(
ready.ensure_readonly([&executable]).await,
Err(ToolRuntimeError::WritableToolDirectory(_))
));
}
fn assert_prepared(bundle: &ToolBundle, prepared: &PreparedTools<PathBuf>) {
for tool in ManagedTool::ALL {
let path = prepared
.bin_dir()
.join(tool.executable_name(&bundle.target));
assert_eq!(
std::fs::read(&path).unwrap().as_slice(),
bundle.binary(tool).bytes.as_ref(),
"{} differs from the bundle",
path.display()
);
}
bundle
.verify_prepared(prepared.bin_dir().parent().unwrap())
.unwrap();
}
fn leftovers(root: &Path, bundle: &ToolBundle) -> Vec<OsString> {
let key = bundle.cache_key().unwrap();
let lock = format!(".{key}.lock");
std::fs::read_dir(root)
.unwrap()
.map(|entry| entry.unwrap().file_name())
.filter(|name| *name != *key && *name != *lock)
.collect()
}
fn make_writable(path: &Path) {
let mut permissions = std::fs::metadata(path).unwrap().permissions();
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
permissions.set_mode(0o755);
}
#[cfg(not(unix))]
permissions.set_readonly(false);
std::fs::set_permissions(path, permissions).unwrap();
}
#[test]
fn missing_executable_is_reported_with_its_path() {
let cache = tempfile::tempdir().unwrap();
let bundle = bundle();
let destination = cache.path().join(bundle.cache_key().unwrap());
std::fs::create_dir_all(destination.join("bin")).unwrap();
let first = destination
.join("bin")
.join(bundle.binaries[0].tool.executable_name(&bundle.target));
assert!(matches!(
bundle.verify_prepared(&destination),
Err(ToolRuntimeError::InvalidCache(path)) if path == first
));
}
#[tokio::test]
async fn empty_runtime_skeleton_is_replaced() {
let cache = tempfile::tempdir().unwrap();
let bundle = bundle();
std::fs::create_dir_all(cache.path().join(bundle.cache_key().unwrap()).join("bin"))
.unwrap();
let ready = bundle.prepare_local(cache.path()).await.unwrap();
assert_prepared(&bundle, &ready);
assert_eq!(leftovers(cache.path(), &bundle), Vec::<OsString>::new());
}
#[tokio::test]
async fn tampered_executable_is_replaced() {
let cache = tempfile::tempdir().unwrap();
let bundle = bundle();
let ready = bundle.prepare_local(cache.path()).await.unwrap();
let path = ready
.bin_dir()
.join(ManagedTool::Ripgrep.executable_name(&bundle.target));
make_writable(&path);
std::fs::write(&path, b"tampered").unwrap();
assert!(matches!(
bundle.verify_prepared(ready.bin_dir().parent().unwrap()),
Err(ToolRuntimeError::Integrity {
tool: DistributedTool::Command(ManagedTool::Ripgrep)
})
));
let repaired = bundle.prepare_local(cache.path()).await.unwrap();
assert_eq!(repaired.bin_dir(), ready.bin_dir());
assert_prepared(&bundle, &repaired);
assert_eq!(leftovers(cache.path(), &bundle), Vec::<OsString>::new());
}
#[tokio::test]
async fn writable_executable_is_replaced() {
let cache = tempfile::tempdir().unwrap();
let bundle = bundle();
let ready = bundle.prepare_local(cache.path()).await.unwrap();
let path = ready
.bin_dir()
.join(ManagedTool::Jq.executable_name(&bundle.target));
make_writable(&path);
assert!(matches!(
bundle.verify_prepared(ready.bin_dir().parent().unwrap()),
Err(ToolRuntimeError::InvalidCache(invalid)) if invalid == path
));
let repaired = bundle.prepare_local(cache.path()).await.unwrap();
assert_prepared(&bundle, &repaired);
}
#[tokio::test]
async fn valid_runtime_is_reused_untouched() {
let cache = tempfile::tempdir().unwrap();
let bundle = bundle();
let ready = bundle.prepare_local(cache.path()).await.unwrap();
let snapshot = |prepared: &PreparedTools<PathBuf>| {
let mut entries = vec![prepared.bin_dir().parent().unwrap().to_path_buf()];
entries.push(prepared.bin_dir().clone());
entries.extend(ManagedTool::ALL.into_iter().map(|tool| {
prepared
.bin_dir()
.join(tool.executable_name(&bundle.target))
}));
entries
.into_iter()
.map(|path| {
let metadata = std::fs::symlink_metadata(&path).unwrap();
#[cfg(unix)]
let inode = std::os::unix::fs::MetadataExt::ino(&metadata);
#[cfg(not(unix))]
let inode = 0;
(path, inode, metadata.modified().unwrap())
})
.collect::<Vec<_>>()
};
let before = snapshot(&ready);
let reused = bundle.prepare_local(cache.path()).await.unwrap();
assert_eq!(before, snapshot(&reused));
}
#[tokio::test]
async fn concurrent_preparers_repair_one_damaged_runtime() {
let cache = tempfile::tempdir().unwrap();
let bundle = bundle();
std::fs::create_dir_all(cache.path().join(bundle.cache_key().unwrap()).join("bin"))
.unwrap();
let results =
futures::future::join_all((0..8).map(|_| bundle.prepare_local(cache.path()))).await;
for result in results {
assert_prepared(&bundle, &result.unwrap());
}
assert_eq!(leftovers(cache.path(), &bundle), Vec::<OsString>::new());
}
#[test]
fn prompt_catalog_is_operational_and_stable() {
let catalog = bundle().catalog();
let first = catalog.render().unwrap();
assert_eq!(first, catalog.render().unwrap());
for command in ManagedTool::ALL {
assert!(first.contains(command.command()), "{command:?}");
}
assert!(first.contains("ast-grep run"));
assert!(first.contains("*** Begin Patch"));
assert!(first.contains("--help"));
assert!(!first.contains("command -v"));
assert!(!first.contains("which "));
}
struct RuntimeProbe;
#[derive(serde::Deserialize, schemars::JsonSchema)]
struct RuntimeProbeArgs {}
impl heel::IpcCommand for RuntimeProbe {
fn name(&self) -> std::borrow::Cow<'static, str> {
"runtime_probe".into()
}
type Args = RuntimeProbeArgs;
type Response = String;
fn handle(
&self,
_args: RuntimeProbeArgs,
) -> impl Future<Output = Result<String, heel::IpcError>> {
std::future::ready(Ok("runtime_probe_ok".into()))
}
}
#[tokio::test]
async fn prepared_path_retains_heel_ipc_commands() {
let cache = tempfile::tempdir().unwrap();
let workspace = tempfile::tempdir().unwrap();
let ready = bundle().prepare_local(cache.path()).await.unwrap();
let sandbox_path = heel::default_sandbox_path();
let path = ready.prepend_path(Some(OsStr::new(&sandbox_path))).unwrap();
let config = heel::SandboxConfig::builder()
.network(heel::DenyAll)
.working_dir(
std::fs::canonicalize(workspace.path())
.unwrap_or_else(|_| workspace.path().to_path_buf()),
)
.grants([heel::Grant::new(
heel::ResolvedPath::new(
std::fs::canonicalize(ready.bin_dir())
.unwrap_or_else(|_| ready.bin_dir().clone()),
)
.unwrap(),
heel::Access::EXEC,
)])
.ipc(heel::IpcRouter::new().register(RuntimeProbe))
.security(heel::SecurityConfig::interactive())
.build();
let sandbox =
heel::Sandbox::with_config_and_executor(config, executor_core::tokio::TokioGlobal)
.await
.unwrap();
#[cfg(not(windows))]
let command = sandbox.command("sh").args(["-c", "runtime_probe"]);
#[cfg(windows)]
let command = sandbox
.command("cmd.exe")
.args(["/D", "/S", "/C", "runtime_probe"]);
let output = command
.env("PATH", path.to_str().unwrap())
.output()
.await
.unwrap();
assert!(
output.status.success(),
"prepared PATH removed IPC commands: {}",
String::from_utf8_lossy(&output.stderr)
);
assert!(String::from_utf8_lossy(&output.stdout).contains("runtime_probe_ok"));
}
#[test]
fn unknown_remote_platform_is_not_a_native_fallback() {
assert!(matches!(
ToolTarget::from_probe("FreeBSD", "riscv64"),
Err(ToolRuntimeError::UnsupportedTarget { .. })
));
}
}