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>
This commit is contained in:
danny 2026-11-02 19:09:17 +01:00
parent 33a7185d75
commit 0644b044f2
16 changed files with 5250 additions and 13 deletions

43
crates/deploy/Cargo.toml Normal file
View file

@ -0,0 +1,43 @@
[package]
name = "lutra-deploy"
description = "Deployment: upload and download over SFTP, FTP, FTPS or to a folder, with credentials in the OS keychain"
version.workspace = true
edition.workspace = true
rust-version.workspace = true
license.workspace = true
authors.workspace = true
publish.workspace = true
[package.metadata.lutra]
layer = 3
[dependencies]
keyring = { version = "3", default-features = false, features = ["windows-native", "apple-native"] }
russh = { version = "0.63", default-features = false, features = ["ring", "rsa", "flate2"] }
russh-sftp = "3"
rustls = { version = "0.23", default-features = false, features = ["ring", "std", "tls12"] }
rustls-native-certs = "0.8"
serde = { version = "1.0.229", features = ["derive"] }
serde_json = "1.0.154"
suppaftp = { version = "12", default-features = false, features = ["rustls-ring"] }
thiserror = "2.0.21"
tokio = { version = "1", features = ["rt", "net", "io-util", "sync", "time", "macros"] }
lutra-vfs.workspace = true
[lints]
workspace = true
[dev-dependencies]
proptest.workspace = true
tempfile.workspace = true
[features]
testserver = []
[[test]]
name = "transfer"
required-features = ["testserver"]
[[example]]
name = "testserver"
required-features = ["testserver"]

View file

@ -0,0 +1,26 @@
//! Runs the local SFTP and FTP test servers for a live check:
//! `cargo run -p lutra-deploy --features testserver --example testserver -- DIR USER PASSWORD`.
//! Both listen on 127.0.0.1 only and serve `DIR`. Prints their ports and
//! the SFTP host key, then runs until standard input closes.
use std::io::Read;
use std::path::PathBuf;
use lutra_deploy::testserver::{FtpServer, SftpServer};
fn main() -> std::io::Result<()> {
let args: Vec<String> = std::env::args().skip(1).collect();
let [dir, user, password] = args.as_slice() else {
eprintln!("usage: testserver DIR USER PASSWORD");
std::process::exit(2);
};
let dir = PathBuf::from(dir);
std::fs::create_dir_all(&dir)?;
let sftp = SftpServer::start(&dir, user, Some(password), None, 9)?;
let ftp = FtpServer::start(&dir, user, password)?;
println!("sftp {} {} {}", sftp.addr, sftp.algorithm, sftp.fingerprint);
println!("ftp {}", ftp.addr);
let mut sink = Vec::new();
std::io::stdin().read_to_end(&mut sink)?;
Ok(())
}

460
crates/deploy/src/config.rs Normal file
View file

@ -0,0 +1,460 @@
//! Server definitions of one project and their storage.
//!
//! A project's deployment configuration is a JSON file the engine keeps in
//! Lutra's config directory, one per project. It never holds a password or
//! a passphrase: those live only in the OS keychain ([`crate::secrets`]).
//! Unknown fields are refused, so a `password` field written by hand is an
//! error instead of being kept on disk unnoticed.
use std::path::Path;
use serde::{Deserialize, Serialize};
use crate::error::{DeployError, Result};
use crate::mapping::{normalize_relative, valid_pattern};
/// Largest configuration file read.
const MAX_FILE: u64 = 1024 * 1024;
/// Most servers per project.
pub const MAX_SERVERS: usize = 100;
/// Most path mappings or excluded paths per server.
pub const MAX_ENTRIES: usize = 200;
/// How Lutra reaches a server.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ServerKind {
/// SFTP over SSH.
Sftp,
/// Plain FTP. Everything, including the password, crosses the network
/// unencrypted.
Ftp,
/// FTP with explicit TLS (`AUTH TLS`), certificates checked against the
/// operating system's roots.
Ftps,
/// A local or mounted folder.
Folder,
}
impl ServerKind {
/// The port used when a server sets none.
pub fn default_port(self) -> u16 {
match self {
Self::Sftp => 22,
Self::Ftp | Self::Ftps => 21,
Self::Folder => 0,
}
}
/// The name used in messages and the log.
pub fn name(self) -> &'static str {
match self {
Self::Sftp => "sftp",
Self::Ftp => "ftp",
Self::Ftps => "ftps",
Self::Folder => "folder",
}
}
}
/// How Lutra signs in.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum AuthKind {
/// A password from the keychain (SFTP, FTP, FTPS).
#[default]
Password,
/// An SSH private key file, with an optional passphrase from the
/// keychain (SFTP).
Key,
/// The running SSH agent: Pageant or the OpenSSH agent on Windows,
/// `SSH_AUTH_SOCK` elsewhere (SFTP).
Agent,
}
/// One local folder uploaded to one remote folder.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Mapping {
/// Project-relative folder with `/`; empty for the project root.
pub local: String,
/// Remote folder: relative to the server's root, or absolute when it
/// starts with `/`. Empty for the root itself.
pub remote: String,
}
/// One deployment server.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct Server {
/// Stable id within the project: letters, digits, `-` and `_`.
pub id: String,
/// Shown name.
pub name: String,
/// Protocol.
pub kind: ServerKind,
/// Host name or address; empty for a folder.
#[serde(default)]
pub host: String,
/// Port; `None` for the protocol's default.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub port: Option<u16>,
/// User name; empty for a folder.
#[serde(default)]
pub user: String,
/// The deployment root: a remote folder (empty for the login folder)
/// or, for a folder server, an absolute local path.
#[serde(default)]
pub root: String,
/// How to sign in.
#[serde(default)]
pub auth: AuthKind,
/// The private key file for [`AuthKind::Key`].
#[serde(default, skip_serializing_if = "Option::is_none")]
pub key_path: Option<String>,
/// Path mappings; none means the project root maps to the root.
#[serde(default)]
pub mappings: Vec<Mapping>,
/// Paths never uploaded or downloaded: project-relative paths (a
/// folder excludes everything inside), or file name patterns with `*`
/// and `?` such as `*.log`.
#[serde(default)]
pub excluded: Vec<String>,
/// Upload a file after every save. Off by default.
#[serde(default)]
pub upload_on_save: bool,
}
impl Server {
/// The port to connect to.
pub fn effective_port(&self) -> u16 {
self.port.unwrap_or_else(|| self.kind.default_port())
}
/// `host:port` for messages and the known-hosts file.
pub fn address(&self) -> String {
format!("{}:{}", self.host, self.effective_port())
}
/// Whether signing in needs a secret from the keychain, and which:
/// `password` or `passphrase`. A key may have no passphrase, so the
/// key's is `Some` but optional.
pub fn secret_kind(&self) -> Option<&'static str> {
match (self.kind, self.auth) {
(ServerKind::Folder, _) | (ServerKind::Sftp, AuthKind::Agent) => None,
(ServerKind::Sftp, AuthKind::Key) => Some("passphrase"),
_ => Some("password"),
}
}
/// Checks every field and normalizes the mappings.
///
/// # Errors
/// [`DeployError::Invalid`] naming the first bad field.
pub fn validate(&mut self) -> Result<()> {
let bad = |m: String| Err(DeployError::Invalid(m));
if self.id.is_empty()
|| self.id.len() > 64
|| !self
.id
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
{
return bad(format!(
"server id {:?} must be 1 to 64 letters, digits, - or _",
self.id
));
}
self.name = self.name.trim().to_string();
if self.name.is_empty() || self.name.chars().count() > 100 || has_control(&self.name) {
return bad("the server needs a name of at most 100 characters".into());
}
self.host = self.host.trim().to_string();
self.user = self.user.trim().to_string();
if self.kind == ServerKind::Folder {
if !Path::new(&self.root).is_absolute() {
return bad("a folder server needs an absolute local folder as its root".into());
}
} else {
if self.host.is_empty()
|| self.host.len() > 255
|| self.host.starts_with('-')
|| self
.host
.chars()
.any(|c| c.is_whitespace() || c.is_control())
{
return bad(format!("{:?} is not a valid host name", self.host));
}
if self.port == Some(0) {
return bad("the port must be between 1 and 65535".into());
}
if self.user.is_empty() || self.user.len() > 255 || has_control(&self.user) {
return bad("the server needs a user name".into());
}
if self.kind != ServerKind::Sftp && self.auth != AuthKind::Password {
return bad("FTP and FTPS servers sign in with a password".into());
}
if has_control(&self.root) || self.root.len() > 4096 {
return bad("the root folder contains control characters".into());
}
}
if self.auth == AuthKind::Key {
let key = self.key_path.as_deref().unwrap_or_default().trim();
if !Path::new(key).is_absolute() {
return bad("key sign-in needs the absolute path of a private key file".into());
}
self.key_path = Some(key.to_string());
} else {
self.key_path = None;
}
if self.mappings.len() > MAX_ENTRIES || self.excluded.len() > MAX_ENTRIES {
return bad(format!(
"at most {MAX_ENTRIES} mappings and {MAX_ENTRIES} excluded paths per server"
));
}
for mapping in &mut self.mappings {
mapping.local = normalize_relative(&mapping.local).ok_or_else(|| {
DeployError::Invalid(format!(
"the local path {:?} of a mapping must stay inside the project",
mapping.local
))
})?;
let absolute = mapping.remote.starts_with('/');
let rest = normalize_relative(&mapping.remote).ok_or_else(|| {
DeployError::Invalid(format!(
"the remote path {:?} of a mapping may not contain .. or control characters",
mapping.remote
))
})?;
mapping.remote = if absolute { format!("/{rest}") } else { rest };
}
for pattern in &mut self.excluded {
*pattern = pattern.trim().trim_matches('/').to_string();
if !valid_pattern(pattern) {
return bad(format!("{pattern:?} is not a valid excluded path"));
}
}
Ok(())
}
}
/// Whether `text` holds a control character.
fn has_control(text: &str) -> bool {
text.chars().any(char::is_control)
}
/// The deployment configuration of one project.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct Deployment {
/// The servers, in the user's order.
#[serde(default)]
pub servers: Vec<Server>,
/// The id of the default server.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub default_server: Option<String>,
}
impl Deployment {
/// The server with `id`.
pub fn server(&self, id: &str) -> Option<&Server> {
self.servers.iter().find(|s| s.id == id)
}
/// The default server: the chosen one, else the only one.
pub fn default_target(&self) -> Option<&Server> {
match &self.default_server {
Some(id) => self.server(id),
None if self.servers.len() == 1 => self.servers.first(),
None => None,
}
}
/// Adds `server`, or replaces the one with the same id, after
/// validating it.
///
/// # Errors
/// [`DeployError::Invalid`] for a bad server or too many servers.
pub fn upsert(&mut self, mut server: Server) -> Result<()> {
server.validate()?;
if let Some(slot) = self.servers.iter_mut().find(|s| s.id == server.id) {
*slot = server;
return Ok(());
}
if self.servers.len() >= MAX_SERVERS {
return Err(DeployError::Invalid(format!(
"at most {MAX_SERVERS} servers per project"
)));
}
self.servers.push(server);
Ok(())
}
/// Removes the server with `id`; clears the default if it was that one.
/// Returns whether it existed.
pub fn remove(&mut self, id: &str) -> bool {
let before = self.servers.len();
self.servers.retain(|s| s.id != id);
if self.default_server.as_deref() == Some(id) {
self.default_server = None;
}
self.servers.len() != before
}
/// A fresh id: `s1`, `s2`… whichever is free.
pub fn fresh_id(&self) -> String {
(1..)
.map(|n: u32| format!("s{n}"))
.find(|id| self.server(id).is_none())
.unwrap_or_else(|| "s0".into())
}
/// Reads the configuration at `path`; a missing file is an empty one.
///
/// # Errors
/// [`DeployError::Transfer`] when the file cannot be read or is not a
/// valid configuration; the file is left as it is.
pub fn load(path: &Path) -> Result<Self> {
let meta = match std::fs::metadata(path) {
Ok(meta) => meta,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Self::default()),
Err(e) => return Err(io_error(path, &e)),
};
if meta.len() > MAX_FILE {
return Err(DeployError::Transfer(format!(
"{} is larger than 1 MiB",
path.display()
)));
}
let bytes = std::fs::read(path).map_err(|e| io_error(path, &e))?;
let mut deployment: Self = serde_json::from_slice(&bytes).map_err(|e| {
DeployError::Transfer(format!(
"{} is not a valid deployment configuration: {e}",
path.display()
))
})?;
for server in &mut deployment.servers {
server.validate()?;
}
Ok(deployment)
}
/// Writes the configuration to `path` atomically, creating its folder.
///
/// # Errors
/// [`DeployError::Transfer`] when it cannot be written.
pub fn save(&self, path: &Path) -> Result<()> {
if let Some(dir) = path.parent() {
std::fs::create_dir_all(dir).map_err(|e| io_error(dir, &e))?;
}
let text = serde_json::to_vec_pretty(self)
.map_err(|e| DeployError::Transfer(format!("cannot encode the configuration: {e}")))?;
lutra_vfs::write_atomic(path, &text).map_err(|e| DeployError::Transfer(e.to_string()))
}
}
/// A local file error as a [`DeployError::Transfer`].
pub(crate) fn io_error(path: &Path, error: &std::io::Error) -> DeployError {
DeployError::Transfer(format!("{}: {error}", path.display()))
}
#[cfg(test)]
mod tests {
use super::*;
fn sftp() -> Server {
Server {
id: "prod".into(),
name: " Production ".into(),
kind: ServerKind::Sftp,
host: "example.test".into(),
port: None,
user: "deploy".into(),
root: "/var/www".into(),
auth: AuthKind::Password,
key_path: Some("ignored".into()),
mappings: vec![Mapping {
local: "./public/".into(),
remote: "/srv//html/".into(),
}],
excluded: vec!["/.env".into(), "*.log".into()],
upload_on_save: false,
}
}
#[test]
fn validation_normalizes() {
let mut s = sftp();
s.validate().unwrap();
assert_eq!(s.name, "Production");
assert_eq!(s.key_path, None);
assert_eq!(s.mappings[0].local, "public");
assert_eq!(s.mappings[0].remote, "/srv/html");
assert_eq!(s.excluded, vec![".env", "*.log"]);
assert_eq!(s.address(), "example.test:22");
assert_eq!(s.secret_kind(), Some("password"));
}
/// One change that makes a valid server invalid.
type Change = Box<dyn Fn(&mut Server)>;
#[test]
fn validation_refuses_bad_fields() {
let cases: Vec<Change> = vec![
Box::new(|s| s.id = "a b".into()),
Box::new(|s| s.name = " ".into()),
Box::new(|s| s.host = "-oProxyCommand=x".into()),
Box::new(|s| s.host = "a b".into()),
Box::new(|s| s.port = Some(0)),
Box::new(|s| s.user = String::new()),
Box::new(|s| s.auth = AuthKind::Key),
Box::new(|s| s.mappings[0].local = "../up".into()),
Box::new(|s| s.mappings[0].remote = "a/../../b".into()),
Box::new(|s| {
s.kind = ServerKind::Ftp;
s.auth = AuthKind::Agent;
}),
Box::new(|s| {
s.kind = ServerKind::Folder;
s.root = "relative".into();
}),
];
for (i, change) in cases.iter().enumerate() {
let mut s = sftp();
change(&mut s);
assert!(s.validate().is_err(), "case {i} passed");
}
}
#[test]
fn a_password_field_is_refused() {
let text = r#"{"servers":[{"id":"a","name":"A","kind":"ftp","host":"h","user":"u","password":"x"}]}"#;
assert!(serde_json::from_str::<Deployment>(text).is_err());
}
#[test]
fn upsert_remove_default_and_round_trip() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("sub").join("d.json");
let mut d = Deployment::load(&path).unwrap();
assert!(d.servers.is_empty());
d.upsert(sftp()).unwrap();
assert_eq!(d.default_target().unwrap().id, "prod");
let mut other = sftp();
other.id = d.fresh_id();
assert_eq!(other.id, "s1");
d.upsert(other).unwrap();
assert!(d.default_target().is_none());
d.default_server = Some("s1".into());
d.save(&path).unwrap();
let back = Deployment::load(&path).unwrap();
assert_eq!(back, d);
d.remove("s1");
assert_eq!(d.default_server, None);
std::fs::write(&path, "nope").unwrap();
assert!(Deployment::load(&path).is_err());
}
}

