Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.
Rust agentai-agentapplecomputer-usecomputer-use-agentcontainerizationcuadesktop-automationhacktoberfestlumemacosmanusoperatorswiftvirtualizationvirtualization-frameworkwindowswindows-sandbox
fix(relay): audit log in its own file, stale temp cleanup, sessions survive restarts (#4939)
Francesco Bonaccicfc95fd70369
diff --git a/libs/cua-spacesd/crates/cua-relay/src/api.rs b/libs/cua-spacesd/crates/cua-relay/src/api.rsindex b7fe092cbf..37959cb25f 100644--- a/libs/cua-spacesd/crates/cua-relay/src/api.rs+++ b/libs/cua-spacesd/crates/cua-relay/src/api.rs@@ -331,6 +331,9 @@ async fn info(State(relay): State<Relay>, headers: HeaderMap) -> Response {"issuer": relay.config.oidc.as_ref().map(|o| o.config().issuer.clone()),"account_auth": relay.config.oidc.is_some(),"static_tokens": !relay.config.tokens.is_empty(),+ // False once a write of the relay's state files fails, until one+ // succeeds again (a full or read-only state volume).+ "state_writable": relay.devices.writes_ok() && relay.directory.writes_ok(),})).into_response()}diff --git a/libs/cua-spacesd/crates/cua-relay/src/assertion.rs b/libs/cua-spacesd/crates/cua-relay/src/assertion.rsindex 5e06a89770..46bdc65659 100644--- a/libs/cua-spacesd/crates/cua-relay/src/assertion.rs+++ b/libs/cua-spacesd/crates/cua-relay/src/assertion.rs@@ -152,15 +152,17 @@ impl RelayKey {/// symlink) that is renamed over `path`, so `path` holds either the old or/// the new content, never a mix.pub fn write_private(path: &std::path::Path, bytes: &[u8]) -> std::io::Result<()> {+ write_private_with(path, |w| w.write_all(bytes))+}++/// [`write_private`], streaming: `fill` writes the content through a+/// buffer, so a large file is never held in memory whole.+pub fn write_private_with(+ path: &std::path::Path,+ fill: impl FnOnce(&mut dyn std::io::Write) -> std::io::Result<()>,+) -> std::io::Result<()> {use std::io::Write as _;- let dir = match path.parent() {- Some(d) if !d.as_os_str().is_empty() => d,- _ => std::path::Path::new("."),- };- let name = path- .file_name()- .map(|n| n.to_string_lossy().into_owned())- .unwrap_or_else(|| "file".into());+ let (dir, name) = temp_parts(path);let mut last = None;for _ in 0..16 {let tmp = dir.join(format!(".{name}.{}.tmp", uuid::Uuid::new_v4().simple()));@@ -171,7 +173,7 @@ pub fn write_private(path: &std::path::Path, bytes: &[u8]) -> std::io::Result<()use std::os::unix::fs::OpenOptionsExt as _;options.mode(0o600);}- let mut file = match options.open(&tmp) {+ let file = match options.open(&tmp) {Ok(f) => f,Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {last = Some(e);@@ -179,8 +181,11 @@ pub fn write_private(path: &std::path::Path, bytes: &[u8]) -> std::io::Result<()}Err(e) => return Err(e),};- let written = file.write_all(bytes).and_then(|()| file.sync_all());- drop(file);+ let mut out = std::io::BufWriter::new(file);+ let written = fill(&mut out)+ .and_then(|()| out.flush())+ .and_then(|()| out.get_ref().sync_all());+ drop(out);if let Err(e) = written.and_then(|()| std::fs::rename(&tmp, path)) {let _ = std::fs::remove_file(&tmp);return Err(e);@@ -190,6 +195,58 @@ pub fn write_private(path: &std::path::Path, bytes: &[u8]) -> std::io::Result<()Err(last.unwrap_or_else(|| std::io::Error::other("no free temp name")))}+fn temp_parts(path: &std::path::Path) -> (&std::path::Path, String) {+ let dir = match path.parent() {+ Some(d) if !d.as_os_str().is_empty() => d,+ _ => std::path::Path::new("."),+ };+ let name = path+ .file_name()+ .map(|n| n.to_string_lossy().into_owned())+ .unwrap_or_else(|| "file".into());+ (dir, name)+}++/// Deletes the temp files [`write_private`] leaves next to `path` when the+/// process dies mid-write (an OOM kill): `.<name>.<uuid>.tmp`, which also+/// covers sibling files named `<name>.<suffix>`. Each can be as large as+/// the file itself, so a few of them fill a small volume. Call it at+/// startup, before this process writes `path` (one writer per state file).+/// Returns how many files and bytes it removed.+pub fn remove_stale_temps(path: &std::path::Path) -> std::io::Result<(usize, u64)> {+ let (dir, name) = temp_parts(path);+ let prefix = format!(".{name}.");+ let entries = match std::fs::read_dir(dir) {+ Ok(entries) => entries,+ Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok((0, 0)),+ Err(e) => return Err(e),+ };+ let (mut files, mut bytes) = (0, 0);+ for entry in entries {+ let entry = entry?;+ let file_name = entry.file_name().to_string_lossy().into_owned();+ if !file_name.starts_with(&prefix) || !file_name.ends_with(".tmp") {+ continue;+ }+ let meta = entry.metadata()?;+ if !meta.is_file() {+ continue;+ }+ std::fs::remove_file(entry.path())?;+ files += 1;+ bytes += meta.len();+ }+ if files > 0 {+ tracing::warn!(+ path = %path.display(),+ files,+ bytes,+ "removed temp files left by an interrupted write"+ );+ }+ Ok((files, bytes))+}+/// The Ed25519 public keys (`x`, base64url) in a JWKS document.pub fn ed25519_keys(jwks: &str) -> Result<Vec<String>, String> {let set: JwkSet = serde_json::from_str(jwks).map_err(|e| format!("relay JWKS: {e}"))?;@@ -313,6 +370,46 @@ impl TrustedKeys {#[cfg(test)]mod tests {+ #[test]+ fn stale_temps_of_the_file_and_its_siblings_are_removed() {+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("devices.json");+ std::fs::write(&path, "live").unwrap();+ for stale in [+ ".devices.json.0123abcd.tmp",+ ".devices.json.audit.jsonl.4567ef.tmp",+ ] {+ std::fs::write(dir.path().join(stale), "partial").unwrap();+ }+ for kept in [+ ".machines.json.89ab.tmp",+ "devices.json.audit-key",+ ".devices.json",+ ] {+ std::fs::write(dir.path().join(kept), "keep").unwrap();+ }+ assert_eq!(remove_stale_temps(&path).unwrap(), (2, 14));+ let mut left: Vec<String> = std::fs::read_dir(dir.path())+ .unwrap()+ .map(|e| e.unwrap().file_name().to_string_lossy().into_owned())+ .collect();+ left.sort();+ assert_eq!(+ left,+ [+ ".devices.json",+ ".machines.json.89ab.tmp",+ "devices.json",+ "devices.json.audit-key"+ ]+ );+ assert_eq!(remove_stale_temps(&path).unwrap(), (0, 0));+ assert_eq!(+ remove_stale_temps(&dir.path().join("missing/devices.json")).unwrap(),+ (0, 0)+ );+ }+#[cfg(unix)]#[test]fn private_writes_are_atomic_and_never_follow_a_planted_temp() {diff --git a/libs/cua-spacesd/crates/cua-relay/src/devices.rs b/libs/cua-spacesd/crates/cua-relay/src/devices.rsindex 1a4a30ca4f..105751f0cb 100644--- a/libs/cua-spacesd/crates/cua-relay/src/devices.rs+++ b/libs/cua-spacesd/crates/cua-relay/src/devices.rs@@ -71,16 +71,18 @@//! machine token and is never enrolled as a client.//!//! Every enrollment change, device session and machine access is recorded-//! in the account's audit log (bounded, persisted with the devices).+//! in the account's audit log (bounded per account, appended to its own+//! file next to the devices file; see [`AuditLog`]).use std::collections::{BTreeMap, HashMap};-use std::path::PathBuf;+use std::io::{BufRead as _, Write as _};+use std::path::{Path, PathBuf};use std::sync::Mutex;use base64::Engine as _;use serde::{Deserialize, Serialize};-use crate::assertion::{now_secs, write_private};+use crate::assertion::{now_secs, remove_stale_temps, write_private, write_private_with};/// Header carrying the device session token.pub const DEVICE_SESSION_HEADER: &str = "x-cua-device-session";@@ -385,8 +387,8 @@ fn chain_mac(key: &[u8; 32], hash: &str) -> String {hex::encode(ring::hmac::sign(&key, hash.as_bytes()).as_ref())}-/// Appends `event` to `log`, hash-chained from its current tail.-fn chain_append(log: &mut Vec<AuditEntry>, event: AuditEvent, audit_key: &[u8; 32]) {+/// The entry that appends `event` to `log`, hash-chained from its tail.+fn chain_next(log: &[AuditEntry], event: AuditEvent, audit_key: &[u8; 32]) -> AuditEntry {let prev = log.last().map(|e| e.hash.clone())@@ -394,13 +396,19 @@ fn chain_append(log: &mut Vec<AuditEntry>, event: AuditEvent, audit_key: &[u8; 3let seq = log.last().map(|e| e.seq).unwrap_or(0) + 1;let hash = chain_hash(&prev, seq, &event);let mac = chain_mac(audit_key, &hash);- log.push(AuditEntry {+ AuditEntry {seq,event,prev,hash,mac,- });+ }+}++/// Appends `event` to `log`, hash-chained from its current tail.+fn chain_append(log: &mut Vec<AuditEntry>, event: AuditEvent, audit_key: &[u8; 32]) {+ let entry = chain_next(log, event, audit_key);+ log.push(entry);}/// Why chain verification failed: the entry's sequence number and why.@@ -456,9 +464,12 @@ pub fn verify_audit_chain(log: &[AuditEntry], audit_key: &[u8; 32]) -> Result<()/// sentinel anywhere in an account's log is enough to rebuild that whole/// log rather than require it to already be entry-by-entry consistent./// Returns whether anything was migrated (the caller persists the result).-fn migrate_legacy_audit(state: &mut State, audit_key: &[u8; 32]) -> bool {+fn migrate_legacy_audit(+ audit: &mut BTreeMap<String, Vec<AuditEntry>>,+ audit_key: &[u8; 32],+) -> bool {let mut migrated = false;- for log in state.audit.values_mut() {+ for log in audit.values_mut() {if log.iter().any(|entry| entry.seq == 0) {let events: Vec<AuditEvent> =std::mem::take(log).into_iter().map(|e| e.event).collect();@@ -471,9 +482,23 @@ fn migrate_legacy_audit(state: &mut State, audit_key: &[u8; 32]) -> bool {migrated}-#[derive(Debug, Default, Clone, Serialize, Deserialize)]+/// The devices file: devices only. Audit entries live in [`AuditLog`]'s+/// own file, so an audit event never rewrites this one.+#[derive(Debug, Default, Clone, Serialize)]struct State {/// Start of the migration grace period (first start with devices).+ grace_started_at: u64,+ devices: BTreeMap<String, DeviceRecord>,+ /// Audit events of the change being applied, appended to the audit log+ /// once the change is persisted ([`DeviceStore::mutate`]).+ #[serde(skip)]+ pending_audit: Vec<(String, AuditEvent)>,+}++/// The devices file as read: earlier relays also kept every account's+/// audit log in it, which [`DeviceStore::open`] moves to the audit log file.+#[derive(Debug, Default, Deserialize)]+struct StoredState {#[serde(default)]grace_started_at: u64,#[serde(default)]@@ -482,7 +507,193 @@ struct State {audit: BTreeMap<String, Vec<AuditEntry>>,}-#[derive(Debug, Clone)]+/// The live device sessions are kept in the devices file's path plus this+/// suffix, so a relay restart does not cut every client off until its+/// session expires.+pub const SESSIONS_FILE_SUFFIX: &str = ".sessions.json";++/// The audit log file is the devices file's path plus this suffix.+pub const AUDIT_FILE_SUFFIX: &str = ".audit.jsonl";++/// Lines the audit log file may hold beyond the retained entries (trimmed+/// ones) before it is rewritten with only the retained ones.+const AUDIT_COMPACT_SLACK: usize = 50_000;++/// One line of the audit log file.+#[derive(Serialize)]+struct AuditLineOut<'a> {+ account: &'a str,+ #[serde(flatten)]+ entry: &'a AuditEntry,+}++/// One line of the audit log file, read back.+#[derive(Deserialize)]+struct AuditLineIn {+ account: String,+ #[serde(flatten)]+ entry: AuditEntry,+}++/// Every account's audit log: the last [`AUDIT_LIMIT`] entries per account+/// in memory and, for a persisted store, an append-only file of one JSON+/// line per entry (`{"account": ..., <entry>}`). Recording an event appends+/// one line; the file is rewritten (atomically, streamed) only when trimmed+/// entries make up more than [`AUDIT_COMPACT_SLACK`] of its lines, or at+/// startup after a torn final line.+#[derive(Debug, Default)]+struct AuditLog {+ path: Option<PathBuf>,+ /// Append handle, opened on first use.+ file: Option<std::fs::File>,+ logs: BTreeMap<String, Vec<AuditEntry>>,+ /// Entries in `logs`.+ retained: usize,+ /// Lines in the file.+ lines: usize,+ compact_slack: usize,+}++impl AuditLog {+ fn path_for(devices_path: &Path) -> PathBuf {+ PathBuf::from(format!("{}{AUDIT_FILE_SUFFIX}", devices_path.display()))+ }++ fn in_memory() -> Self {+ Self {+ compact_slack: AUDIT_COMPACT_SLACK,+ ..Self::default()+ }+ }++ /// Loads the log file at `path` (empty when missing). A line that does+ /// not parse (a write torn by a crash) is dropped and the file is+ /// rewritten without it.+ fn load(path: PathBuf) -> std::io::Result<Self> {+ let mut log = Self {+ path: Some(path),+ ..Self::in_memory()+ };+ let path = log.path.clone().expect("path");+ let file = match std::fs::File::open(&path) {+ Ok(f) => f,+ Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(log),+ Err(e) => return Err(e),+ };+ let mut reader = std::io::BufReader::new(file);+ let mut line = Vec::new();+ let mut dropped = 0usize;+ loop {+ line.clear();+ if reader.read_until(b'\n', &mut line)? == 0 {+ break;+ }+ let complete = line.last() == Some(&b'\n');+ let body = line.strip_suffix(b"\n").unwrap_or(&line);+ if body.is_empty() {+ continue;+ }+ log.lines += 1;+ match serde_json::from_slice::<AuditLineIn>(body) {+ Ok(parsed) if complete => log.insert(parsed.account, parsed.entry),+ _ => dropped += 1,+ }+ }+ if dropped > 0 {+ tracing::warn!(path = %path.display(), dropped, "dropped unreadable audit log lines");+ log.compact()?;+ }+ Ok(log)+ }++ /// Adds an already chained entry, trimming the account to+ /// [`AUDIT_LIMIT`].+ fn insert(&mut self, account: String, entry: AuditEntry) {+ let log = self.logs.entry(account).or_default();+ log.push(entry);+ self.retained += 1;+ if log.len() > AUDIT_LIMIT {+ let excess = log.len() - AUDIT_LIMIT;+ log.drain(..excess);+ self.retained -= excess;+ }+ }++ /// Chains `event` onto `account`'s log and appends it to the file. On+ /// a write error nothing is recorded (the file is cut back to where it+ /// was, so no torn line stays in it).+ fn append(+ &mut self,+ account: &str,+ event: AuditEvent,+ audit_key: &[u8; 32],+ ) -> std::io::Result<()> {+ let entry = chain_next(+ self.logs.get(account).map(Vec::as_slice).unwrap_or(&[]),+ event,+ audit_key,+ );+ if self.path.is_some() {+ let mut line = serde_json::to_vec(&AuditLineOut {+ account,+ entry: &entry,+ })?;+ line.push(b'\n');+ self.write_line(&line)?;+ self.lines += 1;+ }+ self.insert(account.to_owned(), entry);+ if self.lines > self.retained.saturating_add(self.compact_slack) {+ if let Err(e) = self.compact() {+ tracing::warn!(error = %e, "audit log not compacted");+ }+ }+ Ok(())+ }++ fn write_line(&mut self, line: &[u8]) -> std::io::Result<()> {+ let path = self.path.as_ref().expect("persisted audit log");+ if self.file.is_none() {+ let mut options = std::fs::OpenOptions::new();+ options.append(true).create(true);+ #[cfg(unix)]+ {+ use std::os::unix::fs::OpenOptionsExt as _;+ options.mode(0o600);+ }+ self.file = Some(options.open(path)?);+ }+ let file = self.file.as_mut().expect("open");+ let before = file.metadata()?.len();+ let written = file.write_all(line).and_then(|()| file.sync_data());+ if written.is_err() {+ let _ = file.set_len(before);+ }+ written+ }++ /// Rewrites the file with only the retained entries.+ fn compact(&mut self) -> std::io::Result<()> {+ let Some(path) = self.path.clone() else {+ return Ok(());+ };+ self.file = None;+ let logs = &self.logs;+ write_private_with(&path, |out| {+ for (account, entries) in logs {+ for entry in entries {+ serde_json::to_writer(&mut *out, &AuditLineOut { account, entry })?;+ out.write_all(b"\n")?;+ }+ }+ Ok(())+ })?;+ self.lines = self.retained;+ Ok(())+ }+}++#[derive(Debug, Clone, Serialize, Deserialize)]struct Session {account: String,device: String,@@ -616,7 +827,10 @@ pub struct DeviceStore {path: Option<PathBuf>,policy: DevicePolicy,state: Mutex<State>,+ /// Live sessions by the SHA-256 of their token (never the token).sessions: Mutex<HashMap<String, Session>>,+ /// Where `sessions` is persisted (`<devices>`[`SESSIONS_FILE_SUFFIX`]).+ sessions_path: Option<PathBuf>,/// Signed timestamps already used (replay guard), with their expiry.proofs: Mutex<HashMap<String, u64>>,/// Last recorded access per (account, actor, machine).@@ -625,6 +839,10 @@ pub struct DeviceStore {/// in-memory store, loaded or created (0600, next to the devices file)/// for a persisted one.audit_key: [u8; 32],+ /// Locked after `state` when both are needed.+ audit: Mutex<AuditLog>,+ /// Whether the last write (devices file or audit log) succeeded.+ writes_ok: std::sync::atomic::AtomicBool,}fn random_audit_key() -> [u8; 32] {@@ -720,6 +938,7 @@ impl DeviceStore {..State::default()},random_audit_key(),+ AuditLog::in_memory(),)}@@ -728,35 +947,87 @@ impl DeviceStore {policy: DevicePolicy,state: State,audit_key: [u8; 32],+ audit: AuditLog,) -> Self {Self {path,policy,state: Mutex::new(state),sessions: Mutex::default(),+ sessions_path: None,proofs: Mutex::default(),throttle: Mutex::default(),audit_key,+ audit: Mutex::new(audit),+ writes_ok: true.into(),}}/// Loads (or starts, recording the start of the grace period) the store- /// persisted at `path`.+ /// persisted at `path`, with its audit log next to it+ /// (`<path>`[`AUDIT_FILE_SUFFIX`]). Temp files an interrupted write left+ /// behind are removed first. An audit log still kept inside the devices+ /// file (earlier relays) is moved to the audit log file.pub fn open(path: PathBuf, policy: DevicePolicy) -> std::io::Result<Self> {- let mut state = match std::fs::read(&path) {- Ok(raw) => serde_json::from_slice::<State>(&raw)+ // Also covers the audit log's temps (`.<name>.audit.jsonl.*.tmp`).+ remove_stale_temps(&path)?;+ let stored = match std::fs::read(&path) {+ Ok(raw) => serde_json::from_slice::<StoredState>(&raw).map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?,- Err(e) if e.kind() == std::io::ErrorKind::NotFound => State::default(),+ Err(e) if e.kind() == std::io::ErrorKind::NotFound => StoredState::default(),Err(e) => return Err(e),};+ let StoredState {+ grace_started_at,+ devices,+ audit: mut legacy_audit,+ } = stored;+ let state = State {+ grace_started_at,+ devices,+ pending_audit: Vec::new(),+ };let fresh = state.grace_started_at == 0;let audit_key = load_or_create_audit_key(&path)?;- // S9 landed after accounts already had audit history: migrate a- // pre-chain log (see `migrate_legacy_audit`) instead of failing to- // deserialize it and refusing to start.- let migrated = migrate_legacy_audit(&mut state, &audit_key);- let store = Self::with_state(Some(path), policy, state, audit_key);- if fresh || migrated {+ let audit_path = AuditLog::path_for(&path);+ let moved = !legacy_audit.is_empty();+ let audit = if moved {+ // S9 landed after accounts already had audit history: migrate a+ // pre-chain log (see `migrate_legacy_audit`) instead of failing+ // to deserialize it and refusing to start.+ migrate_legacy_audit(&mut legacy_audit, &audit_key);+ // The devices file is the source until it is rewritten below+ // without the log, so a crash in between repeats this move+ // rather than losing or doubling entries.+ let mut audit = AuditLog {+ path: Some(audit_path),+ ..AuditLog::in_memory()+ };+ for (account, entries) in legacy_audit {+ for entry in entries {+ audit.insert(account.clone(), entry);+ }+ }+ audit.compact()?;+ audit+ } else {+ AuditLog::load(audit_path)?+ };+ let sessions_path = PathBuf::from(format!("{}{SESSIONS_FILE_SUFFIX}", path.display()));+ let now = now_secs();+ let sessions: HashMap<String, Session> = match std::fs::read(&sessions_path) {+ // An unreadable file only costs clients a new session.+ Ok(raw) => serde_json::from_slice(&raw).unwrap_or_default(),+ Err(e) if e.kind() == std::io::ErrorKind::NotFound => HashMap::new(),+ Err(e) => return Err(e),+ };+ let mut store = Self::with_state(Some(path), policy, state, audit_key, audit);+ *store.sessions.get_mut().expect("sessions") = sessions+ .into_iter()+ .filter(|(_, s)| s.expires > now)+ .collect();+ store.sessions_path = Some(sessions_path);+ if fresh || moved {let mut state = store.state.lock().expect("devices");if fresh {state.grace_started_at = now_secs();@@ -796,11 +1067,26 @@ impl DeviceStore {if let Some(parent) = path.parent() {std::fs::create_dir_all(parent).map_err(|e| DeviceError::Storage(e.to_string()))?;}- write_private(path, &raw).map_err(|e| DeviceError::Storage(e.to_string()))+ let written = write_private(path, &raw);+ self.record_write(written.is_ok());+ written.map_err(|e| DeviceError::Storage(e.to_string()))+ }++ fn record_write(&self, ok: bool) {+ self.writes_ok+ .store(ok, std::sync::atomic::Ordering::Relaxed);+ }++ /// Whether the last write of the devices file or the audit log+ /// succeeded (true until one fails, and for an in-memory store). Audit+ /// events are written all the time, so this tracks the state volume+ /// within moments of it filling up or going read-only.+ pub fn writes_ok(&self) -> bool {+ self.writes_ok.load(std::sync::atomic::Ordering::Relaxed)}/// Applies `change` to a copy of the state and persists it before- /// making it current.+ /// making it current, then records the audit events `change` queued.fn mutate<T>(&self,change: impl FnOnce(&mut State) -> Result<T, DeviceError>,@@ -809,26 +1095,28 @@ impl DeviceStore {let mut next = state.clone();let out = change(&mut next)?;self.persist(&next)?;+ let events = std::mem::take(&mut next.pending_audit);*state = next;+ // Still under the state lock, so the log follows the order of the+ // changes.+ for (account, event) in events {+ self.audit(&account, event);+ }Ok(out)}- fn push_audit(state: &mut State, account: &str, event: AuditEvent, audit_key: &[u8; 32]) {- let log = state.audit.entry(account.to_owned()).or_default();- chain_append(log, event, audit_key);- if log.len() > AUDIT_LIMIT {- let excess = log.len() - AUDIT_LIMIT;- log.drain(..excess);- }+ /// Queues `event` for `account`'s audit log, recorded once the change+ /// in progress is persisted.+ fn push_audit(state: &mut State, account: &str, event: AuditEvent) {+ state.pending_audit.push((account.to_owned(), event));}/// Records `event` in `account`'s audit log (best effort).pub fn audit(&self, account: &str, event: AuditEvent) {- let audit_key = self.audit_key;- if let Err(e) = self.mutate(|s| {- Self::push_audit(s, account, event, &audit_key);- Ok(())- }) {+ let mut audit = self.audit.lock().expect("audit");+ let appended = audit.append(account, event, &self.audit_key);+ self.record_write(appended.is_ok());+ if let Err(e) = appended {tracing::warn!(error = %e, "audit event not persisted");}}@@ -856,18 +1144,16 @@ impl DeviceStore {/// `account`'s audit log, hash-chained entries, newest last (at most/// `limit`; S9).pub fn audit_log(&self, account: &str, limit: usize) -> Vec<AuditEntry> {- let state = self.state.lock().expect("devices");- let log = state.audit.get(account).cloned().unwrap_or_default();- let skip = log.len().saturating_sub(limit);- log.into_iter().skip(skip).collect()+ let audit = self.audit.lock().expect("audit");+ let log = audit.logs.get(account).map(Vec::as_slice).unwrap_or(&[]);+ log[log.len().saturating_sub(limit)..].to_vec()}/// Verifies `account`'s full retained audit chain (S9). `Ok(())` for an/// empty log.pub fn verify_audit_chain(&self, account: &str) -> Result<(), ChainBroken> {- let state = self.state.lock().expect("devices");- let empty = Vec::new();- let log = state.audit.get(account).unwrap_or(&empty);+ let audit = self.audit.lock().expect("audit");+ let log = audit.logs.get(account).map(Vec::as_slice).unwrap_or(&[]);verify_audit_chain(log, &self.audit_key)}@@ -934,7 +1220,6 @@ impl DeviceStore {by: &str,machine: Option<&str>,now: u64,- audit_key: &[u8; 32],) -> Vec<String> {let Some(machine) = machine else {return Vec::new();@@ -961,7 +1246,6 @@ impl DeviceStore {.device(Some(by)).subject(id.clone()).detail(format!("replaced {name} on the same machine")),- audit_key,);}replaced.into_iter().map(|(id, _)| id).collect()@@ -971,10 +1255,24 @@ impl DeviceStore {if devices.is_empty() {return;}- self.sessions- .lock()- .expect("sessions")- .retain(|_, s| !devices.contains(&s.device));+ let mut sessions = self.sessions.lock().expect("sessions");+ sessions.retain(|_, s| !devices.contains(&s.device));+ self.persist_sessions(&sessions);+ }++ /// Saves the live sessions (best effort: a lost write only means+ /// clients open new sessions after the next restart).+ fn persist_sessions(&self, sessions: &HashMap<String, Session>) {+ let Some(path) = &self.sessions_path else {+ return;+ };+ let written = serde_json::to_vec(sessions)+ .map_err(std::io::Error::other)+ .and_then(|raw| write_private(path, &raw));+ self.record_write(written.is_ok());+ if let Err(e) = written {+ tracing::warn!(error = %e, "device sessions not persisted");+ }}/// Registers (or re-registers) a device, its key proven by `r.sig` over@@ -1003,7 +1301,6 @@ impl DeviceStore {let grace = self.in_grace(now);let policy = self.policy;let machine = r.machine_id.and_then(|m| machine_key(account, m));- let audit_key = self.audit_key;let out = self.mutate(|state| {if let Some(existing) = state.devices.get(&id) {if existing.account != account {@@ -1073,7 +1370,6 @@ impl DeviceStore {AuditEvent::new("device_registered").device(Some(&id)).detail(device.name.clone()),- &audit_key,);}let fresh_sign_in = r.auth_time.filter(|t| {@@ -1159,7 +1455,6 @@ impl DeviceStore {(BY_FRESH_SIGN_IN, false, false) => "enrolled by fresh sign-in",_ => "enrolled during the grace period",}),- &audit_key,);state.devices.insert(id.clone(), device.clone());let superseded = Self::supersede(@@ -1168,7 +1463,6 @@ impl DeviceStore {&id,device.machine.as_deref(),now,- &audit_key,);return Ok(Registered {device,@@ -1206,7 +1500,6 @@ impl DeviceStore {(Some(c), None) if c.starts_with(DEVICE_ID_PREFIX) => (None, Some(c)),(c, t) => (c, t),};- let audit_key = self.audit_key;let (approved, superseded) = self.mutate(|state| {let approving = state.devices@@ -1263,7 +1556,6 @@ impl DeviceStore {AuditEvent::new("device_enrolled").device(Some(&approved.id)).subject(approving_id),- &audit_key,);let superseded = Self::supersede(state,@@ -1271,7 +1563,6 @@ impl DeviceStore {&approved.id,approved.machine.as_deref(),now,- &audit_key,);Ok((approved, superseded))})?;@@ -1323,7 +1614,6 @@ impl DeviceStore {};let ttl = self.policy.ttl_secs;let step = RENEW_STEP_SECS.min(ttl / 2);- let audit_key = self.audit_key;let until = self.mutate(|state| {let mut until = until;@@ -1344,7 +1634,6 @@ impl DeviceStore {state,account,AuditEvent::new("device_session").device(Some(device)),- &audit_key,);Ok(until)})@@ -1361,6 +1650,7 @@ impl DeviceStore {expires,},);+ self.persist_sessions(&sessions);Ok((token, expires))}@@ -1394,7 +1684,6 @@ impl DeviceStore {if name.is_empty() {return Err(DeviceError::Invalid("empty name".into()));}- let audit_key = self.audit_key;self.mutate(|state| {let device = state.devices@@ -1410,7 +1699,6 @@ impl DeviceStore {.device(Some(actor)).subject(id).detail(name),- &audit_key,);Ok(renamed)})@@ -1424,7 +1712,6 @@ impl DeviceStore {id: &str,) -> Result<DeviceRecord, DeviceError> {let now = now_secs();- let audit_key = self.audit_key;let revoked = self.mutate(|state| {let device = state.devices@@ -1443,7 +1730,6 @@ impl DeviceStore {AuditEvent::new("device_revoked").device(Some(actor)).subject(id),- &audit_key,);Ok(revoked)})?;@@ -1914,12 +2200,220 @@ pub(crate) mod tests {// The migration is persisted, so a second restart (the pod// restarting again) does not re-migrate or duplicate entries, and// the chain still verifies under the same (now persisted) key.+ // The log moved out of the devices file into its own.+ let devices: serde_json::Value =+ serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();+ assert!(devices.get("audit").is_none());+ assert_eq!(devices["grace_started_at"], 1_790_000_000u64);+ let lines = std::fs::read_to_string(AuditLog::path_for(&path)).unwrap();+ assert_eq!(lines.lines().count(), 2);drop(store);let reopened = DeviceStore::open(path, enforcing()).unwrap();assert!(reopened.verify_audit_chain("acct").is_ok());assert_eq!(reopened.audit_log("acct", 10).len(), 2);}+ #[test]+ fn a_chained_log_in_the_devices_file_moves_out_once_and_keeps_verifying() {+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("devices.json");+ let audit_key = load_or_create_audit_key(&path).unwrap();+ let mut log = Vec::new();+ for kind in ["device_registered", "device_enrolled", "device_session"] {+ chain_append(&mut log, AuditEvent::new(kind), &audit_key);+ }+ let mut other = Vec::new();+ chain_append(&mut other, AuditEvent::new("machine_access"), &audit_key);+ // The shape the previous relay persisted: the log inside the file.+ let previous = serde_json::json!({+ "grace_started_at": 1_790_000_000u64,+ "devices": {},+ "audit": { "acct": log, "other": other },+ });+ std::fs::write(&path, serde_json::to_vec_pretty(&previous).unwrap()).unwrap();++ let store = DeviceStore::open(path.clone(), enforcing()).unwrap();+ assert_eq!(store.audit_log("acct", 10), log);+ assert!(store.verify_audit_chain("acct").is_ok());+ assert!(store.verify_audit_chain("other").is_ok());+ // New entries chain on from the moved ones.+ store.audit("acct", AuditEvent::new("machine_access"));+ drop(store);++ let reopened = DeviceStore::open(path.clone(), enforcing()).unwrap();+ let after = reopened.audit_log("acct", 10);+ assert_eq!(after.len(), 4);+ assert_eq!(after[3].seq, 4);+ assert_eq!(after[3].prev, log[2].hash);+ assert!(reopened.verify_audit_chain("acct").is_ok());+ assert_eq!(reopened.audit_log("other", 10).len(), 1);+ }++ #[test]+ fn an_audit_event_appends_a_line_and_leaves_the_devices_file_alone() {+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("devices.json");+ let store = DeviceStore::open(path.clone(), enforcing()).unwrap();+ let key = TestKey::new();+ register(&store, "acct", &key, true, Some(now_secs())).unwrap();+ let devices = std::fs::read(&path).unwrap();+ let log_path = AuditLog::path_for(&path);+ let before = std::fs::read_to_string(&log_path).unwrap();+ let enrollment = before.lines().count();+ assert!(enrollment > 0, "the enrollment is audited");++ for _ in 0..200 {+ store.audit("acct", AuditEvent::new("machine_access"));+ }+ assert_eq!(std::fs::read(&path).unwrap(), devices);+ let after = std::fs::read_to_string(&log_path).unwrap();+ assert!(+ after.starts_with(&before),+ "earlier lines are never rewritten"+ );+ assert_eq!(after.lines().count(), enrollment + 200);+ #[cfg(unix)]+ {+ use std::os::unix::fs::PermissionsExt as _;+ assert_eq!(+ std::fs::metadata(&log_path).unwrap().permissions().mode() & 0o777,+ 0o600+ );+ }+ drop(store);+ let reopened = DeviceStore::open(path, enforcing()).unwrap();+ assert_eq!(+ reopened.audit_log("acct", usize::MAX).len(),+ enrollment + 200+ );+ assert!(reopened.verify_audit_chain("acct").is_ok());+ }++ #[test]+ fn a_torn_last_line_is_dropped_and_the_log_stays_appendable() {+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("devices.json");+ let store = DeviceStore::open(path.clone(), enforcing()).unwrap();+ store.audit("acct", AuditEvent::new("device_registered"));+ store.audit("acct", AuditEvent::new("device_enrolled"));+ drop(store);+ // A crash mid-append: half a line, no newline.+ let log_path = AuditLog::path_for(&path);+ let mut raw = std::fs::read(&log_path).unwrap();+ raw.extend_from_slice(br#"{"account":"acct","seq":3,"ts":1"#);+ std::fs::write(&log_path, raw).unwrap();++ let store = DeviceStore::open(path.clone(), enforcing()).unwrap();+ assert_eq!(store.audit_log("acct", 10).len(), 2);+ store.audit("acct", AuditEvent::new("device_session"));+ drop(store);+ let reopened = DeviceStore::open(path, enforcing()).unwrap();+ let log = reopened.audit_log("acct", 10);+ assert_eq!(log.len(), 3);+ assert_eq!(log[2].seq, 3);+ assert!(reopened.verify_audit_chain("acct").is_ok());+ }++ #[test]+ fn trimmed_entries_are_compacted_out_of_the_file() {+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("devices.json");+ let store = DeviceStore::open(path.clone(), enforcing()).unwrap();+ store.audit.lock().unwrap().compact_slack = 100;+ for _ in 0..(AUDIT_LIMIT + 150) {+ store.audit("acct", AuditEvent::new("machine_access"));+ }+ store.audit("other", AuditEvent::new("machine_access"));+ let lines = std::fs::read_to_string(AuditLog::path_for(&path))+ .unwrap()+ .lines()+ .count();+ assert!(lines <= AUDIT_LIMIT + 1 + 100, "{lines} lines");+ drop(store);+ let reopened = DeviceStore::open(path, enforcing()).unwrap();+ let log = reopened.audit_log("acct", usize::MAX);+ assert_eq!(log.len(), AUDIT_LIMIT);+ assert_eq!(log.last().unwrap().seq, (AUDIT_LIMIT + 150) as u64);+ assert!(reopened.verify_audit_chain("acct").is_ok());+ assert_eq!(reopened.audit_log("other", 10).len(), 1);+ }++ #[cfg(unix)]+ #[test]+ fn a_failed_write_is_reported_until_one_succeeds() {+ use std::os::unix::fs::PermissionsExt as _;+ let dir = tempfile::tempdir().unwrap();+ let state = dir.path().join("state");+ std::fs::create_dir(&state).unwrap();+ let store = DeviceStore::open(state.join("devices.json"), enforcing()).unwrap();+ store.audit("acct", AuditEvent::new("device_registered"));+ assert!(store.writes_ok());+ // The volume goes read-only (or full): the next write fails.+ store.audit.lock().unwrap().file = None;+ std::fs::remove_file(AuditLog::path_for(&state.join("devices.json"))).unwrap();+ std::fs::set_permissions(&state, std::fs::Permissions::from_mode(0o500)).unwrap();+ store.audit("acct", AuditEvent::new("machine_access"));+ let blocked = !store.writes_ok();+ std::fs::set_permissions(&state, std::fs::Permissions::from_mode(0o700)).unwrap();+ // Root ignores directory permissions; nothing to check then.+ if blocked {+ assert_eq!(+ store.audit_log("acct", 10).len(),+ 1,+ "a failed event is not recorded"+ );+ store.audit("acct", AuditEvent::new("machine_access"));+ assert!(store.writes_ok());+ }+ }++ #[test]+ fn sessions_survive_a_restart_but_not_a_revocation() {+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("devices.json");+ let store = DeviceStore::open(path.clone(), enforcing()).unwrap();+ let laptop = TestKey::new();+ let phone = TestKey::new();+ register(&store, "acct", &laptop, true, Some(now_secs())).unwrap();+ register(&store, "acct", &phone, true, Some(now_secs())).unwrap();+ let laptop_session = session(&store, "acct", &laptop).unwrap();+ let phone_session = session(&store, "acct", &phone).unwrap();+ let saved =+ std::fs::read_to_string(format!("{}{SESSIONS_FILE_SUFFIX}", path.display())).unwrap();+ assert!(+ !saved.contains(&laptop_session),+ "only token hashes are stored"+ );+ drop(store);++ let reopened = DeviceStore::open(path.clone(), enforcing()).unwrap();+ assert_eq!(+ reopened.session_device("acct", &laptop_session),+ Some(laptop.id())+ );+ assert_eq!(reopened.session_device("other", &laptop_session), None);+ reopened.revoke("acct", &laptop.id(), &phone.id()).unwrap();+ assert_eq!(reopened.session_device("acct", &phone_session), None);+ drop(reopened);+ let again = DeviceStore::open(path, enforcing()).unwrap();+ assert_eq!(again.session_device("acct", &phone_session), None);+ assert_eq!(+ again.session_device("acct", &laptop_session),+ Some(laptop.id())+ );+ }++ #[test]+ fn opening_removes_temp_files_an_interrupted_write_left() {+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("devices.json");+ let stale = dir.path().join(".devices.json.6852b0d9e7d6.tmp");+ std::fs::write(&stale, vec![b'{'; 4096]).unwrap();+ let store = DeviceStore::open(path, enforcing()).unwrap();+ assert!(!stale.exists());+ drop(store);+ }+#[test]fn audit_is_bounded_and_throttled() {let store = DeviceStore::in_memory(DevicePolicy::default());@@ -1953,8 +2447,8 @@ pub(crate) mod tests {// Editing the middle entry's content breaks the chain from there:// its own hash/mac no longer match, and the entry after it no// longer chains from a hash that is still valid.- let mut tampered = store.state.lock().unwrap();- tampered.audit.get_mut("acct").unwrap()[1].event.kind = "device_revoked".into();+ let mut tampered = store.audit.lock().unwrap();+ tampered.logs.get_mut("acct").unwrap()[1].event.kind = "device_revoked".into();drop(tampered);let err = store.verify_audit_chain("acct").unwrap_err();assert_eq!(err.seq, 2);@@ -1978,20 +2472,24 @@ pub(crate) mod tests {store.audit("acct", AuditEvent::new("device_enrolled"));drop(store);- // Someone with only the state file (a backup, a disk snapshot) edits- // an event and recomputes the plain hash chain (no secret needed for- // that much), but cannot reproduce a valid mac without the audit- // key, which lives in a separate file.- let raw = std::fs::read_to_string(&path).unwrap();- let mut value: serde_json::Value = serde_json::from_str(&raw).unwrap();- let entries = value["audit"]["acct"].as_array_mut().unwrap();+ // Someone with only the state files (a backup, a disk snapshot)+ // edits an event and recomputes the plain hash chain (no secret+ // needed for that much), but cannot reproduce a valid mac without+ // the audit key, which lives in a separate file.+ let log_path = AuditLog::path_for(&path);+ let raw = std::fs::read_to_string(&log_path).unwrap();+ let mut entries: Vec<serde_json::Value> = raw+ .lines()+ .map(|l| serde_json::from_str(l).unwrap())+ .collect();entries[0]["kind"] = "device_revoked".into();- let prev = entries[0]["hash"].as_str().unwrap().to_owned();let seq = entries[0]["seq"].as_u64().unwrap();let event: AuditEvent = serde_json::from_value(entries[0].clone()).unwrap();- entries[0]["hash"] = chain_hash(AUDIT_GENESIS, seq, &event).into();- entries[1]["prev"] = prev.clone().into();- std::fs::write(&path, serde_json::to_vec_pretty(&value).unwrap()).unwrap();+ let hash = chain_hash(AUDIT_GENESIS, seq, &event);+ entries[0]["hash"] = hash.clone().into();+ entries[1]["prev"] = hash.into();+ let edited: String = entries.iter().map(|e| format!("{e}\n")).collect();+ std::fs::write(&log_path, edited).unwrap();let reopened = DeviceStore::open(path, DevicePolicy::default()).unwrap();// The mac on the edited entry no longer matches (it was neverdiff --git a/libs/cua-spacesd/crates/cua-relay/src/directory.rs b/libs/cua-spacesd/crates/cua-relay/src/directory.rsindex c04eb8d1e0..74da81be63 100644--- a/libs/cua-spacesd/crates/cua-relay/src/directory.rs+++ b/libs/cua-spacesd/crates/cua-relay/src/directory.rs@@ -13,7 +13,7 @@ use std::sync::Mutex;use base64::Engine as _;use serde::{Deserialize, Serialize};-use crate::assertion::{now_secs, write_private};+use crate::assertion::{now_secs, remove_stale_temps, write_private};use crate::oidc::Identity;/// Prefix of machine tokens (tells them apart from static relay tokens and@@ -183,6 +183,8 @@ pub struct Directory {path: Option<PathBuf>,records: Mutex<BTreeMap<String, MachineRecord>>,max_per_account: usize,+ /// Whether the last write of the directory file succeeded.+ writes_ok: std::sync::atomic::AtomicBool,}fn hash_token(token: &str) -> String {@@ -241,11 +243,14 @@ impl Directory {path: None,records: Mutex::default(),max_per_account,+ writes_ok: true.into(),}}- /// Loads (or starts) the directory persisted at `path`.+ /// Loads (or starts) the directory persisted at `path`, first removing+ /// temp files an interrupted write left behind.pub fn open(path: PathBuf, max_per_account: usize) -> std::io::Result<Self> {+ remove_stale_temps(&path)?;let records = match std::fs::read(&path) {Ok(raw) => {let list: Vec<MachineRecord> = serde_json::from_slice(&raw)@@ -259,9 +264,16 @@ impl Directory {path: Some(path),records: Mutex::new(records),max_per_account,+ writes_ok: true.into(),})}+ /// Whether the last write of the directory file succeeded (true until+ /// one fails, and for an in-memory directory).+ pub fn writes_ok(&self) -> bool {+ self.writes_ok.load(std::sync::atomic::Ordering::Relaxed)+ }+fn persist(&self, records: &BTreeMap<String, MachineRecord>) -> Result<(), DirectoryError> {let Some(path) = &self.path else {return Ok(());@@ -272,7 +284,10 @@ impl Directory {if let Some(parent) = path.parent() {std::fs::create_dir_all(parent).map_err(|e| DirectoryError::Storage(e.to_string()))?;}- write_private(path, &raw).map_err(|e| DirectoryError::Storage(e.to_string()))+ let written = write_private(path, &raw);+ self.writes_ok+ .store(written.is_ok(), std::sync::atomic::Ordering::Relaxed);+ written.map_err(|e| DirectoryError::Storage(e.to_string()))}/// Registers `id` for `who` (or rotates its token when `who` already