Qualify pinned database identity and restricted MySQL backup profile
ABSDEV Framework CI / framework-ci (push) Successful in 24s
ABSDEV Framework CI / framework-ci (push) Successful in 24s
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
import io
|
||||
import json
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from core.backup import BackupEngine, BackupError
|
||||
from core.database import DatabaseError, get_adapter
|
||||
from core.database_identity import DatabaseIdentityProbe
|
||||
|
||||
|
||||
MYSQL_ID = "11111111-2222-3333-4444-555555555555"
|
||||
ACCOUNT = "[email protected]"
|
||||
|
||||
|
||||
def adapter(driver, **kwargs):
|
||||
site = json.loads(Path("config/site.example.json").read_text())
|
||||
site["database"].update(driver=driver, port=3306 if driver == "mysql" else 5432)
|
||||
return get_adapter(site, **kwargs)
|
||||
|
||||
|
||||
class IdentityTests(unittest.TestCase):
|
||||
def test_strict_live_identity(self):
|
||||
for driver, pin, account, separator, probe_account in (
|
||||
("mysql", MYSQL_ID, ACCOUNT, "\t", ACCOUNT),
|
||||
("pgsql", "112233445566778899", "example_app", "|", None),
|
||||
):
|
||||
with self.subTest(driver=driver):
|
||||
db = adapter(driver)
|
||||
expected = separator.join(("example_db", account, pin))
|
||||
result = io.BytesIO()
|
||||
def run(command, output):
|
||||
self.assertEqual(command, db.live_identity_command())
|
||||
output.write(result.getvalue())
|
||||
probe = DatabaseIdentityProbe(db, run, server_id=pin, account=probe_account)
|
||||
result.write((expected + "\n").encode())
|
||||
self.assertTrue(probe())
|
||||
for candidate in (expected.replace("example_db", "other_db"),
|
||||
expected.replace(account, "other_user"), expected[:-1],
|
||||
expected + "\n" + expected):
|
||||
result = io.BytesIO(candidate.encode())
|
||||
with self.assertRaises(DatabaseError):
|
||||
probe()
|
||||
|
||||
def test_read_only_backup_fails_closed(self):
|
||||
db = adapter("mysql", mysql_read_only_backup=True)
|
||||
identity = f"example_db\t{ACCOUNT}\t{MYSQL_ID}\n".encode()
|
||||
principal = "`example_app`@`127.0.0.1`"
|
||||
grants = (
|
||||
"1\n"
|
||||
f"GRANT USAGE ON *.* TO {principal}\n"
|
||||
f"GRANT SELECT, SHOW VIEW ON `example_db`.* TO {principal}\n"
|
||||
f"GRANT EXECUTE ON PROCEDURE `absdev_backup_control`.`check_inventory` TO {principal}\n"
|
||||
).encode()
|
||||
inventory = [b"0\t0\t0\t0\n"]
|
||||
commands = []
|
||||
def run(command, output):
|
||||
commands.append(command)
|
||||
if command == db.live_identity_command():
|
||||
output.write(identity)
|
||||
elif command == db.backup_grants_command():
|
||||
output.write(grants)
|
||||
elif command == db.backup_inventory_command():
|
||||
output.write(inventory.pop(0))
|
||||
else:
|
||||
self.assertEqual(command, db.backup_command())
|
||||
output.write(b"backup data")
|
||||
probe = DatabaseIdentityProbe(db, run, server_id=MYSQL_ID, account=ACCOUNT)
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory).resolve()
|
||||
with self.assertRaises(BackupError):
|
||||
BackupEngine(db, root, run)
|
||||
engine = BackupEngine(db, root, run, live_identity=probe)
|
||||
inventory[:] = [b"1\t0\t0\t0\n"]
|
||||
with self.assertRaises(BackupError):
|
||||
engine.create()
|
||||
self.assertNotIn(db.backup_command(), commands)
|
||||
self.assertEqual(list(root.iterdir()), [])
|
||||
inventory[:] = [b"0\t0\t0\t0\n", b"0\t1\t0\t0\n"]
|
||||
with self.assertRaises(BackupError):
|
||||
engine.create()
|
||||
self.assertEqual(list(root.iterdir()), [])
|
||||
inventory[:] = [b"0\t0\t0\t0\n", b"0\t0\t0\t0\n"]
|
||||
self.assertEqual(engine.verify(engine.create())["identity"], identity.decode().strip())
|
||||
grants += f"GRANT INSERT ON `example_db`.* TO {principal}\n".encode()
|
||||
commands.clear()
|
||||
with self.assertRaises(BackupError):
|
||||
engine.create()
|
||||
self.assertNotIn(db.backup_command(), commands)
|
||||
Reference in New Issue
Block a user