[tests] Tests first, fix round 1: proof only on the forward-auth UI host, revocations that cannot be written, root key files by content, intermediates that cannot issue, stale SNI after a failed or resumed handshake, edge re-reads the allow list, changes apply while Caddy serves, Hostname focus (red)

This commit is contained in:
2026-11-08 10:26:31 +01:00
parent e2a777882c
commit 0749119f19
6 changed files with 690 additions and 11 deletions
+191
View File
@@ -536,4 +536,195 @@ mod unix {
fn contains(hay: &[u8], needle: &[u8]) -> bool {
!needle.is_empty() && hay.windows(needle.len()).any(|w| w == needle)
}
/// QC: the openssl step can produce intermediates that are CA + pathlen 0 but cannot
/// issue what control issues. Control installs them and reissues every Domi host.
#[tokio::test(flavor = "multi_thread")]
async fn qc_install_refuses_intermediates_that_cannot_issue() {
let Some(openssl) = ianitor_testkit::proc::require_binary("openssl") else {
return;
};
let off = ianitor_testkit::proc::TempDir::new("qc-openssl-root");
let run = |args: &[&str]| {
let o = std::process::Command::new(&openssl)
.args(args)
.current_dir(off.join("."))
.output()
.unwrap();
assert!(
o.status.success(),
"openssl {args:?}: {}",
String::from_utf8_lossy(&o.stderr)
);
};
run(&[
"req",
"-x509",
"-new",
"-newkey",
"ec",
"-pkeyopt",
"ec_paramgen_curve:P-256",
"-nodes",
"-keyout",
"domi-root.key",
"-out",
"domi-root.crt",
"-days",
"3653",
"-subj",
"/CN=Domi PKI Root/O=Fabrica Domi",
"-addext",
"basicConstraints=critical,CA:TRUE,pathlen:1",
"-addext",
"keyUsage=critical,keyCertSign,cRLSign",
]);
let root_pem = std::fs::read_to_string(off.join("domi-root.crt")).unwrap();
let env = env_with_root(&root_pem).await;
let good = "basicConstraints = critical, CA:TRUE, pathlen:0\nkeyUsage = critical, keyCertSign, cRLSign\n";
let cases = [
(
"keyUsage without keyCertSign",
"basicConstraints = critical, CA:TRUE, pathlen:0\nkeyUsage = critical, digitalSignature\n",
"365",
),
(
"EKU serverAuth only (devices cannot chain)",
"basicConstraints = critical, CA:TRUE, pathlen:0\nkeyUsage = critical, keyCertSign, cRLSign\nextendedKeyUsage = serverAuth\n",
"365",
),
("valid past the root (-days 4000)", good, "4000"),
];
let mut accepted = Vec::new();
for (label, ext, days) in cases {
let r = env
.call(
"POST",
"/api/v1/certs/intermediate/renewal",
Some(json!({})),
)
.await;
assert_eq!(r.status, 202, "{}", r.text);
let rid = r.json["request_id"].as_str().unwrap().to_owned();
std::fs::write(off.join("q.csr"), r.json["csr_pem"].as_str().unwrap()).unwrap();
std::fs::write(off.join("q.ext"), ext).unwrap();
run(&[
"x509",
"-req",
"-in",
"q.csr",
"-CA",
"domi-root.crt",
"-CAkey",
"domi-root.key",
"-days",
days,
"-sha256",
"-extfile",
"q.ext",
"-out",
"q.crt",
]);
let signed = std::fs::read_to_string(off.join("q.crt")).unwrap();
let r = env
.call(
"PUT",
&format!("/api/v1/pki-requests/{rid}/certificate"),
Some(json!({"certificate_pem": signed})),
)
.await;
eprintln!("QC {label}: {} {}", r.status, r.json["state"]);
if r.status == 200 {
accepted.push(label);
}
}
assert!(
accepted.is_empty(),
"control installed unusable intermediates: {accepted:?}"
);
}
/// QC: the root key name that `ianitor-control ca init` and docs/pki.md's openssl
/// command write is `domi-root.key`.
#[test]
fn qc_root_key_check_finds_the_names_ianitor_itself_uses() {
let d = ianitor_testkit::proc::TempDir::new("qc-rootkey");
let mut missed = Vec::new();
for name in [
"domi-root.key",
"offline/domi-root.key",
"Domi_Root_CA.pem",
"ca.key",
] {
let p = d.join(name);
std::fs::create_dir_all(p.parent().unwrap()).unwrap();
std::fs::write(
&p,
// (Split so that this file does not match the pre-commit hook itself.)
concat!("-----BEGIN ", "PRIVATE KEY-----\nx\n-----END ", "PRIVATE KEY-----\n"),
)
.unwrap();
if ianitor_control::pki::root_key_files(&d.join(".")).is_empty() {
missed.push(name);
}
std::fs::remove_file(&p).unwrap();
}
assert!(missed.is_empty(), "root_key_files missed: {missed:?}");
}
/// QC M2: a revocation that cannot be written (or not told to edge) must not answer 204 and
/// be forgotten. It answers an error, the certificates screen says so, and once the disk
/// works again (here: a restart without the fault) the allow list catches up; a repeated
/// DELETE nudges again.
#[tokio::test(flavor = "multi_thread")]
async fn a_revocation_that_cannot_be_written_is_an_error_and_catches_up() {
let (mut env, off) = env_with_pki().await;
install_intermediate(&env, &off).await;
env.kill_control();
env.start_control_env(None, &[("IANITOR_TEST_FAIL_WRITE", "domi-devices.pem")]);
let r = env
.call(
"POST",
"/api/v1/certs/devices",
Some(json!({"name": "tablet", "kind": "phone"})),
)
.await;
assert_eq!(r.status, 202, "{}", r.text);
let rid = r.json["request_id"].as_str().unwrap().to_owned();
let code = r.json["match_code"].as_str().unwrap().to_owned();
assert!(cli(&env, &["pki", "approve", &code]).status.success());
let done = wait_request(&env, &rid, "approved").await;
let did = done["device_id"].as_str().unwrap().to_owned();
// The allow list could not be written: the screen says so.
let deadline = Instant::now() + Duration::from_secs(10);
loop {
let c = env.call("GET", "/api/v1/certs", None).await;
if !c.json["devices_problem"].is_null() {
break;
}
assert!(Instant::now() < deadline, "no problem shown: {}", c.text);
tokio::time::sleep(Duration::from_millis(200)).await;
}
let d = env
.call("DELETE", &format!("/api/v1/certs/devices/{did}"), None)
.await;
assert_eq!(d.status, 503, "{}", d.text);
assert_eq!(
d.json["error"]["code"], "pki.devices_unwritten",
"{}",
d.text
);
// The disk works again.
env.kill_control();
env.start_control(None);
let d = env
.call("DELETE", &format!("/api/v1/certs/devices/{did}"), None)
.await;
assert_eq!(d.status, 204, "{}", d.text);
let allow =
std::fs::read_to_string(env.dir.join("certs/domi-devices.pem")).unwrap_or_default();
assert!(!allow.contains("BEGIN CERTIFICATE"), "{allow}");
let c = env.call("GET", "/api/v1/certs", None).await;
assert!(c.json["devices_problem"].is_null(), "{}", c.text);
}
}