763 lines
24 KiB
Rust
763 lines
24 KiB
Rust
//! Native TLS tests: the real `slinuxm` binary serving HTTPS with
|
|
//! generated self-signed certificates.
|
|
|
|
use std::path::{Path, PathBuf};
|
|
use std::process::{Child, Command, Stdio};
|
|
use std::sync::Arc;
|
|
use std::time::{Duration, Instant};
|
|
|
|
use rustls::pki_types::pem::PemObject;
|
|
use rustls::pki_types::{CertificateDer, ServerName};
|
|
use sha2::{Digest, Sha256};
|
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
|
use tokio::net::TcpStream;
|
|
use tokio_rustls::TlsConnector;
|
|
|
|
mod common;
|
|
|
|
const BIN: &str = env!("CARGO_BIN_EXE_slinuxm");
|
|
|
|
struct Pair {
|
|
cert_pem: String,
|
|
key_pem: String,
|
|
}
|
|
|
|
fn gen_pair() -> Pair {
|
|
let ck = rcgen::generate_simple_self_signed(vec!["localhost".to_owned(), "127.0.0.1".into()])
|
|
.unwrap();
|
|
Pair {
|
|
cert_pem: ck.cert.pem(),
|
|
key_pem: ck.signing_key.serialize_pem(),
|
|
}
|
|
}
|
|
|
|
fn write_pair(dir: &Path, p: &Pair) -> (PathBuf, PathBuf) {
|
|
let (c, k) = (dir.join("cert.pem"), dir.join("key.pem"));
|
|
std::fs::write(&c, &p.cert_pem).unwrap();
|
|
std::fs::write(&k, &p.key_pem).unwrap();
|
|
(c, k)
|
|
}
|
|
|
|
fn free_port() -> u16 {
|
|
let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
|
l.local_addr().unwrap().port()
|
|
}
|
|
|
|
struct Server {
|
|
child: Child,
|
|
port: u16,
|
|
token: String,
|
|
dir: tempdir::Dir,
|
|
}
|
|
|
|
mod tempdir {
|
|
use std::path::PathBuf;
|
|
pub struct Dir(pub PathBuf);
|
|
impl Dir {
|
|
pub fn new(tag: &str) -> Self {
|
|
static N: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
|
|
let p = std::env::temp_dir().join(format!(
|
|
"slinuxm-tls-{tag}-{}-{}",
|
|
std::process::id(),
|
|
N.fetch_add(1, std::sync::atomic::Ordering::SeqCst)
|
|
));
|
|
std::fs::create_dir_all(&p).unwrap();
|
|
Self(p)
|
|
}
|
|
}
|
|
impl Drop for Dir {
|
|
fn drop(&mut self) {
|
|
let _ = std::fs::remove_dir_all(&self.0);
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Drop for Server {
|
|
fn drop(&mut self) {
|
|
let _ = self.child.kill();
|
|
let _ = self.child.wait();
|
|
}
|
|
}
|
|
|
|
fn config_toml(dir: &Path, port: u16, cert: &Path, key: &Path) -> PathBuf {
|
|
let f = dir.join("config.toml");
|
|
std::fs::write(
|
|
&f,
|
|
format!(
|
|
"[server]\nport = {port}\n[server.tls]\nenabled = true\ncert_file = {:?}\nkey_file = {:?}\n[auth]\ndb_path = {:?}\n",
|
|
cert.display().to_string(),
|
|
key.display().to_string(),
|
|
dir.join("db.sqlite").display().to_string(),
|
|
),
|
|
)
|
|
.unwrap();
|
|
f
|
|
}
|
|
|
|
fn mint_token(cfg: &Path) -> String {
|
|
let out = Command::new(BIN)
|
|
.args(["token", "create", "t", "--config"])
|
|
.arg(cfg)
|
|
.env_clear()
|
|
.output()
|
|
.unwrap();
|
|
assert!(
|
|
out.status.success(),
|
|
"{}",
|
|
String::from_utf8_lossy(&out.stderr)
|
|
);
|
|
String::from_utf8(out.stdout).unwrap().trim().to_owned()
|
|
}
|
|
|
|
async fn start(pair: &Pair) -> Server {
|
|
// The port is probed before the child binds it. If something else takes
|
|
// it in between, our child exits and the socket belongs to a stranger, so
|
|
// a fresh port is tried instead of testing the wrong server.
|
|
for _ in 0..3 {
|
|
if let Some(s) = try_start_on(pair, free_port()).await {
|
|
return s;
|
|
}
|
|
}
|
|
panic!("server did not start");
|
|
}
|
|
|
|
/// Ready means the port completes *our* TLS handshake while the child is
|
|
/// still running. `None` means the child was gone before it served, which is
|
|
/// only possible when the probed port was taken by someone else.
|
|
async fn try_start_on(pair: &Pair, port: u16) -> Option<Server> {
|
|
let dir = tempdir::Dir::new("srv");
|
|
let (c, k) = write_pair(&dir.0, pair);
|
|
let cfg = config_toml(&dir.0, port, &c, &k);
|
|
let token = mint_token(&cfg);
|
|
let mut s = Server {
|
|
child: Command::new(BIN)
|
|
.args(["serve", "--config"])
|
|
.arg(&cfg)
|
|
.env_clear()
|
|
.stderr(std::fs::File::create(dir.0.join("log.txt")).unwrap())
|
|
.spawn()
|
|
.unwrap(),
|
|
port,
|
|
token,
|
|
dir,
|
|
};
|
|
for _ in 0..100 {
|
|
if s.child.try_wait().unwrap().is_some() {
|
|
return None;
|
|
}
|
|
if handshake_succeeds(pair, port).await {
|
|
return Some(s);
|
|
}
|
|
tokio::time::sleep(Duration::from_millis(100)).await;
|
|
}
|
|
panic!("server did not start");
|
|
}
|
|
|
|
async fn handshake_succeeds(pair: &Pair, port: u16) -> bool {
|
|
let Ok(tcp) = TcpStream::connect(("127.0.0.1", port)).await else {
|
|
return false;
|
|
};
|
|
// A listener that never speaks TLS must not hang the probe.
|
|
let probe = connector(pair).connect(ServerName::try_from("localhost").unwrap(), tcp);
|
|
matches!(
|
|
tokio::time::timeout(Duration::from_millis(500), probe).await,
|
|
Ok(Ok(_))
|
|
)
|
|
}
|
|
|
|
/// A port already held by a listener that never speaks TLS is not ours: the
|
|
/// helper reports "retry" instead of testing the wrong server.
|
|
#[tokio::test]
|
|
async fn a_probed_port_taken_by_someone_else_is_retried() {
|
|
let pair = gen_pair();
|
|
let squatter = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
|
let port = squatter.local_addr().unwrap().port();
|
|
assert!(try_start_on(&pair, port).await.is_none());
|
|
drop(squatter);
|
|
}
|
|
|
|
fn connector(trusted: &Pair) -> TlsConnector {
|
|
let mut roots = rustls::RootCertStore::empty();
|
|
for c in CertificateDer::pem_slice_iter(trusted.cert_pem.as_bytes()) {
|
|
roots.add(c.unwrap()).unwrap();
|
|
}
|
|
let cfg = rustls::ClientConfig::builder_with_provider(Arc::new(
|
|
rustls::crypto::ring::default_provider(),
|
|
))
|
|
.with_safe_default_protocol_versions()
|
|
.unwrap()
|
|
.with_root_certificates(roots)
|
|
.with_no_client_auth();
|
|
TlsConnector::from(Arc::new(cfg))
|
|
}
|
|
|
|
struct Resp {
|
|
status: u16,
|
|
head: String,
|
|
body: String,
|
|
peer_fp: String,
|
|
}
|
|
|
|
/// One HTTPS request with `Connection: close`.
|
|
async fn https(
|
|
port: u16,
|
|
trusted: &Pair,
|
|
method: &str,
|
|
path: &str,
|
|
headers: &[(&str, &str)],
|
|
body: Option<&str>,
|
|
) -> Resp {
|
|
let tcp = TcpStream::connect(("127.0.0.1", port)).await.unwrap();
|
|
let mut tls = connector(trusted)
|
|
.connect(ServerName::try_from("localhost").unwrap(), tcp)
|
|
.await
|
|
.expect("TLS handshake");
|
|
let peer_fp = {
|
|
let (_, conn) = tls.get_ref();
|
|
let der = &conn.peer_certificates().unwrap()[0];
|
|
hex(&Sha256::digest(der.as_ref()))
|
|
};
|
|
let req = common::request(method, path, &format!("localhost:{port}"), headers, body);
|
|
tls.write_all(req.as_bytes()).await.unwrap();
|
|
let mut buf = Vec::new();
|
|
let mut chunk = [0u8; 4096];
|
|
let deadline = Instant::now() + Duration::from_secs(5);
|
|
while Instant::now() < deadline {
|
|
match tokio::time::timeout(Duration::from_secs(2), tls.read(&mut chunk)).await {
|
|
Ok(Ok(0)) | Ok(Err(_)) | Err(_) => break,
|
|
Ok(Ok(n)) => buf.extend_from_slice(&chunk[..n]),
|
|
}
|
|
}
|
|
let (status, head, body) = common::parse_response(&buf);
|
|
Resp {
|
|
status,
|
|
head,
|
|
body,
|
|
peer_fp,
|
|
}
|
|
}
|
|
|
|
fn hex(b: &[u8]) -> String {
|
|
b.iter().map(|x| format!("{x:02x}")).collect()
|
|
}
|
|
|
|
fn signal(child: &Child, sig: &str) {
|
|
assert!(Command::new("kill")
|
|
.args([sig, &child.id().to_string()])
|
|
.status()
|
|
.unwrap()
|
|
.success());
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn healthz_over_https() {
|
|
let pair = gen_pair();
|
|
let s = start(&pair).await;
|
|
let r = https(s.port, &pair, "GET", "/healthz", &[], None).await;
|
|
assert_eq!(r.status, 200, "{}", r.head);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn auth_over_tls() {
|
|
let pair = gen_pair();
|
|
let s = start(&pair).await;
|
|
let r = https(s.port, &pair, "GET", "/api/v1/nope", &[], None).await;
|
|
assert_eq!(r.status, 401);
|
|
let auth = format!("Bearer {}", s.token);
|
|
let r = https(
|
|
s.port,
|
|
&pair,
|
|
"GET",
|
|
"/api/v1/nope",
|
|
&[("Authorization", &auth)],
|
|
None,
|
|
)
|
|
.await;
|
|
assert_eq!(r.status, 404, "token accepted, route unknown");
|
|
let r = https(
|
|
s.port,
|
|
&pair,
|
|
"GET",
|
|
"/api/v1/systemd/units",
|
|
&[("Authorization", "Bearer wrong")],
|
|
None,
|
|
)
|
|
.await;
|
|
assert_eq!(r.status, 401);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn plain_http_to_tls_port_fails() {
|
|
let pair = gen_pair();
|
|
let s = start(&pair).await;
|
|
let mut c = TcpStream::connect(("127.0.0.1", s.port)).await.unwrap();
|
|
c.write_all(b"GET /healthz HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
|
|
.await
|
|
.unwrap();
|
|
let mut buf = Vec::new();
|
|
let _ = tokio::time::timeout(Duration::from_secs(3), c.read_to_end(&mut buf)).await;
|
|
assert!(
|
|
!String::from_utf8_lossy(&buf).contains("200 OK"),
|
|
"plain HTTP must not be served on the TLS port"
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn mcp_initialize_over_https() {
|
|
let pair = gen_pair();
|
|
let s = start(&pair).await;
|
|
let auth = format!("Bearer {}", s.token);
|
|
let origin = format!("https://localhost:{}", s.port);
|
|
let r = https(
|
|
s.port,
|
|
&pair,
|
|
"POST",
|
|
"/mcp",
|
|
&[
|
|
("Content-Type", "application/json"),
|
|
("Accept", "application/json, text/event-stream"),
|
|
("Authorization", &auth),
|
|
("Origin", &origin),
|
|
],
|
|
Some(r#"{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-03-26","capabilities":{},"clientInfo":{"name":"it","version":"0"}}}"#),
|
|
)
|
|
.await;
|
|
assert_eq!(r.status, 200, "{}\n{}", r.head, r.body);
|
|
assert!(r.body.contains("serverInfo"), "{}", r.body);
|
|
// The http:// origin is no longer a default when TLS is on.
|
|
let bad = format!("http://localhost:{}", s.port);
|
|
let r = https(
|
|
s.port,
|
|
&pair,
|
|
"POST",
|
|
"/mcp",
|
|
&[
|
|
("Content-Type", "application/json"),
|
|
("Accept", "application/json, text/event-stream"),
|
|
("Authorization", &auth),
|
|
("Origin", &bad),
|
|
],
|
|
Some("{}"),
|
|
)
|
|
.await;
|
|
assert_eq!(r.status, 403);
|
|
}
|
|
|
|
/// Certificate verifier that accepts anything: used only to read which
|
|
/// certificate the server presents while rotation is in flight.
|
|
#[derive(Debug)]
|
|
struct NoVerify(rustls::crypto::WebPkiSupportedAlgorithms);
|
|
|
|
impl rustls::client::danger::ServerCertVerifier for NoVerify {
|
|
fn verify_server_cert(
|
|
&self,
|
|
_: &CertificateDer<'_>,
|
|
_: &[CertificateDer<'_>],
|
|
_: &ServerName<'_>,
|
|
_: &[u8],
|
|
_: rustls::pki_types::UnixTime,
|
|
) -> Result<rustls::client::danger::ServerCertVerified, rustls::Error> {
|
|
Ok(rustls::client::danger::ServerCertVerified::assertion())
|
|
}
|
|
fn verify_tls12_signature(
|
|
&self,
|
|
m: &[u8],
|
|
c: &CertificateDer<'_>,
|
|
d: &rustls::DigitallySignedStruct,
|
|
) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
|
|
rustls::crypto::verify_tls12_signature(m, c, d, &self.0)
|
|
}
|
|
fn verify_tls13_signature(
|
|
&self,
|
|
m: &[u8],
|
|
c: &CertificateDer<'_>,
|
|
d: &rustls::DigitallySignedStruct,
|
|
) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
|
|
rustls::crypto::verify_tls13_signature(m, c, d, &self.0)
|
|
}
|
|
fn supported_verify_schemes(&self) -> Vec<rustls::SignatureScheme> {
|
|
self.0.supported_schemes()
|
|
}
|
|
}
|
|
|
|
/// Fingerprint of the certificate currently served, without trusting it.
|
|
/// `None` on any connection error (treated by callers as "not yet").
|
|
async fn probe_fp(port: u16) -> Option<String> {
|
|
let provider = Arc::new(rustls::crypto::ring::default_provider());
|
|
let algs = provider.signature_verification_algorithms;
|
|
let cfg = rustls::ClientConfig::builder_with_provider(provider)
|
|
.with_safe_default_protocol_versions()
|
|
.unwrap()
|
|
.dangerous()
|
|
.with_custom_certificate_verifier(Arc::new(NoVerify(algs)))
|
|
.with_no_client_auth();
|
|
let tcp = TcpStream::connect(("127.0.0.1", port)).await.ok()?;
|
|
let tls = TlsConnector::from(Arc::new(cfg))
|
|
.connect(ServerName::try_from("localhost").unwrap(), tcp)
|
|
.await
|
|
.ok()?;
|
|
let (_, conn) = tls.get_ref();
|
|
let der = conn.peer_certificates()?.first()?.clone();
|
|
Some(hex(&Sha256::digest(der.as_ref())))
|
|
}
|
|
|
|
fn log_count(dir: &Path, needle: &str) -> usize {
|
|
std::fs::read_to_string(dir.join("log.txt"))
|
|
.unwrap_or_default()
|
|
.matches(needle)
|
|
.count()
|
|
}
|
|
|
|
/// Wait (bounded) until the server log holds at least `n` lines with `needle`.
|
|
async fn wait_log(dir: &Path, needle: &str, n: usize) {
|
|
for _ in 0..100 {
|
|
if log_count(dir, needle) >= n {
|
|
return;
|
|
}
|
|
tokio::time::sleep(Duration::from_millis(100)).await;
|
|
}
|
|
panic!("server log never showed {n} x {needle:?}");
|
|
}
|
|
|
|
/// Poll until the served certificate fingerprint satisfies `want`.
|
|
async fn wait_fp(port: u16, want: impl Fn(&str) -> bool) -> String {
|
|
for _ in 0..50 {
|
|
if let Some(fp) = probe_fp(port).await {
|
|
if want(&fp) {
|
|
return fp;
|
|
}
|
|
}
|
|
tokio::time::sleep(Duration::from_millis(100)).await;
|
|
}
|
|
panic!("served certificate never reached the expected state");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn sighup_serves_new_cert_and_bad_cert_keeps_old() {
|
|
let a = gen_pair();
|
|
let b = gen_pair();
|
|
let s = start(&a).await;
|
|
let fp_a = https(s.port, &a, "GET", "/healthz", &[], None)
|
|
.await
|
|
.peer_fp;
|
|
|
|
// Hold a keep-alive connection across the reload.
|
|
let tcp = TcpStream::connect(("127.0.0.1", s.port)).await.unwrap();
|
|
let mut held = connector(&a)
|
|
.connect(ServerName::try_from("localhost").unwrap(), tcp)
|
|
.await
|
|
.unwrap();
|
|
held.write_all(b"GET /healthz HTTP/1.1\r\nHost: localhost\r\n\r\n")
|
|
.await
|
|
.unwrap();
|
|
let mut buf = [0u8; 1024];
|
|
assert!(held.read(&mut buf).await.unwrap() > 0);
|
|
|
|
// Corrupt cert: reload fails (wait for the log), old cert still served.
|
|
std::fs::write(s.dir.0.join("cert.pem"), "not a certificate").unwrap();
|
|
signal(&s.child, "-HUP");
|
|
wait_log(&s.dir.0, "TLS reload failed", 1).await;
|
|
let r = https(s.port, &a, "GET", "/healthz", &[], None).await;
|
|
assert_eq!(r.status, 200);
|
|
assert_eq!(r.peer_fp, fp_a, "bad reload must keep the old certificate");
|
|
|
|
// Valid new pair: SIGHUP swaps it.
|
|
write_pair(&s.dir.0, &b);
|
|
signal(&s.child, "-HUP");
|
|
wait_log(&s.dir.0, "TLS certificate reloaded", 1).await;
|
|
let fp_b = wait_fp(s.port, |fp| fp != fp_a).await;
|
|
assert_ne!(fp_b, fp_a, "SIGHUP should serve the new certificate");
|
|
|
|
// The pre-reload connection survived.
|
|
held.write_all(b"GET /healthz HTTP/1.1\r\nHost: localhost\r\n\r\n")
|
|
.await
|
|
.unwrap();
|
|
let n = held.read(&mut buf).await.unwrap();
|
|
assert!(String::from_utf8_lossy(&buf[..n]).contains("200 OK"));
|
|
}
|
|
|
|
#[test]
|
|
fn bad_files_fail_startup() {
|
|
let dir = tempdir::Dir::new("bad");
|
|
let pair = gen_pair();
|
|
let (c, k) = write_pair(&dir.0, &pair);
|
|
let run = |cert: &Path, key: &Path, tag: &str| {
|
|
let cfg = config_toml(&dir.0, free_port(), cert, key);
|
|
let out = Command::new(BIN)
|
|
.args(["serve", "--config"])
|
|
.arg(&cfg)
|
|
.env_clear()
|
|
.output()
|
|
.unwrap();
|
|
assert!(!out.status.success(), "{tag}: should fail");
|
|
String::from_utf8_lossy(&out.stderr).into_owned()
|
|
};
|
|
let missing = dir.0.join("missing.pem");
|
|
assert!(run(&missing, &k, "missing cert").contains("cert_file"));
|
|
assert!(run(&c, &missing, "missing key").contains("key_file"));
|
|
let junk = dir.0.join("junk.pem");
|
|
std::fs::write(&junk, "garbage").unwrap();
|
|
assert!(run(&junk, &k, "junk cert").contains("cert_file"));
|
|
assert!(run(&c, &junk, "junk key").contains("key_file"));
|
|
// Mismatched pair.
|
|
let other = gen_pair();
|
|
let ok = dir.0.join("other-key.pem");
|
|
std::fs::write(&ok, &other.key_pem).unwrap();
|
|
assert!(run(&c, &ok, "mismatch").contains("does not match"));
|
|
// No key material in the error output.
|
|
let err = run(&c, &junk, "junk key");
|
|
assert!(!err.contains("PRIVATE KEY") && !err.contains("garbage"));
|
|
}
|
|
|
|
#[test]
|
|
fn enabled_without_paths_fails_validation_and_disabled_ignores_paths() {
|
|
let dir = tempdir::Dir::new("val");
|
|
let f = dir.0.join("c.toml");
|
|
std::fs::write(&f, "[server.tls]\nenabled = true\n").unwrap();
|
|
let out = Command::new(BIN)
|
|
.args(["serve", "--config"])
|
|
.arg(&f)
|
|
.env_clear()
|
|
.output()
|
|
.unwrap();
|
|
assert!(!out.status.success());
|
|
assert!(String::from_utf8_lossy(&out.stderr).contains("server.tls"));
|
|
|
|
// enabled = false with unreadable paths: startup proceeds on plain HTTP.
|
|
let port = free_port();
|
|
std::fs::write(
|
|
&f,
|
|
format!(
|
|
"[server]\nport = {port}\n[server.tls]\nenabled = false\ncert_file = \"/nonexistent/c.pem\"\nkey_file = \"/nonexistent/k.pem\"\n[auth]\ndb_path = {:?}\n",
|
|
dir.0.join("db.sqlite").display().to_string()
|
|
),
|
|
)
|
|
.unwrap();
|
|
let mut child = Command::new(BIN)
|
|
.args(["serve", "--config"])
|
|
.arg(&f)
|
|
.env_clear()
|
|
.stderr(Stdio::null())
|
|
.spawn()
|
|
.unwrap();
|
|
let mut up = false;
|
|
for _ in 0..100 {
|
|
if let Ok(mut c) = std::net::TcpStream::connect(("127.0.0.1", port)) {
|
|
use std::io::{Read, Write};
|
|
c.write_all(b"GET /healthz HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
|
|
.unwrap();
|
|
let mut s = String::new();
|
|
let _ = c.read_to_string(&mut s);
|
|
up = s.contains("200 OK");
|
|
break;
|
|
}
|
|
std::thread::sleep(Duration::from_millis(100));
|
|
}
|
|
let _ = child.kill();
|
|
let _ = child.wait();
|
|
assert!(up, "plain HTTP should serve with tls disabled");
|
|
}
|
|
|
|
/// SIGTERM with an open (idle keep-alive) TLS connection and an in-flight
|
|
/// long-poll still exits 0 within the existing bound.
|
|
#[tokio::test]
|
|
async fn sigterm_with_open_tls_connections_exits_promptly() {
|
|
let pair = gen_pair();
|
|
let mut s = start(&pair).await;
|
|
let tcp = TcpStream::connect(("127.0.0.1", s.port)).await.unwrap();
|
|
let mut idle = connector(&pair)
|
|
.connect(ServerName::try_from("localhost").unwrap(), tcp)
|
|
.await
|
|
.unwrap();
|
|
idle.write_all(b"GET /healthz HTTP/1.1\r\nHost: localhost\r\n\r\n")
|
|
.await
|
|
.unwrap();
|
|
let mut buf = [0u8; 1024];
|
|
let _ = idle.read(&mut buf).await.unwrap();
|
|
|
|
// Open SSE stream (journal follow) over TLS, left running.
|
|
let tcp = TcpStream::connect(("127.0.0.1", s.port)).await.unwrap();
|
|
let mut sse = connector(&pair)
|
|
.connect(ServerName::try_from("localhost").unwrap(), tcp)
|
|
.await
|
|
.unwrap();
|
|
let req = format!(
|
|
"GET /api/v1/journal/follow HTTP/1.1\r\nHost: localhost\r\nAccept: text/event-stream\r\nAuthorization: Bearer {}\r\n\r\n",
|
|
s.token
|
|
);
|
|
sse.write_all(req.as_bytes()).await.unwrap();
|
|
let _ = tokio::time::timeout(Duration::from_millis(500), sse.read(&mut buf)).await;
|
|
|
|
signal(&s.child, "-TERM");
|
|
let start = Instant::now();
|
|
loop {
|
|
if let Some(st) = s.child.try_wait().unwrap() {
|
|
assert!(st.success(), "exit status {st}");
|
|
break;
|
|
}
|
|
assert!(
|
|
start.elapsed() < Duration::from_secs(15),
|
|
"did not exit within the drain bound"
|
|
);
|
|
tokio::time::sleep(Duration::from_millis(50)).await;
|
|
}
|
|
}
|
|
|
|
/// In-process TLS listener with explicit limits.
|
|
async fn inproc(
|
|
pair: &Pair,
|
|
limits: skyg_linux_tracker::server::Limits,
|
|
) -> (u16, tokio_util::sync::CancellationToken, tempdir::Dir) {
|
|
use skyg_linux_tracker::{config::Config, server, sources::fake::fake_state, tls};
|
|
let dir = tempdir::Dir::new("inproc");
|
|
let (c, k) = write_pair(&dir.0, pair);
|
|
let mut cfg = Config::default();
|
|
cfg.auth.enabled = false;
|
|
cfg.server.tls.enabled = true;
|
|
cfg.server.tls.cert_file = c;
|
|
cfg.server.tls.key_file = k;
|
|
let cert = tls::ReloadableCert::load(&cfg.server.tls).unwrap();
|
|
let acceptor = tls::acceptor(cert).unwrap();
|
|
let state = fake_state(cfg);
|
|
let shutdown = state.shutdown.clone();
|
|
let app = server::build_router(state);
|
|
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
|
let port = listener.local_addr().unwrap().port();
|
|
tokio::spawn(server::serve_listener(
|
|
listener,
|
|
Some(acceptor),
|
|
app,
|
|
shutdown.clone(),
|
|
limits,
|
|
));
|
|
(port, shutdown, dir)
|
|
}
|
|
|
|
async fn handshake(
|
|
port: u16,
|
|
pair: &Pair,
|
|
) -> std::io::Result<tokio_rustls::client::TlsStream<TcpStream>> {
|
|
let tcp = TcpStream::connect(("127.0.0.1", port)).await?;
|
|
connector(pair)
|
|
.connect(ServerName::try_from("localhost").unwrap(), tcp)
|
|
.await
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn silent_connection_closed_after_header_timeout() {
|
|
let pair = gen_pair();
|
|
let (port, shutdown, _d) = inproc(
|
|
&pair,
|
|
skyg_linux_tracker::server::Limits {
|
|
max_conns: 16,
|
|
header_timeout: Duration::from_millis(500),
|
|
},
|
|
)
|
|
.await;
|
|
let mut c = handshake(port, &pair).await.unwrap();
|
|
let start = Instant::now();
|
|
let mut buf = [0u8; 256];
|
|
let r = tokio::time::timeout(Duration::from_secs(5), async {
|
|
loop {
|
|
match c.read(&mut buf).await {
|
|
Ok(0) | Err(_) => break,
|
|
Ok(_) => {}
|
|
}
|
|
}
|
|
})
|
|
.await;
|
|
assert!(r.is_ok(), "silent post-handshake connection was not closed");
|
|
assert!(start.elapsed() < Duration::from_secs(4));
|
|
shutdown.cancel();
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn connection_cap_is_enforced() {
|
|
let pair = gen_pair();
|
|
let (port, shutdown, _d) = inproc(
|
|
&pair,
|
|
skyg_linux_tracker::server::Limits {
|
|
max_conns: 2,
|
|
header_timeout: Duration::from_secs(30),
|
|
},
|
|
)
|
|
.await;
|
|
let c1 = handshake(port, &pair).await.unwrap();
|
|
let c2 = handshake(port, &pair).await.unwrap();
|
|
let third = tokio::time::timeout(Duration::from_millis(800), handshake(port, &pair)).await;
|
|
assert!(third.is_err(), "third connection was served beyond the cap");
|
|
drop(c1);
|
|
let c3 = tokio::time::timeout(Duration::from_secs(5), handshake(port, &pair)).await;
|
|
assert!(c3.is_ok(), "slot was not released after a close");
|
|
drop(c2);
|
|
shutdown.cancel();
|
|
}
|
|
|
|
#[test]
|
|
fn sighup_with_tls_disabled_keeps_serving() {
|
|
use std::io::{Read, Write};
|
|
let dir = tempdir::Dir::new("plainhup");
|
|
let port = free_port();
|
|
let f = dir.0.join("c.toml");
|
|
std::fs::write(
|
|
&f,
|
|
format!(
|
|
"[server]\nport = {port}\n[auth]\ndb_path = {:?}\n",
|
|
dir.0.join("db.sqlite").display().to_string()
|
|
),
|
|
)
|
|
.unwrap();
|
|
let mut child = Command::new(BIN)
|
|
.args(["serve", "--config"])
|
|
.arg(&f)
|
|
.env_clear()
|
|
.stderr(Stdio::null())
|
|
.spawn()
|
|
.unwrap();
|
|
let get = || -> Option<String> {
|
|
let mut c = std::net::TcpStream::connect(("127.0.0.1", port)).ok()?;
|
|
c.write_all(b"GET /healthz HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
|
|
.ok()?;
|
|
let mut s = String::new();
|
|
let _ = c.read_to_string(&mut s);
|
|
Some(s)
|
|
};
|
|
for _ in 0..100 {
|
|
if get().is_some_and(|s| s.contains("200 OK")) {
|
|
break;
|
|
}
|
|
std::thread::sleep(Duration::from_millis(100));
|
|
}
|
|
signal(&child, "-HUP");
|
|
std::thread::sleep(Duration::from_millis(500));
|
|
assert!(
|
|
child.try_wait().unwrap().is_none(),
|
|
"SIGHUP killed the process"
|
|
);
|
|
assert!(get().is_some_and(|s| s.contains("200 OK")));
|
|
let _ = child.kill();
|
|
let _ = child.wait();
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn mismatched_pair_on_sighup_keeps_old_cert() {
|
|
let a = gen_pair();
|
|
let b = gen_pair();
|
|
let s = start(&a).await;
|
|
let fp_a = https(s.port, &a, "GET", "/healthz", &[], None)
|
|
.await
|
|
.peer_fp;
|
|
// New cert B with old key A: mismatch, old pair kept.
|
|
std::fs::write(s.dir.0.join("cert.pem"), &b.cert_pem).unwrap();
|
|
signal(&s.child, "-HUP");
|
|
wait_log(&s.dir.0, "TLS reload failed", 1).await;
|
|
let r = https(s.port, &a, "GET", "/healthz", &[], None).await;
|
|
assert_eq!(r.peer_fp, fp_a);
|
|
// Completing the rotation (key B) and sending HUP again works.
|
|
std::fs::write(s.dir.0.join("key.pem"), &b.key_pem).unwrap();
|
|
signal(&s.child, "-HUP");
|
|
wait_log(&s.dir.0, "TLS certificate reloaded", 1).await;
|
|
let fp = wait_fp(s.port, |fp| fp != fp_a).await;
|
|
assert_ne!(fp, fp_a);
|
|
}
|