yconn 1.5.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
// Handler for `yconn connect <name>` — resolve a connection and invoke SSH,
// optionally bootstrapping into Docker first.

use std::path::PathBuf;

use anyhow::Result;

use crate::config::LoadedConfig;
use crate::display::Renderer;
use crate::{connect, docker, security};

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

pub fn run(cfg: &LoadedConfig, renderer: &Renderer, name: &str, verbose: bool) -> Result<()> {
    let conn = cfg.find_with_wildcard(name)?;

    // Security: validate the key file before trying to connect.
    // Expand a leading `~` so that the existence and permission checks operate
    // on the real path — Path::new("~/.ssh/id_rsa") does not exist literally.
    if conn.auth == "key" {
        if let Some(ref key) = conn.key {
            let expanded = expand_tilde(key);
            for w in security::check_key_file(&expanded) {
                renderer.warn(&w.message);
            }
        }
    }

    // Docker bootstrap path: re-invoke inside container.
    if let Some(ref docker_cfg) = cfg.docker {
        if !docker::in_container() {
            let original_argv: Vec<String> = std::env::args().collect();
            docker::exec(docker_cfg, &original_argv, verbose, renderer)?;
            unreachable!("docker::exec replaced the process");
        }
    }

    // Direct SSH path: replace the current process with ssh.
    if verbose {
        let ssh_args = connect::build_args(&conn);
        renderer.verbose_ssh_cmd(&ssh_args);
    }
    connect::exec(&conn)
}

// ─── Private helpers ──────────────────────────────────────────────────────────

