sett 0.4.0

Rust port of sett (data compression, encryption and transfer tool).
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
//! SFTP data transfers
//!
//! This module provides support for establishing SSH connections and
//! transferring data with SFTP.

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

use chrono::Utc;
use ssh2::{RenameFlags, Session, Sftp};
use tracing::{debug, info, instrument};

use crate::{
    progress::{ProgressDisplay, ProgressReader},
    secret::Secret,
    task::{Mode, Status},
};

/// Holds parameters necessary for establishing an SSH connection.
pub struct ClientBuilder {
    /// Domain name (or IP address) of the SFTP server.
    host: String,
    /// SFTP server port number.
    port: u16,
    /// User name for authentication with the SFTP server.
    username: String,
    /// Private SSH key path.
    key_path: Option<PathBuf>,
    /// Private SSH key password.
    key_password: Option<Secret>,
    /// Two factor authentication callback function.
    two_factor_callback: Option<Box<dyn Fn() -> String + Send + Sync>>,
}

impl std::fmt::Debug for ClientBuilder {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ClientBuilder")
            .field("host", &self.host)
            .field("port", &self.port)
            .field("username", &self.username)
            .field("key_path", &self.key_path)
            .field("key_password", &self.key_password)
            .finish()
    }
}

impl Default for ClientBuilder {
    fn default() -> Self {
        Self {
            host: "localhost".into(),
            port: 22,
            username: "user".into(),
            key_path: None,
            key_password: None,
            two_factor_callback: None,
        }
    }
}

impl ClientBuilder {
    /// Creates a new builder.
    ///
    /// Default values:
    ///
    /// - `host`: `localhost`
    /// - `port`: `22`
    /// - `username` : `user`
    pub fn new() -> Self {
        Default::default()
    }

    /// Builds a new [`Client`] instance.
    pub fn build(self) -> Client {
        Client::new(self)
    }

    /// Sets the domain name (or IP address) of the SFTP server.
    ///
    /// Default is `localhost`.
    pub fn host(mut self, host: impl Into<String>) -> Self {
        self.host = host.into();
        self
    }

    /// Sets the SFTP server port number.
    ///
    /// Default is `22`.
    pub fn port(mut self, port: u16) -> Self {
        self.port = port;
        self
    }

    /// Sets the user name for authentication with the SFTP server.
    ///
    /// Default is `root`.
    pub fn username(mut self, username: impl Into<String>) -> Self {
        self.username = username.into();
        self
    }

    /// Sets the private SSH key path.
    pub fn key_path(mut self, key_path: Option<impl Into<PathBuf>>) -> Self {
        self.key_path = key_path.map(Into::into);
        self
    }

    /// Sets the private SSH key password.
    pub fn key_password(mut self, key_password: Option<impl Into<Secret>>) -> Self {
        self.key_password = key_password.map(Into::into);
        self
    }

    /// Sets the two factor authentication callback function.
    pub fn two_factor_callback<F: Fn() -> String + Send + Sync + 'static>(
        mut self,
        two_factor_callback: Option<F>,
    ) -> Self {
        self.two_factor_callback =
            two_factor_callback.map(|f| -> Box<dyn Fn() -> String + Send + Sync> { Box::new(f) });
        self
    }
}

/// SFTP client.
pub struct Client {
    builder: ClientBuilder,
}

impl std::fmt::Debug for Client {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Client")
            .field(
                "host_url",
                &format!(
                    "sftp://{}@{}:{}",
                    self.builder.username, self.builder.host, self.builder.port
                ),
            )
            .finish()
    }
}

impl Client {
    /// Creates a new builder.
    pub fn builder() -> ClientBuilder {
        ClientBuilder::new()
    }

    fn new(builder: ClientBuilder) -> Self {
        Self { builder }
    }
    /// Establishes an SSH connection.
    pub(crate) fn connect(&self) -> Result<ClientConnected, error::ConnectionError> {
        Ok(ClientConnected {
            inner: connect(&self.builder)?,
            host_url: format!("sftp://{}:{}", self.builder.host, self.builder.port),
        })
    }
}

pub(crate) struct ClientConnected {
    pub(crate) inner: Sftp,
    host_url: String,
}

impl ClientConnected {
    /// Returns the URL of the SFTP server.
    ///
    /// The URL is in the form `sftp://<username>@<host>:<port>/<path>`.
    pub(crate) fn get_url(&self, path: &Path) -> String {
        format!("{}/{}", self.host_url, path.to_string_lossy())
    }
}

