remotefs-ssh 0.8.3

remotefs SSH client library
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
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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
use std::io::{Read, Seek, Write};
use std::net::{SocketAddr, TcpStream, ToSocketAddrs as _};
use std::path::{Path, PathBuf};
use std::str::FromStr as _;
use std::time::{Duration, SystemTime};

use remotefs::fs::stream::{ReadAndSeek, WriteAndSeek};
use remotefs::fs::{FileType, Metadata, ReadStream, UnixPex, WriteStream};
use remotefs::{File, RemoteError, RemoteErrorType, RemoteResult};
use ssh2::{FileStat, OpenType, RenameFlags};

use super::SshSession;
use crate::ssh::backend::Sftp;
use crate::ssh::config::Config;
use crate::{SshAgentIdentity, SshOpts};

/// An implementation of [`SshSession`] using libssh2 as the backend.
pub struct LibSsh2Session {
    session: ssh2::Session,
}

/// A wrapper around [`ssh2::Sftp`] to provide a SFTP client for [`LibSsh2Session`]
pub struct LibSsh2Sftp {
    inner: ssh2::Sftp,
}

/// Authentication method
#[derive(Debug, Clone, PartialEq, Eq)]
enum Authentication {
    RsaKey(PathBuf),
    Password(String),
}

impl SshSession for LibSsh2Session {
    type Sftp = LibSsh2Sftp;

    fn connect(opts: &SshOpts) -> RemoteResult<Self> {
        // parse configuration
        let ssh_config = Config::try_from(opts)?;
        // Resolve host
        debug!("Connecting to '{}'", ssh_config.address);
        // setup tcp stream
        let socket_addresses: Vec<SocketAddr> = match ssh_config.address.to_socket_addrs() {
            Ok(s) => s.collect(),
            Err(err) => {
                return Err(RemoteError::new_ex(
                    RemoteErrorType::BadAddress,
                    err.to_string(),
                ));
            }
        };
        let mut stream = None;
        for _ in 0..ssh_config.connection_attempts {
            for socket_addr in socket_addresses.iter() {
                trace!(
                    "Trying to connect to socket address '{}' (timeout: {}s)",
                    socket_addr,
                    ssh_config.connection_timeout.as_secs()
                );
                if let Ok(tcp_stream) = tcp_connect(socket_addr, ssh_config.connection_timeout) {
                    debug!("Connection established with address {socket_addr}");
                    stream = Some(tcp_stream);
                    break;
                }
            }
            // break from attempts cycle if some
            if stream.is_some() {
                break;
            }
        }
        // If stream is None, return connection timeout
        let stream = match stream {
            Some(s) => s,
            None => {
                error!("No suitable socket address found; connection timeout");
                return Err(RemoteError::new_ex(
                    RemoteErrorType::ConnectionError,
                    "connection timeout",
                ));
            }
        };
        // Create session
        let mut session = match ssh2::Session::new() {
            Ok(s) => s,
            Err(err) => {
                error!("Could not create session: {err}");
                return Err(RemoteError::new_ex(RemoteErrorType::ConnectionError, err));
            }
        };
        // Set TCP stream
        session.set_tcp_stream(stream);
        // configure algos
        set_algo_prefs(&mut session, opts, &ssh_config)?;
        // Open connection and initialize handshake
        if let Err(err) = session.handshake() {
            error!("SSH handshake failed: {err}");
            return Err(RemoteError::new_ex(RemoteErrorType::ProtocolError, err));
        }

        // if use_ssh_agent is enabled, try to authenticate with ssh agent
        if let Some(ssh_agent_config) = &opts.ssh_agent_identity {
            match session_auth_with_agent(&mut session, &ssh_config.username, ssh_agent_config) {
                Ok(_) => {
                    info!("Authenticated with ssh agent");
                    return Ok(Self { session });
                }
                Err(err) => {
                    error!("Could not authenticate with ssh agent: {err}");
                }
            }
        }

        // Authenticate with password or key
        if !session.authenticated() {
            let mut methods = vec![];
            // first try with ssh agent
            if let Some(rsa_key) = opts.key_storage.as_ref().and_then(|x| {
                x.resolve(ssh_config.host.as_str(), ssh_config.username.as_str())
                    .or(x.resolve(
                        ssh_config.resolved_host.as_str(),
                        ssh_config.username.as_str(),
                    ))
            }) {
                methods.push(Authentication::RsaKey(rsa_key.clone()));
            }
            // then try with password
            if let Some(password) = opts.password.as_ref() {
                methods.push(Authentication::Password(password.clone()));
            }

            // try with methods
            let mut last_err = None;
            for auth_method in methods {
                match session_auth(&mut session, opts, &ssh_config, auth_method) {
                    Ok(_) => {
                        info!("Authenticated successfully");
                        return Ok(Self { session });
                    }
                    Err(err) => {
                        error!("Authentication failed: {err}",);
                        last_err = Some(err);
                    }
                }
            }

            return Err(last_err.unwrap_or_else(|| {
                RemoteError::new_ex(
                    RemoteErrorType::AuthenticationFailed,
                    "no authentication method provided",
                )
            }));
        }

        Ok(Self { session })
    }

