osvm 0.8.3

OpenSVM CLI tool for managing SVM nodes and deployments
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
//! Template-based Report Generation
//!
//! This module provides template-based report generation using Tera template engine
//! to improve maintainability and reduce syntax errors in report generation.

use crate::utils::audit::{AuditFinding, AuditReport, AuditSeverity, AuditSummary, SystemInfo};
use anyhow::{Context, Result};
use rand::Rng;
use serde_json::json;
use std::collections::HashMap;
use std::path::Path;
use tera::{Context as TeraContext, Tera};

/// Template-based report generator
pub struct TemplateReportGenerator {
    tera: Tera,
}

impl TemplateReportGenerator {
    /// Create a new template report generator
    pub fn new() -> Result<Self> {
        let mut tera = Tera::default();

        // Register built-in templates
        let typst_template = include_str!("../../templates/audit_report.typ");
        tera.add_raw_template("audit_report.typ", typst_template)
            .context("Failed to register Typst template")?;

        // JSON audit report template
        let json_template = include_str!("../../templates/audit_report.json");
        tera.add_raw_template("audit_report.json", json_template)
            .context("Failed to register JSON template")?;

        // HTML audit report template
        let html_template = include_str!("../../templates/audit_report.html");
        tera.add_raw_template("audit_report.html", html_template)
            .context("Failed to register HTML template")?;

        // Markdown summary template
        let md_template = include_str!("../../templates/audit_summary.md");
        tera.add_raw_template("audit_summary.md", md_template)
            .context("Failed to register Markdown template")?;

        Ok(Self { tera })
    }

    /// Generate report using template
    pub fn generate_report(
        &self,
        report: &AuditReport,
        template_name: &str,
        output_path: &Path,
    ) -> Result<()> {
        self.generate_report_with_optional_template(report, template_name, output_path, None)
    }

    /// Generate report using template with optional external template override
    pub fn generate_report_with_optional_template(
        &self,
        report: &AuditReport,
        template_name: &str,
        output_path: &Path,
        external_template_path: Option<&str>,
    ) -> Result<()> {
        let context = self.create_template_context(report)?;

        let rendered = if let Some(external_path) = external_template_path {
            // Use external template
            let external_content = std::fs::read_to_string(external_path).context(format!(
                "Failed to read external template: {}",
                external_path
            ))?;

            // Create a temporary Tera instance for the external template
            let mut temp_tera = Tera::default();
            temp_tera
                .add_raw_template("external", &external_content)
                .context(format!(
                    "Failed to parse external template: {}",
                    external_path
                ))?;

            temp_tera.render("external", &context).context(format!(
                "Failed to render external template: {}",
                external_path
            ))?
        } else {
            // Use built-in template
            self.tera
                .render(template_name, &context)
                .context(format!("Failed to render template: {}", template_name))?
        };

        std::fs::write(output_path, rendered).context("Failed to write rendered report")?;

        Ok(())
    }

    /// Generate Typst document from audit report
    pub fn generate_typst_document(&self, report: &AuditReport, output_path: &Path) -> Result<()> {
        self.generate_report(report, "audit_report.typ", output_path)
    }

    /// Generate Typst document with optional external template
    pub fn generate_typst_document_with_template(
        &self,
        report: &AuditReport,
        output_path: &Path,
        external_template: Option<&str>,
    ) -> Result<()> {
        self.generate_report_with_optional_template(
            report,
            "audit_report.typ",
            output_path,
            external_template,
        )
    }

    /// Generate JSON report
    pub fn generate_json_report(&self, report: &AuditReport, output_path: &Path) -> Result<()> {
        self.generate_report(report, "audit_report.json", output_path)
    }

    /// Generate JSON report with optional external template
    pub fn generate_json_report_with_template(
        &self,
        report: &AuditReport,
        output_path: &Path,
        external_template: Option<&str>,
    ) -> Result<()> {
        self.generate_report_with_optional_template(
            report,
            "audit_report.json",
            output_path,
            external_template,
        )
    }

