tinyetl 0.12.0

Fast, zero-config ETL in a single binary for transforming data between formats and databases
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
use crate::{
    connectors::{create_source_with_options, Source, Target},
    protocols::Protocol,
    Result, TinyEtlError,
};
use async_trait::async_trait;
use std::collections::HashMap;
use std::process::Command;
use tempfile::NamedTempFile;
use tracing::info;
use url::Url;

/// SSH protocol for downloading files via SCP/SFTP.
/// Uses system SSH client for file transfers to temporary locations.
pub struct SshProtocol;

impl Default for SshProtocol {
    fn default() -> Self {
        Self::new()
    }
}

impl SshProtocol {
    pub fn new() -> Self {
        Self
    }

    /// Download a file via SCP to a temporary file with progress
    async fn download_via_scp(&self, url: &Url) -> Result<NamedTempFile> {
        // Parse SSH URL: ssh://user@host:port/path/to/file
        let host = url.host_str().ok_or_else(|| {
            TinyEtlError::Configuration("SSH URL must specify a host".to_string())
        })?;

        let username = if !url.username().is_empty() {
            url.username()
        } else {
            return Err(TinyEtlError::Configuration(
                "SSH URL must specify a username (ssh://user@host/path)".to_string(),
            ));
        };

        let port = url.port().unwrap_or(22);
        let remote_path = url.path();

        if remote_path.is_empty() || remote_path == "/" {
            return Err(TinyEtlError::Configuration(
                "SSH URL must specify a file path".to_string(),
            ));
        }

        // Create temporary file with appropriate extension
        let extension = self.extract_extension_from_path(remote_path);
        let temp_file = if let Some(ext) = extension {
            tempfile::Builder::new()
                .suffix(&format!(".{}", ext))
                .tempfile()
                .map_err(TinyEtlError::Io)?
        } else {
            tempfile::NamedTempFile::new().map_err(TinyEtlError::Io)?
        };

        let temp_path = temp_file.path().to_string_lossy().to_string();

        // Build SCP command: scp -P port user@host:remote_path local_path
        let scp_source = format!("{}@{}:{}", username, host, remote_path);

        info!("Downloading via SSH: {}", scp_source);

        let output = Command::new("scp")
            .arg("-P")
            .arg(port.to_string())
            .arg("-o")
            .arg("StrictHostKeyChecking=no") // Allow connecting to new hosts
            .arg("-o")
            .arg("UserKnownHostsFile=/dev/null") // Don't save host keys
            .arg("-q") // Quiet mode
            .arg(&scp_source)
            .arg(&temp_path)
            .output()
            .map_err(|e| {
                TinyEtlError::Connection(format!("Failed to execute scp command: {}", e))
            })?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(TinyEtlError::Connection(format!(
                "SCP failed to download file from {}: {}",
                scp_source, stderr
            )));
        }

        info!("SSH download completed");

        Ok(temp_file)
    }

    /// Upload a file via SCP (for target operations)
    #[allow(dead_code)]
    async fn upload_via_scp(&self, url: &Url, local_path: &str) -> Result<()> {
        let host = url.host_str().ok_or_else(|| {
            TinyEtlError::Configuration("SSH URL must specify a host".to_string())
        })?;

        let username = if !url.username().is_empty() {
            url.username()
        } else {
            return Err(TinyEtlError::Configuration(
                "SSH URL must specify a username (ssh://user@host/path)".to_string(),
            ));
        };

        let port = url.port().unwrap_or(22);
        let remote_path = url.path();

        if remote_path.is_empty() || remote_path == "/" {
            return Err(TinyEtlError::Configuration(
                "SSH URL must specify a file path".to_string(),
            ));
        }

        // Build SCP command: scp -P port local_path user@host:remote_path
        let scp_dest = format!("{}@{}:{}", username, host, remote_path);

        info!("Uploading via SSH to: {}", scp_dest);

        let output = Command::new("scp")
            .arg("-P")
            .arg(port.to_string())
            .arg("-o")
            .arg("StrictHostKeyChecking=no")
            .arg("-o")
            .arg("UserKnownHostsFile=/dev/null")
            .arg("-q")
            .arg(local_path)
            .arg(&scp_dest)
            .output()
            .map_err(|e| {
                TinyEtlError::Connection(format!("Failed to execute scp command: {}", e))
            })?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(TinyEtlError::Connection(format!(
                "SCP failed to upload file to {}: {}",
                scp_dest, stderr
            )));
        }

        info!("SSH upload completed");

        Ok(())
    }

    /// Extract file extension from remote path
    fn extract_extension_from_path(&self, path: &str) -> Option<String> {
        crate::protocols::extension_from_path(path)
    }
}

