agentd 0.1.2

Agent daemon for secure capability execution with pluggable isolation backends
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
//! Isolation backend implementations
//!
//! This module contains concrete implementations of the `IsolationBackend` trait:
//! - `LinuxNativeBackend`: Uses Landlock LSM for filesystem access control (no mount isolation)
//! - `ContainerBackend`: Uses mount namespaces for true filesystem isolation
//! - `FirecrackerBackend`: Uses Firecracker microVMs for full hardware isolation
//! - `HostDirectBackend`: No isolation, just policy guards (workstation mode)
//!
//! Future implementations:
//! - `MacosNativeBackend`: Uses sandbox-exec (seatbelt)

#[cfg(target_os = "linux")]
pub mod container;
#[cfg(target_os = "linux")]
pub mod firecracker;
pub mod gondolin;
pub mod host_direct;
pub mod linux;

#[cfg(target_os = "linux")]
pub use container::ContainerBackend;
#[cfg(target_os = "linux")]
pub use firecracker::{FirecrackerBackend, FirecrackerConfig};
pub use gondolin::GondolinBackend;
pub use host_direct::HostDirectBackend;
pub use linux::LinuxNativeBackend;

use crate::core::isolation::{BackendCapabilities, IsolationBackend};
use anyhow::Context;
use once_cell::sync::Lazy;
use std::collections::{HashMap, HashSet};
use std::path::Path;
use std::sync::{Arc, RwLock};

type BackendFactory = dyn Fn(&Path) -> anyhow::Result<Arc<dyn IsolationBackend>> + Send + Sync;

#[derive(Clone)]
struct RegisteredBackend {
    factory: Arc<BackendFactory>,
}

#[derive(Default)]
struct BackendRegistry {
    backends: HashMap<String, RegisteredBackend>,
    aliases: HashMap<String, String>,
    insertion_order: Vec<String>,
}

impl BackendRegistry {
    fn register_backend(
        &mut self,
        canonical_name: &str,
        aliases: &[&str],
        factory: Arc<BackendFactory>,
    ) -> anyhow::Result<()> {
        let canonical = normalize_backend_name(canonical_name);
        if canonical.is_empty() {
            anyhow::bail!("Isolation backend canonical name cannot be empty");
        }

        if self.backends.contains_key(&canonical) {
            anyhow::bail!("Isolation backend '{}' is already registered", canonical);
        }

        let mut alias_set = HashSet::new();
        alias_set.insert(canonical.clone());
        alias_set.insert(canonical.replace('-', "_"));

        for alias in aliases {
            let normalized = normalize_backend_name(alias);
            if normalized.is_empty() {
                continue;
            }

            alias_set.insert(normalized.clone());
            alias_set.insert(normalized.replace('-', "_"));
        }

        for alias in &alias_set {
            if let Some(existing) = self.aliases.get(alias) {
                anyhow::bail!(
                    "Isolation backend alias '{}' is already registered for '{}'",
                    alias,
                    existing
                );
            }
        }

        self.backends
            .insert(canonical.clone(), RegisteredBackend { factory });
        self.insertion_order.push(canonical.clone());

        for alias in alias_set {
            self.aliases.insert(alias, canonical.clone());
        }

        Ok(())
    }

    fn resolve_canonical(&self, name: &str) -> Option<String> {
        let normalized = normalize_backend_name(name);
        if normalized.is_empty() {
            return None;
        }

        self.aliases.get(&normalized).cloned()
    }

    fn backend_factory(&self, name: &str) -> Option<Arc<BackendFactory>> {
        let canonical = self.resolve_canonical(name)?;
        self.backends
            .get(&canonical)
            .map(|entry| entry.factory.clone())
    }

    fn available_backend_names(&self) -> Vec<String> {
        self.insertion_order.clone()
    }
}

fn normalize_backend_name(name: &str) -> String {
    name.trim().to_ascii_lowercase().replace('_', "-")
}

fn build_registry() -> BackendRegistry {
    let mut registry = BackendRegistry::default();
    register_builtin_backends(&mut registry)
        .expect("builtin isolation backend registration failed");
    registry
}

static BACKEND_REGISTRY: Lazy<RwLock<BackendRegistry>> =
    Lazy::new(|| RwLock::new(build_registry()));