    /// Generate HTML report
    pub fn generate_html_report(&self, report: &AuditReport, output_path: &Path) -> Result<()> {
        self.generate_report(report, "audit_report.html", output_path)
    }

    /// Generate HTML report with optional external template
    pub fn generate_html_report_with_template(
        &self,
        report: &AuditReport,
        output_path: &Path,
        external_template: Option<&str>,
    ) -> Result<()> {
        self.generate_report_with_optional_template(
            report,
            "audit_report.html",
            output_path,
            external_template,
        )
    }

    /// Generate Markdown summary
    pub fn generate_markdown_summary(
        &self,
        report: &AuditReport,
        output_path: &Path,
    ) -> Result<()> {
        self.generate_report(report, "audit_summary.md", output_path)
    }

    /// Generate Markdown summary with optional external template
    pub fn generate_markdown_summary_with_template(
        &self,
        report: &AuditReport,
        output_path: &Path,
        external_template: Option<&str>,
    ) -> Result<()> {
        self.generate_report_with_optional_template(
            report,
            "audit_summary.md",
            output_path,
            external_template,
        )
    }

    /// Create template context from audit report
    fn create_template_context(&self, report: &AuditReport) -> Result<TeraContext> {
        let mut context = TeraContext::new();

        // Basic report information
        context.insert("report", report);
        context.insert(
            "timestamp",
            &report.timestamp.format("%Y-%m-%d %H:%M:%S UTC").to_string(),
        );
        context.insert("version", &report.version);

        // Summary information
        context.insert("summary", &report.summary);
        context.insert("security_score", &report.summary.security_score);
        context.insert("compliance_level", &report.summary.compliance_level);

        // Categorized findings
        let categorized_findings = self.categorize_findings(&report.findings);
        context.insert("categorized_findings", &categorized_findings);

        // Severity-based findings
        let severity_findings = self.group_by_severity(&report.findings);
        context.insert("severity_findings", &severity_findings);

        // System information
        context.insert("system_info", &report.system_info);

        // Statistics
        let stats = self.calculate_statistics(&report.findings);
        context.insert("statistics", &stats);

        // Recommendations and compliance notes
        context.insert("recommendations", &report.recommendations);
        context.insert("compliance_notes", &report.compliance_notes);

        // DeepLogic findings
        context.insert("deeplogic_findings", &report.deeplogic_findings);

        // Helper data
        context.insert("has_critical", &(report.summary.critical_findings > 0));
        context.insert("has_high", &(report.summary.high_findings > 0));
        context.insert(
            "total_serious",
            &(report.summary.critical_findings + report.summary.high_findings),
        );

        Ok(context)
    }

    /// Categorize findings by category
    fn categorize_findings<'a>(
        &self,
        findings: &'a [AuditFinding],
    ) -> HashMap<String, Vec<&'a AuditFinding>> {
        let mut categorized = HashMap::new();

        for finding in findings {
            categorized
                .entry(finding.category.clone())
                .or_insert_with(Vec::new)
                .push(finding);
        }

        categorized
    }

    /// Group findings by severity
    fn group_by_severity<'a>(
        &self,
        findings: &'a [AuditFinding],
    ) -> HashMap<String, Vec<&'a AuditFinding>> {
        let mut grouped = HashMap::new();

        for finding in findings {
            let severity = format!("{:?}", finding.severity);
            grouped
                .entry(severity)
                .or_insert_with(Vec::new)
                .push(finding);
        }

        grouped
    }

    /// Calculate additional statistics
    fn calculate_statistics(&self, findings: &[AuditFinding]) -> serde_json::Value {
        let total = findings.len();
        let with_cwe = findings.iter().filter(|f| f.cwe_id.is_some()).count();
        let with_cvss = findings.iter().filter(|f| f.cvss_score.is_some()).count();
        let with_location = findings
            .iter()
            .filter(|f| f.code_location.is_some())
            .count();

        let categories: std::collections::HashSet<_> =
            findings.iter().map(|f| &f.category).collect();
        let unique_categories = categories.len();

        let avg_cvss = if with_cvss > 0 {
            findings.iter().filter_map(|f| f.cvss_score).sum::<f32>() / with_cvss as f32
        } else {
            0.0
        };

        json!({
            "total_findings": total,
            "findings_with_cwe": with_cwe,
            "findings_with_cvss": with_cvss,
            "findings_with_location": with_location,
            "unique_categories": unique_categories,
            "average_cvss_score": avg_cvss,
            "coverage_percentage": if total > 0 { (with_location as f32 / total as f32) * 100.0 } else { 0.0 }
        })
    }

    /// Add custom template
    pub fn add_template(&mut self, name: &str, content: &str) -> Result<()> {
        self.tera
            .add_raw_template(name, content)
            .context(format!("Failed to add template: {}", name))?;
        Ok(())
    }

    /// List available templates
    pub fn list_templates(&self) -> Vec<String> {
        self.tera
            .get_template_names()
            .map(|s| s.to_string())
            .collect()
    }
}

