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
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
//! Template repository management system

use crate::error::{Error, Result};
use crate::template::git::GitClient;
use crate::template::paths::PathResolver;
use crate::template::repository_config::{Repository, RepositoryConfig};
use chrono::Utc;
use std::path::PathBuf;
use tracing::{info, warn};

#[derive(Debug)]
/// Manager for template repository operations
pub struct RepositoryManager {
    config_path: PathBuf,
    config: RepositoryConfig,
}

impl RepositoryManager {
    /// Create a new repository manager
    pub fn new() -> Result<Self> {
        let config_path = PathResolver::user_config_dir().join("repositories.yaml");

        let config = if config_path.exists() {
            RepositoryConfig::load(&config_path)?
        } else {
            // Create default config
            let config = RepositoryConfig::default_config();

            // Create config directory if needed
            if let Some(parent) = config_path.parent() {
                std::fs::create_dir_all(parent)?;
            }

            config.save(&config_path)?;
            info!("Created default repository configuration");
            config
        };

        let mut manager = Self {
            config_path,
            config,
        };

        // @comment -- "Migrate stale official repo URL from old org to current default"
        manager.migrate_official_url()?;

        Ok(manager)
    }

    /// Migrate the official repository URL if it differs from the current default.
    /// Handles org renames (e.g. BugB-Tech -> Bugb-Technologies) transparently.
    fn migrate_official_url(&mut self) -> Result<()> {
        let default_config = RepositoryConfig::default_config();
        let Some(default_official) = default_config.repositories.first() else {
            return Ok(());
        };

        if let Some(official) = self.config.get_repository_mut("official") {
            if official.url != default_official.url {
                warn!(
                    "Migrating official repo URL: {} -> {}",
                    official.url, default_official.url
                );
                official.url = default_official.url.clone();
                self.save()?;
            }
        }

        Ok(())
    }

    /// Initialize repositories (clone if needed)
    pub fn initialize(&mut self) -> Result<()> {
        info!("Initializing template repositories");

        for repo in &mut self.config.repositories {
            if !repo.enabled {
                continue;
            }

            if repo.local_path.exists() {
                info!(
                    "Repository '{}' already exists at {}",
                    repo.name,
                    repo.local_path.display()
                );
            } else {
                info!("Cloning repository '{}'...", repo.name);
                GitClient::clone(&repo.url, &repo.local_path, &repo.branch)?;
                repo.last_updated = Some(Utc::now());
            }
        }

        self.save()?;
        Ok(())
    }

    // @comment -- "Updates a single repo: detects remote URL drift, re-clones if needed, then pulls"
    /// Update a specific repository
    pub fn update_repository(&mut self, name: &str) -> Result<()> {
        let repo = self
            .config
            .repositories
            .iter_mut()
            .find(|r| r.name == name)
            .ok_or_else(|| Error::config(format!("Repository '{}' not found", name)))?;

        if !repo.enabled {
            return Err(Error::config(format!("Repository '{}' is disabled", name)));
        }

        if !repo.local_path.exists() {
            info!("Cloning repository '{}'...", name);
            GitClient::clone(&repo.url, &repo.local_path, &repo.branch)?;
        } else {
            // Check if the configured URL differs from the on-disk remote
            if let Ok(current_url) = GitClient::get_remote_url(&repo.local_path) {
                if current_url != repo.url {
                    warn!(
                        "Repository '{}' remote URL changed: {} -> {}",
                        name, current_url, repo.url
                    );
                    info!("Re-cloning repository '{}' with new URL...", name);
                    std::fs::remove_dir_all(&repo.local_path).map_err(|e| {
                        Error::config(format!(
                            "Failed to remove old clone at {}: {}",
                            repo.local_path.display(),
                            e
                        ))
                    })?;
                    GitClient::clone(&repo.url, &repo.local_path, &repo.branch)?;
                    repo.last_updated = Some(Utc::now());
                    self.save()?;
                    info!("Successfully re-cloned repository '{}'", name);
                    return Ok(());
                }
            }

            if !GitClient::is_clean(&repo.local_path)? {
                warn!(
                    "Repository '{}' has uncommitted changes. Skipping update.",
                    name
                );
                return Ok(());
            }

            info!("Updating repository '{}'...", name);
            GitClient::pull(&repo.local_path, &repo.branch)?;
        }

        repo.last_updated = Some(Utc::now());
        self.save()?;

        info!("Successfully updated repository '{}'", name);
        Ok(())
    }