122
crates/deploy/src/error.rs Normal file
View file

@ -0,0 +1,122 @@
//! The crate's error type. Every message says what happened and, where the
//! cause is known, what to do. No message ever contains a password or a
//! passphrase: errors from the protocol libraries pass through
//! [`DeployError::redacted`] with the secret that was in use.
use thiserror::Error;
/// What can go wrong while configuring, connecting or transferring.
#[derive(Debug, Error, Clone, PartialEq, Eq)]
pub enum DeployError {
/// A server definition or a parameter is invalid; the caller's mistake.
#[error("{0}")]
Invalid(String),
/// The server's host key is not in Lutra's known-hosts file yet.
#[error(
"the host key of {host} is not known yet ({algorithm} {fingerprint}); check the fingerprint with the server's administrator, then trust it with Test Connection"
)]
HostKeyUnknown {
/// `host:port`.
host: String,
/// The key's algorithm, e.g. `ssh-ed25519`.
algorithm: String,
/// `SHA256:` and the base64 digest.
fingerprint: String,
},
/// The server's host key differs from the trusted one.
#[error(
"the host key of {host} changed: it is now {algorithm} {fingerprint}, but {known} is trusted. Lutra refuses to connect, because someone may be intercepting the connection. If the server was reinstalled, remove the old key in the server's settings and test the connection again"
)]
HostKeyMismatch {
/// `host:port`.
host: String,
/// The offered key's algorithm.
algorithm: String,
/// The offered key's fingerprint.
fingerprint: String,
/// The trusted fingerprint.
known: String,
},
/// The password or passphrase the server needs is not in the keychain.
#[error(
"no {kind} for {server} is stored in the system keychain; enter it in the server's settings"
)]
MissingSecret {
/// The server's name.
server: String,
/// `password` or `passphrase`.
kind: String,
},
/// The OS keychain refused or is not available.
#[error("the system keychain is not available: {0}")]
Keychain(String),
/// The server is unreachable or the connection broke.
#[error("cannot reach {host}: {message}. Check the host name, the port and your network")]
Connect {
/// `host:port`.
host: String,
/// The cause.
message: String,
},
/// The server refused the sign-in.
#[error("{0}")]
Auth(String),
/// The server refused an operation, or a local file failed.
#[error("{0}")]
Transfer(String),
/// The job was cancelled.
#[error("cancelled")]
Cancelled,
}
impl DeployError {
/// Whether the caller's input was wrong, as opposed to the server, the
/// network or the disk.
pub fn is_invalid_input(&self) -> bool {
matches!(self, Self::Invalid(_))
}
/// The same error with every occurrence of `secret` in its text
/// replaced by `***`. Applied to every error that a protocol library
/// produced while `secret` was in use.
#[must_use]
pub fn redacted(self, secret: Option<&str>) -> Self {
let Some(secret) = secret.filter(|s| !s.is_empty()) else {
return self;
};
let clean = |text: String| text.replace(secret, "***");
match self {
Self::Invalid(m) => Self::Invalid(clean(m)),
Self::Connect { host, message } => Self::Connect {
host,
message: clean(message),
},
Self::Auth(m) => Self::Auth(clean(m)),
Self::Transfer(m) => Self::Transfer(clean(m)),
Self::Keychain(m) => Self::Keychain(clean(m)),
other => other,
}
}
}
/// The crate's result type.
pub type Result<T> = std::result::Result<T, DeployError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn redaction_removes_the_secret() {
let e = DeployError::Auth("server said: bad pass s3cr3t!".into()).redacted(Some("s3cr3t!"));
assert_eq!(e.to_string(), "server said: bad pass ***");
let e = DeployError::Transfer("x".into()).redacted(Some(""));
assert_eq!(e.to_string(), "x");
}
#[test]
fn invalid_is_the_callers() {
assert!(DeployError::Invalid("x".into()).is_invalid_input());
assert!(!DeployError::Cancelled.is_invalid_input());
}
}

248
crates/deploy/src/ftp.rs Normal file
View file

@ -0,0 +1,248 @@
//! FTP and FTPS with `suppaftp`, blocking, TLS through `rustls` with
//! `ring`.
//!
//! FTPS is explicit TLS (`AUTH TLS` on the control port); the server's
//! certificate is checked against the operating system's roots. Transfers
//! are binary and passive, with the NAT workaround on (a server behind NAT
//! often announces its private address). Plain FTP sends everything,
//! including the password, unencrypted; the documentation says so.
use std::io::{Read, Write};
use std::net::ToSocketAddrs;
use std::sync::Arc;
use std::time::Duration;
use suppaftp::types::FileType;
use suppaftp::{FtpError, ImplFtpStream, RustlsConnector, RustlsFtpStream, TlsStream};
use crate::config::{Server, ServerKind};
use crate::error::{DeployError, Result};
use crate::remote::{Cancel, Entry, MAX_LISTING, Remote, Tick, copy_chunks, safe_name};
use crate::secrets::Secret;
/// Longest wait for the TCP connection.
const CONNECT_TIMEOUT: Duration = Duration::from_secs(20);
/// Longest wait for any read or write on the control connection.
const IO_TIMEOUT: Duration = Duration::from_secs(60);
/// An open FTP or FTPS session.
pub struct Ftp {
/// The control connection; TLS-wrapped for FTPS, plain for FTP (the
/// same stream type, never upgraded).
stream: RustlsFtpStream,
/// The folder the server put us in after sign-in.
home: String,
/// The job's cancel flag.
cancel: Cancel,
}
/// The TLS settings for FTPS: the OS roots, TLS 1.2 and 1.3, `ring`.
///
/// # Errors
/// [`DeployError::Connect`] when no root certificate could be loaded.
fn tls_config(host: &str) -> Result<rustls::ClientConfig> {
let mut roots = rustls::RootCertStore::empty();
let found = rustls_native_certs::load_native_certs();
let (added, _) = roots.add_parsable_certificates(found.certs);
if added == 0 {
return Err(DeployError::Connect {
host: host.to_string(),
message: "no trusted root certificates were found on this system for FTPS".into(),
});
}
rustls::ClientConfig::builder_with_provider(Arc::new(rustls::crypto::ring::default_provider()))
.with_safe_default_protocol_versions()
.map(|b| b.with_root_certificates(roots).with_no_client_auth())
.map_err(|e| DeployError::Connect {
host: host.to_string(),
message: format!("TLS setup failed: {e}"),
})
}
/// An FTP library error as ours.
fn ftp_error(what: &str, e: &FtpError) -> DeployError {
DeployError::Transfer(format!("{what}: {e}"))
}
/// Signs in on a connected stream and switches to binary mode; returns the
/// stream and the login folder.
fn sign_in<T: TlsStream>(
mut stream: ImplFtpStream<T>,
server: &Server,
secret: &Secret,
) -> Result<(ImplFtpStream<T>, String)> {
stream.set_passive_nat_workaround(true);
stream
.login(server.user.as_str(), secret.expose())
.map_err(|e| match e {
FtpError::UnexpectedResponse(_) => DeployError::Auth(format!(
"{} refused the sign-in; check the user name and password",
server.address()
)),
other => DeployError::Connect {
host: server.address(),
message: other.to_string(),
},
})?;
stream
.transfer_type(FileType::Binary)
.map_err(|e| ftp_error("the server refused binary mode", &e))?;
let home = stream.pwd().unwrap_or_else(|_| "/".into());
Ok((stream, home))
}
impl Ftp {
/// Connects to `server`, upgrades to TLS for FTPS, signs in with
/// `secret` and switches to binary mode.
///
/// # Errors
/// [`DeployError::MissingSecret`] without a password,
/// [`DeployError::Connect`] when the server or its TLS fails,
/// [`DeployError::Auth`] when it refuses the sign-in.
///
/// Socket timeouts that cannot be set are ignored: the connection
/// still works, only without the 60 s limit.
pub fn connect(server: &Server, secret: Option<&Secret>, cancel: &Cancel) -> Result<Self> {
let Some(secret) = secret else {
return Err(DeployError::MissingSecret {
server: server.name.clone(),
kind: "password".into(),
});
};
let host = server.address();
let connect_error = |message: String| DeployError::Connect {
host: host.clone(),
message,
};
let addr = (server.host.as_str(), server.effective_port())
.to_socket_addrs()
.map_err(|e| connect_error(e.to_string()))?
.next()
.ok_or_else(|| connect_error("the host name has no address".into()))?;
cancel.check()?;
let set_timeouts = |tcp: &std::net::TcpStream| {
let _ = tcp.set_read_timeout(Some(IO_TIMEOUT));
let _ = tcp.set_write_timeout(Some(IO_TIMEOUT));
};
let tcp = std::net::TcpStream::connect_timeout(&addr, CONNECT_TIMEOUT)
.map_err(|e| connect_error(e.to_string()))?;
set_timeouts(&tcp);
let stream =
RustlsFtpStream::connect_with_stream(tcp).map_err(|e| connect_error(e.to_string()))?;
let stream = if server.kind == ServerKind::Ftps {
let config = tls_config(&host)?;
stream
.into_secure(RustlsConnector::from(Arc::new(config)), &server.host)
.map_err(|e| connect_error(format!("TLS failed: {e}")))?
} else {
stream
};
cancel.check()?;
let (stream, home) = sign_in(stream, server, secret)?;
Ok(Self {
stream,
home,
cancel: cancel.clone(),
})
}
/// The control stream.
fn ftp(&mut self) -> &mut RustlsFtpStream {
&mut self.stream
}
}
impl Remote for Ftp {
/// Creates each missing folder. A refused `MKD` is ignored when the
/// folder exists already, which is the usual reason.
fn mkdir_all(&mut self, dir: &str) -> Result<()> {
let absolute = dir.starts_with('/');
let mut path = String::new();
for part in dir.split('/').filter(|p| !p.is_empty()) {
self.cancel.check()?;
if absolute || !path.is_empty() {
path.push('/');
}
path.push_str(part);
if self.kind(&path)? == Some(true) {
continue;
}
self.ftp()
.mkdir(&path)
.map_err(|e| ftp_error(&format!("cannot create the folder {path}"), &e))?;
}
Ok(())
}
fn put(&mut self, path: &str, data: &mut dyn Read, tick: Tick<'_>) -> Result<()> {
let mut transfer = self
.ftp()
.put_with_stream(path)
.map_err(|e| ftp_error(&format!("cannot write {path}"), &e))?;
copy_chunks(data, &mut transfer, tick)?;
transfer
.finish()
.map_err(|e| ftp_error(&format!("cannot finish {path}"), &e))
}
fn get(&mut self, path: &str, out: &mut dyn Write, tick: Tick<'_>) -> Result<()> {
let mut transfer = self
.ftp()
.retr_as_stream(path)
.map_err(|e| ftp_error(&format!("cannot read {path}"), &e))?;
copy_chunks(&mut transfer, out, tick)?;
transfer
.finish()
.map_err(|e| ftp_error(&format!("cannot finish {path}"), &e))
}
/// A folder is a path the server lets us change into (then we change
/// back to the login folder); a file is one with a size.
fn kind(&mut self, path: &str) -> Result<Option<bool>> {
let target = if path.is_empty() {
self.home.clone()
} else {
path.to_string()
};
if self.ftp().cwd(&target).is_ok() {
let home = self.home.clone();
self.ftp()
.cwd(&home)
.map_err(|e| ftp_error("cannot return to the login folder", &e))?;
return Ok(Some(true));
}
Ok(self.ftp().size(&target).ok().map(|_| false))
}
fn list(&mut self, path: &str) -> Result<Vec<Entry>> {
let target = if path.is_empty() {
self.home.clone()
} else {
path.to_string()
};
let lines = self
.ftp()
.list(Some(&target))
.map_err(|e| ftp_error(&format!("cannot list {path}"), &e))?;
let mut entries: Vec<Entry> = lines
.iter()
.filter_map(|line| line.parse::<suppaftp::list::File>().ok())
.filter(|f| safe_name(f.name()))
.take(MAX_LISTING)
.map(|f| Entry {
name: f.name().to_string(),
is_dir: f.is_directory(),
size: (!f.is_directory()).then(|| f.size() as u64),
})
.collect();
entries.sort_by(|a, b| a.name.cmp(&b.name));
Ok(entries)
}
/// Sends `QUIT`; a failure is ignored because the connection is
/// dropped right after.
fn close(&mut self) {
let _ = self.ftp().quit();
}
}

