[control] Dev mode: a throw-away Domi root signs the first intermediate at start, so tls = "domi" works for any name; --ui-dir serves a built UI

This commit is contained in:
2026-11-07 23:24:45 +01:00
parent 6237e721d6
commit f9dd2247f8
3 changed files with 44 additions and 44 deletions
-1
View File
@@ -35,7 +35,6 @@ ring.workspace = true
base64.workspace = true
http.workspace = true
bytes.workspace = true
rcgen.workspace = true
# Device certificate install files (PKCS#12) and their QR codes.
p12-keystore = "0.4"
qrcode = { version = "0.14", default-features = false, features = ["svg"] }
+44 -42
View File
@@ -18,42 +18,16 @@ 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::<String>::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(())
/// A throw-away Domi root for dev mode, in `offline-root/` (its key stays in memory, like
/// on the offline machine), and the PKI password credential. Control then works like
/// production: it issues server certificates for every `tls = "domi"` host.
fn dev_pki(dir: &Path) -> Result<(String, ianitor_core::pki::KeyPair), String> {
let (root, key) =
ianitor_core::pki::create_root(ianitor_control::util::now() as i64).map_err(|e| e.to_string())?;
std::fs::write(dir.join("offline-root/domi-root.crt"), &root).map_err(|e| e.to_string())?;
let pass = hex(&random::<24>().map_err(|e| e.to_string())?);
write_atomic(&dir.join("creds/pki-password"), pass.as_bytes(), 0o600).map_err(|e| e.to_string())?;
Ok((root, key))
}
const SITE: &str = "# Dev site settings. Sign-in goes to an Authelia that may not run here.\n\
@@ -125,10 +99,11 @@ pub async fn run(args: &[String]) -> Result<(), String> {
"creds",
"logs",
"edge-state",
"offline-root",
] {
std::fs::create_dir_all(dir.join(d)).map_err(|e| e.to_string())?;
}
dev_certs(&dir)?;
let (root_pem, root_key) = dev_pki(&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())?;
@@ -174,8 +149,17 @@ pub async fn run(args: &[String]) -> Result<(), String> {
caddy_admin: None,
},
compile_caddy: None,
pki: None,
ui: None,
pki: Some(ianitor_control::config::PkiConfig {
root_cert: dir.join("offline-root/domi-root.crt"),
password_credential: "pki-password".into(),
approval: "manual".into(),
kdf_iterations: 10_000,
}),
ui: arg(args, "--ui-dir").map(|d| ianitor_control::config::UiConfig {
hostname: None,
proof_credential: None,
static_dir: Some(PathBuf::from(d)),
}),
engine: None,
};
cfg.check().map_err(|e| e.to_string())?;
@@ -186,6 +170,21 @@ pub async fn run(args: &[String]) -> Result<(), String> {
// Control writes edge.json first; then edge can start from it.
let ctl_cfg = cfg.clone();
let opened = ianitor_control::Control::open(ctl_cfg).await?;
// Dev only: sign the first intermediate with the throw-away root at once (in production
// this is the offline step in docs/pki.md).
if let Some(p) = &opened.pki {
let now = ianitor_control::util::now();
let r = p.request_renewal(now, "dev").map_err(|e| e.to_string())?;
let signed = ianitor_core::pki::sign_intermediate(
&root_pem,
&root_key,
r.csr_pem.as_deref().unwrap_or_default(),
now as i64,
)
.map_err(|e| e.to_string())?;
p.install_intermediate(&r.id, &signed, now)
.map_err(|e| e.to_string())?;
}
drop(opened);
if let Some(bin) = &edge_bin {
cleanup.edge = Some(start_edge(bin, &dir, &cfg)?);
@@ -195,16 +194,19 @@ 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). tls = \"domi\" works for {}; CA: {}",
"Dev proxy: {} on https://127.0.0.1:18443 (HTTP 18080). tls = \"domi\" works for any name; Domi root: {}",
b.display(),
DEV_HOSTS.join(", "),
dir.join("certs/dev-ca.crt").display()
dir.join("offline-root/domi-root.crt").display()
),
None => println!(
"No ianitor-edge found: applies are written but not loaded or watched (use --edge-bin)."
),
}
println!("API token (shown once): {token}");
println!(
"Approve a device request: ianitor-control pki approve <code> --state-dir {}",
dir.join("state").display()
);
println!("Press Ctrl-C to stop and remove {}", dir.display());
let r = ianitor_control::serve(cfg, ianitor_control::stop_signal()).await;
drop(cleanup);