purple-ssh 3.29.2

Free, open-source terminal SSH manager and SSH config editor in Rust for macOS and Linux that keeps ~/.ssh/config in sync with 18 cloud providers, monitors live SSH tunnels and manages Docker and Podman containers fleet-wide. Spin up a VM, it shows up. Destroy it, it dims. Plus scp, Vault SSH certs and an MCP server for AI agents. MIT licensed.
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
// Seeds the split category directories from the legacy `~/.purple` tree the
// first time a category resolves somewhere else. Runs once at process start,
// before logging opens the log file in the state directory.

use std::fs;
use std::io;
use std::path::{Path, PathBuf};

use super::env::{Category, Paths};

/// Legacy entries per category, relative to `~/.purple`. Askpass retry
/// markers expire within a minute and are not carried over.
fn legacy_entries(category: Category) -> &'static [&'static str] {
    match category {
        Category::Config => &["preferences", "snippets", "providers", "themes"],
        Category::Data => &["certs", "config.original"],
        Category::State => &[
            "history.tsv",
            "recents.json",
            "key_activity.json",
            "snippet_runs.json",
            "sync_history.tsv",
            "purple.log",
            "mcp-audit.log",
        ],
        Category::Cache => &["container_cache.jsonl", "last_version_check"],
    }
}

/// What one startup migration did. Logged by the launcher once the log file
/// is open, since the state directory holding that file may itself be a
/// migration target.
#[derive(Debug, Default, PartialEq, Eq)]
pub struct MigrationReport {
    /// `(legacy source, new location)` pairs that were copied.
    pub copied: Vec<(PathBuf, PathBuf)>,
    /// `(legacy source, error text)` pairs for copies that failed.
    pub failed: Vec<(PathBuf, String)>,
}

impl MigrationReport {
    pub fn is_empty(&self) -> bool {
        self.copied.is_empty() && self.failed.is_empty()
    }
}

/// Copy legacy files into every category directory that resolved outside
/// `~/.purple` and holds none of that category's files yet. Directories are
/// created with mode 0700; copied files keep their mode. The legacy copies
/// stay in place, so a user can roll back by unsetting the variables.
///
/// A category is seeded all or nothing: when one entry fails to copy, the
/// entries already copied for that category are removed again, so the next
/// start finds the directory untouched and tries again.
pub fn migrate_legacy_layout(paths: &Paths) -> MigrationReport {
    let mut report = MigrationReport::default();
    let legacy = paths.legacy_dir();
    for category in Category::ALL {
        let target = paths.dir(category);
        if target == legacy {
            continue;
        }
        if let Err(e) = ensure_private_dir(target) {
            report
                .failed
                .push((target.to_path_buf(), format!("create directory: {e}")));
            continue;
        }
        let entries = legacy_entries(category);
        if entries.iter().any(|name| target.join(name).exists()) {
            continue;
        }
        let mut done: Vec<(PathBuf, PathBuf)> = Vec::new();
        let mut failure: Option<(PathBuf, String)> = None;
        for name in entries {
            let src = legacy.join(name);
            if !src.exists() {
                continue;
            }
            let dst = target.join(name);
            match copy_entry(&src, &dst) {
                Ok(()) => done.push((src, dst)),
                Err(e) => {
                    failure = Some((src, e.to_string()));
                    break;
                }
            }
        }
        match failure {
            Some((src, err)) => {
                for (_, dst) in &done {
                    let _ = remove_path(dst);
                }
                report.failed.push((
                    src,
                    format!(
                        "{err} ({} category left unseeded, retried on the next start)",
                        category.name()
                    ),
                ));
            }
            None => report.copied.extend(done),
        }
    }
    report
}

/// Create `dir` (and its parents) when absent, mode 0700 on the leaf. An
/// existing directory is left exactly as it is.
pub fn ensure_private_dir(dir: &Path) -> io::Result<()> {
    if dir.is_dir() {
        return Ok(());
    }
    fs::create_dir_all(dir)?;
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        fs::set_permissions(dir, fs::Permissions::from_mode(0o700))?;
    }
    Ok(())
}

/// Copy one legacy entry so that `dst` either appears complete or not at
/// all. A file goes through an atomic write. A directory tree is copied
/// into a temporary sibling first and renamed into place at the end; a
/// failure removes the partial copy.
fn copy_entry(src: &Path, dst: &Path) -> io::Result<()> {
    if !src.is_dir() {
        return copy_file(src, dst);
    }
    let mut tmp_name = std::ffi::OsString::from(".");
    tmp_name.push(dst.file_name().unwrap_or_default());
    tmp_name.push(format!(".purple_tmp.{}", std::process::id()));
    let tmp = dst.with_file_name(tmp_name);
    let _ = fs::remove_dir_all(&tmp);
    let copied = copy_tree(src, &tmp).and_then(|()| fs::rename(&tmp, dst));
    if copied.is_err() {
        let _ = fs::remove_dir_all(&tmp);
    }
    copied
}