/// Expand a leading `~` to the current user's home directory.
///
/// Only a literal leading `~/` (or the bare string `"~"`) is expanded.
/// `~username` forms are not supported. If `dirs::home_dir()` returns `None`,
/// the path is returned unchanged.
fn expand_tilde(path: &str) -> PathBuf {
    if path == "~" {
        return dirs::home_dir().unwrap_or_else(|| PathBuf::from("~"));
    }
    if let Some(rest) = path.strip_prefix("~/") {
        if let Some(home) = dirs::home_dir() {
            return home.join(rest);
        }
    }
    PathBuf::from(path)
}

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

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

    use crate::config;
    use crate::display::Renderer;

    /// What `connect` would execute, without actually exec-ing.
    ///
    /// `in_container` is injected so tests can control container detection
    /// without relying on `/.dockerenv` or the `CONN_IN_DOCKER` env var.
    #[derive(Debug)]
    enum ConnectPlan {
        Docker(Vec<String>),
        Ssh(Vec<String>),
    }

    fn plan(
        cfg: &LoadedConfig,
        name: &str,
        original_argv: &[String],
        in_container: bool,
    ) -> Result<ConnectPlan> {
        let conn = cfg.find_with_wildcard(name)?;

        if let Some(ref docker_cfg) = cfg.docker {
            if !in_container {
                let args = docker::build_args(docker_cfg, original_argv)?;
                return Ok(ConnectPlan::Docker(args));
            }
        }

        Ok(ConnectPlan::Ssh(connect::build_args(&conn)))
    }

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

    fn no_color() -> Renderer {
        Renderer::new(false)
    }

    fn load(
        cwd: &std::path::Path,
        user: Option<&std::path::Path>,
        sys: &std::path::Path,
    ) -> config::LoadedConfig {
        config::load_impl(cwd, Some("connections"), false, user, sys).unwrap()
    }

    fn argv(args: &[&str]) -> Vec<String> {
        args.iter().map(|s| s.to_string()).collect()
    }

    // ── Unknown name ──────────────────────────────────────────────────────────

    #[test]
    fn test_connect_unknown_name_returns_error() {
        let cwd = TempDir::new().unwrap();
        let empty = TempDir::new().unwrap();
        let cfg = load(cwd.path(), None, empty.path());

        let err = plan(
            &cfg,
            "does-not-exist",
            &argv(&["yconn", "connect", "does-not-exist"]),
            false,
        )
        .unwrap_err();
        assert!(err.to_string().contains("does-not-exist"));
    }

    #[test]
    fn test_connect_error_message_contains_name() {
        let cwd = TempDir::new().unwrap();
        let empty = TempDir::new().unwrap();
        let cfg = load(cwd.path(), None, empty.path());

        let err = plan(
            &cfg,
            "my-server",
            &argv(&["yconn", "connect", "my-server"]),
            false,
        )
        .unwrap_err();
        assert!(err.to_string().contains("my-server"));
    }

    // ── Non-Docker SSH path ───────────────────────────────────────────────────

    #[test]
    fn test_connect_no_docker_produces_ssh_plan() {
        let cwd = TempDir::new().unwrap();
        let user = TempDir::new().unwrap();
        write_yaml(
            user.path(),
            "connections.yaml",
            "connections:\n  prod:\n    host: 10.0.0.1\n    user: deploy\n    auth: key\n    key: ~/.ssh/id_rsa\n    description: Prod\n",
        );
        let empty = TempDir::new().unwrap();
        let cfg = load(cwd.path(), Some(user.path()), empty.path());

        let p = plan(&cfg, "prod", &argv(&["yconn", "connect", "prod"]), false).unwrap();
        assert!(matches!(p, ConnectPlan::Ssh(_)));
        if let ConnectPlan::Ssh(args) = p {
            assert_eq!(args[0], "ssh");
            assert!(args.contains(&"-i".to_string()));
            assert!(args.contains(&"~/.ssh/id_rsa".to_string()));
            assert!(args.last().unwrap().contains("deploy@10.0.0.1"));
        }
    }

    #[test]
    fn test_connect_key_auth_default_port_ssh_args() {
        let cwd = TempDir::new().unwrap();
        let user = TempDir::new().unwrap();
        write_yaml(
            user.path(),
            "connections.yaml",
            "connections:\n  srv:\n    host: myhost\n    user: admin\n    auth: key\n    key: ~/.ssh/id_ed25519\n    description: Server\n",
        );
        let empty = TempDir::new().unwrap();
        let cfg = load(cwd.path(), Some(user.path()), empty.path());

        let p = plan(&cfg, "srv", &argv(&["yconn", "connect", "srv"]), false).unwrap();
        if let ConnectPlan::Ssh(args) = p {
            assert_eq!(args, vec!["ssh", "-i", "~/.ssh/id_ed25519", "admin@myhost"]);
        }
    }

    #[test]
    fn test_connect_password_auth_ssh_args() {
        let cwd = TempDir::new().unwrap();
        let user = TempDir::new().unwrap();
        write_yaml(
            user.path(),
            "connections.yaml",
            "connections:\n  db:\n    host: db.internal\n    user: dbadmin\n    auth: password\n    description: DB\n",
        );
        let empty = TempDir::new().unwrap();
        let cfg = load(cwd.path(), Some(user.path()), empty.path());

        let p = plan(&cfg, "db", &argv(&["yconn", "connect", "db"]), false).unwrap();
        if let ConnectPlan::Ssh(args) = p {
            assert_eq!(args, vec!["ssh", "dbadmin@db.internal"]);
        }
    }

    // ── Docker bootstrap path ─────────────────────────────────────────────────

    #[test]
    fn test_connect_docker_not_in_container_produces_docker_plan() {
        let root = TempDir::new().unwrap();
        let yconn = root.path().join(".yconn");
        fs::create_dir_all(&yconn).unwrap();
        write_yaml(
            &yconn,
            "connections.yaml",
            "docker:\n  image: ghcr.io/org/keys:latest\nconnections:\n  prod:\n    host: 10.0.0.1\n    user: deploy\n    auth: key\n    key: ~/.ssh/id_rsa\n    description: Prod\n",
        );
        let empty = TempDir::new().unwrap();
        let cfg = load(root.path(), None, empty.path());
        assert!(cfg.docker.is_some());

        // in_container = false → should bootstrap into Docker
        let p = plan(&cfg, "prod", &argv(&["yconn", "connect", "prod"]), false).unwrap();
        assert!(matches!(p, ConnectPlan::Docker(_)));
        if let ConnectPlan::Docker(args) = p {
            assert_eq!(args[0], "docker");
            assert_eq!(args[1], "run");
            assert!(args.contains(&"ghcr.io/org/keys:latest".to_string()));
            assert!(args.contains(&"CONN_IN_DOCKER=1".to_string()));
        }
    }

    #[test]
    fn test_connect_docker_in_container_produces_ssh_plan() {
        let root = TempDir::new().unwrap();
        let yconn = root.path().join(".yconn");
        fs::create_dir_all(&yconn).unwrap();
        write_yaml(
            &yconn,
            "connections.yaml",
            "docker:\n  image: ghcr.io/org/keys:latest\nconnections:\n  prod:\n    host: 10.0.0.1\n    user: deploy\n    auth: key\n    key: ~/.ssh/id_rsa\n    description: Prod\n",
        );
        let empty = TempDir::new().unwrap();
        let cfg = load(root.path(), None, empty.path());

        // in_container = true → Docker skipped, SSH invoked directly
        let p = plan(&cfg, "prod", &argv(&["yconn", "connect", "prod"]), true).unwrap();
        assert!(matches!(p, ConnectPlan::Ssh(_)));
    }

    #[test]
    fn test_connect_docker_argv_passed_through() {
        let root = TempDir::new().unwrap();
        let yconn = root.path().join(".yconn");
        fs::create_dir_all(&yconn).unwrap();
        write_yaml(
            &yconn,
            "connections.yaml",
            "docker:\n  image: myimage:v1\nconnections:\n  srv:\n    host: h\n    user: u\n    auth: key\n    key: ~/.ssh/k\n    description: d\n",
        );
        let empty = TempDir::new().unwrap();
        let cfg = load(root.path(), None, empty.path());

        let orig = argv(&["yconn", "connect", "srv"]);
        let p = plan(&cfg, "srv", &orig, false).unwrap();
        if let ConnectPlan::Docker(args) = p {
            let img_pos = args.iter().position(|a| a == "myimage:v1").unwrap();
            assert_eq!(&args[img_pos + 1..], &["yconn", "connect", "srv"]);
        }
    }

    #[test]
    fn test_connect_no_docker_block_goes_ssh() {
        let cwd = TempDir::new().unwrap();
        let user = TempDir::new().unwrap();
        write_yaml(
            user.path(),
            "connections.yaml",
            "connections:\n  srv:\n    host: h\n    user: u\n    auth: password\n    description: d\n",
        );
        let empty = TempDir::new().unwrap();
        let cfg = load(cwd.path(), Some(user.path()), empty.path());

        assert!(cfg.docker.is_none());
        let p = plan(&cfg, "srv", &argv(&["yconn", "connect", "srv"]), false).unwrap();
        assert!(matches!(p, ConnectPlan::Ssh(_)));
    }

    // ── run() error path (no exec involved) ───────────────────────────────────

    #[test]
    fn test_run_unknown_name_returns_error() {
        let cwd = TempDir::new().unwrap();
        let empty = TempDir::new().unwrap();
        let cfg = load(cwd.path(), None, empty.path());

        let err = run(&cfg, &no_color(), "no-such-server", false).unwrap_err();
        assert!(err.to_string().contains("no-such-server"));
    }

    // ── expand_tilde ──────────────────────────────────────────────────────────

    #[test]
    fn test_expand_tilde_prefix_joins_home() {
        // "~/foo/bar" must resolve to <home>/foo/bar, not the literal string.
        let result = expand_tilde("~/foo/bar");
        let home = dirs::home_dir().expect("home dir must be set in test environment");
        assert_eq!(result, home.join("foo/bar"));
    }

    #[test]
    fn test_expand_tilde_bare_returns_home() {
        let result = expand_tilde("~");
        let home = dirs::home_dir().expect("home dir must be set in test environment");
        assert_eq!(result, home);
    }

    #[test]
    fn test_expand_tilde_absolute_path_unchanged() {
        let result = expand_tilde("/home/user/.ssh/id_rsa");
        assert_eq!(result, std::path::PathBuf::from("/home/user/.ssh/id_rsa"));
    }

    #[test]
    fn test_expand_tilde_no_tilde_unchanged() {
        let result = expand_tilde("relative/path/key");
        assert_eq!(result, std::path::PathBuf::from("relative/path/key"));
    }

    /// A tilde key path that resolves to an existing file should produce no
    /// "does not exist" warning from `security::check_key_file`.
    #[test]
    fn test_tilde_key_exists_no_warning() {
        use std::os::unix::fs::PermissionsExt;

        let dir = TempDir::new().unwrap();
        let key_path = dir.path().join("id_rsa");
        fs::write(&key_path, "KEY").unwrap();
        fs::set_permissions(&key_path, fs::Permissions::from_mode(0o600)).unwrap();

        let expanded = key_path.clone();
        let warnings = security::check_key_file(&expanded);
        // File exists and has safe permissions — no warnings.
        assert!(
            warnings.is_empty(),
            "unexpected warnings for existing key: {:?}",
            warnings
        );
    }

    /// A tilde key path whose resolved path does not exist must still emit the
    /// "key file does not exist" warning.
    #[test]
    fn test_tilde_key_missing_warns() {
        let dir = TempDir::new().unwrap();
        // Construct a path that does not exist.
        let missing = dir.path().join("no_such_key");

        let warnings = security::check_key_file(&missing);
        assert_eq!(warnings.len(), 1, "expected exactly one warning");
        assert!(
            warnings[0].message.contains("does not exist"),
            "warning message should say 'does not exist': {}",
            warnings[0].message
        );
    }

    /// Verify the full expand_tilde → check_key_file pipeline: a path written
    /// as "~/..." in config must not trigger a false "does not exist" warning
    /// when the file is actually present under the real home directory.
    #[test]
    fn test_expand_tilde_then_check_existing_key_no_warning() {
        use std::os::unix::fs::PermissionsExt;

        let dir = TempDir::new().unwrap();
        let key_path = dir.path().join("id_ed25519");
        fs::write(&key_path, "KEY DATA").unwrap();
        fs::set_permissions(&key_path, fs::Permissions::from_mode(0o600)).unwrap();

        // Simulate expand_tilde on an already-absolute path (as produced by
        // a non-tilde config entry) to verify the pipeline handles it correctly.
        let expanded = expand_tilde(key_path.to_str().unwrap());
        assert_eq!(expanded, key_path);

        let warnings = security::check_key_file(&expanded);
        assert!(
            warnings.is_empty(),
            "absolute path to existing key must not warn: {:?}",
            warnings
        );
    }
}