627
crates/deploy/src/job.rs Normal file
View file

@ -0,0 +1,627 @@
//! Jobs: test a connection, upload or download project paths.
//!
//! A job first plans (walks the local folders or lists the remote ones and
//! maps every file), then connects once and transfers file by file. Its
//! [`Monitor`] holds the progress and the new log lines; the engine reads
//! it from another thread. A failing file is logged and the job goes on; a
//! lost connection, a refused sign-in or a cancel ends it.
//!
//! Folder walks skip symbolic links to folders and the version-control
//! folders `.git`, `.svn` and `.hg`, which are never deployed.
use std::collections::BTreeSet;
use std::fs::File;
use std::path::Path;
use std::sync::{Arc, Mutex, PoisonError};
use serde::Serialize;
use crate::config::{Server, ServerKind, io_error};
use crate::error::{DeployError, Result};
use crate::mapping::{is_excluded, normalize_relative, remote_path};
use crate::remote::{Cancel, Folder, Remote};
use crate::secrets::Secret;
/// Most files one job plans.
pub const MAX_FILES: usize = 200_000;
/// Deepest folder nesting a job follows.
const MAX_DEPTH: usize = 64;
/// Folders never deployed.
const VCS_DIRS: [&str; 3] = [".git", ".svn", ".hg"];
/// Severity of a log line.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Level {
/// Something worked.
Info,
/// Something was skipped.
Warning,
/// Something failed.
Error,
}
/// One line of the deployment log.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct LogLine {
/// Severity.
pub level: Level,
/// The text; never contains a secret.
pub message: String,
}
/// Progress of a running job.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Progress {
/// `connecting`, `planning`, `transferring`.
pub phase: String,
/// The file being transferred, project-relative.
#[serde(skip_serializing_if = "Option::is_none")]
pub file: Option<String>,
/// Files finished, successful or not.
pub files_done: usize,
/// Files planned.
pub files_total: usize,
/// Bytes transferred.
pub bytes_done: u64,
/// Bytes planned (uploads only; 0 when unknown).
pub bytes_total: u64,
}
/// What a job shares with its observer.
#[derive(Debug, Default)]
struct Shared {
/// The progress.
progress: Progress,
/// Log lines not taken yet.
lines: Vec<LogLine>,
}
/// A job's progress and log, readable from another thread. Clones share
/// the state.
#[derive(Debug, Clone, Default)]
pub struct Monitor(Arc<Mutex<Shared>>);
impl Monitor {
/// A fresh monitor.
pub fn new() -> Self {
Self::default()
}
/// The current progress.
pub fn progress(&self) -> Progress {
self.0
.lock()
.unwrap_or_else(PoisonError::into_inner)
.progress
.clone()
}
/// Takes the log lines written since the last call.
pub fn take_lines(&self) -> Vec<LogLine> {
std::mem::take(&mut self.0.lock().unwrap_or_else(PoisonError::into_inner).lines)
}
/// Changes the progress.
fn update(&self, change: impl FnOnce(&mut Progress)) {
change(
&mut self
.0
.lock()
.unwrap_or_else(PoisonError::into_inner)
.progress,
);
}
/// Adds a log line.
fn log(&self, level: Level, message: String) {
self.0
.lock()
.unwrap_or_else(PoisonError::into_inner)
.lines
.push(LogLine { level, message });
}
}
/// Everything a job needs.
#[derive(Debug, Clone)]
pub struct Job {
/// The server.
pub server: Server,
/// The project root on disk.
pub project_root: std::path::PathBuf,
/// The password or passphrase from the keychain, when the server
/// needs one and it is stored.
pub secret: Option<Secret>,
/// Lutra's known-hosts file.
pub known_hosts: std::path::PathBuf,
/// Stops the job.
pub cancel: Cancel,
/// Progress and log.
pub monitor: Monitor,
}
/// What a finished upload or download did.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Summary {
/// Files transferred.
pub files: usize,
/// Bytes transferred.
pub bytes: u64,
/// Files skipped: excluded, not mapped, or missing on the server.
pub skipped: usize,
/// Files that failed.
pub failed: usize,
}
/// One planned file.
#[derive(Debug, Clone)]
struct Planned {
/// Project-relative path.
local: String,
/// Remote path.
remote: String,
/// Size, when known.
size: u64,
}
/// Connects to the job's server.
///
/// # Errors
/// What the transport's `connect` returns, with the secret redacted.
pub fn connect(job: &Job) -> Result<Box<dyn Remote>> {
job.monitor.update(|p| p.phase = "connecting".into());
let secret = job.secret.as_ref();
let remote: Result<Box<dyn Remote>> = match job.server.kind {
ServerKind::Sftp => {
crate::sftp::Sftp::connect(&job.server, secret, &job.known_hosts, &job.cancel)
.map(|s| Box::new(s) as Box<dyn Remote>)
}
ServerKind::Ftp | ServerKind::Ftps => {
crate::ftp::Ftp::connect(&job.server, secret, &job.cancel)
.map(|s| Box::new(s) as Box<dyn Remote>)
}
ServerKind::Folder => {
Folder::open(Path::new(&job.server.root)).map(|s| Box::new(s) as Box<dyn Remote>)
}
};
remote.map_err(|e| e.redacted(secret.map(Secret::expose)))
}
/// Connects, signs in and checks the root folder. Returns a sentence for
/// the user.
///
/// # Errors
/// What [`connect`] returns.
pub fn test_connection(job: &Job) -> Result<String> {
let mut remote = connect(job)?;
let root = if job.server.kind == ServerKind::Folder {
String::new()
} else {
job.server.root.clone()
};
let kind = remote
.kind(&root)
.map_err(|e| e.redacted(job.secret.as_ref().map(Secret::expose)));
remote.close();
let kind = kind?;
let name = &job.server.name;
let shown = display_root(&root);
let message = match kind {
_ if job.server.kind == ServerKind::Folder => {
format!("Connected to the folder {}.", job.server.root)
}
Some(true) => format!("Connected to {name}; the root folder {shown} exists."),
Some(false) => {
format!("Connected to {name}, but the root {shown} is a file, not a folder.")
}
None => format!(
"Connected to {name}; the root folder {shown} does not exist yet and is created by the first upload."
),
};
job.monitor.log(Level::Info, message.clone());
Ok(message)
}
/// The root as shown to the user.
fn display_root(root: &str) -> &str {
if root.is_empty() {
"(login folder)"
} else {
root
}
}
/// Adds the files under the project-relative `rel` to `plan`.
fn walk_local(
job: &Job,
rel: &str,
depth: usize,
plan: &mut Vec<Planned>,
skipped: &mut usize,
) -> Result<()> {
job.cancel.check()?;
let path = if rel.is_empty() {
job.project_root.clone()
} else {
job.project_root.join(rel)
};
let meta = std::fs::symlink_metadata(&path).map_err(|e| io_error(&path, &e))?;
let is_dir = meta.is_dir() || (meta.file_type().is_symlink() && depth == 0 && path.is_dir());
if !is_dir {
let size = std::fs::metadata(&path)
.map_err(|e| io_error(&path, &e))?
.len();
match remote_path(&job.server, rel) {
Some(remote) if !rel.is_empty() => plan.push(Planned {
local: rel.to_string(),
remote,
size,
}),
_ => {
*skipped += 1;
job.monitor.log(
Level::Warning,
format!("{rel}: skipped (excluded or not mapped)"),
);
}
}
if plan.len() > MAX_FILES {
return Err(DeployError::Invalid(format!(
"more than {MAX_FILES} files; upload a smaller folder"
)));
}
return Ok(());
}
if depth > MAX_DEPTH || (!rel.is_empty() && is_excluded(&job.server, rel)) {
return Ok(());
}
let mut names: Vec<String> = std::fs::read_dir(&path)
.map_err(|e| io_error(&path, &e))?
.filter_map(|e| e.ok())
.filter(|e| {
e.file_type()
.map(|t| !(t.is_symlink() && e.path().is_dir()))
.unwrap_or(false)
})
.map(|e| e.file_name().to_string_lossy().into_owned())
.filter(|n| !VCS_DIRS.contains(&n.as_str()))
.collect();
names.sort();
for name in names {
let child = if rel.is_empty() {
name
} else {
format!("{rel}/{name}")
};
walk_local(job, &child, depth + 1, plan, skipped)?;
}
Ok(())
}
/// The parent folder of a remote path; empty for a top-level one.
fn parent(remote: &str) -> &str {
match remote.rfind('/') {
Some(0) => "/",
Some(i) => remote.get(..i).unwrap_or_default(),
None => "",
}
}
/// Whether an error ends the whole job instead of one file.
fn is_fatal(e: &DeployError) -> bool {
!matches!(e, DeployError::Transfer(_))
}
/// Uploads the project-relative `paths` (files or folders).
///
/// # Errors
/// [`DeployError::Invalid`] for a path outside the project, otherwise
/// what [`connect`] returns, or [`DeployError::Cancelled`]. Failing files
/// are counted in [`Summary::failed`] instead.
pub fn upload(job: &Job, paths: &[String]) -> Result<Summary> {
let secret = job.secret.as_ref().map(Secret::expose);
job.monitor.update(|p| p.phase = "planning".into());
let mut plan = Vec::new();
let mut summary = Summary::default();
for path in paths {
let rel = normalize_relative(path)
.ok_or_else(|| DeployError::Invalid(format!("{path:?} is not inside the project")))?;
walk_local(job, &rel, 0, &mut plan, &mut summary.skipped)?;
}
let total: u64 = plan.iter().map(|p| p.size).sum();
job.monitor.update(|p| {
p.files_total = plan.len();
p.bytes_total = total;
});
if plan.is_empty() {
job.monitor.log(Level::Warning, "nothing to upload".into());
return Ok(summary);
}
let mut remote = connect(job)?;
job.monitor.update(|p| p.phase = "transferring".into());
let mut made: BTreeSet<String> = BTreeSet::new();
let result = (|| {
for file in &plan {
job.cancel.check()?;
job.monitor.update(|p| p.file = Some(file.local.clone()));
let outcome =
upload_one(job, remote.as_mut(), file, &mut made).map_err(|e| e.redacted(secret));
job.monitor.update(|p| p.files_done += 1);
match outcome {
Ok(()) => {
summary.files += 1;
summary.bytes += file.size;
job.monitor
.log(Level::Info, format!("{} → {}", file.local, file.remote));
}
Err(e) if is_fatal(&e) => return Err(e),
Err(e) => {
summary.failed += 1;
job.monitor
.log(Level::Error, format!("{}: {e}", file.local));
}
}
}
Ok(())
})();
remote.close();
result.map(|()| summary)
}
/// Uploads one file, creating its remote folder first.
fn upload_one(
job: &Job,
remote: &mut dyn Remote,
file: &Planned,
made: &mut BTreeSet<String>,
) -> Result<()> {
let dir = parent(&file.remote).to_string();
if !dir.is_empty() && dir != "/" && !made.contains(&dir) {
remote.mkdir_all(&dir)?;
made.insert(dir);
}
let path = job.project_root.join(&file.local);
let mut data = File::open(&path).map_err(|e| io_error(&path, &e))?;
let monitor = job.monitor.clone();
let cancel = job.cancel.clone();
remote.put(&file.remote, &mut data, &mut |n| {
monitor.update(|p| p.bytes_done = p.bytes_done.saturating_add(n));
cancel.check()
})
}
/// Adds the remote files under the project-relative `rel` to `plan`.
fn walk_remote(
job: &Job,
remote: &mut dyn Remote,
rel: &str,
depth: usize,
plan: &mut Vec<Planned>,
skipped: &mut usize,
) -> Result<()> {
job.cancel.check()?;
let Some(path) = remote_path(&job.server, rel) else {
*skipped += 1;
job.monitor.log(
Level::Warning,
format!("{rel}: skipped (excluded or not mapped)"),
);
return Ok(());
};
match remote.kind(&path)? {
None => {
*skipped += 1;
job.monitor
.log(Level::Warning, format!("{rel}: not on the server ({path})"));
}
Some(false) if !rel.is_empty() => plan.push(Planned {
local: rel.to_string(),
remote: path,
size: 0,
}),
Some(false) => {
*skipped += 1;
}
Some(true) if depth <= MAX_DEPTH => {
for entry in remote.list(&path)? {
if VCS_DIRS.contains(&entry.name.as_str()) {
continue;
}
let child = if rel.is_empty() {
entry.name
} else {
format!("{rel}/{}", entry.name)
};
walk_remote(job, remote, &child, depth + 1, plan, skipped)?;
if plan.len() > MAX_FILES {
return Err(DeployError::Invalid(format!(
"more than {MAX_FILES} files; download a smaller folder"
)));
}
}
}
Some(true) => {}
}
Ok(())
}
/// Downloads the project-relative `paths` (files or folders) from the
/// server into the project, replacing local files.
///
/// # Errors
/// Like [`upload`].
pub fn download(job: &Job, paths: &[String]) -> Result<Summary> {
let secret = job.secret.as_ref().map(Secret::expose);
let mut rels = Vec::new();
for path in paths {
rels.push(
normalize_relative(path).ok_or_else(|| {
DeployError::Invalid(format!("{path:?} is not inside the project"))
})?,
);
}
let mut remote = connect(job)?;
let mut summary = Summary::default();
let result = (|| {
job.monitor.update(|p| p.phase = "planning".into());
let mut plan = Vec::new();
for rel in &rels {
walk_remote(
job,
remote.as_mut(),
rel,
0,
&mut plan,
&mut summary.skipped,
)
.map_err(|e| e.redacted(secret))?;
}
job.monitor.update(|p| {
p.files_total = plan.len();
p.phase = "transferring".into();
});
for file in &plan {
job.cancel.check()?;
job.monitor.update(|p| p.file = Some(file.local.clone()));
let outcome = download_one(job, remote.as_mut(), file).map_err(|e| e.redacted(secret));
job.monitor.update(|p| p.files_done += 1);
match outcome {
Ok(bytes) => {
summary.files += 1;
summary.bytes += bytes;
job.monitor
.log(Level::Info, format!("{} ← {}", file.local, file.remote));
}
Err(e) if is_fatal(&e) => return Err(e),
Err(e) => {
summary.failed += 1;
job.monitor
.log(Level::Error, format!("{}: {e}", file.local));
}
}
}
Ok(())
})();
remote.close();
result.map(|()| summary)
}
/// Downloads one file through a temporary file next to it. Returns its
/// size. A temporary file that cannot be removed after a failure is left;
/// the next download of the same file replaces it.
fn download_one(job: &Job, remote: &mut dyn Remote, file: &Planned) -> Result<u64> {
let target = job.project_root.join(&file.local);
if let Some(dir) = target.parent() {
std::fs::create_dir_all(dir).map_err(|e| io_error(dir, &e))?;
}
let temp = target.with_extension("lutra-download");
let monitor = job.monitor.clone();
let cancel = job.cancel.clone();
let mut bytes = 0u64;
let result = (|| {
let mut out = File::create(&temp).map_err(|e| io_error(&temp, &e))?;
remote.get(&file.remote, &mut out, &mut |n| {
bytes = bytes.saturating_add(n);
monitor.update(|p| p.bytes_done = p.bytes_done.saturating_add(n));
cancel.check()
})?;
drop(out);
std::fs::rename(&temp, &target).map_err(|e| io_error(&target, &e))
})();
if result.is_err() {
let _ = std::fs::remove_file(&temp);
}
result.map(|()| bytes)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::{AuthKind, Mapping};
fn folder_job(project: &Path, target: &Path) -> Job {
Job {
server: Server {
id: "f".into(),
name: "Mounted".into(),
kind: ServerKind::Folder,
host: String::new(),
port: None,
user: String::new(),
root: target.display().to_string(),
auth: AuthKind::Password,
key_path: None,
mappings: vec![Mapping {
local: String::new(),
remote: "site".into(),
}],
excluded: vec!["*.log".into()],
upload_on_save: false,
},
project_root: project.to_path_buf(),
secret: None,
known_hosts: project.join("kh"),
cancel: Cancel::new(),
monitor: Monitor::new(),
}
}
#[test]
fn parents() {
assert_eq!(parent("/a/b"), "/a");
assert_eq!(parent("/a"), "/");
assert_eq!(parent("a/b"), "a");
assert_eq!(parent("a"), "");
}
#[test]
fn upload_and_download_through_a_folder() {
let project = tempfile::tempdir().unwrap();
let target = tempfile::tempdir().unwrap();
std::fs::create_dir_all(project.path().join("src/sub")).unwrap();
std::fs::create_dir_all(project.path().join(".git")).unwrap();
std::fs::write(project.path().join("src/a.php"), "<?php // a").unwrap();
std::fs::write(project.path().join("src/sub/b.php"), "<?php // b").unwrap();
std::fs::write(project.path().join("src/x.log"), "log").unwrap();
std::fs::write(project.path().join(".git/HEAD"), "ref").unwrap();
let job = folder_job(project.path(), target.path());
let summary = upload(&job, &[String::new()]).unwrap();
assert_eq!(summary.files, 2);
assert_eq!(summary.failed, 0);
assert_eq!(
std::fs::read_to_string(target.path().join("site/src/sub/b.php")).unwrap(),
"<?php // b"
);
assert!(!target.path().join("site/src/x.log").exists());
assert!(!target.path().join("site/.git").exists());
let progress = job.monitor.progress();
assert_eq!(progress.files_done, 2);
assert_eq!(progress.bytes_done, 20);
assert!(
job.monitor
.take_lines()
.iter()
.any(|l| l.message.contains("src/a.php → src/a.php")
|| l.message.contains("src/a.php →"))
);
std::fs::write(target.path().join("site/src/a.php"), "<?php // server").unwrap();
let summary = download(&job, &["src".into()]).unwrap();
assert_eq!(summary.files, 2);
assert_eq!(
std::fs::read_to_string(project.path().join("src/a.php")).unwrap(),
"<?php // server"
);
assert!(upload(&job, &["../x".into()]).is_err());
let summary = download(&job, &["missing.php".into()]).unwrap();
assert_eq!(summary.skipped, 1);
let msg = test_connection(&job).unwrap();
assert!(msg.contains("Connected"), "{msg}");
job.cancel.cancel();
assert_eq!(upload(&job, &["src".into()]), Err(DeployError::Cancelled));
}
}