    /// Update all enabled repositories
    pub fn update_all(&mut self) -> Result<Vec<String>> {
        let mut updated = Vec::new();
        let mut errors = Vec::new();

        // Collect repository names first to avoid borrow checker issues
        let repo_names: Vec<String> = self
            .config
            .repositories
            .iter()
            .filter(|r| r.enabled)
            .map(|r| r.name.clone())
            .collect();

        // Now update each repository
        for name in repo_names {
            match self.update_repository(&name) {
                Ok(_) => updated.push(name.clone()),
                Err(e) => {
                    warn!("Failed to update '{}': {}", name, e);
                    errors.push(format!("{}: {}", name, e));
                }
            }
        }

        if !errors.is_empty() {
            warn!("Some repositories failed to update: {:?}", errors);
        }

        Ok(updated)
    }

    /// Add a new repository
    pub fn add_repository(
        &mut self,
        name: String,
        url: String,
        branch: Option<String>,
    ) -> Result<()> {
        // Check for duplicates
        if self.config.repositories.iter().any(|r| r.name == name) {
            return Err(Error::config(format!(
                "Repository '{}' already exists",
                name
            )));
        }

        let branch = branch.unwrap_or_else(|| self.config.default_branch.clone());
        let local_path = PathResolver::user_template_dir().join(&name);

        let repo = Repository {
            name: name.clone(),
            url,
            branch,
            local_path,
            enabled: true,
            trusted: false, // Require manual trust
            last_updated: None,
            description: None,
        };

        self.config.repositories.push(repo);
        self.save()?;

        info!("Added repository '{}'", name);
        Ok(())
    }

    /// Remove a repository
    pub fn remove_repository(&mut self, name: &str) -> Result<()> {
        let index = self
            .config
            .repositories
            .iter()
            .position(|r| r.name == name)
            .ok_or_else(|| Error::config(format!("Repository '{}' not found", name)))?;

        let repo = self.config.repositories.remove(index);

        // Optionally delete local files
        if repo.local_path.exists() {
            info!(
                "Repository files still exist at: {}",
                repo.local_path.display()
            );
            info!("Run 'rm -rf {}' to remove them", repo.local_path.display());
        }

        self.save()?;
        info!("Removed repository '{}'", name);
        Ok(())
    }

    /// List all repositories
    pub fn list_repositories(&self) -> &[Repository] {
        &self.config.repositories
    }

    /// Get a specific repository
    pub fn get_repository(&self, name: &str) -> Option<&Repository> {
        self.config.repositories.iter().find(|r| r.name == name)
    }

    /// Save configuration
    fn save(&self) -> Result<()> {
        self.config.save(&self.config_path)
    }

    /// Check if templates need updating (24 hour check)
    pub fn should_auto_update(&self) -> bool {
        // Check if any repository needs updating (hasn't been updated in 24h)
        for repo in &self.config.repositories {
            if !repo.enabled {
                continue;
            }

            if let Some(last_updated) = repo.last_updated {
                let hours_since_update = Utc::now().signed_duration_since(last_updated).num_hours();

                if hours_since_update >= 24 {
                    return true;
                }
            } else {
                // Never updated
                return true;
            }
        }

        false
    }

    /// Check if any templates are installed
    pub fn has_templates(&self) -> bool {
        for repo in &self.config.repositories {
            if !repo.enabled {
                continue;
            }

            if repo.local_path.exists() && repo.local_path.is_dir() {
                // Check if directory has any files
                if let Ok(entries) = std::fs::read_dir(&repo.local_path) {
                    if entries.count() > 0 {
                        return true;
                    }
                }
            }
        }

        false
    }

