chamber-secret-manager-cli 0.5.3

CLI interface for Chamber secrets manager
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
use chamber_backup::BackupManager;
use chamber_import_export::{detect_format_from_extension, import_items};
use chamber_vault::{BackupConfig, Vault};
use clap::Subcommand;
use color_eyre::eyre::eyre;
use std::path::{Path, PathBuf};

#[derive(Subcommand, Debug)]
pub enum BackupCommand {
    /// Configure automatic backups
    Configure {
        /// Enable or disable automatic backups
        #[arg(long)]
        enable: Option<bool>,
        /// Backup interval in hours
        #[arg(long)]
        interval: Option<u64>,
        /// Maximum number of backups to retain
        #[arg(long)]
        max_backups: Option<usize>,
        /// Backup directory path
        #[arg(long)]
        backup_dir: Option<PathBuf>,
        /// Backup format (json, csv, backup)
        #[arg(long)]
        format: Option<String>,
        /// Enable/disable compression
        #[arg(long)]
        compress: Option<bool>,
        /// Enable/disable backup verification
        #[arg(long)]
        verify: Option<bool>,
    },
    /// Create a backup immediately
    Now {
        /// Optional custom backup path
        #[arg(long)]
        output: Option<PathBuf>,
        /// Force backup even if one was recently created
        #[arg(long)]
        force: bool,
    },
    /// List all existing backups
    List {
        /// Show detailed information
        #[arg(long, short)]
        verbose: bool,
    },
    /// Restore from a backup file
    Restore {
        /// Path to the backup file
        backup_path: PathBuf,
        /// Skip confirmation prompt
        #[arg(long, short)]
        yes: bool,
    },
    /// Verify backup integrity
    Verify {
        /// Path to the backup file to verify
        backup_path: PathBuf,
    },
    /// Show current backup configuration
    Status,
    /// Clean up old backups manually
    Cleanup {
        /// Number of backups to keep (overrides config)
        #[arg(long)]
        keep: Option<usize>,
        /// Perform a dry run (show what would be deleted)
        #[arg(long)]
        dry_run: bool,
    },
}

/// Handles backup-related commands issued by the user.
///
/// This function processes a given `BackupCommand` by delegating it to the appropriate
/// handler based on the command variant. The function works on the provided `Vault` instance
/// and executes the associated functionality.
///
/// # Arguments
///
/// * `vault` - A `Vault` instance representing the storage system or environment where backups
///   are managed.
/// * `cmd` - An instance of the `BackupCommand` enum, specifying the backup operation to be performed.
///
/// # Variants of `BackupCommand`
///
/// - `Configure`:
///   Configures backup settings such as enabling/disabling backup, setting backup intervals,
///   maximum number of backups to retain, backup directory, format, compression settings,
///   and verification options.
///
///   * `enable` - Whether to enable or disable backups.
///   * `interval` - The interval between automatic backups (if enabled).
///   * `max_backups` - Maximum number of backups to retain.
///   * `backup_dir` - Directory where backups are saved.
///   * `format` - Format of the backup files.
///   * `compress` - Whether to compress backups.
///   * `verify` - Whether to verify backups after creation.
///
/// - `Now`:
///   Immediately initiates a backup.
///   * `output` - Optional output location for the backup.
///   * `force` - Whether to force the backup operation, ignoring certain conditions.
///
/// - `List`:
///   Lists existing backups.
///   * `verbose` - If `true`, provides detailed information about each backup.
///
/// - `Restore`:
///   Restores a specific backup.
///   * `backup_path` - Path to the backup file to restore from.
///   * `yes` - If `true`, skips confirmation prompts.
///
/// - `Verify`:
///   Verifies the integrity of a specific backup file.
///   * `backup_path` - Path to the backup file to verify.
///
/// - `Status`:
///   Provides the current backup status, including information about recent backups
///   and configuration status.
///
/// - `Cleanup`:
///   Cleans up old or excess backups based on retention rules.
///   * `keep` - Number of recent backups to retain.
///   * `dry_run` - If `true`, performs a simulation of the cleanup process without deleting files.
///
/// # Returns
///
/// * `Ok(())` - If the specified command is successfully executed.
/// * `Err` - If an error occurs during processing. The specific error depends on the invoked operation.
///
/// # Errors
///
/// This function will return an error if:
/// - The supplied arguments are invalid.
/// - The `Vault` is inaccessible or improperly configured.
/// - File operations (e.g., reading/writing backups) encounter an issue.
/// - A requested backup file is not found, corrupted, or fails verification.
///
pub fn handle_backup_command(vault: Vault, cmd: BackupCommand) -> color_eyre::Result<()> {
    match cmd {
        BackupCommand::Configure {
            enable,
            interval,
            max_backups,
            backup_dir,
            format,
            compress,
            verify,
        } => handle_configure(
            &vault,
            enable,
            interval,
            max_backups,
            backup_dir,
            format,
            compress,
            verify,
        ),

        BackupCommand::Now { output, force } => handle_backup_now(vault, output, force),

        BackupCommand::List { verbose } => handle_list_backups(vault, verbose),

        BackupCommand::Restore { backup_path, yes } => handle_restore_backup(vault, &backup_path, yes),

        BackupCommand::Verify { backup_path } => handle_verify_backup(&backup_path),

        BackupCommand::Status => handle_backup_status(vault),

        BackupCommand::Cleanup { keep, dry_run } => handle_cleanup_backups(vault, keep, dry_run),
    }
}

