Skip to main content

ai_agent/utils/
mtls.rs

1// Source: /data/home/swei/claudecode/openclaudecode/src/utils/mtls.ts
2//! mTLS (mutual TLS) configuration utilities.
3
4use crate::constants::env::ai_code;
5use once_cell::sync::Lazy;
6use std::sync::Mutex;
7
8/// mTLS configuration
9#[derive(Debug, Clone, Default)]
10pub struct MTLSConfig {
11    pub cert: Option<String>,
12    pub key: Option<String>,
13    pub passphrase: Option<String>,
14}
15
16/// TLS configuration including mTLS and CA certificates
17#[derive(Debug, Clone, Default)]
18pub struct TLSConfig {
19    pub cert: Option<String>,
20    pub key: Option<String>,
21    pub passphrase: Option<String>,
22    pub ca: Option<Vec<String>>,
23}
24
25/// Check if mTLS is enabled
26pub fn is_mtls_enabled() -> bool {
27    std::env::var(ai_code::CLIENT_CERT).is_ok() || std::env::var(ai_code::CLIENT_KEY).is_ok()
28}
29
30/// Get mTLS configuration from environment variables
31pub fn get_mtls_config() -> Option<MTLSConfig> {
32    let mut config = MTLSConfig::default();
33
34    if let Ok(cert_path) = std::env::var(ai_code::CLIENT_CERT) {
35        if let Ok(cert) = std::fs::read_to_string(&cert_path) {
36            config.cert = Some(cert);
37        }
38    }
39
40    if let Ok(key_path) = std::env::var(ai_code::CLIENT_KEY) {
41        if let Ok(key) = std::fs::read_to_string(&key_path) {
42            config.key = Some(key);
43        }
44    }
45
46    if let Ok(passphrase) = std::env::var(ai_code::CLIENT_KEY_PASSPHRASE) {
47        config.passphrase = Some(passphrase);
48    }
49
50    if config.cert.is_none() && config.key.is_none() && config.passphrase.is_none() {
51        None
52    } else {
53        Some(config)
54    }
55}
56
57/// Get CA certificate
58pub fn get_ca_cert() -> Option<String> {
59    // Would load from caCerts config
60    None
61}
62
63/// Get client certificate
64pub fn get_client_cert() -> Option<String> {
65    get_mtls_config().and_then(|c| c.cert)
66}
67
68/// Get client key
69pub fn get_client_key() -> Option<String> {
70    get_mtls_config().and_then(|c| c.key)
71}
72
73/// Configure mTLS
74pub fn configure_mtls() {
75    // Would configure global TLS settings
76    // For now this is a stub
77}
78
79/// Clear mTLS cache
80pub fn clear_mtls_cache() {
81    // Would clear memoization cache
82}