View file

@ -0,0 +1,200 @@
//! Lutra's own known-hosts file for SFTP servers.
//!
//! One line per server: `host:port algorithm SHA256:fingerprint`. Lines
//! starting with `#` and blank lines are ignored. The user's
//! `~/.ssh/known_hosts` is neither read nor written. The first connection
//! to a server fails with [`crate::DeployError::HostKeyUnknown`] carrying the
//! fingerprint; the user checks it and trusts it with [`trust`]. A later
//! key that differs fails with [`crate::DeployError::HostKeyMismatch`] until
//! the old entry is removed with [`forget`].
use std::path::Path;
use crate::config::io_error;
use crate::error::{DeployError, Result};
/// Largest known-hosts file read.
const MAX_FILE: u64 = 1024 * 1024;
/// One trusted key.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HostKey {
/// `host:port`.
pub host: String,
/// The key's algorithm, e.g. `ssh-ed25519`.
pub algorithm: String,
/// `SHA256:` and the base64 digest.
pub fingerprint: String,
}
/// What the file says about an offered key.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HostKeyStatus {
/// The key is the trusted one.
Trusted,
/// No key is trusted for this host.
Unknown,
/// Another key is trusted for this host: its fingerprint.
Mismatch(String),
}
/// Reads the trusted keys; a missing file has none. Malformed lines are
/// skipped.
///
/// # Errors
/// [`DeployError::Transfer`] when the file cannot be read.
pub fn load(path: &Path) -> Result<Vec<HostKey>> {
let meta = match std::fs::metadata(path) {
Ok(meta) => meta,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()),
Err(e) => return Err(io_error(path, &e)),
};
if meta.len() > MAX_FILE {
return Err(DeployError::Transfer(format!(
"{} is larger than 1 MiB",
path.display()
)));
}
let text = std::fs::read_to_string(path).map_err(|e| io_error(path, &e))?;
Ok(text
.lines()
.filter(|l| !l.trim().is_empty() && !l.trim_start().starts_with('#'))
.filter_map(|line| {
let mut fields = line.split_whitespace();
let host = fields.next()?;
let algorithm = fields.next()?;
let fingerprint = fields.next()?;
Some(HostKey {
host: host.to_string(),
algorithm: algorithm.to_string(),
fingerprint: fingerprint.to_string(),
})
})
.collect())
}
/// Checks the offered `fingerprint` of `host` against the file.
///
/// # Errors
/// [`DeployError::Transfer`] when the file cannot be read.
pub fn check(path: &Path, host: &str, fingerprint: &str) -> Result<HostKeyStatus> {
let keys = load(path)?;
let mut known: Option<String> = None;
for key in keys.iter().filter(|k| k.host == host) {
if key.fingerprint == fingerprint {
return Ok(HostKeyStatus::Trusted);
}
known = Some(key.fingerprint.clone());
}
Ok(known.map_or(HostKeyStatus::Unknown, HostKeyStatus::Mismatch))
}
/// Writes `keys` to the file atomically.
fn store(path: &Path, keys: &[HostKey]) -> Result<()> {
if let Some(dir) = path.parent() {
std::fs::create_dir_all(dir).map_err(|e| io_error(dir, &e))?;
}
let mut text = String::from("# Lutra deployment: trusted SFTP host keys\n");
for key in keys {
text.push_str(&format!(
"{} {} {}\n",
key.host, key.algorithm, key.fingerprint
));
}
lutra_vfs::write_atomic(path, text.as_bytes()).map_err(|e| DeployError::Transfer(e.to_string()))
}
/// Trusts `key` for its host, unless another key is trusted for it.
///
/// # Errors
/// [`DeployError::Invalid`] for a field with whitespace or an empty one,
/// [`DeployError::HostKeyMismatch`] when another key is trusted (remove it
/// with [`forget`] first), [`DeployError::Transfer`] when the file cannot
/// be read or written.
pub fn trust(path: &Path, key: &HostKey) -> Result<()> {
for field in [&key.host, &key.algorithm, &key.fingerprint] {
if field.is_empty()
|| field.len() > 512
|| field.chars().any(|c| c.is_whitespace() || c.is_control())
{
return Err(DeployError::Invalid(format!(
"{field:?} is not a valid host key field"
)));
}
}
let mut keys = load(path)?;
if let Some(old) = keys.iter().find(|k| k.host == key.host) {
if old.fingerprint == key.fingerprint {
return Ok(());
}
return Err(DeployError::HostKeyMismatch {
host: key.host.clone(),
algorithm: key.algorithm.clone(),
fingerprint: key.fingerprint.clone(),
known: old.fingerprint.clone(),
});
}
keys.push(key.clone());
store(path, &keys)
}
/// Removes every trusted key of `host`. Returns whether there was one.
///
/// # Errors
/// [`DeployError::Transfer`] when the file cannot be read or written.
pub fn forget(path: &Path, host: &str) -> Result<bool> {
let mut keys = load(path)?;
let before = keys.len();
keys.retain(|k| k.host != host);
if keys.len() == before {
return Ok(false);
}
store(path, &keys)?;
Ok(true)
}
#[cfg(test)]
mod tests {
use super::*;
fn key(fp: &str) -> HostKey {
HostKey {
host: "h:22".into(),
algorithm: "ssh-ed25519".into(),
fingerprint: fp.into(),
}
}
#[test]
fn trust_check_mismatch_forget() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("d").join("known_hosts");
assert_eq!(
check(&path, "h:22", "SHA256:a").unwrap(),
HostKeyStatus::Unknown
);
trust(&path, &key("SHA256:a")).unwrap();
trust(&path, &key("SHA256:a")).unwrap();
assert_eq!(
check(&path, "h:22", "SHA256:a").unwrap(),
HostKeyStatus::Trusted
);
assert_eq!(
check(&path, "h:22", "SHA256:b").unwrap(),
HostKeyStatus::Mismatch("SHA256:a".into())
);
assert!(matches!(
trust(&path, &key("SHA256:b")),
Err(DeployError::HostKeyMismatch { .. })
));
assert_eq!(
check(&path, "other:22", "SHA256:a").unwrap(),
HostKeyStatus::Unknown
);
assert!(forget(&path, "h:22").unwrap());
assert!(!forget(&path, "h:22").unwrap());
trust(&path, &key("SHA256:b")).unwrap();
assert_eq!(load(&path).unwrap().len(), 1);
assert!(trust(&path, &key("SHA256:a b")).is_err());
}
}

36
crates/deploy/src/lib.rs Normal file
View file

@ -0,0 +1,36 @@
//! Deployment (G17, owner decision D27): upload and download project files
//! over SFTP, FTP, FTPS or to a local or mounted folder, with path
//! mappings and excluded paths per server.
//!
//! - [`config`]: server definitions, stored per project without secrets.
//! - [`mapping`]: which remote path a project file goes to.
//! - [`secrets`]: passwords and passphrases, only in the OS keychain.
//! - [`known_hosts`]: Lutra's own trusted SFTP host keys.
//! - [`remote`]: the operations every transport offers (the extension
//! point for the remote browser and compare of D2), and the folder
//! transport; `sftp` and `ftp` are the network ones.
//! - [`job`]: test connection, upload and download with progress, log and
//! cancel.
//!
//! Pure Rust: `russh` and `russh-sftp` for SFTP, `suppaftp` with `rustls`
//! for FTP and FTPS, `ring` for cryptography, `keyring` for the keychain.
//! No credential is ever written to a file, a log or an error message.
pub mod config;
pub mod error;
mod ftp;
pub mod job;
pub mod known_hosts;
pub mod mapping;
pub mod remote;
pub mod secrets;
mod sftp;
#[cfg(feature = "testserver")]
pub mod testserver;
pub use config::{AuthKind, Deployment, Mapping, Server, ServerKind};
pub use error::{DeployError, Result};
pub use job::{Job, Level, LogLine, Monitor, Progress, Summary};
pub use remote::Cancel;
pub use secrets::{Keychain, MemoryStore, Secret, SecretStore};

View file

