[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:
@@ -30,7 +30,11 @@ pub fn status_of(code: &str) -> u16 {
|
||||
| "pki.wrong_key"
|
||||
| "pki.device_name"
|
||||
| "pki.device_kind"
|
||||
| "pki.wrong_code" => 422,
|
||||
| "pki.wrong_code"
|
||||
| "pki.manual.not_ca"
|
||||
| "pki.manual.eku"
|
||||
| "pki.manual.dates" => 422,
|
||||
"pki.devices_unwritten" => 503,
|
||||
"pki.too_many_requests" => 429,
|
||||
_ => 409,
|
||||
}
|
||||
@@ -74,7 +78,6 @@ impl Control {
|
||||
.filter(|h| h.tls == TlsMode::Domi)
|
||||
.map(|h| h.hostname.to_string())
|
||||
.collect();
|
||||
let devices = model.hosts().iter().any(|h| h.device_certs);
|
||||
let mut changed = false;
|
||||
for dir in self.cert_dirs() {
|
||||
let issued = p.ensure_host_certs(&domi, dir, t).map_err(|e| {
|
||||
@@ -89,9 +92,8 @@ impl Control {
|
||||
log::info!(target: "ianitor::pki", "issued server certificates for {}", issued.join(", "));
|
||||
changed = true;
|
||||
}
|
||||
if devices || dir.join(ianitor_core::compile::DEVICE_CA_FILE).exists() {
|
||||
changed |= p.write_device_files(dir, t).map_err(pki_fail)?;
|
||||
}
|
||||
// Always: the files are tiny, and a list that failed to be written catches up here.
|
||||
changed |= p.write_device_files(dir, t).map_err(pki_fail)?;
|
||||
}
|
||||
Ok(changed)
|
||||
}
|
||||
@@ -102,6 +104,9 @@ impl Control {
|
||||
return;
|
||||
}
|
||||
let mut last_renewal_check = now();
|
||||
// At start: bring the device files in line (a revocation that could not be written
|
||||
// before a restart is in force now).
|
||||
let _ = self.devices_changed().await;
|
||||
loop {
|
||||
tokio::time::sleep(Duration::from_secs(2)).await;
|
||||
let me = self.clone();
|
||||
@@ -126,8 +131,13 @@ impl Control {
|
||||
last_renewal_check = t;
|
||||
self.refresh_certificates(changes.intermediate).await;
|
||||
}
|
||||
if changes.devices {
|
||||
self.devices_changed().await;
|
||||
let retry = self
|
||||
.devices_problem
|
||||
.lock()
|
||||
.map(|p| p.is_some())
|
||||
.unwrap_or(false);
|
||||
if changes.devices || retry {
|
||||
let _ = self.devices_changed().await;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -154,35 +164,48 @@ impl Control {
|
||||
}
|
||||
|
||||
/// The device files changed (issue or revoke): rewrite them and tell the engine at once.
|
||||
pub async fn devices_changed(&self) {
|
||||
let Some(p) = &self.pki else { return };
|
||||
pub async fn devices_changed(&self) -> Result<(), String> {
|
||||
let Some(p) = &self.pki else { return Ok(()) };
|
||||
let _engine = self.engine_lock.lock().await;
|
||||
let t = now();
|
||||
let mut changed = false;
|
||||
let mut result = Ok(());
|
||||
for dir in self.cert_dirs() {
|
||||
match p.write_device_files(dir, t) {
|
||||
Ok(c) => changed |= c,
|
||||
Err(e) => log::error!(target: "ianitor::pki", "device files not written: {e}"),
|
||||
if let Err(e) = p.write_device_files(dir, t) {
|
||||
result = Err(format!(
|
||||
"the device files in {} could not be written: {e}",
|
||||
dir.display()
|
||||
));
|
||||
}
|
||||
}
|
||||
if !changed {
|
||||
return;
|
||||
// Tell the serving engine every time, changed or not: a repeated Revoke nudges again.
|
||||
if result.is_ok() {
|
||||
result = if self.engine_running() == "caddy" {
|
||||
self.caddy_reload()
|
||||
.await
|
||||
.map_err(|e| format!("Caddy did not reload the device list: {e}"))
|
||||
} else if self.edge.attached() {
|
||||
self.edge
|
||||
.reload_devices()
|
||||
.await
|
||||
.map_err(|e| format!("edge did not re-read the device list: {e}"))
|
||||
} else {
|
||||
Ok(())
|
||||
};
|
||||
}
|
||||
if self.engine_running() == "caddy" {
|
||||
// Caddy reads the allow list when it starts: restart it so a revocation counts.
|
||||
if let Err(e) = self.restart_caddy().await {
|
||||
log::error!(target: "ianitor::pki", "caddy-fallback not restarted after a device change: {e}");
|
||||
}
|
||||
} else if self.edge.attached()
|
||||
&& let Err(e) = self.edge.reload_devices().await
|
||||
{
|
||||
log::error!(target: "ianitor::pki", "edge did not re-read the device certificates: {e}");
|
||||
if let Ok(mut pr) = self.devices_problem.lock() {
|
||||
*pr = result.as_ref().err().cloned();
|
||||
}
|
||||
if let Err(e) = &result {
|
||||
log::error!(target: "ianitor::pki", "{e}; retrying");
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
async fn reload_serving_engine(&self) {
|
||||
let _engine = self.engine_lock.lock().await;
|
||||
if self.engine_running() == "caddy" {
|
||||
if let Err(e) = self.restart_caddy().await {
|
||||
log::error!(target: "ianitor::pki", "caddy-fallback not restarted: {e}");
|
||||
if let Err(e) = self.caddy_reload().await {
|
||||
log::error!(target: "ianitor::pki", "Caddy did not reload the new certificates: {e}");
|
||||
}
|
||||
} else if self.edge.attached()
|
||||
&& let Err(e) = self.edge.reload().await
|
||||
|
||||
Reference in New Issue
Block a user