a3s-box-runtime 3.2.0

MicroVM runtime engine — VM lifecycle, OCI images, attestation, networking
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
//! Explicit production composition for Sandbox migration to A3S OCI Runtime.

use std::ffi::OsString;
use std::path::{Component, Path, PathBuf};
use std::sync::Arc;

#[cfg(target_os = "linux")]
use a3s_box_core::ExecutionBackend;
use a3s_box_core::{ExecutionManagerError, ExecutionManagerResult};
#[cfg(target_os = "linux")]
use sha2::{Digest, Sha256};

use super::LocalExecutionManager;
#[cfg(target_os = "linux")]
use super::{NativeLinuxOciBundleProvider, OciLocalExecutionBackend, OciMigrationPolicy};

pub const OCI_MIGRATION_ENV: &str = "A3S_BOX_OCI_MIGRATION";
pub const OCI_HOST_ROOT_ENV: &str = "A3S_BOX_OCI_HOST_ROOT";
pub const OCI_RUNTIME_PATH_ENV: &str = "A3S_BOX_OCI_RUNTIME_PATH";
pub const OCI_AGENT_PATH_ENV: &str = "A3S_BOX_OCI_AGENT_PATH";
pub const OCI_WHPX_ENDPOINT_ENV: &str = "A3S_BOX_OCI_WHPX_ENDPOINT";
#[cfg(test)]
const DEFAULT_OCI_WHPX_ENDPOINT: &str = r"\\.\pipe\a3s-oci-box-qualification";

/// Explicit native-Linux owner and artifact selection for Sandbox migration.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NativeLinuxOciMigrationConfig {
    service_root: PathBuf,
    runtime_path: Option<PathBuf>,
    agent_path: Option<PathBuf>,
}

/// Explicit connection to an externally owned qualification-only WHPX service.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WindowsWhpxOciMigrationConfig {
    runtime_root: PathBuf,
    endpoint: super::OciRuntimeEndpoint,
}

impl WindowsWhpxOciMigrationConfig {
    pub fn new(
        runtime_root: impl Into<PathBuf>,
        endpoint: impl Into<String>,
    ) -> ExecutionManagerResult<Self> {
        let config = Self {
            runtime_root: runtime_root.into(),
            endpoint: super::OciRuntimeEndpoint::windows_named_pipe(endpoint)?,
        };
        config.validate()?;
        Ok(config)
    }

    pub fn runtime_root(&self) -> &Path {
        &self.runtime_root
    }

    pub fn endpoint(&self) -> &super::OciRuntimeEndpoint {
        &self.endpoint
    }

    pub fn from_environment(home_dir: &Path) -> ExecutionManagerResult<Option<Self>> {
        parse_windows_environment(
            std::env::var_os(OCI_MIGRATION_ENV),
            std::env::var_os(OCI_HOST_ROOT_ENV),
            std::env::var_os(OCI_WHPX_ENDPOINT_ENV),
            home_dir,
        )
    }

    fn validate(&self) -> ExecutionManagerResult<()> {
        validate_absolute_normalized(&self.runtime_root, "WHPX OCI runtime root")?;
        match &self.endpoint {
            super::OciRuntimeEndpoint::WindowsNamedPipe { .. } => Ok(()),
            super::OciRuntimeEndpoint::UnixSocket { .. } => {
                Err(ExecutionManagerError::InvalidRequest(
                    "WHPX OCI qualification requires a Windows named-pipe endpoint".to_string(),
                ))
            }
        }
    }
}

impl NativeLinuxOciMigrationConfig {
    pub fn new(service_root: impl Into<PathBuf>) -> ExecutionManagerResult<Self> {
        let config = Self {
            service_root: service_root.into(),
            runtime_path: None,
            agent_path: None,
        };
        config.validate()?;
        Ok(config)
    }

    pub fn with_artifacts(
        mut self,
        runtime_path: impl Into<PathBuf>,
        agent_path: impl Into<PathBuf>,
    ) -> ExecutionManagerResult<Self> {
        self.runtime_path = Some(runtime_path.into());
        self.agent_path = Some(agent_path.into());
        self.validate()?;
        Ok(self)
    }

