cert-x-gen 1.9.2

Advanced Multi-Language Security Scanning Engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! AI configuration management
//!
//! Handles loading, saving, and managing AI provider configurations.
//! Configuration file location: `~/.cert-x-gen/ai-config.yaml`

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;

/// Main AI configuration structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AIConfig {
    /// Default LLM provider to use
    #[serde(default = "default_provider")]
    pub default_provider: String,

    /// Provider-specific configurations
    #[serde(default)]
    pub providers: HashMap<String, ProviderConfig>,
}

/// Provider-specific configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProviderConfig {
    /// Whether this provider is enabled
    #[serde(default = "default_true")]
    pub enabled: bool,

    /// API endpoint (for local or custom endpoints)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub endpoint: Option<String>,

    /// API key (supports environment variable substitution)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub api_key: Option<String>,

    /// Model name to use
    pub model: String,

    /// Maximum tokens to generate
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_tokens: Option<u32>,

    /// Temperature for generation (0.0-2.0)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f32>,

    /// Request timeout in seconds
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timeout_secs: Option<u64>,
}

// Default value functions
fn default_provider() -> String {
    "ollama".to_string()
}

fn default_true() -> bool {
    true
}

impl Default for AIConfig {
    fn default() -> Self {
        Self {
            default_provider: default_provider(),
            providers: Self::default_providers(),
        }
    }
}

impl AIConfig {
    /// Load configuration from default location
    pub fn load() -> Result<Self> {
        let config_path = Self::config_path()?;

        if !config_path.exists() {
            // Create default configuration on first run
            let config = Self::default();
            config.save()?;
            return Ok(config);
        }

        let content = fs::read_to_string(&config_path)
            .with_context(|| format!("Failed to read AI config from {}", config_path.display()))?;

        let mut config: AIConfig =
            serde_yaml::from_str(&content).with_context(|| "Failed to parse AI configuration")?;

        // Expand environment variables in API keys
        config.expand_env_vars();

        Ok(config)
    }

    /// Save configuration to default location
    pub fn save(&self) -> Result<()> {
        let config_path = Self::config_path()?;

        // Ensure parent directory exists
        if let Some(parent) = config_path.parent() {
            fs::create_dir_all(parent).with_context(|| {
                format!("Failed to create config directory: {}", parent.display())
            })?;
        }

        let content =
            serde_yaml::to_string(self).with_context(|| "Failed to serialize AI configuration")?;

        fs::write(&config_path, content)
            .with_context(|| format!("Failed to write AI config to {}", config_path.display()))?;

        Ok(())
    }

    /// Get the configuration file path
    pub fn config_path() -> Result<PathBuf> {
        let home = dirs::home_dir().context("Failed to determine home directory")?;

        Ok(home.join(".cert-x-gen").join("ai-config.yaml"))
    }

    /// Expand environment variables in configuration
    fn expand_env_vars(&mut self) {
        for (name, provider) in self.providers.iter_mut() {
            // Clone the api_key to avoid borrow checker issues
            let api_key_template = provider.api_key.clone();

            if let Some(api_key) = api_key_template {
                if api_key.starts_with("${") && api_key.ends_with("}") {
                    let env_var = &api_key[2..api_key.len() - 1];
                    if let Ok(value) = std::env::var(env_var) {
                        if !value.is_empty() {
                            provider.api_key = Some(value);
                            // Auto-enable provider when API key is available
                            provider.enabled = true;
                            tracing::debug!(
                                "Auto-enabled provider '{}' from environment variable {}",
                                name,
                                env_var
                            );
                        }
                    }
                }
            }
        }
    }

    /// Get provider configuration by name
    pub fn get_provider(&self, name: &str) -> Option<&ProviderConfig> {
        self.providers.get(name)
    }

    /// Check if a provider is enabled
    pub fn is_provider_enabled(&self, name: &str) -> bool {
        self.get_provider(name).map(|p| p.enabled).unwrap_or(false)
    }

    /// Get the default provider name
    pub fn default_provider_name(&self) -> &str {
        &self.default_provider
    }

    /// Create default provider configurations
    fn default_providers() -> HashMap<String, ProviderConfig> {
        let mut providers = HashMap::new();

        // Ollama (local, no API key required)
        providers.insert(
            "ollama".to_string(),
            ProviderConfig {
                enabled: true,
                endpoint: Some("http://localhost:11434".to_string()),
                api_key: None,
                model: "codellama:13b".to_string(),
                max_tokens: Some(4000),
                temperature: Some(0.7),
                timeout_secs: Some(300),
            },
        );

        // OpenAI
        providers.insert(
            "openai".to_string(),
            ProviderConfig {
                enabled: false,
                endpoint: None,
                api_key: Some("${OPENAI_API_KEY}".to_string()),
                model: "gpt-4".to_string(),
                max_tokens: Some(4000),
                temperature: Some(0.7),
                timeout_secs: Some(60),
            },
        );

        // Anthropic
        providers.insert(
            "anthropic".to_string(),
            ProviderConfig {
                enabled: false,
                endpoint: None,
                api_key: Some("${ANTHROPIC_API_KEY}".to_string()),
                model: "claude-3-5-sonnet-20241022".to_string(),
                max_tokens: Some(4000),
                temperature: Some(0.7),
                timeout_secs: Some(60),
            },
        );

        // DeepSeek
        providers.insert(
            "deepseek".to_string(),
            ProviderConfig {
                enabled: false,
                endpoint: None,
                api_key: Some("${DEEPSEEK_API_KEY}".to_string()),
                model: "deepseek-coder".to_string(),
                max_tokens: Some(4000),
                temperature: Some(0.7),
                timeout_secs: Some(60),
            },
        );

        providers
    }

