ipfrs_interface/
tls.rs

1//! TLS/SSL Configuration for HTTPS Support
2//!
3//! Provides TLS certificate and key loading for secure HTTPS connections.
4
5use axum_server::tls_rustls::RustlsConfig;
6use std::io;
7use std::path::{Path, PathBuf};
8
9/// TLS configuration errors
10#[derive(Debug, thiserror::Error)]
11pub enum TlsError {
12    #[error("IO error: {0}")]
13    Io(#[from] io::Error),
14
15    #[error("Failed to load certificate: {0}")]
16    CertificateError(String),
17
18    #[error("Failed to load private key: {0}")]
19    PrivateKeyError(String),
20
21    #[error("TLS configuration error: {0}")]
22    ConfigError(String),
23}
24
25pub type TlsResult<T> = Result<T, TlsError>;
26
27/// TLS configuration for HTTPS server
28#[derive(Debug, Clone)]
29pub struct TlsConfig {
30    /// Path to PEM-encoded certificate file
31    pub cert_path: PathBuf,
32    /// Path to PEM-encoded private key file
33    pub key_path: PathBuf,
34}
35
36impl TlsConfig {
37    /// Create a new TLS configuration
38    pub fn new(cert_path: impl AsRef<Path>, key_path: impl AsRef<Path>) -> Self {
39        Self {
40            cert_path: cert_path.as_ref().to_path_buf(),
41            key_path: key_path.as_ref().to_path_buf(),
42        }
43    }
44
45    /// Build axum-server RustlsConfig from this TLS configuration
46    ///
47    /// This is an async method that loads the certificates and private key.
48    pub async fn build_server_config(&self) -> TlsResult<RustlsConfig> {
49        RustlsConfig::from_pem_file(&self.cert_path, &self.key_path)
50            .await
51            .map_err(|e| TlsError::ConfigError(format!("Failed to load TLS configuration: {}", e)))
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn test_tls_config_creation() {
61        let config = TlsConfig::new("cert.pem", "key.pem");
62        assert_eq!(config.cert_path, PathBuf::from("cert.pem"));
63        assert_eq!(config.key_path, PathBuf::from("key.pem"));
64    }
65
66    // Note: Actual certificate loading tests would require test certificates
67    // and are better done in integration tests
68}