    pub fn service_root(&self) -> &Path {
        &self.service_root
    }

    pub fn runtime_path(&self) -> Option<&Path> {
        self.runtime_path.as_deref()
    }

    pub fn agent_path(&self) -> Option<&Path> {
        self.agent_path.as_deref()
    }

    /// Parse the process-wide opt-in. An absent/off value preserves the
    /// existing VM backend without probing or starting an OCI owner.
    pub fn from_environment(home_dir: &Path) -> ExecutionManagerResult<Option<Self>> {
        parse_environment(
            std::env::var_os(OCI_MIGRATION_ENV),
            std::env::var_os(OCI_HOST_ROOT_ENV),
            std::env::var_os(OCI_RUNTIME_PATH_ENV),
            std::env::var_os(OCI_AGENT_PATH_ENV),
            home_dir,
        )
    }

    fn validate(&self) -> ExecutionManagerResult<()> {
        validate_absolute_normalized(&self.service_root, "OCI host root")?;
        match (&self.runtime_path, &self.agent_path) {
            (Some(runtime), Some(agent)) => {
                validate_absolute_normalized(runtime, "OCI runtime path")?;
                validate_absolute_normalized(agent, "OCI agent path")?;
            }
            (None, None) => {}
            _ => {
                return Err(ExecutionManagerError::InvalidRequest(
                    "OCI runtime and agent paths must be supplied together".to_string(),
                ))
            }
        }
        Ok(())
    }
}

impl LocalExecutionManager {
    /// Compose the retained VM backend with the production native-Linux OCI
    /// owner and bundle provider. Only new Sandbox reservations use OCI.
    pub async fn with_native_linux_oci_migration(
        state_path: impl Into<PathBuf>,
        home_dir: impl Into<PathBuf>,
        config: NativeLinuxOciMigrationConfig,
    ) -> ExecutionManagerResult<Self> {
        Self::with_native_linux_oci_migration_and_pull_progress(
            state_path.into(),
            home_dir.into(),
            config,
            None,
        )
        .await
    }

    async fn with_native_linux_oci_migration_and_pull_progress(
        state_path: PathBuf,
        home_dir: PathBuf,
        config: NativeLinuxOciMigrationConfig,
        pull_progress_fn: Option<crate::PullProgressFn>,
    ) -> ExecutionManagerResult<Self> {
        config.validate()?;

        #[cfg(target_os = "linux")]
        {
            let capabilities = crate::sandbox::probe_sandbox_capabilities_for(
                ExecutionBackend::A3sOci,
                config.runtime_path(),
                config.agent_path(),
            );
            capabilities.require_ready().map_err(|error| {
                ExecutionManagerError::Unavailable(format!(
                    "native Linux OCI migration preflight failed: {error}"
                ))
            })?;
            let artifacts = capabilities.a3s_oci.as_ref().ok_or_else(|| {
                ExecutionManagerError::Unavailable(
                    "native Linux OCI migration preflight returned no runtime artifacts"
                        .to_string(),
                )
            })?;
            let endpoint =
                super::oci_owner::ensure_native_linux_oci_owner(config.service_root(), artifacts)
                    .await?;
            let mut provider = NativeLinuxOciBundleProvider::new(
                home_dir.clone(),
                artifacts.runtime_path.clone(),
                artifacts.agent_path.clone(),
            );
            if let Some(progress) = pull_progress_fn.as_ref() {
                provider = provider.with_pull_progress_fn(progress.clone());
            }
            let provider = Arc::new(provider);
            let oci = Arc::new(OciLocalExecutionBackend::connect(endpoint, provider).await?);
            Ok(Self::with_oci_migration_backend_and_pull_progress(
                state_path,
                home_dir,
                oci,
                OciMigrationPolicy::SandboxViaOci,
                pull_progress_fn,
            ))
        }

        #[cfg(not(target_os = "linux"))]
        {
            let _ = (state_path, home_dir, config, pull_progress_fn);
            Err(ExecutionManagerError::Unavailable(
                "native Linux OCI migration is supported only on Linux".to_string(),
            ))
        }
    }

