use std::time::Duration;
use isoloom_manifest::{ConsoleSource, Machine};
use super::none;
use crate::Result;
use crate::error::Error;
use crate::executor::{Cx, Echo, call};
use crate::process::ProcessSpec;
use crate::secrets::SecretValue;
use crate::store::Transient;
const LIMIT: Duration = Duration::from_secs(30);
const CONFIG: &str = "proxmox-console.curl";
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ProxmoxVnc {
pub websocket: SecretValue,
pub port: u16,
pub ticket: SecretValue,
pub authorization: (String, SecretValue),
pub insecure: bool,
}
enum SignIn {
Token(String),
Ticket { ticket: String, csrf: String },
}
impl SignIn {
fn lines(&self, post: bool) -> Vec<String> {
match self {
SignIn::Token(t) => vec![format!("header = {}", quoted(&format!("Authorization: PVEAPIToken={t}")))],
SignIn::Ticket { ticket, csrf } => {
let mut out = vec![format!("cookie = {}", quoted(&format!("PVEAuthCookie={ticket}")))];
if post {
out.push(format!("header = {}", quoted(&format!("CSRFPreventionToken: {csrf}"))));
}
out
}
}
}
fn header(&self) -> (String, SecretValue) {
match self {
SignIn::Token(t) => ("Authorization".into(), SecretValue::new(format!("PVEAPIToken={t}"))),
SignIn::Ticket { ticket, .. } => ("Cookie".into(), SecretValue::new(format!("PVEAuthCookie={ticket}"))),
}
}
}
pub(super) async fn vnc(cx: &Cx<'_>, machine: &Machine, console: &isoloom_manifest::Console) -> Result<ProxmoxVnc> {
let Some(ConsoleSource::ProxmoxApi {
endpoint,
node,
vm_id,
insecure,
username,
token,
password,
}) = &console.source
else {
return Err(none(machine, "it has no Proxmox console"));
};
let at = |f: &str| format!("machines.{}.consoles.source.{f}", machine.name);
let base = cx.vars.render(endpoint, &at("endpoint"))?.trim_end_matches('/').to_owned();
let (node, vm_id) = (cx.vars.render(node, &at("node"))?, cx.vars.render(vm_id, &at("vm_id"))?);
let insecure = cx.vars.render(insecure, &at("insecure"))?.trim() == "true";
let host = base
.strip_prefix("https://")
.ok_or_else(|| none(machine, format!("the Proxmox API `{base}` isn't https")))?;
if !plain(&node) || !vm_id.chars().all(|c| c.is_ascii_digit()) || vm_id.is_empty() {
return Err(none(machine, format!("its VM `{vm_id}` on node `{node}` can't be named in a request")));
}
let sign_in = sign_in(cx, machine, &base, insecure, (username, token, password)).await?;
let path = format!("/api2/json/nodes/{node}/qemu/{vm_id}");
let mut fields = sign_in.lines(true);
fields.push("data = \"websocket=1\"".to_owned());
let said = api(cx, machine, &format!("{base}{path}/vncproxy"), insecure, &fields).await?;
let port = said
.get("port")
.and_then(|p| p.as_u64().or_else(|| p.as_str()?.parse().ok()))
.and_then(|p| u16::try_from(p).ok());
let ticket = said.get("ticket").and_then(serde_json::Value::as_str);
let (Some(port), Some(ticket)) = (port, ticket) else {
return Err(none(machine, "Proxmox's vncproxy gave no port and ticket"));
};
cx.events.hide([ticket.to_owned()]);
Ok(ProxmoxVnc {
websocket: SecretValue::new(format!("wss://{host}{path}/vncwebsocket?port={port}&vncticket={}", encoded(ticket))),
port,
ticket: SecretValue::new(ticket),
authorization: sign_in.header(),
insecure,
})
}
async fn sign_in(
cx: &Cx<'_>,
machine: &Machine,
base: &str,
insecure: bool,
(username, token, password): (&isoloom_manifest::Template, &isoloom_manifest::Identifier, &isoloom_manifest::Identifier),
) -> Result<SignIn> {
let value = |slot: &isoloom_manifest::Identifier| cx.secrets.get(slot.as_str()).map(|v| v.expose().to_owned()).filter(|v| !v.is_empty());
match (value(token), value(password)) {
(Some(t), _) => Ok(SignIn::Token(t)),
(None, Some(p)) => {
let user = cx.vars.render(username, &format!("machines.{}.consoles.source.username", machine.name))?;
let fields = [
format!("data-urlencode = {}", quoted(&format!("username={user}"))),
format!("data-urlencode = {}", quoted(&format!("password={p}"))),
];
let said = api(cx, machine, &format!("{base}/api2/json/access/ticket"), insecure, &fields).await?;
let ticket = said.get("ticket").and_then(serde_json::Value::as_str);
let csrf = said.get("CSRFPreventionToken").and_then(serde_json::Value::as_str);
match (ticket, csrf) {
(Some(t), Some(c)) => Ok(SignIn::Ticket {
ticket: t.to_owned(),
csrf: c.to_owned(),
}),
_ => Err(none(machine, "Proxmox gave no ticket for the user's password")),
}
}
(None, None) => Err(none(machine, "the run's connection gives no Proxmox API token or password")),
}
}
async fn api(cx: &Cx<'_>, machine: &Machine, url: &str, insecure: bool, fields: &[String]) -> Result<serde_json::Value> {
let out = request(cx, "console", (url, Some("POST")), insecure, fields).await?;
if !out.success() {
return Err(none(machine, format!("Proxmox's API refused: {}", cx.events.mask(&out.reason()))));
}
let body: serde_json::Value = serde_json::from_str(&out.stdout.join("\n")).map_err(|e| Error::NoConsole {
machine: machine.name.to_string(),
why: format!("Proxmox's API answered no JSON: {e}"),
})?;
Ok(body.get("data").cloned().unwrap_or(serde_json::Value::Null))
}
async fn request(cx: &Cx<'_>, step: &str, (url, method): (&str, Option<&str>), insecure: bool, fields: &[String]) -> Result<crate::process::Captured> {
let mut text = format!("url = {}\n", quoted(url));
if let Some(method) = method {
text.push_str("request = ");
text.push_str("ed(method));
text.push('\n');
}
for f in fields {
text.push_str(f);
text.push('\n');
}
let config = Transient::write(cx.state_dir().join(CONFIG), text.as_bytes(), "a Proxmox API request's curl config")?;
let mut args = vec!["--silent", "--show-error", "--fail-with-body", "--max-time", "25", "--config", "-"];
if insecure {
args.push("--insecure");
}
let mut spec = ProcessSpec::new("curl", args);
spec.stdin = Some(config.path().to_path_buf());
config.remove_after(call(cx, step, &spec, LIMIT, Echo::Quiet).await)
}
pub(crate) async fn vm_states(cx: &Cx<'_>, vars: &crate::vars::Vars) -> Result<std::collections::BTreeMap<String, crate::MachineState>> {
let mut out = std::collections::BTreeMap::new();
let vms = proxmox_vms(cx, vars)?;
let Some((_, first)) = vms.first() else { return Ok(out) };
let Some(sign_in) = status_sign_in(cx, vars, first).await? else {
return Ok(out);
};
let url = format!("{}/api2/json/cluster/resources?type=vm", first.base);
let Some(answer) = asked(request(cx, "status", (&url, None), first.insecure, &sign_in.lines(false)).await) else {
return Ok(out);
};
let Some(listed) = answered(cx, vars, &answer)? else { return Ok(out) };
for (name, vm) in &vms {
let found = listed.iter().find(|r| {
r.get("vmid")
.and_then(|v| v.as_u64().map(|n| n.to_string()).or_else(|| v.as_str().map(str::to_owned)))
.as_deref()
== Some(vm.id.as_str())
});
let state = match found.and_then(|r| r.get("status")).and_then(serde_json::Value::as_str) {
None if found.is_none() => crate::MachineState::Absent,
Some("running") => crate::MachineState::Running,
Some("stopped") => crate::MachineState::Stopped,
Some(other) => crate::MachineState::Unknown(other.to_owned()),
None => continue,
};
out.insert(name.clone(), state);
}
Ok(out)
}
struct ApiVm<'m> {
base: String,
id: String,
insecure: bool,
sign_in: (
&'m isoloom_manifest::Template,
&'m isoloom_manifest::Identifier,
&'m isoloom_manifest::Identifier,
),
}
fn proxmox_vms<'m>(cx: &Cx<'m>, vars: &crate::vars::Vars) -> Result<Vec<(String, ApiVm<'m>)>> {
let mut out = Vec::new();
for m in &cx.run.manifest().machines {
let source = m.consoles.iter().find_map(|c| match &c.source {
Some(s @ ConsoleSource::ProxmoxApi { .. }) => Some(s),
_ => None,
});
let Some(ConsoleSource::ProxmoxApi {
endpoint,
vm_id,
insecure,
username,
token,
password,
..
}) = source
else {
continue;
};
let at = |f: &str| format!("machines.{}.consoles.source.{f}", m.name);
let Ok(id) = vars.render(vm_id, &at("vm_id")) else { continue };
if id.is_empty() || !id.chars().all(|c| c.is_ascii_digit()) {
continue;
}
let base = vars.render(endpoint, &at("endpoint"))?.trim_end_matches('/').to_owned();
let insecure = vars.render(insecure, &at("insecure"))?.trim() == "true";
out.push((
m.name.to_string(),
ApiVm {
base,
id,
insecure,
sign_in: (username, token, password),
},
));
}
Ok(out)
}
async fn status_sign_in(cx: &Cx<'_>, vars: &crate::vars::Vars, vm: &ApiVm<'_>) -> Result<Option<SignIn>> {
let (username, token, password) = vm.sign_in;
let value = |slot: &isoloom_manifest::Identifier| cx.secrets.get(slot.as_str()).map(|v| v.expose().to_owned()).filter(|v| !v.is_empty());
if let Some(t) = value(token) {
return Ok(Some(SignIn::Token(t)));
}
let Some(p) = value(password) else { return Ok(None) };
let user = vars.render(username, "consoles.source.username")?;
let fields = [
format!("data-urlencode = {}", quoted(&format!("username={user}"))),
format!("data-urlencode = {}", quoted(&format!("password={p}"))),
];
let url = format!("{}/api2/json/access/ticket", vm.base);
let Some(answer) = asked(request(cx, "status", (&url, Some("POST")), vm.insecure, &fields).await) else {
return Ok(None);
};
let Some(data) = data_of(cx, vars, &answer)? else { return Ok(None) };
let field = |k: &str| data.get(k).and_then(serde_json::Value::as_str).map(str::to_owned);
Ok(field("ticket")
.zip(field("CSRFPreventionToken"))
.map(|(ticket, csrf)| SignIn::Ticket { ticket, csrf }))
}
fn asked(done: Result<crate::process::Captured>) -> Option<crate::process::Captured> {
done.map_err(|e| tracing::warn!(error = %e, "Proxmox's API couldn't be asked where the run's VMs stand: its state stands"))
.ok()
}
fn answered(cx: &Cx<'_>, vars: &crate::vars::Vars, answer: &crate::process::Captured) -> Result<Option<Vec<serde_json::Value>>> {
Ok(data_of(cx, vars, answer)?.and_then(|d| d.as_array().cloned()))
}
fn data_of(cx: &Cx<'_>, vars: &crate::vars::Vars, answer: &crate::process::Captured) -> Result<Option<serde_json::Value>> {
if !answer.success() {
let reason = cx.events.mask(&answer.reason());
if crate::server_auth::refused(&reason) {
return Err(Error::ServerAuthFailed {
step: "status".into(),
server: crate::server_auth::server_of(vars.params()),
reason,
});
}
tracing::warn!(%reason, "Proxmox's API couldn't be asked where the run's VMs stand: its state stands");
return Ok(None);
}
match serde_json::from_str::<serde_json::Value>(&answer.stdout.join("\n")) {
Ok(body) => Ok(body.get("data").cloned()),
Err(e) => {
tracing::warn!(error = %e, "Proxmox's API answered no JSON: the run's state stands");
Ok(None)
}
}
}
fn quoted(s: &str) -> String {
format!("\"{}\"", s.replace('\\', "\\\\").replace('"', "\\\"").replace('\n', "\\n").replace('\r', "\\r"))
}
fn plain(s: &str) -> bool {
!s.is_empty() && s.chars().all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '.')
}
fn encoded(s: &str) -> String {
s.bytes()
.map(|b| {
if b.is_ascii_alphanumeric() || b"-._~".contains(&b) {
char::from(b).to_string()
} else {
format!("%{b:02X}")
}
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_ticket_is_encoded_for_the_url_and_a_value_quoted_for_curl() {
assert_eq!(encoded("PVEVNC:6A3F::a+b/c="), "PVEVNC%3A6A3F%3A%3Aa%2Bb%2Fc%3D");
assert_eq!(quoted(r#"a"b\c"#), r#""a\"b\\c""#);
assert_eq!(quoted("x\ny"), "\"x\\ny\"", "a new line can't start another option");
assert!(plain("pve-1") && !plain("pve/../x") && !plain(""));
}
#[test]
fn a_token_or_a_ticket_signs_the_websocket_in() {
let t = SignIn::Token("root@pam!x=s3cr3t".into());
assert_eq!(t.lines(true), ["header = \"Authorization: PVEAPIToken=root@pam!x=s3cr3t\""]);
assert_eq!(t.header().0, "Authorization");
let k = SignIn::Ticket {
ticket: "PVE:t".into(),
csrf: "c".into(),
};
assert_eq!(k.lines(true), ["cookie = \"PVEAuthCookie=PVE:t\"", "header = \"CSRFPreventionToken: c\""]);
assert_eq!(k.header(), ("Cookie".to_owned(), SecretValue::new("PVEAuthCookie=PVE:t")));
}
}