From 2c06efead14f8c97c23908e74ffa0d8dead3bc24 Mon Sep 17 00:00:00 2001 From: mnovak Date: Sat, 7 Nov 2026 18:23:41 +0100 Subject: [PATCH] [control] Dev mode: throw-away CA and certificates for three dev hosts; push step skipped without a remote --- Cargo.lock | 1 + crates/control/Cargo.toml | 2 ++ crates/control/src/dev.rs | 44 ++++++++++++++++++++++++++++++++++-- crates/control/src/engine.rs | 18 ++++++++++++--- 4 files changed, 60 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4e5a097..037678f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1064,6 +1064,7 @@ dependencies = [ "ianitor-edge", "ianitor-testkit", "log", + "rcgen", "ring", "rustls", "serde", diff --git a/crates/control/Cargo.toml b/crates/control/Cargo.toml index bd59d3a..3929d18 100644 --- a/crates/control/Cargo.toml +++ b/crates/control/Cargo.toml @@ -29,6 +29,8 @@ ring.workspace = true base64.workspace = true http.workspace = true bytes.workspace = true +# Dev mode only: a throw-away CA for the dev hosts (the Domi CA arrives in phase 3). +rcgen.workspace = true [dev-dependencies] ianitor-testkit.workspace = true diff --git a/crates/control/src/dev.rs b/crates/control/src/dev.rs index 4a867f9..70930e6 100644 --- a/crates/control/src/dev.rs +++ b/crates/control/src/dev.rs @@ -18,6 +18,44 @@ use ianitor_control::repo::Repo; use ianitor_control::tokens::Tokens; use ianitor_control::util::{hex, random, write_atomic}; +/// Hosts that get a certificate from the dev CA, so `tls = "domi"` works for them. +const DEV_HOSTS: [&str; 3] = ["app.dev.test", "wiki.dev.test", "photos.dev.test"]; + +/// A throw-away CA and certificates for [`DEV_HOSTS`] in `certs/`; the CA as `dev-ca.crt`. +fn dev_certs(dir: &Path) -> Result<(), String> { + use rcgen::{ + BasicConstraints, CertificateParams, DnType, IsCa, Issuer, KeyPair, KeyUsagePurpose, + }; + let e = |x: rcgen::Error| x.to_string(); + let ca_key = KeyPair::generate().map_err(e)?; + let mut ca = CertificateParams::new(Vec::::new()).map_err(e)?; + ca.distinguished_name + .push(DnType::CommonName, "Ianitor dev CA"); + ca.is_ca = IsCa::Ca(BasicConstraints::Constrained(0)); + ca.key_usages = vec![ + KeyUsagePurpose::KeyCertSign, + KeyUsagePurpose::DigitalSignature, + ]; + let ca_cert = ca.self_signed(&ca_key).map_err(e)?; + std::fs::write(dir.join("certs/dev-ca.crt"), ca_cert.pem()).map_err(|x| x.to_string())?; + let issuer = Issuer::new(ca, ca_key); + for h in DEV_HOSTS { + let key = KeyPair::generate().map_err(e)?; + let mut p = CertificateParams::new(vec![h.to_owned()]).map_err(e)?; + p.distinguished_name.push(DnType::CommonName, h); + let cert = p.signed_by(&key, &issuer).map_err(e)?; + std::fs::write(dir.join(format!("certs/{h}.crt")), cert.pem()) + .map_err(|x| x.to_string())?; + write_atomic( + &dir.join(format!("certs/{h}.key")), + key.serialize_pem().as_bytes(), + 0o600, + ) + .map_err(|x| x.to_string())?; + } + Ok(()) +} + const SITE: &str = "# Dev site settings. Sign-in goes to an Authelia that may not run here.\n\ [forward_auth]\nurl = \"http://127.0.0.1:9091/api/authz/forward-auth\"\n"; @@ -90,6 +128,7 @@ pub async fn run(args: &[String]) -> Result<(), String> { ] { std::fs::create_dir_all(dir.join(d)).map_err(|e| e.to_string())?; } + dev_certs(&dir)?; let key = hex(&random::<32>().map_err(|e| e.to_string())?); write_atomic(&dir.join("creds/share-link-key"), key.as_bytes(), 0o600) .map_err(|e| e.to_string())?; @@ -153,9 +192,10 @@ pub async fn run(args: &[String]) -> Result<(), String> { println!("Routes repository: {}", cfg.routes.repo.display()); match &edge_bin { Some(b) => println!( - "Dev proxy: {} on https://127.0.0.1:18443 (HTTP 18080); Domi hosts need certificates in {}", + "Dev proxy: {} on https://127.0.0.1:18443 (HTTP 18080). tls = \"domi\" works for {}; CA: {}", b.display(), - dir.join("certs").display() + DEV_HOSTS.join(", "), + dir.join("certs/dev-ca.crt").display() ), None => println!( "No ianitor-edge found: applies are written but not loaded or watched (use --edge-bin)." diff --git a/crates/control/src/engine.rs b/crates/control/src/engine.rs index 9878cab..925c695 100644 --- a/crates/control/src/engine.rs +++ b/crates/control/src/engine.rs @@ -790,11 +790,12 @@ impl Control { ) .await; } + let push_step = self.push_step(); self.update(&id, |r| { r.step("write", "done"); r.step("reload", "skipped"); r.step("watch", "skipped"); - r.step("push", "pending"); + r.step("push", push_step); }); self.finish( &id, @@ -963,11 +964,21 @@ impl Control { let _ = self.journal(&j); crash::point("promoted"); self.clear_journal(); - self.update(&id, |r| r.step("push", "pending")); + let push_step = self.push_step(); + self.update(&id, |r| r.step("push", push_step)); self.finish(&id, "kept", Some(kept_reason), Vec::new()); self.push_wake.notify_one(); } + /// The push step of a kept apply: pending, or skipped when there is no remote. + fn push_step(&self) -> &'static str { + if self.cfg.routes.remote.is_some() { + "pending" + } else { + "skipped" + } + } + fn promote(&self) -> std::io::Result<()> { for (trial, fin) in [ (self.cfg.trial_path(), self.cfg.edge.config.clone()), @@ -1260,7 +1271,8 @@ impl Control { "confirmed" | "promoted" => { self.promote().map_err(|e| e.to_string())?; self.clear_journal(); - self.update(&j.apply_id, |r| r.step("push", "pending")); + let push_step = self.push_step(); + self.update(&j.apply_id, |r| r.step("push", push_step)); self.finish(&j.apply_id, "kept", None, Vec::new()); } _ => {