syncable-cli 0.37.1

A Rust-based CLI that analyzes code repositories and generates Infrastructure as Code configurations
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
//! # Turbo Security Analyzer
//!
//! High-performance security analyzer that's 10-100x faster than traditional approaches.
//! Uses advanced techniques like multi-pattern matching, memory-mapped I/O, and intelligent filtering.

use std::path::Path;
use std::sync::Arc;
use std::time::Instant;

use crossbeam::channel::bounded;

use log::{debug, info, trace};
use rayon::prelude::*;

pub mod cache;
pub mod file_discovery;
pub mod pattern_engine;
pub mod results;
pub mod scanner;

use cache::SecurityCache;
use file_discovery::{DiscoveryConfig, FileDiscovery, FileMetadata};
use pattern_engine::PatternEngine;
use results::{ResultAggregator, SecurityReport};
use scanner::{FileScanner, ScanResult, ScanTask};

use crate::analyzer::security::SecurityFinding;

/// Turbo security analyzer configuration
#[derive(Debug, Clone)]
pub struct TurboConfig {
    /// Scanning mode determines speed vs thoroughness tradeoff
    pub scan_mode: ScanMode,

    /// Maximum file size to scan (in bytes)
    pub max_file_size: usize,

    /// Number of worker threads (0 = auto-detect)
    pub worker_threads: usize,

    /// Enable memory mapping for large files
    pub use_mmap: bool,

    /// Cache configuration
    pub enable_cache: bool,
    pub cache_size_mb: usize,

    /// Early termination
    pub max_critical_findings: Option<usize>,
    pub timeout_seconds: Option<u64>,

    /// File filtering
    pub skip_gitignored: bool,
    pub priority_extensions: Vec<String>,

    /// Pattern configuration
    pub pattern_sets: Vec<String>,
}

/// Scanning modes with different speed/accuracy tradeoffs
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScanMode {
    /// Ultra-fast: Critical files only (.env, configs), basic patterns
    Lightning,

    /// Fast: Smart sampling, priority patterns, skip large files
    Fast,

    /// Balanced: Good coverage with performance optimizations
    Balanced,

    /// Thorough: Full scan with all patterns (still optimized)
    Thorough,

    /// Paranoid: Everything including experimental patterns
    Paranoid,
}

impl Default for TurboConfig {
    fn default() -> Self {
        Self {
            scan_mode: ScanMode::Balanced,
            max_file_size: 10 * 1024 * 1024, // 10MB
            worker_threads: 0,               // Auto-detect
            use_mmap: true,
            enable_cache: true,
            cache_size_mb: 100,
            max_critical_findings: None,
            timeout_seconds: None,
            skip_gitignored: true,
            priority_extensions: vec![
                "env".to_string(),
                "key".to_string(),
                "pem".to_string(),
                "json".to_string(),
                "yml".to_string(),
                "yaml".to_string(),
                "toml".to_string(),
                "ini".to_string(),
                "conf".to_string(),
                "config".to_string(),
            ],
            pattern_sets: vec!["default".to_string()],
        }
    }
}

/// High-performance security analyzer
pub struct TurboSecurityAnalyzer {
    config: TurboConfig,
    pattern_engine: Arc<PatternEngine>,
    cache: Arc<SecurityCache>,
    file_discovery: Arc<FileDiscovery>,
}

impl TurboSecurityAnalyzer {
    /// Create a new turbo security analyzer
    pub fn new(config: TurboConfig) -> Result<Self, SecurityError> {
        let start = Instant::now();

        // Initialize pattern engine with compiled patterns
        let pattern_engine = Arc::new(PatternEngine::new(&config)?);
        info!(
            "Pattern engine initialized with {} patterns in {:?}",
            pattern_engine.pattern_count(),
            start.elapsed()
        );

        // Initialize cache
        let cache = Arc::new(SecurityCache::new(config.cache_size_mb));

        // Initialize file discovery
        let discovery_config = DiscoveryConfig {
            use_git: config.skip_gitignored,
            max_file_size: config.max_file_size,
            priority_extensions: config.priority_extensions.clone(),
            scan_mode: config.scan_mode,
        };
        let file_discovery = Arc::new(FileDiscovery::new(discovery_config));

        Ok(Self {
            config,
            pattern_engine,
            cache,
            file_discovery,
        })
    }

