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();