fix(nix): share a native runtime layout across packages

This commit is contained in:
Kevin Yue committed 2026-10-07 18:17:56 +08:00
1 parent 673cf464b0
commit 0e444236d9
11 files changed
+238 -207

No files matched your search

+5 -4
View File
@@ -33,10 +33,11 @@ pub fn gp_vpnc_script_installer() -> PathBuf {
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
pub fn gp_hip_script_installer() -> PathBuf {
env::var_os("GP_HIP_SCRIPT_INSTALLER_BINARY")
.filter(|value| !value.is_empty())
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from(GP_HIP_SCRIPT_INSTALLER_BINARY))
resolve(
"GP_HIP_SCRIPT_INSTALLER_BINARY",
"gp-hip-script-installer",
GP_HIP_SCRIPT_INSTALLER_BINARY,
)
}
pub fn gpauth() -> PathBuf {
+34 -4
View File
@@ -53,8 +53,19 @@ const TOOL_DIRECTORIES: &[&str] = &[
"/sbin",
"/usr/local/bin",
"/usr/local/sbin",
#[cfg(target_os = "linux")]
"/run/current-system/sw/bin",
];
fn tool_directories() -> Vec<PathBuf> {
let configured = std::env::var_os("GP_COMMAND_PATH").unwrap_or_default();
let mut directories: Vec<_> = std::env::split_paths(&configured)
.filter(|path| path.is_absolute())
.collect();
directories.extend(TOOL_DIRECTORIES.iter().map(PathBuf::from));
directories
}
pub struct CollectorCommands<'a> {
control: &'a dyn CollectionControl,
}
@@ -65,9 +76,7 @@ impl<'a> CollectorCommands<'a> {
}
pub fn resolve(&self, name: &str) -> Option<PathBuf> {
TOOL_DIRECTORIES
.iter()
.find_map(|directory| resolve_executable(&Path::new(directory).join(name), uzers::get_effective_uid() == 0))
resolve_in_directories(name, &tool_directories(), uzers::get_effective_uid() == 0)
}
pub fn run(&self, name: &str, args: &[&str]) -> io::Result<Option<Output>> {
@@ -116,6 +125,12 @@ impl<'a> CollectorCommands<'a> {
}
}
fn resolve_in_directories(name: &str, directories: &[PathBuf], require_root: bool) -> Option<PathBuf> {
directories
.iter()
.find_map(|directory| resolve_executable(&directory.join(name), require_root))
}
fn resolve_executable(path: &Path, require_root: bool) -> Option<PathBuf> {
let resolved = fs::canonicalize(path).ok()?;
let metadata = fs::metadata(&resolved).ok()?;
@@ -129,7 +144,8 @@ fn resolve_executable(path: &Path, require_root: bool) -> Option<PathBuf> {
/// Known command directories, canonicalized and restricted to trusted root
/// ownership when used for privileged execution.
pub fn command_path(require_root: bool) -> io::Result<std::ffi::OsString> {
let directories = TOOL_DIRECTORIES.iter().filter_map(|directory| {
let configured = tool_directories();
let directories = configured.iter().filter_map(|directory| {
let resolved = fs::canonicalize(directory).ok()?;
if resolved.is_dir() && (!require_root || root_owned_path(&resolved)) {
Some(resolved)
@@ -152,6 +168,20 @@ fn root_owned_path(path: &Path) -> bool {
mod tests {
use super::*;
#[test]
fn configured_directories_resolve_tools_without_using_the_process_path() {
let dir = tempfile::tempdir().unwrap();
let tool = dir.path().join("configured-tool");
fs::write(&tool, "#!/bin/sh\nexit 0\n").unwrap();
fs::set_permissions(&tool, fs::Permissions::from_mode(0o755)).unwrap();
assert_eq!(
resolve_in_directories("configured-tool", &[dir.path().to_owned()], false),
Some(fs::canonicalize(&tool).unwrap())
);
fs::set_permissions(&tool, fs::Permissions::from_mode(0o777)).unwrap();
assert!(resolve_in_directories("configured-tool", &[dir.path().to_owned()], true).is_none());
}
#[test]
fn root_command_path_contains_only_trusted_canonical_directories() {
let path = command_path(true).unwrap();
+4 -4
View File
@@ -435,22 +435,22 @@ impl VpnBuilder {
self
}
fn determine_script(&self) -> Result<&str, VpnError> {
fn determine_script(&self) -> Result<String, VpnError> {
match &self.script {
Some(script) => {
if self.script_is_path && !std::path::Path::new(script).exists() {
return Err(VpnError::new(format!("VPN script does not exist: {script}")));
}
check_executable(script).map_err(|e| VpnError::new(e.to_string()))?;
Ok(script)
Ok(script.clone())
}
None => find_vpnc_script().ok_or_else(|| VpnError::new(String::from("Failed to find vpnc-script"))),
}
}
pub fn build(self) -> Result<Vpn, VpnError> {
let script = self.determine_script()?.to_owned();
let script = if self.script_is_path {
let script = self.determine_script()?;
let script = if self.script_is_path || self.script.is_none() {
shell_quote_path(&script)
} else {
script
+26 -2
View File
@@ -30,8 +30,18 @@ fn find_executable(locations: &[&'static str]) -> Option<&'static str> {
None
}
pub fn find_vpnc_script() -> Option<&'static str> {
find_executable(VPNC_SCRIPT_LOCATIONS)
pub fn find_vpnc_script() -> Option<String> {
if let Some(path) = std::env::var_os("GP_VPNC_SCRIPT").filter(|path| !path.is_empty()) {
return configured_script(Path::new(&path));
}
find_executable(VPNC_SCRIPT_LOCATIONS).map(str::to_owned)
}
fn configured_script(path: &Path) -> Option<String> {
if path.is_absolute() && path.is_file() && path.is_executable() {
return path.to_str().map(str::to_owned);
}
None
}
/// If file exists, check if it is executable
@@ -47,3 +57,17 @@ pub fn check_executable(file: &str) -> Result<(), io::Error> {
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn configured_script_requires_an_absolute_executable_path() {
assert_eq!(configured_script(Path::new("/bin/sh")), Some("/bin/sh".to_owned()));
assert!(configured_script(Path::new("bin/sh")).is_none());
assert!(configured_script(Path::new("/missing-openconnect-vpnc-script")).is_none());
assert!(configured_script(Path::new("/etc/passwd")).is_none());
assert!(configured_script(Path::new("/tmp")).is_none());
}
}