[control] Store and recovery fixes: cross-process token lock, revert keeps its journal until done, epoch and deny list never roll back, journal errors propagate, lost confirm reloads edge, trial hash checked

This commit is contained in:
2026-11-07 18:51:58 +01:00
parent b9e14c6aee
commit 4448527b16
10 changed files with 305 additions and 66 deletions
+99 -25
View File
@@ -11,6 +11,7 @@
//! and on its own every 5 s. When control is down nothing can be stopped, but edge keeps
//! refusing what is already on the list, also across its own restarts.
use std::collections::BTreeSet;
use std::path::{Path, PathBuf};
use ianitor_core::share::{self, LinkSpec, ShareId, ShareKey};
@@ -61,6 +62,10 @@ struct RevocationFile {
const KEEP_SECS: u64 = 30 * 86_400;
pub const MIN_EXPIRY: u64 = 3600;
pub const MAX_EXPIRY: u64 = 90 * 86_400;
/// Most active links at once.
pub const MAX_ACTIVE: usize = 2000;
/// Most ids edge takes on its deny list (edge refuses 10,000 and more).
const MAX_DENY: usize = 9_999;
pub struct Shares {
path: PathBuf,
@@ -80,6 +85,8 @@ pub enum MintError {
PasswordShort,
PasswordLong,
Replaces,
/// More active links than [`MAX_ACTIVE`].
TooMany,
Store(String),
}
@@ -96,33 +103,91 @@ impl Shares {
Ok(read_json(&self.path)?.unwrap_or_default())
}
/// The revocation file as edge has it now, if it can be read.
fn published(&self) -> Option<(u32, BTreeSet<String>)> {
#[derive(Deserialize)]
struct Published {
epoch: u32,
deny: Vec<String>,
}
let p: Published = serde_json::from_slice(&std::fs::read(&self.revocations).ok()?).ok()?;
Some((p.epoch, p.deny.into_iter().collect()))
}
/// The records, with the epoch never below the published one (records lost or restored
/// from an older backup must not bring stopped links back).
fn load_reconciled(&self) -> Result<ShareFile, String> {
let mut f = self.load()?;
if let Some((epoch, _)) = self.published()
&& epoch > f.epoch
{
log::warn!(target: "ianitor::share", "share-link records are behind the published revocations (epoch {} < {epoch}); continuing from {epoch}", f.epoch);
f.epoch = epoch;
}
Ok(f)
}
fn save(&self, f: &mut ShareFile) -> Result<(), String> {
let t = now();
f.links.retain(|l| {
let end = l.stopped_at.unwrap_or(l.expires_at).min(l.expires_at);
t < end.saturating_add(KEEP_SECS)
});
// Kept at least until they expire: a stopped link must stay on the deny list as long
// as its token would otherwise work.
f.links
.retain(|l| t < l.expires_at.saturating_add(KEEP_SECS));
let deny = self.reconcile(f);
write_json(&self.path, f)?;
self.write_revocations(f)
self.publish(f.epoch, deny)
}
/// The deny list for `f`, raising `f.epoch` when the list cannot be kept whole:
/// - a published id that the records cannot account for (stopped link lost from the
/// records) is never just dropped: the epoch goes up, which stops every older link;
/// - a list longer than edge accepts also raises the epoch, instead of being cut short.
fn reconcile(&self, f: &mut ShareFile) -> Vec<String> {
let t = now();
let published = self.published();
if let Some((epoch, _)) = &published {
f.epoch = f.epoch.max(*epoch);
}
let deny_for = |f: &ShareFile| -> Vec<String> {
let mut d: Vec<String> = f
.links
.iter()
.filter(|l| l.stopped_at.is_some() && l.expires_at > t && l.epoch >= f.epoch)
.map(|l| l.id.clone())
.collect();
d.sort();
d
};
let mut deny = deny_for(f);
if let Some((epoch, old)) = published
&& epoch == f.epoch
{
let accounted = |id: &String| {
deny.binary_search(id).is_ok()
|| f.links
.iter()
.any(|l| &l.id == id && (l.expires_at <= t || l.epoch < f.epoch))
};
if !old.iter().all(accounted) {
log::warn!(target: "ianitor::share", "stopped links are missing from the records; raising the epoch, which stops every earlier link");
f.epoch = f.epoch.saturating_add(1);
deny = deny_for(f);
}
}
if deny.len() > MAX_DENY {
log::warn!(target: "ianitor::share", "more than {MAX_DENY} stopped links; raising the epoch, which stops every earlier link");
f.epoch = f.epoch.saturating_add(1);
deny = deny_for(f);
}
deny
}
/// Write the revocation file edge reads.
pub fn write_revocations(&self, f: &ShareFile) -> Result<(), String> {
let t = now();
let mut deny: Vec<String> = f
.links
.iter()
.filter(|l| l.stopped_at.is_some() && l.expires_at > t && l.epoch >= f.epoch)
.map(|l| l.id.clone())
.collect();
deny.sort();
deny.truncate(9_999);
fn publish(&self, epoch: u32, deny: Vec<String>) -> Result<(), String> {
// Readable by edge (another user); it holds only random link ids.
let text = serde_json::to_vec(&RevocationFile {
epoch: f.epoch,
deny,
})
.map_err(|e| e.to_string())?;
let text =
serde_json::to_vec(&RevocationFile { epoch, deny }).map_err(|e| e.to_string())?;
crate::crash::fail_write(&self.revocations)?;
crate::util::write_atomic(&self.revocations, &text, 0o644)
.map_err(|e| format!("cannot write {}: {e}", self.revocations.display()))
}
@@ -132,8 +197,8 @@ impl Shares {
.lock
.lock()
.map_err(|_| "share store unavailable".to_owned())?;
let f = self.load()?;
self.write_revocations(&f)
let mut f = self.load()?;
self.save(&mut f)
}
#[allow(clippy::too_many_arguments)]
@@ -164,7 +229,16 @@ impl Shares {
.lock
.lock()
.map_err(|_| MintError::Store("share store unavailable".into()))?;
let mut f = self.load().map_err(MintError::Store)?;
let mut f = self.load_reconciled().map_err(MintError::Store)?;
let t0 = now();
if f.links
.iter()
.filter(|l| l.stopped_at.is_none() && l.expires_at > t0)
.count()
>= MAX_ACTIVE
{
return Err(MintError::TooMany);
}
if let Some(r) = replaces {
let Some(old) = f
.links
@@ -208,7 +282,7 @@ impl Shares {
.lock
.lock()
.map_err(|_| "share store unavailable".to_owned())?;
let mut f = self.load()?;
let mut f = self.load_reconciled()?;
let Some(l) = f.links.iter_mut().find(|l| l.id == id) else {
return Ok(None);
};
@@ -224,7 +298,7 @@ impl Shares {
.lock
.lock()
.map_err(|_| "share store unavailable".to_owned())?;
let mut f = self.load()?;
let mut f = self.load_reconciled()?;
f.epoch = f.epoch.saturating_add(1);
let e = f.epoch;
self.save(&mut f)?;