Lutra/crates/deploy/tests/transfer.rs
danny 0644b044f2 D1: lutra-deploy crate: SFTP, FTP/FTPS and folder transports, keychain secrets, known hosts, path mappings
Pure Rust (D27): russh + russh-sftp with ring, suppaftp with rustls/ring,
keyring (Windows Credential Manager, macOS Keychain). Lutra's own
known-hosts file, refuse on mismatch. In-process SFTP and FTP test
servers behind the testserver feature.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 18:44:26 +01:00

354 lines
11 KiB
Rust

//! Uploads, downloads, host keys, sign-in failures and cancel against the
//! local SFTP and FTP test servers. No real network: both listen on
//! 127.0.0.1 only.
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::indexing_slicing
)]
use std::path::Path;
use std::time::Duration;
use lutra_deploy::known_hosts::{self, HostKey};
use lutra_deploy::testserver::{FtpServer, SftpServer, host_key};
use lutra_deploy::{
AuthKind, Cancel, DeployError, Job, Mapping, Monitor, Secret, Server, ServerKind, job,
};
const PASSWORD: &str = "Otter-Test-Pa55word!";
fn server(kind: ServerKind, port: u16) -> Server {
Server {
id: "test".into(),
name: "Test server".into(),
kind,
host: "127.0.0.1".into(),
port: Some(port),
user: "lutra".into(),
root: "/www".into(),
auth: AuthKind::Password,
key_path: None,
mappings: vec![Mapping {
local: String::new(),
remote: "site".into(),
}],
excluded: vec!["*.log".into()],
upload_on_save: false,
}
}
fn project() -> tempfile::TempDir {
let dir = tempfile::tempdir().unwrap();
std::fs::create_dir_all(dir.path().join("src/sub")).unwrap();
std::fs::write(dir.path().join("index.php"), "<?php echo 'hi';").unwrap();
std::fs::write(dir.path().join("src/a.php"), "<?php // a").unwrap();
std::fs::write(dir.path().join("src/sub/b.php"), "<?php // b").unwrap();
std::fs::write(dir.path().join("src/debug.log"), "noise").unwrap();
dir
}
fn job(server: Server, project: &Path, known: &Path, secret: Option<&str>) -> Job {
Job {
server,
project_root: project.to_path_buf(),
secret: secret.map(|s| Secret::new(s.into()).unwrap()),
known_hosts: known.to_path_buf(),
cancel: Cancel::new(),
monitor: Monitor::new(),
}
}
/// Collects everything a job left for the user to read.
fn transcript(job: &Job, errors: &[String]) -> String {
let mut text: Vec<String> = job
.monitor
.take_lines()
.into_iter()
.map(|l| l.message)
.collect();
text.extend(errors.iter().cloned());
text.join("\n")
}
#[test]
fn sftp_host_key_then_upload_and_download() {
let remote = tempfile::tempdir().unwrap();
std::fs::create_dir(remote.path().join("www")).unwrap();
let sftp = SftpServer::start(remote.path(), "lutra", Some(PASSWORD), None, 7).unwrap();
let project = project();
let state = tempfile::tempdir().unwrap();
let known = state.path().join("known_hosts");
let j = job(
server(ServerKind::Sftp, sftp.addr.port()),
project.path(),
&known,
Some(PASSWORD),
);
let err = job::test_connection(&j).unwrap_err();
let DeployError::HostKeyUnknown {
host,
algorithm,
fingerprint,
} = &err
else {
panic!("{err:?}");
};
assert_eq!(fingerprint, &sftp.fingerprint);
assert_eq!(algorithm, &sftp.algorithm);
assert!(err.to_string().contains("Test Connection"));
known_hosts::trust(
&known,
&HostKey {
host: host.clone(),
algorithm: algorithm.clone(),
fingerprint: fingerprint.clone(),
},
)
.unwrap();
let msg = job::test_connection(&j).unwrap();
assert!(msg.contains("/www exists"), "{msg}");
let summary = job::upload(&j, &[String::new()]).unwrap();
assert_eq!(
(summary.files, summary.failed, summary.skipped),
(3, 0, 1),
"{summary:?}"
);
let up = remote.path().join("www/site");
assert_eq!(
std::fs::read_to_string(up.join("src/sub/b.php")).unwrap(),
"<?php // b"
);
assert!(!up.join("src/debug.log").exists());
let progress = j.monitor.progress();
assert_eq!(progress.files_done, 3);
assert_eq!(progress.files_total, 3);
std::fs::write(up.join("src/a.php"), "<?php // from the server").unwrap();
std::fs::write(up.join("src/new.php"), "<?php // new").unwrap();
let summary = job::download(&j, &["src".into()]).unwrap();
assert_eq!(summary.files, 3);
assert_eq!(
std::fs::read_to_string(project.path().join("src/a.php")).unwrap(),
"<?php // from the server"
);
assert_eq!(
std::fs::read_to_string(project.path().join("src/new.php")).unwrap(),
"<?php // new"
);
let text = transcript(&j, &[err.to_string()]);
assert!(text.contains("src/sub/b.php"));
assert!(!text.contains(PASSWORD));
}
#[test]
fn sftp_refuses_a_changed_host_key() {
let remote = tempfile::tempdir().unwrap();
let sftp = SftpServer::start(remote.path(), "lutra", Some(PASSWORD), None, 2).unwrap();
let project = project();
let state = tempfile::tempdir().unwrap();
let known = state.path().join("known_hosts");
let s = server(ServerKind::Sftp, sftp.addr.port());
let old = host_key(1)
.public_key()
.fingerprint(russh::keys::HashAlg::Sha256)
.to_string();
known_hosts::trust(
&known,
&HostKey {
host: s.address(),
algorithm: "ssh-ed25519".into(),
fingerprint: old.clone(),
},
)
.unwrap();
let j = job(s, project.path(), &known, Some(PASSWORD));
let err = job::upload(&j, &["index.php".into()]).unwrap_err();
assert!(
matches!(&err, DeployError::HostKeyMismatch { known, .. } if *known == old),
"{err:?}"
);
assert!(err.to_string().contains("refuses"));
assert!(!remote.path().join("www").exists());
}
#[test]
fn sftp_sign_in_failures_never_show_the_password() {
let remote = tempfile::tempdir().unwrap();
let sftp = SftpServer::start(remote.path(), "lutra", Some(PASSWORD), None, 3).unwrap();
let project = project();
let state = tempfile::tempdir().unwrap();
let known = state.path().join("known_hosts");
let s = server(ServerKind::Sftp, sftp.addr.port());
known_hosts::trust(
&known,
&HostKey {
host: s.address(),
algorithm: sftp.algorithm.clone(),
fingerprint: sftp.fingerprint.clone(),
},
)
.unwrap();
let wrong = format!("{PASSWORD}-wrong");
let j = job(s.clone(), project.path(), &known, Some(&wrong));
let err = job::test_connection(&j).unwrap_err();
assert!(matches!(err, DeployError::Auth(_)), "{err:?}");
assert!(err.to_string().contains("password"));
let j2 = job(s, project.path(), &known, None);
let missing = job::upload(&j2, &["index.php".into()]).unwrap_err();
assert!(
matches!(missing, DeployError::MissingSecret { .. }),
"{missing:?}"
);
let text = transcript(
&j,
&[err.to_string(), missing.to_string(), format!("{err:?}")],
);
assert!(!text.contains(PASSWORD));
}
#[test]
fn sftp_key_file_sign_in() {
let remote = tempfile::tempdir().unwrap();
let key = host_key(42);
let sftp = SftpServer::start(
remote.path(),
"lutra",
None,
Some(key.public_key().clone()),
4,
)
.unwrap();
let project = project();
let state = tempfile::tempdir().unwrap();
let key_path = state.path().join("id_ed25519");
let pem = key
.to_openssh(russh::keys::ssh_key::LineEnding::LF)
.unwrap();
std::fs::write(&key_path, pem.as_bytes()).unwrap();
let known = state.path().join("known_hosts");
let mut s = server(ServerKind::Sftp, sftp.addr.port());
s.auth = AuthKind::Key;
s.key_path = Some(key_path.display().to_string());
s.root = String::new();
known_hosts::trust(
&known,
&HostKey {
host: s.address(),
algorithm: sftp.algorithm.clone(),
fingerprint: sftp.fingerprint.clone(),
},
)
.unwrap();
let j = job(s, project.path(), &known, None);
let summary = job::upload(&j, &["index.php".into()]).unwrap();
assert_eq!(summary.files, 1);
assert!(remote.path().join("site/index.php").is_file());
}
#[test]
fn sftp_cancel_stops_a_running_upload() {
let remote = tempfile::tempdir().unwrap();
let sftp = SftpServer::start(remote.path(), "lutra", Some(PASSWORD), None, 5).unwrap();
let project = project();
std::fs::write(project.path().join("big.bin"), vec![7u8; 24 * 1024 * 1024]).unwrap();
let state = tempfile::tempdir().unwrap();
let known = state.path().join("known_hosts");
let s = server(ServerKind::Sftp, sftp.addr.port());
known_hosts::trust(
&known,
&HostKey {
host: s.address(),
algorithm: sftp.algorithm.clone(),
fingerprint: sftp.fingerprint.clone(),
},
)
.unwrap();
let j = job(s, project.path(), &known, Some(PASSWORD));
let (cancel, monitor) = (j.cancel.clone(), j.monitor.clone());
let watcher = std::thread::spawn(move || {
for _ in 0..2000 {
if monitor.progress().bytes_done > 0 {
cancel.cancel();
return true;
}
std::thread::sleep(Duration::from_millis(5));
}
false
});
let started = std::time::Instant::now();
let result = job::upload(&j, &["big.bin".into()]);
assert!(watcher.join().unwrap());
assert_eq!(result, Err(DeployError::Cancelled));
assert!(started.elapsed() < Duration::from_secs(10));
}
#[test]
fn ftp_upload_download_and_wrong_password() {
let remote = tempfile::tempdir().unwrap();
let ftp = FtpServer::start(remote.path(), "lutra", PASSWORD).unwrap();
let project = project();
let state = tempfile::tempdir().unwrap();
let known = state.path().join("known_hosts");
let j = job(
server(ServerKind::Ftp, ftp.addr.port()),
project.path(),
&known,
Some(PASSWORD),
);
let msg = job::test_connection(&j).unwrap();
assert!(msg.contains("does not exist yet"), "{msg}");
let summary = job::upload(&j, &[String::new()]).unwrap();
assert_eq!((summary.files, summary.failed), (3, 0), "{summary:?}");
let up = remote.path().join("www/site");
assert_eq!(
std::fs::read_to_string(up.join("src/sub/b.php")).unwrap(),
"<?php // b"
);
assert!(job::test_connection(&j).unwrap().contains("exists"));
std::fs::write(up.join("src/a.php"), "<?php // ftp server").unwrap();
let summary = job::download(&j, &["src".into()]).unwrap();
assert_eq!(summary.files, 2, "{summary:?}");
assert_eq!(
std::fs::read_to_string(project.path().join("src/a.php")).unwrap(),
"<?php // ftp server"
);
let wrong = format!("{PASSWORD}x");
let bad = job(
server(ServerKind::Ftp, ftp.addr.port()),
project.path(),
&known,
Some(&wrong),
);
let err = job::upload(&bad, &["index.php".into()]).unwrap_err();
assert!(matches!(err, DeployError::Auth(_)), "{err:?}");
let text = transcript(&j, &[err.to_string()]) + &transcript(&bad, &[]);
assert!(!text.contains(PASSWORD));
}
#[test]
fn unreachable_server_explains() {
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let port = listener.local_addr().unwrap().port();
drop(listener);
let project = project();
let j = job(
server(ServerKind::Ftp, port),
project.path(),
&project.path().join("kh"),
Some(PASSWORD),
);
let err = job::test_connection(&j).unwrap_err();
assert!(matches!(err, DeployError::Connect { .. }), "{err:?}");
assert!(err.to_string().contains("Check the host name"));
}