From c5972b762f064d22278f3a7479a201853eb7bb04 Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Fri, 16 Oct 2026 07:10:14 -0400 Subject: [PATCH] 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 --- README.md | 28 +- TODO.md | 16 +- config.example.toml | 8 + plan/BOARD.md | 2 +- plan/workstreams/WS-I-tls.md | 3 +- src/config.rs | 55 ++++ src/lib.rs | 1 + src/main.rs | 25 +- src/mcp/mod.rs | 17 +- src/tls.rs | 196 ++++++++++++++ tests/tls.rs | 499 +++++++++++++++++++++++++++++++++++ 11 files changed, 835 insertions(+), 15 deletions(-) create mode 100644 src/tls.rs create mode 100644 tests/tls.rs diff --git a/README.md b/README.md index 30d2ae2..8be7d2a 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,33 @@ curl -si -X POST http://127.0.0.1:9109/mcp \ ### Origin and Host checks -`/mcp` validates `Origin` and `Host` to defend against DNS rebinding from browsers. Loopback names, `server.bind` and the configured port are accepted by default. Requests without an `Origin` header (non-browser clients) pass this check, so the bearer token remains the real gate. If you bind to `0.0.0.0` or serve behind a name, add the names clients use to `server.allowed_hosts` (for example `["monitor.lan:9109"]`) and any browser origins to `server.allowed_origins`. With `auth.enabled = false` (loopback only), REST requests are checked against a Host allowlist (loopback names, `server.bind` and `server.allowed_hosts`) and get `403` otherwise, to blunt DNS rebinding. There is no Origin check on REST and no token in that mode, so keep auth enabled on any multi-user host. Terminate TLS in a reverse proxy if the host is reachable beyond a trusted network; the server itself speaks plain HTTP. +`/mcp` validates `Origin` and `Host` to defend against DNS rebinding from browsers. Loopback names, `server.bind` and the configured port are accepted by default. Requests without an `Origin` header (non-browser clients) pass this check, so the bearer token remains the real gate. If you bind to `0.0.0.0` or serve behind a name, add the names clients use to `server.allowed_hosts` (for example `["monitor.lan:9109"]`) and any browser origins to `server.allowed_origins`. With `auth.enabled = false` (loopback only), REST requests are checked against a Host allowlist (loopback names, `server.bind` and `server.allowed_hosts`) and get `403` otherwise, to blunt DNS rebinding. There is no Origin check on REST and no token in that mode, so keep auth enabled on any multi-user host. If the host is reachable beyond a trusted network, enable native TLS (see the TLS section) or terminate TLS in a reverse proxy. + +## TLS + +TLS is optional and off by default. With `server.tls.enabled = false` (the default) the server speaks plain HTTP exactly as before, the certificate paths are ignored and never read, and no TLS code runs. A non-loopback `bind` without TLS logs a startup warning (it is not refused). + +Enable it in the config file or the environment (`SKYG_SERVER__TLS__ENABLED=true`, `SKYG_SERVER__TLS__CERT_FILE`, `SKYG_SERVER__TLS__KEY_FILE`): + +```toml +[server.tls] +enabled = true +cert_file = "/etc/skyg/tls/cert.pem" # PEM chain, leaf first +key_file = "/etc/skyg/tls/key.pem" # PEM, PKCS#8, PKCS#1 or SEC1 +``` + +Both files are required and checked at startup; a missing, unparsable or mismatched pair stops the server with an error naming the file (key contents are never logged). REST and `/mcp` are then served over HTTPS only (plain HTTP on that port fails), and the default MCP `Origin` allowlist uses `https://` instead of `http://`. Add your public names to `server.allowed_hosts` and `server.allowed_origins` as before. TLS is rustls with the ring provider; openssl is not used. + +Self-signed example: + +```sh +openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes -days 365 \ + -keyout key.pem -out cert.pem -subj '/CN=monitor.lan' \ + -addext 'subjectAltName=DNS:monitor.lan,DNS:localhost,IP:127.0.0.1' +curl --cacert cert.pem -H "Authorization: Bearer $TOKEN" https://localhost:9109/api/v1/systemd/units +``` + +Rotation: replace the files and send `SIGHUP` (`kill -HUP $(pidof slinuxm)`, or `systemctl kill -s HUP `). New connections get the new certificate; established ones are not dropped. If the new pair is unreadable or mismatched, the old certificate stays and an error is logged. For ACME, point `cert_file` and `key_file` at the client's output (for example certbot's `live//fullchain.pem` and `privkey.pem`, or the NixOS `security.acme` directory) and send SIGHUP from the renewal hook. For sops-nix or systemd `LoadCredential`, point at the decrypted or credential path (`/run/credentials//...`) so the service user needs no access to the original key file. ## Docker access diff --git a/TODO.md b/TODO.md index cab9e5f..bda6c83 100644 --- a/TODO.md +++ b/TODO.md @@ -106,14 +106,14 @@ Design: [`plan.md`](plan.md) · Review fixes: [`plan/review.md`](plan/review.md) ## Phase 4 — TLS and NixOS packaging ### WS-I TLS · [card](plan/workstreams/WS-I-tls.md) -- [ ] **T1** Config `server.tls.{enabled,cert_file,key_file}` with validation and documented example keys -- [ ] **T2** Serve over rustls with a single pinned crypto provider. No openssl. -- [ ] **T3** Reload cert and key on SIGHUP without dropping connections. A bad cert keeps the old one. -- [ ] **T4** MCP allowed origins use `https://` when TLS is on -- [ ] **T5** Startup warning for non-loopback bind without TLS -- [ ] **T6** Tests with a generated self-signed cert: HTTPS, 401/200 over TLS, plain HTTP rejected, SIGHUP reload, bad files, bounded shutdown -- [ ] **T7** README TLS section -- [ ] **T8** Live check with `curl --cacert` and an `/mcp` initialize over HTTPS +- [x] **T1** Config `server.tls.{enabled,cert_file,key_file}` with validation and documented example keys +- [x] **T2** Serve over rustls with a single pinned crypto provider. No openssl. +- [x] **T3** Reload cert and key on SIGHUP without dropping connections. A bad cert keeps the old one. +- [x] **T4** MCP allowed origins use `https://` when TLS is on +- [x] **T5** Startup warning for non-loopback bind without TLS +- [x] **T6** Tests with a generated self-signed cert: HTTPS, 401/200 over TLS, plain HTTP rejected, SIGHUP reload, bad files, bounded shutdown +- [x] **T7** README TLS section +- [x] **T8** Live check with `curl --cacert` and an `/mcp` initialize over HTTPS ### WS-H NixOS · [card](plan/workstreams/WS-H-nixos.md) - [ ] **H1** `nix/package.nix`: `buildRustPackage` from `Cargo.lock` with pkg-config, cmake, systemd. Binary `slinuxm`. diff --git a/config.example.toml b/config.example.toml index f39a88d..80f8e22 100644 --- a/config.example.toml +++ b/config.example.toml @@ -12,6 +12,14 @@ port = 9109 # listen port; SKYG_SERVER__PORT allowed_hosts = [] # extra Host header values accepted on /mcp, added to loopback names and bind; SKYG_SERVER__ALLOWED_HOSTS allowed_origins = [] # extra Origin values accepted on /mcp, must include scheme, e.g. "https://app.example.com"; SKYG_SERVER__ALLOWED_ORIGINS +# Native TLS for REST and /mcp. Optional and off by default: when enabled = false the +# server speaks plain HTTP and the file paths are ignored and never read. +# Certificate and key are re-read on SIGHUP (a bad new pair is rejected and the old one kept). +[server.tls] +enabled = false # serve HTTPS; requires cert_file and key_file; SKYG_SERVER__TLS__ENABLED +cert_file = "" # PEM certificate chain, leaf first; SKYG_SERVER__TLS__CERT_FILE +key_file = "" # PEM private key (PKCS#8, PKCS#1 or SEC1); SKYG_SERVER__TLS__KEY_FILE + [auth] enabled = true # false is rejected at startup unless server.bind is loopback; SKYG_AUTH__ENABLED db_path = "/var/lib/skyg-linux-tracker/db.sqlite" # SQLite database holding hashed API tokens; SKYG_AUTH__DB_PATH diff --git a/plan/BOARD.md b/plan/BOARD.md index 77b0eab..f826363 100644 --- a/plan/BOARD.md +++ b/plan/BOARD.md @@ -58,4 +58,4 @@ Move a card by cutting its row into another column. Keep the card's own **Status | M2 Surfaces | Verification #3, #4 and #5 pass | 🟨 #3 and #4 verified live on 2026-10-15; #5 (OTel smoke test) not run, no otelcol-contrib binary | | M3 Release | Verification #1–#6 pass. Unit file and README shipped. | ⬜ | | M4 NixOS | `nix flake check` passes including the VM test. `:1984` serves and a token is minted with the system-installed CLI. | ⬜ | -| M5 TLS | HTTPS serves REST and `/mcp`, SIGHUP reloads certs, no openssl. | ⬜ | +| M5 TLS | HTTPS serves REST and `/mcp`, SIGHUP reloads certs, no openssl. Verified on `ws-i/tls` (unmerged). | ✅ | diff --git a/plan/workstreams/WS-I-tls.md b/plan/workstreams/WS-I-tls.md index be0ef8f..2c488d3 100644 --- a/plan/workstreams/WS-I-tls.md +++ b/plan/workstreams/WS-I-tls.md @@ -2,7 +2,7 @@ | Field | Value | |---|---| -| Status | Backlog | +| Status | Done on `ws-i/tls` (not merged) | | Owner | — | | Phase | 4 (single track, after P3, before WS-H) | | Depends on | P3 | @@ -39,3 +39,4 @@ Tick these in [`TODO.md`](../../TODO.md). ## Log - 2026-10-16: Card created from design discussion. +- 2026-10-16: Implemented on `ws-i/tls`, T1-T8 done. **TLS is optional, default off** (user requirement): with `server.tls.enabled = false` the plain path is unchanged, the cert/key paths are ignored and never read, no rustls code runs, no provider is built, MCP origins stay `http://`; env (`SKYG_SERVER__TLS__*`) and `--print-config` cover the block. Design: `src/tls.rs` uses `tokio-rustls` + `hyper-util` (auto builder, HTTP/1.1 ALPN) with an accept loop and a `ReloadableCert` (`ResolvesServerCert` behind an RwLock) so SIGHUP swaps the cert for new handshakes without touching live connections; a bad pair logs an error and keeps the old one. The `ring` provider is passed explicitly to `ServerConfig::builder_with_provider`; no process-wide default is installed (aws-lc-rs was already in the tree via reqwest/OTLP; this adds no new provider or openssl). Cargo.toml: rustls, tokio-rustls, hyper-util (separate commit), rcgen dev-dependency. Shutdown: accept stops, each connection gets `graceful_shutdown`, bounded by the existing 10s drain (test: SIGTERM with an idle keep-alive and an open SSE stream over TLS exits 0). T5 warning is logged only when TLS is off and bind is non-loopback. Tests: 3 new unit tests (config validation, env, origins) and 8 in `tests/tls.rs`. Live check (debug binary, openssl-generated certs, curl): healthz, 401/200 REST, MCP initialize over HTTPS, plain HTTP rejected, SIGHUP changed the served fingerprint, a junk cert + SIGHUP kept the rotated one, SIGTERM exit 0. diff --git a/src/config.rs b/src/config.rs index 69611a5..60dbbbe 100644 --- a/src/config.rs +++ b/src/config.rs @@ -42,6 +42,20 @@ pub struct ServerConfig { /// Extra `Origin` values accepted on `/mcp`, added to the defaults /// (loopback origins plus `bind`). Entries include a scheme. pub allowed_origins: Vec, + /// Optional native TLS (off by default). + pub tls: TlsConfig, +} + +/// Native TLS for REST and `/mcp`. Optional: when `enabled` is false the +/// paths are ignored and never read. +#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)] +#[serde(default)] +pub struct TlsConfig { + pub enabled: bool, + /// PEM certificate chain (leaf first). + pub cert_file: PathBuf, + /// PEM private key (PKCS#8, PKCS#1 or SEC1). + pub key_file: PathBuf, } impl Default for ServerConfig { @@ -51,6 +65,7 @@ impl Default for ServerConfig { port: 9109, allowed_hosts: Vec::new(), allowed_origins: Vec::new(), + tls: TlsConfig::default(), } } } @@ -254,6 +269,15 @@ impl Config { "server.port must be greater than zero".into(), )); } + let tls = &self.server.tls; + if tls.enabled + && (tls.cert_file.as_os_str().is_empty() || tls.key_file.as_os_str().is_empty()) + { + return Err(ConfigError::Invalid( + "server.tls.enabled = true requires both server.tls.cert_file and server.tls.key_file" + .into(), + )); + } if !self.auth.enabled && !self.server.bind.is_loopback() { return Err(ConfigError::Invalid(format!( "auth.enabled = false is only allowed on a loopback bind address (server.bind = {})", @@ -402,6 +426,37 @@ mod tests { }); } + #[test] + fn tls_enabled_requires_both_paths() { + let mut c = Config::default(); + assert!(!c.server.tls.enabled); + c.validate().unwrap(); + c.server.tls.enabled = true; + assert!(c.validate().is_err()); + c.server.tls.cert_file = "/c.pem".into(); + assert!(c.validate().is_err()); + c.server.tls.key_file = "/k.pem".into(); + c.validate().unwrap(); + } + + #[test] + fn tls_disabled_ignores_paths_and_env_overrides() { + let mut c = Config::default(); + c.server.tls.cert_file = "/nonexistent/c.pem".into(); + c.server.tls.key_file = "/nonexistent/k.pem".into(); + c.validate().unwrap(); + Jail::expect_with(|jail| { + jail.set_env("SKYG_SERVER__TLS__ENABLED", "true"); + jail.set_env("SKYG_SERVER__TLS__CERT_FILE", "/c.pem"); + jail.set_env("SKYG_SERVER__TLS__KEY_FILE", "/k.pem"); + let c = load(None, &CliOverrides::default()).unwrap(); + assert!(c.server.tls.enabled); + assert_eq!(c.server.tls.cert_file, Path::new("/c.pem")); + assert!(c.to_toml_redacted().unwrap().contains("[server.tls]")); + Ok(()) + }); + } + #[test] fn example_file_matches_defaults() { let c: Config = toml::from_str(include_str!("../config.example.toml")).unwrap(); diff --git a/src/lib.rs b/src/lib.rs index a1693f2..88168b4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,3 +13,4 @@ pub mod server; pub mod sources; pub mod state; pub mod telemetry; +pub mod tls; diff --git a/src/main.rs b/src/main.rs index a155782..be2e61e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ use tokio_util::sync::CancellationToken; use skyg_linux_tracker::auth::tokens::{self, TokenCmd}; use skyg_linux_tracker::config::{CliOverrides, Config}; use skyg_linux_tracker::state::AppState; -use skyg_linux_tracker::{db, server, telemetry}; +use skyg_linux_tracker::{db, server, telemetry, tls}; #[derive(Debug, Parser)] #[command(name = "slinuxm", version, about = "Read-only Linux system monitor")] @@ -113,6 +113,20 @@ async fn serve(config: Config) -> Result<()> { None }; let addr = SocketAddr::new(config.server.bind, config.server.port); + // TLS is optional: nothing from the tls module runs unless enabled. + let tls = if config.server.tls.enabled { + let cert = tls::ReloadableCert::load(&config.server.tls) + .context("loading TLS certificate and key")?; + Some((tls::acceptor(cert.clone())?, cert)) + } else { + if !config.server.bind.is_loopback() { + tracing::warn!( + bind = %config.server.bind, + "listening on a non-loopback address without TLS: bearer tokens travel in cleartext (set server.tls.enabled or terminate TLS in front)" + ); + } + None + }; let listener = tokio::net::TcpListener::bind(addr) .await .with_context(|| format!("binding {addr}"))?; @@ -122,6 +136,9 @@ async fn serve(config: Config) -> Result<()> { let exporters = telemetry::spawn_exporters(state.clone()); let pool = state.db.clone(); let app = server::build_router(state); + if let Some((_, cert)) = &tls { + tls::spawn_sighup_reload(cert.clone(), shutdown.clone()); + } let signal_token = shutdown.clone(); tokio::spawn(async move { @@ -133,7 +150,11 @@ async fn serve(config: Config) -> Result<()> { // `serve_listener` returns once the shutdown token fires and connections // drain. If the token has not fired it is a server error, so return // immediately. Bound the drain so a stuck client cannot hang SIGTERM. - let serving = server::serve_listener(listener, app, shutdown.clone()); + let serving: std::pin::Pin> + Send>> = match tls + { + Some((acceptor, _)) => Box::pin(tls::serve_tls(listener, acceptor, app, shutdown.clone())), + None => Box::pin(server::serve_listener(listener, app, shutdown.clone())), + }; tokio::pin!(serving); let result = tokio::select! { r = &mut serving => r, diff --git a/src/mcp/mod.rs b/src/mcp/mod.rs index ebb389e..388890b 100644 --- a/src/mcp/mod.rs +++ b/src/mcp/mod.rs @@ -412,7 +412,8 @@ fn allowed_hosts(state: &AppState) -> Vec { } /// Allowed `Origin` values: loopback origins and `server.bind` on the -/// configured port, plus configured extras. +/// configured port, plus configured extras. The scheme is `https` when +/// `server.tls.enabled`, otherwise `http`. fn allowed_origins(state: &AppState) -> Vec { let s = &state.config.server; let mut hosts = vec![ @@ -423,9 +424,10 @@ fn allowed_origins(state: &AppState) -> Vec { if !s.bind.is_unspecified() { hosts.push(authority(s.bind)); } + let scheme = if s.tls.enabled { "https" } else { "http" }; let mut v: Vec = hosts .into_iter() - .map(|h| format!("http://{h}:{}", s.port)) + .map(|h| format!("{scheme}://{h}:{}", s.port)) .collect(); v.extend(s.allowed_origins.iter().cloned()); v @@ -783,6 +785,17 @@ mod tests { let _svc = service(st); } + #[test] + fn tls_switches_default_origins_to_https() { + let mut cfg = Config::default(); + cfg.server.tls.enabled = true; + let st = fake_state(cfg); + let o = allowed_origins(&st); + assert!(o.contains(&"https://localhost:9109".to_string())); + assert!(o.contains(&"https://127.0.0.1:9109".to_string())); + assert!(!o.iter().any(|x| x.starts_with("http://"))); + } + #[test] fn ipv6_non_loopback_bind_allowlists() { let mut cfg = Config::default(); diff --git a/src/tls.rs b/src/tls.rs new file mode 100644 index 0000000..4733c2d --- /dev/null +++ b/src/tls.rs @@ -0,0 +1,196 @@ +//! Native TLS (DEC-12): rustls with the `ring` provider passed explicitly to +//! the config builder (nothing is installed process-wide, so the OTLP +//! clients' provider choice is unaffected). Only used when +//! `server.tls.enabled`; the plain path never touches this module. +//! +//! The certificate lives behind a [`ResolvesServerCert`] that can be swapped +//! at runtime, so SIGHUP reload leaves established connections untouched and +//! new handshakes pick up the new certificate. Key material is never logged. + +use std::path::{Path, PathBuf}; +use std::sync::{Arc, RwLock}; +use std::time::Duration; + +use anyhow::{anyhow, Context, Result}; +use axum::Router; +use hyper_util::rt::{TokioExecutor, TokioIo}; +use hyper_util::server::conn::auto::Builder; +use hyper_util::service::TowerToHyperService; +use rustls::crypto::ring as provider; +use rustls::pki_types::pem::PemObject; +use rustls::pki_types::{CertificateDer, PrivateKeyDer}; +use rustls::server::{ClientHello, ResolvesServerCert}; +use rustls::sign::CertifiedKey; +use rustls::ServerConfig; +use tokio::task::JoinSet; +use tokio_rustls::TlsAcceptor; +use tokio_util::sync::CancellationToken; + +use crate::config::TlsConfig; + +const HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(10); + +/// Hot-swappable server certificate. +#[derive(Debug)] +pub struct ReloadableCert { + current: RwLock>, + cert_file: PathBuf, + key_file: PathBuf, +} + +impl ReloadableCert { + /// Load the initial pair; fails with a clear error naming the file. + pub fn load(cfg: &TlsConfig) -> Result> { + let key = load_certified_key(&cfg.cert_file, &cfg.key_file)?; + Ok(Arc::new(Self { + current: RwLock::new(Arc::new(key)), + cert_file: cfg.cert_file.clone(), + key_file: cfg.key_file.clone(), + })) + } + + /// Re-read the files. On error the previous certificate stays in use. + pub fn reload(&self) -> Result<()> { + let key = load_certified_key(&self.cert_file, &self.key_file)?; + *self.current.write().unwrap_or_else(|e| e.into_inner()) = Arc::new(key); + Ok(()) + } +} + +impl ResolvesServerCert for ReloadableCert { + fn resolve(&self, _hello: ClientHello<'_>) -> Option> { + Some( + self.current + .read() + .unwrap_or_else(|e| e.into_inner()) + .clone(), + ) + } +} + +fn load_certified_key(cert_file: &Path, key_file: &Path) -> Result { + let certs = CertificateDer::pem_file_iter(cert_file) + .map_err(|e| anyhow!("reading server.tls.cert_file {}: {e}", cert_file.display()))? + .collect::, _>>() + .map_err(|e| anyhow!("parsing server.tls.cert_file {}: {e}", cert_file.display()))?; + if certs.is_empty() { + return Err(anyhow!( + "server.tls.cert_file {} contains no PEM certificates", + cert_file.display() + )); + } + let key = PrivateKeyDer::from_pem_file(key_file) + .map_err(|e| anyhow!("reading server.tls.key_file {}: {e}", key_file.display()))?; + let signing = provider::sign::any_supported_type(&key).map_err(|_| { + anyhow!( + "server.tls.key_file {}: unsupported or invalid private key", + key_file.display() + ) + })?; + let ck = CertifiedKey::new(certs, signing); + ck.keys_match() + .map_err(|_| anyhow!("server.tls.key_file does not match server.tls.cert_file"))?; + Ok(ck) +} + +/// Build the acceptor around a reloadable certificate. +pub fn acceptor(cert: Arc) -> Result { + let mut cfg = ServerConfig::builder_with_provider(Arc::new(provider::default_provider())) + .with_safe_default_protocol_versions() + .context("TLS protocol versions")? + .with_no_client_auth() + .with_cert_resolver(cert); + // HTTP/1.1 only, matching the plain listener. + cfg.alpn_protocols = vec![b"http/1.1".to_vec()]; + Ok(TlsAcceptor::from(Arc::new(cfg))) +} + +/// Reload on every SIGHUP until `shutdown`. Failures are logged, never fatal. +pub fn spawn_sighup_reload(cert: Arc, shutdown: CancellationToken) { + use tokio::signal::unix::{signal, SignalKind}; + let mut hup = match signal(SignalKind::hangup()) { + Ok(s) => s, + Err(e) => { + tracing::error!(error = %e, "cannot install SIGHUP handler; TLS reload disabled"); + return; + } + }; + tokio::spawn(async move { + loop { + tokio::select! { + _ = shutdown.cancelled() => break, + r = hup.recv() => if r.is_none() { break }, + } + match cert.reload() { + Ok(()) => tracing::info!("TLS certificate reloaded"), + Err(e) => tracing::error!( + error = %format!("{e:#}"), + "TLS reload failed; keeping previous certificate" + ), + } + } + }); +} + +/// Serve `app` over TLS until `shutdown` is cancelled, then drain: stop +/// accepting, ask each connection to finish gracefully, and wait. The caller +/// bounds the drain by dropping this future (which aborts the connections). +pub async fn serve_tls( + listener: tokio::net::TcpListener, + acceptor: TlsAcceptor, + app: Router, + shutdown: CancellationToken, +) -> Result<()> { + let addr = listener.local_addr().context("local_addr")?; + tracing::info!(%addr, tls = true, "listening"); + let mut conns: JoinSet<()> = JoinSet::new(); + loop { + tokio::select! { + _ = shutdown.cancelled() => break, + Some(_) = conns.join_next(), if !conns.is_empty() => {} + r = listener.accept() => { + let (tcp, peer) = match r { + Ok(x) => x, + Err(e) => { + tracing::warn!(error = %e, "accept failed"); + tokio::time::sleep(Duration::from_millis(100)).await; + continue; + } + }; + let acceptor = acceptor.clone(); + let svc = TowerToHyperService::new(app.clone()); + let stop = shutdown.clone(); + conns.spawn(async move { + let tls = match tokio::time::timeout(HANDSHAKE_TIMEOUT, acceptor.accept(tcp)).await { + Ok(Ok(t)) => t, + Ok(Err(e)) => { + tracing::debug!(%peer, error = %e, "TLS handshake failed"); + return; + } + Err(_) => { + tracing::debug!(%peer, "TLS handshake timed out"); + return; + } + }; + let builder = Builder::new(TokioExecutor::new()); + let conn = builder.serve_connection_with_upgrades(TokioIo::new(tls), svc); + tokio::pin!(conn); + tokio::select! { + r = conn.as_mut() => { + if let Err(e) = r { tracing::debug!(%peer, error = %e, "connection error"); } + } + _ = stop.cancelled() => { + conn.as_mut().graceful_shutdown(); + if let Err(e) = conn.as_mut().await { + tracing::debug!(%peer, error = %e, "connection error"); + } + } + } + }); + } + } + } + drop(listener); + while conns.join_next().await.is_some() {} + Ok(()) +} diff --git a/tests/tls.rs b/tests/tls.rs new file mode 100644 index 0000000..1c2657d --- /dev/null +++ b/tests/tls.rs @@ -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; + } +}