[control] Fix round 1: changes apply while Caddy serves (admin socket, watch through Caddy), revocations that fail are errors and catch up, proof only on the forward-auth UI host, root keys found by content, intermediates checked before install, stale SNI cleared before each handshake

This commit is contained in:
2026-11-08 10:49:31 +01:00
parent 0749119f19
commit e84eb1ba74
36 changed files with 934 additions and 131 deletions
+57 -5
View File
@@ -96,6 +96,10 @@ pub struct Shared {
pub certs: Arc<CertStore>,
/// The listener's device-certificate check; its trust follows the runtime.
pub devices: Arc<crate::tls::DeviceVerifier>,
/// Serialises building the device trust with installing a runtime.
trust_lock: Mutex<()>,
/// Size and mtime of the device files when they were last read.
device_stamp: Mutex<Option<DeviceStamp>>,
generation: AtomicU64,
changed: tokio::sync::watch::Sender<u64>,
trial: Mutex<Option<Trial>>,
@@ -142,6 +146,8 @@ impl Shared {
current: ArcSwap::from_pointee(rt),
certs,
devices,
trust_lock: Mutex::new(()),
device_stamp: Mutex::new(None),
generation: AtomicU64::new(1),
changed,
trial: Mutex::new(None),
@@ -196,8 +202,12 @@ impl Shared {
rt.device_trust.clone()
}
};
// Trust and runtime change together (reload_device_trust checks the runtime under the
// same lock).
let guard = self.trust_lock.lock();
self.devices.replace(trust);
self.current.store(rt);
drop(guard);
self.generation.fetch_add(1, Ordering::SeqCst);
self.notify();
self.acme_wake.notify_one();
@@ -530,11 +540,53 @@ impl Shared {
/// Re-read the device CA bundle and allow list now (control calls this after an issue
/// or a revocation; allowed during a trial). Returns how many devices are allowed.
pub fn reload_device_trust(&self) -> Result<usize, String> {
// Built from the runtime that serves when it is installed: if a reload swapped the
// runtime meanwhile, build again from the new one (its host set may differ).
for _ in 0..5 {
let rt = self.current();
let t = fresh_trust(&rt)?;
let n = t.as_ref().map_or(0, |t| t.allowed());
let _g = self
.trust_lock
.lock()
.map_err(|_| "device lock".to_owned())?;
if Arc::ptr_eq(&rt, &self.current()) {
self.devices.replace(t);
log::info!(target: "ianitor::tls", "device certificates re-read: {n} allowed");
return Ok(n);
}
}
Err("the config changed while the device certificates were read; try again".into())
}
/// The backstop for revocations (every 5 s, like the share deny list): re-read the device
/// files when they changed on disk, so a revocation counts even if control's nudge is lost.
pub fn poll_device_trust(&self) {
let rt = self.current();
let t = fresh_trust(&rt)?;
let n = t.as_ref().map_or(0, |t| t.allowed());
self.devices.replace(t);
log::info!(target: "ianitor::tls", "device certificates re-read: {n} allowed");
Ok(n)
let Some(dc) = &rt.cfg.device_certs else {
return;
};
let stamp = |p: &str| {
std::fs::metadata(p)
.ok()
.map(|m| (m.len(), m.modified().ok()))
};
let now = (stamp(&dc.ca), stamp(&dc.allow));
let changed = match self.device_stamp.lock() {
Ok(mut s) => {
let c = s.as_ref() != Some(&now);
*s = Some(now);
c
}
Err(_) => true,
};
if changed && let Err(e) = self.reload_device_trust() {
// Fail closed: an unreadable list lets no device in until it is fixed.
log::error!(target: "ianitor::tls", "{e}; no device certificate is accepted until it is fixed");
self.devices.replace(None);
}
}
}
type FileStamp = Option<(u64, Option<std::time::SystemTime>)>;
type DeviceStamp = (FileStamp, FileStamp);