mod capture;
mod git;
mod lock;
mod paths;
mod restore;
mod retention;
mod session;
#[cfg(test)]
mod tests;
use std::fmt::Write as _;
use std::path::{Path, PathBuf};
use anyhow::{Context as _, Result, bail};
use serde::{Deserialize, Serialize};
use sha2::{Digest as _, Sha256};
use crate::home::{Home, write_private};
pub use capture::{Limits, Taken, relative};
use git::ShadowGit;
pub use lock::WAIT as LOCK_WAIT;
pub use restore::{ChangeKind, Plan};
pub use retention::Retention;
pub use session::{RunSnapshots, denied, said};
pub const DIR: &str = "snapshots";
const REF_PREFIX: &str = "refs/openmoat/snapshots/";
const PROJECT_FILE: &str = "openmoat-project";
const META_VERSION: u32 = 1;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Reason {
RunStart,
RunExit,
Manual,
PreRestore,
}
impl Reason {
pub fn words(self) -> &'static str {
match self {
Self::RunStart => "before the agent started",
Self::RunExit => "after the agent exited",
Self::Manual => "taken by hand",
Self::PreRestore => "before a restore",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum LeftOut {
Denied,
TooLarge,
NestedRepository,
NotAFile,
NameNotUtf8,
StateFolder,
}
impl LeftOut {
pub fn words(self) -> &'static str {
match self {
Self::Denied => "a deny rule names it",
Self::TooLarge => "larger than the size limit",
Self::NestedRepository => "a git repository of its own",
Self::NotAFile => "not a file or a link",
Self::NameNotUtf8 => "its name is not UTF-8",
Self::StateFolder => "inside .git or .moat",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Omitted {
pub path: String,
pub why: LeftOut,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Meta {
pub version: u32,
pub reason: Reason,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub session: Option<String>,
pub files: usize,
pub bytes: u64,
pub left_out: Vec<Omitted>,
pub left_out_total: usize,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub ignored: Vec<String>,
#[serde(default)]
pub ignored_total: usize,
}
impl Meta {
pub fn lists_all_left_out(&self) -> bool {
self.left_out.len() == self.left_out_total
}
pub fn may_have_ignored(&self, path: &str) -> bool {
if self.ignored.len() != self.ignored_total {
return true;
}
let listed = |p: &str| self.ignored.binary_search_by(|i| i.as_str().cmp(p)).is_ok();
listed(path) || path.match_indices('/').any(|(at, _)| listed(&path[..=at]))
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Listed {
pub id: String,
#[serde(skip)]
pub commit: String,
#[serde(skip)]
pub tree: String,
#[serde(skip)]
pub refname: String,
pub ts_ms: i64,
pub meta: Meta,
}
#[derive(Debug, Clone)]
pub struct Store {
dir: PathBuf,
project: PathBuf,
git: ShadowGit,
}
impl Store {
pub fn open(home: &Home, project: &Path) -> Result<Self> {
let store = Self::at(home, project)?;
if !store.dir.join("HEAD").is_file() {
write_private(
&store.dir.join(PROJECT_FILE),
format!("{}\n", store.project.display()).as_bytes(),
)?;
store.git.init()?;
}
Ok(store)
}
pub fn existing(home: &Home, project: &Path) -> Result<Option<Self>> {
let store = Self::at(home, project)?;
Ok(store.dir.join("HEAD").is_file().then_some(store))
}
fn at(home: &Home, project: &Path) -> Result<Self> {
let project = std::fs::canonicalize(project)
.with_context(|| format!("resolving the project {}", project.display()))?;
let program = crate::environment::program(home, "git").context(
"no git with an absolute path is in the search path `openmoat init` recorded; \
snapshots need git",
)?;
let dir = home.root().join(DIR).join(store_name(&project));
let git = ShadowGit::new(program, dir.clone(), project.clone());
Ok(Self { dir, project, git })
}
pub fn project(&self) -> &Path {
&self.project
}
pub fn list(&self) -> Result<Vec<Listed>> {
let text = self.git.output(
&[
"for-each-ref",
"--sort=refname",
"--format=%(refname)%00%(objectname)%00%(tree)%00%(committerdate:unix)%00%(contents)%00",
REF_PREFIX,
],
None,
)?;
parse_listing(&String::from_utf8_lossy(&text))
}
pub fn find(&self, id: &str) -> Result<Listed> {
if id.len() < 4 || !id.bytes().all(|b| b.is_ascii_hexdigit()) {
bail!("`{id}` is not a snapshot id: `openmoat snapshot --list` shows them");
}
let id = id.to_ascii_lowercase();
let mut found = self
.list()?
.into_iter()
.filter(|s| s.commit.starts_with(&id));
match (found.next(), found.next()) {
(Some(one), None) => Ok(one),
(Some(_), Some(_)) => bail!("`{id}` names more than one snapshot; give more of it"),
(None, _) => bail!("no snapshot `{id}` of this project: `openmoat snapshot --list`"),
}
}
}
fn store_name(real: &Path) -> String {
let digest = Sha256::digest(crate::context::path_string(real).as_bytes());
digest[..16].iter().fold(String::new(), |mut hex, b| {
let _ = write!(hex, "{b:02x}");
hex
})
}
fn parse_listing(text: &str) -> Result<Vec<Listed>> {
let mut fields = text.split('\0');
let mut listed = Vec::new();
while let Some(refname) = fields.next().map(str::trim) {
if refname.is_empty() {
break;
}
let (Some(commit), Some(tree), Some(time), Some(message)) =
(fields.next(), fields.next(), fields.next(), fields.next())
else {
bail!("the snapshot store lists {refname} incompletely");
};
let meta: Meta = serde_json::from_str(message.trim())
.with_context(|| format!("reading snapshot {refname}"))?;
if meta.version != META_VERSION {
bail!(
"snapshot {refname} has version {}; this build reads {META_VERSION}",
meta.version
);
}
let secs: i64 = time.trim().parse().unwrap_or_default();
listed.push(Listed {
id: commit.chars().take(12).collect(),
commit: commit.to_owned(),
tree: tree.to_owned(),
refname: refname.to_owned(),
ts_ms: secs.saturating_mul(1000),
meta,
});
}
Ok(listed)
}
pub fn record(home: &Home, session: &str, tool: &str, rule: &str, reason: String) {
let decision = openmoat_core::Decision::single(openmoat_core::Verdict::Allow, rule, reason);
let written = crate::audit_key::open_existing(home).and_then(|store| {
Ok(store.record(&openmoat_audit::NewEvent {
host: "openmoat",
session_id: session,
call_id: None,
cwd: None,
tool,
action: None,
decision: &decision,
latency_us: 0,
})?)
});
if let Err(error) = written {
eprintln!("openmoat: the audit log did not record the {tool}: {error:#}");
}
}