2020-11-03 10:38:40 +00:00
|
|
|
import os
|
2022-05-20 05:11:51 +00:00
|
|
|
import signal
|
2020-10-04 22:31:04 +00:00
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
from pathlib import Path
|
2022-05-20 05:11:51 +00:00
|
|
|
from unittest import mock, skipUnless
|
2020-10-04 22:31:04 +00:00
|
|
|
|
2022-05-20 05:11:51 +00:00
|
|
|
from django.db import connection
|
2014-05-21 13:05:41 +00:00
|
|
|
from django.db.backends.mysql.client import DatabaseClient
|
|
|
|
from django.test import SimpleTestCase
|
|
|
|
|
|
|
|
|
|
|
|
class MySqlDbshellCommandTestCase(SimpleTestCase):
|
2020-10-04 22:25:29 +00:00
|
|
|
def settings_to_cmd_args_env(self, settings_dict, parameters=None):
|
|
|
|
if parameters is None:
|
|
|
|
parameters = []
|
|
|
|
return DatabaseClient.settings_to_cmd_args_env(settings_dict, parameters)
|
2014-05-21 13:05:41 +00:00
|
|
|
|
|
|
|
def test_fails_with_keyerror_on_incomplete_config(self):
|
|
|
|
with self.assertRaises(KeyError):
|
2020-10-04 22:25:29 +00:00
|
|
|
self.settings_to_cmd_args_env({})
|
2014-05-21 13:05:41 +00:00
|
|
|
|
|
|
|
def test_basic_params_specified_in_settings(self):
|
2020-10-04 22:25:29 +00:00
|
|
|
expected_args = [
|
2022-02-03 19:24:19 +00:00
|
|
|
"mysql",
|
|
|
|
"--user=someuser",
|
|
|
|
"--host=somehost",
|
|
|
|
"--port=444",
|
|
|
|
"somedbname",
|
2020-10-04 22:25:29 +00:00
|
|
|
]
|
2022-02-03 19:24:19 +00:00
|
|
|
expected_env = {"MYSQL_PWD": "somepassword"}
|
2014-05-21 13:05:41 +00:00
|
|
|
self.assertEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
self.settings_to_cmd_args_env(
|
|
|
|
{
|
|
|
|
"NAME": "somedbname",
|
|
|
|
"USER": "someuser",
|
|
|
|
"PASSWORD": "somepassword",
|
|
|
|
"HOST": "somehost",
|
|
|
|
"PORT": 444,
|
|
|
|
"OPTIONS": {},
|
|
|
|
}
|
|
|
|
),
|
2020-10-04 22:25:29 +00:00
|
|
|
(expected_args, expected_env),
|
|
|
|
)
|
2014-05-21 13:05:41 +00:00
|
|
|
|
|
|
|
def test_options_override_settings_proper_values(self):
|
|
|
|
settings_port = 444
|
|
|
|
options_port = 555
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertNotEqual(settings_port, options_port, "test pre-req")
|
2020-10-04 22:25:29 +00:00
|
|
|
expected_args = [
|
2022-02-03 19:24:19 +00:00
|
|
|
"mysql",
|
|
|
|
"--user=optionuser",
|
|
|
|
"--host=optionhost",
|
|
|
|
"--port=%s" % options_port,
|
|
|
|
"optiondbname",
|
2020-10-04 22:25:29 +00:00
|
|
|
]
|
2022-02-03 19:24:19 +00:00
|
|
|
expected_env = {"MYSQL_PWD": "optionpassword"}
|
|
|
|
for keys in [("database", "password"), ("db", "passwd")]:
|
2021-05-10 20:32:10 +00:00
|
|
|
with self.subTest(keys=keys):
|
|
|
|
database, password = keys
|
|
|
|
self.assertEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
self.settings_to_cmd_args_env(
|
|
|
|
{
|
|
|
|
"NAME": "settingdbname",
|
|
|
|
"USER": "settinguser",
|
|
|
|
"PASSWORD": "settingpassword",
|
|
|
|
"HOST": "settinghost",
|
|
|
|
"PORT": settings_port,
|
|
|
|
"OPTIONS": {
|
|
|
|
database: "optiondbname",
|
|
|
|
"user": "optionuser",
|
|
|
|
password: "optionpassword",
|
|
|
|
"host": "optionhost",
|
|
|
|
"port": options_port,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
),
|
2021-05-10 20:32:10 +00:00
|
|
|
(expected_args, expected_env),
|
|
|
|
)
|
2014-05-21 13:05:41 +00:00
|
|
|
|
2021-05-10 20:32:10 +00:00
|
|
|
def test_options_non_deprecated_keys_preferred(self):
|
2020-10-04 22:25:29 +00:00
|
|
|
expected_args = [
|
2022-02-03 19:24:19 +00:00
|
|
|
"mysql",
|
|
|
|
"--user=someuser",
|
|
|
|
"--host=somehost",
|
|
|
|
"--port=444",
|
|
|
|
"optiondbname",
|
2020-10-04 22:25:29 +00:00
|
|
|
]
|
2022-02-03 19:24:19 +00:00
|
|
|
expected_env = {"MYSQL_PWD": "optionpassword"}
|
2020-06-11 18:12:35 +00:00
|
|
|
self.assertEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
self.settings_to_cmd_args_env(
|
|
|
|
{
|
|
|
|
"NAME": "settingdbname",
|
|
|
|
"USER": "someuser",
|
|
|
|
"PASSWORD": "settingpassword",
|
|
|
|
"HOST": "somehost",
|
|
|
|
"PORT": 444,
|
|
|
|
"OPTIONS": {
|
|
|
|
"database": "optiondbname",
|
|
|
|
"db": "deprecatedoptiondbname",
|
|
|
|
"password": "optionpassword",
|
|
|
|
"passwd": "deprecatedoptionpassword",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
),
|
2020-10-04 22:25:29 +00:00
|
|
|
(expected_args, expected_env),
|
2020-06-11 18:12:35 +00:00
|
|
|
)
|
|
|
|
|
2020-10-14 18:07:57 +00:00
|
|
|
def test_options_charset(self):
|
2020-10-04 22:25:29 +00:00
|
|
|
expected_args = [
|
2022-02-03 19:24:19 +00:00
|
|
|
"mysql",
|
|
|
|
"--user=someuser",
|
|
|
|
"--host=somehost",
|
|
|
|
"--port=444",
|
|
|
|
"--default-character-set=utf8",
|
|
|
|
"somedbname",
|
2020-10-04 22:25:29 +00:00
|
|
|
]
|
2022-02-03 19:24:19 +00:00
|
|
|
expected_env = {"MYSQL_PWD": "somepassword"}
|
2020-10-14 18:07:57 +00:00
|
|
|
self.assertEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
self.settings_to_cmd_args_env(
|
|
|
|
{
|
|
|
|
"NAME": "somedbname",
|
|
|
|
"USER": "someuser",
|
|
|
|
"PASSWORD": "somepassword",
|
|
|
|
"HOST": "somehost",
|
|
|
|
"PORT": 444,
|
|
|
|
"OPTIONS": {"charset": "utf8"},
|
|
|
|
}
|
|
|
|
),
|
2020-10-04 22:25:29 +00:00
|
|
|
(expected_args, expected_env),
|
2020-10-14 18:07:57 +00:00
|
|
|
)
|
|
|
|
|
2014-05-21 13:05:41 +00:00
|
|
|
def test_can_connect_using_sockets(self):
|
2020-10-04 22:25:29 +00:00
|
|
|
expected_args = [
|
2022-02-03 19:24:19 +00:00
|
|
|
"mysql",
|
|
|
|
"--user=someuser",
|
|
|
|
"--socket=/path/to/mysql.socket.file",
|
|
|
|
"somedbname",
|
2020-10-04 22:25:29 +00:00
|
|
|
]
|
2022-02-03 19:24:19 +00:00
|
|
|
expected_env = {"MYSQL_PWD": "somepassword"}
|
2014-05-21 13:05:41 +00:00
|
|
|
self.assertEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
self.settings_to_cmd_args_env(
|
|
|
|
{
|
|
|
|
"NAME": "somedbname",
|
|
|
|
"USER": "someuser",
|
|
|
|
"PASSWORD": "somepassword",
|
|
|
|
"HOST": "/path/to/mysql.socket.file",
|
|
|
|
"PORT": None,
|
|
|
|
"OPTIONS": {},
|
|
|
|
}
|
|
|
|
),
|
2020-10-04 22:25:29 +00:00
|
|
|
(expected_args, expected_env),
|
|
|
|
)
|
2014-05-21 13:05:41 +00:00
|
|
|
|
|
|
|
def test_ssl_certificate_is_added(self):
|
2020-10-04 22:25:29 +00:00
|
|
|
expected_args = [
|
2022-02-03 19:24:19 +00:00
|
|
|
"mysql",
|
|
|
|
"--user=someuser",
|
|
|
|
"--host=somehost",
|
|
|
|
"--port=444",
|
|
|
|
"--ssl-ca=sslca",
|
|
|
|
"--ssl-cert=sslcert",
|
|
|
|
"--ssl-key=sslkey",
|
|
|
|
"somedbname",
|
2020-10-04 22:25:29 +00:00
|
|
|
]
|
2022-02-03 19:24:19 +00:00
|
|
|
expected_env = {"MYSQL_PWD": "somepassword"}
|
2014-05-21 13:05:41 +00:00
|
|
|
self.assertEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
self.settings_to_cmd_args_env(
|
|
|
|
{
|
|
|
|
"NAME": "somedbname",
|
|
|
|
"USER": "someuser",
|
|
|
|
"PASSWORD": "somepassword",
|
|
|
|
"HOST": "somehost",
|
|
|
|
"PORT": 444,
|
|
|
|
"OPTIONS": {
|
|
|
|
"ssl": {
|
|
|
|
"ca": "sslca",
|
|
|
|
"cert": "sslcert",
|
|
|
|
"key": "sslkey",
|
|
|
|
},
|
2017-06-19 22:11:25 +00:00
|
|
|
},
|
2022-02-03 19:24:19 +00:00
|
|
|
}
|
|
|
|
),
|
2020-10-04 22:25:29 +00:00
|
|
|
(expected_args, expected_env),
|
|
|
|
)
|
2014-05-21 13:05:41 +00:00
|
|
|
|
2020-04-14 07:56:40 +00:00
|
|
|
def test_parameters(self):
|
|
|
|
self.assertEqual(
|
2020-10-04 22:25:29 +00:00
|
|
|
self.settings_to_cmd_args_env(
|
2020-04-14 07:56:40 +00:00
|
|
|
{
|
2022-02-03 19:24:19 +00:00
|
|
|
"NAME": "somedbname",
|
|
|
|
"USER": None,
|
|
|
|
"PASSWORD": None,
|
|
|
|
"HOST": None,
|
|
|
|
"PORT": None,
|
|
|
|
"OPTIONS": {},
|
2020-04-14 07:56:40 +00:00
|
|
|
},
|
2022-02-03 19:24:19 +00:00
|
|
|
["--help"],
|
2020-04-14 07:56:40 +00:00
|
|
|
),
|
2022-02-03 19:24:19 +00:00
|
|
|
(["mysql", "somedbname", "--help"], None),
|
2020-04-14 07:56:40 +00:00
|
|
|
)
|
2020-10-04 22:31:04 +00:00
|
|
|
|
|
|
|
def test_crash_password_does_not_leak(self):
|
|
|
|
# The password doesn't leak in an exception that results from a client
|
|
|
|
# crash.
|
|
|
|
args, env = DatabaseClient.settings_to_cmd_args_env(
|
|
|
|
{
|
2022-02-03 19:24:19 +00:00
|
|
|
"NAME": "somedbname",
|
|
|
|
"USER": "someuser",
|
|
|
|
"PASSWORD": "somepassword",
|
|
|
|
"HOST": "somehost",
|
|
|
|
"PORT": 444,
|
|
|
|
"OPTIONS": {},
|
2020-10-04 22:31:04 +00:00
|
|
|
},
|
|
|
|
[],
|
|
|
|
)
|
2020-11-03 10:38:40 +00:00
|
|
|
if env:
|
|
|
|
env = {**os.environ, **env}
|
2022-02-03 19:24:19 +00:00
|
|
|
fake_client = Path(__file__).with_name("fake_client.py")
|
2020-10-04 22:31:04 +00:00
|
|
|
args[0:1] = [sys.executable, str(fake_client)]
|
|
|
|
with self.assertRaises(subprocess.CalledProcessError) as ctx:
|
|
|
|
subprocess.run(args, check=True, env=env)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertNotIn("somepassword", str(ctx.exception))
|
2022-05-20 05:11:51 +00:00
|
|
|
|
|
|
|
@skipUnless(connection.vendor == "mysql", "Requires a MySQL connection")
|
|
|
|
def test_sigint_handler(self):
|
|
|
|
"""SIGINT is ignored in Python and passed to mysql to abort queries."""
|
|
|
|
|
|
|
|
def _mock_subprocess_run(*args, **kwargs):
|
|
|
|
handler = signal.getsignal(signal.SIGINT)
|
|
|
|
self.assertEqual(handler, signal.SIG_IGN)
|
|
|
|
|
|
|
|
sigint_handler = signal.getsignal(signal.SIGINT)
|
|
|
|
# The default handler isn't SIG_IGN.
|
|
|
|
self.assertNotEqual(sigint_handler, signal.SIG_IGN)
|
|
|
|
with mock.patch("subprocess.run", new=_mock_subprocess_run):
|
|
|
|
connection.client.runshell([])
|
|
|
|
# dbshell restores the original handler.
|
|
|
|
self.assertEqual(sigint_handler, signal.getsignal(signal.SIGINT))
|