Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix up URIish instances with @ in user name #913

Merged
merged 2 commits into from Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.

- Folder names that were very long did not look right
- Error message for wrong SSH/HTTPS password now looks cleaner
- Fix authentication failure with usernames that contain the `@` character

### Added

Expand Down
Expand Up @@ -126,11 +126,24 @@ private fun makeTofuHostKeyVerifier(hostKeyFile: File): HostKeyVerifier {
}
}

private class SshjSession(private val uri: URIish, private val username: String, private val authData: SshAuthData, private val hostKeyFile: File) : RemoteSession {
private class SshjSession(uri: URIish, private val username: String, private val authData: SshAuthData, private val hostKeyFile: File) : RemoteSession {

private lateinit var ssh: SSHClient
private var currentCommand: Session? = null

private val uri = if (uri.host.contains('@')) {
// URIish's String constructor cannot handle '@' in the user part of the URI and the URL
// constructor can't be used since Java's URL does not recognize the ssh scheme. We thus
// need to patch everything up ourselves.
d { "Before fixup: user=${uri.user}, host=${uri.host}" }
val userPlusHost = "${uri.user}@${uri.host}"
val realUser = userPlusHost.substringBeforeLast('@')
val realHost = userPlusHost.substringAfterLast('@')
uri.setUser(realUser).setHost(realHost).also { d { "After fixup: user=${it.user}, host=${it.host}" } }
} else {
uri
}

fun connect(): SshjSession {
ssh = SSHClient(SshjConfig())
ssh.addHostKeyVerifier(makeTofuHostKeyVerifier(hostKeyFile))
Expand Down