impl Default for TemplateReportGenerator {
    fn default() -> Self {
        Self::new().expect("Failed to create template generator")
    }
}

/// Enhanced AI error handling with rate limiting and better logging
pub struct EnhancedAIErrorHandler {
    last_error_log: std::sync::Arc<std::sync::Mutex<std::time::Instant>>,
    error_log_threshold: std::time::Duration,
}

impl EnhancedAIErrorHandler {
    /// Create a new AI error handler with rate limiting
    pub fn new() -> Self {
        Self {
            last_error_log: std::sync::Arc::new(std::sync::Mutex::new(
                std::time::Instant::now() - std::time::Duration::from_secs(60),
            )),
            error_log_threshold: std::time::Duration::from_secs(30), // Log at most once every 30 seconds
        }
    }

    /// Check if we should log the error (rate limiting)
    fn should_log_error(&self) -> bool {
        if let Ok(mut last_log) = self.last_error_log.try_lock() {
            let now = std::time::Instant::now();
            if now.duration_since(*last_log) > self.error_log_threshold {
                *last_log = now;
                true
            } else {
                false
            }
        } else {
            false // If we can't acquire the lock, skip logging
        }
    }

    /// Handle AI analysis error with comprehensive but rate-limited logging
    pub fn handle_ai_error_with_context(
        &self,
        error: &anyhow::Error,
        context: &str,
    ) -> Option<String> {
        // Always log the first occurrence or after the threshold period
        if self.should_log_error() {
            log::error!("AI Analysis Error in {}: {}", context, error);
            log::warn!("AI error logging rate-limited to prevent log flooding");

            // Log the full error chain for detailed diagnostics
            let mut current_error = error.source();
            let mut error_level = 1;
            while let Some(err) = current_error {
                log::error!("  Error level {}: {}", error_level, err);
                current_error = err.source();
                error_level += 1;
            }
        }

        // Always provide fallback handling regardless of logging
        Self::handle_ai_error(error, context)
    }

    /// Handle AI analysis error with comprehensive logging (static method for backward compatibility)
    pub fn handle_ai_error(error: &anyhow::Error, _context: &str) -> Option<String> {
        // Check error type and provide specific fallback
        let error_string = error.to_string();

        if error_string.contains("rate limit") || error_string.contains("429") {
            log::warn!("Rate limit exceeded - consider reducing API calls or increasing delays");
            Some("Rate limit exceeded".to_string())
        } else if error_string.contains("timeout") {
            log::warn!("AI request timeout - network or service issues");
            Some("Request timeout".to_string())
        } else if error_string.contains("api_key") || error_string.contains("unauthorized") {
            log::error!("AI API authentication failed - check API key");
            Some("Authentication failed".to_string())
        } else if error_string.contains("network") || error_string.contains("connection") {
            log::warn!("Network connectivity issues with AI service");
            Some("Network error".to_string())
        } else {
            log::error!("Unknown AI service error - disabling AI analysis for this session");
            None
        }
    }

    /// Generate fallback analysis when AI fails
    pub fn generate_fallback_analysis(finding_title: &str, finding_category: &str) -> String {
        format!(
            "AI analysis unavailable. Manual review recommended for {} in category {}. \
            Consider checking relevant security guidelines and best practices.",
            finding_title, finding_category
        )
    }