    fn disconnect(&self) -> RemoteResult<()> {
        self.session
            .disconnect(None, "Mandi!", None)
            .map_err(|err| RemoteError::new_ex(RemoteErrorType::ConnectionError, err))
    }

    fn authenticated(&self) -> RemoteResult<bool> {
        Ok(self.session.authenticated())
    }

    fn banner(&self) -> RemoteResult<Option<String>> {
        Ok(self.session.banner().map(String::from))
    }

    fn cmd<S>(&mut self, cmd: S) -> RemoteResult<(u32, String)>
    where
        S: AsRef<str>,
    {
        let output = perform_shell_cmd(&mut self.session, format!("{}; echo $?", cmd.as_ref()))?;
        if let Some(index) = output.trim().rfind('\n') {
            trace!("Read from stdout: '{output}'");
            let actual_output = (output[0..index + 1]).to_string();
            trace!("Actual output '{actual_output}'");
            trace!("Parsing return code '{}'", output[index..].trim());
            let rc = match u32::from_str(output[index..].trim()).ok() {
                Some(val) => val,
                None => {
                    return Err(RemoteError::new_ex(
                        RemoteErrorType::ProtocolError,
                        "Failed to get command exit code",
                    ));
                }
            };
            debug!(r#"Command output: "{actual_output}"; exit code: {rc}"#);
            Ok((rc, actual_output))
        } else {
            match u32::from_str(output.trim()).ok() {
                Some(val) => Ok((val, String::new())),
                None => Err(RemoteError::new_ex(
                    RemoteErrorType::ProtocolError,
                    "Failed to get command exit code",
                )),
            }
        }
    }

    fn scp_recv(&self, path: &Path) -> RemoteResult<Box<dyn Read + Send>> {
        self.session.set_blocking(true);

        self.session
            .scp_recv(path)
            .map(|(reader, _stat)| Box::new(reader) as Box<dyn Read + Send>)
            .map_err(|err| {
                RemoteError::new_ex(
                    RemoteErrorType::ProtocolError,
                    format!("Could not receive file over SCP: {err}"),
                )
            })
    }

    fn scp_send(
        &self,
        remote_path: &Path,
        mode: i32,
        size: u64,
        times: Option<(u64, u64)>,
    ) -> RemoteResult<Box<dyn Write + Send>> {
        self.session.set_blocking(true);

        self.session
            .scp_send(remote_path, mode, size, times)
            .map(|writer| Box::new(writer) as Box<dyn Write + Send>)
            .map_err(|err| {
                RemoteError::new_ex(
                    RemoteErrorType::ProtocolError,
                    format!("Could not send file over SCP: {err}"),
                )
            })
    }

    fn sftp(&self) -> RemoteResult<Self::Sftp> {
        self.session.set_blocking(true);

        Ok(LibSsh2Sftp {
            inner: self.session.sftp().map_err(|err| {
                RemoteError::new_ex(
                    RemoteErrorType::ProtocolError,
                    format!("Could not create SFTP session: {err}"),
                )
            })?,
        })
    }
}

struct SftpFileReader(ssh2::File);

struct SftpFileWriter(ssh2::File);

impl Write for SftpFileWriter {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        self.0.write(buf)
    }

