| 1 |
import os |
| 2 |
import signal |
| 3 |
import subprocess |
| 4 |
import sys |
| 5 |
from pathlib import Path |
| 6 |
from unittest import mock, skipUnless |
| 7 |
|
| 8 |
from django.db import connection |
| 9 |
from django.db.backends.mysql.client import DatabaseClient |
| 10 |
from django.test import SimpleTestCase |
| 11 |
|
| 12 |
|
| 13 |
class MySqlDbshellCommandTestCase(SimpleTestCase): |
| 14 |
def settings_to_cmd_args_env(self, settings_dict, parameters=None): |
| 15 |
if parameters is None: |
| 16 |
parameters = [] |
| 17 |
return DatabaseClient.settings_to_cmd_args_env(settings_dict, parameters) |
| 18 |
|
| 19 |
def test_fails_with_keyerror_on_incomplete_config(self): |
| 20 |
with self.assertRaises(KeyError): |
| 21 |
self.settings_to_cmd_args_env({}) |
| 22 |
|
| 23 |
def test_basic_params_specified_in_settings(self): |
| 24 |
expected_args = [ |
| 25 |
"mysql", |
| 26 |
"--user=someuser", |
| 27 |
"--host=somehost", |
| 28 |
"--port=444", |
| 29 |
"somedbname", |
| 30 |
] |
| 31 |
expected_env = {"MYSQL_PWD": "somepassword"} |
| 32 |
self.assertEqual( |
| 33 |
self.settings_to_cmd_args_env( |
| 34 |
{ |
| 35 |
"NAME": "somedbname", |
| 36 |
"USER": "someuser", |
| 37 |
"PASSWORD": "somepassword", |
| 38 |
"HOST": "somehost", |
| 39 |
"PORT": 444, |
| 40 |
"OPTIONS": {}, |
| 41 |
} |
| 42 |
), |
| 43 |
(expected_args, expected_env), |
| 44 |
) |
| 45 |
|
| 46 |
def test_options_override_settings_proper_values(self): |
| 47 |
settings_port = 444 |
| 48 |
options_port = 555 |
| 49 |
self.assertNotEqual(settings_port, options_port, "test pre-req") |
| 50 |
expected_args = [ |
| 51 |
"mysql", |
| 52 |
"--user=optionuser", |
| 53 |
"--host=optionhost", |
| 54 |
"--port=%s" % options_port, |
| 55 |
"optiondbname", |
| 56 |
] |
| 57 |
expected_env = {"MYSQL_PWD": "optionpassword"} |
| 58 |
for keys in [("database", "password"), ("db", "passwd")]: |
| 59 |
with self.subTest(keys=keys): |
| 60 |
database, password = keys |
| 61 |
self.assertEqual( |
| 62 |
self.settings_to_cmd_args_env( |
| 63 |
{ |
| 64 |
"NAME": "settingdbname", |
| 65 |
"USER": "settinguser", |
| 66 |
"PASSWORD": "settingpassword", |
| 67 |
"HOST": "settinghost", |
| 68 |
"PORT": settings_port, |
| 69 |
"OPTIONS": { |
| 70 |
database: "optiondbname", |
| 71 |
"user": "optionuser", |
| 72 |
password: "optionpassword", |
| 73 |
"host": "optionhost", |
| 74 |
"port": options_port, |
| 75 |
}, |
| 76 |
} |
| 77 |
), |
| 78 |
(expected_args, expected_env), |
| 79 |
) |
| 80 |
|
| 81 |
def test_options_non_deprecated_keys_preferred(self): |
| 82 |
expected_args = [ |
| 83 |
"mysql", |
| 84 |
"--user=someuser", |
| 85 |
"--host=somehost", |
| 86 |
"--port=444", |
| 87 |
"optiondbname", |
| 88 |
] |
| 89 |
expected_env = {"MYSQL_PWD": "optionpassword"} |
| 90 |
self.assertEqual( |
| 91 |
self.settings_to_cmd_args_env( |
| 92 |
{ |
| 93 |
"NAME": "settingdbname", |
| 94 |
"USER": "someuser", |
| 95 |
"PASSWORD": "settingpassword", |
| 96 |
"HOST": "somehost", |
| 97 |
"PORT": 444, |
| 98 |
"OPTIONS": { |
| 99 |
"database": "optiondbname", |
| 100 |
"db": "deprecatedoptiondbname", |
| 101 |
"password": "optionpassword", |
| 102 |
"passwd": "deprecatedoptionpassword", |
| 103 |
}, |
| 104 |
} |
| 105 |
), |
| 106 |
(expected_args, expected_env), |
| 107 |
) |
| 108 |
|
| 109 |
def test_options_charset(self): |
| 110 |
expected_args = [ |
| 111 |
"mysql", |
| 112 |
"--user=someuser", |
| 113 |
"--host=somehost", |
| 114 |
"--port=444", |
| 115 |
"--default-character-set=utf8mb4", |
| 116 |
"somedbname", |
| 117 |
] |
| 118 |
expected_env = {"MYSQL_PWD": "somepassword"} |
| 119 |
self.assertEqual( |
| 120 |
self.settings_to_cmd_args_env( |
| 121 |
{ |
| 122 |
"NAME": "somedbname", |
| 123 |
"USER": "someuser", |
| 124 |
"PASSWORD": "somepassword", |
| 125 |
"HOST": "somehost", |
| 126 |
"PORT": 444, |
| 127 |
"OPTIONS": {"charset": "utf8mb4"}, |
| 128 |
} |
| 129 |
), |
| 130 |
(expected_args, expected_env), |
| 131 |
) |
| 132 |
|
| 133 |
def test_can_connect_using_sockets(self): |
| 134 |
expected_args = [ |
| 135 |
"mysql", |
| 136 |
"--user=someuser", |
| 137 |
"--socket=/path/to/mysql.socket.file", |
| 138 |
"somedbname", |
| 139 |
] |
| 140 |
expected_env = {"MYSQL_PWD": "somepassword"} |
| 141 |
self.assertEqual( |
| 142 |
self.settings_to_cmd_args_env( |
| 143 |
{ |
| 144 |
"NAME": "somedbname", |
| 145 |
"USER": "someuser", |
| 146 |
"PASSWORD": "somepassword", |
| 147 |
"HOST": "/path/to/mysql.socket.file", |
| 148 |
"PORT": None, |
| 149 |
"OPTIONS": {}, |
| 150 |
} |
| 151 |
), |
| 152 |
(expected_args, expected_env), |
| 153 |
) |
| 154 |
|
| 155 |
def test_ssl_certificate_is_added(self): |
| 156 |
expected_args = [ |
| 157 |
"mysql", |
| 158 |
"--user=someuser", |
| 159 |
"--host=somehost", |
| 160 |
"--port=444", |
| 161 |
"--ssl-ca=sslca", |
| 162 |
"--ssl-cert=sslcert", |
| 163 |
"--ssl-key=sslkey", |
| 164 |
"somedbname", |
| 165 |
] |
| 166 |
expected_env = {"MYSQL_PWD": "somepassword"} |
| 167 |
self.assertEqual( |
| 168 |
self.settings_to_cmd_args_env( |
| 169 |
{ |
| 170 |
"NAME": "somedbname", |
| 171 |
"USER": "someuser", |
| 172 |
"PASSWORD": "somepassword", |
| 173 |
"HOST": "somehost", |
| 174 |
"PORT": 444, |
| 175 |
"OPTIONS": { |
| 176 |
"ssl": { |
| 177 |
"ca": "sslca", |
| 178 |
"cert": "sslcert", |
| 179 |
"key": "sslkey", |
| 180 |
}, |
| 181 |
}, |
| 182 |
} |
| 183 |
), |
| 184 |
(expected_args, expected_env), |
| 185 |
) |
| 186 |
|
| 187 |
def test_parameters(self): |
| 188 |
self.assertEqual( |
| 189 |
self.settings_to_cmd_args_env( |
| 190 |
{ |
| 191 |
"NAME": "somedbname", |
| 192 |
"USER": None, |
| 193 |
"PASSWORD": None, |
| 194 |
"HOST": None, |
| 195 |
"PORT": None, |
| 196 |
"OPTIONS": {}, |
| 197 |
}, |
| 198 |
["--help"], |
| 199 |
), |
| 200 |
(["mysql", "somedbname", "--help"], None), |
| 201 |
) |
| 202 |
|
| 203 |
def test_crash_password_does_not_leak(self): |
| 204 |
# The password doesn't leak in an exception that results from a client |
| 205 |
# crash. |
| 206 |
args, env = DatabaseClient.settings_to_cmd_args_env( |
| 207 |
{ |
| 208 |
"NAME": "somedbname", |
| 209 |
"USER": "someuser", |
| 210 |
"PASSWORD": "somepassword", |
| 211 |
"HOST": "somehost", |
| 212 |
"PORT": 444, |
| 213 |
"OPTIONS": {}, |
| 214 |
}, |
| 215 |
[], |
| 216 |
) |
| 217 |
if env: |
| 218 |
env = {**os.environ, **env} |
| 219 |
fake_client = Path(__file__).with_name("fake_client.py") |
| 220 |
args[0:1] = [sys.executable, str(fake_client)] |
| 221 |
with self.assertRaises(subprocess.CalledProcessError) as ctx: |
| 222 |
subprocess.run(args, check=True, env=env) |
| 223 |
self.assertNotIn("somepassword", str(ctx.exception)) |
| 224 |
|
| 225 |
@skipUnless(connection.vendor == "mysql", "Requires a MySQL connection") |
| 226 |
def test_sigint_handler(self): |
| 227 |
"""SIGINT is ignored in Python and passed to mysql to abort queries.""" |
| 228 |
|
| 229 |
def _mock_subprocess_run(*args, **kwargs): |
| 230 |
handler = signal.getsignal(signal.SIGINT) |
| 231 |
self.assertEqual(handler, signal.SIG_IGN) |
| 232 |
|
| 233 |
sigint_handler = signal.getsignal(signal.SIGINT) |
| 234 |
# The default handler isn't SIG_IGN. |
| 235 |
self.assertNotEqual(sigint_handler, signal.SIG_IGN) |
| 236 |
with mock.patch("subprocess.run", new=_mock_subprocess_run): |
| 237 |
connection.client.runshell([]) |
| 238 |
# dbshell restores the original handler. |
| 239 |
self.assertEqual(sigint_handler, signal.getsignal(signal.SIGINT)) |