    /// Validate the configuration
    ///
    /// Checks for:
    /// - Valid provider references
    /// - Valid configuration values
    pub fn validate(&self) -> Result<()> {
        // Check that default provider exists
        if !self.providers.contains_key(&self.default_provider) {
            anyhow::bail!(
                "Default provider '{}' not found in providers list. Available: {}",
                self.default_provider,
                self.providers
                    .keys()
                    .map(|s| s.as_str())
                    .collect::<Vec<_>>()
                    .join(", ")
            );
        }

        // Validate provider configurations
        for (name, provider) in &self.providers {
            // Check max_tokens is reasonable
            if let Some(max_tokens) = provider.max_tokens {
                if max_tokens == 0 || max_tokens > 200_000 {
                    anyhow::bail!(
                        "Provider '{}' has invalid max_tokens: {}. Must be between 1 and 200,000",
                        name,
                        max_tokens
                    );
                }
            }

            // Check temperature is in valid range
            if let Some(temperature) = provider.temperature {
                if !(0.0..=2.0).contains(&temperature) {
                    anyhow::bail!(
                        "Provider '{}' has invalid temperature: {}. Must be between 0.0 and 2.0",
                        name,
                        temperature
                    );
                }
            }

            // Check timeout is reasonable
            if let Some(timeout) = provider.timeout_secs {
                if timeout == 0 || timeout > 600 {
                    anyhow::bail!(
                        "Provider '{}' has invalid timeout: {}s. Must be between 1 and 600 seconds",
                        name,
                        timeout
                    );
                }
            }
        }

        Ok(())
    }

    /// Update a provider's configuration
    pub fn update_provider(&mut self, name: &str, config: ProviderConfig) -> Result<()> {
        if !self.providers.contains_key(name) {
            anyhow::bail!("Provider '{}' not found", name);
        }

        self.providers.insert(name.to_string(), config);
        Ok(())
    }

    /// Enable a provider
    pub fn enable_provider(&mut self, name: &str) -> Result<()> {
        let provider = self
            .providers
            .get_mut(name)
            .context(format!("Provider '{}' not found", name))?;

        provider.enabled = true;
        Ok(())
    }

    /// Disable a provider
    pub fn disable_provider(&mut self, name: &str) -> Result<()> {
        let provider = self
            .providers
            .get_mut(name)
            .context(format!("Provider '{}' not found", name))?;

        provider.enabled = false;
        Ok(())
    }

    /// Set the default provider
    pub fn set_default_provider(&mut self, name: &str) -> Result<()> {
        if !self.providers.contains_key(name) {
            anyhow::bail!("Provider '{}' not found", name);
        }

        self.default_provider = name.to_string();
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_config() {
        let config = AIConfig::default();
        assert_eq!(config.default_provider, "ollama");
        assert!(config.providers.contains_key("ollama"));
        assert!(config.providers.contains_key("openai"));
    }

    #[test]
    fn test_serialization() {
        let config = AIConfig::default();
        let yaml = serde_yaml::to_string(&config).unwrap();
        assert!(yaml.contains("default_provider"));
        assert!(yaml.contains("ollama"));

        let deserialized: AIConfig = serde_yaml::from_str(&yaml).unwrap();
        assert_eq!(deserialized.default_provider, config.default_provider);
    }

    #[test]
    fn test_provider_access() {
        let config = AIConfig::default();

        assert!(config.is_provider_enabled("ollama"));
        assert!(!config.is_provider_enabled("openai")); // Disabled by default

        let ollama = config.get_provider("ollama").unwrap();
        assert_eq!(ollama.model, "codellama:13b");
    }

    #[test]
    fn test_validation() {
        let mut config = AIConfig::default();

        // Valid config should pass
        assert!(config.validate().is_ok());

        // Invalid default provider
        config.default_provider = "nonexistent".to_string();
        assert!(config.validate().is_err());
        config.default_provider = "ollama".to_string();

        // Invalid max_tokens
        {
            let provider = config.providers.get_mut("ollama").unwrap();
            provider.max_tokens = Some(0);
        }
        assert!(config.validate().is_err());
        {
            let provider = config.providers.get_mut("ollama").unwrap();
            provider.max_tokens = Some(4000);
        }

        // Should be valid again
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_provider_management() {
        let mut config = AIConfig::default();

        // Enable a disabled provider
        assert!(!config.is_provider_enabled("openai"));
        config.enable_provider("openai").unwrap();
        assert!(config.is_provider_enabled("openai"));

        // Disable it again
        config.disable_provider("openai").unwrap();
        assert!(!config.is_provider_enabled("openai"));

        // Try to enable non-existent provider
        assert!(config.enable_provider("nonexistent").is_err());
    }

    #[test]
    fn test_default_provider_management() {
        let mut config = AIConfig::default();

        assert_eq!(config.default_provider_name(), "ollama");

        // Change default provider
        config.set_default_provider("openai").unwrap();
        assert_eq!(config.default_provider_name(), "openai");

        // Try to set non-existent provider as default
        assert!(config.set_default_provider("nonexistent").is_err());
    }
}