WS-I: native TLS (optional, default off) with SIGHUP cert reload

server.tls.{enabled,cert_file,key_file}; rustls (ring, explicit provider) via
tokio-rustls + hyper-util; https:// MCP origins when enabled; non-loopback
plain-HTTP startup warning; tests/tls.rs; README TLS section.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
ariandClaude Sonnet 5.5 committed 2026-10-16 07:10:14 -04:00
1 parent c83a5fa1ac
commit c5972b762f
11 files changed
+835 -15

No files matched your search

+499
View File
@@ -0,0 +1,499 @@
//! Native TLS tests (WS-I T6): 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;
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()
}
fn start(pair: &Pair) -> Server {
let dir = tempdir::Dir::new("srv");
let (c, k) = write_pair(&dir.0, pair);
let port = free_port();
let cfg = config_toml(&dir.0, port, &c, &k);
let token = mint_token(&cfg);
let child = Command::new(BIN)
.args(["serve", "--config"])
.arg(&cfg)
.env_clear()
.stderr(Stdio::null())
.spawn()
.unwrap();
let s = Server {
child,
port,
token,
dir,
};
for _ in 0..100 {
if std::net::TcpStream::connect(("127.0.0.1", port)).is_ok() {
return s;
}
std::thread::sleep(Duration::from_millis(100));
}
panic!("server did not start");
}
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 mut req =
format!("{method} {path} HTTP/1.1\r\nHost: localhost:{port}\r\nConnection: close\r\n");
for (k, v) in headers {
req.push_str(&format!("{k}: {v}\r\n"));
}
if let Some(b) = body {
req.push_str(&format!("Content-Length: {}\r\n", b.len()));
}
req.push_str("\r\n");
if let Some(b) = body {
req.push_str(b);
}
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 text = String::from_utf8_lossy(&buf).into_owned();
let (head, body) = text.split_once("\r\n\r\n").unwrap_or((&text, ""));
let status = head
.split_whitespace()
.nth(1)
.and_then(|s| s.parse().ok())
.unwrap_or(0);
Resp {
status,
head: head.to_lowercase(),
body: body.to_owned(),
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);
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);
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);
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);
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);
}
#[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);
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, old cert still served.
std::fs::write(s.dir.0.join("cert.pem"), "not a certificate").unwrap();
signal(&s.child, "-HUP");
tokio::time::sleep(Duration::from_millis(500)).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");
let mut fp_b = String::new();
for _ in 0..30 {
tokio::time::sleep(Duration::from_millis(200)).await;
fp_b = https(s.port, &b, "GET", "/healthz", &[], None)
.await
.peer_fp;
if fp_b != fp_a {
break;
}
}
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);
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;
}
}