diff --git a/AUTHORS b/AUTHORS index b96d20bb0d..279c1a4dd4 100644 --- a/AUTHORS +++ b/AUTHORS @@ -695,6 +695,7 @@ answer newbie questions, and generally made Django that much better: Gasper Zejn Jarek Zgoda Cheng Zhang + Peter Zsoldos A big THANK YOU goes to: diff --git a/django/db/backends/mysql/client.py b/django/db/backends/mysql/client.py index 2106c4d3da..dc1d9846b8 100644 --- a/django/db/backends/mysql/client.py +++ b/django/db/backends/mysql/client.py @@ -6,14 +6,15 @@ from django.db.backends import BaseDatabaseClient class DatabaseClient(BaseDatabaseClient): executable_name = 'mysql' - def runshell(self): - settings_dict = self.connection.settings_dict - args = [self.executable_name] + @classmethod + def settings_to_cmd_args(cls, settings_dict): + args = [cls.executable_name] db = settings_dict['OPTIONS'].get('db', settings_dict['NAME']) user = settings_dict['OPTIONS'].get('user', settings_dict['USER']) passwd = settings_dict['OPTIONS'].get('passwd', settings_dict['PASSWORD']) host = settings_dict['OPTIONS'].get('host', settings_dict['HOST']) port = settings_dict['OPTIONS'].get('port', settings_dict['PORT']) + cert = settings_dict['OPTIONS'].get('ssl', {}).get('ca') defaults_file = settings_dict['OPTIONS'].get('read_default_file') # Seems to be no good way to set sql_mode with CLI. @@ -30,7 +31,12 @@ class DatabaseClient(BaseDatabaseClient): args += ["--host=%s" % host] if port: args += ["--port=%s" % port] + if cert: + args += ["--ssl-ca=%s" % cert] if db: args += [db] + return args + def runshell(self): + args = DatabaseClient.settings_to_cmd_args(self.connection.settings_dict) subprocess.call(args) diff --git a/docs/releases/1.8.txt b/docs/releases/1.8.txt index 86d39c4f88..6569a2c447 100644 --- a/docs/releases/1.8.txt +++ b/docs/releases/1.8.txt @@ -203,6 +203,9 @@ Management Commands command is now always the ``dest`` name specified in the command option definition (as long as the command uses the new :py:mod:`argparse` module). +* The :djadmin:`dbshell` command now supports MySQL's optional SSL certificate + authority setting (``--ssl-ca``). + Models ^^^^^^ diff --git a/tests/dbshell/__init__.py b/tests/dbshell/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/dbshell/tests.py b/tests/dbshell/tests.py new file mode 100644 index 0000000000..7efb97d9cb --- /dev/null +++ b/tests/dbshell/tests.py @@ -0,0 +1,73 @@ +from django.db.backends.mysql.client import DatabaseClient +from django.test import SimpleTestCase + + +class MySqlDbshellCommandTestCase(SimpleTestCase): + + def test_fails_with_keyerror_on_incomplete_config(self): + with self.assertRaises(KeyError): + self.get_command_line_arguments({}) + + def test_basic_params_specified_in_settings(self): + self.assertEqual( + ['mysql', '--user=someuser', '--password=somepassword', + '--host=somehost', '--port=444', 'somedbname'], + self.get_command_line_arguments({ + 'NAME': 'somedbname', + 'USER': 'someuser', + 'PASSWORD': 'somepassword', + 'HOST': 'somehost', + 'PORT': 444, + 'OPTIONS': {}, + })) + + def test_options_override_settings_proper_values(self): + settings_port = 444 + options_port = 555 + self.assertNotEqual(settings_port, options_port, 'test pre-req') + self.assertEqual( + ['mysql', '--user=optionuser', '--password=optionpassword', + '--host=optionhost', '--port={}'.format(options_port), 'optiondbname'], + self.get_command_line_arguments({ + 'NAME': 'settingdbname', + 'USER': 'settinguser', + 'PASSWORD': 'settingpassword', + 'HOST': 'settinghost', + 'PORT': settings_port, + 'OPTIONS': { + 'db': 'optiondbname', + 'user': 'optionuser', + 'passwd': 'optionpassword', + 'host': 'optionhost', + 'port': options_port, + }, + })) + + def test_can_connect_using_sockets(self): + self.assertEqual( + ['mysql', '--user=someuser', '--password=somepassword', + '--socket=/path/to/mysql.socket.file', 'somedbname'], + self.get_command_line_arguments({ + 'NAME': 'somedbname', + 'USER': 'someuser', + 'PASSWORD': 'somepassword', + 'HOST': '/path/to/mysql.socket.file', + 'PORT': None, + 'OPTIONS': {}, + })) + + def test_ssl_certificate_is_added(self): + self.assertEqual( + ['mysql', '--user=someuser', '--password=somepassword', + '--host=somehost', '--port=444', '--ssl-ca=sslca', 'somedbname'], + self.get_command_line_arguments({ + 'NAME': 'somedbname', + 'USER': 'someuser', + 'PASSWORD': 'somepassword', + 'HOST': 'somehost', + 'PORT': 444, + 'OPTIONS': {'ssl': {'ca': 'sslca'}}, + })) + + def get_command_line_arguments(self, connection_settings): + return DatabaseClient.settings_to_cmd_args(connection_settings)