[edge] Reload and share links: in-process config swap with self-reverting trials, admin socket with health and certificate status, share-link cookies, visitor pages and throttle

This commit is contained in:
2026-11-07 17:49:53 +01:00
parent c0ed9c65c5
commit 6ce1fe9d2b
18 changed files with 2137 additions and 186 deletions
+89 -22
View File
@@ -44,9 +44,14 @@ mod unix {
}
async fn generation(edge: &Edge) -> String {
let r = client::https(edge.https, "app.test", edge.roots(), &Req::get("app.test", "/"))
.await
.unwrap();
let r = client::https(
edge.https,
"app.test",
edge.roots(),
&Req::get("app.test", "/"),
)
.await
.unwrap();
assert_eq!(r.status, 200, "{}", r.text());
r.header("x-generation").unwrap_or("none").to_owned()
}
@@ -118,7 +123,11 @@ mod unix {
let mut reloads = 0u64;
let mut refused = 0u64;
while started.elapsed() < Duration::from_secs(secs) {
let text = if reloads % 2 == 0 { &config_b } else { &config_a };
let text = if reloads.is_multiple_of(2) {
&config_b
} else {
&config_a
};
let tmp = edge.config_path().with_extension("tmp");
std::fs::write(&tmp, text).unwrap();
std::fs::rename(&tmp, edge.config_path()).unwrap();
@@ -237,7 +246,11 @@ mod unix {
// Not confirmed: back to "a" after the deadline, without anyone asking.
let r = edge
.admin("POST", "/v1/trial", Some(json!({ "confirm_within_secs": 2 })))
.admin(
"POST",
"/v1/trial",
Some(json!({ "confirm_within_secs": 2 })),
)
.await;
assert_eq!(r.status, 200, "{}", r.text);
let trial_sha = r.json["config_sha256"].as_str().unwrap().to_owned();
@@ -248,9 +261,13 @@ mod unix {
// No reload and no second trial while one runs.
assert_eq!(edge.admin("POST", "/v1/reload", None).await.status, 409);
assert_eq!(
edge.admin("POST", "/v1/trial", Some(json!({ "confirm_within_secs": 2 })))
.await
.status,
edge.admin(
"POST",
"/v1/trial",
Some(json!({ "confirm_within_secs": 2 }))
)
.await
.status,
409
);
tokio::time::sleep(Duration::from_millis(3500)).await;
@@ -260,21 +277,37 @@ mod unix {
assert_eq!(s.json["config_sha256"], sha256(&edge.config_text));
// Confirming a trial that already reverted fails.
let r = edge
.admin("POST", "/v1/confirm", Some(json!({ "config_sha256": trial_sha })))
.admin(
"POST",
"/v1/confirm",
Some(json!({ "config_sha256": trial_sha })),
)
.await;
assert_eq!(r.status, 409, "{}", r.text);
// Confirmed: stays.
let r = edge
.admin("POST", "/v1/trial", Some(json!({ "confirm_within_secs": 2 })))
.admin(
"POST",
"/v1/trial",
Some(json!({ "confirm_within_secs": 2 })),
)
.await;
assert_eq!(r.status, 200, "{}", r.text);
let wrong = edge
.admin("POST", "/v1/confirm", Some(json!({ "config_sha256": "00" })))
.admin(
"POST",
"/v1/confirm",
Some(json!({ "config_sha256": "00" })),
)
.await;
assert_eq!(wrong.status, 409);
let r = edge
.admin("POST", "/v1/confirm", Some(json!({ "config_sha256": trial_sha })))
.admin(
"POST",
"/v1/confirm",
Some(json!({ "config_sha256": trial_sha })),
)
.await;
assert_eq!(r.status, 200, "{}", r.text);
tokio::time::sleep(Duration::from_millis(2500)).await;
@@ -283,7 +316,11 @@ mod unix {
// Aborted: back at once. Abort is idempotent.
std::fs::write(edge.trial_path(), &edge.config_text).unwrap();
let r = edge
.admin("POST", "/v1/trial", Some(json!({ "confirm_within_secs": 30 })))
.admin(
"POST",
"/v1/trial",
Some(json!({ "confirm_within_secs": 30 })),
)
.await;
assert_eq!(r.status, 200, "{}", r.text);
assert_eq!(generation(&edge).await, "a");
@@ -298,7 +335,11 @@ mod unix {
// A broken trial is refused and nothing changes.
std::fs::write(edge.trial_path(), "{}").unwrap();
let r = edge
.admin("POST", "/v1/trial", Some(json!({ "confirm_within_secs": 30 })))
.admin(
"POST",
"/v1/trial",
Some(json!({ "confirm_within_secs": 30 })),
)
.await;
assert_eq!(r.status, 422);
assert_eq!(generation(&edge).await, "b");
@@ -321,7 +362,10 @@ mod unix {
sick.url()
),
),
host_file("plain.test", &format!("upstreams = [\"{}\"]\n", plain.url())),
host_file(
"plain.test",
&format!("upstreams = [\"{}\"]\n", plain.url()),
),
],
..Default::default()
});
@@ -329,7 +373,10 @@ mod unix {
let s = edge.admin("GET", "/v1/status", None).await;
assert_eq!(s.status, 200, "{}", s.text);
let hosts = s.json["hosts"].as_array().unwrap();
let mixed = hosts.iter().find(|h| h["hostname"] == "mixed.test").unwrap();
let mixed = hosts
.iter()
.find(|h| h["hostname"] == "mixed.test")
.unwrap();
assert_eq!(mixed["health_check"], true);
let ups = mixed["upstreams"].as_array().unwrap();
assert_eq!(ups.len(), 3);
@@ -344,9 +391,17 @@ mod unix {
assert_eq!(last(&ups[2])["kind"], "refused");
assert!(ups[0]["checks"].as_array().unwrap().len() <= 5);
assert!(mixed["certificate"]["state"] == "ok");
let plain_h = hosts.iter().find(|h| h["hostname"] == "plain.test").unwrap();
let plain_h = hosts
.iter()
.find(|h| h["hostname"] == "plain.test")
.unwrap();
assert_eq!(plain_h["health_check"], false);
assert!(plain_h["upstreams"][0]["checks"].as_array().unwrap().is_empty());
assert!(
plain_h["upstreams"][0]["checks"]
.as_array()
.unwrap()
.is_empty()
);
// During a trial, watched hosts without a health check get `GET /` probes; any answer
// below 500 counts as alive. Hosts with one are checked at least every `every_secs`.
@@ -372,8 +427,12 @@ mod unix {
assert!(!checks.is_empty(), "{}", s.text);
assert_eq!(checks.last().unwrap()["ok"], true);
assert_eq!(checks.last().unwrap()["source"], "watch");
assert!(plain.requests().iter().any(|r| r.path == "/"
&& r.header("host") == Some(b"plain.test".as_slice())));
assert!(
plain
.requests()
.iter()
.any(|r| r.path == "/" && r.header("host") == Some(b"plain.test".as_slice()))
);
edge.admin("POST", "/v1/abort", None).await;
}
@@ -391,11 +450,19 @@ mod unix {
// Unknown paths and bad bodies get typed errors, not a crash.
assert_eq!(edge.admin("GET", "/v1/nope", None).await.status, 404);
let r = edge
.admin("POST", "/v1/trial", Some(json!({ "confirm_within_secs": 2, "extra": 1 })))
.admin(
"POST",
"/v1/trial",
Some(json!({ "confirm_within_secs": 2, "extra": 1 })),
)
.await;
assert_eq!(r.status, 400, "{}", r.text);
let r = edge
.admin("POST", "/v1/trial", Some(json!({ "confirm_within_secs": 0 })))
.admin(
"POST",
"/v1/trial",
Some(json!({ "confirm_within_secs": 0 })),
)
.await;
assert_eq!(r.status, 400, "{}", r.text);
assert_eq!(generation(&edge).await, "a");
+66 -20
View File
@@ -107,11 +107,7 @@ mod unix {
}
async fn post_password(&self, token: &str, password: &str) -> Resp {
let body = format!(
"t={}&password={}",
urlencode(token),
urlencode(password)
);
let body = format!("t={}&password={}", urlencode(token), urlencode(password));
let req = Req::new("POST", HOST, "/.well-known/ianitor/share")
.header("content-type", "application/x-www-form-urlencoded")
.body(body.as_bytes());
@@ -159,7 +155,10 @@ mod unix {
);
assert!(r.header("content-type").unwrap().starts_with("text/html"));
assert!(!r.text().contains("<script"), "no JavaScript");
assert!(!r.text().contains("http://") && !r.text().contains("https://"), "no external requests");
assert!(
!r.text().contains("http://") && !r.text().contains("https://"),
"no external requests"
);
assert!(r.body.len() < 8 * 1024);
}
@@ -180,19 +179,36 @@ mod unix {
.find(|c| c.starts_with("__Secure-ianitor-share="))
.unwrap()
.to_owned();
for attr in ["Path=/albums", "HttpOnly", "Secure", "SameSite=Lax", "Max-Age="] {
for attr in [
"Path=/albums",
"HttpOnly",
"Secure",
"SameSite=Lax",
"Max-Age=",
] {
assert!(set.contains(attr), "{attr} missing in {set}");
}
assert!(!set.contains(&t), "the cookie is not the link token");
assert!(e.upstream_paths().is_empty(), "the token request never reached the upstream");
assert!(
e.upstream_paths().is_empty(),
"the token request never reached the upstream"
);
let auth_calls = e.auth.requests().len();
let cookie = share_cookie(&r);
let r = e
.get(HOST, "/albums/summer?x=1&y=2", Some(&format!("{cookie}; other=1")))
.get(
HOST,
"/albums/summer?x=1&y=2",
Some(&format!("{cookie}; other=1")),
)
.await;
assert_eq!(r.status, 200, "{}", r.text());
assert_eq!(e.auth.requests().len(), auth_calls, "no forward-auth for a share visitor");
assert_eq!(
e.auth.requests().len(),
auth_calls,
"no forward-auth for a share visitor"
);
let seen = e.echo.proxied();
let last = seen.last().unwrap();
assert_eq!(last.path, "/albums/summer");
@@ -200,20 +216,32 @@ mod unix {
.header("cookie")
.map(|c| String::from_utf8_lossy(c).into_owned())
.unwrap_or_default();
assert!(!cookie_sent.contains("ianitor-share"), "share cookie stripped: {cookie_sent}");
assert!(cookie_sent.contains("other=1"), "other cookies kept: {cookie_sent}");
assert!(
!cookie_sent.contains("ianitor-share"),
"share cookie stripped: {cookie_sent}"
);
assert!(
cookie_sent.contains("other=1"),
"other cookies kept: {cookie_sent}"
);
assert!(last.header("referer").is_none());
// The prefix itself and deeper paths are covered.
assert_eq!(e.get(HOST, "/albums", Some(&cookie)).await.status, 200);
assert_eq!(e.get(HOST, "/albums/a/b/c.jpg", Some(&cookie)).await.status, 200);
assert_eq!(
e.get(HOST, "/albums/a/b/c.jpg", Some(&cookie)).await.status,
200
);
// Neither the token nor the cookie appears in any log.
tokio::time::sleep(Duration::from_millis(200)).await;
let logs = e.edge.all_output();
assert!(!logs.contains(&t[5..40]), "token in logs");
assert!(!logs.contains(&cookie[30..60]), "cookie in logs");
assert!(!e.edge.access_log().contains("share="), "query in access log");
assert!(
!e.edge.access_log().contains("share="),
"query in access log"
);
for r in e.echo.requests() {
for (_, v) in &r.headers {
assert!(!String::from_utf8_lossy(v).contains(&t[5..40]));
@@ -309,7 +337,11 @@ mod unix {
// The cookie of a password link cannot be turned into a link that skips the password.
let v = cookie.split_once('=').unwrap().1.to_owned();
let r = e
.get(HOST, &format!("/docs?share={}", v.replacen("iss1.", "ish1.", 1)), None)
.get(
HOST,
&format!("/docs?share={}", v.replacen("iss1.", "ish1.", 1)),
None,
)
.await;
assert_ne!(r.status, 303);
@@ -384,16 +416,25 @@ mod unix {
assert_eq!(e.get(HOST, "/", Some(&cookie)).await.status, 200);
e.edge.write_revocations(0, &[ShareId([12; 16]).to_hex()]);
let r = e.edge.admin("POST", "/v1/share-revocations/reload", None).await;
let r = e
.edge
.admin("POST", "/v1/share-revocations/reload", None)
.await;
assert_eq!(r.status, 200, "{}", r.text);
assert_eq!(e.get(HOST, "/", Some(&cookie)).await.status, 302, "back to sign-in");
assert_eq!(
e.get(HOST, "/", Some(&cookie)).await.status,
302,
"back to sign-in"
);
let r = e.get(HOST, &format!("/?share={t}"), None).await;
assert_visitor_page(&r, 403, "This link no longer works");
assert_eq!(e.get(HOST, "/", Some(&keep_cookie)).await.status, 200);
// Stop all: a new epoch ends every older link.
e.edge.write_revocations(1, &[]);
e.edge.admin("POST", "/v1/share-revocations/reload", None).await;
e.edge
.admin("POST", "/v1/share-revocations/reload", None)
.await;
assert_eq!(e.get(HOST, "/", Some(&keep_cookie)).await.status, 302);
// Without the admin call, edge notices a changed file within 5 s.
@@ -403,11 +444,16 @@ mod unix {
// A broken file keeps the last good state; a missing one stops all share access.
std::fs::write(e.edge.revocations_path(), "{ broken").unwrap();
let r = e.edge.admin("POST", "/v1/share-revocations/reload", None).await;
let r = e
.edge
.admin("POST", "/v1/share-revocations/reload", None)
.await;
assert_eq!(r.status, 422);
assert_eq!(e.get(HOST, "/", Some(&keep_cookie)).await.status, 200);
std::fs::remove_file(e.edge.revocations_path()).unwrap();
e.edge.admin("POST", "/v1/share-revocations/reload", None).await;
e.edge
.admin("POST", "/v1/share-revocations/reload", None)
.await;
assert_eq!(e.get(HOST, "/", Some(&keep_cookie)).await.status, 302);
}
}