@ -0,0 +1,262 @@
//! Path mappings: which remote path a project file goes to, and which files
//! are excluded.
//!
//! Local paths are project-relative with `/`. A file maps through the
//! mapping with the longest local folder that contains it; without any
//! mapping the project root maps to the server's root. Remote paths are
//! joined with `/`, never contain `..`, and stay inside the mapping's
//! remote folder.
use crate::config::Server;
/// `path` with `\` turned into `/`, empty and `.` segments dropped, and no
/// leading or trailing `/`. `None` if it contains `..` or a control
/// character.
pub fn normalize_relative(path: &str) -> Option<String> {
if path.chars().any(char::is_control) {
return None;
}
let mut parts = Vec::new();
for part in path.split(['/', '\\']) {
match part {
"" | "." => {}
".." => return None,
other => parts.push(other),
}
}
Some(parts.join("/"))
}
/// Whether `pattern` is a usable excluded path: not empty, no `..`, no
/// control characters.
pub fn valid_pattern(pattern: &str) -> bool {
!pattern.is_empty() && normalize_relative(pattern).is_some_and(|p| !p.is_empty())
}
/// Whether `name` matches `pattern`, where `*` matches any run of
/// characters and `?` exactly one.
fn glob(pattern: &[char], name: &[char]) -> bool {
let (mut p, mut n) = (0usize, 0usize);
let mut star: Option<(usize, usize)> = None;
while n < name.len() {
match pattern.get(p) {
Some('*') => {
star = Some((p, n));
p += 1;
}
Some(c) if *c == '?' || Some(c) == name.get(n) => {
p += 1;
n += 1;
}
_ => match star {
Some((sp, sn)) => {
p = sp + 1;
n = sn + 1;
star = Some((sp, sn + 1));
}
None => return false,
},
}
}
pattern
.get(p..)
.is_some_and(|rest| rest.iter().all(|c| *c == '*'))
}
/// Whether the project-relative `path` (normalized) is excluded by
/// `server`: a pattern without `*` or `?` excludes that path and
/// everything below it; a pattern with them is matched against every
/// segment's name.
pub fn is_excluded(server: &Server, path: &str) -> bool {
server.excluded.iter().any(|pattern| {
if pattern.contains(['*', '?']) {
let pattern: Vec<char> = pattern.chars().collect();
path.split('/').any(|segment| {
let name: Vec<char> = segment.chars().collect();
glob(&pattern, &name)
})
} else {
path == pattern
|| path
.strip_prefix(pattern.as_str())
.is_some_and(|rest| rest.starts_with('/'))
}
})
}
/// Joins remote path pieces with `/`, skipping empty ones. Keeps a leading
/// `/` of the first non-empty piece.
fn join(pieces: &[&str]) -> String {
let mut out = String::new();
for piece in pieces.iter().filter(|p| !p.is_empty()) {
if out.is_empty() {
out.push_str(piece.trim_end_matches('/'));
if out.is_empty() {
out.push('/');
}
} else {
if !out.ends_with('/') {
out.push('/');
}
out.push_str(piece.trim_matches('/'));
}
}
out
}
/// The remote path of the project-relative `path` on `server`, or `None`
/// when it is excluded, escapes the project, or no mapping covers it.
/// For a folder server the result is relative to its root folder.
pub fn remote_path(server: &Server, path: &str) -> Option<String> {
let path = normalize_relative(path)?;
if is_excluded(server, &path) {
return None;
}
let (local, remote) = if server.mappings.is_empty() {
("", "")
} else {
server
.mappings
.iter()
.filter(|m| {
m.local.is_empty()
|| path == m.local
|| path
.strip_prefix(m.local.as_str())
.is_some_and(|rest| rest.starts_with('/'))
})
.max_by_key(|m| m.local.len())
.map(|m| (m.local.as_str(), m.remote.as_str()))?
};
let rest = path
.strip_prefix(local)
.unwrap_or(&path)
.trim_start_matches('/');
let root = if remote.starts_with('/') || server.kind == crate::ServerKind::Folder {
""
} else {
server.root.as_str()
};
let joined = join(&[root, remote, rest]);
Some(joined)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::{AuthKind, Mapping, ServerKind};
use proptest::prelude::*;
fn server(mappings: &[(&str, &str)], excluded: &[&str]) -> Server {
Server {
id: "s".into(),
name: "S".into(),
kind: ServerKind::Sftp,
host: "h".into(),
port: None,
user: "u".into(),
root: "/var/www".into(),
auth: AuthKind::Password,
key_path: None,
mappings: mappings
.iter()
.map(|(l, r)| Mapping {
local: (*l).into(),
remote: (*r).into(),
})
.collect(),
excluded: excluded.iter().map(|e| (*e).into()).collect(),
upload_on_save: false,
}
}
#[test]
fn normalizes() {
assert_eq!(normalize_relative("./a//b\\c/").unwrap(), "a/b/c");
assert_eq!(normalize_relative("").unwrap(), "");
assert!(normalize_relative("a/../b").is_none());
assert!(normalize_relative("a\nb").is_none());
}
#[test]
fn default_mapping_uses_the_root() {
let s = server(&[], &[]);
assert_eq!(remote_path(&s, "src/a.php").unwrap(), "/var/www/src/a.php");
let mut s = s;
s.root = String::new();
assert_eq!(remote_path(&s, "src/a.php").unwrap(), "src/a.php");
s.root = "/".into();
assert_eq!(remote_path(&s, "a.php").unwrap(), "/a.php");
}
#[test]
fn longest_mapping_wins() {
let s = server(&[("", "app"), ("public", "/srv/html"), ("pub", "x")], &[]);
assert_eq!(
remote_path(&s, "public/index.php").unwrap(),
"/srv/html/index.php"
);
assert_eq!(remote_path(&s, "public").unwrap(), "/srv/html");
assert_eq!(
remote_path(&s, "publicity.txt").unwrap(),
"/var/www/app/publicity.txt"
);
assert_eq!(remote_path(&s, "pub/a").unwrap(), "/var/www/x/a");
}
#[test]
fn unmapped_and_excluded_paths_are_none() {
let s = server(
&[("public", "html")],
&[".env", "var/cache", "*.log", "tmp?"],
);
assert!(remote_path(&s, "src/a.php").is_none());
let s = server(&[], &[".env", "var/cache", "*.log", "tmp?"]);
assert!(remote_path(&s, ".env").is_none());
assert!(remote_path(&s, "var/cache/x").is_none());
assert!(remote_path(&s, "var/cache").is_none());
assert!(remote_path(&s, "var/cached").is_some());
assert!(remote_path(&s, "logs/app.log").is_none());
assert!(remote_path(&s, "tmp1/a").is_none());
assert!(remote_path(&s, "tmp12/a").is_some());
assert!(remote_path(&s, "../x").is_none());
}
#[test]
fn globs() {
let g = |p: &str, n: &str| {
glob(
&p.chars().collect::<Vec<_>>(),
&n.chars().collect::<Vec<_>>(),
)
};
assert!(g("*.log", "a.log"));
assert!(g("*", ""));
assert!(g("a*b*c", "aXbYbZc"));
assert!(!g("a*b", "aXc"));
assert!(!g("?", ""));
}
proptest! {
#[test]
fn mapped_paths_stay_under_the_mapping(
parts in proptest::collection::vec("[a-zA-Z0-9._ -]{1,8}", 1..6),
remote in "[a-z]{0,6}",
) {
let s = server(&[("", &remote)], &[]);
let path = parts.join("/");
if let Some(r) = remote_path(&s, &path) {
let base = join(&["/var/www", &remote]);
prop_assert!(r.starts_with(&base));
prop_assert!(!r.split('/').any(|seg| seg == ".."));
} else {
prop_assert!(normalize_relative(&path).is_none());
}
}
#[test]
fn glob_never_panics(p in ".{0,12}", n in ".{0,12}") {
let _ = glob(&p.chars().collect::<Vec<_>>(), &n.chars().collect::<Vec<_>>());
}
}
}

294
crates/deploy/src/remote.rs Normal file
View file