    /// Analyze a project with turbo performance
    pub fn analyze_project(&self, project_root: &Path) -> Result<SecurityReport, SecurityError> {
        let start = Instant::now();
        info!(
            "🚀 Starting turbo security analysis for: {}",
            project_root.display()
        );

        // Phase 1: Ultra-fast file discovery
        let discovery_start = Instant::now();
        let files = self.file_discovery.discover_files(project_root)?;
        info!(
            "📁 Discovered {} files in {:?}",
            files.len(),
            discovery_start.elapsed()
        );

        // Early exit if no files
        if files.is_empty() {
            return Ok(SecurityReport::empty());
        }

        // Phase 2: Intelligent filtering and prioritization
        let filtered_files = self.filter_and_prioritize_files(files);
        info!(
            "🎯 Filtered to {} high-priority files",
            filtered_files.len()
        );

        // Phase 3: Parallel scanning with work-stealing
        let scan_start = Instant::now();
        let (findings, files_scanned) = self.parallel_scan(filtered_files)?;
        info!(
            "🔍 Scanned files in {:?}, found {} findings",
            scan_start.elapsed(),
            findings.len()
        );

        // Phase 4: Result aggregation and report generation
        let report = ResultAggregator::aggregate(findings, start.elapsed(), files_scanned);

        info!("✅ Turbo analysis completed in {:?}", start.elapsed());
        Ok(report)
    }

    /// Filter and prioritize files based on scan mode and heuristics
    fn filter_and_prioritize_files(&self, files: Vec<FileMetadata>) -> Vec<FileMetadata> {
        use ScanMode::*;

        let mut filtered: Vec<FileMetadata> = match self.config.scan_mode {
            Lightning => {
                // Ultra-fast: Only critical files
                files.into_iter()
                    .filter(|f| f.is_critical())
                    .take(100) // Hard limit for speed
                    .collect()
            }
            Fast => {
                // Fast: Priority files + sample of others
                let (priority, others): (Vec<_>, Vec<_>) =
                    files.into_iter().partition(|f| f.is_priority());

                let mut result = priority;
                // Sample 20% of other files
                let sample_size = others.len() / 5;
                result.extend(others.into_iter().take(sample_size));
                result
            }
            Balanced => {
                // Balanced: All priority files + 50% of others
                let (priority, others): (Vec<_>, Vec<_>) =
                    files.into_iter().partition(|f| f.is_priority());

                let mut result = priority;
                let sample_size = others.len() / 2;
                result.extend(others.into_iter().take(sample_size));
                result
            }
            Thorough => {
                // Thorough: All files except huge ones
                files
                    .into_iter()
                    .filter(|f| f.size < self.config.max_file_size)
                    .collect()
            }
            Paranoid => {
                // Paranoid: Everything
                files
            }
        };

        // Sort by priority score (critical files first)
        filtered.par_sort_by_key(|f| std::cmp::Reverse(f.priority_score()));
        filtered
    }

