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

+55
View File
@@ -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<String>,
/// 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();
+1
View File
@@ -13,3 +13,4 @@ pub mod server;
pub mod sources;
pub mod state;
pub mod telemetry;
pub mod tls;
+23 -2
View File
@@ -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<Box<dyn std::future::Future<Output = Result<()>> + 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,
+15 -2
View File
@@ -412,7 +412,8 @@ fn allowed_hosts(state: &AppState) -> Vec<String> {
}
/// 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<String> {
let s = &state.config.server;
let mut hosts = vec![
@@ -423,9 +424,10 @@ fn allowed_origins(state: &AppState) -> Vec<String> {
if !s.bind.is_unspecified() {
hosts.push(authority(s.bind));
}
let scheme = if s.tls.enabled { "https" } else { "http" };
let mut v: Vec<String> = 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();
+196
View File
@@ -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<Arc<CertifiedKey>>,
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<Arc<Self>> {
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<Arc<CertifiedKey>> {
Some(
self.current
.read()
.unwrap_or_else(|e| e.into_inner())
.clone(),
)
}
}
fn load_certified_key(cert_file: &Path, key_file: &Path) -> Result<CertifiedKey> {
let certs = CertificateDer::pem_file_iter(cert_file)
.map_err(|e| anyhow!("reading server.tls.cert_file {}: {e}", cert_file.display()))?
.collect::<Result<Vec<_>, _>>()
.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<ReloadableCert>) -> Result<TlsAcceptor> {
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<ReloadableCert>, 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(())
}