    fn flush(&mut self) -> std::io::Result<()> {
        self.0.flush()
    }
}

impl Seek for SftpFileWriter {
    fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
        self.0.seek(pos)
    }
}

impl WriteAndSeek for SftpFileWriter {}

impl Read for SftpFileReader {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        self.0.read(buf)
    }
}

impl Seek for SftpFileReader {
    fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
        self.0.seek(pos)
    }
}

impl ReadAndSeek for SftpFileReader {}

impl Sftp for LibSsh2Sftp {
    fn mkdir(&self, path: &Path, mode: i32) -> RemoteResult<()> {
        self.inner.mkdir(path, mode).map_err(|err| {
            RemoteError::new_ex(
                RemoteErrorType::FileCreateDenied,
                format!(
                    "Could not create directory '{path}': {err}",
                    path = path.display()
                ),
            )
        })
    }

    fn open_read(&self, path: &Path) -> RemoteResult<ReadStream> {
        self.inner
            .open(path)
            .map(|file| ReadStream::from(Box::new(SftpFileReader(file)) as Box<dyn ReadAndSeek>))
            .map_err(|err| {
                RemoteError::new_ex(
                    RemoteErrorType::ProtocolError,
                    format!(
                        "Could not open file at '{path}': {err}",
                        path = path.display()
                    ),
                )
            })
    }

    fn open_write(
        &self,
        path: &Path,
        flags: super::WriteMode,
        mode: i32,
    ) -> RemoteResult<WriteStream> {
        let flags = match flags {
            super::WriteMode::Append => {
                ssh2::OpenFlags::WRITE | ssh2::OpenFlags::APPEND | ssh2::OpenFlags::CREATE
            }
            super::WriteMode::Truncate => {
                ssh2::OpenFlags::WRITE | ssh2::OpenFlags::CREATE | ssh2::OpenFlags::TRUNCATE
            }
        };

        self.inner
            .open_mode(path, flags, mode, OpenType::File)
            .map(|file| WriteStream::from(Box::new(SftpFileWriter(file)) as Box<dyn WriteAndSeek>))
            .map_err(|err| {
                RemoteError::new_ex(
                    RemoteErrorType::ProtocolError,
                    format!(
                        "Could not open file at '{path}': {err}",
                        path = path.display()
                    ),
                )
            })
    }

    fn readdir<T>(&self, dirname: T) -> RemoteResult<Vec<remotefs::File>>
    where
        T: AsRef<Path>,
    {
        self.inner
            .readdir(dirname)
            .map(|files| {
                files
                    .into_iter()
                    .map(|(path, metadata)| self.make_fsentry(path.as_path(), &metadata))
                    .collect()
            })
            .map_err(|err| {
                RemoteError::new_ex(
                    RemoteErrorType::ProtocolError,
                    format!("Could not read directory: {err}",),
                )
            })
    }

    fn realpath(&self, path: &Path) -> RemoteResult<PathBuf> {
        self.inner.realpath(path).map_err(|err| {
            RemoteError::new_ex(
                RemoteErrorType::ProtocolError,
                format!(
                    "Could not resolve real path for '{path}': {err}",
                    path = path.display()
                ),
            )
        })
    }

    fn rename(&self, src: &Path, dest: &Path) -> RemoteResult<()> {
        self.inner
            .rename(src, dest, Some(RenameFlags::OVERWRITE))
            .map_err(|err| {
                RemoteError::new_ex(
                    RemoteErrorType::ProtocolError,
                    format!("Could not rename file '{src}': {err}", src = src.display()),
                )
            })
    }