fn register_builtin_backends(registry: &mut BackendRegistry) -> anyhow::Result<()> {
    registry.register_backend(
        "host-direct",
        &["none", "host", "workstation", "host_direct"],
        Arc::new(|work_root| Ok(Arc::new(HostDirectBackend::new(work_root)))),
    )?;

    registry.register_backend(
        "gondolin",
        &[],
        Arc::new(|work_root| Ok(Arc::new(GondolinBackend::new(work_root)))),
    )?;

    registry.register_backend(
        "linux-native",
        &["linux", "native", "landlock", "linux_native"],
        Arc::new(|work_root| {
            #[cfg(target_os = "linux")]
            {
                Ok(Arc::new(LinuxNativeBackend::new(work_root)?))
            }
            #[cfg(not(target_os = "linux"))]
            {
                anyhow::bail!("LinuxNativeBackend is only available on Linux")
            }
        }),
    )?;

    registry.register_backend(
        "container",
        &["namespace", "mount-ns", "mount_ns"],
        Arc::new(|work_root| {
            #[cfg(target_os = "linux")]
            {
                Ok(Arc::new(ContainerBackend::new(work_root, None)?))
            }
            #[cfg(not(target_os = "linux"))]
            {
                anyhow::bail!("ContainerBackend is only available on Linux")
            }
        }),
    )?;

    registry.register_backend(
        "firecracker",
        &["microvm", "vm"],
        Arc::new(|work_root| {
            #[cfg(target_os = "linux")]
            {
                let config = FirecrackerConfig {
                    work_root: work_root.to_path_buf(),
                    ..Default::default()
                };
                Ok(Arc::new(FirecrackerBackend::new(config)?))
            }
            #[cfg(not(target_os = "linux"))]
            {
                anyhow::bail!("FirecrackerBackend is only available on Linux")
            }
        }),
    )?;

    Ok(())
}

/// Register a custom isolation backend factory.
///
/// This allows modular provider integration (for example, a Gondolin-backed
/// provider) without modifying backend selection code paths.
pub fn register_backend_factory<F>(
    canonical_name: &str,
    aliases: &[&str],
    factory: F,
) -> anyhow::Result<()>
where
    F: Fn(&Path) -> anyhow::Result<Arc<dyn IsolationBackend>> + Send + Sync + 'static,
{
    let mut registry = BACKEND_REGISTRY.write().expect("backend registry poisoned");
    registry.register_backend(canonical_name, aliases, Arc::new(factory))
}

/// Resolve an alias to its canonical backend name.
pub fn canonical_backend_name(name: &str) -> Option<String> {
    let registry = BACKEND_REGISTRY.read().expect("backend registry poisoned");
    registry.resolve_canonical(name)
}

/// List registered canonical backend names.
pub fn available_backends() -> Vec<String> {
    let registry = BACKEND_REGISTRY.read().expect("backend registry poisoned");
    registry.available_backend_names()
}

fn backend_score(caps: &BackendCapabilities) -> u8 {
    let mut score = 0u8;
    if caps.filesystem_isolation {
        score += 4;
    }
    if caps.process_isolation {
        score += 2;
    }
    if caps.network_isolation {
        score += 2;
    }
    if caps.syscall_filtering {
        score += 2;
    }
    if caps.resource_limits {
        score += 1;
    }
    score
}

/// Probe the system and return the best available isolation backend
pub async fn detect_best_backend(work_root: &Path) -> Arc<dyn IsolationBackend> {
    let mut best_candidate: Option<(u8, Arc<dyn IsolationBackend>)> = None;

    for name in available_backends() {
        let backend = match create_backend(&name, work_root) {
            Ok(backend) => backend,
            Err(err) => {
                tracing::debug!(
                    backend = %name,
                    error = %err,
                    "Skipping unavailable backend candidate"
                );
                continue;
            }
        };

        match backend.probe().await {
            Ok(caps) => {
                let score = backend_score(&caps);
                tracing::debug!(
                    backend = %caps.name,
                    score,
                    filesystem_isolation = caps.filesystem_isolation,
                    network_isolation = caps.network_isolation,
                    process_isolation = caps.process_isolation,
                    syscall_filtering = caps.syscall_filtering,
                    "Scored backend candidate"
                );

                let should_replace = best_candidate
                    .as_ref()
                    .map(|(existing_score, _)| score > *existing_score)
                    .unwrap_or(true);

                if should_replace {
                    best_candidate = Some((score, backend));
                }
            }
            Err(err) => {
                tracing::warn!(
                    backend = %name,
                    error = %err,
                    "Backend probe failed; skipping candidate"
                );
            }
        }
    }

    if let Some((score, backend)) = best_candidate {
        tracing::info!(backend = %backend.name(), score, "Using detected best isolation backend");
        return backend;
    }

    tracing::warn!(
        "No registered isolation backend could be initialized; falling back to host-direct"
    );
    Arc::new(HostDirectBackend::new(work_root))
}