#[async_trait]
impl Protocol for SshProtocol {
    async fn create_source(
        &self,
        url: &Url,
        options: &HashMap<String, String>,
    ) -> Result<Box<dyn Source>> {
        // SSH protocol could use options for:
        // - identity_file: path to SSH key
        // - known_hosts_file: path to known_hosts
        // - ssh_options: additional SSH options
        // Download the file via SCP to a temporary location
        let temp_file = self.download_via_scp(url).await?;
        let temp_path = temp_file.path().to_string_lossy().to_string();

        // Create source using the temporary file path
        // Note: Similar limitation as HTTP - the temp file lifetime management
        // could be improved
        create_source_with_options(&temp_path, options)
    }

    async fn create_target(
        &self,
        _url: &Url,
        _options: &HashMap<String, String>,
    ) -> Result<Box<dyn Target>> {
        // For SSH targets, we'll create a local temporary file target
        // and then upload it after writing is complete
        // This is a simplified implementation - a full implementation would
        // need better integration with the Target trait lifecycle
        Err(TinyEtlError::Configuration(
            "SSH target implementation requires additional coordination with the ETL pipeline. Use file:// for local output and manually upload via SSH.".to_string()
        ))
    }

    fn validate_url(&self, url: &Url) -> Result<()> {
        if url.scheme() != "ssh" {
            return Err(TinyEtlError::Configuration(format!(
                "SSH protocol requires ssh:// scheme, got: {}",
                url.scheme()
            )));
        }

        if url.host().is_none() {
            return Err(TinyEtlError::Configuration(
                "SSH protocol requires a valid host".to_string(),
            ));
        }

        if url.username().is_empty() {
            return Err(TinyEtlError::Configuration(
                "SSH protocol requires a username in the URL (ssh://user@host/path)".to_string(),
            ));
        }

        let path = url.path();
        if path.is_empty() || path == "/" {
            return Err(TinyEtlError::Configuration(
                "SSH protocol requires a file path".to_string(),
            ));
        }

        Ok(())
    }