#[allow(clippy::too_many_arguments)]
fn handle_configure(
    vault: &Vault,
    enable: Option<bool>,
    interval: Option<u64>,
    max_backups: Option<usize>,
    backup_dir: Option<PathBuf>,
    format: Option<String>,
    compress: Option<bool>,
    verify: Option<bool>,
) -> color_eyre::Result<()> {
    let mut config = vault.get_backup_config().unwrap_or_default();
    let mut changed = false;

    if let Some(enabled) = enable {
        config.enabled = enabled;
        changed = true;
        println!("Automatic backups {}", if enabled { "enabled" } else { "disabled" });
    }

    if let Some(hours) = interval {
        if hours == 0 {
            return Err(eyre!("Backup interval must be greater than 0"));
        }
        config.interval_hours = hours;
        changed = true;
        println!("Backup interval set to {hours} hours");
    }

    if let Some(max) = max_backups {
        if max == 0 {
            return Err(eyre!("Max backups must be greater than 0"));
        }
        config.max_backups = max;
        changed = true;
        println!("Maximum backups set to {max}");
    }

    if let Some(dir) = backup_dir {
        config.backup_dir.clone_from(&dir);
        changed = true;
        println!("Backup directory set to: {}", dir.display());
    }

    if let Some(fmt) = format {
        match fmt.as_str() {
            "json" | "csv" | "backup" => {
                config.format.clone_from(&fmt);
                changed = true;
                println!("Backup format set to: {fmt}");
            }
            _ => return Err(eyre!("Invalid format '{}'. Use: json, csv, or backup", fmt)),
        }
    }

    if let Some(comp) = compress {
        config.compress = comp;
        changed = true;
        println!("Compression {}", if comp { "enabled" } else { "disabled" });
    }

    if let Some(ver) = verify {
        config.verify_after_backup = ver;
        changed = true;
        println!("Backup verification {}", if ver { "enabled" } else { "disabled" });
    }

    if changed {
        vault.set_backup_config(&config)?;
        println!("Backup configuration updated successfully");
    } else {
        println!("No configuration changes specified");
        print_backup_config(&vault.get_backup_config().unwrap_or_default());
    }

    Ok(())
}

fn handle_backup_now(mut vault: Vault, output: Option<PathBuf>, force: bool) -> color_eyre::Result<()> {
    // Unlock vault if needed
    if !vault.is_unlocked() {
        let password = rpassword::prompt_password("Enter master password: ")?;
        vault.unlock(&password)?;
    }

    let config = if let Some(custom_path) = output {
        let mut config = vault.get_backup_config().unwrap_or_default();
        config.backup_dir = custom_path
            .parent()
            .unwrap_or_else(|| std::path::Path::new("."))
            .to_path_buf();
        config
    } else {
        vault.get_backup_config().unwrap_or_default()
    };

    let mut backup_manager = BackupManager::new(vault, config);

    println!("Creating backup...");
    let backup_path = if force {
        backup_manager.force_backup()?
    } else {
        backup_manager
            .backup_if_needed()?
            .ok_or_else(|| eyre!("No backup needed (use --force to create anyway)"))?
    };

    println!("✅ Backup created successfully: {}", backup_path.display());

    // Show backup info
    let metadata = std::fs::metadata(&backup_path)?;
    println!("   Size: {} bytes", metadata.len());
    println!(
        "   Created: {}",
        chrono::DateTime::<chrono::Utc>::from(metadata.created()?).format("%Y-%m-%d %H:%M:%S UTC")
    );

    Ok(())
}