/// Copy a directory tree. New directories get mode 0700, files keep theirs.
fn copy_tree(src: &Path, dst: &Path) -> io::Result<()> {
    ensure_private_dir(dst)?;
    for entry in fs::read_dir(src)? {
        let entry = entry?;
        let from = entry.path();
        let to = dst.join(entry.file_name());
        if from.is_dir() {
            copy_tree(&from, &to)?;
        } else {
            copy_file(&from, &to)?;
        }
    }
    Ok(())
}

/// Copy one file through an atomic write, keeping its mode. Everything that
/// can fail is read before the write, so a returned error means `dst` was
/// never created. Carrying the mode over is best effort: the atomic write
/// leaves 0600 behind, which is never wider than the source.
fn copy_file(src: &Path, dst: &Path) -> io::Result<()> {
    #[cfg(unix)]
    let mode = {
        use std::os::unix::fs::PermissionsExt;
        fs::metadata(src)?.permissions().mode() & 0o777
    };
    let content = fs::read(src)?;
    crate::fs_util::atomic_write(dst, &content)?;
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        if let Err(e) = fs::set_permissions(dst, fs::Permissions::from_mode(mode)) {
            log::debug!(
                "[purple] could not carry mode {mode:o} onto {}: {e}",
                dst.display()
            );
        }
    }
    Ok(())
}

