Skip to content
This repository was archived by the owner on Oct 27, 2021. It is now read-only.

Commit 01801ed

Browse files
zsoldosptimgraham
authored andcommitted
Fixed #22646: Added support for the MySQL ssl-ca option to dbshell.
1 parent 6947885 commit 01801ed

File tree

5 files changed

+86
-3
lines changed

5 files changed

+86
-3
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,7 @@ answer newbie questions, and generally made Django that much better:
695695
Gasper Zejn <[email protected]>
696696
Jarek Zgoda <[email protected]>
697697
Cheng Zhang
698+
Peter Zsoldos <http://zsoldosp.eu>
698699
<Please alphabetize new entries>
699700

700701
A big THANK YOU goes to:

django/db/backends/mysql/client.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
class DatabaseClient(BaseDatabaseClient):
77
executable_name = 'mysql'
88

9-
def runshell(self):
10-
settings_dict = self.connection.settings_dict
11-
args = [self.executable_name]
9+
@classmethod
10+
def settings_to_cmd_args(cls, settings_dict):
11+
args = [cls.executable_name]
1212
db = settings_dict['OPTIONS'].get('db', settings_dict['NAME'])
1313
user = settings_dict['OPTIONS'].get('user', settings_dict['USER'])
1414
passwd = settings_dict['OPTIONS'].get('passwd', settings_dict['PASSWORD'])
1515
host = settings_dict['OPTIONS'].get('host', settings_dict['HOST'])
1616
port = settings_dict['OPTIONS'].get('port', settings_dict['PORT'])
17+
cert = settings_dict['OPTIONS'].get('ssl', {}).get('ca')
1718
defaults_file = settings_dict['OPTIONS'].get('read_default_file')
1819
# Seems to be no good way to set sql_mode with CLI.
1920

@@ -30,7 +31,12 @@ def runshell(self):
3031
args += ["--host=%s" % host]
3132
if port:
3233
args += ["--port=%s" % port]
34+
if cert:
35+
args += ["--ssl-ca=%s" % cert]
3336
if db:
3437
args += [db]
38+
return args
3539

40+
def runshell(self):
41+
args = DatabaseClient.settings_to_cmd_args(self.connection.settings_dict)
3642
subprocess.call(args)

docs/releases/1.8.txt

+3
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ Management Commands
203203
command is now always the ``dest`` name specified in the command option
204204
definition (as long as the command uses the new :py:mod:`argparse` module).
205205

206+
* The :djadmin:`dbshell` command now supports MySQL's optional SSL certificate
207+
authority setting (``--ssl-ca``).
208+
206209
Models
207210
^^^^^^
208211

tests/dbshell/__init__.py

Whitespace-only changes.

tests/dbshell/tests.py

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from django.db.backends.mysql.client import DatabaseClient
2+
from django.test import SimpleTestCase
3+
4+
5+
class MySqlDbshellCommandTestCase(SimpleTestCase):
6+
7+
def test_fails_with_keyerror_on_incomplete_config(self):
8+
with self.assertRaises(KeyError):
9+
self.get_command_line_arguments({})
10+
11+
def test_basic_params_specified_in_settings(self):
12+
self.assertEqual(
13+
['mysql', '--user=someuser', '--password=somepassword',
14+
'--host=somehost', '--port=444', 'somedbname'],
15+
self.get_command_line_arguments({
16+
'NAME': 'somedbname',
17+
'USER': 'someuser',
18+
'PASSWORD': 'somepassword',
19+
'HOST': 'somehost',
20+
'PORT': 444,
21+
'OPTIONS': {},
22+
}))
23+
24+
def test_options_override_settings_proper_values(self):
25+
settings_port = 444
26+
options_port = 555
27+
self.assertNotEqual(settings_port, options_port, 'test pre-req')
28+
self.assertEqual(
29+
['mysql', '--user=optionuser', '--password=optionpassword',
30+
'--host=optionhost', '--port={}'.format(options_port), 'optiondbname'],
31+
self.get_command_line_arguments({
32+
'NAME': 'settingdbname',
33+
'USER': 'settinguser',
34+
'PASSWORD': 'settingpassword',
35+
'HOST': 'settinghost',
36+
'PORT': settings_port,
37+
'OPTIONS': {
38+
'db': 'optiondbname',
39+
'user': 'optionuser',
40+
'passwd': 'optionpassword',
41+
'host': 'optionhost',
42+
'port': options_port,
43+
},
44+
}))
45+
46+
def test_can_connect_using_sockets(self):
47+
self.assertEqual(
48+
['mysql', '--user=someuser', '--password=somepassword',
49+
'--socket=/path/to/mysql.socket.file', 'somedbname'],
50+
self.get_command_line_arguments({
51+
'NAME': 'somedbname',
52+
'USER': 'someuser',
53+
'PASSWORD': 'somepassword',
54+
'HOST': '/path/to/mysql.socket.file',
55+
'PORT': None,
56+
'OPTIONS': {},
57+
}))
58+
59+
def test_ssl_certificate_is_added(self):
60+
self.assertEqual(
61+
['mysql', '--user=someuser', '--password=somepassword',
62+
'--host=somehost', '--port=444', '--ssl-ca=sslca', 'somedbname'],
63+
self.get_command_line_arguments({
64+
'NAME': 'somedbname',
65+
'USER': 'someuser',
66+
'PASSWORD': 'somepassword',
67+
'HOST': 'somehost',
68+
'PORT': 444,
69+
'OPTIONS': {'ssl': {'ca': 'sslca'}},
70+
}))
71+
72+
def get_command_line_arguments(self, connection_settings):
73+
return DatabaseClient.settings_to_cmd_args(connection_settings)

0 commit comments

Comments
 (0)