Kaydet (Commit) 01801edd authored tarafından zsoldosp's avatar zsoldosp Kaydeden (comit) Tim Graham

Fixed #22646: Added support for the MySQL ssl-ca option to dbshell.

üst 69478859
...@@ -695,6 +695,7 @@ answer newbie questions, and generally made Django that much better: ...@@ -695,6 +695,7 @@ answer newbie questions, and generally made Django that much better:
Gasper Zejn <zejn@kiberpipa.org> Gasper Zejn <zejn@kiberpipa.org>
Jarek Zgoda <jarek.zgoda@gmail.com> Jarek Zgoda <jarek.zgoda@gmail.com>
Cheng Zhang Cheng Zhang
Peter Zsoldos <http://zsoldosp.eu>
<Please alphabetize new entries> <Please alphabetize new entries>
A big THANK YOU goes to: A big THANK YOU goes to:
......
...@@ -6,14 +6,15 @@ from django.db.backends import BaseDatabaseClient ...@@ -6,14 +6,15 @@ from django.db.backends import BaseDatabaseClient
class DatabaseClient(BaseDatabaseClient): class DatabaseClient(BaseDatabaseClient):
executable_name = 'mysql' executable_name = 'mysql'
def runshell(self): @classmethod
settings_dict = self.connection.settings_dict def settings_to_cmd_args(cls, settings_dict):
args = [self.executable_name] args = [cls.executable_name]
db = settings_dict['OPTIONS'].get('db', settings_dict['NAME']) db = settings_dict['OPTIONS'].get('db', settings_dict['NAME'])
user = settings_dict['OPTIONS'].get('user', settings_dict['USER']) user = settings_dict['OPTIONS'].get('user', settings_dict['USER'])
passwd = settings_dict['OPTIONS'].get('passwd', settings_dict['PASSWORD']) passwd = settings_dict['OPTIONS'].get('passwd', settings_dict['PASSWORD'])
host = settings_dict['OPTIONS'].get('host', settings_dict['HOST']) host = settings_dict['OPTIONS'].get('host', settings_dict['HOST'])
port = settings_dict['OPTIONS'].get('port', settings_dict['PORT']) 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') defaults_file = settings_dict['OPTIONS'].get('read_default_file')
# Seems to be no good way to set sql_mode with CLI. # Seems to be no good way to set sql_mode with CLI.
...@@ -30,7 +31,12 @@ class DatabaseClient(BaseDatabaseClient): ...@@ -30,7 +31,12 @@ class DatabaseClient(BaseDatabaseClient):
args += ["--host=%s" % host] args += ["--host=%s" % host]
if port: if port:
args += ["--port=%s" % port] args += ["--port=%s" % port]
if cert:
args += ["--ssl-ca=%s" % cert]
if db: if db:
args += [db] args += [db]
return args
def runshell(self):
args = DatabaseClient.settings_to_cmd_args(self.connection.settings_dict)
subprocess.call(args) subprocess.call(args)
...@@ -203,6 +203,9 @@ Management Commands ...@@ -203,6 +203,9 @@ Management Commands
command is now always the ``dest`` name specified in the command option 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). 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 Models
^^^^^^ ^^^^^^
......
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)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment