yconn 1.12.0

SSH connection manager for teams and DevOps environments
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
// src/commands/add.rs
// Handler for `yconn add` — interactive wizard to add a connection to a
// chosen layer.

use std::io::{self, BufRead, Write};
use std::path::{Path, PathBuf};

use anyhow::{bail, Context, Result};

use crate::cli::LayerArg;
use crate::config::Layer;

// ─── Public entry point ───────────────────────────────────────────────────────

pub fn run(layer: Option<LayerArg>) -> Result<()> {
    let target_layer = layer_arg_to_layer(layer);
    let target_path = layer_path(target_layer)?;

    let stdin = io::stdin();
    let stdout = io::stdout();
    run_impl(
        target_layer,
        &target_path,
        &mut stdin.lock(),
        &mut stdout.lock(),
    )
}

// ─── Layer resolution ─────────────────────────────────────────────────────────

/// Convert the CLI `--layer` argument to the internal [`Layer`] type.
///
/// When `--layer` is omitted, the default is [`Layer::User`].
fn layer_arg_to_layer(layer: Option<LayerArg>) -> Layer {
    match layer {
        Some(LayerArg::System) => Layer::System,
        Some(LayerArg::Project) => Layer::Project,
        Some(LayerArg::User) | None => Layer::User,
    }
}

fn layer_path(layer: Layer) -> Result<PathBuf> {
    match layer {
        Layer::System => Ok(PathBuf::from("/etc/yconn")),
        Layer::User => {
            let base = dirs::config_dir().context("cannot determine user config directory")?;
            Ok(base.join("yconn"))
        }
        Layer::Project => {
            let cwd = std::env::current_dir().context("cannot determine current directory")?;
            Ok(cwd.join(".yconn"))
        }
    }
}

// ─── Testable impl ────────────────────────────────────────────────────────────

pub(crate) fn run_impl(
    layer: Layer,
    layer_dir: &Path,
    input: &mut dyn BufRead,
    output: &mut dyn Write,
) -> Result<()> {
    // Always write to connections.yaml — groups are inline fields, not per-file.
    let target = layer_dir.join("connections.yaml");

    writeln!(
        output,
        "Adding connection to {} layer ({})",
        layer.label(),
        target.display()
    )?;
    writeln!(output, "Leave a field blank to abort.")?;
    writeln!(output)?;

    // Collect required fields.
    let name = prompt(output, input, "Connection name")?;
    if name.is_empty() {
        bail!("aborted");
    }
    let host = prompt(output, input, "Host")?;
    if host.is_empty() {
        bail!("aborted");
    }
    let user = prompt(output, input, "User")?;
    if user.is_empty() {
        bail!("aborted");
    }
    let port_raw = prompt(output, input, "Port [22]")?;
    let port: u16 = if port_raw.is_empty() {
        22
    } else {
        port_raw
            .parse()
            .with_context(|| format!("invalid port '{port_raw}'"))?
    };
    let auth = prompt_choice(output, input, "Auth", &["key", "password", "identity"])?;
    if auth.is_empty() {
        bail!("aborted");
    }
    let key = if auth == "key" || auth == "identity" {
        let k = prompt(output, input, "Key path (e.g. ~/.ssh/id_rsa)")?;
        if k.is_empty() {
            bail!("aborted");
        }
        Some(k)
    } else {
        None
    };
    let description = prompt(output, input, "Description")?;
    if description.is_empty() {
        bail!("aborted");
    }
    let link = prompt(output, input, "Link (optional)")?;

    // Build the YAML entry.
    let entry = build_entry(
        &host,
        &user,
        port,
        &auth,
        key.as_deref(),
        &description,
        if link.is_empty() {
            None
        } else {
            Some(link.as_str())
        },
    );

    write_entry(&target, &name, &entry)?;

    writeln!(output)?;
    writeln!(output, "Added '{name}' to {}", target.display())?;

    Ok(())
}

// ─── Prompt helpers ───────────────────────────────────────────────────────────

fn prompt(output: &mut dyn Write, input: &mut dyn BufRead, label: &str) -> Result<String> {
    write!(output, "  {label}: ")?;
    output.flush()?;
    let mut line = String::new();
    input.read_line(&mut line)?;
    Ok(line.trim().to_string())
}

fn prompt_choice(
    output: &mut dyn Write,
    input: &mut dyn BufRead,
    label: &str,
    choices: &[&str],
) -> Result<String> {
    let options = choices.join("/");
    loop {
        let answer = prompt(output, input, &format!("{label} [{options}]"))?;
        if answer.is_empty() {
            return Ok(answer);
        }
        if choices.contains(&answer.as_str()) {
            return Ok(answer);
        }
        writeln!(output, "  Please enter one of: {options}")?;
    }
}

// ─── YAML construction ────────────────────────────────────────────────────────

pub(crate) fn build_entry(
    host: &str,
    user: &str,
    port: u16,
    auth: &str,
    key: Option<&str>,
    description: &str,
    link: Option<&str>,
) -> String {
    let mut s = String::new();
    s.push_str(&format!("    host: {}\n", host));
    s.push_str(&format!("    user: {}\n", user));
    if port != 22 {
        s.push_str(&format!("    port: {}\n", port));
    }
    // Emit nested auth block.
    s.push_str("    auth:\n");
    s.push_str(&format!("      type: {}\n", auth));
    if let Some(k) = key {
        s.push_str(&format!("      key: {}\n", k));
    }
    s.push_str(&format!(
        "    description: \"{}\"\n",
        description.replace('"', "\\\"")
    ));
    if let Some(l) = link {
        s.push_str(&format!("    link: {}\n", l));
    }
    s
}

/// Append (or create) the entry in the target YAML file under `connections:`.
pub(crate) fn write_entry(target: &Path, name: &str, entry: &str) -> Result<()> {
    // Ensure the directory exists.
    if let Some(parent) = target.parent() {
        std::fs::create_dir_all(parent)
            .with_context(|| format!("failed to create {}", parent.display()))?;
    }

    if target.exists() {
        // Read existing content and insert the new entry under `connections:`.
        let existing = std::fs::read_to_string(target)
            .with_context(|| format!("failed to read {}", target.display()))?;

        // Check for name collision.
        if entry_exists(&existing, name) {
            bail!("connection '{name}' already exists in {}", target.display());
        }

        let updated = insert_connection(&existing, name, entry);
        std::fs::write(target, updated)
            .with_context(|| format!("failed to write {}", target.display()))?;
    } else {
        // Create a minimal file with just this connection.
        let content = format!("version: 1\n\nconnections:\n  {name}:\n{entry}");
        std::fs::write(target, content)
            .with_context(|| format!("failed to write {}", target.display()))?;
    }

    set_private_permissions(target)?;

    Ok(())
}

/// Set 0o600 permissions on `path` so it is not world-readable.
///
/// This is a no-op on non-Unix platforms where the concept does not apply.
#[cfg(unix)]
pub(crate) fn set_private_permissions(path: &Path) -> Result<()> {
    use std::os::unix::fs::PermissionsExt;
    let perms = std::fs::Permissions::from_mode(0o600);
    std::fs::set_permissions(path, perms)
        .with_context(|| format!("failed to set permissions on {}", path.display()))
}

#[cfg(not(unix))]
pub(crate) fn set_private_permissions(_path: &Path) -> Result<()> {
    Ok(())
}

/// Return `true` if a connections key named `name` already appears in `content`.
pub(crate) fn entry_exists(content: &str, name: &str) -> bool {
    // Simple line-based scan: look for "  <name>:" at the start of a line.
    let pattern = format!("  {name}:");
    content
        .lines()
        .any(|l| l == pattern || l.starts_with(&format!("{pattern} ")))
}

/// Insert `name: <entry>` under the `connections:` key of `content`.
///
/// If a `connections:` line is found we append after the last line of
/// the connections block (i.e. at end of file). Otherwise we append a
/// new `connections:` section at the end.
pub(crate) fn insert_connection(content: &str, name: &str, entry: &str) -> String {
    let new_block = format!("  {name}:\n{entry}");

    // If there is already a `connections:` key, append to the end of the file.
    if content
        .lines()
        .any(|l| l == "connections:" || l.starts_with("connections:"))
    {
        let trimmed = content.trim_end();
        format!("{trimmed}\n{new_block}\n")
    } else {
        // Append a new connections section.
        let trimmed = content.trim_end();
        format!("{trimmed}\n\nconnections:\n{new_block}\n")
    }
}

// ─── Tests ────────────────────────────────────────────────────────────────────

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

    fn write_yaml(dir: &Path, name: &str, content: &str) {
        fs::write(dir.join(name), content).unwrap();
    }

    /// Simulate user input through run_impl.
    fn run_with_input(layer: Layer, layer_dir: &Path, answers: &[&str]) -> Result<String> {
        let input_str = answers.join("\n") + "\n";
        let mut input = input_str.as_bytes();
        let mut output = Vec::new();
        run_impl(layer, layer_dir, &mut input, &mut output)?;
        Ok(String::from_utf8(output).unwrap())
    }

    // ── layer resolution ──────────────────────────────────────────────────────

    #[test]
    fn test_layer_arg_none_defaults_to_user() {
        assert!(matches!(layer_arg_to_layer(None), Layer::User));
    }

    #[test]
    fn test_layer_arg_user() {
        assert!(matches!(
            layer_arg_to_layer(Some(LayerArg::User)),
            Layer::User
        ));
    }

    #[test]
    fn test_layer_arg_project() {
        assert!(matches!(
            layer_arg_to_layer(Some(LayerArg::Project)),
            Layer::Project
        ));
    }

    #[test]
    fn test_layer_arg_system() {
        assert!(matches!(
            layer_arg_to_layer(Some(LayerArg::System)),
            Layer::System
        ));
    }

    // ── add creates new file ──────────────────────────────────────────────────

    #[test]
    fn test_add_creates_new_file_with_connection() {
        let dir = TempDir::new().unwrap();
        // key auth: name, host, user, port, auth, key, description, link
        let answers = [
            "myconn",
            "10.0.0.1",
            "deploy",
            "",
            "key",
            "~/.ssh/id_rsa",
            "My server",
            "",
        ];
        run_with_input(Layer::User, dir.path(), &answers).unwrap();

        let target = dir.path().join("connections.yaml");
        assert!(target.exists());
        let content = fs::read_to_string(&target).unwrap();
        assert!(content.contains("myconn:"));
        assert!(content.contains("host: 10.0.0.1"));
        assert!(content.contains("user: deploy"));
        assert!(content.contains("type: key"));
        assert!(content.contains("key: ~/.ssh/id_rsa"));
        assert!(content.contains("description:"));
    }

    #[test]
    fn test_add_password_auth_no_key_field() {
        let dir = TempDir::new().unwrap();
        // password auth: name, host, user, port, auth, description, link
        let answers = [
            "dbconn",
            "db.internal",
            "dbadmin",
            "",
            "password",
            "Database",
            "",
        ];
        run_with_input(Layer::User, dir.path(), &answers).unwrap();

        let content = fs::read_to_string(dir.path().join("connections.yaml")).unwrap();
        assert!(content.contains("type: password"));
        assert!(!content.contains("key:"));
    }

    #[test]
    fn test_add_identity_auth_creates_correct_yaml() {
        let dir = TempDir::new().unwrap();
        // identity auth: name, host, user, port, auth, key, description, link
        let answers = [
            "github",
            "github.com",
            "git",
            "",
            "identity",
            "~/.ssh/github_key",
            "GitHub identity",
            "",
        ];
        run_with_input(Layer::User, dir.path(), &answers).unwrap();

        let target = dir.path().join("connections.yaml");
        assert!(target.exists());
        let content = fs::read_to_string(&target).unwrap();
        assert!(content.contains("github:"));
        assert!(content.contains("host: github.com"));
        assert!(content.contains("user: git"));
        assert!(content.contains("type: identity"));
        assert!(content.contains("key: ~/.ssh/github_key"));
        assert!(content.contains("description:"));
    }

    #[test]
    fn test_add_custom_port_included() {
        let dir = TempDir::new().unwrap();
        let answers = [
            "sshbox",
            "host.example.com",
            "admin",
            "2222",
            "key",
            "~/.ssh/k",
            "Box",
            "",
        ];
        run_with_input(Layer::User, dir.path(), &answers).unwrap();

        let content = fs::read_to_string(dir.path().join("connections.yaml")).unwrap();
        assert!(content.contains("port: 2222"));
    }

    #[test]
    fn test_add_default_port_22_omitted() {
        let dir = TempDir::new().unwrap();
        let answers = [
            "srv", "1.2.3.4", "root", "", "key", "~/.ssh/k", "Server", "",
        ];
        run_with_input(Layer::User, dir.path(), &answers).unwrap();

        let content = fs::read_to_string(dir.path().join("connections.yaml")).unwrap();
        assert!(!content.contains("port: 22"));
    }

    #[test]
    fn test_add_appends_to_existing_file() {
        let dir = TempDir::new().unwrap();
        write_yaml(
            dir.path(),
            "connections.yaml",
            "version: 1\n\nconnections:\n  existing:\n    host: h\n    user: u\n    auth:\n      type: key\n      key: ~/.ssh/id_rsa\n    description: \"d\"\n",
        );

        let answers = [
            "newconn",
            "2.2.2.2",
            "user2",
            "",
            "password",
            "New server",
            "",
        ];
        run_with_input(Layer::User, dir.path(), &answers).unwrap();

        let content = fs::read_to_string(dir.path().join("connections.yaml")).unwrap();
        assert!(content.contains("existing:"));
        assert!(content.contains("newconn:"));
    }

    #[test]
    fn test_add_duplicate_name_returns_error() {
        let dir = TempDir::new().unwrap();
        write_yaml(
            dir.path(),
            "connections.yaml",
            "version: 1\n\nconnections:\n  myconn:\n    host: h\n    user: u\n    auth:\n      type: key\n      key: ~/.ssh/id_rsa\n    description: \"d\"\n",
        );

        let answers = ["myconn", "other.host", "user", "", "password", "Dup", ""];
        let err = run_with_input(Layer::User, dir.path(), &answers).unwrap_err();
        assert!(err.to_string().contains("already exists"));
    }

    #[test]
    fn test_add_empty_name_aborts() {
        let dir = TempDir::new().unwrap();
        // First answer is name = "" → abort
        let answers = [""];
        let err = run_with_input(Layer::User, dir.path(), &answers).unwrap_err();
        assert!(err.to_string().contains("aborted"));
    }

    // ── layer targeting ───────────────────────────────────────────────────────

    #[test]
    fn test_add_to_project_layer_creates_in_yconn_dir() {
        let dir = TempDir::new().unwrap();
        let yconn = dir.path().join(".yconn");
        // The layer_dir IS the yconn dir for project layer.
        let answers = [
            "proj-conn",
            "10.1.1.1",
            "ops",
            "",
            "password",
            "Proj server",
            "",
        ];
        run_with_input(Layer::Project, &yconn, &answers).unwrap();

        let target = yconn.join("connections.yaml");
        assert!(target.exists());
        let content = fs::read_to_string(&target).unwrap();
        assert!(content.contains("proj-conn:"));
    }

    // ── insert_connection helper ──────────────────────────────────────────────

    #[test]
    fn test_insert_connection_appends_under_existing_connections_key() {
        let content = "version: 1\n\nconnections:\n  a:\n    host: h\n";
        let entry = "    host: newhost\n    user: u\n    auth:\n      type: key\n      key: ~/.ssh/id_rsa\n    description: \"d\"\n";
        let result = insert_connection(content, "b", entry);
        assert!(result.contains("a:"));
        assert!(result.contains("b:"));
        assert!(result.contains("newhost"));
    }

    #[test]
    fn test_insert_connection_adds_connections_section_when_missing() {
        let content = "version: 1\n";
        let entry = "    host: h\n    user: u\n    auth:\n      type: key\n      key: ~/.ssh/id_rsa\n    description: \"d\"\n";
        let result = insert_connection(content, "srv", entry);
        assert!(result.contains("connections:"));
        assert!(result.contains("srv:"));
    }

    // ── entry_exists helper ───────────────────────────────────────────────────

    #[test]
    fn test_entry_exists_detects_existing_name() {
        let content = "connections:\n  myconn:\n    host: h\n";
        assert!(entry_exists(content, "myconn"));
    }

    #[test]
    fn test_entry_exists_returns_false_when_absent() {
        let content = "connections:\n  other:\n    host: h\n";
        assert!(!entry_exists(content, "myconn"));
    }

    // ── file permissions ──────────────────────────────────────────────────────

    #[test]
    #[cfg(unix)]
    fn test_add_new_file_has_0o600_permissions() {
        use std::os::unix::fs::PermissionsExt;
        let dir = TempDir::new().unwrap();
        let answers = [
            "srv", "1.2.3.4", "root", "", "key", "~/.ssh/k", "Server", "",
        ];
        run_with_input(Layer::User, dir.path(), &answers).unwrap();
        let target = dir.path().join("connections.yaml");
        let mode = fs::metadata(&target).unwrap().permissions().mode() & 0o777;
        assert_eq!(mode, 0o600, "new config file should have 0o600 permissions");
    }

    #[test]
    #[cfg(unix)]
    fn test_add_existing_file_has_0o600_permissions_after_append() {
        use std::os::unix::fs::PermissionsExt;
        let dir = TempDir::new().unwrap();
        let target = dir.path().join("connections.yaml");
        // Write initial content with looser permissions to simulate existing file.
        fs::write(
            &target,
            "version: 1\n\nconnections:\n  existing:\n    host: h\n    user: u\n    auth:\n      type: key\n      key: ~/.ssh/id_rsa\n    description: \"d\"\n",
        )
        .unwrap();
        fs::set_permissions(&target, fs::Permissions::from_mode(0o644)).unwrap();

        let answers = [
            "newconn",
            "2.2.2.2",
            "user2",
            "",
            "password",
            "New server",
            "",
        ];
        run_with_input(Layer::User, dir.path(), &answers).unwrap();
        let mode = fs::metadata(&target).unwrap().permissions().mode() & 0o777;
        assert_eq!(
            mode, 0o600,
            "updated config file should have 0o600 permissions"
        );
    }
}