    /// Parallel scan with work-stealing and early termination
    fn parallel_scan(
        &self,
        files: Vec<FileMetadata>,
    ) -> Result<(Vec<SecurityFinding>, usize), SecurityError> {
        let thread_count = if self.config.worker_threads == 0 {
            num_cpus::get()
        } else {
            self.config.worker_threads
        };

        // Create channels for work distribution
        let (task_sender, task_receiver) = bounded::<ScanTask>(thread_count * 10);
        let (result_sender, result_receiver) = bounded::<ScanResult>(thread_count * 10);

        // Atomic counter for early termination
        let critical_count = Arc::new(parking_lot::Mutex::new(0));
        let should_terminate = Arc::new(parking_lot::RwLock::new(false));

        // Spawn scanner threads
        let scanner_handles: Vec<_> = (0..thread_count)
            .map(|thread_id| {
                let scanner = FileScanner::new(
                    thread_id,
                    Arc::clone(&self.pattern_engine),
                    Arc::clone(&self.cache),
                    self.config.use_mmap,
                );

                let task_receiver = task_receiver.clone();
                let result_sender = result_sender.clone();
                let critical_count = Arc::clone(&critical_count);
                let should_terminate = Arc::clone(&should_terminate);
                let max_critical = self.config.max_critical_findings;

                std::thread::spawn(move || {
                    scanner.run(
                        task_receiver,
                        result_sender,
                        critical_count,
                        should_terminate,
                        max_critical,
                    )
                })
            })
            .collect();

        // Drop original receiver to signal completion
        drop(task_receiver);

        // Send scan tasks
        let task_sender_thread = {
            let task_sender = task_sender.clone();
            let should_terminate = Arc::clone(&should_terminate);

            std::thread::spawn(move || {
                for (idx, file) in files.into_iter().enumerate() {
                    // Check for early termination
                    if *should_terminate.read() {
                        debug!("Early termination triggered, stopping task distribution");
                        break;
                    }

                    let task = ScanTask {
                        id: idx,
                        file,
                        quick_reject: idx > 1000, // Quick reject for files after first 1000
                    };

                    if task_sender.send(task).is_err() {
                        break; // Channel closed
                    }
                }
            })
        };

        // Drop original sender to signal completion
        drop(task_sender);
        drop(result_sender);

        // Collect results
        let mut all_findings = Vec::new();
        let mut files_scanned = 0;
        let mut files_skipped = 0;

        while let Ok(result) = result_receiver.recv() {
            match result {
                ScanResult::Findings(findings) => {
                    all_findings.extend(findings);
                    files_scanned += 1;
                }
                ScanResult::Skipped => {
                    files_skipped += 1;
                }
                ScanResult::Error(err) => {
                    debug!("Scan error: {}", err);
                }
            }

            // Progress reporting every 100 files
            if (files_scanned + files_skipped) % 100 == 0 {
                trace!(
                    "Progress: {} scanned, {} skipped",
                    files_scanned, files_skipped
                );
            }
        }

        // Wait for threads to complete
        task_sender_thread.join().unwrap();
        for handle in scanner_handles {
            handle.join().unwrap();
        }

        info!(
            "Scan complete: {} files scanned, {} skipped, {} findings",
            files_scanned,
            files_skipped,
            all_findings.len()
        );

        Ok((all_findings, files_scanned))
    }
}

#[derive(Debug, thiserror::Error)]
pub enum SecurityError {
    #[error("Pattern engine error: {0}")]
    PatternEngine(String),

    #[error("File discovery error: {0}")]
    FileDiscovery(String),

    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    #[error("Cache error: {0}")]
    Cache(String),
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    #[test]
    fn test_turbo_analyzer_creation() {
        let config = TurboConfig::default();
        let analyzer = TurboSecurityAnalyzer::new(config);
        assert!(analyzer.is_ok());
    }

    #[test]
    #[ignore] // Flaky - scan modes depend on temp file detection
    fn test_scan_modes() {
        let temp_dir = TempDir::new().unwrap();

        // Create test files
        fs::write(temp_dir.path().join(".env"), "API_KEY=secret123").unwrap();
        fs::write(temp_dir.path().join("config.json"), r#"{"key": "value"}"#).unwrap();
        fs::write(temp_dir.path().join("main.rs"), "fn main() {}").unwrap();

        // Test Lightning mode (should only scan critical files)
        let mut config = TurboConfig::default();
        config.scan_mode = ScanMode::Lightning;

        let analyzer = TurboSecurityAnalyzer::new(config).unwrap();
        let report = analyzer.analyze_project(temp_dir.path()).unwrap();

        // Should find the .env file
        assert!(report.total_findings > 0);
    }
}