isothrum-runner 2.1.1

The Isothrum runner: executes the run manifest Isoloom writes next to a target's files (lifecycle, state and locks, drivers, checks, events).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! A Proxmox VE VM's console, through its API (`source.from: proxmox_api`): `vncproxy` asked of
//! the VM with `curl`, signed in with the run's connection (an API token, else the user's
//! password through a ticket), its answer turned into the API's VNC websocket and the ticket that
//! opens it ([`ProxmoxVnc`]). The credentials reach `curl` in a config file the runner writes
//! owner-only in the run's state folder, pipes into it, and removes however the request ends
//! (a cancel, Ctrl-C included): never on its command line.

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;

/// How long one API call may go without progress.
const LIMIT: Duration = Duration::from_secs(30);

/// The config file `curl` reads, in the run's state folder, only while it runs ([`Transient`]).
const CONFIG: &str = "proxmox-console.curl";

/// Proxmox VE's VNC websocket for one VM, opened for one session: connect to `websocket` with
/// the `authorization` header (TLS unverified when `insecure`), then speak VNC with `ticket` as
/// the password. Every secret is a [`SecretValue`]: never print or log one.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ProxmoxVnc {
    /// `wss://<server>:8006/api2/json/nodes/<node>/qemu/<id>/vncwebsocket?port=<port>&vncticket=<ticket>`:
    /// it holds the ticket.
    pub websocket: SecretValue,
    /// The VNC port `vncproxy` opened on the node (in the URL already).
    pub port: u16,
    /// The ticket: the VNC password, and in the URL.
    pub ticket: SecretValue,
    /// The header that signs the websocket in: its name (`Authorization`, `Cookie`) and value.
    pub authorization: (String, SecretValue),
    /// The server's certificate isn't verified (a self-signed one: the run's `proxmox_insecure`).
    pub insecure: bool,
}

/// How the API is signed in to.
enum SignIn {
    /// An API token: `Authorization: PVEAPIToken=<token>`.
    Token(String),
    /// A ticket from `access/ticket`: the cookie, and the CSRF header a POST needs.
    Ticket { ticket: String, csrf: String },
}

impl SignIn {
    /// The config lines that sign a request in.
    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
            }
        }
    }

    /// The websocket's header.
    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}"))),
        }
    }
}

/// `machine`'s console as `console` (a `proxmox_api` one) says, `vncproxy` asked for it.
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,
    })
}

/// How the API is signed in to: the run's connection's API token (`token`), else the user's
/// password (`password`) traded for a ticket.
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")),
    }
}

/// The `data` of a POST to `url`, its `fields` (the config lines that sign it in and its form)
/// in the config file `curl` reads, removed once it ran or the request is cut short.
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))
}

/// What `curl` answered to a request to `url` (`method`: `POST`, or a GET), for step `step`, its
/// `fields` in the config file it reads, removed once it ran or the request is cut short.
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(&quoted(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)
}

/// Where each VM of the run with Proxmox's console stands on its server, by machine name, as the
/// server's API says (`cluster/resources`, every VM of the cluster it lets the run's credentials
/// see, wherever it migrated): running, stopped, or absent when the server no longer has it (its
/// web UI's Remove). `vars` holds the run's params and outputs (each VM's id). Nothing for a run
/// whose VMs have no such console, no id yet, or whose connection gives no credentials; nothing
/// either when the server can't be asked (unreachable, an answer that isn't the API's), traced:
/// the run's own state then stands.
///
/// # Errors
///
/// `server_auth_failed` when the server refuses the credentials (401, 403); a value of the
/// console that can't be filled.
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)
}

/// One VM as its Proxmox console names it, filled.
struct ApiVm<'m> {
    /// The API's address, without its last `/`.
    base: String,
    /// Its id on the server.
    id: String,
    /// The server's certificate isn't verified.
    insecure: bool,
    /// The console's sign-in: its user, token slot and password slot.
    sign_in: (
        &'m isoloom_manifest::Template,
        &'m isoloom_manifest::Identifier,
        &'m isoloom_manifest::Identifier,
    ),
}

/// The run's VMs with Proxmox's console and an id `vars` fills, by machine name.
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);
        // Before its first apply (or from a state of an older module), the run has no id.
        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)
}

/// How a status read signs in to the API: the connection's token, else a ticket for its password;
/// none when it gives neither (the run's state then stands).
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 }))
}

/// What `curl` answered a status read, when it ran to its end; a request that couldn't (`curl`
/// missing, the server silent past the limit) is traced, and the run's state then stands.
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()
}

/// The list a status read's `answer` holds; none when the API couldn't be asked (traced).
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()))
}

/// The `data` of the API's `answer`; none when it didn't answer one (unreachable, not the API),
/// which is traced.
///
/// # Errors
///
/// `server_auth_failed` when it refused the credentials.
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)
        }
    }
}

/// `s` as a double-quoted value of a curl config file (`\` and `"` escaped).
fn quoted(s: &str) -> String {
    format!("\"{}\"", s.replace('\\', "\\\\").replace('"', "\\\"").replace('\n', "\\n").replace('\r', "\\r"))
}

/// Whether `s` is a node name that goes in a URL path as it is.
fn plain(s: &str) -> bool {
    !s.is_empty() && s.chars().all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '.')
}

/// `s` percent-encoded for a query value: every byte but an unreserved one.
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")));
    }
}