use std::collections::BTreeMap;
use std::path::PathBuf;
use std::time::Duration;
use serde::Deserialize;
use super::{Module, machines_of, text_of};
use crate::Result;
use crate::cli::terraform::{self as tf, StateAt};
use crate::events::Stream;
use crate::formats::{Output, SeenMachine};
use crate::graphical::tunnel::To;
use crate::graphical::{Console, ConsoleOffer, ConsoleProtocol, no_console};
use crate::secrets::SecretValue;
use crate::ssh::Login;
const CONSOLES_OUTPUT: &str = "consoles";
const PASSWORD_OUTPUT: &str = "windows_password";
const USER_OUTPUT: &str = "windows_user";
const VNC_PASSWORD_OUTPUT: &str = "vnc_password";
const VIA_OUTPUT: &str = "ip";
const VIA_USER_OUTPUT: &str = "ssh_user";
pub(super) const KEY_VAR: &str = "ssh_private_key_file";
const ADMINISTRATOR: &str = "Administrator";
const RDP_PORT: u16 = 3389;
const VNC_PORT: u16 = 5900;
const READ_LIMIT: Duration = Duration::from_secs(60);
pub(super) fn texts(outputs: &BTreeMap<String, Output>) -> BTreeMap<String, String> {
outputs
.iter()
.filter(|(_, o)| !o.value.is_empty() && o.value != "null")
.map(|(name, o)| (name.clone(), o.value.clone()))
.collect()
}
#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum Entry {
Protocol(String),
Full(Declared),
}
#[derive(Debug, Default, Deserialize)]
struct Declared {
protocol: String,
#[serde(default)]
port: Option<u16>,
#[serde(default)]
host: Option<String>,
#[serde(default)]
via: Option<String>,
#[serde(default)]
user: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct Plan {
protocol: ConsoleProtocol,
port: u16,
host: Option<String>,
via: Option<String>,
user: Option<String>,
}
fn plan(out: &BTreeMap<String, String>, machine: &SeenMachine) -> Option<std::result::Result<Plan, String>> {
if let Some(consoles) = out.get(CONSOLES_OUTPUT) {
let entries: BTreeMap<String, Entry> = match serde_json::from_str(consoles) {
Ok(entries) => entries,
Err(e) => return Some(Err(format!("the module's `{CONSOLES_OUTPUT}` output isn't a map of machines to consoles: {e}"))),
};
let declared = match entries.into_iter().find(|(name, _)| *name == machine.name)?.1 {
Entry::Protocol(protocol) => Declared {
protocol,
..Declared::default()
},
Entry::Full(declared) => declared,
};
let (protocol, port) = match declared.protocol.to_ascii_lowercase().as_str() {
"rdp" => (ConsoleProtocol::Rdp, RDP_PORT),
"vnc" => (ConsoleProtocol::Vnc, VNC_PORT),
other => {
return Some(Err(format!(
"the module's `{CONSOLES_OUTPUT}` output gives it a console of `{}`: `rdp` or `vnc` only",
other.escape_debug()
)));
}
};
return Some(Ok(Plan {
protocol,
port: declared.port.unwrap_or(port),
host: declared.host,
via: declared.via,
user: declared.user,
}));
}
(machine.windows && out.contains_key(PASSWORD_OUTPUT)).then_some(Ok(Plan {
protocol: ConsoleProtocol::Rdp,
port: RDP_PORT,
host: None,
via: None,
user: None,
}))
}
fn via(out: &BTreeMap<String, String>, plan: &Plan) -> std::result::Result<Login, String> {
let host = plan
.via
.clone()
.or_else(|| out.get(VIA_OUTPUT).cloned())
.ok_or_else(|| format!("the module names no Linux machine to tunnel it through (its `{VIA_OUTPUT}` output)"))?;
let user =
plan.user.clone().or_else(|| user_at(out, &host)).ok_or_else(|| {
format!("the module names no SSH user on {host}, the machine its tunnel goes through (`ssh_user_<machine>`, `{VIA_USER_OUTPUT}`)")
})?;
Login::new(&user, &host).ok_or_else(|| format!("`{}@{}` isn't an SSH login the runner signs in with", user.escape_debug(), host.escape_debug()))
}
pub(super) fn user_at(out: &BTreeMap<String, String>, host: &str) -> Option<String> {
out.iter()
.filter(|(_, v)| *v == host)
.find_map(|(name, _)| out.get(&format!("ssh_user_{}", name.strip_prefix("address_")?)))
.or_else(|| out.get(VIA_USER_OUTPUT))
.cloned()
}
pub(super) fn named_address(out: &BTreeMap<String, String>, machine: &str) -> Option<String> {
let all: serde_json::Value = serde_json::from_str(out.get("machines")?).ok()?;
all.get(machine)?.as_str().filter(|a| !a.is_empty()).map(str::to_owned)
}
pub(super) fn published(out: &BTreeMap<String, String>) -> Vec<crate::formats::Published> {
let Some(all) = out.get("published").and_then(|v| serde_json::from_str::<BTreeMap<String, String>>(v).ok()) else {
return Vec::new();
};
all.into_iter()
.filter_map(|(name, at)| {
let (host, port) = at.rsplit_once(':')?;
let host = host.trim_start_matches('[').trim_end_matches(']');
if host.is_empty() {
return None;
}
Some(crate::formats::Published {
name,
host: host.to_owned(),
port: port.parse().ok()?,
})
})
.collect()
}
pub(super) fn offer(out: &BTreeMap<String, String>, machine: &SeenMachine) -> Option<ConsoleOffer> {
let plan = match plan(out, machine) {
Some(Ok(plan)) => plan,
Some(Err(why)) => return Some(ConsoleOffer::none(&why)),
None if machine.windows => {
return Some(ConsoleOffer::none(&format!(
"the module gives no Remote Desktop password for it (`{PASSWORD_OUTPUT}`) nor a `{CONSOLES_OUTPUT}` output naming it"
)));
}
None => return None,
};
let offer = match via(out, &plan) {
Ok(login) => ConsoleOffer::of(plan.protocol, Some(&format!("through an SSH tunnel to {}", login.target()))),
Err(why) => ConsoleOffer::of(plan.protocol, Some("through an SSH tunnel")).unavailable(Some(why)),
};
Some(offer)
}
pub(super) async fn outputs(module: &Module<'_, '_>, state: StateAt<'_>) -> Result<BTreeMap<String, String>> {
if matches!(state, StateAt::File(file) if !file.exists()) {
return Ok(BTreeMap::new());
}
let quiet = |_: Stream, _: &str| {};
let read = tf::outputs(&module.call(READ_LIMIT, &quiet), state)
.await
.map_err(|e| module.error("output", e))?;
let outputs = read
.into_iter()
.map(|(name, o)| {
let output = Output {
value: text_of(&o.value),
sensitive: o.sensitive,
};
(name, output)
})
.collect();
Ok(texts(&outputs))
}
pub(super) async fn reach(module: &Module<'_, '_>, state: StateAt<'_>, vm: &tf::Resource, machine: &str) -> Result<Console> {
let cx = module.cx;
let out = outputs(module, state).await?;
let seen = machines_of(std::slice::from_ref(vm))
.pop()
.ok_or_else(|| no_console(machine, "it isn't a VM"))?;
let plan = match plan(&out, &seen) {
Some(Ok(plan)) => plan,
Some(Err(why)) => return Err(no_console(machine, why)),
None if seen.windows => {
return Err(no_console(
machine,
format!("the module gives no Remote Desktop password for it (`{PASSWORD_OUTPUT}`) nor a `{CONSOLES_OUTPUT}` output naming it"),
));
}
None => {
return Err(no_console(
machine,
format!(
"a `{}` gives no screen this runner reaches (its module names none in a `{CONSOLES_OUTPUT}` output)",
vm.kind
),
));
}
};
let login = via(&out, &plan).map_err(|why| no_console(machine, why))?;
let host = plan
.host
.clone()
.or_else(|| vm.vm.private_address.clone())
.or_else(|| vm.vm.address.clone())
.ok_or_else(|| no_console(machine, "the module's state gives no address of it to tunnel to"))?;
let note = format!("through an SSH tunnel to {}", login.target());
let to = To {
via: login,
host,
port: plan.port,
key: cx.var(KEY_VAR).map(PathBuf::from),
known_hosts: None,
};
let mut console = Console::tunnelled(machine, plan.protocol, to).with_note(Some(¬e));
match plan.protocol {
ConsoleProtocol::Rdp => {
console.user = Some(out.get(USER_OUTPUT).cloned().unwrap_or_else(|| ADMINISTRATOR.to_owned()));
console.password = out.get(PASSWORD_OUTPUT).cloned().or_else(|| cx.var(PASSWORD_OUTPUT)).map(SecretValue::new);
}
_ => console.password = out.get(VNC_PASSWORD_OUTPUT).cloned().map(SecretValue::new),
}
Ok(console)
}
#[cfg(test)]
mod tests {
use super::*;
fn vm(windows: bool) -> SeenMachine {
SeenMachine {
name: "dc".into(),
state: crate::MachineState::Running,
published: Vec::new(),
kind: Some("aws_instance".into()),
address: Some("198.51.100.20".into()),
windows,
}
}
fn outputs(pairs: &[(&str, &str)]) -> BTreeMap<String, String> {
crate::testing::owned_map(pairs)
}
#[test]
fn a_windows_vm_with_its_password_has_remote_desktop_through_the_modules_ip() {
let out = outputs(&[
("windows_password", "s3cret!"),
("ip", "203.0.113.7"),
("address_web", "203.0.113.7"),
("ssh_user_web", "admin"),
("address_dc", "198.51.100.20"),
("ssh_user_dc", "ops"),
]);
let plan = plan(&out, &vm(true)).unwrap().unwrap();
assert_eq!((plan.protocol, plan.port), (ConsoleProtocol::Rdp, 3389));
assert_eq!(via(&out, &plan).unwrap().target(), "admin@203.0.113.7");
let offer = offer(&out, &vm(true)).unwrap();
assert_eq!(offer.protocol, ConsoleProtocol::Rdp);
assert_eq!(offer.unavailable, None);
assert_eq!(super::offer(&out, &vm(false)), None, "a Linux VM has none");
}
#[test]
fn without_a_jump_host_the_offer_says_why_and_without_a_password_there_is_none() {
let out = outputs(&[("windows_password", "x")]);
assert!(offer(&out, &vm(true)).unwrap().unavailable.unwrap().contains("`ip`"));
let none = offer(&outputs(&[("ip", "203.0.113.7")]), &vm(true)).unwrap();
assert_eq!(none.protocol, ConsoleProtocol::None);
}
#[test]
fn the_consoles_output_names_the_machines_and_wins() {
let out = outputs(&[
("consoles", r#"{"dc":{"protocol":"vnc","port":5901,"via":"10.0.0.1","user":"ops"},"db":"rdp"}"#),
("windows_password", "x"),
("ip", "203.0.113.7"),
("ssh_user", "admin"),
]);
let plan = plan(&out, &vm(false)).unwrap().unwrap();
assert_eq!((plan.protocol, plan.port), (ConsoleProtocol::Vnc, 5901));
assert_eq!(via(&out, &plan).unwrap().target(), "ops@10.0.0.1");
let mut db = vm(false);
db.name = "db".into();
let rdp = super::plan(&out, &db).unwrap().unwrap();
assert_eq!(via(&out, &rdp).unwrap().target(), "admin@203.0.113.7");
let mut other = vm(true);
other.name = "other".into();
assert!(super::plan(&out, &other).is_none(), "a machine the output leaves out has none");
let spice = super::plan(&outputs(&[("consoles", r#"{"dc":"spice"}"#)]), &vm(false)).unwrap();
assert!(
spice.as_ref().is_err_and(|why| why.ends_with("a console of `spice`: `rdp` or `vnc` only")),
"{spice:?}"
);
}
}