    /// Compose the retained backend with the externally launched Box/WHPX OCI service.
    pub async fn with_windows_whpx_oci_qualification(
        state_path: impl Into<PathBuf>,
        home_dir: impl Into<PathBuf>,
        config: WindowsWhpxOciMigrationConfig,
    ) -> ExecutionManagerResult<Self> {
        Self::with_windows_whpx_oci_qualification_and_pull_progress(
            state_path.into(),
            home_dir.into(),
            config,
            None,
        )
        .await
    }

    async fn with_windows_whpx_oci_qualification_and_pull_progress(
        state_path: PathBuf,
        home_dir: PathBuf,
        config: WindowsWhpxOciMigrationConfig,
        pull_progress_fn: Option<crate::PullProgressFn>,
    ) -> ExecutionManagerResult<Self> {
        config.validate()?;

        #[cfg(all(target_os = "windows", target_arch = "x86_64"))]
        {
            let mut provider =
                super::WindowsWhpxOciBundleProvider::new(home_dir.clone(), config.runtime_root());
            if let Some(progress) = pull_progress_fn.as_ref() {
                provider = provider.with_pull_progress_fn(progress.clone());
            }
            let oci = Arc::new(
                super::OciLocalExecutionBackend::connect(
                    config.endpoint().clone(),
                    Arc::new(provider),
                )
                .await?,
            );
            Ok(Self::with_oci_migration_backend_and_pull_progress(
                state_path,
                home_dir,
                oci,
                super::OciMigrationPolicy::AllViaOci,
                pull_progress_fn,
            ))
        }

        #[cfg(not(all(target_os = "windows", target_arch = "x86_64")))]
        {
            let _ = (state_path, home_dir, config, pull_progress_fn);
            Err(ExecutionManagerError::Unavailable(
                "Box/WHPX OCI qualification requires Windows x86_64".to_string(),
            ))
        }
    }

    /// Select the production migration composition only when explicitly opted
    /// in through `A3S_BOX_OCI_MIGRATION=sandbox`.
    pub async fn with_configured_backend(
        state_path: impl Into<PathBuf>,
        home_dir: impl Into<PathBuf>,
    ) -> ExecutionManagerResult<Self> {
        Self::with_configured_backend_and_pull_progress(state_path, home_dir, None).await
    }

    /// Configured construction retaining the CLI's image-pull progress hook on
    /// both the legacy and migrated preparation paths.
    pub async fn with_configured_backend_and_pull_progress(
        state_path: impl Into<PathBuf>,
        home_dir: impl Into<PathBuf>,
        pull_progress_fn: Option<crate::PullProgressFn>,
    ) -> ExecutionManagerResult<Self> {
        let state_path = state_path.into();
        let home_dir = home_dir.into();

        #[cfg(all(target_os = "windows", target_arch = "x86_64"))]
        {
            match WindowsWhpxOciMigrationConfig::from_environment(&home_dir)? {
                Some(config) => {
                    Self::with_windows_whpx_oci_qualification_and_pull_progress(
                        state_path,
                        home_dir,
                        config,
                        pull_progress_fn,
                    )
                    .await
                }
                None => legacy_backend(state_path, home_dir, pull_progress_fn),
            }
        }

        #[cfg(not(all(target_os = "windows", target_arch = "x86_64")))]
        {
            match NativeLinuxOciMigrationConfig::from_environment(&home_dir)? {
                Some(config) => {
                    Self::with_native_linux_oci_migration_and_pull_progress(
                        state_path,
                        home_dir,
                        config,
                        pull_progress_fn,
                    )
                    .await
                }
                None => legacy_backend(state_path, home_dir, pull_progress_fn),
            }
        }
    }
}

