//! 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, events: Mutex>, said: Mutex>, } impl Host { fn new(config: &Path) -> Arc { 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 { 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 { 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 = 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"), "= 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 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(_)) )); }