rust-data-processing 0.3.4

Schema-first ingestion (CSV, JSON, Parquet, Excel) into an in-memory DataSet, plus Polars-backed pipelines, SQL, profiling, validation, and map/reduce-style processing.
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
//! SFTP and FTP/FTPS file ingest (download remote object, then use local path readers).
//!
//! URIs: `sftp://`, `ftp://`, `ftps://`
//!
//! Credentials are read from the URL userinfo when present; production passwords and SSH keys
//! should be supplied via environment variables on the process running Rust (not pipeline JSON):
//!
//! - `SFTP_PASSWORD` / `FTP_PASSWORD` — override URL password when set
//! - `SFTP_USER` / `FTP_USER` — used when URL has no username
//! - `SFTP_PRIVATE_KEY_PATH` — SSH private key for SFTP (optional; password auth otherwise)

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

use ssh2::Session;
use url::Url;

use crate::error::{IngestionError, IngestionResult};
use crate::types::{DataSet, Schema};

use super::{IngestionFormat, IngestionOptions, ingest_from_path};

/// Returns true when `uri` uses a supported file-transfer scheme.
pub fn is_file_transfer_uri(uri: &str) -> bool {
    file_transfer_scheme(uri).is_some()
}

/// `sftp`, `ftp`, or `ftps` when recognized.
pub fn file_transfer_scheme(uri: &str) -> Option<&'static str> {
    let lower = uri.to_ascii_lowercase();
    if lower.starts_with("sftp://") {
        Some("sftp")
    } else if lower.starts_with("ftps://") {
        Some("ftps")
    } else if lower.starts_with("ftp://") {
        Some("ftp")
    } else {
        None
    }
}

fn temp_download_path(suffix: &str) -> IngestionResult<PathBuf> {
    let stamp = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_nanos();
    Ok(std::env::temp_dir().join(format!("rdp_ft_dl_{stamp}{suffix}")))
}

fn infer_format_from_remote_path(
    remote_path: &str,
    options: &IngestionOptions,
) -> IngestionResult<IngestionFormat> {
    if let Some(f) = options.format {
        return Ok(f);
    }
    let ext = Path::new(remote_path)
        .extension()
        .and_then(|e| e.to_str())
        .unwrap_or("");
    match ext.to_ascii_lowercase().as_str() {
        "csv" => Ok(IngestionFormat::Csv),
        "json" | "ndjson" => Ok(IngestionFormat::Json),
        "parquet" => Ok(IngestionFormat::Parquet),
        "xml" => Ok(IngestionFormat::Xml),
        _ => Err(IngestionError::SchemaMismatch {
            message: format!(
                "cannot infer ingest format from remote path `{remote_path}`; set sources.options.format"
            ),
        }),
    }
}

struct TransferAuth {
    username: String,
    password: Option<String>,
    private_key_path: Option<PathBuf>,
}

fn env_password(scheme: &str) -> Option<String> {
    let key = match scheme {
        "sftp" => "SFTP_PASSWORD",
        "ftp" | "ftps" => "FTP_PASSWORD",
        _ => return None,
    };
    std::env::var(key).ok().filter(|s| !s.is_empty())
}

fn env_username(scheme: &str) -> Option<String> {
    let key = match scheme {
        "sftp" => "SFTP_USER",
        "ftp" | "ftps" => "FTP_USER",
        _ => return None,
    };
    std::env::var(key).ok().filter(|s| !s.is_empty())
}

fn resolve_auth(scheme: &str, url: &Url) -> IngestionResult<TransferAuth> {
    let username = if url.username().is_empty() {
        env_username(scheme).ok_or_else(|| IngestionError::SchemaMismatch {
            message: format!(
                "{scheme} URI must include a username or set {} env var",
                if scheme == "sftp" {
                    "SFTP_USER"
                } else {
                    "FTP_USER"
                }
            ),
        })?
    } else {
        url.username().to_string()
    };

    let password = env_password(scheme).or_else(|| url.password().map(|s| s.to_string()));

    let private_key_path = if scheme == "sftp" {
        std::env::var("SFTP_PRIVATE_KEY_PATH")
            .ok()
            .filter(|s| !s.is_empty())
            .map(PathBuf::from)
    } else {
        None
    };

    Ok(TransferAuth {
        username,
        password,
        private_key_path,
    })
}

fn parse_file_transfer_url(uri: &str) -> IngestionResult<(String, u16, String, TransferAuth)> {
    let url = Url::parse(uri).map_err(|e| IngestionError::SchemaMismatch {
        message: format!("invalid file-transfer URI `{uri}`: {e}"),
    })?;
    let scheme = url.scheme();
    if !matches!(scheme, "sftp" | "ftp" | "ftps") {
        return Err(IngestionError::SchemaMismatch {
            message: format!("unsupported file-transfer scheme `{scheme}` in `{uri}`"),
        });
    }

    let host = url
        .host_str()
        .ok_or_else(|| IngestionError::SchemaMismatch {
            message: format!("file-transfer URI missing host: `{uri}`"),
        })?
        .to_string();

    let port = url.port().unwrap_or(match scheme {
        "sftp" => 22,
        "ftp" => 21,
        "ftps" => 990,
        _ => 0,
    });

    let remote_path = url.path().to_string();
    if remote_path.is_empty() || remote_path == "/" {
        return Err(IngestionError::SchemaMismatch {
            message: format!("file-transfer URI must include a remote file path: `{uri}`"),
        });
    }

    let auth = resolve_auth(scheme, &url)?;
    Ok((host, port, remote_path, auth))
}

