use std::fmt::Debug;
use std::path::Path;
use anyhow::Result;
use openmoat_core::Secret;
use openmoat_core::ir::Enforcement;
use openmoat_hosts::Host;
use serde_json::Value;
use toml_edit::DocumentMut;
use super::install::Rendered;
use super::{Plan, Report};
use crate::home::Home;
use crate::integrity::Lock;
#[derive(Clone, Copy)]
pub enum Undo {
WithHooks(fn(&mut Value, &[String]) -> bool),
Toml(fn(&mut DocumentMut) -> bool),
Json(fn(&mut Value) -> bool),
}
#[derive(Clone, Copy)]
pub enum Pin {
WithHooks,
Whole(fn(&Path) -> Result<bool>),
Part {
carries: fn(&Path) -> Result<bool>,
digest: fn(&Path) -> Result<String>,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MaskLevel {
Masked,
NotWritten,
Unmasked,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SecretMask<'a> {
pub secret: &'a Secret,
pub text: String,
pub level: MaskLevel,
}
pub struct Input<'a> {
pub ir: &'a Enforcement,
pub proxy_port: Option<u16>,
pub homes: &'a [String],
pub binary_paths: &'a [String],
pub linux: bool,
}
pub trait SandboxBackend: Sync {
fn generate(&self, input: &Input<'_>, settings: &Path) -> Result<Box<dyn Output>>;
fn undo(&self) -> Undo;
fn pin(&self) -> Pin;
fn masks_secrets(&self) -> bool {
false
}
}
pub trait Output: Debug {
fn render(&self, home: &Home, host: Host, current: &str) -> Result<Option<Rendered>>;
fn report(&self) -> &Report;
fn shown(&self) -> Result<String>;
fn problems(
&self,
home: &Home,
path: &Path,
plan: &Plan,
lock: Option<&Lock>,
) -> Result<Vec<String>>;
fn writable_dirs(&self) -> Vec<String>;
fn secret_masks<'a>(&self, _settings: &Value, _secrets: &'a [Secret]) -> Vec<SecretMask<'a>> {
Vec::new()
}
}
pub(super) fn strings(value: Option<&Value>) -> Vec<String> {
value
.and_then(Value::as_array)
.into_iter()
.flatten()
.filter_map(|v| v.as_str().map(str::to_owned))
.collect()
}
pub(super) const OUT_OF_DATE: &str = "differs from the policy; run `openmoat sandbox sync`";
pub(super) const NOT_PINNED: &str = "not pinned by the lock; run `openmoat sandbox sync`";