fn handle_list_backups(vault: Vault, verbose: bool) -> color_eyre::Result<()> {
    let config = vault.get_backup_config().unwrap_or_default();
    let backup_manager = BackupManager::new(vault, config);

    let backups = backup_manager.find_all_backups()?;

    if backups.is_empty() {
        println!("No backups found in: {}", backup_manager.config.backup_dir.display());
        return Ok(());
    }

    println!("Found {} backup(s):", backups.len());
    println!();

    #[allow(clippy::cast_precision_loss)]
    #[allow(clippy::expect_used)]
    for (i, backup_path) in backups.iter().enumerate() {
        let metadata = std::fs::metadata(backup_path)?;
        let size = metadata.len();
        let modified = metadata.modified()?;
        let datetime = chrono::DateTime::<chrono::Utc>::from(modified);

        println!(
            "{}. {}",
            i + 1,
            backup_path
                .file_name()
                .expect("Unable to get the file name")
                .to_string_lossy()
        );
        println!("   Path: {}", backup_path.display());
        println!("   Size: {} bytes ({:.2} MB)", size, size as f64 / 1_048_576.0);
        println!("   Date: {}", datetime.format("%Y-%m-%d %H:%M:%S UTC"));

        if verbose {
            // Try to extract additional info from the backup
            if let Some(timestamp) = backup_manager.extract_timestamp_from_filename(backup_path) {
                println!(
                    "   Timestamp: {}",
                    timestamp.format(&time::format_description::well_known::Rfc3339)?
                );
            }
        }
        println!();
    }

    Ok(())
}

fn handle_restore_backup(mut vault: Vault, backup_path: &Path, skip_confirmation: bool) -> color_eyre::Result<()> {
    if !backup_path.exists() {
        return Err(eyre!("Backup file not found: {}", backup_path.display()));
    }

    // Verify backup first
    println!("Verifying backup integrity...");
    verify_backup_file(backup_path)?;
    println!("✅ Backup verification passed");

    if !skip_confirmation {
        println!("⚠️  WARNING: This will replace all current vault data!");
        print!("Are you sure you want to restore from backup? (y/N): ");
        std::io::Write::flush(&mut std::io::stdout())?;

        let mut input = String::new();
        std::io::stdin().read_line(&mut input)?;

        if !matches!(input.trim().to_lowercase().as_str(), "y" | "yes") {
            println!("Restore cancelled");
            return Ok(());
        }
    }

    // Unlock vault
    let password = rpassword::prompt_password("Enter master password: ")?;
    vault.unlock(&password)?;

    // Detect backup format
    let format = chamber_import_export::detect_format_from_extension(backup_path)
        .unwrap_or(chamber_import_export::ExportFormat::Json);

    println!("Importing backup data...");
    let items = chamber_import_export::import_items(backup_path, &format)?;

    println!("Found {} items in backup", items.len());

    // Clear existing items (if user confirmed)
    let existing_items = vault.list_items()?;
    for item in existing_items {
        vault.delete_item(item.id)?;
    }

    // Import new items
    let mut imported_count = 0;
    for item in items {
        match vault.create_item(&item) {
            Ok(()) => imported_count += 1,
            Err(e) => println!("⚠️  Failed to import '{}': {}", item.name, e),
        }
    }

    println!("✅ Successfully imported {imported_count} items from backup");
    Ok(())
}

fn handle_verify_backup(backup_path: &PathBuf) -> color_eyre::Result<()> {
    println!("Verifying backup: {}", backup_path.display());

    verify_backup_file(backup_path)?;

    println!("✅ Backup verification passed");

    // Show additional info
    let metadata = std::fs::metadata(backup_path)?;
    println!("   Size: {} bytes", metadata.len());

    // Try to get item count
    if let Some(format) = chamber_import_export::detect_format_from_extension(backup_path) {
        if let Ok(items) = chamber_import_export::import_items(backup_path, &format) {
            println!("   Items: {}", items.len());
        }
    }

    Ok(())
}

