use serde::Serialize;
use crate::mxc_common::models::ContainmentBackend;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum ProxyEnforcement {
Supported,
Unsupported,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BubblewrapNetworkSupport {
pub proxy_enforcement: ProxyEnforcement,
pub warnings: Vec<String>,
}
#[derive(Debug, Clone, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PlatformSupport {
pub is_supported: bool,
pub reason: Option<String>,
pub available_methods: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bubblewrap_network: Option<BubblewrapNetworkSupport>,
}
#[cfg(target_os = "linux")]
struct BwrapProbe<F>(F);
#[cfg(target_os = "linux")]
struct ProxyEnforcementProbe<G>(G);
#[cfg(target_os = "linux")]
struct LxcProbe<H>(H);
#[cfg(target_os = "linux")]
fn linux_platform_support_with<F, G, H>(
lxc: LxcProbe<H>,
bwrap: BwrapProbe<F>,
proxy_enforcement: ProxyEnforcementProbe<G>,
) -> PlatformSupport
where
F: FnOnce() -> Result<
crate::bwrap_common::bwrap_version::BwrapVersion,
crate::bwrap_common::bwrap_version::BwrapUnavailable,
>,
G: FnOnce() -> Result<(), String>,
H: FnOnce() -> bool,
{
let lxc_available = (lxc.0)();
let bwrap_probe = (bwrap.0)();
let mut available_methods = Vec::new();
if lxc_available {
available_methods.push(ContainmentBackend::Lxc.wire_name().to_string());
}
if bwrap_probe.is_ok() {
available_methods.push(ContainmentBackend::Bubblewrap.wire_name().to_string());
}
match bwrap_probe {
Ok(_) => PlatformSupport {
is_supported: true,
available_methods,
bubblewrap_network: Some(bubblewrap_network_support((proxy_enforcement.0)())),
..Default::default()
},
Err(_) if lxc_available => PlatformSupport {
is_supported: true,
available_methods,
..Default::default()
},
Err(err) => PlatformSupport {
reason: Some(format!(
"Neither LXC nor Bubblewrap is available on this system ({err})"
)),
..Default::default()
},
}
}
pub fn platform_support() -> PlatformSupport {
#[cfg(target_os = "macos")]
{
if crate::seatbelt_common::seatbelt_runner::sandbox_init_available() {
PlatformSupport {
is_supported: true,
available_methods: vec![ContainmentBackend::Seatbelt.wire_name().to_string()],
..Default::default()
}
} else {
PlatformSupport {
reason: Some(
"Seatbelt could not be found on this installation of macOS. Your macOS install might be incomplete. (sandbox_init not found)".to_string(),
),
..Default::default()
}
}
}
#[cfg(target_os = "linux")]
{
linux_platform_support_with(
LxcProbe(crate::lxc_common::availability::is_lxc_available),
BwrapProbe(crate::bwrap_common::bwrap_version::probe_bwrap),
ProxyEnforcementProbe(crate::bwrap_common::proxy_network::probe_proxy_enforcement),
)
}
#[cfg(target_os = "windows")]
{
let mut available_methods =
vec![ContainmentBackend::ProcessContainer.wire_name().to_string()];
if wslc_available() {
available_methods.push(ContainmentBackend::Wslc.wire_name().to_string());
}
if isolation_session_present() {
available_methods.push(ContainmentBackend::IsolationSession.wire_name().to_string());
}
PlatformSupport {
is_supported: true,
available_methods,
..Default::default()
}
}
#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
{
PlatformSupport {
reason: Some("MXC is not supported on this platform".to_string()),
..Default::default()
}
}
}
#[cfg(target_os = "linux")]
fn bubblewrap_network_support(probe: Result<(), String>) -> BubblewrapNetworkSupport {
match probe {
Ok(()) => BubblewrapNetworkSupport {
proxy_enforcement: ProxyEnforcement::Supported,
warnings: Vec::new(),
},
Err(reason) => BubblewrapNetworkSupport {
proxy_enforcement: ProxyEnforcement::Unsupported,
warnings: vec![reason],
},
}
}
#[cfg(target_os = "windows")]
fn wslc_available() -> bool {
#[cfg(feature = "wslc")]
{
crate::wslc_common::is_available()
}
#[cfg(not(feature = "wslc"))]
{
false
}
}
#[cfg(all(target_os = "windows", feature = "isolation_session"))]
pub fn isolation_session_available() -> bool {
crate::isolation_session_common::availability::is_isolation_session_available()
}
#[cfg(target_os = "windows")]
fn isolation_session_present() -> bool {
#[cfg(feature = "isolation_session")]
{
isolation_session_available()
}
#[cfg(not(feature = "isolation_session"))]
{
false
}
}
#[cfg(test)]
mod tests {
#[cfg(target_os = "linux")]
use super::linux_platform_support_with;
use super::platform_support;
#[cfg(target_os = "linux")]
use super::{
bubblewrap_network_support, BwrapProbe, LxcProbe, ProxyEnforcement, ProxyEnforcementProbe,
};
#[cfg(target_os = "linux")]
use crate::bwrap_common::bwrap_version::{BwrapUnavailable, BwrapVersion, MIN_BWRAP_VERSION};
use crate::mxc_engine::backend_registry::{self, CANONICAL_BACKEND_NAMES};
#[cfg(target_os = "linux")]
#[test]
fn bubblewrap_network_is_supported_when_probe_succeeds() {
let support = bubblewrap_network_support(Ok(()));
assert_eq!(support.proxy_enforcement, ProxyEnforcement::Supported);
assert!(support.warnings.is_empty());
}
#[cfg(target_os = "linux")]
#[test]
fn bubblewrap_network_is_unsupported_with_reason_when_probe_fails() {
let support = bubblewrap_network_support(Err("slirp4netns not found".to_string()));
assert_eq!(support.proxy_enforcement, ProxyEnforcement::Unsupported);
assert_eq!(support.warnings, vec!["slirp4netns not found".to_string()]);
}
#[cfg(not(target_os = "linux"))]
#[test]
fn non_linux_hosts_report_no_bubblewrap_network() {
let support = platform_support();
assert!(
support.bubblewrap_network.is_none(),
"bubblewrap_network must be absent off Linux, got: {:?}",
support.bubblewrap_network
);
let json = serde_json::to_string(&support).expect("PlatformSupport serializes");
assert!(
!json.contains("bubblewrapNetwork"),
"the field must be omitted from the wire form off Linux, got: {json}"
);
}
fn all_containment_names() -> Vec<String> {
backend_registry::all_containment_names()
}
#[test]
fn reported_method_names_match_the_containment_wire_names() {
for (backend, expected) in CANONICAL_BACKEND_NAMES {
assert_eq!(backend.wire_name(), *expected, "{backend:?}");
}
}
#[test]
fn every_reported_method_is_a_real_wire_name() {
let known = all_containment_names();
for method in platform_support().available_methods {
assert!(
known.contains(&method),
"reported method {method:?} is not a Containment wire name"
);
}
}
#[cfg(target_os = "linux")]
#[test]
fn linux_support_reports_bubblewrap_when_probe_succeeds() {
let support = linux_platform_support_with(
LxcProbe(|| false),
BwrapProbe(|| Ok(MIN_BWRAP_VERSION)),
ProxyEnforcementProbe(|| Ok(())),
);
assert!(support.is_supported);
assert_eq!(support.reason, None);
assert_eq!(support.available_methods, ["bubblewrap"]);
assert_eq!(
support
.bubblewrap_network
.expect("network support is reported alongside a usable bwrap")
.proxy_enforcement,
ProxyEnforcement::Supported
);
}
#[cfg(target_os = "linux")]
#[test]
fn linux_support_reports_lxc_alongside_bubblewrap() {
let support = linux_platform_support_with(
LxcProbe(|| true),
BwrapProbe(|| Ok(MIN_BWRAP_VERSION)),
ProxyEnforcementProbe(|| Ok(())),
);
assert!(support.is_supported);
assert_eq!(support.available_methods, ["lxc", "bubblewrap"]);
}
#[cfg(target_os = "linux")]
#[test]
fn linux_support_stands_on_lxc_alone() {
let support = linux_platform_support_with(
LxcProbe(|| true),
BwrapProbe(|| Err(BwrapUnavailable::TooOld(BwrapVersion::new(0, 4, 1)))),
ProxyEnforcementProbe(|| {
panic!("the network walk must not run without a usable bwrap")
}),
);
assert!(support.is_supported);
assert_eq!(support.reason, None);
assert_eq!(support.available_methods, ["lxc"]);
assert!(support.bubblewrap_network.is_none());
}
#[cfg(target_os = "linux")]
#[test]
fn linux_support_preserves_probe_failure_reason() {
let failure = BwrapUnavailable::TooOld(BwrapVersion::new(0, 4, 1));
let expected = failure.to_string();
let support = linux_platform_support_with(
LxcProbe(|| false),
BwrapProbe(|| Err(failure)),
ProxyEnforcementProbe(|| {
panic!("the network walk must not run without a usable bwrap")
}),
);
assert!(!support.is_supported);
let reason = support.reason.expect("an unsupported host explains itself");
assert!(reason.contains(&expected), "got: {reason}");
assert!(reason.contains("LXC"), "got: {reason}");
assert!(support.available_methods.is_empty());
assert!(support.bubblewrap_network.is_none());
}
#[cfg(target_os = "linux")]
#[test]
fn linux_support_reports_bubblewrap_without_proxy_enforcement() {
let support = linux_platform_support_with(
LxcProbe(|| false),
BwrapProbe(|| Ok(MIN_BWRAP_VERSION)),
ProxyEnforcementProbe(|| Err("slirp4netns not found".to_string())),
);
assert!(support.is_supported);
assert_eq!(support.available_methods, ["bubblewrap"]);
let network = support
.bubblewrap_network
.expect("network support is reported alongside a usable bwrap");
assert_eq!(network.proxy_enforcement, ProxyEnforcement::Unsupported);
assert_eq!(network.warnings, ["slirp4netns not found".to_string()]);
}
}