feat(gateway): Add server-side TLS support (#15052)

This commit is contained in:
ratish
2025-12-14 15:37:55 -08:00
committed by GitHub
parent 99cb2ed988
commit 0e4108ba29
9 changed files with 312 additions and 8 deletions
+58
View File
@@ -14,6 +14,8 @@ pub struct RouterConfigBuilder {
client_cert_path: Option<String>,
client_key_path: Option<String>,
ca_cert_paths: Vec<String>,
server_cert_path: Option<String>,
server_key_path: Option<String>,
mcp_config_path: Option<String>,
}
@@ -29,6 +31,8 @@ impl RouterConfigBuilder {
client_cert_path: None,
client_key_path: None,
ca_cert_paths: Vec::new(),
server_cert_path: None,
server_key_path: None,
mcp_config_path: None,
}
}
@@ -582,6 +586,30 @@ impl RouterConfigBuilder {
self
}
// ==================== Server TLS ====================
/// Both paths must be provided together. Files read during build()
pub fn server_cert_and_key<S1: Into<String>, S2: Into<String>>(
mut self,
cert_path: S1,
key_path: S2,
) -> Self {
self.server_cert_path = Some(cert_path.into());
self.server_key_path = Some(key_path.into());
self
}
/// Files read during build()
pub fn maybe_server_cert_and_key(
mut self,
cert_path: Option<impl Into<String>>,
key_path: Option<impl Into<String>>,
) -> Self {
self.server_cert_path = cert_path.map(|p| p.into());
self.server_key_path = key_path.map(|p| p.into());
self
}
// ==================== MCP ====================
/// Config file loaded during build()
@@ -610,6 +638,9 @@ impl RouterConfigBuilder {
// Read mTLS certificates from paths if provided
self = self.read_mtls_certificates()?;
// Read Server TLS certificates from paths if provided
self = self.read_server_certificates()?;
// Read MCP config from path if provided
self = self.read_mcp_config()?;
@@ -672,6 +703,33 @@ impl RouterConfigBuilder {
Ok(self)
}
/// Internal method to read Server TLS certificates from paths
fn read_server_certificates(mut self) -> ConfigResult<Self> {
match (&self.server_cert_path, &self.server_key_path) {
(Some(cert_path), Some(key_path)) => {
let cert = std::fs::read(cert_path).map_err(|e| ConfigError::ValidationFailed {
reason: format!(
"Failed to read server certificate from {}: {}",
cert_path, e
),
})?;
let key = std::fs::read(key_path).map_err(|e| ConfigError::ValidationFailed {
reason: format!("Failed to read server key from {}: {}", key_path, e),
})?;
self.config.server_cert = Some(cert);
self.config.server_key = Some(key);
}
(None, None) => {}
_ => {
return Err(ConfigError::ValidationFailed {
reason: "Both --tls-cert-path and --tls-key-path must be specified together"
.to_string(),
});
}
}
Ok(self)
}
/// Internal method to read MCP config from path
fn read_mcp_config(mut self) -> ConfigResult<Self> {
if let Some(mcp_config_path) = &self.mcp_config_path {
+8
View File
@@ -64,6 +64,12 @@ pub struct RouterConfig {
pub tool_call_parser: Option<String>,
#[serde(default)]
pub tokenizer_cache: TokenizerCacheConfig,
/// Server TLS certificate (PEM)
#[serde(skip)]
pub server_cert: Option<Vec<u8>>,
/// Server TLS private key (PEM)
#[serde(skip)]
pub server_key: Option<Vec<u8>>,
/// Combined certificate + key in PEM format, loaded from client_cert_path and client_key_path during config creation
#[serde(skip)]
pub client_identity: Option<Vec<u8>>,
@@ -523,6 +529,8 @@ impl Default for RouterConfig {
ca_certificates: vec![],
mcp_config: None,
enable_wasm: false,
server_cert: None,
server_key: None,
}
}
}