    /// Check if error should disable AI analysis
    pub fn should_disable_ai(error: &anyhow::Error) -> bool {
        let error_string = error.to_string();
        error_string.contains("api_key")
            || error_string.contains("unauthorized")
            || error_string.contains("quota")
            || error_string.contains("suspended")
    }

    /// Create retry strategy based on error type with exponential backoff and jitter
    pub fn get_retry_strategy(error: &anyhow::Error) -> (bool, std::time::Duration) {
        let error_string = error.to_string();

        // Base delays for different error types
        let base_delay = if error_string.contains("rate limit") || error_string.contains("429") {
            60 // 1 minute base for rate limits
        } else if error_string.contains("timeout") || error_string.contains("network") {
            5 // 5 seconds base for network issues
        } else if error_string.contains("server") || error_string.contains("503") {
            30 // 30 seconds base for server issues
        } else {
            return (false, std::time::Duration::from_secs(0)); // Don't retry for other errors
        };

        // Add exponential backoff with jitter to prevent thundering herd
        let jitter = rand::random::<f64>() * 0.5 + 0.5; // Random factor between 0.5 and 1.0
        let backoff_delay = (base_delay as f64 * jitter) as u64;

        (true, std::time::Duration::from_secs(backoff_delay))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::Utc;
    use std::collections::HashMap;

    fn create_test_report() -> AuditReport {
        let findings = vec![AuditFinding {
            id: "TEST-001".to_string(),
            title: "Test finding".to_string(),
            description: "Test description".to_string(),
            severity: AuditSeverity::High,
            category: "Test".to_string(),
            cwe_id: Some("CWE-123".to_string()),
            cvss_score: Some(7.5),
            impact: "Test impact".to_string(),
            recommendation: "Test recommendation".to_string(),
            code_location: Some("test.rs".to_string()),
            references: vec!["https://example.com".to_string()],
        }];

        let summary = AuditSummary {
            total_findings: 1,
            critical_findings: 0,
            high_findings: 1,
            medium_findings: 0,
            low_findings: 0,
            info_findings: 0,
            security_score: 75.0,
            compliance_level: "Good".to_string(),
        };

        let system_info = SystemInfo {
            rust_version: "1.70.0".to_string(),
            solana_version: Some("1.16.0".to_string()),
            os_info: "Linux".to_string(),
            architecture: "x86_64".to_string(),
            dependencies: HashMap::new(),
        };

        AuditReport {
            timestamp: Utc::now(),
            version: "1.0.0".to_string(),
            summary,
            findings,
            deeplogic_findings: Vec::new(), // No DeepLogic findings in this test
            system_info,
            recommendations: vec!["Test recommendation".to_string()],
            compliance_notes: vec!["Test compliance note".to_string()],
        }
    }

    #[test]
    fn test_template_generator_creation() {
        let generator = TemplateReportGenerator::new();
        assert!(generator.is_ok());
    }

    #[test]
    fn test_categorize_findings() {
        let generator = TemplateReportGenerator::new().unwrap();
        let report = create_test_report();

        let categorized = generator.categorize_findings(&report.findings);
        assert!(categorized.contains_key("Test"));
        assert_eq!(categorized["Test"].len(), 1);
    }

    #[test]
    fn test_ai_error_handler() {
        let error = anyhow::anyhow!("rate limit exceeded");
        let result = EnhancedAIErrorHandler::handle_ai_error(&error, "test");
        assert_eq!(result, Some("Rate limit exceeded".to_string()));

        let retry_info = EnhancedAIErrorHandler::get_retry_strategy(&error);
        assert!(retry_info.0); // Should retry
        assert!(retry_info.1.as_secs() > 0); // Should have delay
    }

    #[test]
    fn test_fallback_analysis() {
        let fallback =
            EnhancedAIErrorHandler::generate_fallback_analysis("Test Finding", "Security");
        assert!(fallback.contains("Test Finding"));
        assert!(fallback.contains("Security"));
    }
}