fn legacy_backend(
    state_path: PathBuf,
    home_dir: PathBuf,
    pull_progress_fn: Option<crate::PullProgressFn>,
) -> ExecutionManagerResult<LocalExecutionManager> {
    let mut backend = crate::local_execution::VmLocalExecutionBackend::new(&home_dir);
    if let Some(progress) = pull_progress_fn {
        backend = backend.with_pull_progress_fn(progress);
    }
    Ok(LocalExecutionManager::new(
        state_path,
        home_dir,
        Arc::new(backend),
    ))
}

fn parse_environment(
    mode: Option<OsString>,
    service_root: Option<OsString>,
    runtime_path: Option<OsString>,
    agent_path: Option<OsString>,
    home_dir: &Path,
) -> ExecutionManagerResult<Option<NativeLinuxOciMigrationConfig>> {
    let Some(mode) = mode.filter(|value| !value.is_empty()) else {
        return Ok(None);
    };
    let mode = mode.to_str().ok_or_else(|| {
        ExecutionManagerError::InvalidRequest(format!(
            "{OCI_MIGRATION_ENV} must contain UTF-8 text"
        ))
    })?;
    match mode.trim().to_ascii_lowercase().as_str() {
        "" | "0" | "false" | "off" | "disabled" | "legacy" => return Ok(None),
        "1" | "true" | "on" | "sandbox" | "sandbox-via-oci" => {}
        "all" | "all-via-oci" => {
            return Err(ExecutionManagerError::InvalidRequest(
                "all-via-OCI migration is not qualified yet; use sandbox".to_string(),
            ))
        }
        value => {
            return Err(ExecutionManagerError::InvalidRequest(format!(
                "unsupported {OCI_MIGRATION_ENV} value {value:?}; expected off or sandbox"
            )))
        }
    }

    let root = service_root
        .filter(|value| !value.is_empty())
        .map(PathBuf::from)
        .unwrap_or_else(|| default_service_root(home_dir));
    let mut config = NativeLinuxOciMigrationConfig::new(root)?;
    match (
        runtime_path.filter(|value| !value.is_empty()),
        agent_path.filter(|value| !value.is_empty()),
    ) {
        (Some(runtime), Some(agent)) => {
            config = config.with_artifacts(PathBuf::from(runtime), PathBuf::from(agent))?;
        }
        (None, None) => {}
        _ => {
            return Err(ExecutionManagerError::InvalidRequest(format!(
                "{OCI_RUNTIME_PATH_ENV} and {OCI_AGENT_PATH_ENV} must be set together"
            )))
        }
    }
    Ok(Some(config))
}

fn parse_windows_environment(
    mode: Option<OsString>,
    runtime_root: Option<OsString>,
    endpoint: Option<OsString>,
    home_dir: &Path,
) -> ExecutionManagerResult<Option<WindowsWhpxOciMigrationConfig>> {
    let Some(mode) = mode.filter(|value| !value.is_empty()) else {
        return Ok(None);
    };
    let mode = mode.to_str().ok_or_else(|| {
        ExecutionManagerError::InvalidRequest(format!(
            "{OCI_MIGRATION_ENV} must contain UTF-8 text"
        ))
    })?;
    match mode.trim().to_ascii_lowercase().as_str() {
        "" | "0" | "false" | "off" | "disabled" | "legacy" => return Ok(None),
        "all" | "all-via-oci" | "microvm" | "microvm-via-oci" | "whpx" => {}
        "1" | "true" | "on" | "sandbox" | "sandbox-via-oci" => {
            return Err(ExecutionManagerError::InvalidRequest(
                "Windows OCI qualification supports only MicroVM/WHPX routing; use all or microvm"
                    .to_string(),
            ))
        }
        value => {
            return Err(ExecutionManagerError::InvalidRequest(format!(
                "unsupported {OCI_MIGRATION_ENV} value {value:?}; expected off, microvm, or all"
            )))
        }
    }

    let runtime_root = runtime_root
        .filter(|value| !value.is_empty())
        .map(PathBuf::from)
        .unwrap_or_else(|| default_service_root(home_dir));
    let endpoint = endpoint
        .filter(|value| !value.is_empty())
        .ok_or_else(|| {
            ExecutionManagerError::InvalidRequest(format!(
            "{OCI_WHPX_ENDPOINT_ENV} must be set explicitly for the qualification-only WHPX service"
        ))
        })?
        .into_string()
        .map_err(|_| {
            ExecutionManagerError::InvalidRequest(format!(
                "{OCI_WHPX_ENDPOINT_ENV} must contain UTF-8 text"
            ))
        })?;
    WindowsWhpxOciMigrationConfig::new(runtime_root, endpoint).map(Some)
}