/// Create a backend by name
pub fn create_backend(name: &str, work_root: &Path) -> anyhow::Result<Arc<dyn IsolationBackend>> {
    let factory = {
        let registry = BACKEND_REGISTRY.read().expect("backend registry poisoned");
        registry.backend_factory(name).ok_or_else(|| {
            let available = registry.available_backend_names().join(", ");
            anyhow::anyhow!(
                "Unknown isolation backend '{}'. Available backends: {}",
                name,
                available
            )
        })?
    };

    factory(work_root).with_context(|| format!("Failed to initialize isolation backend '{}'", name))
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;
    use uuid::Uuid;

    #[test]
    fn test_create_backend_host_direct() {
        let temp_dir = TempDir::new().unwrap();

        // All host-direct aliases should work
        let aliases = ["none", "host", "host-direct", "host_direct", "workstation"];
        for alias in aliases {
            let backend = create_backend(alias, temp_dir.path());
            assert!(backend.is_ok(), "Failed for alias: {}", alias);
            assert_eq!(backend.unwrap().name(), "host-direct");
        }
    }

    #[test]
    fn test_create_backend_unknown() {
        let temp_dir = TempDir::new().unwrap();
        let result = create_backend("unknown-backend", temp_dir.path());
        assert!(result.is_err());
        let err = result.err().unwrap().to_string();
        assert!(err.contains("Unknown isolation backend"));
        assert!(err.contains("host-direct"));
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn test_create_backend_linux() {
        let temp_dir = TempDir::new().unwrap();

        let aliases = ["linux", "native", "linux-native", "linux_native"];
        for alias in aliases {
            let result = create_backend(alias, temp_dir.path());
            // On Linux, this should succeed (or fail only due to permissions)
            // We just check it doesn't panic
            let _ = result;
        }
    }

    #[cfg(not(target_os = "linux"))]
    #[test]
    fn test_create_backend_linux_not_available() {
        let temp_dir = TempDir::new().unwrap();

        let aliases = ["linux", "native", "linux-native", "linux_native"];
        for alias in aliases {
            let result = create_backend(alias, temp_dir.path());
            assert!(result.is_err());
            let err = result.err().unwrap().to_string();
            assert!(err.contains("only available on Linux"));
        }
    }

    #[test]
    fn test_canonical_backend_name_resolves_aliases() {
        assert_eq!(
            canonical_backend_name("host_direct").as_deref(),
            Some("host-direct")
        );
        assert_eq!(
            canonical_backend_name("LANDLOCK").as_deref(),
            Some("linux-native")
        );
    }

    #[test]
    fn test_available_backends_contains_core_backends() {
        let names = available_backends();
        assert!(names.contains(&"host-direct".to_string()));
        assert!(names.contains(&"linux-native".to_string()));
        assert!(names.contains(&"container".to_string()));
        assert!(names.contains(&"firecracker".to_string()));
    }

    #[test]
    fn test_register_backend_factory_allows_custom_provider() {
        let temp_dir = TempDir::new().unwrap();
        let backend_name = format!("test-provider-{}", Uuid::new_v4().simple());
        let alias = format!("{}-alias", backend_name);

        register_backend_factory(&backend_name, &[&alias], |work_root| {
            Ok(Arc::new(HostDirectBackend::new(work_root)))
        })
        .expect("custom backend registration should succeed");

        let canonical = canonical_backend_name(&alias);
        assert_eq!(canonical.as_deref(), Some(backend_name.as_str()));

        let backend =
            create_backend(&alias, temp_dir.path()).expect("custom backend should create");
        assert_eq!(backend.name(), "host-direct");
    }

    #[test]
    fn test_register_backend_factory_rejects_alias_collision() {
        let backend_name = format!("test-collision-{}", Uuid::new_v4().simple());
        let result = register_backend_factory(&backend_name, &["host"], |work_root| {
            Ok(Arc::new(HostDirectBackend::new(work_root)))
        });
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("already registered"));
    }

    #[tokio::test]
    async fn test_detect_best_backend() {
        let temp_dir = TempDir::new().unwrap();
        let backend = detect_best_backend(temp_dir.path()).await;

        // Should return a valid backend
        let name = backend.name();
        assert!(
            name == "host-direct"
                || name == "linux-native"
                || name == "container"
                || name == "firecracker",
            "Unexpected backend: {}",
            name
        );
    }

    #[tokio::test]
    async fn test_backend_probe() {
        let temp_dir = TempDir::new().unwrap();
        let backend = create_backend("host-direct", temp_dir.path()).unwrap();

        let caps = backend.probe().await.unwrap();
        assert_eq!(caps.name, "host-direct");
        // Host-direct has no kernel isolation
        assert!(!caps.filesystem_isolation);
        assert!(!caps.network_isolation);
        assert!(!caps.process_isolation);
        assert!(!caps.syscall_filtering);
        // But supports persistent sandboxes
        assert!(caps.persistent_sandboxes);
    }
}