fn download_sftp(
    host: &str,
    port: u16,
    remote_path: &str,
    auth: &TransferAuth,
    local: &Path,
) -> IngestionResult<()> {
    let addr = format!("{host}:{port}");
    let tcp = TcpStream::connect(&addr).map_err(|e| IngestionError::Engine {
        message: format!("sftp connect `{addr}`"),
        source: Box::new(e),
    })?;

    let mut sess = Session::new().map_err(|e| IngestionError::Engine {
        message: "sftp session init".to_string(),
        source: Box::new(e),
    })?;
    sess.set_tcp_stream(tcp);
    sess.handshake().map_err(|e| IngestionError::Engine {
        message: format!("sftp handshake `{addr}`"),
        source: Box::new(e),
    })?;

    if let Some(key_path) = &auth.private_key_path {
        let passphrase = auth.password.as_deref();
        sess.userauth_pubkey_file(&auth.username, None, key_path, passphrase)
            .map_err(|e| IngestionError::Engine {
                message: format!("sftp pubkey auth user `{}`", auth.username),
                source: Box::new(e),
            })?;
    } else {
        let pass = auth.password.as_deref().unwrap_or("");
        sess.userauth_password(&auth.username, pass)
            .map_err(|e| IngestionError::Engine {
                message: format!("sftp password auth user `{}`", auth.username),
                source: Box::new(e),
            })?;
    }

    if !sess.authenticated() {
        return Err(IngestionError::SchemaMismatch {
            message: format!(
                "sftp authentication failed for user `{}` (set SFTP_PASSWORD or SFTP_PRIVATE_KEY_PATH)",
                auth.username
            ),
        });
    }

    let sftp = sess.sftp().map_err(|e| IngestionError::Engine {
        message: "sftp subsystem".to_string(),
        source: Box::new(e),
    })?;
    let mut remote = sftp
        .open(Path::new(remote_path))
        .map_err(|e| IngestionError::Engine {
            message: format!("sftp open `{remote_path}`"),
            source: Box::new(e),
        })?;

    let mut local_file = std::fs::File::create(local).map_err(IngestionError::Io)?;
    std::io::copy(&mut remote, &mut local_file).map_err(|e| IngestionError::Engine {
        message: format!("sftp read `{remote_path}`"),
        source: Box::new(e),
    })?;
    Ok(())
}

fn split_remote_dir_file(remote_path: &str) -> (String, String) {
    let p = remote_path.trim_start_matches('/');
    match p.rsplit_once('/') {
        Some(("", file)) => (String::new(), file.to_string()),
        Some((dir, file)) => (dir.to_string(), file.to_string()),
        None => (String::new(), p.to_string()),
    }
}

fn download_ftp(
    host: &str,
    port: u16,
    remote_path: &str,
    auth: &TransferAuth,
    local: &Path,
    use_tls: bool,
) -> IngestionResult<()> {
    let addr = format!("{host}:{port}");
    let password = auth.password.as_deref().unwrap_or("");

    let (dir, file_name) = split_remote_dir_file(remote_path);
    if file_name.is_empty() {
        return Err(IngestionError::SchemaMismatch {
            message: format!("ftp remote path must name a file: `{remote_path}`"),
        });
    }

    let bytes = if use_tls {
        download_ftp_tls(&addr, host, &auth.username, password, &dir, &file_name)?
    } else {
        download_ftp_plain(&addr, &auth.username, password, &dir, &file_name)?
    };

    let mut local_file = std::fs::File::create(local).map_err(IngestionError::Io)?;
    local_file.write_all(&bytes).map_err(IngestionError::Io)?;
    Ok(())
}

fn download_ftp_plain(
    addr: &str,
    user: &str,
    password: &str,
    dir: &str,
    file_name: &str,
) -> IngestionResult<Vec<u8>> {
    use suppaftp::FtpStream;
    use suppaftp::types::FileType;

    let mut ftp = FtpStream::connect(addr).map_err(|e| IngestionError::Engine {
        message: format!("ftp connect `{addr}`"),
        source: Box::new(e),
    })?;
    ftp.login(user, password)
        .map_err(|e| IngestionError::Engine {
            message: format!("ftp login user `{user}`"),
            source: Box::new(e),
        })?;
    if !dir.is_empty() {
        ftp.cwd(dir).map_err(|e| IngestionError::Engine {
            message: format!("ftp cwd `{dir}`"),
            source: Box::new(e),
        })?;
    }
    ftp.transfer_type(FileType::Binary)
        .map_err(|e| IngestionError::Engine {
            message: "ftp binary mode".to_string(),
            source: Box::new(e),
        })?;
    let data = ftp
        .retr_as_buffer(file_name)
        .map_err(|e| IngestionError::Engine {
            message: format!("ftp retr `{file_name}`"),
            source: Box::new(e),
        })?;
    let _ = ftp.quit();
    Ok(data.into_inner())
}

