Compare commits

..

3 commits

Author SHA1 Message Date
danny
872b5cb258 D1: deployment UI, docs and G17
Some checks are pending
ci / ci (macos-15) (push) Waiting to run
ci / ci (ubuntu-24.04) (push) Waiting to run
ci / ci (windows-2025) (push) Waiting to run
Servers dialog (write-only keychain secret, test connection, unknown and
changed host key), status-bar progress with Cancel, Deployment log tool
window, Upload/Download in the project tree menu and Tools › Deployment.
docs/deployment.md, architecture section, G17 row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 19:14:58 +01:00
danny
f3c9ef9ce3 D1: engine deploy.* commands, deployProgress/deployLog events, upload on save
Servers per project in the config dir (no secrets), secrets in the
keychain via a swappable SecretStore, test connection with the host-key
and missing-secret questions as values, trust host key, upload,
download, cancel, log. One job at a time; uploads on save queue.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 18:44:26 +01:00
danny
0644b044f2 D1: lutra-deploy crate: SFTP, FTP/FTPS and folder transports, keychain secrets, known hosts, path mappings
Pure Rust (D27): russh + russh-sftp with ring, suppaftp with rustls/ring,
keyring (Windows Credential Manager, macOS Keychain). Lutra's own
known-hosts file, refuse on mismatch. In-process SFTP and FTP test
servers behind the testserver feature.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 18:44:26 +01:00
44 changed files with 9512 additions and 36 deletions

1274
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -21,6 +21,7 @@ lutra-search = { path = "crates/search" }
lutra-lsp = { path = "crates/lsp" } lutra-lsp = { path = "crates/lsp" }
lutra-terminal = { path = "crates/terminal" } lutra-terminal = { path = "crates/terminal" }
lutra-vcs = { path = "crates/vcs" } lutra-vcs = { path = "crates/vcs" }
lutra-deploy = { path = "crates/deploy" }
lutra-engine = { path = "crates/engine" } lutra-engine = { path = "crates/engine" }
lutra-control = { path = "crates/control" } lutra-control = { path = "crates/control" }
lutra-mcp = { path = "crates/mcp" } lutra-mcp = { path = "crates/mcp" }

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"));
}

View file

@ -12,6 +12,7 @@ publish.workspace = true
layer = 4 layer = 4
[dependencies] [dependencies]
lutra-deploy.workspace = true
lutra-index.workspace = true lutra-index.workspace = true
lutra-lsp.workspace = true lutra-lsp.workspace = true
lutra-proc.workspace = true lutra-proc.workspace = true
@ -29,6 +30,7 @@ thiserror = "2.0.21"
workspace = true workspace = true
[dev-dependencies] [dev-dependencies]
lutra-deploy = { workspace = true, features = ["testserver"] }
lutra-testkit.workspace = true lutra-testkit.workspace = true
proptest.workspace = true proptest.workspace = true
tempfile.workspace = true tempfile.workspace = true

File diff suppressed because it is too large Load diff

View file

@ -103,10 +103,12 @@ fn save(state: &mut State, command: &str, id: BufferId) -> Result<u64, EngineErr
let was_dirty = open.dirty(); let was_dirty = open.dirty();
open.saved_version = open.version; open.saved_version = open.version;
let version = open.version; let version = open.version;
let path = open.path.clone();
journal::forget(state, id); journal::forget(state, id);
if was_dirty { if was_dirty {
state.emit(Event::DirtyChanged { id, dirty: false }); state.emit(Event::DirtyChanged { id, dirty: false });
} }
super::deploy::saved(state, &path.to_string_lossy().replace('\\', "/"));
Ok(version) Ok(version)
} }

View file

@ -2,6 +2,7 @@
mod app; mod app;
mod buffer; mod buffer;
pub mod deploy;
mod file; mod file;
pub mod git; pub mod git;
pub mod git_blame; pub mod git_blame;
@ -82,6 +83,7 @@ pub fn register_builtins(registry: &mut Registry) -> Result<(), EngineError> {
git_stash::register(registry)?; git_stash::register(registry)?;
git_chunks::register(registry)?; git_chunks::register(registry)?;
git_ignore::register(registry)?; git_ignore::register(registry)?;
deploy::register(registry)?;
Ok(()) Ok(())
} }
@ -92,6 +94,7 @@ pub fn register_builtins(registry: &mut Registry) -> Result<(), EngineError> {
pub(crate) const BUILTIN_COMMAND_IDS: &[&[&str]] = &[ pub(crate) const BUILTIN_COMMAND_IDS: &[&[&str]] = &[
app::COMMAND_IDS, app::COMMAND_IDS,
buffer::COMMAND_IDS, buffer::COMMAND_IDS,
deploy::COMMAND_IDS,
file::COMMAND_IDS, file::COMMAND_IDS,
git::COMMAND_IDS, git::COMMAND_IDS,
git_blame::COMMAND_IDS, git_blame::COMMAND_IDS,

View file

@ -154,6 +154,7 @@ impl Engine {
crate::lsp::pump(state); crate::lsp::pump(state);
cmd::git::pump(state); cmd::git::pump(state);
cmd::git_remote::pump(state); cmd::git_remote::pump(state);
cmd::deploy::pump(state);
Ok(()) Ok(())
})?; })?;
Ok(state.take_events()) Ok(state.take_events())
@ -268,6 +269,13 @@ impl Engine {
self.state.lsp.set_php_lint(php); self.state.lsp.set_php_lint(php);
} }
/// Keeps deployment passwords and passphrases in `store` instead of
/// the OS keychain: for tests, and for hosts that must not touch the
/// keychain.
pub fn set_secret_store(&mut self, store: std::sync::Arc<dyn lutra_deploy::SecretStore>) {
self.state.deploy.set_secret_store(store);
}
/// Read access to the state. /// Read access to the state.
pub fn state(&self) -> &State { pub fn state(&self) -> &State {
&self.state &self.state

View file

@ -209,6 +209,13 @@ pub enum Event {
LspProgress(crate::lsp::LspProgress), LspProgress(crate::lsp::LspProgress),
/// The running fetch, pull, push or clone: `{active, operation?, phase?, percent?}`; `active: false` once it ended (M4.10). /// The running fetch, pull, push or clone: `{active, operation?, phase?, percent?}`; `active: false` once it ended (M4.10).
GitProgress(crate::cmd::git_remote::GitProgress), GitProgress(crate::cmd::git_remote::GitProgress),
/// The running deployment job: `{active, operation?, server?, phase, file?, filesDone, filesTotal, bytesDone, bytesTotal}`; `active: false` once it ended (G17).
DeployProgress(crate::cmd::deploy::DeployProgress),
/// A new line of the deployment log: `{entry: {id, time, level, server, message}}` (G17).
DeployLog {
/// The line.
entry: crate::cmd::deploy::LogEntry,
},
/// Terminal events (`terminalOutput`, `terminalExited`), see [`crate::cmd::terminal`]. /// Terminal events (`terminalOutput`, `terminalExited`), see [`crate::cmd::terminal`].
#[serde(untagged)] #[serde(untagged)]
Terminal(crate::cmd::terminal::TerminalEvent), Terminal(crate::cmd::terminal::TerminalEvent),

View file

@ -53,6 +53,8 @@ pub struct State {
pub git_lines: crate::cmd::git_lines::GitLines, pub git_lines: crate::cmd::git_lines::GitLines,
/// The running fetch, pull, push or clone (M4.10). /// The running fetch, pull, push or clone (M4.10).
pub git_remote: crate::cmd::git_remote::GitRemote, pub git_remote: crate::cmd::git_remote::GitRemote,
/// Deployment jobs, their log and the secret store (G17).
pub deploy: crate::cmd::deploy::DeployState,
/// Number of notifications emitted, for their ids. /// Number of notifications emitted, for their ids.
notifications: u64, notifications: u64,
/// Events queued for the host, oldest first. /// Events queued for the host, oldest first.

View file

@ -0,0 +1,386 @@
//! The `deploy.*` commands through the engine (G17) against the local test
//! servers of `lutra-deploy` (127.0.0.1 only, no real network), with an
//! in-memory secret store instead of the OS keychain: servers and secrets,
//! the host-key question, upload and download with `deployProgress` and
//! `deployLog`, upload on save, cancel, one job at a time, bad params, and
//! that the password never reaches a file, an event, a value or an error.
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::indexing_slicing
)]
use std::path::Path;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use lutra_deploy::MemoryStore;
use lutra_deploy::testserver::{FtpServer, SftpServer};
use lutra_engine::{Engine, EngineConfig, EngineError, Event, run_waiting};
use serde_json::{Value, json};
const PASSWORD: &str = "Otter-Engine-S3cret#";
/// An engine behind a lock, like a host, recording every event and every
/// value and error text.
struct Host {
engine: Mutex<Engine>,
events: Mutex<Vec<Event>>,
said: Mutex<Vec<String>>,
}
impl Host {
fn new(config: &Path) -> Arc<Self> {
let mut engine = Engine::with_config(EngineConfig {
config_dir: config.join("config"),
data_dir: config.join("data"),
})
.unwrap();
engine.set_secret_store(Arc::new(MemoryStore::default()));
Arc::new(Self {
engine: Mutex::new(engine),
events: Mutex::new(Vec::new()),
said: Mutex::new(Vec::new()),
})
}
fn run(&self, command: &str, params: Value) -> Result<Value, EngineError> {
let result = run_waiting(
|| self.engine.lock().unwrap(),
command,
params,
|events| self.events.lock().unwrap().extend_from_slice(events),
)
.map(|o| o.value);
let text = match &result {
Ok(v) => v.to_string(),
Err(e) => format!("{e} {e:?}"),
};
self.said.lock().unwrap().push(text);
result
}
fn pump(&self) {
let events = self.engine.lock().unwrap().pump_vfs().unwrap();
self.events.lock().unwrap().extend(events);
}
fn seen(&self, kind: &str) -> Vec<Value> {
self.events
.lock()
.unwrap()
.iter()
.map(|e| serde_json::to_value(e).unwrap())
.filter(|e| e["kind"] == kind)
.collect()
}
/// Everything the host saw: events, values, errors.
fn transcript(&self) -> String {
let mut text: Vec<String> = self
.events
.lock()
.unwrap()
.iter()
.map(|e| serde_json::to_string(e).unwrap())
.collect();
text.extend(self.said.lock().unwrap().iter().cloned());
text.join("\n")
}
}
/// Every file under `dir`, read as text where possible.
fn all_files(dir: &Path) -> String {
let mut out = String::new();
let Ok(entries) = std::fs::read_dir(dir) else {
return out;
};
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
out.push_str(&all_files(&path));
} else {
out.push_str(&String::from_utf8_lossy(&std::fs::read(&path).unwrap()));
}
}
out
}
fn project(root: &Path) -> std::path::PathBuf {
let p = root.join("project");
std::fs::create_dir_all(p.join("public")).unwrap();
std::fs::write(p.join("public/index.php"), "<?php echo 1;").unwrap();
std::fs::write(p.join("composer.json"), "{}").unwrap();
p
}
#[test]
fn sftp_servers_host_key_upload_download_and_no_leak() {
let tmp = tempfile::tempdir().unwrap();
let remote = tmp.path().join("remote");
std::fs::create_dir_all(&remote).unwrap();
let sftp = SftpServer::start(&remote, "deploy", Some(PASSWORD), None, 11).unwrap();
let host = Host::new(tmp.path());
let proj = project(tmp.path());
host.run("project.open", json!({"path": proj})).unwrap();
assert_eq!(
host.run("deploy.servers", Value::Null).unwrap()["servers"],
json!([])
);
let saved = host
.run(
"deploy.saveServer",
json!({"server": {
"name": "Staging", "kind": "sftp", "host": "127.0.0.1", "port": sftp.addr.port(),
"user": "deploy", "root": "/www", "mappings": [{"local": "public", "remote": "html"}],
}, "secret": PASSWORD}),
)
.unwrap();
let id = saved["server"]["id"].as_str().unwrap().to_string();
assert_eq!(saved["server"]["hasSecret"], true);
assert_eq!(saved["server"]["secretKind"], "password");
let listed = host.run("deploy.servers", Value::Null).unwrap();
assert_eq!(listed["defaultServer"], json!(id));
let first = host.run("deploy.testConnection", json!({})).unwrap();
assert_eq!(first["ok"], false);
assert_eq!(first["hostKey"]["status"], "unknown");
assert_eq!(first["hostKey"]["fingerprint"], json!(sftp.fingerprint));
host.run(
"deploy.trustHostKey",
json!({"id": id, "algorithm": first["hostKey"]["algorithm"], "fingerprint": first["hostKey"]["fingerprint"]}),
)
.unwrap();
let second = host
.run("deploy.testConnection", json!({"id": id}))
.unwrap();
assert_eq!(second["ok"], true, "{second}");
let up = host.run("deploy.upload", json!({"paths": [""]})).unwrap();
assert_eq!(up["files"], 1, "{up}");
assert_eq!(up["skipped"], 1, "composer.json is not mapped: {up}");
assert_eq!(
std::fs::read_to_string(remote.join("www/html/index.php")).unwrap(),
"<?php echo 1;"
);
let progress = host.seen("deployProgress");
assert_eq!(progress.last().unwrap()["active"], false);
assert_eq!(progress.last().unwrap()["operation"], "upload");
assert_eq!(progress.last().unwrap()["filesDone"], 1);
assert!(host.seen("deployLog").iter().any(|e| {
e["entry"]["message"]
.as_str()
.unwrap()
.contains("public/index.php")
}));
std::fs::write(remote.join("www/html/index.php"), "<?php echo 2;").unwrap();
let down = host
.run("deploy.download", json!({"paths": ["public/index.php"]}))
.unwrap();
assert_eq!(down["files"], 1);
assert_eq!(
std::fs::read_to_string(proj.join("public/index.php")).unwrap(),
"<?php echo 2;"
);
let log = host.run("deploy.log", Value::Null).unwrap();
assert!(log["entries"].as_array().unwrap().len() >= 3);
host.run("deploy.clearLog", Value::Null).unwrap();
assert_eq!(
host.run("deploy.log", Value::Null).unwrap()["entries"],
json!([])
);
let wrong = host
.run("deploy.setSecret", json!({"id": id, "secret": "nope"}))
.unwrap();
assert_eq!(wrong["stored"], true);
let refused = host.run("deploy.testConnection", json!({})).unwrap();
assert_eq!(refused["authFailed"], true, "{refused}");
host.run("deploy.clearSecret", json!({"id": id})).unwrap();
let missing = host.run("deploy.testConnection", json!({})).unwrap();
assert_eq!(missing["needsSecret"], "password", "{missing}");
assert!(
host.run("deploy.upload", json!({"paths": ["public"]}))
.is_err()
);
assert!(
host.run("deploy.removeServer", json!({"id": id})).unwrap()["removed"]
.as_bool()
.unwrap()
);
host.pump();
let transcript = host.transcript();
assert!(
!transcript.contains(PASSWORD),
"the password leaked into events, values or errors"
);
let files = all_files(&tmp.path().join("config")) + &all_files(&tmp.path().join("data"));
assert!(
files.contains("known_hosts") || files.contains("SHA256:"),
"the config dir holds the deployment files"
);
assert!(
!files.contains(PASSWORD),
"the password leaked into the config or data directory"
);
}
#[test]
fn ftp_upload_on_save() {
let tmp = tempfile::tempdir().unwrap();
let remote = tmp.path().join("remote");
std::fs::create_dir_all(&remote).unwrap();
let ftp = FtpServer::start(&remote, "deploy", PASSWORD).unwrap();
let host = Host::new(tmp.path());
let proj = project(tmp.path());
host.run("project.open", json!({"path": proj})).unwrap();
let saved = host
.run(
"deploy.saveServer",
json!({"server": {
"name": "Shared host", "kind": "ftp", "host": "127.0.0.1", "port": ftp.addr.port(),
"user": "deploy", "root": "/", "uploadOnSave": true, "excluded": ["composer.json"],
}, "secret": PASSWORD}),
)
.unwrap();
assert_eq!(saved["server"]["uploadOnSave"], true);
let opened = host
.run("file.open", json!({"path": "public/index.php"}))
.unwrap();
let id = opened["id"].as_u64().unwrap();
host.run(
"buffer.edit",
json!({"id": id, "baseVersion": 1, "edits": [{"from": 0, "to": 0, "insert": "// saved\n"}]}),
)
.unwrap();
host.run("file.save", json!({"id": id})).unwrap();
let deadline = Instant::now() + Duration::from_secs(20);
let uploaded = remote.join("public/index.php");
while Instant::now() < deadline
&& !host
.seen("deployProgress")
.iter()
.any(|p| p["active"] == false)
{
host.pump();
std::thread::sleep(Duration::from_millis(20));
}
assert_eq!(
std::fs::read_to_string(&uploaded).unwrap(),
"// saved\n<?php echo 1;"
);
assert!(!host.transcript().contains(PASSWORD));
}
#[test]
fn sftp_cancel_and_one_job_at_a_time() {
let tmp = tempfile::tempdir().unwrap();
let remote = tmp.path().join("remote");
std::fs::create_dir_all(&remote).unwrap();
let sftp = SftpServer::start(&remote, "deploy", Some(PASSWORD), None, 12).unwrap();
let host = Host::new(tmp.path());
let proj = project(tmp.path());
std::fs::write(proj.join("big.bin"), vec![1u8; 48 * 1024 * 1024]).unwrap();
host.run("project.open", json!({"path": proj})).unwrap();
let saved = host
.run(
"deploy.saveServer",
json!({"server": {
"name": "Big", "kind": "sftp", "host": "127.0.0.1", "port": sftp.addr.port(),
"user": "deploy", "root": "",
}, "secret": PASSWORD}),
)
.unwrap();
let id = saved["server"]["id"].as_str().unwrap().to_string();
host.run(
"deploy.trustHostKey",
json!({"id": id, "algorithm": sftp.algorithm, "fingerprint": sftp.fingerprint}),
)
.unwrap();
let engine_host = Arc::clone(&host);
let worker =
std::thread::spawn(move || engine_host.run("deploy.upload", json!({"paths": ["big.bin"]})));
let deadline = Instant::now() + Duration::from_secs(30);
let mut cancelled = false;
while Instant::now() < deadline && !cancelled {
host.pump();
let running = host
.seen("deployProgress")
.iter()
.any(|p| p["active"] == true && p["bytesDone"].as_u64().unwrap_or(0) > 0);
if running {
let second = host.run("deploy.testConnection", json!({}));
assert!(
second.is_err(),
"a second job must be refused while one runs"
);
assert_eq!(
host.run("deploy.cancel", Value::Null).unwrap()["cancelled"],
true
);
cancelled = true;
}
std::thread::sleep(Duration::from_millis(5));
}
assert!(cancelled, "the upload never reported progress");
let err = worker.join().unwrap().unwrap_err();
assert!(err.to_string().contains("cancelled"), "{err}");
assert!(!host.transcript().contains(PASSWORD));
}
#[test]
fn bad_params_and_no_project() {
let tmp = tempfile::tempdir().unwrap();
let host = Host::new(tmp.path());
assert!(matches!(
host.run("deploy.servers", Value::Null),
Err(EngineError::Disabled(_))
));
let proj = project(tmp.path());
host.run("project.open", json!({"path": proj})).unwrap();
let bad = [
(
"deploy.saveServer",
json!({"server": {"name": "x", "kind": "sftp", "host": "-x", "user": "u"}}),
),
(
"deploy.saveServer",
json!({"server": {"name": "x", "kind": "ftp", "host": "h", "user": "u", "password": "p"}}),
),
(
"deploy.saveServer",
json!({"server": {"name": "x", "kind": "gopher", "host": "h", "user": "u"}}),
),
("deploy.setDefault", json!({"id": "missing"})),
("deploy.upload", json!({"paths": []})),
("deploy.upload", json!({"paths": ["a"], "id": "missing"})),
(
"deploy.trustHostKey",
json!({"id": "missing", "algorithm": "a", "fingerprint": "b"}),
),
];
for (command, params) in bad {
let err = host.run(command, params.clone()).unwrap_err();
assert_eq!(err.code(), "invalid_params", "{command} {params}: {err}");
}
let none = host
.run("deploy.upload", json!({"paths": ["public"]}))
.unwrap_err();
assert!(
none.to_string().contains("no default deployment server"),
"{none}"
);
assert!(matches!(
host.run("deploy.cancel", Value::Null),
Err(EngineError::Disabled(_))
));
}

