F: exercise shutdown and a slow-loris peer for real
The hand-written listener now distinguishes a complete reply, SSE headers followed by a held socket and headers followed by one body byte every 300 ms. shutdown_ends_a_proxied_follow uses the held socket instead of a real peer, so only shutdown can end the stream, and a new trickle case proves the request deadline ends a slow-loris peer (503 timeout) rather than the peer's goodwill. The silent-peer bound drops from 10 s to 4 s so a missed 1 s deadline cannot hide inside it.
This commit is contained in:
1 parent
30ca2010ea
commit
a3b3958add
1 file changed
+93
-21
+93
-21
@@ -312,10 +312,20 @@ impl Ca {
|
||||
}
|
||||
}
|
||||
|
||||
/// What a hand-written listener does with one accepted connection.
|
||||
enum Answer {
|
||||
/// The whole response; the socket closes afterwards.
|
||||
Reply(Vec<u8>),
|
||||
/// Status line and headers, then the socket stays open and silent.
|
||||
Hold(Vec<u8>),
|
||||
/// Status line and headers, then one body byte every `every`.
|
||||
Trickle { head: Vec<u8>, every: Duration },
|
||||
}
|
||||
|
||||
/// A hand-written listener for the cases a real `slinuxm` cannot produce.
|
||||
///
|
||||
/// `respond` returns the raw response for each accepted connection, or
|
||||
/// `None` to accept and answer nothing.
|
||||
/// `respond` returns the answer for each accepted connection, or `None` to
|
||||
/// accept and answer nothing.
|
||||
struct Listener {
|
||||
port: u16,
|
||||
hits: Arc<AtomicUsize>,
|
||||
@@ -325,7 +335,7 @@ struct Listener {
|
||||
impl Listener {
|
||||
async fn start<F>(respond: F) -> Self
|
||||
where
|
||||
F: Fn() -> Option<Vec<u8>> + Send + Sync + 'static,
|
||||
F: Fn() -> Option<Answer> + Send + Sync + 'static,
|
||||
{
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let port = listener.local_addr().unwrap().port();
|
||||
@@ -335,7 +345,6 @@ impl Listener {
|
||||
let counter = hits.clone();
|
||||
let cancelled = stop.clone();
|
||||
tokio::spawn(async move {
|
||||
let mut held: Vec<tokio::net::TcpStream> = Vec::new();
|
||||
loop {
|
||||
let accepted = tokio::select! {
|
||||
_ = cancelled.cancelled() => return,
|
||||
@@ -345,16 +354,39 @@ impl Listener {
|
||||
return;
|
||||
};
|
||||
counter.fetch_add(1, Ordering::SeqCst);
|
||||
let Some(body) = respond() else {
|
||||
held.push(sock);
|
||||
continue;
|
||||
};
|
||||
// `None` means "accept and say nothing": headers with no bytes,
|
||||
// which leaves the client waiting.
|
||||
let answer = respond().unwrap_or(Answer::Hold(Vec::new()));
|
||||
let cancelled = cancelled.clone();
|
||||
tokio::spawn(async move {
|
||||
// Read the head first so the client is not reset mid-send.
|
||||
let mut buf = [0u8; 1024];
|
||||
let _ =
|
||||
tokio::time::timeout(Duration::from_millis(500), sock.read(&mut buf)).await;
|
||||
let _ = sock.write_all(&body).await;
|
||||
match answer {
|
||||
Answer::Reply(body) => {
|
||||
let _ = sock.write_all(&body).await;
|
||||
}
|
||||
Answer::Hold(head) => {
|
||||
let _ = sock.write_all(&head).await;
|
||||
tokio::select! {
|
||||
_ = cancelled.cancelled() => {}
|
||||
_ = tokio::time::sleep(Duration::from_secs(60)) => {}
|
||||
}
|
||||
}
|
||||
Answer::Trickle { head, every } => {
|
||||
let _ = sock.write_all(&head).await;
|
||||
loop {
|
||||
tokio::select! {
|
||||
_ = cancelled.cancelled() => return,
|
||||
_ = tokio::time::sleep(every) => {}
|
||||
}
|
||||
if sock.write_all(b" ").await.is_err() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -552,14 +584,14 @@ async fn a_peer_signed_by_another_ca_is_a_tls_error() {
|
||||
#[tokio::test]
|
||||
async fn a_redirect_is_reported_and_never_followed() {
|
||||
let dir = TempDir::new("redirect");
|
||||
let target = Listener::start(|| Some(response("200 OK", "", b"{}"))).await;
|
||||
let target = Listener::start(|| Some(Answer::Reply(response("200 OK", "", b"{}")))).await;
|
||||
let location = format!("{}/elsewhere", target.url("http"));
|
||||
let source = Listener::start(move || {
|
||||
Some(response(
|
||||
Some(Answer::Reply(response(
|
||||
"302 Found",
|
||||
&format!("Location: {location}\r\n"),
|
||||
b"moved",
|
||||
))
|
||||
)))
|
||||
})
|
||||
.await;
|
||||
let token = dir.token("peer-token", "redirect-token\n");
|
||||
@@ -584,7 +616,8 @@ async fn a_redirect_is_reported_and_never_followed() {
|
||||
#[tokio::test]
|
||||
async fn an_oversize_body_is_refused() {
|
||||
let dir = TempDir::new("oversize");
|
||||
let source = Listener::start(|| Some(response("200 OK", "", &[b'x'; 4096]))).await;
|
||||
let source =
|
||||
Listener::start(|| Some(Answer::Reply(response("200 OK", "", &[b'x'; 4096])))).await;
|
||||
let token = dir.token("peer-token", "big-token\n");
|
||||
let mut cfg = one_peer("beta", source.url("http"), None, &token);
|
||||
cfg.max_response_bytes = 64;
|
||||
@@ -614,7 +647,41 @@ async fn a_silent_peer_times_out_within_the_request_timeout() {
|
||||
let elapsed = started.elapsed();
|
||||
assert_eq!(code, 503, "{body}");
|
||||
assert!(body.contains("timeout"), "{body}");
|
||||
assert!(elapsed < Duration::from_secs(10), "took {elapsed:?}");
|
||||
// With a 1 s timeout, a 10 s bound would hide a deadline that never fired.
|
||||
assert!(elapsed < Duration::from_secs(4), "took {elapsed:?}");
|
||||
consolidator.stop().await;
|
||||
}
|
||||
|
||||
/// Slow loris: the peer answers the headers at once and then drips the body, so
|
||||
/// only the request deadline can end the call.
|
||||
#[tokio::test]
|
||||
async fn a_trickling_peer_times_out_within_the_request_timeout() {
|
||||
let dir = TempDir::new("trickle");
|
||||
let head =
|
||||
b"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 1000000\r\n\r\n"
|
||||
.to_vec();
|
||||
let every = Duration::from_millis(300);
|
||||
let source = Listener::start(move || {
|
||||
Some(Answer::Trickle {
|
||||
head: head.clone(),
|
||||
every,
|
||||
})
|
||||
})
|
||||
.await;
|
||||
let token = dir.token("peer-token", "trickle-token\n");
|
||||
let mut cfg = one_peer("beta", source.url("http"), None, &token);
|
||||
cfg.request_timeout_secs = 1;
|
||||
let consolidator = Consolidator::start(cfg).await;
|
||||
|
||||
let started = std::time::Instant::now();
|
||||
let (code, _, body) = consolidator
|
||||
.get("/api/v1/peers/beta/systemd/units", None)
|
||||
.await;
|
||||
let elapsed = started.elapsed();
|
||||
assert_eq!(code, 503, "{body}");
|
||||
assert!(body.contains("timeout"), "{body}");
|
||||
assert!(elapsed < Duration::from_secs(4), "took {elapsed:?}");
|
||||
assert!(source.hits() > 0);
|
||||
consolidator.stop().await;
|
||||
}
|
||||
|
||||
@@ -644,15 +711,20 @@ fn at_most_64_peers_may_be_configured() {
|
||||
assert!(err.contains("64"), "{err}");
|
||||
}
|
||||
|
||||
/// The peer answers any path with SSE headers and then holds the socket, so
|
||||
/// nothing but shutdown can end the proxied stream.
|
||||
#[tokio::test]
|
||||
async fn shutdown_ends_a_proxied_follow() {
|
||||
let dir = TempDir::new("follow");
|
||||
let peer = Peer::start().await;
|
||||
let token = peer.token_file(&dir);
|
||||
let consolidator = Consolidator::start(one_peer("beta", peer.url("http"), None, &token)).await;
|
||||
consolidator
|
||||
.wait_status("beta", |s| s.server.up && s.api.up)
|
||||
.await;
|
||||
let source = Listener::start(|| {
|
||||
Some(Answer::Hold(
|
||||
b"HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\n\r\n".to_vec(),
|
||||
))
|
||||
})
|
||||
.await;
|
||||
let token = dir.token("peer-token", "follow-token\n");
|
||||
let consolidator =
|
||||
Consolidator::start(one_peer("beta", source.url("http"), None, &token)).await;
|
||||
|
||||
let raw = get_raw(
|
||||
"/api/v1/peers/beta/journal/follow",
|
||||
@@ -683,5 +755,5 @@ async fn shutdown_ends_a_proxied_follow() {
|
||||
.expect("the server did not drain after shutdown")
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
peer.stop().await;
|
||||
assert!(source.hits() > 0, "the peer was never called");
|
||||
}
|
||||
Reference in new issue
Block a user