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()
28        || std::env::var(ai_code::CLIENT_KEY).is_ok()
29}
30
31/// Get mTLS configuration from environment variables
32pub fn get_mtls_config() -> Option<MTLSConfig> {
33    let mut config = MTLSConfig::default();
34
35    if let Ok(cert_path) = std::env::var(ai_code::CLIENT_CERT) {
36        if let Ok(cert) = std::fs::read_to_string(&cert_path) {
37            config.cert = Some(cert);
38        }
39    }
40
41    if let Ok(key_path) = std::env::var(ai_code::CLIENT_KEY) {
42        if let Ok(key) = std::fs::read_to_string(&key_path) {
43            config.key = Some(key);
44        }
45    }
46
47    if let Ok(passphrase) = std::env::var(ai_code::CLIENT_KEY_PASSPHRASE) {
48        config.passphrase = Some(passphrase);
49    }
50
51    if config.cert.is_none() && config.key.is_none() && config.passphrase.is_none() {
52        None
53    } else {
54        Some(config)
55    }
56}
57
58/// Get CA certificate
59pub fn get_ca_cert() -> Option<String> {
60    // Would load from caCerts config
61    None
62}
63
64/// Get client certificate
65pub fn get_client_cert() -> Option<String> {
66    get_mtls_config().and_then(|c| c.cert)
67}
68
69/// Get client key
70pub fn get_client_key() -> Option<String> {
71    get_mtls_config().and_then(|c| c.key)
72}
73
74/// Configure mTLS
75pub fn configure_mtls() {
76    // Would configure global TLS settings
77    // For now this is a stub
78}
79
80/// Clear mTLS cache
81pub fn clear_mtls_cache() {
82    // Would clear memoization cache
83}