View file

@ -14,6 +14,7 @@ crates/engine (L4) command registry, workspace state, guard, recovery jou
crates/lsp (L3) LSP client (Phpactor first), later DAP client crates/lsp (L3) LSP client (Phpactor first), later DAP client
crates/terminal (L3) pseudo-terminal sessions (portable-pty) crates/terminal (L3) pseudo-terminal sessions (portable-pty)
crates/vcs (L3) Git through the installed git command crates/vcs (L3) Git through the installed git command
crates/deploy (L3) deployment: SFTP, FTP/FTPS, folder; keychain secrets, known hosts
crates/index (L2) symbol index, usages, later type inference crates/index (L2) symbol index, usages, later type inference
crates/vfs (L2) project model, file watching, ignore rules, encodings crates/vfs (L2) project model, file watching, ignore rules, encodings
crates/search (L2) find in files (ignore + grep-searcher crates) crates/search (L2) find in files (ignore + grep-searcher crates)
@ -752,6 +753,15 @@ Watched directories: the VFS watcher is non-recursive, so `project.children` wat
- **Checked in the app** (debug build with the bundled UI, Windows 11, git 2.47.1, control port 7949, a demo repository with two stashes on two branches and a modified file; commands and screenshots only, no input): `git.stashList` 143–157 ms, `git.stash` 640 ms, a pop 366 ms, each including the control round trip. The refusals came back as written above: applying onto a modified file ("would be overwritten" with git's text), nothing to stash (with the untracked hint), a missing entry. Screenshots of the list (`stash@{n}`, WIP text, branch, date; Drop, Pop, Apply) and of the form, plus the two new buttons in the Changes bar, read in the dark theme. - **Checked in the app** (debug build with the bundled UI, Windows 11, git 2.47.1, control port 7949, a demo repository with two stashes on two branches and a modified file; commands and screenshots only, no input): `git.stashList` 143–157 ms, `git.stash` 640 ms, a pop 366 ms, each including the control round trip. The refusals came back as written above: applying onto a modified file ("would be overwritten" with git's text), nothing to stash (with the untracked hint), a missing entry. Screenshots of the list (`stash@{n}`, WIP text, branch, date; Drop, Pop, Apply) and of the form, plus the two new buttons in the Changes bar, read in the dark theme.
- **Tests:** `crates/vcs/tests/stash.rs` (6 tests against real temp repositories: stash with and without a message, list order and hashes, apply, pop by an abbreviated hash, drop; untracked files only with the flag, including a nested folder and a name starting with `-`; no commit and nothing to stash; a missing and a changed entry, a bad hash; overwritten local changes and untracked files in the way, both leaving the work tree and the entry alone; a conflicted pop keeping the entry and blocking the next stash and apply); unit and property tests in `stash.rs` (list and subject parsing, message cleaning, unmerged counting, hints, chunking); `crates/engine/tests/git_stash.rs` (the command shapes, `gitChanged` only after changes, a changed hash, an empty stash, bad params); Vitest `ui/tests/vcs/Stash.test.ts` (13 tests: parsing, the form with `flushDirty` first, a blank message, a refusal, a failed save, Escape and focus, the list with keyboard and apply, a conflicted pop, the drop question, an empty stash, a failed read, the commands' registration, the Changes window's buttons). - **Tests:** `crates/vcs/tests/stash.rs` (6 tests against real temp repositories: stash with and without a message, list order and hashes, apply, pop by an abbreviated hash, drop; untracked files only with the flag, including a nested folder and a name starting with `-`; no commit and nothing to stash; a missing and a changed entry, a bad hash; overwritten local changes and untracked files in the way, both leaving the work tree and the entry alone; a conflicted pop keeping the entry and blocking the next stash and apply); unit and property tests in `stash.rs` (list and subject parsing, message cleaning, unmerged counting, hints, chunking); `crates/engine/tests/git_stash.rs` (the command shapes, `gitChanged` only after changes, a changed hash, an empty stash, bad params); Vitest `ui/tests/vcs/Stash.test.ts` (13 tests: parsing, the form with `flushDirty` first, a blank message, a refusal, a failed save, Escape and focus, the list with keyboard and apply, a conflicted pop, the drop question, an empty stash, a failed read, the commands' registration, the Changes window's buttons).
### Deployment (C9 D1)
- **Owner decision D27 (2026-11-02):** pure-Rust libraries (`russh` + `russh-sftp` with `ring`, `suppaftp` with `rustls`/`ring`, `keyring`); secrets only in the OS keychain. User guide: [`deployment.md`](deployment.md).
- **Crate (`crates/deploy`, L3):** `config` (servers per project, `deny_unknown_fields` so a hand-written `password` field is refused), `mapping` (longest local prefix wins, no `..`, excluded paths and `*`/`?` name patterns; unit and property tests), `secrets` (`SecretStore` trait, `Keychain`, `MemoryStore`; `Secret` debug-prints as `***`), `known_hosts` (Lutra's own file; unknown → `HostKeyUnknown`, different → `HostKeyMismatch`, checked before any credential is sent), `remote` (the `Remote` trait: `mkdir_all`, `put`, `get`, `kind`, `list`, `close`; the extension point for D2), `sftp`, `ftp`, `job` (plan, connect once, transfer file by file; a `Monitor` holds progress and log lines; a `Cancel` flag is checked between 32 KiB chunks and races every SFTP wait). Every library error passes `DeployError::redacted` with the secret in use. Names from remote listings are checked before they become local paths.
- **Test servers** (feature `testserver`, 127.0.0.1 only): a russh SFTP server over a folder with a fixed Ed25519 host key, and a small FTP server. Example `testserver` runs both for live checks.
- **Engine (`cmd/deploy.rs`):** configuration at `<config_dir>/deployment/<project-id>.json`, known hosts at `<config_dir>/deployment/known_hosts`, keychain account `<project-id>/<server-id>/<password|passphrase>`. Commands: `deploy.servers`, `deploy.saveServer {server, secret?}`, `deploy.removeServer`, `deploy.setDefault`, `deploy.setSecret`, `deploy.clearSecret`, `deploy.testConnection {id?}` (an unknown or changed host key, a missing secret or a refused sign-in is a value with `ok: false`), `deploy.trustHostKey {id, algorithm, fingerprint, replace?}`, `deploy.upload {paths, id?}`, `deploy.download {paths, id?}`, `deploy.cancel`, `deploy.log`, `deploy.clearLog`. Jobs are pending outcomes on a `lutra-deploy` thread; one at a time. `file.save` and `file.saveAll` queue uploads on save, which the pump starts and finishes. Events: `deployProgress`, `deployLog {entry}`. `Engine::set_secret_store` swaps the keychain (tests).
- **UI (`ui/src/components/deploy/`):** `deploy.ts` store, `DeployStatusItem.vue` (progress, Cancel, the `deployment.*` UI commands), `DeploymentDialog.vue` (exclusive modal `deployment`; write-only secret field; host-key question), `DeploymentLogToolWindow.vue` (bottom panel). The project tree's context menu gains Upload/Download for files and folders.
- **Tests:** `crates/deploy/tests/transfer.rs` (7: host key first use and mismatch, password and key sign-in, upload, download, cancel, FTP, unreachable), `crates/engine/tests/deploy.rs` (4, including a grep of every event, value, error and the config and data directories for the test password), `ui/tests/deploy/Deploy.test.ts` (12).
## Decisions (M0) ## Decisions (M0)
| Question | Decision | Why | | Question | Decision | Why |
@ -772,6 +782,7 @@ Watched directories: the VFS watcher is non-recursive, so `project.children` wat
| Date | Change | Summary | | Date | Change | Summary |
|---|---|---| |---|---|---|
| 2026-11-06 | minor | C9 D1: `crates/deploy` (L3), `deploy.*` commands, deployment events and UI |
| 2026-11-02 | minor | C9 F1: `Kind::Path` and `check_path` (one validator for `settings.set` and the dialog, `path` in `settings.list`), `lutra_proc::spawn_detached` for the CLI's desktop start | | 2026-11-02 | minor | C9 F1: `Kind::Path` and `check_path` (one validator for `settings.set` and the dialog, `path` in `settings.list`), `lutra_proc::spawn_detached` for the CLI's desktop start |
| 2026-10-31 | minor | C7 B1: app identifier `com.tyrveil.lutra` with the one-time copy from `dev.lutra.desktop`, NSIS and DMG installers, `cargo xtask bundle` | | 2026-10-31 | minor | C7 B1: app identifier `com.tyrveil.lutra` with the one-time copy from `dev.lutra.desktop`, NSIS and DMG installers, `cargo xtask bundle` |
| 2026-10-31 | minor | C7 A3: `git.mergeMessage` and the merge message prefill, `git.ignore` and the Changes context menu, gutter markers kept on their line, per-module `COMMAND_IDS` for the command listing test | | 2026-10-31 | minor | C7 A3: `git.mergeMessage` and the merge message prefill, `git.ignore` and the Changes context menu, gutter markers kept on their line, per-module `COMMAND_IDS` for the command listing test |

54
docs/deployment.md Normal file
View file

@ -0,0 +1,54 @@
# Deployment
> **Status:** Deployment v1 (C9 D1, owner decision D27): servers per project, upload, download, upload on save, test connection, host keys, Deployment log. Remote browser, compare and sync follow in D2 · **Last updated:** 2026-11-06
Lutra uploads a PHP project to a server and downloads files back, the way people deploy shared hosting and small servers every day.
## Servers
Open **Tools › Deployment › Servers…**. Each project has its own list; one is the default server (the only one is the default automatically).
| Field | Meaning |
|---|---|
| Name | Shown in the log, the status bar and toasts. |
| Type | **SFTP** (SSH), **FTPS** (FTP with explicit TLS, certificates checked against the system's trusted roots), **FTP** (unencrypted: everything, including the password, crosses the network in clear text; prefer SFTP or FTPS), or a **local or mounted folder**. |
| Host, Port, User | Port defaults to 22 (SFTP) or 21 (FTP, FTPS). |
| Sign in with | SFTP: password, private key file (with an optional passphrase), or the SSH agent (Pageant or the OpenSSH Authentication Agent on Windows, `SSH_AUTH_SOCK` on macOS). FTP and FTPS: password. |
| Root folder | The server folder everything is relative to; empty for the login folder. For a folder server: the absolute local folder. |
| Path mappings | Local folder (project-relative, empty for the root) → remote folder (relative to the root, or absolute with a leading `/`). The longest matching local folder wins. Without mappings the project root goes to the root. Files outside every mapping are skipped. |
| Excluded paths | One per line. A path excludes itself and everything below it (`var/cache`); a pattern with `*` or `?` matches file and folder names (`*.log`). `.git`, `.svn` and `.hg` are never deployed. |
| Upload on save | Off by default. When on, every saved file that maps to the server is uploaded in the background. |
The configuration lives in Lutra's config directory (`deployment/<project-id>.json`), not in the project, and never contains a password.
## Passwords and passphrases
- They are stored **only in the operating system's keychain**: the Windows Credential Manager or the macOS Keychain, under the service "Lutra Deployment".
- Lutra never writes them to a file, a log, the recovery journal, an event, MCP or control-channel output, or an error message. The password field in the dialog is write-only: after Save it empties again and says "Stored in the system keychain".
- Removing a server removes its keychain entries.
- Linux has no keychain store yet; there, use the SSH agent or a key without a passphrase.
## Host keys (SFTP)
On the first connection, **Test Connection** shows the server's key fingerprint (`ssh-ed25519 SHA256:…`). Compare it with the one from the server's administrator, then choose **Trust and Connect**. Trusted keys are kept in Lutra's own known-hosts file (`deployment/known_hosts` in the config directory); `~/.ssh/known_hosts` is neither read nor changed.
If the key later differs, Lutra **refuses to connect** and says so: someone may be intercepting the connection. Only if you know the server was reinstalled, test the connection and choose **Replace the Trusted Key**.
Uploads and downloads never ask about a key; they fail with a message pointing to Test Connection.
## Upload and download
- **Project tree:** right-click a file or folder › **Upload to Server** / **Download from Server** (default server).
- **Tools › Deployment:** Upload Current File, Download Current File, Upload Project, Test Connection, Deployment Log.
- The same actions are in the command palette. Lutra assigns no default shortcuts; bind them in the keymap.
- Uploads send what is on disk; save first. Downloads replace the local files.
- While a job runs, the status bar shows it with a Cancel button. One job runs at a time; uploads on save wait for the running one.
- Every file, skip and error goes to the **Deployment** log in the bottom panel (newest 1,000 lines). A failing file is logged and the job goes on; a lost connection or a refused sign-in stops it. Each error says what to do.
## For agents and scripts
The `deploy.*` engine commands (`deploy.servers`, `deploy.saveServer`, `deploy.setSecret`, `deploy.testConnection`, `deploy.trustHostKey`, `deploy.upload`, `deploy.download`, `deploy.cancel`, `deploy.log`, …) work over the control channel and MCP like every command; see `docs/architecture.md` › Deployment. Their results never contain a secret.
## Not yet (D2)
A remote file browser, opening and editing remote files, compare with the server, and sync with a preview.

View file

@ -22,7 +22,7 @@ Every known shortfall, ranked. This is the work list: unless the owner gives a t
| G14 | Localization: English and German interface: ~~English message catalog for UI strings~~ (M1.11), engine messages (`EngineError.message`) as message keys, language switch, plurals, German catalog | later | ~38 h | | G14 | Localization: English and German interface: ~~English message catalog for UI strings~~ (M1.11), engine messages (`EngineError.message`) as message keys, language switch, plurals, German catalog | later | ~38 h |
| G15 | Database tools, HTTP client, Docker and remote | later | ~430 h | | G15 | Database tools, HTTP client, Docker and remote | later | ~430 h |
| G16 | Plug-ins and ecosystem | later | ~120 h | | G16 | Plug-ins and ecosystem | later | ~120 h |
| G17 | Deployment: servers (FTP, SFTP, local/mounted), path mappings, upload/download, compare with server, edit remote files, save before deploy, optional upload on save per server (owner wish 2026-10-18; needs credential storage and network access, §4) | M7 | ~120 h | | G17 | Deployment: **v1 done (C9 D1)**: SFTP, FTP, FTPS and folder servers per project, path mappings, excluded paths, upload/download of files and folders, upload on save (off by default), test connection, host-key check, keychain secrets, Deployment log. Open (D2): remote browser, edit remote files, compare with server, sync with preview | M7 | ~40 h |
| G18 | Editor productivity: Local History, live and file templates, postfix completion, code generation, intention actions, auto import, scratch files, macros, compare files/folders, TODO view, language injections, code style/formatter | M6–M7 | ~400 h | | G18 | Editor productivity: Local History, live and file templates, postfix completion, code generation, intention actions, auto import, scratch files, macros, compare files/folders, TODO view, language injections, code style/formatter | M6–M7 | ~400 h |
| G19 | Remote development and Dev Containers | M9 | ~200 h | | G19 | Remote development and Dev Containers | M9 | ~200 h |
| G20 | AI features (chat, inline prompts, commit messages); MCP server is G7/M3 | M9 | ~60 h | | G20 | AI features (chat, inline prompts, commit messages); MCP server is G7/M3 | M9 | ~60 h |
@ -55,6 +55,7 @@ G6 progress: M3.2 landed the LSP client library in `crates/lsp`: JSON-RPC framin
| Date | Change | Summary | | Date | Change | Summary |
|---|---|---| |---|---|---|
| 2026-11-06 | patch | G17: deployment v1 (C9 D1); remote browser, compare and sync left for D2 |
| 2026-11-01 | patch | G8: chunk staging, unstaging and revert from the gutter and the diff viewer (C8 C3) | | 2026-11-01 | patch | G8: chunk staging, unstaging and revert from the gutter and the diff viewer (C8 C3) |
| 2026-10-31 | patch | C7 review: stray revision rows moved here; installers (B1, B2) and the alpha checklist (A2) change no gap | | 2026-10-31 | patch | C7 review: stray revision rows moved here; installers (B1, B2) and the alpha checklist (A2) change no gap |
| 2026-10-31 | patch | A1: macOS system menus (G3), Retina region for the macOS screenshot fallback | | 2026-10-31 | patch | A1: macOS system menus (G3), Retina region for the macOS screenshot fallback |

View file

@ -20,7 +20,8 @@
* - **Navigation (M3.5):** the definition picker and the inline rename field * - **Navigation (M3.5):** the definition picker and the inline rename field
* are exclusive modals too. So is the branches popup (M4.5), and so are * are exclusive modals too. So is the branches popup (M4.5), and so are
* "Clone Repository…" and "Manage Remotes…" (M4.10), the merge * "Clone Repository…" and "Manage Remotes…" (M4.10), the merge
* dialog (M4.8) and the stash dialog (M4.9). * dialog (M4.8) and the stash dialog (M4.9), and the deployment servers
* dialog (G17).
*/ */
import { defineStore } from "pinia"; import { defineStore } from "pinia";
import { ref, watch } from "vue"; import { ref, watch } from "vue";
@ -38,6 +39,7 @@ export type ModalId =
| "branches" | "branches"
| "clone" | "clone"
| "remotes" | "remotes"
| "deployment"
| "merge" | "merge"
| "stash"; | "stash";

View file

@ -21,6 +21,12 @@ export interface UiCommandSpec {
/** The frontend's UI commands. */ /** The frontend's UI commands. */
export const UI_COMMANDS: readonly UiCommandSpec[] = [ export const UI_COMMANDS: readonly UiCommandSpec[] = [
{ id: "deployment.download", labelKey: "command.deployment.download", menuPath: "Tools/Deployment/Download Current File" },
{ id: "deployment.servers", labelKey: "command.deployment.servers", menuPath: "Tools/Deployment/Servers…" },
{ id: "deployment.showLog", labelKey: "command.deployment.showLog", menuPath: "Tools/Deployment/Deployment Log" },
{ id: "deployment.testConnection", labelKey: "command.deployment.testConnection", menuPath: "Tools/Deployment/Test Connection" },
{ id: "deployment.upload", labelKey: "command.deployment.upload", menuPath: "Tools/Deployment/Upload Current File" },
{ id: "deployment.uploadProject", labelKey: "command.deployment.uploadProject", menuPath: "Tools/Deployment/Upload Project" },
{ id: "editor.reveal", labelKey: "command.editor.reveal" }, { id: "editor.reveal", labelKey: "command.editor.reveal" },
{ id: "editor.complete", labelKey: "command.editor.complete", menuPath: "Edit/Complete Code" }, { id: "editor.complete", labelKey: "command.editor.complete", menuPath: "Edit/Complete Code" },
{ id: "editor.quickDocumentation", labelKey: "command.editor.quickDocumentation", menuPath: "Edit/Quick Documentation", defaultShortcut: "Mod+I" }, { id: "editor.quickDocumentation", labelKey: "command.editor.quickDocumentation", menuPath: "Edit/Quick Documentation", defaultShortcut: "Mod+I" },

View file

@ -3,12 +3,14 @@
* Status bar along the bottom of the main window. Shows the product name and * Status bar along the bottom of the main window. Shows the product name and
* version, or the last engine error, and on the right the language server's * version, or the last engine error, and on the right the language server's
* progress (H3), a running fetch, pull, push or clone or the commits * progress (H3), a running fetch, pull, push or clone or the commits
* ahead and behind (M4.10), and the Git branch (M4.5). * ahead and behind (M4.10), a running deployment job (G17), and the Git
* branch (M4.5).
*/ */
import { computed } from "vue"; import { computed } from "vue";
import { t } from "../i18n"; import { t } from "../i18n";
import { useAppStore } from "../stores/app"; import { useAppStore } from "../stores/app";
import LspProgressItem from "./problems/LspProgressItem.vue"; import LspProgressItem from "./problems/LspProgressItem.vue";
import DeployStatusItem from "./deploy/DeployStatusItem.vue";
import BranchStatusItem from "./vcs/branches/BranchStatusItem.vue"; import BranchStatusItem from "./vcs/branches/BranchStatusItem.vue";
import RemoteStatusItem from "./vcs/remote/RemoteStatusItem.vue"; import RemoteStatusItem from "./vcs/remote/RemoteStatusItem.vue";
@ -31,6 +33,7 @@ const label = computed(() => {
> >
<span>{{ label }}</span> <span>{{ label }}</span>
<LspProgressItem /> <LspProgressItem />
<DeployStatusItem />
<RemoteStatusItem /> <RemoteStatusItem />
<BranchStatusItem /> <BranchStatusItem />
</footer> </footer>

View file

@ -0,0 +1,233 @@
<script setup lang="ts">
/**
* Status-bar item of deployment (G17): while a test, upload or download
* runs, the server, the file count and the current file from
* `deployProgress`, and a Cancel button (`deploy.cancel`). Hidden
* otherwise.
*
* The item supplies the deployment UI commands, so they work wherever the
* status bar is shown (none has a default shortcut):
* - `deployment.upload {path?}` and `deployment.download {path?}`: the
* given project path (file or folder) or the active editor's file, to or
* from the default server. Without a default server the servers dialog
* opens instead;
* - `deployment.uploadProject`: the whole project;
* - `deployment.testConnection`: opens the servers dialog and tests the
* default server, so a host key can be checked there;
* - `deployment.servers`: opens the servers dialog.
* All are enabled with a project and while no job runs. It also mounts
* the dialog.
*/
import { LoaderCircle, X } from "@lucide/vue";
import { computed, onBeforeUnmount, onMounted } from "vue";
import { useCommandsStore } from "../../commands/store";
import { t } from "../../i18n";
import { useEditorStore } from "../../stores/editor";
import { useNotificationsStore } from "../../stores/notifications";
import { useProjectStore } from "../../stores/project";
import DeploymentDialog from "./DeploymentDialog.vue";
import { useDeployStore } from "./deploy";
const deploy = useDeployStore();
const commands = useCommandsStore();
const editor = useEditorStore();
const project = useProjectStore();
const notifications = useNotificationsStore();
/** The running job's text, or null while none runs. */
const progressText = computed(() => {
const p = deploy.progress;
if (p === null) {
return null;
}
const operation =
p.operation === "test"
? t("deploy.operation.test")
: p.operation === "upload"
? t("deploy.operation.upload")
: t("deploy.operation.download");
if (p.filesTotal === 0) {
return t("deploy.progress.short", { operation, server: p.server });
}
return t("deploy.progress.files", {
operation,
server: p.server,
done: p.filesDone,
total: p.filesTotal,
});
});
/**
* The path a command acts on: its `path` param, else the active file.
*
* @param params - the command's params.
*/
function pathOf(params: unknown): string | null {
const given = (params as { path?: unknown } | null)?.path;
if (typeof given === "string") {
return given;
}
return editor.activeTab?.path ?? null;
}
/**
* Runs `action` against the default server, or opens the servers dialog
* when there is none.
*
* @param action - what to do with the default server.
*/
async function withTarget(action: () => Promise<unknown>): Promise<void> {
if (deploy.servers.length === 0) {
await deploy.load();
}
if (deploy.target === null) {
notifications.notify({ level: "warning", messageKey: "notification.deploy.noServer" });
await deploy.showDialog();
return;
}
await action();
}
/** Withdraws the command handlers; filled while mounted. */
let withdraw: (() => void)[] = [];
onMounted(() => {
deploy.start();
if (project.root !== null) {
void deploy.load();
}
const idle = (): boolean => project.root !== null && !deploy.busy;
withdraw = [
commands.provide("deployment.upload", {
run: async (params) => {
const path = pathOf(params);
if (path !== null) {
await withTarget(() => deploy.upload([path]));
}
},
enabled: idle,
}),
commands.provide("deployment.download", {
run: async (params) => {
const path = pathOf(params);
if (path !== null) {
await withTarget(() => deploy.download([path]));
}
},
enabled: idle,
}),
commands.provide("deployment.uploadProject", {
run: () => withTarget(() => deploy.upload([""])),
enabled: idle,
}),
commands.provide("deployment.testConnection", {
run: async () => {
await deploy.showDialog();
const target = deploy.target;
if (target !== null) {
await deploy.testConnection(target.id);
}
},
enabled: idle,
}),
commands.provide("deployment.servers", {
run: () => deploy.showDialog(),
enabled: () => project.root !== null,
}),
];
});
onBeforeUnmount(() => {
for (const fn of withdraw) {
fn();
}
withdraw = [];
deploy.stop();
});
</script>
<template>
<span
v-if="progressText !== null"
class="deploy-item"
role="status"
:title="deploy.progress?.file || progressText"
data-test="deploy-progress"
>
<LoaderCircle
class="deploy-item__icon"
:size="12"
aria-hidden="true"
/>
<span class="deploy-item__text">{{ progressText }}</span>
<button
type="button"
class="deploy-item__cancel"
:title="t('deploy.cancel')"
:aria-label="t('deploy.cancel')"
data-test="deploy-cancel"
@click="deploy.cancel()"
>
<X
:size="12"
aria-hidden="true"
/>
</button>
</span>
<DeploymentDialog />
</template>
<style scoped>
.deploy-item {
display: inline-flex;
margin-left: auto;
align-items: center;
gap: 4px;
padding: 0 6px;
min-width: 0;
max-width: 35%;
white-space: nowrap;
}
.deploy-item__text {
overflow: hidden;
text-overflow: ellipsis;
}
.deploy-item__icon {
flex: none;
animation: deploy-item-spin 1.2s linear infinite;
}
@media (prefers-reduced-motion: reduce) {
.deploy-item__icon {
animation: none;
}
}
@keyframes deploy-item-spin {
to {
transform: rotate(360deg);
}
}
.deploy-item__cancel {
display: inline-flex;
align-items: center;
padding: 0 2px;
font: inherit;
color: inherit;
background: transparent;
border: none;
border-radius: var(--lt-radius, 4px);
cursor: pointer;
}
.deploy-item__cancel:hover {
background: color-mix(in srgb, var(--lt-statusbar-fg) 12%, transparent);
}
.deploy-item__cancel:focus-visible {
outline: 2px solid var(--lt-accent);
}
</style>

View file

@ -0,0 +1,867 @@
<script setup lang="ts">
/**
* "Deployment Servers…" (G17): the project's servers on the left, the
* selected one's settings on the right.
*
* - **Fields:** name, type (SFTP, FTP, FTPS, local or mounted folder),
* host, port, user, root folder, sign-in (password, key file with an
* optional passphrase, or the SSH agent for SFTP), path mappings,
* excluded paths (one per line), upload on save (off by default) and
* "default server".
* - **Secrets:** the password or passphrase field is write-only. A typed
* value goes to the engine with Save and is kept only in the OS
* keychain; the field then empties again and says that one is stored.
* - **Test Connection** saves first, then tests. An unknown SFTP host key
* shows its fingerprint with "Trust and Connect"; a changed one is a
* warning whose replace button must be chosen explicitly.
* - Remove asks in place. Escape closes. The dialog is an exclusive modal
* (`commands/modals.ts`).
*/
import { computed, nextTick, ref, watch } from "vue";
import { useModalsStore } from "../../commands/modals";
import { t } from "../../i18n";
import { pickDir, pickFile } from "../../ipc";
import { blankServer, useDeployStore, type DeployServer, type ServerKind } from "./deploy";
const deploy = useDeployStore();
const modals = useModalsStore();
/** The selected server's id, "" for a new one. */
const selected = ref<string>("");
/** The form's values. */
const form = ref<DeployServer>(blankServer());
/** The typed password or passphrase, sent once with Save. */
const secret = ref("");
/** Whether the form's server is (to become) the default. */
const isDefault = ref(false);
/** The excluded paths, one per line. */
const excludedText = ref("");
/** Whether Remove waits for its confirmation. */
const confirmRemove = ref(false);
/** Whether Save or Test runs. */
const working = ref(false);
/** The name field. */
const nameInput = ref<HTMLInputElement | null>(null);
/** The host-key question box. */
const questionBox = ref<HTMLElement | null>(null);
/** The element that had the focus before the dialog opened. */
let returnFocus: HTMLElement | null = null;
/** Server types in the select, with their labels. */
const kindLabels = computed<{ id: ServerKind; label: string }[]>(() => [
{ id: "sftp", label: t("deploy.kind.sftp") },
{ id: "ftps", label: t("deploy.kind.ftps") },
{ id: "ftp", label: t("deploy.kind.ftp") },
{ id: "folder", label: t("deploy.kind.folder") },
]);
/** Whether the form needs a host, user and port. */
const remote = computed(() => form.value.kind !== "folder");
/** The secret the form's server needs, or null. */
const secretKind = computed<"password" | "passphrase" | null>(() => {
if (form.value.kind === "folder" || (form.value.kind === "sftp" && form.value.auth === "agent")) {
return null;
}
return form.value.kind === "sftp" && form.value.auth === "key" ? "passphrase" : "password";
});
/** Whether the selected server has its secret in the keychain. */
const hasSecret = computed(() => deploy.servers.find((s) => s.id === selected.value)?.hasSecret === true);
/** The port field as text; empty means the default port. */
const portText = computed({
get: () => (form.value.port === null ? "" : String(form.value.port)),
set: (text: string) => {
const n = Number.parseInt(text, 10);
form.value.port = text.trim() === "" || !Number.isFinite(n) ? null : n;
},
});
/** The key path field as text. */
const keyText = computed({
get: () => form.value.keyPath ?? "",
set: (text: string) => {
form.value.keyPath = text === "" ? null : text;
},
});
/**
* Shows a server in the form.
*
* @param id - its id, or "" for a new server.
*/
function select(id: string): void {
const server = deploy.servers.find((s) => s.id === id);
const copy = server ? { ...server, mappings: server.mappings.map((m) => ({ ...m })), excluded: [...server.excluded] } : blankServer();
selected.value = server ? id : "";
form.value = copy;
secret.value = "";
isDefault.value = server ? deploy.defaultServer === id || deploy.servers.length === 1 : deploy.servers.length === 0;
excludedText.value = copy.excluded.join("\n");
confirmRemove.value = false;
deploy.tested = null;
deploy.question = null;
}
/** Starts a new server. */
async function addNew(): Promise<void> {
select("");
await nextTick();
nameInput.value?.focus();
}
/** Adds an empty mapping row. */
function addMapping(): void {
form.value.mappings = [...form.value.mappings, { local: "", remote: "" }];
}
/**
* Removes a mapping row.
*
* @param index - the row.
*/
function removeMapping(index: number): void {
form.value.mappings = form.value.mappings.filter((_, i) => i !== index);
}
/**
* Saves the form.
*
* @returns the saved server's id, or null.
*/
async function save(): Promise<string | null> {
working.value = true;
try {
form.value.excluded = excludedText.value.split(/\r?\n/);
const id = await deploy.saveServer(form.value, secretKind.value === null ? "" : secret.value, isDefault.value);
if (id !== null) {
select(id);
}
return id;
} finally {
working.value = false;
}
}
/** Saves, then tests the connection. */
async function test(): Promise<void> {
const id = await save();
if (id === null) {
return;
}
working.value = true;
try {
await deploy.testConnection(id);
} finally {
working.value = false;
}
}
/** Removes the selected server after the confirmation. */
async function remove(): Promise<void> {
if (!confirmRemove.value) {
confirmRemove.value = true;
return;
}
const id = selected.value;
confirmRemove.value = false;
await deploy.removeServer(id);
select(deploy.servers[0]?.id ?? "");
}
/** Chooses the key file with the shell's picker. */
async function browseKey(): Promise<void> {
const picked = await pickFile(t("deploy.dialog.pickKey"));
if (picked.ok && picked.value !== null) {
form.value.keyPath = picked.value;
}
}
/** Chooses the deployment folder with the shell's picker. */
async function browseFolder(): Promise<void> {
const picked = await pickDir(t("deploy.dialog.pickFolder"));
if (picked.ok && picked.value !== null) {
form.value.root = picked.value;
}
}
/** Closes the dialog. */
function close(): void {
deploy.hideDialog();
}
/**
* Escape closes.
*
* @param event - the key.
*/
function onKeydown(event: KeyboardEvent): void {
if (event.key === "Escape") {
event.preventDefault();
close();
}
}
watch(
() => deploy.dialogOpen,
async (isOpen) => {
if (isOpen) {
const active = document.activeElement;
returnFocus = active instanceof HTMLElement ? active : null;
modals.opened("deployment", close);
select(deploy.target?.id ?? deploy.servers[0]?.id ?? "");
await nextTick();
nameInput.value?.focus();
} else {
modals.closed("deployment");
returnFocus?.focus();
returnFocus = null;
}
},
);
/**
* Scrolls a new host-key question into view; environments without
* `scrollIntoView` (tests) skip it.
*/
watch(
() => deploy.question,
async (question) => {
if (question !== null) {
await nextTick();
const box = questionBox.value;
if (box !== null && typeof box.scrollIntoView === "function") {
box.scrollIntoView({ block: "nearest" });
}
}
},
);
watch(
() => deploy.servers,
() => {
if (deploy.dialogOpen && selected.value !== "" && !deploy.servers.some((s) => s.id === selected.value)) {
select(deploy.servers[0]?.id ?? "");
}
},
);
</script>
<template>
<div
v-if="deploy.dialogOpen"
class="deploy-dialog"
data-test="deploy-dialog"
@mousedown.self="close"
>
<div
class="deploy-dialog__box"
role="dialog"
aria-modal="true"
aria-labelledby="lt-deploy-title"
@keydown="onKeydown"
>
<h2
id="lt-deploy-title"
class="deploy-dialog__title"
>
{{ t("deploy.dialog.title") }}
</h2>
<div class="deploy-dialog__body">
<nav
class="deploy-dialog__servers"
:aria-label="t('deploy.dialog.servers')"
>
<ul class="deploy-dialog__list">
<li
v-for="server in deploy.servers"
:key="server.id"
>
<button
type="button"
class="deploy-dialog__server"
:class="{ 'deploy-dialog__server--selected': server.id === selected }"
:aria-current="server.id === selected ? 'true' : undefined"
data-test="deploy-server"
@click="select(server.id)"
>
<span class="deploy-dialog__server-name">{{ server.name }}</span>
<span class="deploy-dialog__server-sub">
{{ server.kind.toUpperCase() }}{{ server.host ? ` · ${server.host}` : "" }}{{ server.id === deploy.defaultServer ? ` · ${t("deploy.dialog.defaultMark")}` : "" }}
</span>
</button>
</li>
</ul>
<button
type="button"
class="deploy-dialog__button"
data-test="deploy-add"
@click="addNew"
>
{{ t("deploy.dialog.add") }}
</button>
</nav>
<form
class="deploy-dialog__form"
@submit.prevent="save"
>
<label class="deploy-dialog__field">
<span>{{ t("deploy.field.name") }}</span>
<input
ref="nameInput"
v-model="form.name"
class="deploy-dialog__input"
type="text"
autocomplete="off"
data-test="deploy-name"
>
</label>
<label class="deploy-dialog__field">
<span>{{ t("deploy.field.kind") }}</span>
<select
v-model="form.kind"
class="deploy-dialog__input"
data-test="deploy-kind"
>
<option
v-for="kind in kindLabels"
:key="kind.id"
:value="kind.id"
>
{{ kind.label }}
</option>
</select>
</label>
<p
v-if="form.kind === 'ftp'"
class="deploy-dialog__warning"
data-test="deploy-ftp-warning"
>
{{ t("deploy.dialog.ftpWarning") }}
</p>
<template v-if="remote">
<div class="deploy-dialog__pair">
<label class="deploy-dialog__field deploy-dialog__field--grow">
<span>{{ t("deploy.field.host") }}</span>
<input
v-model="form.host"
class="deploy-dialog__input"
type="text"
spellcheck="false"
autocomplete="off"
data-test="deploy-host"
>
</label>
<label class="deploy-dialog__field deploy-dialog__field--port">
<span>{{ t("deploy.field.port") }}</span>
<input
v-model="portText"
class="deploy-dialog__input"
type="text"
inputmode="numeric"
:placeholder="form.kind === 'sftp' ? '22' : '21'"
data-test="deploy-port"
>
</label>
</div>
<label class="deploy-dialog__field">
<span>{{ t("deploy.field.user") }}</span>
<input
v-model="form.user"
class="deploy-dialog__input"
type="text"
spellcheck="false"
autocomplete="off"
data-test="deploy-user"
>
</label>
<label
v-if="form.kind === 'sftp'"
class="deploy-dialog__field"
>
<span>{{ t("deploy.field.auth") }}</span>
<select
v-model="form.auth"
class="deploy-dialog__input"
data-test="deploy-auth"
>
<option value="password">{{ t("deploy.auth.password") }}</option>
<option value="key">{{ t("deploy.auth.key") }}</option>
<option value="agent">{{ t("deploy.auth.agent") }}</option>
</select>
</label>
<label
v-if="form.kind === 'sftp' && form.auth === 'key'"
class="deploy-dialog__field"
>
<span>{{ t("deploy.field.keyPath") }}</span>
<span class="deploy-dialog__row">
<input
v-model="keyText"
class="deploy-dialog__input"
type="text"
spellcheck="false"
data-test="deploy-key"
>
<button
type="button"
class="deploy-dialog__button"
@click="browseKey"
>{{ t("deploy.dialog.browse") }}</button>
</span>
</label>
<label
v-if="secretKind !== null"
class="deploy-dialog__field"
>
<span>{{ secretKind === "password" ? t("deploy.field.password") : t("deploy.field.passphrase") }}</span>
<input
v-model="secret"
class="deploy-dialog__input"
type="password"
autocomplete="new-password"
:placeholder="hasSecret ? t('deploy.dialog.secretStored') : t('deploy.dialog.secretNone')"
data-test="deploy-secret"
>
</label>
<label class="deploy-dialog__field">
<span>{{ t("deploy.field.root") }}</span>
<input
v-model="form.root"
class="deploy-dialog__input"
type="text"
spellcheck="false"
:placeholder="t('deploy.field.rootPlaceholder')"
data-test="deploy-root"
>
</label>
</template>
<label
v-else
class="deploy-dialog__field"
>
<span>{{ t("deploy.field.folder") }}</span>
<span class="deploy-dialog__row">
<input
v-model="form.root"
class="deploy-dialog__input"
type="text"
spellcheck="false"
data-test="deploy-root"
>
<button
type="button"
class="deploy-dialog__button"
@click="browseFolder"
>{{ t("deploy.dialog.browse") }}</button>
</span>
</label>
<fieldset class="deploy-dialog__group">
<legend>{{ t("deploy.field.mappings") }}</legend>
<p class="deploy-dialog__hint">
{{ t("deploy.dialog.mappingsHint") }}
</p>
<div
v-for="(mapping, index) in form.mappings"
:key="index"
class="deploy-dialog__row"
data-test="deploy-mapping"
>
<input
v-model="mapping.local"
class="deploy-dialog__input"
type="text"
spellcheck="false"
:aria-label="t('deploy.field.mappingLocal')"
:placeholder="t('deploy.field.mappingLocal')"
>
<span aria-hidden="true">{{ t("deploy.dialog.mapsTo") }}</span>
<input
v-model="mapping.remote"
class="deploy-dialog__input"
type="text"
spellcheck="false"
:aria-label="t('deploy.field.mappingRemote')"
:placeholder="t('deploy.field.mappingRemote')"
>
<button
type="button"
class="deploy-dialog__button"
:aria-label="t('deploy.dialog.removeMapping')"
@click="removeMapping(index)"
>
×
</button>
</div>
<button
type="button"
class="deploy-dialog__button deploy-dialog__button--small"
data-test="deploy-add-mapping"
@click="addMapping"
>
{{ t("deploy.dialog.addMapping") }}
</button>
</fieldset>
<label class="deploy-dialog__field">
<span>{{ t("deploy.field.excluded") }}</span>
<textarea
v-model="excludedText"
class="deploy-dialog__input deploy-dialog__textarea"
rows="3"
spellcheck="false"
data-test="deploy-excluded"
/>
</label>
<label class="deploy-dialog__check">
<input
v-model="form.uploadOnSave"
type="checkbox"
data-test="deploy-upload-on-save"
>
{{ t("deploy.field.uploadOnSave") }}
</label>
<label class="deploy-dialog__check">
<input
v-model="isDefault"
type="checkbox"
data-test="deploy-default"
>
{{ t("deploy.field.default") }}
</label>
<div
v-if="deploy.question !== null"
ref="questionBox"
class="deploy-dialog__question"
:class="{ 'deploy-dialog__question--changed': deploy.question.status === 'changed' }"
role="alert"
data-test="deploy-hostkey"
>
<template v-if="deploy.question.status === 'unknown'">
<strong>{{ t("deploy.hostKey.unknownTitle") }}</strong>
<span>{{ t("deploy.hostKey.unknown", { host: deploy.question.host }) }}</span>
<code class="deploy-dialog__fingerprint">{{ deploy.question.algorithm }} {{ deploy.question.fingerprint }}</code>
<span class="deploy-dialog__buttons">
<button
type="button"
class="deploy-dialog__button"
@click="deploy.rejectHostKey()"
>{{ t("deploy.hostKey.reject") }}</button>
<button
type="button"
class="deploy-dialog__button deploy-dialog__button--primary"
data-test="deploy-trust"
@click="deploy.trustHostKey(false)"
>{{ t("deploy.hostKey.trust") }}</button>
</span>
</template>
<template v-else>
<strong>{{ t("deploy.hostKey.changedTitle") }}</strong>
<span>{{ t("deploy.hostKey.changed", { host: deploy.question.host }) }}</span>
<span>{{ t("deploy.hostKey.trusted") }} <code class="deploy-dialog__fingerprint">{{ deploy.question.known }}</code></span>
<span>{{ t("deploy.hostKey.offered") }} <code class="deploy-dialog__fingerprint">{{ deploy.question.algorithm }} {{ deploy.question.fingerprint }}</code></span>
<span class="deploy-dialog__buttons">
<button
type="button"
class="deploy-dialog__button deploy-dialog__button--primary"
@click="deploy.rejectHostKey()"
>{{ t("deploy.hostKey.keep") }}</button>
<button
type="button"
class="deploy-dialog__button deploy-dialog__button--danger"
data-test="deploy-replace"
@click="deploy.trustHostKey(true)"
>{{ t("deploy.hostKey.replace") }}</button>
</span>
</template>
</div>
<p
v-if="deploy.tested !== null && deploy.question === null"
class="deploy-dialog__outcome"
:class="{ 'deploy-dialog__outcome--ok': deploy.tested.ok }"
role="status"
data-test="deploy-tested"
>
{{ deploy.tested.message }}
</p>
<p
v-if="deploy.dialogError !== null"
class="deploy-dialog__error"
role="alert"
data-test="deploy-error"
>
{{ deploy.dialogError }}
</p>
</form>
</div>
<div class="deploy-dialog__buttons">
<button
v-if="selected !== ''"
type="button"
class="deploy-dialog__button"
:class="{ 'deploy-dialog__button--danger': confirmRemove }"
data-test="deploy-remove"
@click="remove"
>
{{ confirmRemove ? t("deploy.dialog.confirmRemove") : t("deploy.dialog.remove") }}
</button>
<span class="deploy-dialog__spacer" />
<button
type="button"
class="deploy-dialog__button"
:disabled="working || deploy.busy"
data-test="deploy-test"
@click="test"
>
{{ t("deploy.dialog.test") }}
</button>
<button
type="button"
class="deploy-dialog__button deploy-dialog__button--primary"
:disabled="working"
data-test="deploy-save"
@click="save"
>
{{ t("deploy.dialog.save") }}
</button>
<button
type="button"
class="deploy-dialog__button"
data-test="deploy-close"
@click="close"
>
{{ t("deploy.dialog.close") }}
</button>
</div>
</div>
</div>
</template>
<style scoped>
.deploy-dialog {
position: fixed;
inset: 0;
display: flex;
align-items: flex-start;
justify-content: center;
padding-top: 5vh;
background: color-mix(in srgb, var(--lt-bg) 50%, transparent);
z-index: 20;
}
.deploy-dialog__box {
width: min(860px, 94vw);
max-height: 90vh;
display: flex;
flex-direction: column;
gap: 10px;
padding: 16px;
background: var(--lt-bg);
color: var(--lt-fg);
border: 1px solid var(--lt-border);
border-radius: 6px;
}
.deploy-dialog__title {
margin: 0;
font-size: var(--lt-font-size-ui);
font-weight: 600;
}
.deploy-dialog__body {
display: flex;
gap: 12px;
min-height: 0;
overflow: hidden;
}
.deploy-dialog__servers {
display: flex;
flex: 0 0 200px;
flex-direction: column;
gap: 8px;
padding-right: 12px;
border-right: 1px solid var(--lt-border);
}
.deploy-dialog__list {
margin: 0;
padding: 0;
list-style: none;
overflow-y: auto;
}
.deploy-dialog__server {
display: flex;
flex-direction: column;
width: 100%;
padding: 4px 6px;
font: inherit;
text-align: left;
color: var(--lt-fg);
background: transparent;
border: none;
border-radius: 4px;
}
.deploy-dialog__server--selected {
background: var(--lt-editor-selection-bg);
}
.deploy-dialog__server-sub {
color: var(--lt-fg-muted);
font-size: var(--lt-font-size-small);
}
.deploy-dialog__form {
display: flex;
flex: 1;
flex-direction: column;
gap: 8px;
min-width: 0;
overflow-y: auto;
padding-right: 4px;
}
.deploy-dialog__field {
display: flex;
flex-direction: column;
gap: 4px;
}
.deploy-dialog__field--grow {
flex: 1;
}
.deploy-dialog__field--port {
flex: 0 0 8ch;
}
.deploy-dialog__pair,
.deploy-dialog__row {
display: flex;
align-items: center;
gap: 8px;
}
.deploy-dialog__input {
flex: 1;
min-width: 0;
padding: 4px 6px;
font: inherit;
color: var(--lt-fg);
background: var(--lt-editor-bg);
border: 1px solid var(--lt-border);
border-radius: 4px;
}
.deploy-dialog__textarea {
resize: vertical;
font-family: var(--lt-font-mono);
}
.deploy-dialog__group {
display: flex;
flex-direction: column;
gap: 6px;
margin: 0;
padding: 6px 8px;
border: 1px solid var(--lt-border);
border-radius: 4px;
}
.deploy-dialog__check {
display: flex;
align-items: center;
gap: 6px;
}
.deploy-dialog__hint {
margin: 0;
color: var(--lt-fg-muted);
font-size: var(--lt-font-size-small);
}
.deploy-dialog__warning {
margin: 0;
color: var(--lt-diag-warning);
font-size: var(--lt-font-size-small);
}
.deploy-dialog__question {
display: flex;
flex-direction: column;
gap: 6px;
padding: 8px;
border: 1px solid var(--lt-accent);
border-radius: 4px;
}
.deploy-dialog__question--changed {
border-color: var(--lt-error);
}
.deploy-dialog__fingerprint {
font-family: var(--lt-font-mono);
overflow-wrap: anywhere;
}
.deploy-dialog__outcome {
margin: 0;
overflow-wrap: anywhere;
}
.deploy-dialog__outcome--ok {
color: var(--lt-diag-info);
}
.deploy-dialog__error {
margin: 0;
color: var(--lt-error);
white-space: pre-wrap;
overflow-wrap: anywhere;
}
.deploy-dialog__buttons {
display: flex;
justify-content: flex-end;
gap: 8px;
}
.deploy-dialog__spacer {
flex: 1;
}
.deploy-dialog__button {
padding: 4px 12px;
font: inherit;
color: var(--lt-fg);
background: var(--lt-editor-bg);
border: 1px solid var(--lt-border);
border-radius: 4px;
}
.deploy-dialog__button--small {
align-self: flex-start;
padding: 2px 8px;
}
.deploy-dialog__button--primary {
border-color: var(--lt-accent);
}
.deploy-dialog__button--danger {
border-color: var(--lt-error);
color: var(--lt-error);
}
.deploy-dialog__button:disabled {
color: var(--lt-fg-muted);
}
.deploy-dialog__input:focus-visible,
.deploy-dialog__button:focus-visible,
.deploy-dialog__server:focus-visible {
outline: 1px solid var(--lt-accent);
}
</style>

View file

@ -0,0 +1,217 @@
<script setup lang="ts">
/**
* The Deployment log tool window (G17), shown in the bottom panel: every
* line of the deployment log (newest at the bottom), with its time, the
* server and a severity icon; the running job's progress above the list
* with Cancel; and Clear. The log follows new lines while it is scrolled
* to the bottom. State lives in the Deployment store (`deploy.ts`).
*/
import { CircleAlert, Info, TriangleAlert } from "@lucide/vue";
import { computed, nextTick, ref, watch } from "vue";
import { t } from "../../i18n";
import { useDeployStore, type LogEntry } from "./deploy";
const deploy = useDeployStore();
/** The list element. */
const list = ref<HTMLElement | null>(null);
/** Icons by severity. */
const ICONS = { error: CircleAlert, warning: TriangleAlert, info: Info } as const;
/**
* The time of a line, `HH:MM:SS` in local time.
*
* @param entry - the line.
*/
function timeOf(entry: LogEntry): string {
const d = new Date(entry.time);
const two = (n: number) => String(n).padStart(2, "0");
return `${two(d.getHours())}:${two(d.getMinutes())}:${two(d.getSeconds())}`;
}
/**
* The accessible name of a severity.
*
* @param level - the severity.
*/
function levelLabel(level: LogEntry["level"]): string {
return level === "error"
? t("deploy.log.error")
: level === "warning"
? t("deploy.log.warning")
: t("deploy.log.info");
}
/** The running job's line, or null. */
const running = computed(() => {
const p = deploy.progress;
if (p === null) {
return null;
}
return t("deploy.log.running", {
server: p.server,
done: p.filesDone,
total: p.filesTotal,
file: p.file,
});
});
watch(
() => deploy.entries.length,
async () => {
const el = list.value;
const atBottom = el === null || el.scrollTop + el.clientHeight >= el.scrollHeight - 24;
await nextTick();
if (atBottom && list.value) {
list.value.scrollTop = list.value.scrollHeight;
}
},
);
</script>
<template>
<section
class="deploy-log"
:aria-label="t('deploy.log.title')"
data-test="deploy-log"
>
<header class="deploy-log__header">
<span
v-if="running !== null"
class="deploy-log__running"
role="status"
>{{ running }}</span>
<span
v-else
class="deploy-log__idle"
>{{ deploy.entries.length === 0 ? t("deploy.log.empty") : "" }}</span>
<button
v-if="running !== null"
type="button"
class="deploy-log__button"
data-test="deploy-log-cancel"
@click="deploy.cancel()"
>
{{ t("deploy.cancel") }}
</button>
<button
type="button"
class="deploy-log__button"
:disabled="deploy.entries.length === 0"
data-test="deploy-log-clear"
@click="deploy.clearLog()"
>
{{ t("deploy.log.clear") }}
</button>
</header>
<ol
ref="list"
class="deploy-log__list"
>
<li
v-for="entry in deploy.entries"
:key="entry.id"
class="deploy-log__line"
:class="`deploy-log__line--${entry.level}`"
data-test="deploy-log-line"
>
<component
:is="ICONS[entry.level]"
class="deploy-log__icon"
:size="12"
:aria-label="levelLabel(entry.level)"
/>
<time class="deploy-log__time">{{ timeOf(entry) }}</time>
<span class="deploy-log__server">{{ entry.server }}</span>
<span class="deploy-log__message">{{ entry.message }}</span>
</li>
</ol>
</section>
</template>
<style scoped>
.deploy-log {
display: flex;
flex-direction: column;
height: 100%;
min-height: 0;
}
.deploy-log__header {
display: flex;
align-items: center;
gap: 8px;
flex: none;
padding: 2px 8px;
border-bottom: 1px solid var(--lt-border);
}
.deploy-log__running,
.deploy-log__idle {
flex: 1;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
color: var(--lt-fg-muted);
}
.deploy-log__button {
padding: 1px 8px;
font: inherit;
color: var(--lt-fg);
background: transparent;
border: 1px solid var(--lt-border);
border-radius: 4px;
}
.deploy-log__button:disabled {
color: var(--lt-fg-muted);
}
.deploy-log__button:focus-visible {
outline: 1px solid var(--lt-accent);
}
.deploy-log__list {
flex: 1;
margin: 0;
padding: 2px 0;
list-style: none;
overflow-y: auto;
font-family: var(--lt-font-mono);
font-size: var(--lt-font-size-small);
}
.deploy-log__line {
display: flex;
align-items: baseline;
gap: 8px;
padding: 1px 8px;
}
.deploy-log__icon {
flex: none;
align-self: center;
}
.deploy-log__line--error {
color: var(--lt-error);
}
.deploy-log__line--warning {
color: var(--lt-diag-warning);
}
.deploy-log__time,
.deploy-log__server {
flex: none;
color: var(--lt-fg-muted);
}
.deploy-log__message {
white-space: pre-wrap;
overflow-wrap: anywhere;
}
</style>

View file

@ -0,0 +1,694 @@
/**
* Deployment (G17, owner decision D27): servers of the project, test
* connection, host keys, upload, download, the log and the running job,
* through the engine's `deploy.*` commands.
*
* - **Secrets** go to the engine once (`deploy.saveServer {secret}` or
* `deploy.setSecret`), which keeps them only in the OS keychain. The store
* never keeps or shows one; a server only says whether one is stored.
* - **Host keys:** test connection answers an unknown or changed SFTP host
* key as a {@link HostKeyQuestion}; the dialog shows the fingerprint and
* the user trusts it (`deploy.trustHostKey`), replacing a changed one only
* on an explicit second choice.
* - **Progress and log:** `deployProgress` feeds {@link progress} (the
* status-bar item, with Cancel); `deployLog` appends to {@link entries}
* (the Deployment log tool window, newest 1,000 lines).
* - **Upload and download** are pending outcomes (`runWaiting`) and end in
* a toast: what was transferred, cancelled, or the explained refusal.
*/
import { defineStore } from "pinia";
import { computed, ref, shallowRef } from "vue";
import { onEngineEvent, run, runWaiting, type EngineEventPayload, type JsonValue } from "../../ipc";
import { t } from "../../i18n";
import { useNotificationsStore } from "../../stores/notifications";
/** Most log lines kept, like the engine. */
export const MAX_LOG = 1000;
/** Most servers read from one answer. */
export const MAX_SERVERS = 100;
/** A server's protocol. */
export type ServerKind = "sftp" | "ftp" | "ftps" | "folder";
/** How a server signs in. */
export type AuthKind = "password" | "key" | "agent";
/** One local folder mapped to one remote folder. */
export interface Mapping {
/** Project-relative folder, "" for the root. */
local: string;
/** Remote folder, relative to the root or absolute. */
remote: string;
}
/** A server as `deploy.servers` lists it. */
export interface DeployServer {
/** Stable id. */
id: string;
/** Shown name. */
name: string;
/** Protocol. */
kind: ServerKind;
/** Host; "" for a folder. */
host: string;
/** Port, or null for the default. */
port: number | null;
/** User name. */
user: string;
/** Remote root, or the local folder. */
root: string;
/** How to sign in. */
auth: AuthKind;
/** Private key file for key sign-in. */
keyPath: string | null;
/** Path mappings. */
mappings: Mapping[];
/** Excluded paths and patterns. */
excluded: string[];
/** Upload after every save. */
uploadOnSave: boolean;
/** Which secret the server needs, if any. */
secretKind: "password" | "passphrase" | null;
/** Whether that secret is in the keychain. */
hasSecret: boolean;
}
/** The running job, as `deployProgress` reports it. */
export interface DeployProgress {
/** `test`, `upload` or `download`. */
operation: "test" | "upload" | "download";
/** The server's name. */
server: string;
/** `connecting`, `planning` or `transferring`. */
phase: string;
/** The file being transferred, or "". */
file: string;
/** Files done. */
filesDone: number;
/** Files planned. */
filesTotal: number;
}
/** One line of the deployment log. */
export interface LogEntry {
/** Increasing id. */
id: number;
/** Milliseconds since the epoch. */
time: number;
/** Severity. */
level: "info" | "warning" | "error";
/** The server's name. */
server: string;
/** The text. */
message: string;
}
/** An SFTP host key test connection asks about. */
export interface HostKeyQuestion {
/** The server's id. */
serverId: string;
/** `unknown`: first connection; `changed`: differs from the trusted one. */
status: "unknown" | "changed";
/** `host:port`. */
host: string;
/** The key's algorithm. */
algorithm: string;
/** The offered fingerprint. */
fingerprint: string;
/** The trusted fingerprint, for a changed key. */
known: string;
}
/** What test connection said, for the dialog. */
export interface TestOutcome {
/** Whether it connected. */
ok: boolean;
/** The engine's sentence. */
message: string;
/** Which secret is missing, if that was the reason. */
needsSecret: "password" | "passphrase" | null;
}
/**
* Reads an untrusted object.
*
* @param raw - anything.
*/
function obj(raw: unknown): Record<string, unknown> {
return typeof raw === "object" && raw !== null && !Array.isArray(raw) ? (raw as Record<string, unknown>) : {};
}
/**
* A string field, or "".
*
* @param raw - anything.
*/
function str(raw: unknown): string {
return typeof raw === "string" ? raw : "";
}
/**
* A non-negative integer field, or 0.
*
* @param raw - anything.
*/
function count(raw: unknown): number {
return typeof raw === "number" && Number.isFinite(raw) && raw > 0 ? Math.floor(raw) : 0;
}
/**
* Reads one server; null when malformed.
*
* @param raw - untrusted.
*/
export function toServer(raw: unknown): DeployServer | null {
const s = obj(raw);
const kind = s.kind;
if (typeof s.id !== "string" || s.id === "" || (kind !== "sftp" && kind !== "ftp" && kind !== "ftps" && kind !== "folder")) {
return null;
}
const auth: AuthKind = s.auth === "key" || s.auth === "agent" ? s.auth : "password";
const mappings = Array.isArray(s.mappings)
? (s.mappings as unknown[]).slice(0, 200).map((m) => ({ local: str(obj(m).local), remote: str(obj(m).remote) }))
: [];
const excluded = Array.isArray(s.excluded)
? (s.excluded as unknown[]).filter((e): e is string => typeof e === "string").slice(0, 200)
: [];
return {
id: s.id,
name: str(s.name),
kind,
host: str(s.host),
port: typeof s.port === "number" && Number.isInteger(s.port) ? s.port : null,
user: str(s.user),
root: str(s.root),
auth,
keyPath: typeof s.keyPath === "string" ? s.keyPath : null,
mappings,
excluded,
uploadOnSave: s.uploadOnSave === true,
secretKind: s.secretKind === "password" || s.secretKind === "passphrase" ? s.secretKind : null,
hasSecret: s.hasSecret === true,
};
}
/**
* Reads a `deployProgress` event; null when nothing runs or it is
* malformed.
*
* @param event - the untrusted event.
*/
export function toProgress(event: EngineEventPayload): DeployProgress | null {
if (event.kind !== "deployProgress" || event.active !== true) {
return null;
}
const operation = event.operation;
if (operation !== "test" && operation !== "upload" && operation !== "download") {
return null;
}
return {
operation,
server: str(event.server).slice(0, 200),
phase: str(event.phase).slice(0, 40),
file: str(event.file).slice(0, 500),
filesDone: count(event.filesDone),
filesTotal: count(event.filesTotal),
};
}
/**
* Reads one log line; null when malformed.
*
* @param raw - untrusted.
*/
export function toEntry(raw: unknown): LogEntry | null {
const e = obj(raw);
const level = e.level;
if (typeof e.id !== "number" || (level !== "info" && level !== "warning" && level !== "error")) {
return null;
}
return { id: e.id, time: count(e.time), level, server: str(e.server), message: str(e.message).slice(0, 4000) };
}
/**
* Reads a test-connection answer into the dialog's outcome and the host
* key question, if any.
*
* @param serverId - the tested server.
* @param raw - the untrusted value.
*/
export function toTest(serverId: string, raw: unknown): { outcome: TestOutcome; question: HostKeyQuestion | null } {
const v = obj(raw);
const key = obj(v.hostKey);
const status = key.status;
const question: HostKeyQuestion | null =
status === "unknown" || status === "changed"
? {
serverId,
status,
host: str(key.host),
algorithm: str(key.algorithm),
fingerprint: str(key.fingerprint),
known: str(key.known),
}
: null;
const needs = v.needsSecret;
return {
outcome: {
ok: v.ok === true,
message: str(v.message),
needsSecret: needs === "password" || needs === "passphrase" ? needs : null,
},
question,
};
}
/**
* A server definition as `deploy.saveServer` takes it: without the
* read-only fields, with the port only when set.
*
* @param server - the edited server; an empty id adds a new one.
*/
export function toSaveParams(server: DeployServer): Record<string, JsonValue> {
const out: Record<string, JsonValue> = {
name: server.name,
kind: server.kind,
host: server.host,
user: server.user,
root: server.root,
auth: server.kind === "sftp" ? server.auth : "password",
mappings: server.mappings
.filter((m) => m.local.trim() !== "" || m.remote.trim() !== "")
.map((m) => ({ local: m.local.trim(), remote: m.remote.trim() })),
excluded: server.excluded.map((e) => e.trim()).filter((e) => e !== ""),
uploadOnSave: server.uploadOnSave,
};
if (server.id !== "") {
out.id = server.id;
}
if (server.port !== null) {
out.port = server.port;
}
if (server.kind === "sftp" && server.auth === "key" && server.keyPath !== null) {
out.keyPath = server.keyPath;
}
return out;
}
/**
* "1 file" or "{n} files".
*
* @param n - the number of files.
*/
export function filesText(n: number): string {
return n === 1 ? t("deploy.files.one") : t("deploy.files.other", { count: String(n) });
}
/** A new server's starting values: SFTP, `.git` and `.env` excluded. */
export function blankServer(): DeployServer {
return {
id: "",
name: "",
kind: "sftp",
host: "",
port: null,
user: "",
root: "",
auth: "password",
keyPath: null,
mappings: [],
excluded: [".env", ".idea", "node_modules"],
uploadOnSave: false,
secretKind: "password",
hasSecret: false,
};
}
/** The Deployment store. */
export const useDeployStore = defineStore("deploy", () => {
const notifications = useNotificationsStore();
/** The project's servers. */
const servers = shallowRef<DeployServer[]>([]);
/** The default server's id. */
const defaultServer = ref<string | null>(null);
/** The running job, or null. */
const progress = shallowRef<DeployProgress | null>(null);
/** A job was started here and has not answered. */
const starting = ref(false);
/** Whether a job runs. */
const busy = computed(() => starting.value || progress.value !== null);
/** The log, oldest first. */
const entries = ref<LogEntry[]>([]);
/** Whether the log tool window is shown. */
const visible = ref(false);
/** Whether the bottom panel is expanded while the log is shown. */
const expanded = ref(true);
/** Whether the servers dialog is open. */
const dialogOpen = ref(false);
/** The open host-key question. */
const question = shallowRef<HostKeyQuestion | null>(null);
/** The last test's outcome, for the dialog. */
const tested = shallowRef<TestOutcome | null>(null);
/** The dialog's last error. */
const dialogError = ref<string | null>(null);
/** The default server, or null. */
const target = computed(() => servers.value.find((s) => s.id === defaultServer.value) ?? null);
/** Stops listening; set while started. */
let unsubscribe: (() => void) | null = null;
/** How many holders called {@link start}. */
let holders = 0;
/**
* Appends a log line, keeping the newest {@link MAX_LOG}.
*
* @param entry - the line.
*/
function append(entry: LogEntry): void {
const next = [...entries.value, entry];
entries.value = next.length > MAX_LOG ? next.slice(next.length - MAX_LOG) : next;
}
/** Follows the deployment events. Counted: every `start` needs a `stop`. */
function start(): void {
holders += 1;
if (unsubscribe === null) {
unsubscribe = onEngineEvent((event) => {
if (event.kind === "deployProgress") {
progress.value = toProgress(event);
} else if (event.kind === "deployLog") {
const entry = toEntry(event.entry);
if (entry !== null) {
append(entry);
}
} else if (event.kind === "projectOpened") {
void load();
} else if (event.kind === "projectClosed") {
servers.value = [];
defaultServer.value = null;
dialogOpen.value = false;
}
});
}
}
/** Stops following the events when the last holder stops. */
function stop(): void {
holders = Math.max(0, holders - 1);
if (holders === 0) {
unsubscribe?.();
unsubscribe = null;
}
}
/**
* Reads the project's servers.
*
* @returns whether it worked.
*/
async function load(): Promise<boolean> {
const result = await run("deploy.servers");
if (!result.ok) {
servers.value = [];
defaultServer.value = null;
return false;
}
const v = obj(result.value);
const list = Array.isArray(v.servers) ? (v.servers as unknown[]) : [];
servers.value = list
.slice(0, MAX_SERVERS)
.map(toServer)
.filter((s): s is DeployServer => s !== null);
defaultServer.value = typeof v.defaultServer === "string" ? v.defaultServer : null;
return true;
}
/** Reads the log the engine kept (after a reload of the window). */
async function loadLog(): Promise<void> {
const result = await run("deploy.log");
if (!result.ok) {
return;
}
const v = obj(result.value);
const list = Array.isArray(v.entries) ? (v.entries as unknown[]) : [];
entries.value = list
.map(toEntry)
.filter((e): e is LogEntry => e !== null)
.slice(-MAX_LOG);
}
/** Shows the log tool window. */
function showLog(): void {
visible.value = true;
expanded.value = true;
void loadLog();
}
/** Hides the log tool window. */
function hide(): void {
visible.value = false;
}
/** Clears the log. */
async function clearLog(): Promise<void> {
const result = await run("deploy.clearLog");
if (result.ok) {
entries.value = [];
}
}
/** Reads the servers, then opens the servers dialog on the default one. */
async function showDialog(): Promise<void> {
dialogError.value = null;
tested.value = null;
question.value = null;
await load();
dialogOpen.value = true;
}
/** Closes the servers dialog. */
function hideDialog(): void {
dialogOpen.value = false;
question.value = null;
}
/**
* Adds or replaces a server, with a new secret when one was typed.
*
* @param server - the edited server.
* @param secret - the typed password or passphrase, or "".
* @param makeDefault - also make it the default server.
* @returns the saved server's id, or null (the reason is in {@link dialogError}).
*/
async function saveServer(server: DeployServer, secret: string, makeDefault: boolean): Promise<string | null> {
const params: Record<string, JsonValue> = { server: toSaveParams(server) };
if (secret !== "") {
params.secret = secret;
}
const result = await run("deploy.saveServer", params);
if (!result.ok) {
dialogError.value = result.error.message;
return null;
}
const saved = toServer(obj(result.value).server);
if (saved === null) {
dialogError.value = null;
await load();
return null;
}
if (makeDefault && defaultServer.value !== saved.id) {
await run("deploy.setDefault", { id: saved.id });
} else if (!makeDefault && defaultServer.value === saved.id && servers.value.length > 1) {
await run("deploy.setDefault", { id: null });
}
dialogError.value = null;
await load();
return saved.id;
}
/**
* Removes a server and its keychain entries.
*
* @param id - the server.
*/
async function removeServer(id: string): Promise<void> {
const result = await run("deploy.removeServer", { id });
dialogError.value = result.ok ? null : result.error.message;
await load();
}
/**
* Tests the connection to a saved server; fills {@link tested} and, for
* an unknown or changed host key, {@link question}.
*
* @param id - the server.
*/
async function testConnection(id: string): Promise<TestOutcome | null> {
starting.value = true;
tested.value = null;
question.value = null;
try {
const result = await runWaiting("deploy.testConnection", { id });
if (!result.ok) {
dialogError.value = result.error.message;
return null;
}
dialogError.value = null;
const read = toTest(id, result.value);
tested.value = read.outcome;
question.value = read.question;
return read.outcome;
} finally {
starting.value = false;
}
}
/**
* Trusts the asked host key and tests again.
*
* @param replace - replace a changed trusted key (the second, explicit choice).
*/
async function trustHostKey(replace: boolean): Promise<void> {
const q = question.value;
if (q === null) {
return;
}
const result = await run("deploy.trustHostKey", {
id: q.serverId,
algorithm: q.algorithm,
fingerprint: q.fingerprint,
replace,
});
if (!result.ok) {
dialogError.value = result.error.message;
return;
}
question.value = null;
await testConnection(q.serverId);
}
/** Dismisses the host-key question without trusting the key. */
function rejectHostKey(): void {
question.value = null;
}
/**
* Runs an upload or download and reports it in a toast.
*
* @param command - `deploy.upload` or `deploy.download`.
* @param paths - project-relative paths, "" for the whole project.
* @param id - a server, or the default one when absent.
* @returns whether it ran without failures.
*/
async function transfer(command: "deploy.upload" | "deploy.download", paths: string[], id?: string): Promise<boolean> {
if (busy.value) {
notifications.notify({ level: "warning", messageKey: "notification.deploy.busy" });
return false;
}
starting.value = true;
try {
const params: Record<string, JsonValue> = { paths };
if (id !== undefined) {
params.id = id;
}
const result = await runWaiting(command, params);
const upload = command === "deploy.upload";
if (!result.ok) {
const reason = result.error.message.trim() || "?";
if (reason === "cancelled" || reason.endsWith(": cancelled")) {
notifications.notify({ level: "info", messageKey: "notification.deploy.cancelled" });
} else {
notifications.notify({
level: "error",
messageKey: upload ? "notification.deploy.uploadFailed" : "notification.deploy.downloadFailed",
message: reason,
params: { reason },
});
}
return false;
}
const v = obj(result.value);
const files = count(v.files);
const failed = count(v.failed);
const skipped = count(v.skipped);
const server = (id !== undefined ? servers.value.find((s) => s.id === id) : target.value)?.name ?? "";
notifications.notify({
level: failed > 0 ? "warning" : "info",
messageKey: failed > 0
? "notification.deploy.partly"
: upload
? "notification.deploy.uploaded"
: "notification.deploy.downloaded",
params: { files: filesText(files), failed: filesText(failed), skipped: String(skipped), server },
});
return failed === 0;
} finally {
starting.value = false;
}
}
/**
* Uploads files or folders.
*
* @param paths - project-relative paths, "" for the whole project.
* @param id - a server, or the default one.
*/
function upload(paths: string[], id?: string): Promise<boolean> {
return transfer("deploy.upload", paths, id);
}
/**
* Downloads files or folders, replacing the local ones.
*
* @param paths - project-relative paths.
* @param id - a server, or the default one.
*/
function download(paths: string[], id?: string): Promise<boolean> {
return transfer("deploy.download", paths, id);
}
/**
* Stops the running job.
*
* @returns whether one was stopped.
*/
async function cancel(): Promise<boolean> {
const result = await run("deploy.cancel");
return result.ok;
}
return {
servers,
defaultServer,
target,
progress,
busy,
entries,
visible,
expanded,
dialogOpen,
question,
tested,
dialogError,
start,
stop,
load,
loadLog,
showLog,
hide,
clearLog,
showDialog,
hideDialog,
saveServer,
removeServer,
testConnection,
trustHostKey,
rejectHostKey,
upload,
download,
cancel,
};
});

View file

@ -16,7 +16,8 @@
* the left window when that tool window is already shown. * the left window when that tool window is already shown.
* *
* The bottom panel ({@link BottomPanel}) holds the Find, Terminal and * The bottom panel ({@link BottomPanel}) holds the Find, Terminal and
* Problems tool windows, as tabs in its header while more than one is * Problems tool windows (and the Deployment log, G17, which appears with
* `deployment.showLog`), as tabs in its header while more than one is
* shown. Find appears with the UI command `find.open` (`Mod+Alt+F`), * shown. Find appears with the UI command `find.open` (`Mod+Alt+F`),
* Terminal with `terminal.open` (`Mod+Shift+T`, M3.6), Problems with * Terminal with `terminal.open` (`Mod+Shift+T`, M3.6), Problems with
* `problems.open` (`Mod+Alt+P`, M3.3); all are supplied here so they work * `problems.open` (`Mod+Alt+P`, M3.3); all are supplied here so they work
@ -60,6 +61,8 @@ import { useChangesStore } from "../vcs/changes/changes";
import LogToolWindow from "../vcs/log/LogToolWindow.vue"; import LogToolWindow from "../vcs/log/LogToolWindow.vue";
import { SHOW_HISTORY_COMMAND, SHOW_LOG_COMMAND, useLogStore } from "../vcs/log/log"; import { SHOW_HISTORY_COMMAND, SHOW_LOG_COMMAND, useLogStore } from "../vcs/log/log";
import StashDialog from "../vcs/stash/StashDialog.vue"; import StashDialog from "../vcs/stash/StashDialog.vue";
import DeploymentLogToolWindow from "../deploy/DeploymentLogToolWindow.vue";
import { useDeployStore } from "../deploy/deploy";
import { SHOW_COMMIT_COMMAND, TOGGLE_BLAME_COMMAND, setCommitOpener, toggleBlame } from "../editor/blame"; import { SHOW_COMMIT_COMMAND, TOGGLE_BLAME_COMMAND, setCommitOpener, toggleBlame } from "../editor/blame";
import { SHOW_CHUNK_COMMAND, openChunkPopup } from "../editor/chunkPopup"; import { SHOW_CHUNK_COMMAND, openChunkPopup } from "../editor/chunkPopup";
import { activeView } from "../editor/nav/navExtension"; import { activeView } from "../editor/nav/navExtension";
@ -113,11 +116,12 @@ const problems = useProblemsStore();
const nav = useNavStore(); const nav = useNavStore();
const changes = useChangesStore(); const changes = useChangesStore();
const vcsLog = useLogStore(); const vcsLog = useLogStore();
const deploy = useDeployStore();
const editor = useEditorStore(); const editor = useEditorStore();
const project = useProjectStore(); const project = useProjectStore();
/** Tool windows of the bottom panel. */ /** Tool windows of the bottom panel. */
type BottomTool = "find" | "terminal" | "problems" | "references" | "changes" | "log"; type BottomTool = "find" | "terminal" | "problems" | "references" | "changes" | "log" | "deployment";
/** The bottom tool window last chosen. */ /** The bottom tool window last chosen. */
const bottomTool = ref<BottomTool>("find"); const bottomTool = ref<BottomTool>("find");
/** Bottom tool windows that are shown, in tab order. */ /** Bottom tool windows that are shown, in tab order. */
@ -129,6 +133,7 @@ const bottomTools = computed(() => {
if (nav.visible) tools.push({ id: "references", title: t("nav.references.title") }); if (nav.visible) tools.push({ id: "references", title: t("nav.references.title") });
if (changes.visible) tools.push({ id: "changes", title: t("vcs.changes.toolWindow.title") }); if (changes.visible) tools.push({ id: "changes", title: t("vcs.changes.toolWindow.title") });
if (vcsLog.visible) tools.push({ id: "log", title: t("vcs.log.toolWindow.title") }); if (vcsLog.visible) tools.push({ id: "log", title: t("vcs.log.toolWindow.title") });
if (deploy.visible) tools.push({ id: "deployment", title: t("deploy.log.title") });
return tools; return tools;
}); });
/** The active bottom tool window: the chosen one if shown, else the first. */ /** The active bottom tool window: the chosen one if shown, else the first. */
@ -148,7 +153,9 @@ const bottomStore = computed(() =>
? changes ? changes
: bottomActive.value === "log" : bottomActive.value === "log"
? vcsLog ? vcsLog
: find, : bottomActive.value === "deployment"
? deploy
: find,
); );
/** Expansion of the panel, kept per tool window by its store. */ /** Expansion of the panel, kept per tool window by its store. */
const bottomOpen = computed({ const bottomOpen = computed({
@ -164,7 +171,15 @@ const bottomOpen = computed({
* @param id - the tool window id from the panel. * @param id - the tool window id from the panel.
*/ */
function chooseBottom(id: string): void { function chooseBottom(id: string): void {
if (id === "find" || id === "terminal" || id === "problems" || id === "references" || id === "changes" || id === "log") { if (
id === "find" ||
id === "terminal" ||
id === "problems" ||
id === "references" ||
id === "changes" ||
id === "log" ||
id === "deployment"
) {
bottomTool.value = id; bottomTool.value = id;
} }
} }
@ -273,6 +288,12 @@ onMounted(() => {
}, },
enabled: () => project.root !== null && activeView() !== null, enabled: () => project.root !== null && activeView() !== null,
}), }),
commands.provide("deployment.showLog", {
run: () => {
bottomTool.value = "deployment";
deploy.showLog();
},
}),
commands.provide("view.toggleStructure", { commands.provide("view.toggleStructure", {
run: () => { run: () => {
toggleLeft("structure"); toggleLeft("structure");
@ -370,6 +391,10 @@ onBeforeUnmount(() => {
v-if="vcsLog.visible" v-if="vcsLog.visible"
v-show="bottomActive === 'log'" v-show="bottomActive === 'log'"
/> />
<DeploymentLogToolWindow
v-if="deploy.visible"
v-show="bottomActive === 'deployment'"
/>
</BottomPanel> </BottomPanel>
<StatusBar /> <StatusBar />
<StashDialog /> <StashDialog />

View file

@ -92,26 +92,24 @@ function isPhp(name: string): boolean {
return /\.(php|phtml|inc)$/i.test(name); return /\.(php|phtml|inc)$/i.test(name);
} }
/** The open context menu: the file and where it shows; null when closed. */ /** The open context menu: the entry and where it shows; null when closed. */
const menu = ref<{ path: string; name: string; x: number; y: number } | null>(null); const menu = ref<{ path: string; name: string; kind: "file" | "dir"; x: number; y: number } | null>(null);
/** /**
* Opens the context menu of a file; folders have none yet. * Opens the context menu of a file or folder (folders get the deployment
* items only).
* *
* @param entry - the entry. * @param entry - the entry.
* @param x - window x of the menu. * @param x - window x of the menu.
* @param y - window y of the menu. * @param y - window y of the menu.
*/ */
function openMenu(entry: TreeEntry, x: number, y: number): void { function openMenu(entry: TreeEntry, x: number, y: number): void {
if (entry.kind !== "file") {
return;
}
project.select(entry.path); project.select(entry.path);
menu.value = { path: entry.path, name: entry.name, x, y }; menu.value = { path: entry.path, name: entry.name, kind: entry.kind === "dir" ? "dir" : "file", x, y };
} }
/** /**
* Opens the context menu of the selected file from the keyboard, below its row. * Opens the context menu of the selected entry from the keyboard, below its row.
* *
* @param entry - the selected entry. * @param entry - the selected entry.
*/ */
@ -375,6 +373,7 @@ function onFocus(): void {
v-if="menu" v-if="menu"
:path="menu.path" :path="menu.path"
:name="menu.name" :name="menu.name"
:kind="menu.kind"
:x="menu.x" :x="menu.x"
:y="menu.y" :y="menu.y"
@close="closeMenu" @close="closeMenu"

View file

@ -1,8 +1,11 @@
<script setup lang="ts"> <script setup lang="ts">
/** /**
* The project tree's context menu for a file (M4.3): Open, Show Diff * The project tree's context menu (M4.3). For a file: Open, Show Diff
* (`vcs.showDiff {path}`) and Show History (`vcs.showHistory {path}`, * (`vcs.showDiff {path}`) and Show History (`vcs.showHistory {path}`,
* M4.6), the Git items dimmed while their command is disabled. * M4.6), the Git items dimmed while their command is disabled. For a file
* and a folder: Upload to Server and Download from Server
* (`deployment.upload` / `deployment.download {path}`, G17), dimmed while
* no project is open or a deployment job runs.
* *
* Follows the WAI-ARIA menu pattern: it takes the focus when it opens, * Follows the WAI-ARIA menu pattern: it takes the focus when it opens,
* Up/Down move between items (wrapping), Enter or Space runs one, Escape, * Up/Down move between items (wrapping), Enter or Space runs one, Escape,
@ -21,6 +24,8 @@ const props = defineProps<{
path: string; path: string;
/** File name, for the accessible label. */ /** File name, for the accessible label. */
name: string; name: string;
/** Whether the entry is a file or a folder; a file when absent. */
kind?: "file" | "dir";
/** Window x of the pointer (or the row) in pixels. */ /** Window x of the pointer (or the row) in pixels. */
x: number; x: number;
/** Window y of the pointer (or the row) in pixels. */ /** Window y of the pointer (or the row) in pixels. */
@ -52,8 +57,24 @@ interface MenuItem {
run: () => Promise<void>; run: () => Promise<void>;
} }
/** The items, in order. */ /** The deployment items, for files and folders. */
const items = computed<MenuItem[]>(() => [ const deployItems = computed<MenuItem[]>(() => [
{
id: "upload",
label: t("project.tree.menu.upload"),
enabled: commands.isEnabled("deployment.upload"),
run: () => commands.execute("deployment.upload", { path: props.path }),
},
{
id: "download",
label: t("project.tree.menu.download"),
enabled: commands.isEnabled("deployment.download"),
run: () => commands.execute("deployment.download", { path: props.path }),
},
]);
/** The file items. */
const fileItems = computed<MenuItem[]>(() => [
{ {
id: "open", id: "open",
label: t("project.tree.menu.open"), label: t("project.tree.menu.open"),
@ -76,6 +97,9 @@ const items = computed<MenuItem[]>(() => [
}, },
]); ]);
/** The items, in order. */
const items = computed<MenuItem[]>(() => (props.kind === "dir" ? deployItems.value : [...fileItems.value, ...deployItems.value]));
/** The menu's position, kept inside the window. */ /** The menu's position, kept inside the window. */
const position = computed(() => ({ const position = computed(() => ({
left: `${String(Math.max(0, Math.min(props.x, window.innerWidth - 200)))}px`, left: `${String(Math.max(0, Math.min(props.x, window.innerWidth - 200)))}px`,

View file

@ -1,5 +1,11 @@
{ {
"app.statusBar.version": "{name} {version}", "app.statusBar.version": "{name} {version}",
"command.deployment.download": "Download Current File from Server",
"command.deployment.servers": "Deployment Servers…",
"command.deployment.showLog": "Deployment Log",
"command.deployment.testConnection": "Test Deployment Connection",
"command.deployment.upload": "Upload Current File to Server",
"command.deployment.uploadProject": "Upload Project to Server",
"command.editor.complete": "Complete Code", "command.editor.complete": "Complete Code",
"command.editor.parameterInfo": "Parameter Info", "command.editor.parameterInfo": "Parameter Info",
"command.editor.quickDocumentation": "Quick Documentation", "command.editor.quickDocumentation": "Quick Documentation",
@ -58,6 +64,75 @@
"common.toolWindow.resize": "Resize {title}", "common.toolWindow.resize": "Resize {title}",
"common.toolWindow.show": "Show {title}", "common.toolWindow.show": "Show {title}",
"common.toolWindow.stripe": "Tool windows", "common.toolWindow.stripe": "Tool windows",
"deploy.auth.agent": "SSH agent",
"deploy.auth.key": "Key file",
"deploy.auth.password": "Password",
"deploy.cancel": "Cancel the deployment",
"deploy.dialog.add": "Add Server",
"deploy.dialog.addMapping": "Add Mapping",
"deploy.dialog.browse": "Browse…",
"deploy.dialog.close": "Close",
"deploy.dialog.confirmRemove": "Really Remove",
"deploy.dialog.defaultMark": "default",
"deploy.dialog.ftpWarning": "Plain FTP sends everything, including the password, unencrypted. Prefer SFTP or FTPS.",
"deploy.dialog.mappingsHint": "Local folder (project-relative, empty for the project root) → remote folder (relative to the root, or absolute). Without mappings the project root goes to the root.",
"deploy.dialog.mapsTo": "→",
"deploy.dialog.pickFolder": "Choose the deployment folder",
"deploy.dialog.pickKey": "Choose the private key file",
"deploy.dialog.remove": "Remove",
"deploy.dialog.removeMapping": "Remove this mapping",
"deploy.dialog.save": "Save",
"deploy.dialog.secretNone": "Not stored yet; kept only in the system keychain",
"deploy.dialog.secretStored": "Stored in the system keychain; type to replace",
"deploy.dialog.servers": "Servers",
"deploy.dialog.test": "Test Connection",
"deploy.dialog.title": "Deployment Servers",
"deploy.field.auth": "Sign in with",
"deploy.field.default": "Default server of this project",
"deploy.field.excluded": "Excluded paths (one per line; * and ? match names)",
"deploy.field.folder": "Folder",
"deploy.field.host": "Host",
"deploy.field.keyPath": "Private key file",
"deploy.field.kind": "Type",
"deploy.field.mappingLocal": "Local folder",
"deploy.field.mappingRemote": "Remote folder",
"deploy.field.mappings": "Path mappings",
"deploy.field.name": "Name",
"deploy.field.passphrase": "Key passphrase",
"deploy.field.password": "Password",
"deploy.field.port": "Port",
"deploy.field.root": "Root folder on the server",
"deploy.field.rootPlaceholder": "/var/www/html",
"deploy.field.uploadOnSave": "Upload files when they are saved",
"deploy.field.user": "User",
"deploy.files.one": "1 file",
"deploy.files.other": "{count} files",
"deploy.hostKey.changed": "The host key of {host} is not the one you trusted. Someone may be intercepting the connection. Replace the trusted key only if you know the server was reinstalled.",
"deploy.hostKey.changedTitle": "The server's host key changed",
"deploy.hostKey.keep": "Keep the Trusted Key",
"deploy.hostKey.offered": "Offered now:",
"deploy.hostKey.reject": "Don't Trust",
"deploy.hostKey.replace": "Replace the Trusted Key",
"deploy.hostKey.trust": "Trust and Connect",
"deploy.hostKey.trusted": "Trusted:",
"deploy.hostKey.unknown": "This is the first connection to {host}. Compare this fingerprint with the one from the server's administrator before you trust it:",
"deploy.hostKey.unknownTitle": "Unknown host key",
"deploy.kind.folder": "Local or mounted folder",
"deploy.kind.ftp": "FTP",
"deploy.kind.ftps": "FTPS (FTP over TLS)",
"deploy.kind.sftp": "SFTP",
"deploy.log.clear": "Clear",
"deploy.log.empty": "No deployment yet.",
"deploy.log.error": "Error",
"deploy.log.info": "Info",
"deploy.log.running": "{server}: {done} of {total} files {file}",
"deploy.log.title": "Deployment",
"deploy.log.warning": "Warning",
"deploy.operation.download": "Downloading",
"deploy.operation.test": "Testing",
"deploy.operation.upload": "Uploading",
"deploy.progress.files": "{operation} {done}/{total} · {server}",
"deploy.progress.short": "{operation} · {server}",
"editor.assist.kind.class": "Class", "editor.assist.kind.class": "Class",
"editor.assist.kind.color": "Colour", "editor.assist.kind.color": "Colour",
"editor.assist.kind.constant": "Constant", "editor.assist.kind.constant": "Constant",
@ -175,6 +250,15 @@
"notification.app.dataMigrationFailed": "Cannot copy your settings and tools from {from}: {error}. Lutra starts with the defaults and tries again next time.", "notification.app.dataMigrationFailed": "Cannot copy your settings and tools from {from}: {error}. Lutra starts with the defaults and tries again next time.",
"notification.close": "Close notification", "notification.close": "Close notification",
"notification.command.failed": "{command} failed: {message}", "notification.command.failed": "{command} failed: {message}",
"notification.deploy.busy": "A deployment is running; wait for it or cancel it.",
"notification.deploy.cancelled": "Deployment cancelled.",
"notification.deploy.downloadFailed": "Download failed: {reason}",
"notification.deploy.downloaded": "Downloaded {files} from {server}.",
"notification.deploy.noServer": "No default deployment server yet. Add one, or choose the default.",
"notification.deploy.partly": "Transferred {files}, but {failed} failed ({server}); see the Deployment log.",
"notification.deploy.uploadFailed": "Upload failed: {reason}",
"notification.deploy.uploadOnSaveFailed": "Upload on save to {server} failed: {error}",
"notification.deploy.uploaded": "Uploaded {files} to {server}.",
"notification.lsp.failed": "The PHP language server stopped: {reason}. Run Restart PHP Language Server to try again.", "notification.lsp.failed": "The PHP language server stopped: {reason}. Run Restart PHP Language Server to try again.",
"notification.lsp.message": "PHP language server: {message}", "notification.lsp.message": "PHP language server: {message}",
"notification.lsp.renamed": "Renamed to {name}: {edits} changes in {files} files ({written} closed files saved)", "notification.lsp.renamed": "Renamed to {name}: {edits} changes in {files} files ({written} closed files saved)",
@ -256,10 +340,12 @@
"project.openFolder": "Open Folder…", "project.openFolder": "Open Folder…",
"project.toolWindow.title": "Project", "project.toolWindow.title": "Project",
"project.tree.loading": "Loading…", "project.tree.loading": "Loading…",
"project.tree.menu.download": "Download from Server",
"project.tree.menu.label": "Actions for {name}", "project.tree.menu.label": "Actions for {name}",
"project.tree.menu.open": "Open", "project.tree.menu.open": "Open",
"project.tree.menu.showDiff": "Show Diff", "project.tree.menu.showDiff": "Show Diff",
"project.tree.menu.showHistory": "Show History", "project.tree.menu.showHistory": "Show History",
"project.tree.menu.upload": "Upload to Server",
"project.tree.truncated": "Listing truncated: more entries than can be shown", "project.tree.truncated": "Listing truncated: more entries than can be shown",
"recovery.dialog.changedOnDisk": "The file changed on disk since then", "recovery.dialog.changedOnDisk": "The file changed on disk since then",
"recovery.dialog.discard": "Discard", "recovery.dialog.discard": "Discard",

View file

@ -165,6 +165,29 @@ export type EngineEventPayload =
phase?: unknown; phase?: unknown;
/** 0–100; unchecked. */ /** 0–100; unchecked. */
percent?: unknown; percent?: unknown;
}
| {
kind: "deployProgress";
/** Whether a deployment job runs; unchecked. */
active: unknown;
/** `test`, `upload` or `download`; unchecked. */
operation?: unknown;
/** The server's name; unchecked. */
server?: unknown;
/** `connecting`, `planning` or `transferring`; unchecked. */
phase?: unknown;
/** The file being transferred; unchecked. */
file?: unknown;
/** Files done and planned, bytes done and planned; unchecked. */
filesDone?: unknown;
filesTotal?: unknown;
bytesDone?: unknown;
bytesTotal?: unknown;
}
| {
kind: "deployLog";
/** `{id, time, level, server, message}`; unchecked. */
entry: unknown;
}; };
/** Callback receiving engine events. */ /** Callback receiving engine events. */

View file

@ -0,0 +1,356 @@
/**
* Tests of deployment (G17): reading untrusted servers, progress, log
* lines and test-connection answers; the save params (no read-only
* fields); the status-bar item (progress with Cancel); upload and download
* through the UI commands with their toasts (no default server opens the
* dialog); the servers dialog (save with a write-only secret that is never
* shown again, test connection, the unknown and the changed host key, and
* remove after asking); the Deployment log; and the registration of the
* UI commands. The engine is a fake behind the mocked Tauri `invoke` and
* `listen`.
*/
import { flushPromises, mount, type VueWrapper } from "@vue/test-utils";
import { createPinia, setActivePinia, type Pinia } from "pinia";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const invoke = vi.hoisted(() => vi.fn());
const listen = vi.hoisted(() => vi.fn());
vi.mock("@tauri-apps/api/core", () => ({ invoke }));
vi.mock("@tauri-apps/api/event", () => ({ listen }));
import { useCommandsStore } from "../../src/commands/store";
import { UI_COMMANDS } from "../../src/commands/uiCommands";
import DeployStatusItem from "../../src/components/deploy/DeployStatusItem.vue";
import DeploymentLogToolWindow from "../../src/components/deploy/DeploymentLogToolWindow.vue";
import {
blankServer,
toEntry,
toProgress,
toSaveParams,
toServer,
toTest,
useDeployStore,
} from "../../src/components/deploy/deploy";
import { useNotificationsStore } from "../../src/stores/notifications";
import { useProjectStore } from "../../src/stores/project";
/** The test password; it must never show anywhere. */
const SECRET = "Otter-UI-Pa55!";
/** A recorded engine call. */
interface Call {
/** Command id. */
command: string;
/** Its params. */
params: Record<string, unknown>;
}
let calls: Call[];
let servers: unknown;
let testAnswer: unknown;
let failures: Record<string, { code: string; message: string }>;
let listeners: ((event: { payload: unknown }) => void)[];
let pinia: Pinia;
let mounted: VueWrapper[];
/** The calls of a command. */
function callsOf(command: string): Call[] {
return calls.filter((c) => c.command === command);
}
/** Sends an engine event to every listener. */
async function emit(payload: unknown): Promise<void> {
for (const listener of listeners) {
listener({ payload });
}
await flushPromises();
}
/** The texts of the toasts shown. */
function toasts(): string[] {
return useNotificationsStore(pinia).toasts.map((toast) => toast.text);
}
/** A server as the engine lists it. */
const STAGING = {
id: "s1",
name: "Staging",
kind: "sftp",
host: "staging.example",
user: "deploy",
root: "/var/www",
auth: "password",
mappings: [{ local: "public", remote: "html" }],
excluded: [".env"],
uploadOnSave: false,
secretKind: "password",
hasSecret: true,
};
/** Mounts the status-bar item with a project open. */
async function mountItem(): Promise<VueWrapper> {
useProjectStore(pinia).root = { root: "C:/p", name: "p" } as never;
const wrapper = mount(DeployStatusItem, { attachTo: document.body, global: { plugins: [pinia] } });
mounted.push(wrapper);
await useCommandsStore(pinia).start();
await flushPromises();
return wrapper;
}
beforeEach(() => {
pinia = createPinia();
setActivePinia(pinia);
calls = [];
mounted = [];
failures = {};
listeners = [];
servers = { servers: [STAGING], defaultServer: "s1" };
testAnswer = { ok: true, message: "Connected to Staging; the root folder /var/www exists." };
invoke.mockReset();
listen.mockReset();
listen.mockImplementation((_name: string, callback: (event: { payload: unknown }) => void) => {
listeners.push(callback);
return Promise.resolve(() => {
listeners = listeners.filter((l) => l !== callback);
});
});
invoke.mockImplementation((cmd: string, args?: { command: string; params: Record<string, unknown> }) => {
if (cmd === "engine_list_commands") {
return Promise.resolve(
["deploy.servers", "deploy.upload", "deploy.download"].map((id) => ({ id, label: id, enabled: true })),
);
}
if ((cmd !== "engine_execute" && cmd !== "engine_execute_waiting") || !args) {
return Promise.resolve(null);
}
calls.push({ command: args.command, params: args.params });
const failure = failures[args.command];
if (failure) {
return Promise.reject(Object.assign(new Error(failure.message), failure));
}
switch (args.command) {
case "deploy.servers":
return Promise.resolve(servers);
case "deploy.saveServer":
return Promise.resolve({ server: { ...STAGING, ...(args.params.server as object), id: "s1" } });
case "deploy.testConnection":
return Promise.resolve(testAnswer);
case "deploy.upload":
return Promise.resolve({ files: 3, bytes: 10, skipped: 1, failed: 0 });
case "deploy.download":
return Promise.resolve({ files: 1, bytes: 10, skipped: 0, failed: 0 });
case "deploy.log":
return Promise.resolve({
entries: [{ id: 1, time: 0, level: "info", server: "Staging", message: "public/index.php → html/index.php" }],
});
default:
return Promise.resolve({});
}
});
});
afterEach(() => {
for (const wrapper of mounted) {
wrapper.unmount();
}
document.body.innerHTML = "";
});
describe("reading the engine's answers", () => {
it("reads servers and drops malformed ones", () => {
expect(toServer(STAGING)?.mappings).toEqual([{ local: "public", remote: "html" }]);
expect(toServer({ ...STAGING, kind: "gopher" })).toBeNull();
expect(toServer({ ...STAGING, id: "" })).toBeNull();
expect(toServer({ ...STAGING, auth: "magic", port: 2.5 })).toMatchObject({ auth: "password", port: null });
expect(toServer(null)).toBeNull();
});
it("reads progress, log lines and test answers", () => {
expect(
toProgress({ kind: "deployProgress", active: true, operation: "upload", server: "S", filesDone: 2, filesTotal: 5, file: "a.php" }),
).toMatchObject({ operation: "upload", filesDone: 2, filesTotal: 5, file: "a.php" });
expect(toProgress({ kind: "deployProgress", active: false, operation: "upload" })).toBeNull();
expect(toProgress({ kind: "deployProgress", active: true, operation: "rm" })).toBeNull();
expect(toEntry({ id: 1, time: 5, level: "error", server: "S", message: "x" })).toMatchObject({ level: "error" });
expect(toEntry({ id: 1, level: "loud" })).toBeNull();
const asked = toTest("s1", {
ok: false,
message: "unknown",
hostKey: { status: "unknown", host: "h:22", algorithm: "ssh-ed25519", fingerprint: "SHA256:abc" },
});
expect(asked.question).toMatchObject({ serverId: "s1", status: "unknown", fingerprint: "SHA256:abc" });
expect(toTest("s1", { ok: false, needsSecret: "passphrase" }).outcome.needsSecret).toBe("passphrase");
expect(toTest("s1", null).outcome.ok).toBe(false);
});
it("sends only editable fields when saving", () => {
const params = toSaveParams({
...blankServer(),
name: "New",
host: "h",
user: "u",
hasSecret: true,
mappings: [{ local: " public ", remote: "html" }, { local: "", remote: " " }],
excluded: [" .env ", ""],
});
expect(params).not.toHaveProperty("id");
expect(params).not.toHaveProperty("hasSecret");
expect(params).not.toHaveProperty("secretKind");
expect(params.mappings).toEqual([{ local: "public", remote: "html" }]);
expect(params.excluded).toEqual([".env"]);
expect(toSaveParams({ ...blankServer(), kind: "ftp", auth: "agent" }).auth).toBe("password");
});
});
describe("status bar and commands", () => {
it("shows progress with Cancel and hides when done", async () => {
const wrapper = await mountItem();
expect(wrapper.find("[data-test='deploy-progress']").exists()).toBe(false);
await emit({ kind: "deployProgress", active: true, operation: "upload", server: "Staging", filesDone: 1, filesTotal: 4, phase: "transferring" });
expect(wrapper.find("[data-test='deploy-progress']").text()).toContain("1/4");
await wrapper.find("[data-test='deploy-cancel']").trigger("click");
await flushPromises();
expect(callsOf("deploy.cancel")).toHaveLength(1);
await emit({ kind: "deployProgress", active: false, operation: "upload" });
expect(wrapper.find("[data-test='deploy-progress']").exists()).toBe(false);
});
it("uploads and downloads a path through the UI commands", async () => {
await mountItem();
const commands = useCommandsStore(pinia);
await commands.execute("deployment.upload", { path: "public" });
await flushPromises();
expect(callsOf("deploy.upload")[0]?.params).toEqual({ paths: ["public"] });
expect(toasts().some((t) => t.includes("3 files") && t.includes("Staging"))).toBe(true);
await commands.execute("deployment.download", { path: "public/index.php" });
await flushPromises();
expect(callsOf("deploy.download")[0]?.params).toEqual({ paths: ["public/index.php"] });
expect(toasts().some((t) => t.includes("1 file from"))).toBe(true);
await commands.execute("deployment.uploadProject");
await flushPromises();
expect(callsOf("deploy.upload")[1]?.params).toEqual({ paths: [""] });
});
it("explains a failed upload and opens the dialog without a default server", async () => {
failures["deploy.upload"] = { code: "failed", message: "the host key of h:22 is not known yet" };
await mountItem();
const commands = useCommandsStore(pinia);
await commands.execute("deployment.upload", { path: "a.php" });
await flushPromises();
expect(toasts().some((t) => t.includes("host key"))).toBe(true);
servers = { servers: [], defaultServer: null };
await useDeployStore(pinia).load();
await commands.execute("deployment.upload", { path: "a.php" });
await flushPromises();
expect(callsOf("deploy.upload")).toHaveLength(1);
expect(useDeployStore(pinia).dialogOpen).toBe(true);
});
it("registers the UI commands with menu paths and without shortcuts", () => {
for (const id of ["deployment.upload", "deployment.download", "deployment.uploadProject", "deployment.testConnection", "deployment.servers", "deployment.showLog"]) {
const spec = UI_COMMANDS.find((c) => c.id === id);
expect(spec?.menuPath?.startsWith("Tools/Deployment/")).toBe(true);
expect(spec?.defaultShortcut).toBeUndefined();
}
});
});
describe("servers dialog", () => {
it("saves with a write-only secret and tests the connection", async () => {
const wrapper = await mountItem();
await useDeployStore(pinia).showDialog();
await flushPromises();
expect(wrapper.find("[data-test='deploy-dialog']").exists()).toBe(true);
const secret = wrapper.find<HTMLInputElement>("[data-test='deploy-secret']");
expect(secret.attributes("type")).toBe("password");
expect(secret.element.value).toBe("");
expect(secret.attributes("placeholder")).toContain("keychain");
await secret.setValue(SECRET);
await wrapper.find("[data-test='deploy-upload-on-save']").setValue(true);
await wrapper.find("[data-test='deploy-test']").trigger("click");
await flushPromises();
const saved = callsOf("deploy.saveServer")[0]?.params;
expect(saved?.secret).toBe(SECRET);
expect((saved?.server as Record<string, unknown>).uploadOnSave).toBe(true);
expect(callsOf("deploy.testConnection")[0]?.params).toEqual({ id: "s1" });
expect(wrapper.find("[data-test='deploy-tested']").text()).toContain("exists");
expect(wrapper.find<HTMLInputElement>("[data-test='deploy-secret']").element.value).toBe("");
expect(document.body.innerHTML).not.toContain(SECRET);
});
it("asks about an unknown host key and trusts it", async () => {
testAnswer = {
ok: false,
message: "unknown",
hostKey: { status: "unknown", host: "staging.example:22", algorithm: "ssh-ed25519", fingerprint: "SHA256:fp1" },
};
const wrapper = await mountItem();
await useDeployStore(pinia).showDialog();
await flushPromises();
await wrapper.find("[data-test='deploy-test']").trigger("click");
await flushPromises();
expect(wrapper.find("[data-test='deploy-hostkey']").text()).toContain("SHA256:fp1");
testAnswer = { ok: true, message: "Connected." };
await wrapper.find("[data-test='deploy-trust']").trigger("click");
await flushPromises();
expect(callsOf("deploy.trustHostKey")[0]?.params).toEqual({
id: "s1",
algorithm: "ssh-ed25519",
fingerprint: "SHA256:fp1",
replace: false,
});
expect(callsOf("deploy.testConnection")).toHaveLength(2);
expect(wrapper.find("[data-test='deploy-hostkey']").exists()).toBe(false);
});
it("warns about a changed host key and replaces it only on the explicit button", async () => {
testAnswer = {
ok: false,
message: "changed",
hostKey: { status: "changed", host: "h:22", algorithm: "ssh-ed25519", fingerprint: "SHA256:new", known: "SHA256:old" },
};
const wrapper = await mountItem();
await useDeployStore(pinia).showDialog();
await flushPromises();
await wrapper.find("[data-test='deploy-test']").trigger("click");
await flushPromises();
const box = wrapper.find("[data-test='deploy-hostkey']");
expect(box.text()).toContain("SHA256:old");
expect(box.text()).toContain("SHA256:new");
expect(wrapper.find("[data-test='deploy-trust']").exists()).toBe(false);
await wrapper.find("[data-test='deploy-replace']").trigger("click");
await flushPromises();
expect(callsOf("deploy.trustHostKey")[0]?.params).toMatchObject({ replace: true });
});
it("removes a server only after asking", async () => {
const wrapper = await mountItem();
await useDeployStore(pinia).showDialog();
await flushPromises();
await wrapper.find("[data-test='deploy-remove']").trigger("click");
expect(callsOf("deploy.removeServer")).toHaveLength(0);
await wrapper.find("[data-test='deploy-remove']").trigger("click");
await flushPromises();
expect(callsOf("deploy.removeServer")[0]?.params).toEqual({ id: "s1" });
});
});
describe("deployment log", () => {
it("shows the kept log and new lines, and clears", async () => {
await mountItem();
const deploy = useDeployStore(pinia);
deploy.showLog();
const wrapper = mount(DeploymentLogToolWindow, { global: { plugins: [pinia] } });
mounted.push(wrapper);
await flushPromises();
expect(wrapper.findAll("[data-test='deploy-log-line']")).toHaveLength(1);
await emit({ kind: "deployLog", entry: { id: 2, time: 0, level: "error", server: "Staging", message: "a.php: denied" } });
const lines = wrapper.findAll("[data-test='deploy-log-line']");
expect(lines).toHaveLength(2);
expect(lines[1]?.classes()).toContain("deploy-log__line--error");
await wrapper.find("[data-test='deploy-log-clear']").trigger("click");
await flushPromises();
expect(callsOf("deploy.clearLog")).toHaveLength(1);
expect(wrapper.findAll("[data-test='deploy-log-line']")).toHaveLength(0);
});
});

View file

@ -165,7 +165,7 @@ describe("en.json", () => {
it("uses flat dotted keys of known areas with string values", () => { it("uses flat dotted keys of known areas with string values", () => {
for (const [key, value] of Object.entries(en)) { for (const [key, value] of Object.entries(en)) {
expect(key).toMatch( expect(key).toMatch(
/^(app|command|project|editor|common|palette|theme|recovery|notification|settings|find|goto|structure|menu|terminal|problems|nav|vcs)\.[A-Za-z0-9]+(\.[A-Za-z0-9]+)*$/, /^(app|command|project|editor|common|palette|theme|recovery|notification|settings|find|goto|structure|menu|terminal|problems|nav|vcs|deploy)\.[A-Za-z0-9]+(\.[A-Za-z0-9]+)*$/,
); );
expect(typeof value).toBe("string"); expect(typeof value).toBe("string");
expect(value.trim()).not.toBe(""); expect(value.trim()).not.toBe("");

View file

@ -254,7 +254,7 @@ describe("AppMenu", () => {
expect(menuShown()).toBe(true); expect(menuShown()).toBe(true);
expect(menu.find("[data-test=menu-button]").attributes("aria-expanded")).toBe("true"); expect(menu.find("[data-test=menu-button]").attributes("aria-expanded")).toBe("true");
const headings = [...document.querySelectorAll("[data-test=menu-group]")].map((g) => g.querySelector(".app-menu__heading")?.textContent.trim()); const headings = [...document.querySelectorAll("[data-test=menu-group]")].map((g) => g.querySelector(".app-menu__heading")?.textContent.trim());
expect(headings).toEqual(["File", "Edit", "View", "Go", "Git", "Help"]); expect(headings).toEqual(["File", "Edit", "View", "Go", "Git", "Tools", "Help"]);
const listed = listing() as { id: string; menuPath: string | null }[]; const listed = listing() as { id: string; menuPath: string | null }[];
const withPath = listed.filter((c) => c.menuPath !== null); const withPath = listed.filter((c) => c.menuPath !== null);
expect([...itemIds()].sort()).toEqual(withPath.map((c) => c.id).sort()); expect([...itemIds()].sort()).toEqual(withPath.map((c) => c.id).sort());

View file

@ -295,7 +295,7 @@ describe("CommandPalette", () => {
const wrapper = await mountPalette(); const wrapper = await mountPalette();
await press(PALETTE_KEY); await press(PALETTE_KEY);
await press({ key: "ArrowUp" }); await press({ key: "ArrowUp" });
expect(wrapper.find("[aria-selected=true]").attributes("data-command")).toBe("terminal.open"); expect(wrapper.find("[aria-selected=true]").attributes("data-command")).toBe("deployment.uploadProject");
await press({ key: "ArrowDown" }); await press({ key: "ArrowDown" });
expect(wrapper.find("[aria-selected=true]").attributes("data-command")).toBe("vcs.abortMerge"); expect(wrapper.find("[aria-selected=true]").attributes("data-command")).toBe("vcs.abortMerge");
}); });

View file

@ -475,7 +475,7 @@ describe("project tree context menu", () => {
await tree.find("[data-path='a.php']").trigger("contextmenu", { clientX: 40, clientY: 50 }); await tree.find("[data-path='a.php']").trigger("contextmenu", { clientX: 40, clientY: 50 });
const menu = document.querySelector("[data-test=tree-menu]"); const menu = document.querySelector("[data-test=tree-menu]");
expect(menu).not.toBeNull(); expect(menu).not.toBeNull();
expect([...document.querySelectorAll("[data-test=tree-menu-item]")].map((item) => item.textContent.trim())).toEqual(["Open", "Show Diff", "Show History"]); expect([...document.querySelectorAll("[data-test=tree-menu-item]")].map((item) => item.textContent.trim())).toEqual(["Open", "Show Diff", "Show History", "Upload to Server", "Download from Server"]);
(document.querySelector("[data-item=showDiff]") as HTMLElement).click(); (document.querySelector("[data-item=showDiff]") as HTMLElement).click();
await flushPromises(); await flushPromises();
await flushPromises(); await flushPromises();