From ecc500af0561bf109c17bd225431d20d273dd93d Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Thu, 8 Apr 2021 16:17:49 -0700 Subject: [PATCH] ssh: don't override the ssh config User value --- config/src/ssh.rs | 12 ++++--- docs/changelog.md | 1 + mux/src/ssh.rs | 6 ++-- wezterm-client/src/client.rs | 9 +++-- wezterm-gui/src/main.rs | 7 ++-- wezterm-ssh/src/config.rs | 69 ++++++++++++++++++++++++++++++++++++ 6 files changed, 91 insertions(+), 13 deletions(-) diff --git a/config/src/ssh.rs b/config/src/ssh.rs index 04180ac4e..aebbf9fe0 100644 --- a/config/src/ssh.rs +++ b/config/src/ssh.rs @@ -32,13 +32,17 @@ impl_lua_conversion!(SshDomain); #[derive(Clone, Debug)] pub struct SshParameters { - pub username: String, + pub username: Option, pub host_and_port: String, } impl Display for SshParameters { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}@{}", self.username, self.host_and_port) + if let Some(user) = &self.username { + write!(f, "{}@{}", user, self.host_and_port) + } else { + write!(f, "{}", self.host_and_port) + } } } @@ -59,12 +63,12 @@ impl FromStr for SshParameters { if parts.len() == 2 { Ok(Self { - username: parts[0].to_string(), + username: Some(parts[0].to_string()), host_and_port: parts[1].to_string(), }) } else if parts.len() == 1 { Ok(Self { - username: username_from_env()?, + username: None, host_and_port: parts[0].to_string(), }) } else { diff --git a/docs/changelog.md b/docs/changelog.md index ce2569b68..e24822c27 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -17,6 +17,7 @@ As features stabilize some brief notes about them will accumulate here. * Fixed: support for "Variable" fonts such as Cascadia Code and Inconsolata on all platforms [#655](https://github.com/wez/wezterm/issues/655) * New: [wezterm.font](config/lua/wezterm/font.md) and [wezterm.font_with_fallback](config/lua/wezterm.font_with_fallback.md) *attributes* parameter now allows matching more granular font weights and font widths. e.g.: `wezterm.font('Iosevka Term', {width="Expanded", weight="Regular"})` * New: [freetype_render_target](config/lua/config/freetype_render_target.md) option for additional control over glyph rasterization. +* Fixed: `wezterm ssh HOST` no longer overrides the `User` config specified by `~/.ssh/config` ### 20210405-110924-a5bb5be8 diff --git a/mux/src/ssh.rs b/mux/src/ssh.rs index 89a3d12f7..b84553c89 100644 --- a/mux/src/ssh.rs +++ b/mux/src/ssh.rs @@ -53,7 +53,7 @@ impl LineEditorHost for PasswordPromptHost { pub fn ssh_connect_with_ui( remote_address: &str, - username: &str, + username: Option<&str>, ui: &mut ConnectionUI, ) -> anyhow::Result { let cloned_ui = ui.clone(); @@ -72,7 +72,9 @@ pub fn ssh_connect_with_ui( }; let mut ssh_config = ssh_config.for_host(&remote_host_name); - ssh_config.insert("user".to_string(), username.to_string()); + if let Some(username) = username { + ssh_config.insert("user".to_string(), username.to_string()); + } if let Some(port) = port { ssh_config.insert("port".to_string(), port.to_string()); } diff --git a/wezterm-client/src/client.rs b/wezterm-client/src/client.rs index 3be254c12..c5e0111f5 100644 --- a/wezterm-client/src/client.rs +++ b/wezterm-client/src/client.rs @@ -425,7 +425,7 @@ impl Reconnectable { initial: bool, ui: &mut ConnectionUI, ) -> anyhow::Result<()> { - let sess = ssh_connect_with_ui(&ssh_dom.remote_address, &ssh_dom.username, ui)?; + let sess = ssh_connect_with_ui(&ssh_dom.remote_address, Some(&ssh_dom.username), ui)?; let proxy_bin = Self::wezterm_bin_path(&ssh_dom.remote_wezterm_path); let cmd = if initial { @@ -577,8 +577,11 @@ impl Reconnectable { if let Some(Ok(ssh_params)) = tls_client.ssh_parameters() { if self.tls_creds.is_none() { // We need to bootstrap via an ssh session - let sess = - ssh_connect_with_ui(&ssh_params.host_and_port, &ssh_params.username, ui)?; + let sess = ssh_connect_with_ui( + &ssh_params.host_and_port, + ssh_params.username.as_ref().map(|s| s.as_str()), + ui, + )?; let creds = ui.run_and_log_error(|| { // The `tlscreds` command will start the server if needed and then diff --git a/wezterm-gui/src/main.rs b/wezterm-gui/src/main.rs index a53d2eaf5..de3414e59 100644 --- a/wezterm-gui/src/main.rs +++ b/wezterm-gui/src/main.rs @@ -97,10 +97,9 @@ async fn async_run_ssh(opts: SshCommand) -> anyhow::Result<()> { let port = fields.next(); let mut ssh_config = ssh_config.for_host(host); - ssh_config.insert( - "user".to_string(), - opts.user_at_host_and_port.username.to_string(), - ); + if let Some(username) = &opts.user_at_host_and_port.username { + ssh_config.insert("user".to_string(), username.to_string()); + } if let Some(port) = port { ssh_config.insert("port".to_string(), port.to_string()); } diff --git a/wezterm-ssh/src/config.rs b/wezterm-ssh/src/config.rs index bf1f3f431..92c205dc5 100644 --- a/wezterm-ssh/src/config.rs +++ b/wezterm-ssh/src/config.rs @@ -389,6 +389,75 @@ mod test { use super::*; use k9::snapshot; + #[test] + fn parse_user() { + let mut config = Config::new(); + + let mut fake_env = ConfigMap::new(); + fake_env.insert("HOME".to_string(), "/home/me".to_string()); + fake_env.insert("USER".to_string(), "me".to_string()); + config.assign_environment(fake_env); + + config.add_config_string( + r#" + Host foo + HostName 10.0.0.1 + User foo + IdentityFile "%d/.ssh/id_pub.dsa" + "#, + ); + + snapshot!( + &config, + r#" +Config { + config_files: [ + ParsedConfigFile { + options: {}, + groups: [ + HostGroup { + patterns: [ + Pattern { + negated: false, + pattern: "^foo$", + }, + ], + options: { + "hostname": "10.0.0.1", + "identityfile": "%d/.ssh/id_pub.dsa", + "user": "foo", + }, + }, + ], + }, + ], + options: {}, + tokens: {}, + environment: Some( + { + "HOME": "/home/me", + "USER": "me", + }, + ), +} +"# + ); + + let opts = config.for_host("foo"); + snapshot!( + opts, + r#" +{ + "hostname": "10.0.0.1", + "identityfile": "/home/me/.ssh/id_pub.dsa", + "port": "22", + "user": "foo", + "userknownhostsfile": "/home/me/.ssh/known_hosts /home/me/.ssh/known_hosts2", +} +"# + ); + } + #[test] fn parse_simple() { let mut config = Config::new();