43 lines
1.8 KiB
Python
43 lines
1.8 KiB
Python
"""Credential validation must fail before deployment and must not echo secrets."""
|
|
import importlib.util
|
|
from pathlib import Path
|
|
import tempfile
|
|
import unittest
|
|
|
|
spec = importlib.util.spec_from_file_location("local", Path(__file__).parents[1] / "scripts/local.py")
|
|
local = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(local)
|
|
|
|
|
|
class MySQLConfigChecks(unittest.TestCase):
|
|
def validate(self, content):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
path = Path(directory) / "my.cnf"
|
|
path.write_text(content, encoding="utf-8")
|
|
local.validate_mysql_config(path)
|
|
|
|
def test_missing_file_has_actionable_error(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
with self.assertRaisesRegex(RuntimeError, "mysql-exporter.cnf"):
|
|
local.validate_mysql_config(Path(directory) / "mysql-exporter.cnf")
|
|
|
|
def test_placeholder_is_rejected(self):
|
|
with self.assertRaisesRegex(RuntimeError, "password"):
|
|
self.validate("[client]\nhost=host.docker.internal\nport=3306\nuser=mysqld_exporter\npassword=REPLACE_WITH_MYSQL_MONITOR_PASSWORD\n")
|
|
|
|
def test_valid_config_accepts_percent_without_interpolation(self):
|
|
self.validate("[client]\nhost=host.docker.internal\nport=3306\nuser=mysqld_exporter\npassword=test-only-50%\n")
|
|
|
|
def test_invalid_file_does_not_echo_its_contents(self):
|
|
with self.assertRaises(RuntimeError) as caught:
|
|
self.validate("sensitive-canary-invalid-config")
|
|
self.assertNotIn("sensitive-canary", str(caught.exception))
|
|
|
|
def test_invalid_port_is_rejected(self):
|
|
with self.assertRaisesRegex(RuntimeError, "port"):
|
|
self.validate("[client]\nhost=host.docker.internal\nport=70000\nuser=mysqld_exporter\npassword=test-only\n")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|