|
1 | 1 | use super::{BuildService, DummyBuildService, grpc::GRPCBuildService}; |
| 2 | +use serde::Deserialize; |
2 | 3 | use snix_castore::{blobservice::BlobService, directoryservice::DirectoryService}; |
| 4 | +use std::path::PathBuf; |
| 5 | +use std::sync::OnceLock; |
3 | 6 | use url::Url; |
4 | 7 |
|
5 | 8 | #[cfg(target_os = "linux")] |
6 | 9 | use super::oci::OCIBuildService; |
7 | 10 |
|
| 11 | +/// Extract the embedded busybox binary to a temporary location and return its path |
| 12 | +#[cfg(all(target_os = "linux", feature = "embedded-busybox"))] |
| 13 | +fn get_embedded_busybox_path() -> Result<PathBuf, std::io::Error> { |
| 14 | + use std::fs; |
| 15 | + use std::os::unix::fs::PermissionsExt; |
| 16 | + |
| 17 | + static EXTRACTED_BUSYBOX_PATH: OnceLock<PathBuf> = OnceLock::new(); |
| 18 | + |
| 19 | + // The embedded busybox binary (included at compile time) |
| 20 | + static EMBEDDED_BUSYBOX_BINARY: &[u8] = include_bytes!(env!("SNIX_BUILD_SANDBOX_SHELL")); |
| 21 | + |
| 22 | + EXTRACTED_BUSYBOX_PATH |
| 23 | + .get_or_try_init(|| { |
| 24 | + let temp_dir = std::env::temp_dir(); |
| 25 | + let busybox_path = temp_dir.join(format!("snix-busybox-{}", std::process::id())); |
| 26 | + |
| 27 | + // Write the binary |
| 28 | + fs::write(&busybox_path, EMBEDDED_BUSYBOX_BINARY)?; |
| 29 | + |
| 30 | + // Make it executable |
| 31 | + let mut perms = fs::metadata(&busybox_path)?.permissions(); |
| 32 | + perms.set_mode(0o755); |
| 33 | + fs::set_permissions(&busybox_path, perms)?; |
| 34 | + |
| 35 | + tracing::debug!(?busybox_path, "extracted embedded busybox binary"); |
| 36 | + |
| 37 | + Ok(busybox_path) |
| 38 | + }) |
| 39 | + .cloned() |
| 40 | +} |
| 41 | + |
| 42 | +/// Configuration for OCIBuildService |
| 43 | +#[cfg(target_os = "linux")] |
| 44 | +#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] |
| 45 | +#[serde(deny_unknown_fields)] |
| 46 | +pub struct OCIBuildServiceConfig { |
| 47 | + /// Root path in which all bundles are created |
| 48 | + pub bundle_root: PathBuf, |
| 49 | + |
| 50 | + /// Path to the sandbox shell to use. |
| 51 | + /// This needs to be a statically linked binary, or you must ensure all |
| 52 | + /// dependencies are part of the build (which they usually are not). |
| 53 | + #[serde(default = "default_sandbox_shell")] |
| 54 | + pub sandbox_shell: PathBuf, |
| 55 | + // TODO: make rootless_uid_gid configurable |
| 56 | +} |
| 57 | + |
| 58 | +#[cfg(target_os = "linux")] |
| 59 | +fn default_sandbox_shell() -> PathBuf { |
| 60 | + // Priority: embedded busybox -> SNIX_BUILD_SANDBOX_SHELL env var -> /bin/sh |
| 61 | + #[cfg(feature = "embedded-busybox")] |
| 62 | + { |
| 63 | + if let Ok(path) = get_embedded_busybox_path() { |
| 64 | + return path; |
| 65 | + } |
| 66 | + tracing::warn!( |
| 67 | + "failed to extract embedded busybox, falling back to environment variable or /bin/sh" |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + // Check environment variable (for backward compatibility when feature is disabled) |
| 72 | + if let Ok(shell_path) = std::env::var("SNIX_BUILD_SANDBOX_SHELL") { |
| 73 | + return PathBuf::from(shell_path); |
| 74 | + } |
| 75 | + |
| 76 | + PathBuf::from("/bin/sh") |
| 77 | +} |
| 78 | + |
| 79 | +#[cfg(target_os = "linux")] |
| 80 | +impl TryFrom<Url> for OCIBuildServiceConfig { |
| 81 | + type Error = Box<dyn std::error::Error + Send + Sync>; |
| 82 | + |
| 83 | + fn try_from(url: Url) -> Result<Self, Self::Error> { |
| 84 | + // oci wants a path in which it creates bundles |
| 85 | + if url.path().is_empty() { |
| 86 | + return Err("oci needs a bundle dir as path".into()); |
| 87 | + } |
| 88 | + |
| 89 | + // Parse sandbox_shell from query parameters |
| 90 | + let query_pairs: std::collections::HashMap<_, _> = url.query_pairs().collect(); |
| 91 | + let sandbox_shell = query_pairs |
| 92 | + .get("sandbox_shell") |
| 93 | + .map(|s| PathBuf::from(s.as_ref())) |
| 94 | + .unwrap_or_else(default_sandbox_shell); |
| 95 | + |
| 96 | + Ok(OCIBuildServiceConfig { |
| 97 | + bundle_root: url.path().into(), |
| 98 | + sandbox_shell, |
| 99 | + }) |
| 100 | + } |
| 101 | +} |
| 102 | + |
8 | 103 | /// Constructs a new instance of a [BuildService] from an URI. |
9 | 104 | /// |
10 | 105 | /// The following schemes are supported by the following services: |
@@ -32,17 +127,14 @@ where |
32 | 127 | "dummy" => Box::<DummyBuildService>::default(), |
33 | 128 | #[cfg(target_os = "linux")] |
34 | 129 | "oci" => { |
35 | | - // oci wants a path in which it creates bundles. |
36 | | - if url.path().is_empty() { |
37 | | - Err(std::io::Error::other("oci needs a bundle dir as path"))? |
38 | | - } |
39 | | - |
40 | | - // TODO: make sandbox shell and rootless_uid_gid |
| 130 | + let config = OCIBuildServiceConfig::try_from(url) |
| 131 | + .map_err(|e| std::io::Error::other(format!("invalid oci config: {}", e)))?; |
41 | 132 |
|
42 | 133 | Box::new(OCIBuildService::new( |
43 | | - url.path().into(), |
| 134 | + config.bundle_root, |
44 | 135 | blob_service, |
45 | 136 | directory_service, |
| 137 | + config.sandbox_shell, |
46 | 138 | )) |
47 | 139 | } |
48 | 140 | scheme => { |
|
0 commit comments