Skip to content

Commit c30e042

Browse files
committed
Fixing SSH Tunnel logic for changes in the SSH library
At version 0.10.6, the libssh library introduced logic that could lead to an unhandled exception in the shell when setting an invalid hostname. Since SSH produces an exception with no code or error message, this patch adds the proper handling and a custom error message. Change-Id: If60f2d5a78866d10a2393d816168f48998b42acb
1 parent d8a2956 commit c30e042

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

mysqlshdk/libs/ssh/ssh_session.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,16 @@ void Ssh_session::authenticate_user(const ssh::Ssh_connection_options &config) {
564564
}
565565

566566
if (ret_val != Ssh_auth_return::AUTH_SUCCESS) {
567-
if (ret_val == Ssh_auth_return::AUTH_DENIED)
567+
if (ret_val == Ssh_auth_return::AUTH_DENIED) {
568568
throw Ssh_auth_exception("Access denied");
569-
else if (ret_val != Ssh_auth_return::AUTH_NONE)
569+
} else if (ret_val != Ssh_auth_return::AUTH_NONE) {
570570
throw std::runtime_error(
571571
"Unable to authenticate, all known authentication methods failed.");
572+
} else {
573+
throw std::runtime_error(
574+
"Unable to authenticate, no compatible authentication methods "
575+
"found.");
576+
}
572577
}
573578
}
574579

mysqlshdk/libs/ssh/ssh_session_options.cc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,20 @@ Ssh_session_options::Ssh_session_options(const std::string &config,
6060
m_no_host = true;
6161
return;
6262
}
63-
m_session->setOption(SSH_OPTIONS_HOST, host.c_str());
63+
try {
64+
m_session->setOption(SSH_OPTIONS_HOST, host.c_str());
65+
} catch (::ssh::SshException &exc) {
66+
auto error = exc.getError();
67+
if (error.empty()) {
68+
error = shcore::str_format(
69+
"SSH: Error setting remote host in ssh session: host '%s' is not "
70+
"valid",
71+
host.c_str());
72+
}
73+
74+
throw std::invalid_argument(error);
75+
}
76+
6477
try {
6578
// On empty we pass nullptr to trigger loading the default configuration
6679
// files

unittest/scripts/auto/py_shell/scripts/shell_ssh_errors_norecord.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,11 @@
127127

128128
#@<> Tunnel Creation Errors - Invalid SSH host
129129
testutil.call_mysqlsh(["--ssh", "ssh_user@invalid_host", MYSQL_OVER_SSH_URI])
130-
EXPECT_STDOUT_CONTAINS("Cannot open SSH Tunnel: Failed to resolve hostname invalid_host")
130+
EXPECT_STDOUT_CONTAINS("SSH: Error setting remote host in ssh session: host 'invalid_host' is not valid")
131+
WIPE_STDOUT()
132+
133+
testutil.call_mysqlsh(["--ssh", "ssh_user@Whatever", MYSQL_OVER_SSH_URI])
134+
EXPECT_STDOUT_CONTAINS("Cannot open SSH Tunnel: Failed to resolve hostname Whatever")
131135
WIPE_STDOUT()
132136

133137
#@<> Tunnel Creation Errors - Invalid User

unittest/scripts/auto/py_shell/scripts/shell_ssh_norecord.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ def check_connection(obj, connection_data):
362362
conns = [{'uri':MYSQL_OVER_SSH_URI, 'ssh':'ssh_user@invalid_host', 'ssh-password':'ssh_pwd'},
363363
{'uri':MYSQL_OVER_SSH_URI, 'ssh':'invalid_user@invalid_host', 'ssh-password':'ssh_pwd'}]
364364
for c in conns:
365-
EXPECT_THROWS(lambda: shell.connect(c), "Cannot open SSH Tunnel: Failed to resolve hostname invalid_host")
365+
EXPECT_THROWS(lambda: shell.connect(c), "SSH: Error setting remote host in ssh session: host 'invalid_host' is not valid")
366366

367367
EXPECT_THROWS(lambda: shell.connect({'uri':MYSQL_OVER_SSH_URI, 'ssh':'', 'ssh-password':'ssh_pwd'}), "Invalid URI: Expected token at position 0 but no tokens left.")
368368
EXPECT_THROWS(lambda: shell.connect({'uri':MYSQL_OVER_SSH_URI, 'ssh':'ssh_user@host name', 'ssh-password':'ssh_pwd'}), "Invalid URI: Illegal space found at position 13")

0 commit comments

Comments
 (0)