fn make_session(host: &str, port: u16) -> Result<Session, error::ConnectionError> {
    let tcp = TcpStream::connect(format!("{host}:{port}"))?;
    let mut session = Session::new()?;
    session.set_tcp_stream(tcp);
    session.handshake()?;
    Ok(session)
}

/// Error namespace
pub mod error {
    /// Error with the credentials
    #[derive(Debug)]
    pub struct CredentialsError(pub std::str::Utf8Error);

    impl std::fmt::Display for CredentialsError {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(f, "password conversion to utf-8 failed")
        }
    }

    impl std::error::Error for CredentialsError {
        fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
            Some(&self.0)
        }
    }

    /// Error occurring when connecting
    #[derive(Debug)]
    pub enum ConnectionError {
        /// Error with the credentials
        Credentials(CredentialsError),
        /// I/O error
        Io(std::io::Error),
        /// ssh session error
        Session(ssh2::Error),
        /// Trouble with the ssh agent
        AgentAuth,
        /// Unsupported ssh auth method requested
        UnsupportedMethods(String),
        /// Request for 2nd factor, but no callback available
        Missing2ndFactor,
    }

    impl std::fmt::Display for ConnectionError {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(f, "sftp connection error")?;
            match self {
                Self::AgentAuth => {
                    write!(f, ": Agent authentication failed")
                }
                Self::UnsupportedMethods(methods) => write!(
                    f,
                    ": The following method(s) are not supported (client side) during multi factor authentication: {methods}"
                ),
                Self::Missing2ndFactor => write!(
                    f,
                    ": A second factor was requested but no two_factor_callback available"
                ),
                _ => Ok(()),
            }
        }
    }

    impl std::error::Error for ConnectionError {
        fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
            match self {
                Self::Credentials(source) => Some(source),
                Self::Io(source) => Some(source),
                Self::Session(source) => Some(source),
                _ => None,
            }
        }
    }

    impl From<CredentialsError> for ConnectionError {
        fn from(value: CredentialsError) -> Self {
            Self::Credentials(value)
        }
    }
    impl From<std::io::Error> for ConnectionError {
        fn from(value: std::io::Error) -> Self {
            Self::Io(value)
        }
    }
    impl From<ssh2::Error> for ConnectionError {
        fn from(value: ssh2::Error) -> Self {
            Self::Session(value)
        }
    }

    /// Error occurring when deleting files / folders
    #[derive(Debug)]
    pub enum DeleteError {
        /// ssh session error
        Session(ssh2::Error),
        /// Not allowed to delete
        Denied(String),
    }

    impl std::fmt::Display for DeleteError {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(f, "sftp delete error")?;
            match self {
                Self::Denied(file) => {
                    write!(
                        f,
                        ": Cannot delete '{file}'. Only '*.part' files can be deleted"
                    )
                }
                _ => Ok(()),
            }
        }
    }

    impl std::error::Error for DeleteError {
        fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
            match self {
                Self::Session(source) => Some(source),
                _ => None,
            }
        }
    }
    impl From<ssh2::Error> for DeleteError {
        fn from(value: ssh2::Error) -> Self {
            Self::Session(value)
        }
    }

    type MetadataError = crate::package::error::MetadataError<
        <std::path::PathBuf as crate::package::source::PackageStream>::Error,
    >;

    /// Error occurring when uploading
    #[derive(Debug)]
    pub enum UploadError {
        /// I/O error
        Io(std::io::Error),
        /// Async join task error
        Join(tokio::task::JoinError),
        /// ssh session error
        Session(ssh2::Error),
        /// ssh connection error
        Connection(ConnectionError),
        /// Something wrong with the metadata
        Metadata(MetadataError),
    }

    impl std::fmt::Display for UploadError {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(f, "sftp upload error")
        }
    }

    impl std::error::Error for UploadError {
        fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
            match self {
                Self::Io(source) => Some(source),
                Self::Join(source) => Some(source),
                Self::Session(source) => Some(source),
                Self::Connection(source) => Some(source),
                Self::Metadata(source) => Some(source),
            }
        }
    }

    impl From<std::io::Error> for UploadError {
        fn from(value: std::io::Error) -> Self {
            Self::Io(value)
        }
    }
    impl From<tokio::task::JoinError> for UploadError {
        fn from(value: tokio::task::JoinError) -> Self {
            Self::Join(value)
        }
    }
    impl From<ssh2::Error> for UploadError {
        fn from(value: ssh2::Error) -> Self {
            Self::Session(value)
        }
    }
    impl From<ConnectionError> for UploadError {
        fn from(value: ConnectionError) -> Self {
            Self::Connection(value)
        }
    }
    impl From<MetadataError> for UploadError {
        fn from(value: MetadataError) -> Self {
            Self::Metadata(value)
        }
    }
}

