use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use isoloom_manifest::{SnakeName, Step, Template};
use crate::Result;
use crate::error::Error;
use crate::executor::{Cx, plan};
use crate::process::{Exit, ProcessRunner, ProcessSpec};
const KEY_FILE: &str = "id_ed25519";
const CONFIG_FILE: &str = "ssh_config";
pub(crate) const KNOWN_HOSTS: &str = "known_hosts";
const KEYGEN_LIMIT: std::time::Duration = std::time::Duration::from_secs(60);
const KEY_STEP: &str = "ssh-key";
pub(crate) fn dir(home: &Path) -> PathBuf {
home.join("ssh")
}
pub(crate) fn key_path(home: &Path) -> PathBuf {
dir(home).join(KEY_FILE)
}
pub(crate) fn public_key(home: &Path) -> Result<Option<String>> {
let path = key_path(home).with_extension("pub");
let text = crate::store::if_there(std::fs::read_to_string(&path)).map_err(Error::io(&path, "read"))?;
Ok(text.map(|t| t.trim().to_owned()).filter(|k| !k.is_empty()))
}
pub(crate) async fn ensure_key(processes: &dyn ProcessRunner, home: &Path) -> Result<String> {
if let Some(key) = public_key(home)? {
return Ok(key);
}
let dir = dir(home);
crate::store::create_dir(&dir)?;
let path = key_path(home);
let spec = ProcessSpec::new(
"ssh-keygen",
["-q", "-t", "ed25519", "-N", "", "-C", "isothrum", "-f"]
.map(String::from)
.into_iter()
.chain([path.display().to_string()]),
);
let progress = crate::executor::Progress::new();
let out = crate::executor::within(KEY_STEP, KEYGEN_LIMIT, &progress, progress.capture(processes, &spec)).await?;
let failed = |reason: String| Error::ToolFailed { step: KEY_STEP.into(), reason };
if !out.success() {
return Err(failed(out.reason()));
}
public_key(home)?.ok_or_else(|| failed("ssh-keygen wrote no public key".into()))
}
pub(crate) fn needed(manifest: &isoloom_manifest::Manifest) -> bool {
use isoloom_manifest::{Action, Condition, Run, RunVar};
let names_key = crate::manifest::names_run_var(manifest, |v| matches!(v, RunVar::SshKey | RunVar::SshPublicKey));
let ssh_step = manifest
.step_lists()
.iter()
.flat_map(|(_, steps)| steps.iter())
.any(|s| matches!(s.run, Run::Action { action: Action::SshRun, .. }));
let ready_file = manifest.ready.iter().any(|c| matches!(c, Condition::ReadyFile { .. }));
names_key || ssh_step || ready_file
}
fn config(key: &Path, known_hosts: &Path) -> Option<String> {
let path = |p: &Path| config_path(&p.to_string_lossy(), cfg!(windows));
Some(format!(
"# Written by the Isothrum runner for this run.\nHost *\n IdentityFile {}\n IdentitiesOnly yes\n UserKnownHostsFile {}\n StrictHostKeyChecking accept-new\n BatchMode yes\n ConnectTimeout 10\n ServerAliveInterval 15\n ServerAliveCountMax 3\n LogLevel ERROR\n",
path(key)?,
path(known_hosts)?,
))
}
fn config_path(path: &str, windows: bool) -> Option<String> {
let path = if windows { path.replace('\\', "/") } else { path.to_owned() };
if path.contains(['"', '\\']) || path.contains("${") || path.chars().any(char::is_control) {
return None;
}
Some(format!("\"{}\"", path.replace('%', "%%")))
}
fn plain_login(login: &str) -> bool {
!login.is_empty() && !login.starts_with('-') && login.matches('@').count() <= 1 && login.chars().all(|c| c.is_ascii_alphanumeric() || ".-_:@".contains(c))
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Login {
pub(crate) target: String,
pub(crate) jumps: Vec<String>,
}
impl Login {
pub(crate) fn new(user: &str, host: &str, jump: Option<&str>, via: &str) -> Result<Login> {
let user = if user.is_empty() { "root" } else { user };
let target = format!("{user}@{host}");
let jumps: Vec<String> = jump
.into_iter()
.flat_map(|j| j.split(','))
.chain(via.split(','))
.map(str::trim)
.filter(|j| !j.is_empty())
.map(str::to_owned)
.collect();
match std::iter::once(&target).chain(&jumps).find(|l| !plain_login(l)) {
Some(bad) => Err(Error::Malformed {
at: format!("the SSH login `{bad}`"),
reason: "a login takes letters, digits, `.`, `-`, `_`, `:` and one `@` only".to_owned(),
}),
None => Ok(Login { target, jumps }),
}
}
pub(crate) fn args(&self, sent: &[String], command: &str) -> Vec<String> {
let mut args = vec!["-F".to_owned(), CONFIG_FILE.to_owned()];
for var in sent {
args.extend(["-o".to_owned(), format!("SendEnv={var}")]);
}
if !self.jumps.is_empty() {
args.extend(["-J".to_owned(), self.jumps.join(",")]);
}
args.extend([self.target.clone(), command.to_owned()]);
args
}
}
pub(crate) fn forget(cx: &Cx<'_>) -> Result<()> {
let state = cx.state_dir();
[KNOWN_HOSTS, CONFIG_FILE].iter().try_for_each(|file| {
let path = state.join(file);
crate::store::remove(&path)
})
}
pub(crate) fn spec(cx: &Cx<'_>, login: &Login, command: &str, env: Vec<(String, String)>) -> Result<ProcessSpec> {
let home = &cx.run.runner.config.home;
let state = cx.state_dir();
crate::store::create_dir(state)?;
let path = state.join(CONFIG_FILE);
let text = config(&key_path(home), &state.join(KNOWN_HOSTS)).ok_or_else(|| {
let why = "the runner's home is at a path SSH's config can't name (it holds `\"`, `\\` outside Windows, `${` or a control character)";
Error::io(&path, "written")(std::io::Error::new(std::io::ErrorKind::InvalidInput, why))
})?;
crate::store::write(&path, text.as_bytes())?;
let mut sent: Vec<String> = env.iter().map(|(k, _)| k.clone()).collect();
sent.sort();
sent.dedup();
let mut spec = ProcessSpec::new("ssh", login.args(&sent, command));
spec.cwd = Some(state.to_path_buf());
spec.env = env;
Ok(spec)
}
pub(crate) fn step_spec(cx: &Cx<'_>, list: &str, step: &Step, with: &BTreeMap<SnakeName, Template>) -> Result<ProcessSpec> {
let at = plan::at(list, step);
let arg = |name: &str| cx.vars.with_text(with, &at, name);
let (host, command) = (arg("host")?, arg("command")?);
if host.is_empty() || command.is_empty() {
return Err(Error::Malformed {
at,
reason: "an `ssh.run` needs its `host` and `command`".to_owned(),
});
}
let login = with_login(cx, &host, arg)?;
let mut spec = spec(cx, &login, &command, crate::executor::env_deliveries(cx, step.id.as_str()))?;
spec.stdin = crate::executor::stdin_of(cx, list, step)?;
Ok(spec)
}
pub(crate) fn with_login(cx: &Cx<'_>, host: &str, arg: impl Fn(&str) -> Result<String>) -> Result<Login> {
Login::new(&arg("user")?, host, cx.ssh_jump.as_deref(), &arg("via")?)
}
fn output_key(machine: &str) -> String {
machine.replace(['-', '.'], "_").to_ascii_lowercase()
}
pub(crate) fn address_output(machine: &str) -> String {
format!("address_{}", output_key(machine))
}
pub(crate) fn ssh_user_output(machine: &str) -> String {
format!("ssh_user_{}", output_key(machine))
}
pub(crate) fn machine_login(output: impl Fn(&str) -> Option<String>, machine: &str, jump: Option<&str>) -> Result<Option<Login>> {
let Some(host) = output(&address_output(machine)).filter(|a| !a.is_empty()) else {
return Ok(None);
};
let user = output(&ssh_user_output(machine)).or_else(|| output("ssh_user")).unwrap_or_default();
Login::new(&user, &host, jump, "").map(Some)
}
pub(crate) const UNREACHABLE: i32 = 255;
pub(crate) const RETRY: std::time::Duration = std::time::Duration::from_secs(5);
pub(crate) fn unreachable(exit: Exit, printed: bool) -> bool {
exit.code == Some(UNREACHABLE) && !printed
}
#[derive(Debug)]
pub(crate) struct Retrying<'w> {
wait: &'w Wait,
deadline: tokio::time::Instant,
retried: bool,
}
impl<'w> Retrying<'w> {
pub(crate) fn new(wait: &'w Wait) -> Retrying<'w> {
Retrying {
wait,
deadline: tokio::time::Instant::now() + wait.limit,
retried: false,
}
}
pub(crate) async fn again(&mut self, unreachable: bool, waiting: impl FnOnce()) -> bool {
if !unreachable || tokio::time::Instant::now() >= self.deadline {
return false;
}
if !self.retried {
waiting();
self.retried = true;
}
tokio::time::sleep(RETRY).await;
true
}
pub(crate) fn answered<T: Try>(&self, cx: &Cx<'_>, step: &str, last: T) -> Result<T> {
if last.unreachable() {
return Err(Error::Unreachable {
step: step.to_owned(),
login: self.wait.login.clone(),
after_s: self.wait.limit.as_secs(),
reason: cx.events.mask(last.said().trim()),
});
}
Ok(last)
}
}
const WAIT_MAX: u64 = 1800;
pub(crate) trait Try {
fn unreachable(&self) -> bool;
fn said(&self) -> String;
}
impl Try for crate::process::Captured {
fn unreachable(&self) -> bool {
unreachable(self.exit, !self.stdout.is_empty())
}
fn said(&self) -> String {
self.stderr.join(" ")
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Wait {
pub(crate) limit: std::time::Duration,
pub(crate) login: String,
}
pub(crate) fn wait_of(cx: &Cx<'_>, list: &str, step: &Step) -> Result<Option<Wait>> {
let isoloom_manifest::Run::Action {
action: isoloom_manifest::Action::SshRun,
with,
} = &step.run
else {
return Ok(None);
};
let at = plan::at(list, step);
let arg = |name: &str| cx.vars.with_text(with, &at, name);
let text = arg("wait_s")?;
if text.is_empty() {
return Ok(None);
}
let malformed = |reason: String| Error::Malformed {
at: format!("{at}.with.wait_s"),
reason,
};
let secs: u64 = text.trim().parse().map_err(|_| malformed(format!("`{text}` isn't a number of seconds")))?;
if secs > WAIT_MAX {
return Err(malformed(format!("{secs} is over the {WAIT_MAX} seconds a step may wait")));
}
let login = with_login(cx, &arg("host")?, arg)?.target;
Ok((secs > 0).then(|| Wait {
limit: std::time::Duration::from_secs(secs),
login,
}))
}
#[cfg(test)]
mod tests {
use proptest::prelude::*;
use super::*;
#[test]
fn only_ssh_failing_to_connect_with_nothing_printed_is_unreachable() {
assert!(unreachable(Exit::new(Some(UNREACHABLE)), false));
assert!(!unreachable(Exit::new(Some(UNREACHABLE)), true), "the command ran: its own 255");
assert!(!unreachable(Exit::new(Some(1)), false));
assert!(!unreachable(Exit::new(None), false));
}
#[tokio::test(start_paused = true)]
async fn an_unreachable_host_is_tried_again_every_retry_until_the_deadline() {
let start = tokio::time::Instant::now();
let wait = |limit| Wait {
limit,
login: "root@web".to_owned(),
};
let two = wait(RETRY * 2);
let mut retrying = Retrying::new(&two);
let mut said = 0;
assert!(retrying.again(true, || said += 1).await);
assert!(retrying.again(true, || said += 1).await);
assert_eq!(start.elapsed(), RETRY * 2, "each retry after RETRY");
assert!(!retrying.again(true, || said += 1).await, "not past the deadline");
assert_eq!(said, 1, "said once, before the first retry");
let ten = wait(RETRY * 10);
let mut connected = Retrying::new(&ten);
assert!(
!connected.again(false, || panic!("nothing to say")).await,
"a try that connected is the outcome"
);
assert_eq!(start.elapsed(), RETRY * 2, "and isn't waited for");
}
#[tokio::test]
async fn a_public_key_that_cant_be_read_is_an_error_and_no_new_key_is_made() {
let home = tempfile::tempdir().unwrap();
std::fs::create_dir_all(key_path(home.path()).with_extension("pub")).unwrap();
let fake = std::sync::Arc::new(crate::process::fake::FakeProcesses::new(|_| crate::process::fake::Reply::ok("")));
let err = ensure_key(fake.as_ref(), home.path()).await.unwrap_err();
assert_eq!(err.code(), crate::ErrorCode::Io, "{err}");
assert!(err.to_string().contains("id_ed25519.pub"), "{err}");
assert!(fake.calls().is_empty(), "no ssh-keygen: {:?}", fake.lines());
assert_eq!(public_key(&home.path().join("elsewhere")).unwrap(), None, "a missing key is none");
}
#[test]
fn a_machines_login_is_its_address_as_its_user_else_the_runs_else_root() {
let outputs = |pairs: &'static [(&'static str, &'static str)]| move |name: &str| pairs.iter().find(|(k, _)| *k == name).map(|(_, v)| (*v).to_owned());
let own = outputs(&[("address_web_1", "198.51.100.7"), ("ssh_user_web_1", "admin"), ("ssh_user", "ubuntu")]);
let login = machine_login(own, "web-1", Some("me@jump")).unwrap().unwrap();
assert_eq!(login.target, "admin@198.51.100.7");
assert_eq!(login.jumps, ["me@jump"]);
let run_wide = outputs(&[("address_web_1", "198.51.100.7"), ("ssh_user", "ubuntu")]);
assert_eq!(machine_login(run_wide, "web-1", None).unwrap().unwrap().target, "ubuntu@198.51.100.7");
let none = outputs(&[("address_web_1", "198.51.100.7")]);
assert_eq!(machine_login(none, "web-1", None).unwrap().unwrap().target, "root@198.51.100.7");
assert_eq!(machine_login(outputs(&[("address_web_1", "")]), "web-1", None).unwrap(), None, "no address yet");
assert_eq!(machine_login(outputs(&[]), "web-1", None).unwrap(), None);
}
#[test]
fn a_machines_output_names_are_its_terraform_identifier_in_lower_case() {
assert_eq!(address_output("web-1"), "address_web_1");
assert_eq!(address_output("Web.1"), "address_web_1");
assert_eq!(ssh_user_output("db.Primary-2"), "ssh_user_db_primary_2");
}
#[test]
fn every_linux_vm_has_the_outputs_isoloom_generates_for_it() {
let spec = "version: 1\nname: pair\nnetworks:\n app: { cidr: 10.80.0.0/24, internet: true }\n\
machines:\n web-1: { os: debian@12, networks: { app: 10 } }\n db-main-2: { os: debian@12, networks: { app: 11 } }\n";
let lab = isoloom_targets::Lab::parse(spec).unwrap();
let (files, _) = isoloom_targets::generate_all(&lab);
let clouds: Vec<_> = files
.iter()
.filter(|f| f.path.starts_with(".isoloom/cloud-vm/") && f.path.ends_with("/run.json"))
.collect();
assert!(!clouds.is_empty(), "{:?}", files.iter().map(|f| &f.path).collect::<Vec<_>>());
for file in clouds {
let manifest = isoloom_manifest::parse(&file.contents).unwrap();
let names: Vec<String> = manifest.outputs.iter().map(|o| o.name.to_string()).collect();
let output = |name: &str| names.iter().any(|n| n == name).then(|| "198.51.100.7".to_owned());
for machine in ["web-1", "db-main-2"] {
assert!(names.contains(&address_output(machine)), "{}: {names:?}", file.path);
assert!(names.contains(&ssh_user_output(machine)), "{}: {names:?}", file.path);
assert!(machine_login(output, machine, None).unwrap().is_some(), "{}", file.path);
}
}
}
#[test]
fn a_login_goes_through_the_runs_jump_then_the_manifests() {
let l = Login::new("isoloom", "10.60.0.10", Some("root@pve.lan"), ",isoloom@10.10.10.5").unwrap();
assert_eq!(l.jumps, ["root@pve.lan", "isoloom@10.10.10.5"]);
assert_eq!(
l.args(&[], "sh -s"),
["-F", "ssh_config", "-J", "root@pve.lan,isoloom@10.10.10.5", "isoloom@10.60.0.10", "sh -s"]
);
let direct = Login::new("isoloom", "10.0.0.9", None, "").unwrap();
assert_eq!(direct.args(&[], "true"), ["-F", "ssh_config", "isoloom@10.0.0.9", "true"]);
}
#[test]
fn the_variables_sent_are_named_before_the_host_their_values_never_in_the_arguments() {
let l = Login::new("isoloom", "10.0.0.9", None, "").unwrap();
let sent = ["DEMO_SECRET".to_owned(), "TOKEN".to_owned()];
assert_eq!(
l.args(&sent, "sudo -E docker compose up"),
[
"-F",
"ssh_config",
"-o",
"SendEnv=DEMO_SECRET",
"-o",
"SendEnv=TOKEN",
"isoloom@10.0.0.9",
"sudo -E docker compose up"
]
);
}
#[test]
fn a_login_with_no_user_is_root() {
assert_eq!(Login::new("", "10.0.0.9", None, "").unwrap().target, "root@10.0.0.9");
assert_eq!(Login::new("admin", "10.0.0.9", None, "").unwrap().target, "admin@10.0.0.9");
}
#[test]
fn an_odd_login_is_refused() {
for (user, host, via) in [
("isoloom", "10.0.0.5;rm -rf /", ""),
("-oProxyCommand=x", "h", ""),
("u", "h", "a@b@c"),
("u", "h", "-J x"),
] {
assert!(Login::new(user, host, None, via).is_err(), "{user}@{host} via {via}");
}
}
#[test]
fn the_config_carries_the_key_and_the_runs_known_hosts_to_every_host() {
let c = config(Path::new("/home/a b/.isothrum/ssh/id_ed25519"), Path::new("/r/known_hosts")).unwrap();
assert!(c.contains("Host *\n"), "{c}");
assert!(c.contains("IdentityFile \"/home/a b/.isothrum/ssh/id_ed25519\"\n"), "{c}");
assert!(c.contains("UserKnownHostsFile \"/r/known_hosts\"\n"), "{c}");
assert!(c.contains("StrictHostKeyChecking accept-new\n") && c.contains("BatchMode yes\n"), "{c}");
}
#[test]
fn a_path_is_quoted_for_every_openssh_and_its_tokens_doubled() {
assert_eq!(config_path("/home/a b/x 50%", false).as_deref(), Some("\"/home/a b/x 50%%\""));
assert_eq!(config_path("it's", false).as_deref(), Some("\"it's\""));
assert_eq!(
config_path("C:\\Users\\a b\\.isothrum\\ssh\\id_ed25519", true).as_deref(),
Some("\"C:/Users/a b/.isothrum/ssh/id_ed25519\"")
);
assert_eq!(
config_path("\\\\server\\share\\h", true).as_deref(),
Some("\"//server/share/h\""),
"a network path"
);
}
#[test]
fn a_path_no_openssh_reads_as_written_is_refused() {
for path in ["/a\"b", "/a\\b", "/a/${HOME}", "/a\nb"] {
assert_eq!(config_path(path, false), None, "{path:?}");
}
assert_eq!(config_path("C:\\a\"b", true), None);
}
#[test]
fn a_login_refused_says_what_a_login_takes() {
let refused = Login::new("u", "h;x", None, "").unwrap_err().to_string();
assert!(
refused.ends_with("a login takes letters, digits, `.`, `-`, `_`, `:` and one `@` only"),
"{refused}"
);
assert!(Login::new("u", "fd00::10", None, "").is_ok(), "`:` is one of them");
}
proptest! {
#[test]
fn a_plain_login_never_starts_an_option(user in "[a-z][a-z0-9_]{0,8}", host in "[a-z0-9.-]{1,20}") {
let l = Login::new(&user, &host, None, "");
prop_assert!(l.is_ok());
let args = l.unwrap().args(&[], "true");
prop_assert!(!args[2].starts_with('-'));
}
}
}