diff --git a/Cargo.lock b/Cargo.lock index 17e8f55..c12947b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -21,9 +21,9 @@ dependencies = [ [[package]] name = "actix-http" -version = "3.20.3" +version = "3.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd4ef186188fed2d4f8177d712ad623c751c6e84f45b31da694cc53f483f320b" +checksum = "50f98f47b03911915aa3f0b856749aa3d3915033afabb295ab6ac5ff3b8bd5c1" dependencies = [ "actix-codec", "actix-service", @@ -847,7 +847,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -1748,7 +1748,7 @@ dependencies = [ [[package]] name = "omnect-cli" -version = "0.29.0" +version = "0.29.1" dependencies = [ "actix-web", "anyhow", @@ -2239,15 +2239,15 @@ dependencies = [ [[package]] name = "rustix" -version = "1.1.5" +version = "1.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "891efababe418670775f199f0d233d84843c227a0949a883ce15b37c78d6629d" +checksum = "e5e76742e5294153d4dde1207dbec5bdd81eef6fabdc0d2118babadabfd858aa" dependencies = [ "bitflags", "errno", "libc", "linux-raw-sys", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -2651,7 +2651,7 @@ dependencies = [ "getrandom 0.4.3", "once_cell", "rustix", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index d9077bc..e138ec1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ license = "MIT OR Apache-2.0" name = "omnect-cli" readme = "README.md" repository = "https://github.com/omnect/omnect-cli" -version = "0.29.0" +version = "0.29.1" [dependencies] actix-web = { version = "4.17", default-features = false, features = ["macros"] } diff --git a/src/ssh.rs b/src/ssh.rs index fc7badc..23db2e8 100644 --- a/src/ssh.rs +++ b/src/ssh.rs @@ -13,6 +13,7 @@ use url::Url; static BACKEND_API_ENDPOINT: &str = "/api/devices/prepareSSHConnection"; static SSH_KEY_FORMAT: &str = "ed25519"; +const MAX_ERROR_BODY_CHARS: usize = 200; static BASTION_CERT_NAME: &str = "bastion-cert.pub"; static DEVICE_CERT_NAME: &str = "device-cert.pub"; @@ -181,28 +182,76 @@ struct SshTunnelInfo { async fn unpack_response(response: reqwest::Response) -> Result { let status = response.status(); - let body = response - .text() - .await - .map_err(|err| anyhow!("could not read response body: {err}"))?; + let body = response.text().await.map_err(|err| { + anyhow!( + "{}: could not read response body: {err}", + status_text(status) + ) + })?; if !status.is_success() { - #[derive(Deserialize)] - struct ErrorMessage { - #[serde(rename = "internalMsg")] - internal_message: String, + anyhow::bail!(error_message(status, &body)) + } else { + serde_json::from_str(&body).map_err(|err| { + anyhow!( + "unsupported reply: {err}: {}", + with_body(status_text(status), &body) + ) + }) + } +} + +fn error_message(status: reqwest::StatusCode, body: &str) -> String { + #[derive(Deserialize)] + struct ErrorMessage { + #[serde(rename = "internalMsg")] + internal_message: String, + } + + if let Ok(err) = serde_json::from_str::(body) { + let message = printable(&err.internal_message); + if !message.is_empty() { + return message; } + } + + let message = with_body(status_text(status), body); - anyhow::bail!( - serde_json::from_str::(&body) - .map(|err| err.internal_message) - .unwrap_or_else(|_| "unknown error type".to_string()) + // A 403 without the backend's error format can also come from a network + // filter in front of the backend, e.g. one that only allows a VPN. + if status == reqwest::StatusCode::FORBIDDEN { + format!( + "{message} (check that your account has access to the device, and that you are on a network the backend allows, e.g. a VPN)" ) } else { - serde_json::from_str(&body).map_err(|_| anyhow!("unsupported reply.")) + message + } +} + +fn status_text(status: reqwest::StatusCode) -> String { + match status.canonical_reason() { + Some(reason) => format!("HTTP {} {reason}", status.as_u16()), + None => format!("HTTP {}", status.as_u16()), } } +fn with_body(status: String, body: &str) -> String { + match printable(body) { + shown if shown.is_empty() => status, + shown => format!("{status}: {shown}"), + } +} + +/// Server text can be a long HTML error page or hold terminal escape sequences. +fn printable(text: &str) -> String { + let mut chars = text.chars().map(|c| if c.is_control() { ' ' } else { c }); + let mut shown: String = chars.by_ref().take(MAX_ERROR_BODY_CHARS).collect(); + if chars.next().is_some() { + shown.push_str("..."); + } + shown.trim().to_string() +} + async fn request_ssh_tunnel( backend: &Url, device_id: &str, @@ -467,6 +516,105 @@ pub async fn ssh_create_tunnel( mod test { use super::*; + #[test] + fn error_message_takes_internal_msg() { + assert_eq!( + error_message( + reqwest::StatusCode::BAD_REQUEST, + r#"{"internalMsg":"Device may not be online"}"# + ), + "Device may not be online" + ); + } + + #[test] + fn error_message_shows_status_and_body_of_unknown_reply() { + assert_eq!( + error_message(reqwest::StatusCode::UNAUTHORIZED, "Unauthorized\n"), + "HTTP 401 Unauthorized: Unauthorized" + ); + } + + #[test] + fn error_message_names_access_and_network_on_forbidden() { + let message = error_message(reqwest::StatusCode::FORBIDDEN, "Forbidden"); + + assert!(message.starts_with("HTTP 403 Forbidden: Forbidden (")); + assert!(message.contains("VPN")); + } + + #[tokio::test] + async fn unpack_response_shows_status_and_body_of_unsupported_reply() { + let server = httpmock::MockServer::start_async().await; + server + .mock_async(|when, then| { + when.any_request(); + then.status(200).body("login"); + }) + .await; + let response = reqwest::get(server.url("/")).await.unwrap(); + + let err = unpack_response::(response) + .await + .err() + .unwrap() + .to_string(); + + assert!(err.starts_with("unsupported reply: ")); + assert!(err.ends_with("HTTP 200 OK: login")); + } + + #[test] + fn error_message_shows_status_of_empty_reply() { + assert_eq!( + error_message(reqwest::StatusCode::BAD_GATEWAY, ""), + "HTTP 502 Bad Gateway" + ); + } + + #[test] + fn error_message_ignores_empty_internal_msg() { + assert_eq!( + error_message( + reqwest::StatusCode::INTERNAL_SERVER_ERROR, + r#"{"internalMsg":" "}"# + ), + "HTTP 500 Internal Server Error: {\"internalMsg\":\" \"}" + ); + } + + #[test] + fn error_message_drops_control_chars_of_internal_msg() { + assert_eq!( + error_message( + reqwest::StatusCode::BAD_REQUEST, + r#"{"internalMsg":"Device\u001b[2J offline\n"}"# + ), + "Device [2J offline" + ); + } + + #[test] + fn error_message_shows_status_without_reason() { + assert_eq!( + error_message(reqwest::StatusCode::from_u16(599).unwrap(), ""), + "HTTP 599" + ); + } + + #[test] + fn error_message_shortens_long_body_and_drops_control_chars() { + let body = format!("a\x1b[2J{}", "x".repeat(MAX_ERROR_BODY_CHARS)); + let message = error_message(reqwest::StatusCode::BAD_GATEWAY, &body); + + assert!(!message.contains('\x1b')); + assert!(message.ends_with("...")); + assert_eq!( + message.chars().count(), + "HTTP 502 Bad Gateway: ".len() + MAX_ERROR_BODY_CHARS + "...".len() + ); + } + fn test_query_yes_no_for_result( input: &str, expected_result: bool,