Skip to content

Commit 72e3b78

Browse files
committed
BUG24953913: Fix SSL connection failing tests
This patch fixes the connection error using older Python versions or when using a non SSL enabled connection settings and the Python installation doesn't have support for SSL. The SSL connection tests were disabled for Python versions < 2.7.9.
1 parent c27c361 commit 72e3b78

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

lib/mysqlx/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def _parse_connection_uri(uri):
117117
settings["user"], settings["password"] = userinfo.split(":", 1)
118118

119119
if host.startswith(("/", "..", ".")):
120-
settings["socket"] = unquote(host).encode("utf-8")
120+
settings["socket"] = unquote(host)
121121
elif host.startswith("\\."):
122122
raise InterfaceError("Windows Pipe is not supported.")
123123
else:

lib/mysqlx/connection.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,6 @@ def set_ssl(self, ssl_opts={}):
8585
self.close()
8686
raise RuntimeError("Python installation has no SSL support.")
8787

88-
if sys.version_info < (2, 7, 9):
89-
self.close()
90-
raise RuntimeError("The support for SSL is not available for this "
91-
"Python version.")
92-
9388
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
9489
context.load_default_certs()
9590
if "ssl-ca" in ssl_opts:
@@ -166,13 +161,19 @@ def _handle_capabilities(self):
166161
data = self.protocol.get_capabilites().capabilities
167162
if not (data[0].name.lower() == "tls" if data else False):
168163
if self.settings.get("ssl-enable", False):
164+
self.close()
169165
raise OperationalError("SSL not enabled at server.")
170166
return
171167

172-
ssl_opts_keys = ("ssl-ca", "ssl-crl", "ssl-cert", "ssl-key",)
173-
if any(key in self.settings for key in ssl_opts_keys):
174-
self.protocol.set_capabilities(tls=True)
175-
self.stream.set_ssl(self.settings)
168+
if sys.version_info < (2, 7, 9):
169+
if self.settings.get("ssl-enable", False):
170+
self.close()
171+
raise RuntimeError("The support for SSL is not available for "
172+
"this Python version.")
173+
return
174+
175+
self.protocol.set_capabilities(tls=True)
176+
self.stream.set_ssl(self.settings)
176177

177178
def _authenticate(self):
178179
plugin = MySQL41AuthPlugin(self._user, self._password)

src/mysql_capi.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,7 @@ MySQL_connect(MySQL *self, PyObject *args, PyObject *kwds)
10331033
unsigned long client_flags= 0;
10341034
unsigned int port= 3306, tmp_uint;
10351035
unsigned int protocol= 0;
1036+
unsigned int ssl_mode;
10361037
my_bool abool;
10371038
MYSQL *res;
10381039

@@ -1115,15 +1116,17 @@ MySQL_connect(MySQL *self, PyObject *args, PyObject *kwds)
11151116
#endif
11161117
#if MYSQL_VERSION_ID >= 50711
11171118
{
1118-
mysql_options(&self->session, MYSQL_OPT_SSL_MODE, SSL_MODE_REQUIRED);
1119+
ssl_mode= SSL_MODE_REQUIRED;
1120+
mysql_options(&self->session, MYSQL_OPT_SSL_MODE, &ssl_mode);
11191121
}
11201122
#endif
11211123

11221124
if (ssl_verify_cert && ssl_verify_cert == Py_True)
11231125
{
11241126
#if MYSQL_VERSION_ID >= 50711
11251127
{
1126-
mysql_options(&self->session, MYSQL_OPT_SSL_MODE, SSL_MODE_VERIFY_IDENTITY);
1128+
ssl_mode= SSL_MODE_VERIFY_IDENTITY;
1129+
mysql_options(&self->session, MYSQL_OPT_SSL_MODE, &ssl_mode);
11271130
}
11281131
#else
11291132
{
@@ -1144,7 +1147,8 @@ MySQL_connect(MySQL *self, PyObject *args, PyObject *kwds)
11441147
#endif
11451148
#if MYSQL_VERSION_ID >= 50711
11461149
{
1147-
mysql_options(&self->session, MYSQL_OPT_SSL_ENFORCE, SSL_MODE_DISABLED);
1150+
ssl_mode= SSL_MODE_DISABLED;
1151+
mysql_options(&self->session, MYSQL_OPT_SSL_MODE, &ssl_mode);
11481152
}
11491153
#endif
11501154
}
@@ -1170,6 +1174,11 @@ MySQL_connect(MySQL *self, PyObject *args, PyObject *kwds)
11701174
client_flags= client_flags & ~CLIENT_CONNECT_WITH_DB;
11711175
}
11721176

1177+
if (client_flags & CLIENT_LOCAL_FILES) {
1178+
abool= 1;
1179+
mysql_options(&self->session, MYSQL_OPT_LOCAL_INFILE, (unsigned int*)&abool);
1180+
}
1181+
11731182
res= mysql_real_connect(&self->session,
11741183
host, user, password, database,
11751184
port, unix_socket, client_flags);

tests/test_mysqlx_connection.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
import tests
3232
import mysqlx
3333

34-
from urllib import quote
35-
34+
if mysqlx.compat.PY3:
35+
from urllib.parse import quote
36+
else:
37+
from urllib import quote
3638

3739
LOGGER = logging.getLogger(tests.LOGGER_NAME)
3840

0 commit comments

Comments
 (0)