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

+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(())
}