wezterm: improve error reporting around mux connection failures

This commit is contained in:
Wez Furlong committed 2020-06-06 09:46:25 -07:00
1 parent d660f53f86
commit 2e7f082bd7
4 files changed
+233 -192

No files matched your search

+30 -1
View File
@@ -234,7 +234,7 @@ impl ConnectionUI {
}
ui.sleep(
"(this window will close automatically)",
Duration::new(10, 0),
Duration::new(120, 0),
)
.ok();
Ok(())
@@ -263,6 +263,35 @@ impl ConnectionUI {
Self { tx }
}
pub fn run_and_log_error<T, F>(&self, f: F) -> anyhow::Result<T>
where
F: FnOnce() -> anyhow::Result<T>,
{
match f() {
Err(e) => {
let what = format!("\r\nFailed: {:?}\r\n", e);
log::error!("{}", what);
self.output_str(&what);
Err(e)
}
result => result,
}
}
pub async fn async_run_and_log_error<T, F>(&self, f: F) -> anyhow::Result<T>
where
F: std::future::Future<Output = anyhow::Result<T>>,
{
match f.await {
Err(e) => {
let what = format!("\r\nFailed: {:?}\r\n", e);
self.output_str(&what);
Err(e)
}
result => result,
}
}
pub fn title(&self, title: &str) {
self.output(vec![Change::Title(title.to_string())]);
}
+45 -35
View File
@@ -520,50 +520,60 @@ impl Reconnectable {
// We need to bootstrap via an ssh session
let sess =
ssh_connect_with_ui(&ssh_params.host_and_port, &ssh_params.username, ui)?;
let mut chan = sess.channel_session()?;
// The `tlscreds` command will start the server if needed and then
// obtain client credentials that we can use for tls.
let cmd = format!("{} cli tlscreds", Self::wezterm_bin_path());
ui.output_str(&format!("Running: {}\n", cmd));
chan.exec(&cmd)
.with_context(|| format!("executing `{}` on remote host", cmd))?;
let creds = ui.run_and_log_error(|| {
let mut chan = sess.channel_session()?;
// stdout holds an encoded pdu
let mut buf = Vec::new();
chan.read_to_end(&mut buf)
.context("reading tlscreds response to buffer")?;
// The `tlscreds` command will start the server if needed and then
// obtain client credentials that we can use for tls.
let cmd = format!("{} cli tlscreds", Self::wezterm_bin_path());
ui.output_str(&format!("Running: {}\n", cmd));
chan.exec(&cmd)
.with_context(|| format!("executing `{}` on remote host", cmd))?;
// stderr is ideally empty
let mut err = String::new();
chan.stderr()
.read_to_string(&mut err)
.context("reading tlscreds stderr")?;
if !err.is_empty() {
log::error!("remote: `{}` stderr -> `{}`", cmd, err);
}
// stdout holds an encoded pdu
let mut buf = Vec::new();
chan.read_to_end(&mut buf)
.context("reading tlscreds response to buffer")?;
let creds = match Pdu::decode(buf.as_slice())
.with_context(|| format!("reading tlscreds response. stderr={}", err))?
.pdu
{
Pdu::GetTlsCredsResponse(creds) => creds,
_ => bail!("unexpected response to tlscreds, stderr={}", err),
};
chan.send_eof()?;
chan.wait_eof()?;
// Save the credentials to disk, as that is currently the easiest
// way to get them into openssl. Ideally we'd keep these entirely
// in memory.
std::fs::write(&self.tls_creds_ca_path()?, creds.ca_cert_pem.as_bytes())?;
std::fs::write(
&self.tls_creds_cert_path()?,
creds.client_cert_pem.as_bytes(),
)?;
// stderr is ideally empty
let mut err = String::new();
chan.stderr()
.read_to_string(&mut err)
.context("reading tlscreds stderr")?;
if !err.is_empty() {
log::error!("remote: `{}` stderr -> `{}`", cmd, err);
}
let creds = match Pdu::decode(buf.as_slice())
.with_context(|| format!("reading tlscreds response. stderr={}", err))?
.pdu
{
Pdu::GetTlsCredsResponse(creds) => creds,
_ => bail!("unexpected response to tlscreds, stderr={}", err),
};
// Save the credentials to disk, as that is currently the easiest
// way to get them into openssl. Ideally we'd keep these entirely
// in memory.
std::fs::write(&self.tls_creds_ca_path()?, creds.ca_cert_pem.as_bytes())?;
std::fs::write(
&self.tls_creds_cert_path()?,
creds.client_cert_pem.as_bytes(),
)?;
Ok(creds)
})?;
self.tls_creds.replace(creds);
}
}
let stream = self.try_connect(&tls_client, ui, &remote_address, remote_host_name)?;
let cloned_ui = ui.clone();
let stream = cloned_ui.run_and_log_error({
|| self.try_connect(&tls_client, ui, &remote_address, remote_host_name)
})?;
self.stream.replace(stream);
Ok(())
}
+24 -17
View File
@@ -342,26 +342,33 @@ impl Domain for ClientDomain {
let ui = ConnectionUI::new();
ui.title("wezterm: Connecting...");
let mut cloned_ui = ui.clone();
let client = join_handle_result(spawn_into_new_thread(move || match &config {
ClientDomainConfig::Unix(unix) => {
let initial = true;
Client::new_unix_domain(domain_id, unix, initial, &mut cloned_ui)
ui.async_run_and_log_error({
let ui = ui.clone();
async move {
let mut cloned_ui = ui.clone();
let client = join_handle_result(spawn_into_new_thread(move || match &config {
ClientDomainConfig::Unix(unix) => {
let initial = true;
Client::new_unix_domain(domain_id, unix, initial, &mut cloned_ui)
}
ClientDomainConfig::Tls(tls) => Client::new_tls(domain_id, tls, &mut cloned_ui),
ClientDomainConfig::Ssh(ssh) => Client::new_ssh(domain_id, ssh, &mut cloned_ui),
}))
.await?;
client.verify_version_compat(&ui).await?;
ui.output_str("Version check OK! Requesting tab list...\n");
let tabs = client.list_tabs().await?;
ui.output_str(&format!(
"Server has {} tabs. Attaching to local UI...\n",
tabs.tabs.len()
));
ClientDomain::finish_attach(domain_id, client, tabs)
}
ClientDomainConfig::Tls(tls) => Client::new_tls(domain_id, tls, &mut cloned_ui),
ClientDomainConfig::Ssh(ssh) => Client::new_ssh(domain_id, ssh, &mut cloned_ui),
}))
})
.await?;
client.verify_version_compat(&ui).await?;
ui.output_str("Version check OK! Requesting tab list...\n");
let tabs = client.list_tabs().await?;
ui.output_str(&format!(
"Server has {} tabs. Attaching to local UI...\n",
tabs.tabs.len()
));
ClientDomain::finish_attach(domain_id, client, tabs)?;
ui.output_str("Attached!\n");
drop(activity);
ui.close();
+134 -139
View File
@@ -51,174 +51,169 @@ pub fn ssh_connect_with_ui(
username: &str,
ui: &mut ConnectionUI,
) -> anyhow::Result<ssh2::Session> {
let mut sess = ssh2::Session::new()?;
let cloned_ui = ui.clone();
cloned_ui.run_and_log_error(move || {
let mut sess = ssh2::Session::new()?;
let (remote_address, remote_host_name, port) = {
let parts: Vec<&str> = remote_address.split(':').collect();
let (remote_address, remote_host_name, port) = {
let parts: Vec<&str> = remote_address.split(':').collect();
if parts.len() == 2 {
(remote_address.to_string(), parts[0], parts[1].parse()?)
} else {
(format!("{}:22", remote_address), remote_address, 22)
}
};
if parts.len() == 2 {
(remote_address.to_string(), parts[0], parts[1].parse()?)
} else {
(format!("{}:22", remote_address), remote_address, 22)
}
};
ui.output_str(&format!("Connecting to {} using SSH\n", remote_address));
ui.output_str(&format!("Connecting to {} using SSH\n", remote_address));
let tcp = TcpStream::connect(&remote_address)
.with_context(|| format!("ssh connecting to {}", remote_address))?;
ui.output_str("SSH: Connected OK!\n");
tcp.set_nodelay(true)?;
sess.set_tcp_stream(tcp);
sess.handshake()
.with_context(|| format!("ssh handshake with {}", remote_address))?;
let tcp = TcpStream::connect(&remote_address)
.with_context(|| format!("ssh connecting to {}", remote_address))?;
ui.output_str("SSH: Connected OK!\n");
tcp.set_nodelay(true)?;
sess.set_tcp_stream(tcp);
sess.handshake()
.with_context(|| format!("ssh handshake with {}", remote_address))?;
if let Ok(mut known_hosts) = sess.known_hosts() {
let varname = if cfg!(windows) { "USERPROFILE" } else { "HOME" };
let var = std::env::var_os(varname)
.ok_or_else(|| anyhow!("environment variable {} is missing", varname))?;
let file = Path::new(&var).join(".ssh/known_hosts");
if file.exists() {
known_hosts
.read_file(&file, ssh2::KnownHostFileKind::OpenSSH)
.with_context(|| format!("reading known_hosts file {}", file.display()))?;
}
if let Ok(mut known_hosts) = sess.known_hosts() {
let varname = if cfg!(windows) { "USERPROFILE" } else { "HOME" };
let var = std::env::var_os(varname)
.ok_or_else(|| anyhow!("environment variable {} is missing", varname))?;
let file = Path::new(&var).join(".ssh/known_hosts");
if file.exists() {
known_hosts
.read_file(&file, ssh2::KnownHostFileKind::OpenSSH)
.with_context(|| format!("reading known_hosts file {}", file.display()))?;
}
let (key, key_type) = sess
.host_key()
.ok_or_else(|| anyhow!("failed to get ssh host key"))?;
let (key, key_type) = sess
.host_key()
.ok_or_else(|| anyhow!("failed to get ssh host key"))?;
let fingerprint = sess
.host_key_hash(ssh2::HashType::Sha256)
.map(|fingerprint| {
format!(
"SHA256:{}",
base64::encode_config(
fingerprint,
base64::Config::new(base64::CharacterSet::Standard, false)
let fingerprint = sess
.host_key_hash(ssh2::HashType::Sha256)
.map(|fingerprint| {
format!(
"SHA256:{}",
base64::encode_config(
fingerprint,
base64::Config::new(base64::CharacterSet::Standard, false)
)
)
)
})
.or_else(|| {
// Querying for the Sha256 can fail if for example we were linked
// against libssh < 1.9, so let's fall back to Sha1 in that case.
sess.host_key_hash(ssh2::HashType::Sha1).map(|fingerprint| {
let mut res = vec![];
write!(&mut res, "SHA1").ok();
for b in fingerprint {
write!(&mut res, ":{:02x}", *b).ok();
}
String::from_utf8(res).unwrap()
})
})
.ok_or_else(|| anyhow!("failed to get host fingerprint"))?;
.or_else(|| {
// Querying for the Sha256 can fail if for example we were linked
// against libssh < 1.9, so let's fall back to Sha1 in that case.
sess.host_key_hash(ssh2::HashType::Sha1).map(|fingerprint| {
let mut res = vec![];
write!(&mut res, "SHA1").ok();
for b in fingerprint {
write!(&mut res, ":{:02x}", *b).ok();
}
String::from_utf8(res).unwrap()
})
})
.ok_or_else(|| anyhow!("failed to get host fingerprint"))?;
use ssh2::CheckResult;
match known_hosts.check_port(&remote_host_name, port, key) {
CheckResult::Match => {}
CheckResult::NotFound => {
ui.output_str(&format!(
"SSH host {} is not yet trusted.\n\
{:?} Fingerprint: {}.\n\
Trust and continue connecting?\n",
remote_address, key_type, fingerprint
));
use ssh2::CheckResult;
match known_hosts.check_port(&remote_host_name, port, key) {
CheckResult::Match => {}
CheckResult::NotFound => {
ui.output_str(&format!(
"SSH host {} is not yet trusted.\n\
{:?} Fingerprint: {}.\n\
Trust and continue connecting?\n",
remote_address, key_type, fingerprint
));
loop {
let line = ui.input("Enter [Y/n]> ")?;
loop {
let line = ui.input("Enter [Y/n]> ")?;
match line.as_ref() {
"y" | "Y" | "yes" | "YES" => break,
"n" | "N" | "no" | "NO" => bail!("user declined to trust host"),
_ => continue,
match line.as_ref() {
"y" | "Y" | "yes" | "YES" => break,
"n" | "N" | "no" | "NO" => bail!("user declined to trust host"),
_ => continue,
}
}
known_hosts
.add(remote_host_name, key, &remote_address, key_type.into())
.context("adding known_hosts entry in memory")?;
known_hosts
.write_file(&file, ssh2::KnownHostFileKind::OpenSSH)
.with_context(|| format!("writing known_hosts file {}", file.display()))?;
}
CheckResult::Mismatch => {
ui.output_str(&format!(
"🛑 host key mismatch for ssh server {}.\n\
Got fingerprint {} instead of expected value from known_hosts\n\
file {}.\n\
Refusing to connect.\n",
remote_address,
fingerprint,
file.display()
));
bail!("host mismatch, man in the middle attack?!");
}
CheckResult::Failure => {
ui.output_str("🛑 Failed to load and check known ssh hosts\n");
bail!("failed to check the known hosts");
}
known_hosts
.add(remote_host_name, key, &remote_address, key_type.into())
.context("adding known_hosts entry in memory")?;
known_hosts
.write_file(&file, ssh2::KnownHostFileKind::OpenSSH)
.with_context(|| format!("writing known_hosts file {}", file.display()))?;
}
CheckResult::Mismatch => {
}
for _ in 0..3 {
if sess.authenticated() {
break;
}
// Re-query the auth methods on each loop as a successful method
// may unlock a new method on a subsequent iteration (eg: password
// auth may then unlock 2fac)
let methods: HashSet<&str> = sess.auth_methods(&username)?.split(',').collect();
log::trace!("ssh auth methods: {:?}", methods);
if !sess.authenticated() && methods.contains("publickey") {
if let Err(err) = sess.userauth_agent(&username) {
log::info!("while attempting agent auth: {}", err);
} else if sess.authenticated() {
ui.output_str("publickey auth successful!\n");
}
}
if !sess.authenticated() && methods.contains("password") {
ui.output_str(&format!(
"🛑 host key mismatch for ssh server {}.\n\
Got fingerprint {} instead of expected value from known_hosts\n\
file {}.\n\
Refusing to connect.\n",
remote_address,
fingerprint,
file.display()
"Password authentication for {}@{}\n",
username, remote_address
));
bail!("host mismatch, man in the middle attack?!");
let pass = ui.password("🔐 Password: ")?;
if let Err(err) = sess.userauth_password(username, &pass) {
log::error!("while attempting password auth: {}", err);
}
}
CheckResult::Failure => {
ui.output_str("🛑 Failed to load and check known ssh hosts\n");
bail!("failed to check the known hosts");
}
}
}
for _ in 0..3 {
if sess.authenticated() {
break;
}
// Re-query the auth methods on each loop as a successful method
// may unlock a new method on a subsequent iteration (eg: password
// auth may then unlock 2fac)
let methods: HashSet<&str> = sess.auth_methods(&username)?.split(',').collect();
log::trace!("ssh auth methods: {:?}", methods);
if !sess.authenticated() && methods.contains("publickey") {
if let Err(err) = sess.userauth_agent(&username) {
log::info!("while attempting agent auth: {}", err);
} else if sess.authenticated() {
ui.output_str("publickey auth successful!\n");
if !sess.authenticated() && methods.contains("keyboard-interactive") {
if let Err(err) = sess.userauth_keyboard_interactive(&username, ui) {
log::error!("while attempting keyboard-interactive auth: {}", err);
}
}
}
if !sess.authenticated() && methods.contains("password") {
ui.output_str(&format!(
"Password authentication for {}@{}\n",
username, remote_address
));
let pass = ui.password("🔐 Password: ")?;
if let Err(err) = sess.userauth_password(username, &pass) {
log::error!("while attempting password auth: {}", err);
}
if !sess.authenticated() {
bail!("unable to authenticate session");
}
if !sess.authenticated() && methods.contains("keyboard-interactive") {
if let Err(err) = sess.userauth_keyboard_interactive(&username, ui) {
log::error!("while attempting keyboard-interactive auth: {}", err);
}
}
}
if !sess.authenticated() {
bail!("unable to authenticate session");
}
Ok(sess)
Ok(sess)
})
}
pub fn ssh_connect(remote_address: &str, username: &str) -> anyhow::Result<ssh2::Session> {
let mut ui = ConnectionUI::new();
ui.title("🔐 wezterm: SSH authentication");
let res = ssh_connect_with_ui(remote_address, username, &mut ui);
match res {
Ok(sess) => {
ui.close();
Ok(sess)
}
Err(err) => {
ui.output_str(&format!("\nFailed: {}", err));
Err(err)
}
}
let sess = ssh_connect_with_ui(remote_address, username, &mut ui)?;
ui.close();
Ok(sess)
}
pub struct RemoteSshDomain {