    /// Perform auto-update if needed (silent, non-blocking)
    pub fn auto_update_if_needed(&mut self) -> Result<()> {
        if !self.should_auto_update() {
            return Ok(());
        }

        // Silent update - just pull if needed
        for repo in self.config.repositories.clone() {
            if !repo.enabled {
                continue;
            }

            if let Err(e) = self.update_repository(&repo.name) {
                // Log but don't fail
                warn!("Auto-update failed for repository '{}': {}", repo.name, e);
            }
        }

        Ok(())
    }
}

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

    // These tests mutate the global CERT_X_GEN_HOME env var to redirect the config
    // dir, so they must run serially — otherwise one test's path leaks into another
    // under cargo's parallel runner. `into_inner` ignores poisoning from a panicked
    // test so a single failure doesn't cascade into the rest.
    static ENV_LOCK: Mutex<()> = Mutex::new(());

    #[test]
    fn test_new_repository_manager() {
        let temp_dir = TempDir::new().unwrap();

        // Override config path for testing
        let _env = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        std::env::set_var("CERT_X_GEN_HOME", temp_dir.path());

        let result = RepositoryManager::new();
        assert!(result.is_ok());

        let manager = result.unwrap();
        assert!(!manager.config.repositories.is_empty());
    }

    #[test]
    fn test_add_repository() {
        let temp_dir = TempDir::new().unwrap();
        let _env = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        std::env::set_var("CERT_X_GEN_HOME", temp_dir.path());

        let mut manager = RepositoryManager::new().unwrap();

        let result = manager.add_repository(
            "test".to_string(),
            "https://github.com/test/repo.git".to_string(),
            Some("main".to_string()),
        );

        assert!(result.is_ok());
        assert!(manager.get_repository("test").is_some());
    }

    #[test]
    fn test_add_duplicate_repository() {
        let temp_dir = TempDir::new().unwrap();
        let _env = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        std::env::set_var("CERT_X_GEN_HOME", temp_dir.path());

        let mut manager = RepositoryManager::new().unwrap();

        manager
            .add_repository(
                "test".to_string(),
                "https://github.com/test/repo.git".to_string(),
                None,
            )
            .unwrap();

        // Try to add again
        let result = manager.add_repository(
            "test".to_string(),
            "https://github.com/test/repo2.git".to_string(),
            None,
        );

        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("already exists"));
    }

    #[test]
    fn test_remove_repository() {
        let temp_dir = TempDir::new().unwrap();
        let _env = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        std::env::set_var("CERT_X_GEN_HOME", temp_dir.path());

        let mut manager = RepositoryManager::new().unwrap();

        manager
            .add_repository(
                "test".to_string(),
                "https://github.com/test/repo.git".to_string(),
                None,
            )
            .unwrap();

        let result = manager.remove_repository("test");
        assert!(result.is_ok());
        assert!(manager.get_repository("test").is_none());
    }

    #[test]
    fn test_remove_nonexistent_repository() {
        let temp_dir = TempDir::new().unwrap();
        let _env = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        std::env::set_var("CERT_X_GEN_HOME", temp_dir.path());

        let mut manager = RepositoryManager::new().unwrap();

        let result = manager.remove_repository("nonexistent");
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not found"));
    }

    #[test]
    fn test_list_repositories() {
        let temp_dir = TempDir::new().unwrap();
        let _env = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        std::env::set_var("CERT_X_GEN_HOME", temp_dir.path());

        let manager = RepositoryManager::new().unwrap();
        let repos = manager.list_repositories();

        // Should have default "official" repository
        assert_eq!(repos.len(), 1);
        assert_eq!(repos[0].name, "official");
    }

    #[test]
    fn test_get_repository() {
        let temp_dir = TempDir::new().unwrap();
        let _env = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        std::env::set_var("CERT_X_GEN_HOME", temp_dir.path());

        let mut manager = RepositoryManager::new().unwrap();

        manager
            .add_repository(
                "test".to_string(),
                "https://github.com/test/repo.git".to_string(),
                None,
            )
            .unwrap();

        let repo = manager.get_repository("test");
        assert!(repo.is_some());
        assert_eq!(repo.unwrap().name, "test");

        let missing = manager.get_repository("missing");
        assert!(missing.is_none());
    }
}