    fn rmdir(&self, path: &Path) -> RemoteResult<()> {
        self.inner.rmdir(path).map_err(|err| {
            RemoteError::new_ex(
                RemoteErrorType::CouldNotRemoveFile,
                format!(
                    "Could not remove directory '{path}': {err}",
                    path = path.display()
                ),
            )
        })
    }

    fn setstat(&self, path: &Path, metadata: Metadata) -> RemoteResult<()> {
        self.inner
            .setstat(path, Self::metadata_to_filestat(metadata))
            .map_err(|err| {
                RemoteError::new_ex(
                    RemoteErrorType::ProtocolError,
                    format!(
                        "Could not set file attributes for '{path}': {err}",
                        path = path.display()
                    ),
                )
            })
    }

    fn stat(&self, filename: &Path) -> RemoteResult<File> {
        self.inner
            .stat(filename)
            .map(|metadata| self.make_fsentry(filename, &metadata))
            .map_err(|err| {
                RemoteError::new_ex(
                    RemoteErrorType::ProtocolError,
                    format!(
                        "Could not get file attributes for '{filename}': {err}",
                        filename = filename.display()
                    ),
                )
            })
    }

    fn symlink(&self, path: &Path, target: &Path) -> RemoteResult<()> {
        self.inner.symlink(path, target).map_err(|err| {
            RemoteError::new_ex(
                RemoteErrorType::FileCreateDenied,
                format!(
                    "Could not create symlink '{path}': {err}",
                    path = path.display()
                ),
            )
        })
    }

    fn unlink(&self, path: &Path) -> RemoteResult<()> {
        self.inner.unlink(path).map_err(|err| {
            RemoteError::new_ex(
                RemoteErrorType::CouldNotRemoveFile,
                format!(
                    "Could not remove file '{path}': {err}",
                    path = path.display()
                ),
            )
        })
    }
}

impl LibSsh2Sftp {
    fn metadata_to_filestat(metadata: Metadata) -> FileStat {
        let atime = metadata
            .accessed
            .and_then(|x| x.duration_since(SystemTime::UNIX_EPOCH).ok())
            .map(|x| x.as_secs());
        let mtime = metadata
            .modified
            .and_then(|x| x.duration_since(SystemTime::UNIX_EPOCH).ok())
            .map(|x| x.as_secs());
        FileStat {
            size: Some(metadata.size),
            uid: metadata.uid,
            gid: metadata.gid,
            perm: metadata.mode.map(u32::from),
            atime,
            mtime,
        }
    }

    fn make_fsentry(&self, path: &Path, metadata: &FileStat) -> File {
        let name = match path.file_name() {
            None => "/".to_string(),
            Some(name) => name.to_string_lossy().to_string(),
        };
        debug!("Found file {name}");
        // parse metadata
        let uid = metadata.uid;
        let gid = metadata.gid;
        let mode = metadata.perm.map(UnixPex::from);
        let size = metadata.size.unwrap_or(0);
        let accessed = metadata.atime.map(|x| {
            SystemTime::UNIX_EPOCH
                .checked_add(Duration::from_secs(x))
                .unwrap_or(SystemTime::UNIX_EPOCH)
        });
        let modified = metadata.mtime.map(|x| {
            SystemTime::UNIX_EPOCH
                .checked_add(Duration::from_secs(x))
                .unwrap_or(SystemTime::UNIX_EPOCH)
        });
        let symlink = match metadata.file_type().is_symlink() {
            false => None,
            true => match self.inner.readlink(path) {
                Ok(p) => Some(p),
                Err(err) => {
                    error!(
                        "Failed to read link of {} (even it's supposed to be a symlink): {}",
                        path.display(),
                        err
                    );
                    None
                }
            },
        };
        let file_type = if symlink.is_some() {
            FileType::Symlink
        } else if metadata.is_dir() {
            FileType::Directory
        } else {
            FileType::File
        };
        let entry_metadata = Metadata {
            accessed,
            created: None,
            file_type,
            gid,
            mode,
            modified,
            size,
            symlink,
            uid,
        };
        trace!("Metadata for {}: {:?}", path.display(), entry_metadata);
        File {
            path: path.to_path_buf(),
            metadata: entry_metadata,
        }
    }
}