/// Remove a copied entry again, file or directory tree.
fn remove_path(path: &Path) -> io::Result<()> {
    if path.is_dir() {
        fs::remove_dir_all(path)
    } else {
        fs::remove_file(path)
    }
}

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

    fn split_paths(home: &Path, base: &Path) -> Paths {
        let cfg = base.join("cfg").to_string_lossy().to_string();
        let data = base.join("data").to_string_lossy().to_string();
        let state = base.join("state").to_string_lossy().to_string();
        let cache = base.join("cache").to_string_lossy().to_string();
        Paths::resolve(home, move |k| match k {
            "XDG_CONFIG_HOME" => Some(cfg.clone()),
            "XDG_DATA_HOME" => Some(data.clone()),
            "XDG_STATE_HOME" => Some(state.clone()),
            "XDG_CACHE_HOME" => Some(cache.clone()),
            _ => None,
        })
    }

    fn seed_legacy(home: &Path) -> PathBuf {
        let legacy = home.join(".purple");
        fs::create_dir_all(legacy.join("certs")).unwrap();
        fs::create_dir_all(legacy.join("themes")).unwrap();
        fs::write(legacy.join("preferences"), "theme=Purple\n").unwrap();
        fs::write(legacy.join("providers"), "[aws]\n").unwrap();
        fs::write(legacy.join("snippets"), "").unwrap();
        fs::write(legacy.join("themes/mine.toml"), "name = \"mine\"\n").unwrap();
        fs::write(legacy.join("certs/web-cert.pub"), "cert").unwrap();
        fs::write(legacy.join("config.original"), "Host a\n").unwrap();
        fs::write(legacy.join("history.tsv"), "a\t1\t1\t1\n").unwrap();
        fs::write(legacy.join("recents.json"), "[]").unwrap();
        fs::write(legacy.join("purple.log"), "log\n").unwrap();
        fs::write(legacy.join("container_cache.jsonl"), "{}\n").unwrap();
        fs::write(legacy.join("last_version_check"), "1\n2\n\n").unwrap();
        fs::write(legacy.join(".askpass_a"), "").unwrap();
        fs::write(legacy.join(".DS_Store"), "junk").unwrap();
        legacy
    }

    #[test]
    fn legacy_layout_is_a_no_op() {
        let dir = tempfile::tempdir().unwrap();
        seed_legacy(dir.path());
        let report = migrate_legacy_layout(&Paths::new(dir.path()));
        assert!(report.is_empty(), "{report:?}");
    }

    #[test]
    fn copies_each_category_into_its_own_directory() {
        let dir = tempfile::tempdir().unwrap();
        let home = dir.path().join("home");
        let legacy = seed_legacy(&home);
        let paths = split_paths(&home, dir.path());

        let report = migrate_legacy_layout(&paths);

        assert!(report.failed.is_empty(), "{report:?}");
        assert_eq!(
            fs::read_to_string(paths.preferences()).unwrap(),
            "theme=Purple\n"
        );
        assert_eq!(
            fs::read_to_string(paths.providers_config()).unwrap(),
            "[aws]\n"
        );
        assert!(paths.snippets_file().exists());
        assert_eq!(
            fs::read_to_string(paths.themes_dir().join("mine.toml")).unwrap(),
            "name = \"mine\"\n"
        );
        assert_eq!(fs::read_to_string(paths.cert_for("web")).unwrap(), "cert");
        assert_eq!(
            fs::read_to_string(paths.config_original()).unwrap(),
            "Host a\n"
        );
        assert!(paths.history().exists());
        assert!(paths.recents().exists());
        assert!(paths.log_file().exists());
        assert!(paths.container_cache().exists());
        assert!(paths.last_version_check().exists());
        // Ephemeral markers and unknown files stay behind.
        assert!(!paths.askpass_marker("a").exists());
        assert!(!paths.state_dir().join(".DS_Store").exists());
        // The legacy tree is untouched.
        assert!(legacy.join("preferences").exists());
        assert!(legacy.join("certs/web-cert.pub").exists());
        assert_eq!(report.copied.len(), 11, "{report:?}");
    }

    #[cfg(unix)]
    #[test]
    fn created_directories_are_private_and_file_modes_survive() {
        use std::os::unix::fs::PermissionsExt;
        let dir = tempfile::tempdir().unwrap();
        let home = dir.path().join("home");
        let legacy = seed_legacy(&home);
        fs::set_permissions(legacy.join("providers"), fs::Permissions::from_mode(0o600)).unwrap();
        fs::set_permissions(
            legacy.join("preferences"),
            fs::Permissions::from_mode(0o644),
        )
        .unwrap();
        let paths = split_paths(&home, dir.path());

        migrate_legacy_layout(&paths);

        let mode = |p: &Path| fs::metadata(p).unwrap().permissions().mode() & 0o777;
        for category in Category::ALL {
            assert_eq!(mode(paths.dir(category)), 0o700, "{category:?}");
        }
        assert_eq!(mode(&paths.certs_dir()), 0o700);
        assert_eq!(mode(&paths.themes_dir()), 0o700);
        assert_eq!(mode(&paths.providers_config()), 0o600);
        assert_eq!(mode(&paths.preferences()), 0o644);
    }

    #[test]
    fn a_target_that_already_holds_category_files_is_left_alone() {
        let dir = tempfile::tempdir().unwrap();
        let home = dir.path().join("home");
        seed_legacy(&home);
        let paths = split_paths(&home, dir.path());
        fs::create_dir_all(paths.config_dir()).unwrap();
        fs::write(paths.preferences(), "theme=Nord\n").unwrap();

        let report = migrate_legacy_layout(&paths);

        // Config is skipped as a whole: the dotfile-managed preferences stay
        // and the sibling snippets file is not seeded next to it.
        assert_eq!(
            fs::read_to_string(paths.preferences()).unwrap(),
            "theme=Nord\n"
        );
        assert!(!paths.snippets_file().exists());
        // Other categories still migrate.
        assert!(paths.history().exists());
        assert!(paths.cert_for("web").exists());
        assert!(
            !report
                .copied
                .iter()
                .any(|(_, dst)| dst.starts_with(paths.config_dir()))
        );
    }

    #[test]
    fn a_second_run_copies_nothing() {
        let dir = tempfile::tempdir().unwrap();
        let home = dir.path().join("home");
        seed_legacy(&home);
        let paths = split_paths(&home, dir.path());

        let first = migrate_legacy_layout(&paths);
        assert!(!first.copied.is_empty());
        let second = migrate_legacy_layout(&paths);
        assert!(second.is_empty(), "{second:?}");
    }

    #[test]
    fn overlapping_categories_each_seed_their_own_files() {
        // Config and state resolve to the same directory. Seeding config must
        // not make the state seed think its files are already there.
        let dir = tempfile::tempdir().unwrap();
        let home = dir.path().join("home");
        seed_legacy(&home);
        let shared = dir.path().join("shared").to_string_lossy().to_string();
        let paths = Paths::resolve(&home, move |k| match k {
            "PURPLE_CONFIG_DIR" | "PURPLE_STATE_DIR" => Some(shared.clone()),
            _ => None,
        });

        let report = migrate_legacy_layout(&paths);

        assert!(report.failed.is_empty(), "{report:?}");
        assert!(paths.preferences().exists());
        assert!(paths.history().exists());
        assert_eq!(paths.config_dir(), paths.state_dir());
    }

    #[test]
    fn missing_legacy_tree_creates_empty_directories() {
        let dir = tempfile::tempdir().unwrap();
        let home = dir.path().join("home");
        let paths = split_paths(&home, dir.path());

        let report = migrate_legacy_layout(&paths);

        assert!(report.is_empty(), "{report:?}");
        for category in Category::ALL {
            assert!(paths.dir(category).is_dir(), "{category:?}");
        }
    }

    #[cfg(unix)]
    #[test]
    fn ensure_private_dir_leaves_an_existing_directory_untouched() {
        use std::os::unix::fs::PermissionsExt;
        let dir = tempfile::tempdir().unwrap();
        let existing = dir.path().join("cfg");
        fs::create_dir_all(&existing).unwrap();
        fs::set_permissions(&existing, fs::Permissions::from_mode(0o755)).unwrap();
        ensure_private_dir(&existing).unwrap();
        assert_eq!(
            fs::metadata(&existing).unwrap().permissions().mode() & 0o777,
            0o755
        );
    }

    #[test]
    fn a_failed_copy_is_reported_and_does_not_stop_the_rest() {
        let dir = tempfile::tempdir().unwrap();
        let home = dir.path().join("home");
        seed_legacy(&home);
        let paths = split_paths(&home, dir.path());
        // A file where the cache directory should go makes that category fail.
        fs::create_dir_all(dir.path().join("cache")).unwrap();
        fs::write(dir.path().join("cache/purple"), "not a directory").unwrap();

        let report = migrate_legacy_layout(&paths);

        assert_eq!(report.failed.len(), 1, "{report:?}");
        assert!(paths.preferences().exists());
        assert!(paths.history().exists());
    }

    /// Tests that inject a copy failure through mode 0o000 skip under
    /// root, because root reads files regardless of permission bits.
    #[cfg(unix)]
    fn is_root() -> bool {
        // SAFETY: geteuid takes no arguments and cannot fail.
        unsafe { libc::geteuid() == 0 }
    }

    #[cfg(unix)]
    #[test]
    fn a_failure_mid_category_rolls_back_and_the_next_run_retries() {
        use std::os::unix::fs::PermissionsExt;
        if is_root() {
            return;
        }
        let dir = tempfile::tempdir().unwrap();
        let home = dir.path().join("home");
        let legacy = seed_legacy(&home);
        let paths = split_paths(&home, dir.path());
        // history.tsv copies first, then recents.json cannot be read.
        let recents = legacy.join("recents.json");
        fs::set_permissions(&recents, fs::Permissions::from_mode(0o000)).unwrap();

        let first = migrate_legacy_layout(&paths);

        let state_failed = first
            .failed
            .iter()
            .any(|(src, err)| src == &recents && err.contains("state category left unseeded"));
        assert!(state_failed, "{first:?}");
        assert!(
            !paths.history().exists(),
            "the entry copied before the failure is rolled back"
        );
        assert!(
            !first
                .copied
                .iter()
                .any(|(_, dst)| dst.starts_with(paths.state_dir()))
        );
        // The other categories still landed.
        assert!(paths.preferences().exists());
        assert!(paths.cert_for("web").exists());

        fs::set_permissions(&recents, fs::Permissions::from_mode(0o600)).unwrap();
        let second = migrate_legacy_layout(&paths);

        assert!(second.failed.is_empty(), "{second:?}");
        assert!(paths.history().exists());
        assert!(paths.recents().exists());
        assert!(paths.log_file().exists());
        // Only the state category was seeded on the retry.
        assert!(
            second
                .copied
                .iter()
                .all(|(_, dst)| dst.starts_with(paths.state_dir())),
            "{second:?}"
        );
    }

    #[cfg(unix)]
    #[test]
    fn a_directory_entry_that_fails_leaves_no_partial_copy() {
        use std::os::unix::fs::PermissionsExt;
        if is_root() {
            return;
        }
        let dir = tempfile::tempdir().unwrap();
        let home = dir.path().join("home");
        let legacy = seed_legacy(&home);
        let paths = split_paths(&home, dir.path());
        // One unreadable file inside certs/ fails the whole certs entry.
        fs::write(legacy.join("certs/locked-cert.pub"), "x").unwrap();
        fs::set_permissions(
            legacy.join("certs/locked-cert.pub"),
            fs::Permissions::from_mode(0o000),
        )
        .unwrap();

        let report = migrate_legacy_layout(&paths);

        assert!(
            report
                .failed
                .iter()
                .any(|(src, _)| src == &legacy.join("certs")),
            "{report:?}"
        );
        assert!(
            !paths.certs_dir().exists(),
            "no half-copied certs directory"
        );
        assert!(
            !paths.config_original().exists(),
            "data category stays unseeded"
        );
        let leftovers: Vec<_> = fs::read_dir(paths.data_dir())
            .unwrap()
            .filter_map(Result::ok)
            .map(|e| e.file_name())
            .collect();
        assert!(
            leftovers.is_empty(),
            "temp directory cleaned up: {leftovers:?}"
        );
    }
}