@ -0,0 +1,294 @@
//! The operations every transport offers, cancellation and progress, and
//! the folder transport.
//!
//! Remote paths are `/`-separated strings as [`crate::mapping`] builds
//! them. Directory listings come from the server and are untrusted: names
//! with `/`, `\`, control characters, `.` or `..` are dropped by
//! [`safe_name`] before they become local paths.
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use crate::config::io_error;
use crate::error::{DeployError, Result};
use crate::mapping::normalize_relative;
/// Size of one transfer chunk; progress and cancellation are checked
/// between chunks.
pub const CHUNK: usize = 32 * 1024;
/// Most entries read from one remote folder.
pub const MAX_LISTING: usize = 100_000;
/// Stops a running job. Clones share the flag.
#[derive(Debug, Clone, Default)]
pub struct Cancel(Arc<AtomicBool>);
impl Cancel {
/// A flag that is not set.
pub fn new() -> Self {
Self::default()
}
/// Asks the job to stop.
pub fn cancel(&self) {
self.0.store(true, Ordering::SeqCst);
}
/// Whether the job was asked to stop.
pub fn is_cancelled(&self) -> bool {
self.0.load(Ordering::SeqCst)
}
/// `Err(Cancelled)` once the flag is set.
///
/// # Errors
/// [`DeployError::Cancelled`].
pub fn check(&self) -> Result<()> {
if self.is_cancelled() {
Err(DeployError::Cancelled)
} else {
Ok(())
}
}
}
/// One entry of a remote folder.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Entry {
/// The entry's name, checked by [`safe_name`].
pub name: String,
/// Whether it is a folder.
pub is_dir: bool,
/// Its size in bytes, when the server said.
pub size: Option<u64>,
}
/// Whether a name from a remote listing may become a local path segment.
pub fn safe_name(name: &str) -> bool {
!name.is_empty()
&& name != "."
&& name != ".."
&& name.len() <= 255
&& !name.contains(['/', '\\'])
&& !name.chars().any(char::is_control)
}
/// Called after every chunk with its size; returning an error (such as
/// [`DeployError::Cancelled`]) stops the transfer.
pub type Tick<'a> = &'a mut dyn FnMut(u64) -> Result<()>;
/// A connected server.
pub trait Remote {
/// Creates `dir` and every missing parent.
///
/// # Errors
/// [`DeployError::Transfer`] when the server refuses.
fn mkdir_all(&mut self, dir: &str) -> Result<()>;
/// Writes `data` to the file `path`, replacing it.
///
/// # Errors
/// [`DeployError::Transfer`], or what `tick` returned.
fn put(&mut self, path: &str, data: &mut dyn Read, tick: Tick<'_>) -> Result<()>;
/// Reads the file `path` into `out`.
///
/// # Errors
/// [`DeployError::Transfer`], or what `tick` returned.
fn get(&mut self, path: &str, out: &mut dyn Write, tick: Tick<'_>) -> Result<()>;
/// Whether `path` is a folder (`Some(true)`), a file (`Some(false)`)
/// or missing (`None`).
///
/// # Errors
/// [`DeployError::Transfer`] when the server fails otherwise.
fn kind(&mut self, path: &str) -> Result<Option<bool>>;
/// The entries of the folder `path`, without unsafe names, at most
/// [`MAX_LISTING`]. Meant for downloads now and the remote browser
/// later.
///
/// # Errors
/// [`DeployError::Transfer`] when the server refuses.
fn list(&mut self, path: &str) -> Result<Vec<Entry>>;
/// Ends the session politely; failures are ignored because the
/// connection is dropped anyway.
fn close(&mut self);
}
/// Copies `from` into `to` in chunks, calling `tick` after each.
///
/// # Errors
/// [`DeployError::Transfer`] on a read or write failure, or what `tick`
/// returned.
pub fn copy_chunks(from: &mut dyn Read, to: &mut dyn Write, tick: Tick<'_>) -> Result<u64> {
let mut buf = vec![0u8; CHUNK];
let mut total = 0u64;
loop {
let n = from
.read(&mut buf)
.map_err(|e| DeployError::Transfer(format!("read failed: {e}")))?;
if n == 0 {
break;
}
let chunk = buf.get(..n).unwrap_or_default();
to.write_all(chunk)
.map_err(|e| DeployError::Transfer(format!("write failed: {e}")))?;
total = total.saturating_add(n as u64);
tick(n as u64)?;
}
to.flush()
.map_err(|e| DeployError::Transfer(format!("write failed: {e}")))?;
Ok(total)
}
/// A local or mounted folder as a server.
#[derive(Debug)]
pub struct Folder {
/// The deployment root.
root: PathBuf,
}
impl Folder {
/// Opens the folder `root`.
///
/// # Errors
/// [`DeployError::Connect`] when it is not an existing folder.
pub fn open(root: &Path) -> Result<Self> {
if !root.is_dir() {
return Err(DeployError::Connect {
host: root.display().to_string(),
message: "the folder does not exist or is not mounted".into(),
});
}
Ok(Self {
root: root.to_path_buf(),
})
}
/// The local path of the remote `path`, always inside the root.
fn resolve(&self, path: &str) -> Result<PathBuf> {
let rel = normalize_relative(path).ok_or_else(|| {
DeployError::Invalid(format!("{path:?} leaves the deployment folder"))
})?;
Ok(if rel.is_empty() {
self.root.clone()
} else {
self.root.join(rel)
})
}
}
impl Remote for Folder {
fn mkdir_all(&mut self, dir: &str) -> Result<()> {
let path = self.resolve(dir)?;
std::fs::create_dir_all(&path).map_err(|e| io_error(&path, &e))
}
/// Writes a temporary file next to the target and renames it. A failed
/// or cancelled write removes the temporary file; if even that fails it
/// is left, and the next upload of the same file replaces it.
fn put(&mut self, path: &str, data: &mut dyn Read, tick: Tick<'_>) -> Result<()> {
let target = self.resolve(path)?;
let temp = target.with_extension("lutra-upload");
let result = (|| {
let mut file = std::fs::File::create(&temp).map_err(|e| io_error(&temp, &e))?;
copy_chunks(data, &mut file, tick)?;
drop(file);
std::fs::rename(&temp, &target).map_err(|e| io_error(&target, &e))
})();
if result.is_err() {
let _ = std::fs::remove_file(&temp);
}
result
}
fn get(&mut self, path: &str, out: &mut dyn Write, tick: Tick<'_>) -> Result<()> {
let source = self.resolve(path)?;
let mut file = std::fs::File::open(&source).map_err(|e| io_error(&source, &e))?;
copy_chunks(&mut file, out, tick).map(|_| ())
}
fn kind(&mut self, path: &str) -> Result<Option<bool>> {
let path = self.resolve(path)?;
match std::fs::metadata(&path) {
Ok(meta) => Ok(Some(meta.is_dir())),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(io_error(&path, &e)),
}
}
fn list(&mut self, path: &str) -> Result<Vec<Entry>> {
let dir = self.resolve(path)?;
let mut entries = Vec::new();
for entry in std::fs::read_dir(&dir).map_err(|e| io_error(&dir, &e))? {
let entry = entry.map_err(|e| io_error(&dir, &e))?;
let name = entry.file_name().to_string_lossy().into_owned();
if !safe_name(&name) {
continue;
}
let Ok(meta) = entry.metadata() else {
continue;
};
entries.push(Entry {
name,
is_dir: meta.is_dir(),
size: meta.is_file().then_some(meta.len()),
});
if entries.len() >= MAX_LISTING {
break;
}
}
entries.sort_by(|a, b| a.name.cmp(&b.name));
Ok(entries)
}
fn close(&mut self) {}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn names() {
assert!(safe_name("a.php"));
for bad in ["", ".", "..", "a/b", "a\\b", "a\nb"] {
assert!(!safe_name(bad), "{bad:?}");
}
}
#[test]
fn folder_round_trip() {
let dir = tempfile::tempdir().unwrap();
let mut f = Folder::open(dir.path()).unwrap();
f.mkdir_all("/a/b").unwrap();
let mut ticks = 0u64;
f.put("a/b/x.txt", &mut &b"hello"[..], &mut |n| {
ticks += n;
Ok(())
})
.unwrap();
assert_eq!(ticks, 5);
assert_eq!(f.kind("a/b").unwrap(), Some(true));
assert_eq!(f.kind("a/b/x.txt").unwrap(), Some(false));
assert_eq!(f.kind("nope").unwrap(), None);
let mut out = Vec::new();
f.get("a/b/x.txt", &mut out, &mut |_| Ok(())).unwrap();
assert_eq!(out, b"hello");
assert_eq!(f.list("a/b").unwrap()[0].name, "x.txt");
assert!(f.resolve("../x").is_err());
let cancel = Cancel::new();
cancel.cancel();
let err = f
.put("a/y.txt", &mut &b"z"[..], &mut |_| cancel.check())
.unwrap_err();
assert_eq!(err, DeployError::Cancelled);
assert_eq!(f.kind("a/y.txt").unwrap(), None);
assert!(Folder::open(&dir.path().join("missing")).is_err());
}
}

View file

@ -0,0 +1,195 @@
//! Passwords and passphrases, kept only in the OS keychain (owner decision
//! D27): the Windows Credential Manager and the macOS Keychain through the
//! `keyring` crate. Lutra never writes a secret to a file, a log, the
//! journal, an event or an error message.
//!
//! Entries use the service [`SERVICE`] and the account
//! `<project-id>/<server-id>/<kind>`, so each project's servers have their
//! own entries. Other platforms have no store yet and refuse with a clear
//! message; agent and key sign-in without a passphrase still work there.
//!
//! Tests and hosts without a keychain use [`MemoryStore`].
use std::collections::BTreeMap;
use std::fmt;
use std::sync::{Mutex, PoisonError};
use crate::error::{DeployError, Result};
/// The keychain service name of Lutra's deployment entries.
pub const SERVICE: &str = "Lutra Deployment";
/// Longest secret accepted.
pub const MAX_SECRET: usize = 4096;
/// A password or passphrase. Its `Debug` output is `Secret(***)`, so it
/// never leaks through a debug print.
#[derive(Clone, PartialEq, Eq)]
pub struct Secret(String);
impl Secret {
/// Wraps `value`.
///
/// # Errors
/// [`DeployError::Invalid`] if it is empty, longer than
/// [`MAX_SECRET`] bytes, or contains a NUL.
pub fn new(value: String) -> Result<Self> {
if value.is_empty() || value.len() > MAX_SECRET || value.contains('\0') {
return Err(DeployError::Invalid(
"the secret must be 1 to 4096 bytes without NUL".into(),
));
}
Ok(Self(value))
}
/// The plain value, for the protocol call that needs it.
pub fn expose(&self) -> &str {
&self.0
}
}
impl fmt::Debug for Secret {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Secret(***)")
}
}
/// Where secrets are kept.
pub trait SecretStore: Send + Sync + fmt::Debug {
/// The secret stored under `account`, if any.
///
/// # Errors
/// [`DeployError::Keychain`] when the store fails.
fn get(&self, account: &str) -> Result<Option<Secret>>;
/// Stores `secret` under `account`, replacing an older one.
///
/// # Errors
/// [`DeployError::Keychain`] when the store fails.
fn set(&self, account: &str, secret: &Secret) -> Result<()>;
/// Removes the secret under `account`; a missing one is fine.
///
/// # Errors
/// [`DeployError::Keychain`] when the store fails.
fn delete(&self, account: &str) -> Result<()>;
}
/// The account name of one server's secret.
pub fn account(project_id: &str, server_id: &str, kind: &str) -> String {
format!("{project_id}/{server_id}/{kind}")
}
/// The OS keychain.
#[derive(Debug, Default)]
pub struct Keychain;
#[cfg(any(windows, target_os = "macos"))]
impl Keychain {
/// The keychain entry of `account`.
fn entry(account: &str) -> Result<keyring::Entry> {
keyring::Entry::new(SERVICE, account).map_err(|e| DeployError::Keychain(e.to_string()))
}
}
#[cfg(any(windows, target_os = "macos"))]
impl SecretStore for Keychain {
fn get(&self, account: &str) -> Result<Option<Secret>> {
match Self::entry(account)?.get_password() {
Ok(value) => Secret::new(value).map(Some),
Err(keyring::Error::NoEntry) => Ok(None),
Err(e) => Err(DeployError::Keychain(e.to_string())),
}
}
fn set(&self, account: &str, secret: &Secret) -> Result<()> {
Self::entry(account)?
.set_password(secret.expose())
.map_err(|e| DeployError::Keychain(e.to_string()))
}
fn delete(&self, account: &str) -> Result<()> {
match Self::entry(account)?.delete_credential() {
Ok(()) | Err(keyring::Error::NoEntry) => Ok(()),
Err(e) => Err(DeployError::Keychain(e.to_string())),
}
}
}
#[cfg(not(any(windows, target_os = "macos")))]
impl SecretStore for Keychain {
fn get(&self, _account: &str) -> Result<Option<Secret>> {
Ok(None)
}
fn set(&self, _account: &str, _secret: &Secret) -> Result<()> {
Err(DeployError::Keychain(
"Lutra stores deployment passwords only in the Windows Credential Manager or the macOS Keychain; on this system use an SSH agent or a key without a passphrase".into(),
))
}
fn delete(&self, _account: &str) -> Result<()> {
Ok(())
}
}
/// Secrets in memory only, for tests and hosts without a keychain.
#[derive(Debug, Default)]
pub struct MemoryStore {
/// Secrets by account.
entries: Mutex<BTreeMap<String, Secret>>,
}
impl SecretStore for MemoryStore {
fn get(&self, account: &str) -> Result<Option<Secret>> {
Ok(self
.entries
.lock()
.unwrap_or_else(PoisonError::into_inner)
.get(account)
.cloned())
}
fn set(&self, account: &str, secret: &Secret) -> Result<()> {
self.entries
.lock()
.unwrap_or_else(PoisonError::into_inner)
.insert(account.to_string(), secret.clone());
Ok(())
}
fn delete(&self, account: &str) -> Result<()> {
self.entries
.lock()
.unwrap_or_else(PoisonError::into_inner)
.remove(account);
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn debug_hides_the_value() {
let s = Secret::new("hunter2".into()).unwrap();
assert_eq!(format!("{s:?}"), "Secret(***)");
assert_eq!(s.expose(), "hunter2");
assert!(Secret::new(String::new()).is_err());
assert!(Secret::new("a\0b".into()).is_err());
}
#[test]
fn memory_store_round_trip() {
let store = MemoryStore::default();
let acc = account("p", "s", "password");
assert_eq!(acc, "p/s/password");
assert!(store.get(&acc).unwrap().is_none());
store.set(&acc, &Secret::new("x".into()).unwrap()).unwrap();
assert_eq!(store.get(&acc).unwrap().unwrap().expose(), "x");
store.delete(&acc).unwrap();
store.delete(&acc).unwrap();
assert!(store.get(&acc).unwrap().is_none());
}
}

458
crates/deploy/src/sftp.rs Normal file
View file

@ -0,0 +1,458 @@
//! SFTP over SSH with `russh` and `russh-sftp`, pure Rust with `ring`.
//!
//! The library is async; [`Sftp`] owns a single-threaded tokio runtime and
//! blocks on it, so callers stay synchronous like the other transports.
//! Every network wait races the job's [`Cancel`] flag (checked every 50
//! ms) and a timeout.
//!
//! The server's host key is checked against Lutra's known-hosts file
//! before any credential is sent. Sign-in uses a password from the
//! keychain, a private key file (with its passphrase from the keychain), or
//! the SSH agent.
use std::future::Future;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, PoisonError};
use std::time::Duration;
use russh::client::{self, Handle};
use russh::keys::{HashAlg, PrivateKeyWithHashAlg, PublicKeyOrCertificate};
use russh_sftp::client::SftpSession;
use russh_sftp::protocol::StatusCode;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::runtime::Runtime;
use crate::config::{AuthKind, Server};
use crate::error::{DeployError, Result};
use crate::known_hosts::{self, HostKeyStatus};
use crate::remote::{CHUNK, Cancel, Entry, MAX_LISTING, Remote, Tick, safe_name};
use crate::secrets::Secret;
/// Longest wait for the TCP connection and the SSH handshake.
const CONNECT_TIMEOUT: Duration = Duration::from_secs(20);
/// Longest wait for one SFTP request or chunk.
const REQUEST_TIMEOUT: Duration = Duration::from_secs(60);
/// The SSH client's callbacks: checks the host key.
struct Client {
/// `host:port`.
host: String,
/// Lutra's known-hosts file.
known_hosts: PathBuf,
/// Why the key was refused, read after the handshake failed.
verdict: Arc<Mutex<Option<DeployError>>>,
}
impl Client {
/// Remembers why the key was refused.
fn refuse(&self, error: DeployError) -> bool {
*self.verdict.lock().unwrap_or_else(PoisonError::into_inner) = Some(error);
false
}
}
impl client::Handler for Client {
type Error = russh::Error;
async fn check_server_key(
&mut self,
offered: &PublicKeyOrCertificate,
) -> std::result::Result<bool, Self::Error> {
let PublicKeyOrCertificate::PublicKey { key, .. } = offered else {
return Ok(self.refuse(DeployError::Auth(format!(
"{} offered an SSH certificate as its host key; Lutra accepts plain host keys only",
self.host
))));
};
let fingerprint = key.fingerprint(HashAlg::Sha256).to_string();
let algorithm = key.algorithm().to_string();
Ok(
match known_hosts::check(&self.known_hosts, &self.host, &fingerprint) {
Ok(HostKeyStatus::Trusted) => true,
Ok(HostKeyStatus::Unknown) => self.refuse(DeployError::HostKeyUnknown {
host: self.host.clone(),
algorithm,
fingerprint,
}),
Ok(HostKeyStatus::Mismatch(known)) => self.refuse(DeployError::HostKeyMismatch {
host: self.host.clone(),
algorithm,
fingerprint,
known,
}),
Err(e) => self.refuse(e),
},
)
}
}
/// An open SFTP session.
pub struct Sftp {
/// Drives the async library.
runtime: Runtime,
/// The SSH connection.
handle: Handle<Client>,
/// The SFTP channel.
sftp: SftpSession,
/// The job's cancel flag.
cancel: Cancel,
}
/// Runs `fut` until it ends, `limit` passes or `cancel` is set.
async fn bounded<T, F: Future<Output = T>>(
cancel: &Cancel,
limit: Duration,
what: &str,
fut: F,
) -> Result<T> {
let watch = async {
loop {
if cancel.is_cancelled() {
return;
}
tokio::time::sleep(Duration::from_millis(50)).await;
}
};
tokio::select! {
out = tokio::time::timeout(limit, fut) => out.map_err(|_| {
DeployError::Transfer(format!("{what} timed out after {} s", limit.as_secs()))
}),
() = watch => Err(DeployError::Cancelled),
}
}
/// An SFTP library error as a transfer error.
fn sftp_error(what: &str, path: &str, e: &russh_sftp::client::error::Error) -> DeployError {
DeployError::Transfer(format!("{what} {path}: {e}"))
}
/// Whether `e` says the path does not exist.
fn is_missing(e: &russh_sftp::client::error::Error) -> bool {
matches!(e, russh_sftp::client::error::Error::Status(s) if s.status_code == StatusCode::NoSuchFile)
}
impl Sftp {
/// Connects to `server`, checks its host key against `known_hosts`,
/// signs in and opens the SFTP subsystem.
///
/// # Errors
/// [`DeployError::HostKeyUnknown`] or [`DeployError::HostKeyMismatch`]
/// before any credential is sent; [`DeployError::MissingSecret`] when a
/// password is needed but `secret` is `None`; [`DeployError::Auth`]
/// when the server refuses; [`DeployError::Connect`] when it cannot be
/// reached; [`DeployError::Cancelled`].
pub fn connect(
server: &Server,
secret: Option<&Secret>,
known_hosts: &Path,
cancel: &Cancel,
) -> Result<Self> {
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.map_err(|e| DeployError::Transfer(format!("cannot start the SSH runtime: {e}")))?;
let host = server.address();
let verdict = Arc::new(Mutex::new(None));
let handler = Client {
host: host.clone(),
known_hosts: known_hosts.to_path_buf(),
verdict: Arc::clone(&verdict),
};
let config = Arc::new(client::Config {
inactivity_timeout: Some(Duration::from_secs(120)),
..Default::default()
});
let target = (server.host.clone(), server.effective_port());
let connected = runtime.block_on(bounded(
cancel,
CONNECT_TIMEOUT,
"connecting",
client::connect(config, target, handler),
))?;
let mut handle = match connected {
Ok(handle) => handle,
Err(e) => {
if let Some(refusal) = verdict
.lock()
.unwrap_or_else(PoisonError::into_inner)
.take()
{
return Err(refusal);
}
return Err(DeployError::Connect {
host,
message: e.to_string(),
});
}
};
let signed_in = runtime.block_on(bounded(
cancel,
CONNECT_TIMEOUT,
"signing in",
authenticate(&mut handle, server, secret),
))??;
if !signed_in {
let how = match server.auth {
AuthKind::Password => "the user name or password",
AuthKind::Key => "the user name or the key",
AuthKind::Agent => "the user name or the keys in the SSH agent",
};
return Err(DeployError::Auth(format!(
"{host} refused the sign-in; check {how}"
)));
}
let sftp = runtime
.block_on(bounded(cancel, CONNECT_TIMEOUT, "opening SFTP", async {
let channel = handle.channel_open_session().await?;
channel.request_subsystem(true, "sftp").await?;
Ok::<_, russh::Error>(channel.into_stream())
}))?
.map_err(|e| DeployError::Connect {
host: host.clone(),
message: format!("the server offers no SFTP: {e}"),
})?;
let sftp = runtime
.block_on(bounded(
cancel,
CONNECT_TIMEOUT,
"opening SFTP",
SftpSession::new(sftp),
))?
.map_err(|e| DeployError::Connect {
host,
message: format!("the SFTP session failed: {e}"),
})?;
Ok(Self {
runtime,
handle,
sftp,
cancel: cancel.clone(),
})
}
/// Runs one request with the request timeout and the cancel flag.
fn run<T, F: Future<Output = T>>(&self, what: &str, fut: F) -> Result<T> {
self.runtime
.block_on(bounded(&self.cancel, REQUEST_TIMEOUT, what, fut))
}
}
/// Signs in on `handle` the way `server` says. Returns whether the server
/// accepted.
async fn authenticate(
handle: &mut Handle<Client>,
server: &Server,
secret: Option<&Secret>,
) -> Result<bool> {
let user = server.user.clone();
let auth_error = |e: russh::Error| DeployError::Auth(format!("signing in failed: {e}"));
match server.auth {
AuthKind::Password => {
let Some(secret) = secret else {
return Err(DeployError::MissingSecret {
server: server.name.clone(),
kind: "password".into(),
});
};
let result = handle
.authenticate_password(user, secret.expose())
.await
.map_err(auth_error)?;
Ok(result.success())
}
AuthKind::Key => {
let path = server.key_path.clone().unwrap_or_default();
let key = match russh::keys::load_secret_key(&path, secret.map(Secret::expose)) {
Ok(key) => key,
Err(russh::keys::Error::KeyIsEncrypted) if secret.is_none() => {
return Err(DeployError::MissingSecret {
server: server.name.clone(),
kind: "passphrase".into(),
});
}
Err(e) => {
return Err(DeployError::Auth(format!(
"cannot read the private key {path}: {e}"
)));
}
};
let hash = handle
.best_supported_rsa_hash()
.await
.map_err(auth_error)?
.flatten();
let result = handle
.authenticate_publickey(user, PrivateKeyWithHashAlg::new(Arc::new(key), hash))
.await
.map_err(auth_error)?;
Ok(result.success())
}
AuthKind::Agent => agent(handle, &user).await,
}
}
/// Tries every key of the SSH agent.
#[cfg(windows)]
async fn agent(handle: &mut Handle<Client>, user: &str) -> Result<bool> {
use russh::keys::agent::client::AgentClient;
if let Ok(client) = AgentClient::connect_named_pipe(r"\\.\pipe\openssh-ssh-agent").await {
return agent_keys(handle, user, client).await;
}
match AgentClient::connect_pageant().await {
Ok(client) => agent_keys(handle, user, client).await,
Err(_) => Err(DeployError::Auth(
"no SSH agent is running: start the OpenSSH Authentication Agent service or Pageant and add your key".into(),
)),
}
}
/// Tries every key of the SSH agent.
#[cfg(unix)]
async fn agent(handle: &mut Handle<Client>, user: &str) -> Result<bool> {
use russh::keys::agent::client::AgentClient;
match AgentClient::connect_env().await {
Ok(client) => agent_keys(handle, user, client).await,
Err(_) => Err(DeployError::Auth(
"no SSH agent is reachable through SSH_AUTH_SOCK: start one and add your key (ssh-add)"
.into(),
)),
}
}
/// Tries each key the agent holds until the server accepts one.
async fn agent_keys<S>(
handle: &mut Handle<Client>,
user: &str,
mut client: russh::keys::agent::client::AgentClient<S>,
) -> Result<bool>
where
S: tokio::io::AsyncRead + tokio::io::AsyncWrite + Unpin + Send + 'static,
{
let identities = client
.request_identities()
.await
.map_err(|e| DeployError::Auth(format!("the SSH agent did not answer: {e}")))?;
let hash = handle
.best_supported_rsa_hash()
.await
.ok()
.flatten()
.flatten();
for identity in identities {
let russh::keys::agent::AgentIdentity::PublicKey { key, .. } = identity else {
continue;
};
if let Ok(result) = handle
.authenticate_publickey_with(user, key, hash, &mut client)
.await
&& result.success()
{
return Ok(true);
}
}
Ok(false)
}
impl Remote for Sftp {
fn mkdir_all(&mut self, dir: &str) -> Result<()> {
let absolute = dir.starts_with('/');
let mut path = String::new();
for part in dir.split('/').filter(|p| !p.is_empty()) {
if absolute || !path.is_empty() {
path.push('/');
}
path.push_str(part);
let exists = self
.run("checking a folder", self.sftp.try_exists(path.clone()))?
.map_err(|e| sftp_error("cannot check", &path, &e))?;
if !exists {
self.run("creating a folder", self.sftp.create_dir(path.clone()))?
.map_err(|e| sftp_error("cannot create the folder", &path, &e))?;
}
}
Ok(())
}
fn put(&mut self, path: &str, data: &mut dyn Read, tick: Tick<'_>) -> Result<()> {
let mut file = self
.run("opening a file", self.sftp.create(path.to_string()))?
.map_err(|e| sftp_error("cannot write", path, &e))?;
let mut buf = vec![0u8; CHUNK];
loop {
let n = data
.read(&mut buf)
.map_err(|e| DeployError::Transfer(format!("read failed: {e}")))?;
if n == 0 {
break;
}
let chunk = buf.get(..n).unwrap_or_default();
self.run("uploading", file.write_all(chunk))?
.map_err(|e| DeployError::Transfer(format!("cannot write {path}: {e}")))?;
tick(n as u64)?;
}
self.run("closing a file", file.shutdown())?
.map_err(|e| DeployError::Transfer(format!("cannot finish {path}: {e}")))
}
fn get(&mut self, path: &str, out: &mut dyn Write, tick: Tick<'_>) -> Result<()> {
let mut file = self
.run("opening a file", self.sftp.open(path.to_string()))?
.map_err(|e| sftp_error("cannot read", path, &e))?;
let mut buf = vec![0u8; CHUNK];
loop {
let n = self
.run("downloading", file.read(&mut buf))?
.map_err(|e| DeployError::Transfer(format!("cannot read {path}: {e}")))?;
if n == 0 {
break;
}
out.write_all(buf.get(..n).unwrap_or_default())
.map_err(|e| DeployError::Transfer(format!("write failed: {e}")))?;
tick(n as u64)?;
}
Ok(())
}
fn kind(&mut self, path: &str) -> Result<Option<bool>> {
let target = if path.is_empty() { "." } else { path };
match self.run("checking a path", self.sftp.metadata(target.to_string()))? {
Ok(meta) => Ok(Some(meta.is_dir())),
Err(e) if is_missing(&e) => Ok(None),
Err(e) => Err(sftp_error("cannot check", path, &e)),
}
}
fn list(&mut self, path: &str) -> Result<Vec<Entry>> {
let target = if path.is_empty() { "." } else { path };
let dir = self
.run("listing a folder", self.sftp.read_dir(target.to_string()))?
.map_err(|e| sftp_error("cannot list", path, &e))?;
let mut entries: Vec<Entry> = dir
.filter(|e| safe_name(&e.file_name()))
.take(MAX_LISTING)
.map(|e| {
let meta = e.metadata();
Entry {
name: e.file_name(),
is_dir: meta.is_dir(),
size: meta.size,
}
})
.collect();
entries.sort_by(|a, b| a.name.cmp(&b.name));
Ok(entries)
}
/// Closes the SFTP channel and the connection. Failures are ignored:
/// the connection is dropped right after.
fn close(&mut self) {
let _ = self.run("closing", self.sftp.close());
let _ = self.run(
"disconnecting",
self.handle
.disconnect(russh::Disconnect::ByApplication, "", "en"),
);
}
}

View file

@ -0,0 +1,664 @@
//! Local servers for tests and live checks (feature `testserver`): an SFTP
//! server built on `russh` and a small FTP server, both bound to
//! `127.0.0.1` on a free port and serving one folder. They never listen on
//! another interface and are not part of the shipped app.
//!
//! The SFTP server has a fixed Ed25519 host key (from a seed) and accepts
//! one user with one password, or one public key. The FTP server speaks
//! enough of RFC 959 for Lutra's client: USER, PASS, SYST, FEAT, TYPE,
//! PWD, CWD, MKD, SIZE, PASV, STOR, RETR, LIST, NOOP, QUIT. Both stop when
//! dropped.
use std::collections::HashMap;
use std::io::{BufRead, BufReader, Read, Seek, SeekFrom, Write};
use std::net::{SocketAddr, TcpListener, TcpStream};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
use russh::keys::ssh_key::private::Ed25519Keypair;
use russh::keys::{HashAlg, PrivateKey, PublicKey};
use russh::server::{Auth, Msg, Session};
use russh::{Channel, ChannelId};
use russh_sftp::protocol::{
Attrs, Data, File as SftpFile, FileAttributes, Handle, Name, OpenFlags, Status, StatusCode,
Version,
};
use tokio::sync::Notify;
use crate::mapping::normalize_relative;
/// The path under `root` of the server path `path`, relative to `cwd`;
/// `None` when it would leave the root.
fn resolve(root: &Path, cwd: &str, path: &str) -> Option<PathBuf> {
let joined = if path.starts_with('/') {
path.to_string()
} else {
format!("{cwd}/{path}")
};
let rel = normalize_relative(&joined)?;
Some(if rel.is_empty() {
root.to_path_buf()
} else {
root.join(rel)
})
}
/// SFTP attributes of a local file, with the POSIX type bits.
fn attrs(meta: &std::fs::Metadata) -> FileAttributes {
FileAttributes {
size: Some(meta.len()),
permissions: Some(if meta.is_dir() { 0o040_755 } else { 0o100_644 }),
mtime: Some(0),
atime: Some(0),
..FileAttributes::default()
}
}
/// A running SFTP server.
pub struct SftpServer {
/// Its address.
pub addr: SocketAddr,
/// Its host key's fingerprint, `SHA256:…`.
pub fingerprint: String,
/// Its host key's algorithm.
pub algorithm: String,
/// Stops it.
stop: Arc<Notify>,
}
impl Drop for SftpServer {
fn drop(&mut self) {
self.stop.notify_waiters();
self.stop.notify_one();
}
}
/// Who may sign in to the SFTP server.
#[derive(Clone)]
struct Credentials {
/// The user name.
user: String,
/// The accepted password.
password: Option<String>,
/// The accepted public key.
key: Option<PublicKey>,
}
/// A fixed host key for the test server, from `seed`.
pub fn host_key(seed: u8) -> PrivateKey {
PrivateKey::from(Ed25519Keypair::from_seed(&[seed; 32]))
}
impl SftpServer {
/// Starts a server for `root` that accepts `user` with `password` or
/// with `key`. `seed` picks the host key, so a test can simulate a
/// changed one.
///
/// # Errors
/// An I/O error when no port can be bound or the runtime fails.
pub fn start(
root: &Path,
user: &str,
password: Option<&str>,
key: Option<PublicKey>,
seed: u8,
) -> std::io::Result<Self> {
let host = host_key(seed);
let fingerprint = host.public_key().fingerprint(HashAlg::Sha256).to_string();
let algorithm = host.public_key().algorithm().to_string();
let config = Arc::new(russh::server::Config {
keys: vec![host],
auth_rejection_time: Duration::from_millis(10),
auth_rejection_time_initial: Some(Duration::from_millis(0)),
inactivity_timeout: Some(Duration::from_secs(60)),
..Default::default()
});
let listener = TcpListener::bind("127.0.0.1:0")?;
listener.set_nonblocking(true)?;
let addr = listener.local_addr()?;
let stop = Arc::new(Notify::new());
let stopped = Arc::clone(&stop);
let creds = Credentials {
user: user.to_string(),
password: password.map(str::to_string),
key,
};
let root = root.to_path_buf();
let runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(2)
.enable_all()
.build()?;
std::thread::Builder::new()
.name("lutra-test-sftp".into())
.spawn(move || {
runtime.block_on(async move {
let Ok(listener) = tokio::net::TcpListener::from_std(listener) else {
return;
};
loop {
let accepted = tokio::select! {
a = listener.accept() => a,
() = stopped.notified() => return,
};
let Ok((socket, _)) = accepted else {
continue;
};
let handler = SshHandler {
creds: creds.clone(),
root: root.clone(),
channels: HashMap::new(),
};
let config = Arc::clone(&config);
tokio::spawn(async move {
if let Ok(session) =
russh::server::run_stream(config, socket, handler).await
{
let _ = session.await;
}
});
}
});
})?;
Ok(Self {
addr,
fingerprint,
algorithm,
stop,
})
}
}
/// One SSH connection.
struct SshHandler {
/// Who may sign in.
creds: Credentials,
/// The served folder.
root: PathBuf,
/// Open session channels, until they ask for SFTP.
channels: HashMap<ChannelId, Channel<Msg>>,
}
impl russh::server::Handler for SshHandler {
type Error = russh::Error;
async fn auth_password(&mut self, user: &str, password: &str) -> Result<Auth, Self::Error> {
Ok(
if user == self.creds.user && self.creds.password.as_deref() == Some(password) {
Auth::Accept
} else {
Auth::reject()
},
)
}
async fn auth_publickey_offered(
&mut self,
user: &str,
key: &PublicKey,
) -> Result<Auth, Self::Error> {
self.auth_publickey(user, key).await
}
async fn auth_publickey(&mut self, user: &str, key: &PublicKey) -> Result<Auth, Self::Error> {
Ok(
if user == self.creds.user
&& self
.creds
.key
.as_ref()
.is_some_and(|k| k.key_data() == key.key_data())
{
Auth::Accept
} else {
Auth::reject()
},
)
}
async fn channel_open_session(
&mut self,
channel: Channel<Msg>,
reply: russh::server::ChannelOpenHandle,
_session: &mut Session,
) -> Result<(), Self::Error> {
self.channels.insert(channel.id(), channel);
reply.accept().await;
Ok(())
}
async fn subsystem_request(
&mut self,
id: ChannelId,
name: &str,
session: &mut Session,
) -> Result<(), Self::Error> {
match (name, self.channels.remove(&id)) {
("sftp", Some(channel)) => {
session.channel_success(id)?;
let handler = SftpHandler {
root: self.root.clone(),
handles: HashMap::new(),
next: 0,
};
tokio::spawn(russh_sftp::server::run(channel.into_stream(), handler));
}
_ => session.channel_failure(id)?,
}
Ok(())
}
}
/// What an SFTP handle refers to.
enum Open {
/// An open file.
File(std::fs::File),
/// A folder listing; `None` once sent.
Dir(Option<Vec<SftpFile>>),
}
/// The SFTP subsystem over a local folder.
struct SftpHandler {
/// The served folder.
root: PathBuf,
/// Open handles.
handles: HashMap<String, Open>,
/// The next handle number.
next: u64,
}
impl SftpHandler {
/// The local path of `path`.
fn path(&self, path: &str) -> Result<PathBuf, StatusCode> {
resolve(&self.root, "/", path).ok_or(StatusCode::PermissionDenied)
}
/// Stores `open` under a fresh handle.
fn add(&mut self, id: u32, open: Open) -> Handle {
self.next += 1;
let handle = format!("h{}", self.next);
self.handles.insert(handle.clone(), open);
Handle { id, handle }
}
}
/// An `Ok` status.
fn ok(id: u32) -> Status {
Status {
id,
status_code: StatusCode::Ok,
error_message: "Ok".into(),
language_tag: "en-US".into(),
}
}
/// A local I/O error as an SFTP status.
fn io_status(e: &std::io::Error) -> StatusCode {
match e.kind() {
std::io::ErrorKind::NotFound => StatusCode::NoSuchFile,
std::io::ErrorKind::PermissionDenied => StatusCode::PermissionDenied,
_ => StatusCode::Failure,
}
}
impl russh_sftp::server::Handler for SftpHandler {
type Error = StatusCode;
fn unimplemented(&self) -> Self::Error {
StatusCode::OpUnsupported
}
async fn init(
&mut self,
_version: u32,
_extensions: HashMap<String, String>,
) -> Result<Version, Self::Error> {
Ok(Version::new())
}
async fn open(
&mut self,
id: u32,
filename: String,
flags: OpenFlags,
_attrs: FileAttributes,
) -> Result<Handle, Self::Error> {
let path = self.path(&filename)?;
let file = std::fs::OpenOptions::new()
.read(flags.contains(OpenFlags::READ))
.write(flags.contains(OpenFlags::WRITE))
.create(flags.contains(OpenFlags::CREATE))
.truncate(flags.contains(OpenFlags::TRUNCATE))
.open(path)
.map_err(|e| io_status(&e))?;
Ok(self.add(id, Open::File(file)))
}
async fn close(&mut self, id: u32, handle: String) -> Result<Status, Self::Error> {
self.handles.remove(&handle);
Ok(ok(id))
}
async fn read(
&mut self,
id: u32,
handle: String,
offset: u64,
len: u32,
) -> Result<Data, Self::Error> {
let Some(Open::File(file)) = self.handles.get_mut(&handle) else {
return Err(StatusCode::Failure);
};
file.seek(SeekFrom::Start(offset))
.map_err(|e| io_status(&e))?;
let mut data = vec![0u8; len.min(256 * 1024) as usize];
let n = file.read(&mut data).map_err(|e| io_status(&e))?;
if n == 0 {
return Err(StatusCode::Eof);
}
data.truncate(n);
Ok(Data { id, data })
}
async fn write(
&mut self,
id: u32,
handle: String,
offset: u64,
data: Vec<u8>,
) -> Result<Status, Self::Error> {
let Some(Open::File(file)) = self.handles.get_mut(&handle) else {
return Err(StatusCode::Failure);
};
file.seek(SeekFrom::Start(offset))
.map_err(|e| io_status(&e))?;
file.write_all(&data).map_err(|e| io_status(&e))?;
Ok(ok(id))
}
async fn stat(&mut self, id: u32, path: String) -> Result<Attrs, Self::Error> {
let meta = std::fs::metadata(self.path(&path)?).map_err(|e| io_status(&e))?;
Ok(Attrs {
id,
attrs: attrs(&meta),
})
}
async fn lstat(&mut self, id: u32, path: String) -> Result<Attrs, Self::Error> {
self.stat(id, path).await
}
async fn fstat(&mut self, id: u32, handle: String) -> Result<Attrs, Self::Error> {
let Some(Open::File(file)) = self.handles.get(&handle) else {
return Err(StatusCode::Failure);
};
let meta = file.metadata().map_err(|e| io_status(&e))?;
Ok(Attrs {
id,
attrs: attrs(&meta),
})
}
async fn setstat(
&mut self,
id: u32,
_path: String,
_attrs: FileAttributes,
) -> Result<Status, Self::Error> {
Ok(ok(id))
}
async fn fsetstat(
&mut self,
id: u32,
_handle: String,
_attrs: FileAttributes,
) -> Result<Status, Self::Error> {
Ok(ok(id))
}
async fn mkdir(
&mut self,
id: u32,
path: String,
_attrs: FileAttributes,
) -> Result<Status, Self::Error> {
std::fs::create_dir(self.path(&path)?).map_err(|e| io_status(&e))?;
Ok(ok(id))
}
async fn opendir(&mut self, id: u32, path: String) -> Result<Handle, Self::Error> {
let dir = self.path(&path)?;
let mut files = Vec::new();
for entry in std::fs::read_dir(dir).map_err(|e| io_status(&e))?.flatten() {
if let Ok(meta) = entry.metadata() {
files.push(SftpFile::new(
entry.file_name().to_string_lossy(),
attrs(&meta),
));
}
}
Ok(self.add(id, Open::Dir(Some(files))))
}
async fn readdir(&mut self, id: u32, handle: String) -> Result<Name, Self::Error> {
let Some(Open::Dir(files)) = self.handles.get_mut(&handle) else {
return Err(StatusCode::Failure);
};
match files.take() {
Some(files) if !files.is_empty() => Ok(Name { id, files }),
_ => Err(StatusCode::Eof),
}
}
async fn realpath(&mut self, id: u32, path: String) -> Result<Name, Self::Error> {
let rel = normalize_relative(&path).ok_or(StatusCode::NoSuchFile)?;
Ok(Name {
id,
files: vec![SftpFile::dummy(format!("/{rel}"))],
})
}
async fn remove(&mut self, id: u32, filename: String) -> Result<Status, Self::Error> {
std::fs::remove_file(self.path(&filename)?).map_err(|e| io_status(&e))?;
Ok(ok(id))
}
async fn rmdir(&mut self, id: u32, path: String) -> Result<Status, Self::Error> {
std::fs::remove_dir(self.path(&path)?).map_err(|e| io_status(&e))?;
Ok(ok(id))
}
}
/// A running FTP server.
pub struct FtpServer {
/// Its address.
pub addr: SocketAddr,
/// Stops it.
stop: Arc<AtomicBool>,
}
impl Drop for FtpServer {
fn drop(&mut self) {
self.stop.store(true, Ordering::SeqCst);
}
}
impl FtpServer {
/// Starts a server for `root` that accepts `user` with `password`.
///
/// # Errors
/// An I/O error when no port can be bound.
pub fn start(root: &Path, user: &str, password: &str) -> std::io::Result<Self> {
let listener = TcpListener::bind("127.0.0.1:0")?;
listener.set_nonblocking(true)?;
let addr = listener.local_addr()?;
let stop = Arc::new(AtomicBool::new(false));
let stopped = Arc::clone(&stop);
let root = root.to_path_buf();
let (user, password) = (user.to_string(), password.to_string());
std::thread::Builder::new()
.name("lutra-test-ftp".into())
.spawn(move || {
while !stopped.load(Ordering::SeqCst) {
match listener.accept() {
Ok((stream, _)) => {
let (root, user, password) =
(root.clone(), user.clone(), password.clone());
let _ = std::thread::Builder::new()
.name("lutra-test-ftp-conn".into())
.spawn(move || {
let _ = ftp_session(stream, &root, &user, &password);
});
}
Err(_) => std::thread::sleep(Duration::from_millis(20)),
}
}
})?;
Ok(Self { addr, stop })
}
}
/// Serves one FTP control connection until QUIT or a closed socket.
fn ftp_session(stream: TcpStream, root: &Path, user: &str, password: &str) -> std::io::Result<()> {
stream.set_nonblocking(false)?;
stream.set_read_timeout(Some(Duration::from_secs(60)))?;
let mut out = stream.try_clone()?;
let mut reader = BufReader::new(stream);
let mut send = |line: &str| out.write_all(format!("{line}\r\n").as_bytes());
send("220 Lutra test FTP")?;
let mut given_user = String::new();
let mut signed_in = false;
let mut cwd = String::from("/");
let mut passive: Option<TcpListener> = None;
loop {
let mut line = String::new();
if reader.read_line(&mut line)? == 0 {
return Ok(());
}
let line = line.trim_end_matches(['\r', '\n']);
let (cmd, arg) = line.split_once(' ').unwrap_or((line, ""));
let cmd = cmd.to_ascii_uppercase();
if !signed_in
&& !matches!(
cmd.as_str(),
"USER" | "PASS" | "QUIT" | "FEAT" | "SYST" | "AUTH"
)
{
send("530 Not logged in")?;
continue;
}
let here = cwd.clone();
let path = |arg: &str| resolve(root, &here, arg);
match cmd.as_str() {
"USER" => {
given_user = arg.to_string();
send("331 Password required")?;
}
"PASS" => {
signed_in = given_user == user && arg == password;
send(if signed_in {
"230 Logged in"
} else {
"530 Login incorrect"
})?;
}
"SYST" => send("215 UNIX Type: L8")?,
"FEAT" | "AUTH" => send("502 Not implemented")?,
"TYPE" | "NOOP" | "OPTS" => send("200 OK")?,
"PWD" => send(&format!("257 \"{cwd}\" is the current directory"))?,
"CWD" => match (path(arg), virtual_path(&here, arg)) {
(Some(p), Some(v)) if p.is_dir() => {
cwd = v;
send("250 OK")?;
}
_ => send("550 No such directory")?,
},
"MKD" => match path(arg).map(std::fs::create_dir) {
Some(Ok(())) => send(&format!("257 \"{arg}\" created"))?,
_ => send("550 Cannot create")?,
},
"SIZE" => match path(arg).and_then(|p| std::fs::metadata(p).ok()) {
Some(m) if m.is_file() => send(&format!("213 {}", m.len()))?,
_ => send("550 No such file")?,
},
"PASV" => {
let l = TcpListener::bind("127.0.0.1:0")?;
let port = l.local_addr()?.port();
passive = Some(l);
send(&format!(
"227 Entering Passive Mode (127,0,0,1,{},{})",
port >> 8,
port & 0xff
))?;
}
"STOR" | "RETR" | "LIST" => {
let Some(listener) = passive.take() else {
send("425 Use PASV first")?;
continue;
};
let target = if cmd == "LIST" && arg.is_empty() {
path(".")
} else {
path(arg)
};
let Some(target) = target else {
send("550 Bad path")?;
continue;
};
if cmd == "RETR" && !target.is_file() {
send("550 No such file")?;
continue;
}
send("150 Opening data connection")?;
let (mut data, _) = listener.accept()?;
let result = match cmd.as_str() {
"STOR" => std::fs::File::create(&target)
.and_then(|mut f| std::io::copy(&mut data, &mut f).map(|_| ())),
"RETR" => std::fs::File::open(&target)
.and_then(|mut f| std::io::copy(&mut f, &mut data).map(|_| ())),
_ => list_dir(&target).and_then(|text| data.write_all(text.as_bytes())),
};
drop(data);
send(if result.is_ok() {
"226 Transfer complete"
} else {
"451 Transfer failed"
})?;
}
"QUIT" => {
send("221 Bye")?;
return Ok(());
}
_ => send("502 Not implemented")?,
}
}
}
/// The FTP path of `arg`, relative to `cwd`, as an absolute path.
fn virtual_path(cwd: &str, arg: &str) -> Option<String> {
let joined = if arg.starts_with('/') {
arg.to_string()
} else {
format!("{cwd}/{arg}")
};
normalize_relative(&joined).map(|r| format!("/{r}"))
}
/// A POSIX `ls -l` style listing of `dir`.
fn list_dir(dir: &Path) -> std::io::Result<String> {
let mut text = String::new();
for entry in std::fs::read_dir(dir)?.flatten() {
let Ok(meta) = entry.metadata() else {
continue;
};
let kind = if meta.is_dir() { 'd' } else { '-' };
text.push_str(&format!(
"{kind}rw-r--r-- 1 lutra lutra {} Jan 01 00:00 {}\r\n",
if meta.is_dir() { 0 } else { meta.len() },
entry.file_name().to_string_lossy()
));
}
Ok(text)
}

View file

@ -0,0 +1,354 @@
//! 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"));
}