[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
+16 -33
View File
@@ -5,20 +5,18 @@
//! Switching the engine at runtime (ROADMAP §1.6 as corrected 2026-11-07, §3, §4).
//!
//! Exactly one of `ianitor-edge.service` and `caddy-fallback.service` runs. Control may start,
//! stop and restart only those two, through a polkit rule scoped to exactly them (NixOS
//! and stop only those two, through a polkit rule scoped to exactly them (NixOS
//! module). `/var/lib/ianitor/engine` names the one to run; the `ianitor-engine-select` unit
//! reads it at boot. A switch writes the file first (so a reboot mid-switch boots the
//! target), stops the old engine, starts the new one and waits until it serves; if the new
//! one does not come up, it goes back to the old one and says so.
//!
//! Decisions (phase 3):
//! - While Caddy serves, changes are paused (`engine.caddy`): Caddy is the emergency
//! fallback, and the apply pipeline's trial, watch and revert are built on edge's admin
//! socket. Changes made then would also have no differential check.
//! - While Caddy serves, changes keep working (Mark, 2026-11-08): an apply is loaded into Caddy
//! through its admin socket, watched through Caddy and kept or reverted (`engine/caddy_apply`).
//! - Share links are refused while Caddy serves (`share.engine_caddy`, see docs/api.md):
//! Caddy cannot check them, and control never serves visitors itself.
//! - Device revocations while Caddy serves restart caddy-fallback (Caddy reads the allow
//! list when it starts); this is why the polkit rule also allows `restart`.
//! - Device revocations while Caddy serves reload Caddy's config through its admin socket.
//!
//! The systemd calls go through [`Units`], so tests use a fake.
@@ -40,7 +38,6 @@ use crate::util::{now, write_atomic};
pub trait Units: Send + Sync {
fn start(&self, unit: &str) -> Result<(), String>;
fn stop(&self, unit: &str) -> Result<(), String>;
fn restart(&self, unit: &str) -> Result<(), String>;
fn is_active(&self, unit: &str) -> Result<bool, String>;
}
@@ -79,9 +76,6 @@ impl Units for Systemctl {
fn stop(&self, unit: &str) -> Result<(), String> {
self.verb("stop", unit)
}
fn restart(&self, unit: &str) -> Result<(), String> {
self.verb("restart", unit)
}
fn is_active(&self, unit: &str) -> Result<bool, String> {
Ok(self.run(&["is-active", "--quiet", unit])?.status.success())
}
@@ -117,12 +111,6 @@ impl Units for FakeUnits {
}
Ok(())
}
fn restart(&self, unit: &str) -> Result<(), String> {
if let Ok(mut c) = self.calls.lock() {
c.push(format!("restart {unit}"));
}
Ok(())
}
fn is_active(&self, unit: &str) -> Result<bool, String> {
Ok(self.active.lock().is_ok_and(|a| a.contains(unit)))
}
@@ -185,16 +173,6 @@ impl Control {
}
}
pub async fn restart_caddy(&self) -> Result<(), String> {
let (Some(cfg), Some(units)) = (self.engine_cfg(), self.units.clone()) else {
return Err("no engine settings".into());
};
let unit = cfg.caddy_unit.clone();
tokio::task::spawn_blocking(move || units.restart(&unit))
.await
.map_err(|e| e.to_string())?
}
/// Check a switch request and start it. Answers at once; the switch runs in the
/// background (`overview.engine.state` is `switching` until it ends).
pub fn request_switch(self: &Arc<Self>, target: &str) -> Result<EngineState, Fail> {
@@ -239,6 +217,7 @@ impl Control {
async fn switch(self: Arc<Self>, target: &str) {
// No apply runs during a switch, and none starts.
let _lock = self.apply_lock.lock().await;
let _engine = self.engine_lock.lock().await;
let from = self.engine_running();
let res = self.switch_inner(&from, target).await;
let t = now();
@@ -314,7 +293,16 @@ impl Control {
let u = units.clone();
let n = new.clone();
Self::blocking(move || u.start(&n)).await?;
self.wait_serving(target, &units, &new).await
self.wait_serving(target, &units, &new).await?;
// Edge reads edge.json when it starts; tell it again in case it kept running (changes
// kept under Caddy are in edge.json).
if target == "pingora"
&& self.edge.attached()
&& let Err(e) = self.edge.reload().await
{
log::warn!(target: "ianitor::engine", "edge did not reload edge.json after the switch: {e}");
}
Ok(())
}
async fn bring_back(&self, engine: &str) -> Result<(), String> {
@@ -397,12 +385,7 @@ impl Control {
/// Any reason changes wait now: Caddy serving, or a pause.
pub fn effective_pause(&self) -> Option<Reason> {
self.engine_pause().or_else(|| self.paused())
}
/// Changes wait while Caddy serves.
pub fn engine_pause(&self) -> Option<Reason> {
(self.engine_running() == "caddy").then(|| reason("engine.caddy", Params::new()))
self.paused()
}
}