/// Establish an SSH connection.
///
/// Connect using the SSH key or SSH Agent (including support for 2 factor authentication).
fn connect(sftp_opts: &ClientBuilder) -> Result<Sftp, error::ConnectionError> {
    let mut session = make_session(sftp_opts.host.as_ref(), sftp_opts.port)?;
    // TODO should we check known hosts?
    if let Some(key) = sftp_opts.key_path.as_deref() {
        if let Some(password) = &sftp_opts.key_password {
            password
                .as_inner()
                .map(|p| -> Result<(), error::ConnectionError> {
                    session.userauth_pubkey_file(
                        sftp_opts.username.as_ref(),
                        None,
                        Path::new(key),
                        Some(std::str::from_utf8(p.as_ref()).map_err(error::CredentialsError)?),
                    )?;
                    Ok(())
                })?;
        } else {
            session.userauth_pubkey_file(
                sftp_opts.username.as_ref(),
                None,
                Path::new(key),
                None,
            )?;
        }
    } else {
        debug!("No SSH key used. Using SSH Agent");
        connect_with_agent(sftp_opts.username.as_ref(), &mut session)?;
    }
    if !session.authenticated() {
        let methods = session
            .auth_methods(sftp_opts.username.as_ref())
            .unwrap_or("none");
        if methods != "keyboard-interactive" {
            return Err(error::ConnectionError::UnsupportedMethods(methods.into()));
        }
        debug!(
            "Partially connected. Trying second factor. Allowed methods: {}",
            methods
        );
        if let Some(cb) = &sftp_opts.two_factor_callback {
            let mut prompt = Prompt { cb };
            session.userauth_keyboard_interactive(sftp_opts.username.as_ref(), &mut prompt)?;
        } else {
            return Err(error::ConnectionError::Missing2ndFactor);
        }
    }

    Ok(session.sftp()?)
}

/// Establishes an SSH connection using SSH Agent.
fn connect_with_agent(username: &str, session: &mut Session) -> Result<(), error::ConnectionError> {
    let mut agent = session.agent()?;
    agent.connect()?;
    agent.list_identities()?;
    let identities = agent.identities()?;
    let key = &identities.iter().find(|i| {
        agent
            .userauth(username, i)
            .or_else(|e| {
                // For some reason, ssh2 returns code -19: "Invalid signature
                // for supplied public key, or bad username/public key combination",
                // where the server receives a "Partial publickey"
                if e.code() == ssh2::ErrorCode::Session(-19) {
                    Ok(())
                } else {
                    Err(e)
                }
            })
            .map_err(|e| {
                debug!("{:?}", e);
                e
            })
            .is_ok()
    });
    agent.disconnect()?;
    if key.is_none() {
        return Err(error::ConnectionError::AgentAuth);
    }
    Ok(())
}

/// A directory for uploading data packages on an SFTP server.
pub(crate) struct UploadDir<'a> {
    pub(crate) path: PathBuf,
    client: &'a ClientConnected,
}

impl<'a> UploadDir<'a> {
    pub(crate) fn new(base_path: &Path, client: &'a ClientConnected) -> Self {
        const DATETIME_FORMAT: &str = "%Y%m%dT%H%M%S_%f";
        Self {
            path: base_path.join(Utc::now().format(DATETIME_FORMAT).to_string()),
            client,
        }
    }

    /// Creates an empty directory on an SFTP server.
    pub(crate) fn create(&self, mode: Option<i32>) -> Result<(), ssh2::Error> {
        // TODO mkdir will fail if parent is missing, should it be recursive?
        self.client.inner.mkdir(&self.path, mode.unwrap_or(0o755))?;
        Ok(())
    }

    /// Creates a marker file indicating that no new packages will be uploaded
    /// to this directory.
    pub(crate) fn finalize(self) -> Result<(), ssh2::Error> {
        const UPLOAD_FINISHED_MARKER_NAME: &str = "done.txt";
        self.client
            .inner
            .create(&self.path.join(UPLOAD_FINISHED_MARKER_NAME))?;
        Ok(())
    }