fn download_ftp_tls(
    addr: &str,
    host: &str,
    user: &str,
    password: &str,
    dir: &str,
    file_name: &str,
) -> IngestionResult<Vec<u8>> {
    use std::sync::Arc;

    use suppaftp::rustls::ClientConfig;
    use suppaftp::types::FileType;
    use suppaftp::{RustlsConnector, RustlsFtpStream};

    let mut root_store = suppaftp::rustls::RootCertStore::empty();
    root_store.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());

    let config = ClientConfig::builder()
        .with_root_certificates(root_store)
        .with_no_client_auth();

    let ftp_plain = RustlsFtpStream::connect(addr).map_err(|e| IngestionError::Engine {
        message: format!("ftps connect `{addr}`"),
        source: Box::new(e),
    })?;
    let mut ftp = ftp_plain
        .into_secure(RustlsConnector::from(Arc::new(config)), host)
        .map_err(|e| IngestionError::Engine {
            message: format!("ftps tls handshake `{host}`"),
            source: Box::new(e),
        })?;
    ftp.login(user, password)
        .map_err(|e| IngestionError::Engine {
            message: format!("ftps login user `{user}`"),
            source: Box::new(e),
        })?;
    if !dir.is_empty() {
        ftp.cwd(dir).map_err(|e| IngestionError::Engine {
            message: format!("ftps cwd `{dir}`"),
            source: Box::new(e),
        })?;
    }
    ftp.transfer_type(FileType::Binary)
        .map_err(|e| IngestionError::Engine {
            message: "ftps binary mode".to_string(),
            source: Box::new(e),
        })?;
    let data = ftp
        .retr_as_buffer(file_name)
        .map_err(|e| IngestionError::Engine {
            message: format!("ftps retr `{file_name}`"),
            source: Box::new(e),
        })?;
    let _ = ftp.quit();
    Ok(data.into_inner())
}

/// Download a remote file over SFTP or FTP/FTPS and ingest into a [`DataSet`].
pub fn ingest_from_file_transfer_uri(
    uri: &str,
    schema: &Schema,
    options: &IngestionOptions,
) -> IngestionResult<DataSet> {
    let scheme = file_transfer_scheme(uri).ok_or_else(|| IngestionError::SchemaMismatch {
        message: format!("not a file-transfer URI (expected sftp://, ftp://, or ftps://): `{uri}`"),
    })?;

    let (host, port, remote_path, auth) = parse_file_transfer_url(uri)?;
    let fmt = infer_format_from_remote_path(&remote_path, options)?;
    let suffix = match fmt {
        IngestionFormat::Csv => ".csv",
        IngestionFormat::Json => ".json",
        IngestionFormat::Parquet => ".parquet",
        IngestionFormat::Xml => ".xml",
        IngestionFormat::Excel => {
            return Err(IngestionError::SchemaMismatch {
                message: "excel ingest from file-transfer URIs is not supported".to_string(),
            });
        }
    };

    let local = temp_download_path(suffix)?;
    match scheme {
        "sftp" => download_sftp(&host, port, &remote_path, &auth, &local)?,
        "ftp" => download_ftp(&host, port, &remote_path, &auth, &local, false)?,
        "ftps" => download_ftp(&host, port, &remote_path, &auth, &local, true)?,
        _ => unreachable!(),
    }

    let mut opts = options.clone();
    opts.format = Some(fmt);
    let ds = ingest_from_path(&local, schema, &opts)?;
    let _ = std::fs::remove_file(&local);
    Ok(ds)
}

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

    #[test]
    fn is_file_transfer_uri_recognizes_schemes() {
        assert!(is_file_transfer_uri("sftp://u:p@host:22/a.parquet"));
        assert!(is_file_transfer_uri("ftp://u:p@host/rdp/in.csv"));
        assert!(is_file_transfer_uri("ftps://u:p@host:990/x.json"));
        assert!(!is_file_transfer_uri("s3://bucket/key"));
    }

    #[test]
    fn parse_url_uses_default_ports() {
        let (host, port, path, auth) = parse_file_transfer_url(
            "ftp://etl_user:secret@ftp.example.com/rdp/incoming/data.parquet",
        )
        .unwrap();
        assert_eq!(host, "ftp.example.com");
        assert_eq!(port, 21);
        assert_eq!(path, "/rdp/incoming/data.parquet");
        assert_eq!(auth.username, "etl_user");
        assert_eq!(auth.password.as_deref(), Some("secret"));
    }

    #[test]
    fn split_remote_dir_file_handles_nested_paths() {
        let (dir, file) = split_remote_dir_file("/rdp/incoming/data.parquet");
        assert_eq!(dir, "rdp/incoming");
        assert_eq!(file, "data.parquet");
    }
}