diff --git a/crates/control/tests/engine_switch.rs b/crates/control/tests/engine_switch.rs index 83b7889..6653efb 100644 --- a/crates/control/tests/engine_switch.rs +++ b/crates/control/tests/engine_switch.rs @@ -228,6 +228,8 @@ mkdir -p "$dir" echo "$*" >> "{d}/fake/calls.log" case "$1:$2" in start:caddy-fallback.service) + # Like systemd: starting a unit that is active does nothing. + [ -e "$dir/$2" ] && exit 0 XDG_DATA_HOME="{d}/caddy-state" XDG_CONFIG_HOME="{d}/caddy-state" HOME="{d}/caddy-state" \ nohup "{c}" run --config "{d}/caddy/caddy.json" >> "{d}/logs/caddy.log" 2>&1 & echo $! > "{d}/fake/caddy.pid" @@ -236,8 +238,17 @@ case "$1:$2" in IANITOR_LOG=info nohup "{g}" caddy-guard --config "{d}/control.toml" >> "{d}/logs/caddy-guard.log" 2>&1 & echo $! > "{d}/fake/guard.pid" fi - touch "$dir/$2" ;; + # fake/start-delay: Caddy serves at once, the unit reports active only later (a switch + # that is still in wait_serving while Caddy has read its files). + if [ -f "{d}/fake/start-delay" ]; then + (sleep "$(cat "{d}/fake/start-delay")"; touch "$dir/$2") >/dev/null 2>&1 & + else + touch "$dir/$2" + fi ;; stop:caddy-fallback.service) + # fake/stop-delay and fake/fail-stop: a slow stop, and one that fails (Caddy keeps running). + [ -f "{d}/fake/stop-delay" ] && sleep "$(cat "{d}/fake/stop-delay")" + grep -qx "$2" "{d}/fake/fail-stop" 2>/dev/null && exit 1 [ -f "{d}/fake/caddy.pid" ] && kill "$(cat "{d}/fake/caddy.pid")" 2>/dev/null [ -f "{d}/fake/guard.pid" ] && kill "$(cat "{d}/fake/guard.pid")" 2>/dev/null rm -f "$dir/$2" "{d}/fake/caddy.pid" "{d}/fake/guard.pid" ;; @@ -1092,4 +1103,126 @@ esac ); assert!(!env.dir.join("state/caddy-trial.json").exists()); } + + // ---------------------------------------------------------------- fix round 4 + + async fn engine_view(env: &Env) -> Value { + env.call("GET", "/api/v1/engine", None).await.json + } + + async fn wait_switching(env: &Env) { + let deadline = Instant::now() + Duration::from_secs(10); + while engine_view(env).await["state"] != "switching" { + assert!(Instant::now() < deadline, "the switch never started"); + tokio::time::sleep(Duration::from_millis(200)).await; + } + } + + async fn wait_switch_over(env: &Env) -> Value { + let deadline = Instant::now() + Duration::from_secs(40); + loop { + let e = engine_view(env).await; + if e["state"] == "running" { + return e; + } + assert!(Instant::now() < deadline, "the switch never ended: {e}"); + tokio::time::sleep(Duration::from_millis(300)).await; + } + } + + async fn revoke(env: &Env, id: &str) { + let t = Instant::now(); + let d = env + .call("DELETE", &format!("/api/v1/certs/devices/{id}"), None) + .await; + assert_eq!(d.status, 204, "{}", d.text); + assert!( + t.elapsed() < Duration::from_secs(3), + "the DELETE waited {:?}", + t.elapsed() + ); + } + + async fn refused_soon(p: &PkiEnv, at: std::net::SocketAddr, what: &str) { + let deadline = Instant::now() + Duration::from_secs(4); + while p.visit_as_device(at).await == 200 { + assert!(Instant::now() < deadline, "{what}"); + tokio::time::sleep(Duration::from_millis(250)).await; + } + } + + /// QC round 4, case A: a device is revoked while a switch to Caddy is still waiting for + /// the unit, after Caddy has already read the allow list. The switch ends by telling + /// Caddy again, so the device is refused once Caddy serves. + #[tokio::test(flavor = "multi_thread")] + async fn a_revocation_during_a_switch_to_caddy_is_in_force_when_it_ends() { + let Some(p) = pki_env(2, false).await else { + return; + }; + let env = &p.ce.env; + std::fs::write(env.dir.join("fake/start-delay"), "5").unwrap(); + let r = env + .call("POST", "/api/v1/engine", Some(json!({"target": "caddy"}))) + .await; + assert_eq!(r.status, 202, "{}", r.text); + wait_switching(env).await; + // Caddy is up and has read its files; the unit is not reported active yet. + let deadline = Instant::now() + Duration::from_secs(5); + while p.visit_as_device(p.ce.https).await != 200 { + assert!(Instant::now() < deadline, "Caddy did not come up"); + tokio::time::sleep(Duration::from_millis(200)).await; + } + assert_eq!(engine_view(env).await["state"], "switching"); + revoke(env, &p.device_id).await; + let e = wait_switch_over(env).await; + assert_eq!(e["running"], "caddy", "{e}"); + refused_soon( + &p, + p.ce.https, + "the revoked device still gets in under Caddy after the switch", + ) + .await; + } + + /// QC round 4, case B: a switch away from Caddy fails before Caddy is stopped, and a + /// device was revoked meanwhile. The Caddy that kept serving must refuse it. + #[tokio::test(flavor = "multi_thread")] + async fn a_revocation_during_a_failed_switch_away_from_caddy_is_in_force() { + let Some(p) = pki_env(2, true).await else { + return; + }; + let env = &p.ce.env; + assert_eq!( + p.visit_as_device(p.ce.https).await, + 200, + "the device gets in" + ); + std::fs::write(env.dir.join("fake/stop-delay"), "4").unwrap(); + std::fs::write(env.dir.join("fake/fail-stop"), "caddy-fallback.service\n").unwrap(); + let r = env + .call("POST", "/api/v1/engine", Some(json!({"target": "pingora"}))) + .await; + assert_eq!(r.status, 202, "{}", r.text); + wait_switching(env).await; + revoke(env, &p.device_id).await; + let e = wait_switch_over(env).await; + assert_eq!(e["running"], "caddy", "{e}"); + assert!(!e["last_error"].is_null(), "{e}"); + refused_soon( + &p, + p.ce.https, + "the revoked device still gets in under the Caddy that kept serving", + ) + .await; + // Nothing is left pending. + let deadline = Instant::now() + Duration::from_secs(6); + loop { + let c = env.call("GET", "/api/v1/certs", None).await; + if c.json["devices_problem"].is_null() { + break; + } + assert!(Instant::now() < deadline, "{}", c.text); + tokio::time::sleep(Duration::from_millis(500)).await; + } + } }