[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:
Generated
-1
@@ -1301,7 +1301,6 @@ dependencies = [
|
|||||||
"log",
|
"log",
|
||||||
"p12-keystore",
|
"p12-keystore",
|
||||||
"qrcode",
|
"qrcode",
|
||||||
"rcgen",
|
|
||||||
"ring",
|
"ring",
|
||||||
"rpassword",
|
"rpassword",
|
||||||
"rustls",
|
"rustls",
|
||||||
|
|||||||
@@ -35,7 +35,6 @@ ring.workspace = true
|
|||||||
base64.workspace = true
|
base64.workspace = true
|
||||||
http.workspace = true
|
http.workspace = true
|
||||||
bytes.workspace = true
|
bytes.workspace = true
|
||||||
rcgen.workspace = true
|
|
||||||
# Device certificate install files (PKCS#12) and their QR codes.
|
# Device certificate install files (PKCS#12) and their QR codes.
|
||||||
p12-keystore = "0.4"
|
p12-keystore = "0.4"
|
||||||
qrcode = { version = "0.14", default-features = false, features = ["svg"] }
|
qrcode = { version = "0.14", default-features = false, features = ["svg"] }
|
||||||
|
|||||||
+44
-42
@@ -18,42 +18,16 @@ use ianitor_control::repo::Repo;
|
|||||||
use ianitor_control::tokens::Tokens;
|
use ianitor_control::tokens::Tokens;
|
||||||
use ianitor_control::util::{hex, random, write_atomic};
|
use ianitor_control::util::{hex, random, write_atomic};
|
||||||
|
|
||||||
/// Hosts that get a certificate from the dev CA, so `tls = "domi"` works for them.
|
/// A throw-away Domi root for dev mode, in `offline-root/` (its key stays in memory, like
|
||||||
const DEV_HOSTS: [&str; 3] = ["app.dev.test", "wiki.dev.test", "photos.dev.test"];
|
/// on the offline machine), and the PKI password credential. Control then works like
|
||||||
|
/// production: it issues server certificates for every `tls = "domi"` host.
|
||||||
/// A throw-away CA and certificates for [`DEV_HOSTS`] in `certs/`; the CA as `dev-ca.crt`.
|
fn dev_pki(dir: &Path) -> Result<(String, ianitor_core::pki::KeyPair), String> {
|
||||||
fn dev_certs(dir: &Path) -> Result<(), String> {
|
let (root, key) =
|
||||||
use rcgen::{
|
ianitor_core::pki::create_root(ianitor_control::util::now() as i64).map_err(|e| e.to_string())?;
|
||||||
BasicConstraints, CertificateParams, DnType, IsCa, Issuer, KeyPair, KeyUsagePurpose,
|
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())?);
|
||||||
let e = |x: rcgen::Error| x.to_string();
|
write_atomic(&dir.join("creds/pki-password"), pass.as_bytes(), 0o600).map_err(|e| e.to_string())?;
|
||||||
let ca_key = KeyPair::generate().map_err(e)?;
|
Ok((root, key))
|
||||||
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(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const SITE: &str = "# Dev site settings. Sign-in goes to an Authelia that may not run here.\n\
|
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",
|
"creds",
|
||||||
"logs",
|
"logs",
|
||||||
"edge-state",
|
"edge-state",
|
||||||
|
"offline-root",
|
||||||
] {
|
] {
|
||||||
std::fs::create_dir_all(dir.join(d)).map_err(|e| e.to_string())?;
|
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())?);
|
let key = hex(&random::<32>().map_err(|e| e.to_string())?);
|
||||||
write_atomic(&dir.join("creds/share-link-key"), key.as_bytes(), 0o600)
|
write_atomic(&dir.join("creds/share-link-key"), key.as_bytes(), 0o600)
|
||||||
.map_err(|e| e.to_string())?;
|
.map_err(|e| e.to_string())?;
|
||||||
@@ -174,8 +149,17 @@ pub async fn run(args: &[String]) -> Result<(), String> {
|
|||||||
caddy_admin: None,
|
caddy_admin: None,
|
||||||
},
|
},
|
||||||
compile_caddy: None,
|
compile_caddy: None,
|
||||||
pki: None,
|
pki: Some(ianitor_control::config::PkiConfig {
|
||||||
ui: None,
|
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,
|
engine: None,
|
||||||
};
|
};
|
||||||
cfg.check().map_err(|e| e.to_string())?;
|
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.
|
// Control writes edge.json first; then edge can start from it.
|
||||||
let ctl_cfg = cfg.clone();
|
let ctl_cfg = cfg.clone();
|
||||||
let opened = ianitor_control::Control::open(ctl_cfg).await?;
|
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);
|
drop(opened);
|
||||||
if let Some(bin) = &edge_bin {
|
if let Some(bin) = &edge_bin {
|
||||||
cleanup.edge = Some(start_edge(bin, &dir, &cfg)?);
|
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());
|
println!("Routes repository: {}", cfg.routes.repo.display());
|
||||||
match &edge_bin {
|
match &edge_bin {
|
||||||
Some(b) => println!(
|
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(),
|
b.display(),
|
||||||
DEV_HOSTS.join(", "),
|
dir.join("offline-root/domi-root.crt").display()
|
||||||
dir.join("certs/dev-ca.crt").display()
|
|
||||||
),
|
),
|
||||||
None => println!(
|
None => println!(
|
||||||
"No ianitor-edge found: applies are written but not loaded or watched (use --edge-bin)."
|
"No ianitor-edge found: applies are written but not loaded or watched (use --edge-bin)."
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
println!("API token (shown once): {token}");
|
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());
|
println!("Press Ctrl-C to stop and remove {}", dir.display());
|
||||||
let r = ianitor_control::serve(cfg, ianitor_control::stop_signal()).await;
|
let r = ianitor_control::serve(cfg, ianitor_control::stop_signal()).await;
|
||||||
drop(cleanup);
|
drop(cleanup);
|
||||||
|
|||||||
Reference in New Issue
Block a user