[control] QC minors: device install links open a page and download on POST (never listed), approve and install failures show on the request and the CLI waits, PBKDF2 floor, private pki dirs or an error, trusted_proxies for a proxy in front, a placeholder keeps the device allow list from being empty (Caddy refuses that)

This commit is contained in:
2026-11-08 11:02:50 +01:00
parent bc336d1188
commit 3bf5fa0e1a
27 changed files with 548 additions and 46 deletions
+101 -3
View File
@@ -100,9 +100,13 @@ pub struct Request {
/// Renewals: when the new intermediate expires, once installed.
#[serde(default)]
pub not_after: Option<i64>,
/// Why it failed (plain English, no secrets).
/// Why it failed, or why the last approval or install for it was not used (plain
/// English, no secrets). A request that is still waiting can carry one.
#[serde(default)]
pub error: Option<String>,
/// When `error` was last set for a request that is still waiting (the CLI waits for it).
#[serde(default)]
pub problem_at: Option<u64>,
/// Who asked (token name or signed-in user).
#[serde(default)]
pub actor: String,
@@ -314,7 +318,8 @@ impl Pki {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let _ = std::fs::set_permissions(&d, std::fs::Permissions::from_mode(0o700));
std::fs::set_permissions(&d, std::fs::Permissions::from_mode(0o700))
.map_err(|e| format!("cannot make {} private: {e}", d.display()))?;
}
}
Ok(Pki {
@@ -485,6 +490,7 @@ impl Pki {
csr_pem: Some(csr),
not_after: None,
error: None,
problem_at: None,
actor: actor.to_owned(),
};
prune(&mut rs, now);
@@ -539,6 +545,8 @@ impl Pki {
remove_if_exists(&pending).map_err(io)?;
*self.current.lock().map_err(|_| io("pki"))? = Some(Arc::new(int));
r.state = "approved".into();
r.error = None;
r.problem_at = None;
r.approved_at = Some(now);
r.not_after = Some(info.not_after);
let out = r.clone();
@@ -605,12 +613,29 @@ impl Pki {
// while no intermediate exists, and the empty allow list still lets no one in.
ca.push_str(&self.root_pem);
}
let allow: String = self
let mut allow: String = self
.devices()?
.iter()
.filter(|d| d.state(now) == "valid")
.map(|d| d.cert_pem.as_str())
.collect();
if allow.is_empty() {
// Caddy refuses an empty leaf list (and the whole config with it): the list
// holds a certificate no one has the key to. Made once, so the file stays put.
let p = self.dir.join("no-device.crt");
allow = match std::fs::read_to_string(&p) {
Ok(pem) if pki::inspect(&pem).is_ok_and(|i| i.subject_cn == pki::NO_DEVICE_CN) => {
pem
}
_ => {
let pem = pki::no_device_placeholder(now as i64).map_err(|e| {
PkiFail::Internal(format!("cannot make the placeholder: {e}"))
})?;
write_atomic(&p, pem.as_bytes(), 0o644).map_err(io)?;
pem
}
};
}
let mut changed = false;
for (name, text) in [
(ianitor_core::compile::DEVICE_CA_FILE, ca),
@@ -686,6 +711,7 @@ impl Pki {
csr_pem: None,
not_after: None,
error: None,
problem_at: None,
actor: actor.to_owned(),
};
rs.push(r.clone());
@@ -749,6 +775,7 @@ impl Pki {
let _ = remove_if_exists(&path);
if let Err(e) = self.approve_device(&rid, code.trim(), now) {
log::warn!(target: "ianitor::pki", "approval for {rid} not used: {e}");
self.note_problem(&rid, &e, now);
} else {
changes.devices = true;
}
@@ -764,6 +791,7 @@ impl Pki {
Ok(_) => changes.intermediate = true,
Err(e) => {
log::warn!(target: "ianitor::pki", "intermediate for {rid} not installed: {e}");
self.note_problem(&rid, &e, now);
}
}
}
@@ -791,6 +819,26 @@ impl Pki {
Ok(changes)
}
/// An approval or install for a waiting request was not used: say why on the request
/// (the UI and the CLI show it), and keep it open for another try.
fn note_problem(&self, rid: &str, e: &PkiFail, now: u64) {
let text = match e {
PkiFail::Code(c) => crate::messages::message(c, &crate::messages::Params::new()),
PkiFail::Internal(_) => {
crate::messages::message("internal", &crate::messages::Params::new())
}
};
let Ok(_g) = self.guard() else { return };
let Ok(mut rs) = self.requests() else { return };
if let Some(r) = rs.iter_mut().find(|r| r.id == rid && r.state == "waiting") {
r.error = Some(text);
r.problem_at = Some(now);
if let Err(e) = self.save_requests(&rs) {
log::error!(target: "ianitor::pki", "cannot record the problem on {rid}: {e}");
}
}
}
fn close(&self, rid: &str, state: &str, error: Option<String>) -> Result<(), PkiFail> {
let _g = self.guard()?;
let mut rs = self.requests()?;
@@ -866,6 +914,8 @@ impl Pki {
}
self.save_devices(&ds)?;
r.state = "approved".into();
r.error = None;
r.problem_at = None;
r.approved_at = Some(now);
r.device_id = Some(did);
self.save_requests(&rs)?;
@@ -929,6 +979,24 @@ impl Pki {
/// Hand out an install file once: the PKCS#12 bytes and the device name. The token is
/// gone afterwards, whatever happens.
/// Whether a one-time link is still unused and in time (the device name, for its page),
/// without using it up.
pub fn peek_bundle(&self, token: &str, now: u64) -> Option<String> {
let device_id = {
let m = self.bundles.lock().ok()?;
let b = m.get(token)?;
if b.expires_at <= now {
return None;
}
b.device_id.clone()
};
self.devices()
.ok()?
.into_iter()
.find(|d| d.id == device_id && d.state(now) == "valid")
.map(|d| d.name)
}
pub fn take_bundle(&self, token: &str, now: u64) -> Result<(Vec<u8>, String), PkiFail> {
let b = {
let mut m = self.bundles.lock().map_err(|_| io("bundles"))?;
@@ -1084,3 +1152,33 @@ pub fn cli_install(state_dir: &Path, cert_pem: &str, now: u64) -> Result<Request
pub fn requests_path(state_dir: &Path) -> PathBuf {
state_dir.join("pki/requests.json")
}
/// After `pki approve` or `pki install`: wait (up to 20 s) for control to act on it. `Ok` with
/// the request's new state, or `Err` with why it was not used.
pub fn cli_wait(state_dir: &Path, rid: &str, since: u64) -> Result<String, String> {
let path = requests_path(state_dir);
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(20);
loop {
let rs: Vec<Request> = read_json(&path)?.unwrap_or_default();
match rs.iter().find(|r| r.id == rid) {
None => return Err("the request is gone (it may have expired)".into()),
Some(r) if r.state != "waiting" => {
return match (&r.error, r.state.as_str()) {
(Some(e), s) if s != "approved" => Err(e.clone()),
_ => Ok(r.state.clone()),
};
}
Some(r) if r.problem_at.is_some_and(|t| t >= since) => {
return Err(r.error.clone().unwrap_or_else(|| "it was not used".into()));
}
Some(_) => {}
}
if std::time::Instant::now() >= deadline {
return Err(
"control has not acted on it within 20 s; is ianitor-control running? It still will when it starts"
.into(),
);
}
std::thread::sleep(std::time::Duration::from_millis(250));
}
}