    /// Deletes an upload directory and all its content in case of a failure
    /// during upload. The directory is expected to contain only `.part` files,
    /// the partially uploaded files.
    pub(crate) fn delete(&self) -> Result<(), error::DeleteError> {
        // Delete all files with a `.part` extension inside the directory.
        // This should in principle delete all files in the directory.
        for (file, _) in self.client.inner.readdir(&self.path)? {
            if file.extension().is_some_and(|e| e != "part") {
                return Err(error::DeleteError::Denied(
                    file.to_string_lossy().to_string(),
                ));
            }
            self.client.inner.unlink(&file)?
        }

        // Delete the directory itself, which should be empty at this point.
        self.client.inner.rmdir(&self.path)?;
        Ok(())
    }
}

/// A path to a data package on an SFTP server.
pub(crate) struct DpkgPath<'a> {
    pub(crate) tmp: PathBuf,
    pub(crate) path: PathBuf,
    client: &'a ClientConnected,
}

impl<'a> DpkgPath<'a> {
    pub(crate) fn new<P: AsRef<Path>, S: AsRef<str>>(
        base: P,
        name: S,
        client: &'a ClientConnected,
    ) -> Self {
        const UPLOAD_TMP_SUFFIX: &str = ".part";
        let p: PathBuf = base.as_ref().into();
        Self {
            tmp: p.join(format!("{}.{}", name.as_ref(), UPLOAD_TMP_SUFFIX)),
            path: p.join(name.as_ref()),
            client,
        }
    }

    /// Renames the package name to its final version.
    pub(crate) fn finalize(&self) -> Result<(), ssh2::Error> {
        self.client.inner.rename(
            &self.tmp,
            &self.path,
            Some(RenameFlags::ATOMIC | RenameFlags::NATIVE),
        )?;
        Ok(())
    }
}

/// Uploads files with SFTP.
///
/// This function establishes a connection to a remove SSH server and transfers
/// the provided files sequentially.
#[instrument(skip(progress), err(Debug, level=tracing::Level::ERROR))]
pub async fn upload(
    package: &crate::package::Package<PathBuf, crate::package::state::Verified>,
    client: &Client,
    base_path: &Path,
    mode: Mode,
    progress: Option<impl ProgressDisplay + Send + 'static>,
) -> Result<Status, error::UploadError> {
    let metadata = package.metadata().await?;
    let path = package.source().to_path_buf();
    let name = package.name.clone();
    let base_path = base_path.to_path_buf();
    let parent_span = tracing::Span::current();
    let client = client.connect()?;
    let handle = tokio::task::spawn_blocking(move || -> Result<Status, error::UploadError> {
        let thread_span = tracing::info_span!(parent: &parent_span, "sftp upload thread");
        let _enter = thread_span.enter();
        let source_size = path.metadata()?.len();
        let upload_dir = UploadDir::new(&base_path, &client);
        let dpkg_path = DpkgPath::new(&upload_dir.path, &name, &client);
        let destination = client.get_url(&dpkg_path.path);
        if let Mode::Check = mode {
            debug!(
                destination,
                source_size, "Checked {name} for transfer into {destination}"
            );
            return Ok(Status::Checked {
                destination,
                source_size,
            });
        }
        upload_dir.create(None)?;
        const BUF_SIZE: usize = 1 << 22;
        let mut reader = io::BufReader::with_capacity(BUF_SIZE, File::open(path)?);
        let mut fout = io::BufWriter::with_capacity(BUF_SIZE, client.inner.create(&dpkg_path.tmp)?);
        if let Some(p) = progress {
            let mut reader = ProgressReader::new(reader, p.start(source_size));
            io::copy(&mut reader, &mut fout)?;
        } else {
            io::copy(&mut reader, &mut fout)?;
        }
        dpkg_path.finalize()?;
        upload_dir.finalize()?;
        info!(
            destination,
            source_size,
            destination_size = source_size,
            "Successfully transferred {name} into {destination}"
        );
        Ok(Status::Completed {
            destination,
            source_size,
            destination_size: source_size,
            metadata,
        })
    });
    handle.await?
}

/// 2FA prompt.
struct Prompt<Cb: Fn() -> String> {
    cb: Cb,
}
impl<Cb: Fn() -> String> ssh2::KeyboardInteractivePrompt for Prompt<Cb> {
    fn prompt(
        &mut self,
        username: &str,
        instructions: &str,
        prompts: &[ssh2::Prompt],
    ) -> Vec<String> {
        debug!(
            "prompt: username='{}', instructions='{}', prompts={:?}",
            username, instructions, prompts
        );
        prompts
            .iter()
            .map(|p| {
                debug!("prompting for '{}'", p.text);
                let response = (self.cb)();
                debug!("Returning '{}'", response);
                response
            })
            .collect()
    }
}