fn perform_shell_cmd<S: AsRef<str>>(session: &mut ssh2::Session, cmd: S) -> RemoteResult<String> {
    // Create channel
    trace!("Running command: {}", cmd.as_ref());
    let mut channel = match session.channel_session() {
        Ok(ch) => ch,
        Err(err) => {
            return Err(RemoteError::new_ex(
                RemoteErrorType::ProtocolError,
                format!("Could not open channel: {err}"),
            ));
        }
    };

    // escape single quotes in command
    let cmd = cmd.as_ref().replace('\'', r#"'\''"#); // close, escape, and reopen

    // Execute command; always execute inside of sh -c to have proper shell behavior.
    // if the remote peer has fish or other non-bash shell as default, commands like
    // "cd /some/dir; somecommand" may fail.
    if let Err(err) = channel.exec(format!("sh -c '{cmd}'").as_str()) {
        return Err(RemoteError::new_ex(
            RemoteErrorType::ProtocolError,
            format!("Could not execute command \"{cmd}\": {err}"),
        ));
    }
    // Read output
    let mut output: String = String::new();
    match channel.read_to_string(&mut output) {
        Ok(_) => {
            // Wait close
            let _ = channel.wait_close();
            trace!("Command output: {output}");
            Ok(output)
        }
        Err(err) => Err(RemoteError::new_ex(
            RemoteErrorType::ProtocolError,
            format!("Could not read output: {err}"),
        )),
    }
}

/// connect to socket address with provided timeout.
/// If timeout is zero, don't set timeout
fn tcp_connect(address: &SocketAddr, timeout: Duration) -> std::io::Result<TcpStream> {
    if timeout.is_zero() {
        TcpStream::connect(address)
    } else {
        TcpStream::connect_timeout(address, timeout)
    }
}

/// Configure algorithm preferences into session
fn set_algo_prefs(
    session: &mut ssh2::Session,
    opts: &SshOpts,
    config: &Config,
) -> RemoteResult<()> {
    // Configure preferences from config
    let params = &config.params;
    trace!("Configuring algorithm preferences...");
    if let Some(compress) = params.compression {
        trace!("compression: {compress}");
        session.set_compress(compress);
    }

    // kex
    let algos = params.kex_algorithms.algorithms().join(",");
    trace!("Configuring KEX algorithms: {algos}");
    if let Err(err) = session.method_pref(ssh2::MethodType::Kex, algos.as_str()) {
        error!("Could not set KEX algorithms: {err}");
        return Err(RemoteError::new_ex(RemoteErrorType::ProtocolError, err));
    }

    // HostKey
    let algos = params.host_key_algorithms.algorithms().join(",");
    trace!("Configuring HostKey algorithms: {algos}");
    if let Err(err) = session.method_pref(ssh2::MethodType::HostKey, algos.as_str()) {
        error!("Could not set host key algorithms: {err}");
        return Err(RemoteError::new_ex(RemoteErrorType::ProtocolError, err));
    }

    // ciphers
    let algos = params.ciphers.algorithms().join(",");
    trace!("Configuring Crypt algorithms: {algos}");
    if let Err(err) = session.method_pref(ssh2::MethodType::CryptCs, algos.as_str()) {
        error!("Could not set crypt algorithms (client-server): {err}");
        return Err(RemoteError::new_ex(RemoteErrorType::ProtocolError, err));
    }
    if let Err(err) = session.method_pref(ssh2::MethodType::CryptSc, algos.as_str()) {
        error!("Could not set crypt algorithms (server-client): {err}");
        return Err(RemoteError::new_ex(RemoteErrorType::ProtocolError, err));
    }

    // MAC
    let algos = params.mac.algorithms().join(",");
    trace!("Configuring MAC algorithms: {algos}");
    if let Err(err) = session.method_pref(ssh2::MethodType::MacCs, algos.as_str()) {
        error!("Could not set MAC algorithms (client-server): {err}");
        return Err(RemoteError::new_ex(RemoteErrorType::ProtocolError, err));
    }
    if let Err(err) = session.method_pref(ssh2::MethodType::MacSc, algos.as_str()) {
        error!("Could not set MAC algorithms (server-client): {err}");
        return Err(RemoteError::new_ex(RemoteErrorType::ProtocolError, err));
    }

    // -- configure algos from opts
    for method in opts.methods.iter() {
        let algos = method.prefs();
        trace!("Configuring {:?} algorithm: {}", method.method_type, algos);
        if let Err(err) = session.method_pref(method.method_type.into(), algos.as_str()) {
            error!("Could not set {:?} algorithms: {}", method.method_type, err);
            return Err(RemoteError::new_ex(RemoteErrorType::ProtocolError, err));
        }
    }
    Ok(())
}

/// Authenticate on session with ssh agent
fn session_auth_with_agent(
    session: &mut ssh2::Session,
    username: &str,
    ssh_agent_config: &SshAgentIdentity,
) -> RemoteResult<()> {
    let mut agent = session
        .agent()
        .map_err(|err| RemoteError::new_ex(RemoteErrorType::ConnectionError, err))?;

    agent
        .connect()
        .map_err(|err| RemoteError::new_ex(RemoteErrorType::ConnectionError, err))?;

    agent
        .list_identities()
        .map_err(|err| RemoteError::new_ex(RemoteErrorType::ConnectionError, err))?;

    let mut connection_result = Err(RemoteError::new(RemoteErrorType::AuthenticationFailed));

    for identity in agent
        .identities()
        .map_err(|err| RemoteError::new_ex(RemoteErrorType::ConnectionError, err))?
    {
        if ssh_agent_config.pubkey_matches(identity.blob()) {
            debug!("Trying to authenticate with ssh agent with key: {identity:?}");
        } else {
            continue;
        }
        match agent.userauth(username, &identity) {
            Ok(()) => {
                connection_result = Ok(());
                debug!("Authenticated with ssh agent with key: {identity:?}");
                break;
            }
            Err(err) => {
                debug!("SSH agent auth failed: {err}");
                connection_result = Err(RemoteError::new_ex(
                    RemoteErrorType::AuthenticationFailed,
                    err,
                ));
            }
        }
    }

    if let Err(err) = agent.disconnect() {
        warn!("Could not disconnect from ssh agent: {err}");
    }

    connection_result
}

/// Authenticate on session with private key
fn session_auth_with_rsakey(
    session: &mut ssh2::Session,
    username: &str,
    private_key: &Path,
    password: Option<&str>,
    identity_file: Option<&[PathBuf]>,
) -> RemoteResult<()> {
    debug!("Authenticating with username '{username}' and RSA key");
    let mut keys = vec![private_key];
    if let Some(identity_file) = identity_file {
        let other_keys: Vec<&Path> = identity_file.iter().map(|x| x.as_path()).collect();
        keys.extend(other_keys);
    }
    // iterate over keys
    for key in keys.into_iter() {
        trace!("Trying to authenticate with RSA key at '{}'", key.display());
        match session.userauth_pubkey_file(username, None, key, password) {
            Ok(_) => {
                debug!("Authenticated with key at '{}'", key.display());
                return Ok(());
            }
            Err(err) => {
                error!("Authentication failed: {err}");
            }
        }
    }
    Err(RemoteError::new_ex(
        RemoteErrorType::AuthenticationFailed,
        "could not find any suitable RSA key to authenticate with",
    ))
}

/// Authenticate on session with the provided [`Authentication`] method.
fn session_auth(
    session: &mut ssh2::Session,
    opts: &SshOpts,
    ssh_config: &Config,
    authentication: Authentication,
) -> RemoteResult<()> {
    match authentication {
        Authentication::RsaKey(private_key) => session_auth_with_rsakey(
            session,
            &ssh_config.username,
            private_key.as_path(),
            opts.password.as_deref(),
            ssh_config.params.identity_file.as_deref(),
        ),
        Authentication::Password(password) => {
            session_auth_with_password(session, &ssh_config.username, &password)
        }
    }
}

/// Authenticate on session with username and password
fn session_auth_with_password(
    session: &mut ssh2::Session,
    username: &str,
    password: &str,
) -> RemoteResult<()> {
    // Username / password
    debug!("Authenticating with username '{username}' and password");
    if let Err(err) = session.userauth_password(username, password) {
        error!("Authentication failed: {err}");
        Err(RemoteError::new_ex(
            RemoteErrorType::AuthenticationFailed,
            err,
        ))
    } else {
        Ok(())
    }
}

#[cfg(test)]
mod test {

    use ssh2_config::ParseRule;

    use super::*;
    use crate::mock::ssh as ssh_mock;

    #[test]
    fn should_connect_to_ssh_server_auth_user_password() {
        use crate::ssh::container::OpensshServer;

        let container = OpensshServer::start();
        let port = container.port();

        crate::mock::logger();
        let config_file = ssh_mock::create_ssh_config(port);
        let opts = SshOpts::new("sftp")
            .config_file(config_file.path(), ParseRule::ALLOW_UNKNOWN_FIELDS)
            .password("password");

        if let Err(err) = LibSsh2Session::connect(&opts) {
            panic!("Could not connect to server: {err}");
        }
        let session = LibSsh2Session::connect(&opts).unwrap();
        assert!(session.authenticated().unwrap());

        drop(container);
    }

    #[test]
    fn should_connect_to_ssh_server_auth_key() {
        use crate::ssh::container::OpensshServer;

        let container = OpensshServer::start();
        let port = container.port();

        crate::mock::logger();
        let config_file = ssh_mock::create_ssh_config(port);
        let opts = SshOpts::new("sftp")
            .config_file(config_file.path(), ParseRule::ALLOW_UNKNOWN_FIELDS)
            .key_storage(Box::new(ssh_mock::MockSshKeyStorage::default()));
        let session = LibSsh2Session::connect(&opts).unwrap();
        assert!(session.authenticated().unwrap());
    }

    #[test]

    fn should_perform_shell_command_on_server() {
        crate::mock::logger();
        let container = crate::ssh::container::OpensshServer::start();
        let port = container.port();

        let opts = SshOpts::new("127.0.0.1")
            .port(port)
            .username("sftp")
            .password("password");
        let mut session = LibSsh2Session::connect(&opts).unwrap();
        assert!(session.authenticated().unwrap());
        // run commands
        assert!(session.cmd("pwd").is_ok());
    }

    #[test]

    fn should_perform_shell_command_on_server_and_return_exit_code() {
        crate::mock::logger();
        let container = crate::ssh::container::OpensshServer::start();
        let port = container.port();

        let opts = SshOpts::new("127.0.0.1")
            .port(port)
            .username("sftp")
            .password("password");
        let mut session = LibSsh2Session::connect(&opts).unwrap();
        assert!(session.authenticated().unwrap());
        // run commands
        assert_eq!(
            session.cmd_at("pwd", Path::new("/tmp")).ok().unwrap(),
            (0, String::from("/tmp\n"))
        );
        assert_eq!(
            session
                .cmd_at("pippopluto", Path::new("/tmp"))
                .ok()
                .unwrap()
                .0,
            127
        );
    }

    #[test]
    fn should_fail_authentication() {
        crate::mock::logger();
        let container = crate::ssh::container::OpensshServer::start();
        let port = container.port();

        let opts = SshOpts::new("127.0.0.1")
            .port(port)
            .username("sftp")
            .password("ippopotamo");
        assert!(LibSsh2Session::connect(&opts).is_err());
    }

    #[test]
    fn test_filetransfer_sftp_bad_server() {
        crate::mock::logger();
        let opts = SshOpts::new("myverybad.verybad.server")
            .port(10022)
            .username("sftp")
            .password("ippopotamo");
        assert!(LibSsh2Session::connect(&opts).is_err());
    }
}