fn handle_backup_status(vault: Vault) -> color_eyre::Result<()> {
    let config = vault.get_backup_config().unwrap_or_default();

    println!("🔒 Backup Configuration Status");
    println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
    print_backup_config(&config);

    // Show last backup info
    let backup_manager = BackupManager::new(vault, config);
    if let Ok(Some(last_backup)) = backup_manager.find_most_recent_backup() {
        println!();
        println!("📁 Most Recent Backup:");
        println!("   File: {}", last_backup.display());

        if let Ok(metadata) = std::fs::metadata(&last_backup) {
            let modified = metadata.modified()?;
            let datetime = chrono::DateTime::<chrono::Utc>::from(modified);
            println!("   Date: {}", datetime.format("%Y-%m-%d %H:%M:%S UTC"));
            println!("   Size: {} bytes", metadata.len());
        }
    } else {
        println!();
        println!("📁 No backups found");
    }

    Ok(())
}

fn handle_cleanup_backups(vault: Vault, keep: Option<usize>, dry_run: bool) -> color_eyre::Result<()> {
    let mut config = vault.get_backup_config().unwrap_or_default();

    if let Some(keep_count) = keep {
        config.max_backups = keep_count;
    }

    let backup_manager = BackupManager::new(vault, config.clone());
    let all_backups = backup_manager.find_all_backups()?;

    if all_backups.len() <= config.max_backups {
        println!(
            "No cleanup needed. Found {} backups, keeping {}",
            all_backups.len(),
            config.max_backups
        );
        return Ok(());
    }

    let to_delete = all_backups.len() - config.max_backups;
    println!(
        "Found {} backups, will {} {} oldest backup(s)",
        all_backups.len(),
        if dry_run { "identify" } else { "delete" },
        to_delete
    );

    // Sort by timestamp (the oldest first for deletion)
    let mut sorted_backups = all_backups;
    sorted_backups.sort_by(|a, b| {
        let time_a = backup_manager
            .extract_timestamp_from_filename(a)
            .unwrap_or(time::OffsetDateTime::UNIX_EPOCH);
        let time_b = backup_manager
            .extract_timestamp_from_filename(b)
            .unwrap_or(time::OffsetDateTime::UNIX_EPOCH);
        time_a.cmp(&time_b)
    });

    for (i, backup_path) in sorted_backups.iter().take(to_delete).enumerate() {
        if dry_run {
            println!("  [{}] Would delete: {}", i + 1, backup_path.display());
        } else {
            match std::fs::remove_file(backup_path) {
                Ok(()) => println!("  [{}] Deleted: {}", i + 1, backup_path.display()),
                Err(e) => println!("  [{}] Failed to delete {}: {}", i + 1, backup_path.display(), e),
            }
        }
    }

    if dry_run {
        println!("Dry run completed. Use without --dry-run to actually delete files.");
    } else {
        println!("✅ Cleanup completed");
    }

    Ok(())
}

fn print_backup_config(config: &BackupConfig) {
    println!(
        "   Status: {}",
        if config.enabled { "✅ Enabled" } else { "❌ Disabled" }
    );
    println!("   Directory: {}", config.backup_dir.display());
    println!("   Interval: {} hours", config.interval_hours);
    println!("   Max backups: {}", config.max_backups);
    println!("   Format: {}", config.format);
    println!(
        "   Compression: {}",
        if config.compress { "✅ Enabled" } else { "❌ Disabled" }
    );
    println!(
        "   Verification: {}",
        if config.verify_after_backup {
            "✅ Enabled"
        } else {
            "❌ Disabled"
        }
    );
}

fn verify_backup_file(backup_path: &std::path::Path) -> color_eyre::Result<()> {
    if !backup_path.exists() {
        return Err(eyre!("Backup file does not exist"));
    }

    let metadata = std::fs::metadata(backup_path)?;
    if metadata.len() == 0 {
        return Err(eyre!("Backup file is empty"));
    }

    // Try to parse the backup based on format
    if let Some(format) = detect_format_from_extension(backup_path) {
        import_items(backup_path, &format)?;
    } else {
        return Err(eyre!("Unable to detect backup format"));
    }

    Ok(())
}