    fn name(&self) -> &'static str {
        "ssh"
    }
}

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

    #[test]
    fn test_validate_ssh_url() {
        let protocol = SshProtocol::new();

        // Valid SSH URLs
        let url = Url::parse("ssh://user@example.com/path/to/file.csv").unwrap();
        assert!(protocol.validate_url(&url).is_ok());

        let url = Url::parse("ssh://user@example.com:2222/data/file.json").unwrap();
        assert!(protocol.validate_url(&url).is_ok());

        // Invalid - missing username
        let url = Url::parse("ssh://example.com/path/to/file.csv").unwrap();
        assert!(protocol.validate_url(&url).is_err());

        // Invalid - no path
        let url = Url::parse("ssh://user@example.com/").unwrap();
        assert!(protocol.validate_url(&url).is_err());

        // Invalid scheme
        let url = Url::parse("http://example.com/file.csv").unwrap();
        assert!(protocol.validate_url(&url).is_err());
    }

    #[test]
    fn test_extract_extension_from_path() {
        let protocol = SshProtocol::new();

        // Paths with extensions
        assert_eq!(
            protocol.extract_extension_from_path("/path/to/data.csv"),
            Some("csv".to_string())
        );
        assert_eq!(
            protocol.extract_extension_from_path("/data/file.json"),
            Some("json".to_string())
        );
        assert_eq!(
            protocol.extract_extension_from_path("file.parquet"),
            Some("parquet".to_string())
        );

        // Paths without extensions
        assert_eq!(protocol.extract_extension_from_path("/path/to/data"), None);
        assert_eq!(protocol.extract_extension_from_path("/api/endpoint"), None);
    }

    #[test]
    fn test_target_not_fully_supported() {
        let protocol = SshProtocol::new();
        let url = Url::parse("ssh://user@example.com/upload/file.csv").unwrap();
        let options = std::collections::HashMap::new();

        // SSH target operations are not fully implemented yet
        tokio_test::block_on(async {
            let result = protocol.create_target(&url, &options).await;
            assert!(result.is_err());
        });
    }

    #[test]
    fn test_ssh_protocol_name() {
        let protocol = SshProtocol::new();
        assert_eq!(protocol.name(), "ssh");
    }

    #[test]
    fn test_url_validation_error_messages() {
        let protocol = SshProtocol::new();

        // Test wrong scheme
        let url = Url::parse("ftp://user@example.com/file.csv").unwrap();
        let result = protocol.validate_url(&url);
        assert!(result.is_err());
        if let Err(TinyEtlError::Configuration(msg)) = result {
            assert!(msg.contains("SSH protocol requires ssh:// scheme"));
            assert!(msg.contains("ftp"));
        } else {
            panic!("Expected Configuration error");
        }

        // Test missing username
        let url = Url::parse("ssh://example.com/file.csv").unwrap();
        let result = protocol.validate_url(&url);
        assert!(result.is_err());
        if let Err(TinyEtlError::Configuration(msg)) = result {
            assert!(msg.contains("requires a username"));
        } else {
            panic!("Expected Configuration error");
        }

        // Test missing path
        let url = Url::parse("ssh://user@example.com").unwrap();
        let result = protocol.validate_url(&url);
        assert!(result.is_err());
        if let Err(TinyEtlError::Configuration(msg)) = result {
            assert!(msg.contains("requires a file path"));
        } else {
            panic!("Expected Configuration error");
        }

        // Test empty path
        let url = Url::parse("ssh://user@example.com/").unwrap();
        let result = protocol.validate_url(&url);
        assert!(result.is_err());
        if let Err(TinyEtlError::Configuration(msg)) = result {
            assert!(msg.contains("requires a file path"));
        } else {
            panic!("Expected Configuration error");
        }
    }

    #[test]
    fn test_extract_extension_edge_cases() {
        let protocol = SshProtocol::new();

        // Test various edge cases for extension extraction
        assert_eq!(protocol.extract_extension_from_path(""), None);
        assert_eq!(protocol.extract_extension_from_path("/"), None);
        assert_eq!(
            protocol.extract_extension_from_path("/.hidden"),
            Some("hidden".to_string())
        ); // Current implementation treats this as extension
        assert_eq!(protocol.extract_extension_from_path("/path/to/."), None);
        assert_eq!(protocol.extract_extension_from_path("/path/to/.."), None);

        // Test file names with dots but no extension
        assert_eq!(protocol.extract_extension_from_path("/path/to/file."), None);
        assert_eq!(
            protocol.extract_extension_from_path("/path/file.toolongextension"),
            None
        );

        // Test various valid extensions
        assert_eq!(
            protocol.extract_extension_from_path("/data.CSV"),
            Some("csv".to_string())
        );
        assert_eq!(
            protocol.extract_extension_from_path("/file.JSON"),
            Some("json".to_string())
        );
        assert_eq!(
            protocol.extract_extension_from_path("/archive.tar.gz"),
            Some("tar.gz".to_string()) // compressed files keep their inner format
        );

        // Test nested paths
        assert_eq!(
            protocol.extract_extension_from_path("/home/user/documents/data.xlsx"),
            Some("xlsx".to_string())
        );
        assert_eq!(
            protocol.extract_extension_from_path("./relative/path/file.txt"),
            Some("txt".to_string())
        );

        // Test Windows-style paths (shouldn't matter for SSH but good to test)
        assert_eq!(
            protocol.extract_extension_from_path("C:\\data\\file.csv"),
            Some("csv".to_string())
        );
    }

    #[test]
    fn test_url_parsing_components() {
        // Test URL component extraction logic that would be used in SCP operations

        // Basic URL
        let url = Url::parse("ssh://user@example.com/path/to/file.csv").unwrap();
        assert_eq!(url.host_str(), Some("example.com"));
        assert_eq!(url.username(), "user");
        assert_eq!(url.port(), None); // Should default to 22
        assert_eq!(url.path(), "/path/to/file.csv");

        // URL with port
        let url = Url::parse("ssh://admin@server.local:2222/data/export.json").unwrap();
        assert_eq!(url.host_str(), Some("server.local"));
        assert_eq!(url.username(), "admin");
        assert_eq!(url.port(), Some(2222));
        assert_eq!(url.path(), "/data/export.json");

        // URL with special characters in username
        let url = Url::parse("ssh://user%2Bname@example.com/file.csv").unwrap();
        assert_eq!(url.username(), "user%2Bname"); // URL encoded

        // URL with IP address
        let url = Url::parse("ssh://root@192.168.1.100:22/tmp/data.parquet").unwrap();
        assert_eq!(url.host_str(), Some("192.168.1.100"));
        assert_eq!(url.port(), Some(22));
    }

    #[test]
    fn test_scp_command_building() {
        // Test the logic for building SCP commands (without actually executing them)
        let url = Url::parse("ssh://user@example.com:2222/path/to/file.csv").unwrap();

        let host = url.host_str().unwrap();
        let username = url.username();
        let port = url.port().unwrap_or(22);
        let remote_path = url.path();

        // Test source construction for download
        let scp_source = format!("{}@{}:{}", username, host, remote_path);
        assert_eq!(scp_source, "user@example.com:/path/to/file.csv");

        // Test port handling
        assert_eq!(port, 2222);

        // Test destination construction for upload
        let scp_dest = format!("{}@{}:{}", username, host, "/upload/target.csv");
        assert_eq!(scp_dest, "user@example.com:/upload/target.csv");
    }

    #[test]
    fn test_default_port_handling() {
        let url = Url::parse("ssh://user@example.com/file.csv").unwrap();
        let port = url.port().unwrap_or(22);
        assert_eq!(port, 22);

        let url_with_port = Url::parse("ssh://user@example.com:443/file.csv").unwrap();
        let port_explicit = url_with_port.port().unwrap_or(22);
        assert_eq!(port_explicit, 443);
    }

    #[test]
    fn test_path_validation_edge_cases() {
        let protocol = SshProtocol::new();

        // Test various path scenarios that should be invalid
        let invalid_paths = vec![
            "ssh://user@host",  // No path
            "ssh://user@host/", // Root path only
        ];

        for url_str in invalid_paths {
            let url = Url::parse(url_str).unwrap();
            assert!(
                protocol.validate_url(&url).is_err(),
                "Should reject URL: {}",
                url_str
            );
        }

        // Test valid paths
        let valid_paths = vec![
            "ssh://user@host/file",
            "ssh://user@host/path/to/file.csv",
            "ssh://user@host/data/export.json",
            "ssh://user@host/home/user/.bashrc",
        ];

        for url_str in valid_paths {
            let url = Url::parse(url_str).unwrap();
            assert!(
                protocol.validate_url(&url).is_ok(),
                "Should accept URL: {}",
                url_str
            );
        }
    }

    #[tokio::test]
    async fn test_create_source_url_validation() {
        let protocol = SshProtocol::new();
        let options = std::collections::HashMap::new();

        // Test that create_source validates the URL before attempting download
        let invalid_url = Url::parse("ssh://example.com/file.csv").unwrap(); // Missing username
        let result = protocol.create_source(&invalid_url, &options).await;

        // Should fail at validation stage, not at SCP execution
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_create_target_error_handling() {
        let protocol = SshProtocol::new();
        let options = std::collections::HashMap::new();

        let url = Url::parse("ssh://user@example.com/upload/file.csv").unwrap();
        let result = protocol.create_target(&url, &options).await;

        assert!(result.is_err());
        if let Err(TinyEtlError::Configuration(msg)) = result {
            assert!(msg.contains("SSH target implementation requires additional coordination"));
        } else {
            panic!("Expected specific Configuration error message");
        }
    }

    #[test]
    fn test_error_types_and_messages() {
        // Test that appropriate error types are returned for different failure modes
        let protocol = SshProtocol::new();

        // Configuration errors
        let url = Url::parse("http://example.com/file.csv").unwrap();
        match protocol.validate_url(&url) {
            Err(TinyEtlError::Configuration(_)) => {} // Expected
            _ => panic!("Expected Configuration error"),
        }

        // Test error message content
        let url = Url::parse("ssh://host/file.csv").unwrap(); // Missing username
        if let Err(TinyEtlError::Configuration(msg)) = protocol.validate_url(&url) {
            assert!(msg.contains("username"));
            assert!(msg.contains("ssh://user@host/path"));
        } else {
            panic!("Expected Configuration error with helpful message");
        }
    }

    #[test]
    fn test_temp_file_extension_preservation() {
        let protocol = SshProtocol::new();

        // Test that extensions are preserved for temporary files
        let paths_and_extensions = vec![
            ("/data/file.csv", Some("csv".to_string())),
            ("/exports/data.json", Some("json".to_string())),
            ("/files/archive.parquet", Some("parquet".to_string())),
            ("/backup/dump.sql", Some("sql".to_string())),
            ("/logs/access.log", Some("log".to_string())),
            ("/data/noextension", None),
        ];

        for (path, expected_ext) in paths_and_extensions {
            let actual_ext = protocol.extract_extension_from_path(path);
            assert_eq!(actual_ext, expected_ext, "Failed for path: {}", path);
        }
    }

    #[test]
    fn test_ssh_protocol_instantiation() {
        let protocol = SshProtocol::new();

        // Test that the protocol can be created and has expected properties
        assert_eq!(protocol.name(), "ssh");

        // Test that it implements the expected traits
        let _: &dyn Protocol = &protocol;
    }
}