fn default_service_root(home_dir: &Path) -> PathBuf {
    #[cfg(target_os = "linux")]
    {
        use std::os::unix::ffi::OsStrExt as _;

        // Keep runtime.sock below the small sockaddr_un limit while separating
        // different A3S homes owned by the same UID.
        let digest = Sha256::digest(home_dir.as_os_str().as_bytes());
        // SAFETY: geteuid has no preconditions or failure result.
        let uid = unsafe { libc::geteuid() };
        std::env::temp_dir().join(format!("a3s-box-oci-{uid}-{}", hex::encode(&digest[..6])))
    }

    #[cfg(not(target_os = "linux"))]
    home_dir.join("run").join("oci-host")
}

fn validate_absolute_normalized(path: &Path, label: &str) -> ExecutionManagerResult<()> {
    if !path.is_absolute()
        || path.parent().is_none()
        || path
            .components()
            .any(|component| matches!(component, Component::CurDir | Component::ParentDir))
    {
        return Err(ExecutionManagerError::InvalidRequest(format!(
            "{label} must be an absolute normalized non-root path: {}",
            path.display()
        )));
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    fn absolute(name: &str) -> PathBuf {
        std::env::temp_dir().join(name)
    }

    #[test]
    fn environment_is_opt_in_and_rejects_unqualified_all_policy() {
        let home = absolute("a3s-oci-config-home");
        assert_eq!(
            parse_environment(None, None, None, None, &home).unwrap(),
            None
        );
        assert_eq!(
            parse_environment(Some(OsString::from("off")), None, None, None, &home).unwrap(),
            None
        );
        assert!(parse_environment(Some(OsString::from("all")), None, None, None, &home).is_err());
    }

    #[test]
    fn environment_requires_artifact_pair_and_accepts_sandbox() {
        let home = absolute("a3s-oci-config-home");
        let runtime = absolute("a3s-oci");
        let agent = absolute("a3s-oci-agent");
        assert!(parse_environment(
            Some(OsString::from("sandbox")),
            None,
            Some(runtime.clone().into_os_string()),
            None,
            &home
        )
        .is_err());
        let config = parse_environment(
            Some(OsString::from("sandbox")),
            Some(absolute("a3s-oci-root").into_os_string()),
            Some(runtime.clone().into_os_string()),
            Some(agent.clone().into_os_string()),
            &home,
        )
        .unwrap()
        .unwrap();
        assert_eq!(config.runtime_path(), Some(runtime.as_path()));
        assert_eq!(config.agent_path(), Some(agent.as_path()));
    }

    #[test]
    fn windows_environment_requires_explicit_pipe_and_accepts_microvm() {
        let home = absolute("a3s-oci-config-home");
        assert!(
            parse_windows_environment(Some(OsString::from("all")), None, None, &home,).is_err()
        );

        let config = parse_windows_environment(
            Some(OsString::from("microvm")),
            Some(absolute("a3s-oci-whpx-root").into_os_string()),
            Some(OsString::from(DEFAULT_OCI_WHPX_ENDPOINT)),
            &home,
        )
        .unwrap()
        .unwrap();
        assert_eq!(
            config.endpoint(),
            &crate::local_execution::OciRuntimeEndpoint::windows_named_pipe(
                DEFAULT_OCI_WHPX_ENDPOINT,
            )
            .unwrap()
        );
    }
}