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:
+53
-2
@@ -17,6 +17,8 @@ import uuid
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
|
||||
from core.database_identity import DatabaseIdentityProbe
|
||||
|
||||
|
||||
class BackupError(RuntimeError):
|
||||
"""Invalid or unsuccessful backup operation."""
|
||||
@@ -154,11 +156,26 @@ def flush_file(handle):
|
||||
|
||||
class BackupEngine:
|
||||
|
||||
def __init__(self, adapter, directory, runner):
|
||||
def __init__(self, adapter, directory, runner, *, live_identity=None):
|
||||
|
||||
self.adapter = adapter
|
||||
self.root = Path(directory).absolute()
|
||||
self.runner = runner
|
||||
self.live_identity = live_identity
|
||||
inventory = getattr(adapter, "backup_inventory_command", None)
|
||||
self.inventory_command = inventory() if inventory is not None else None
|
||||
require(
|
||||
live_identity is None or (
|
||||
isinstance(live_identity, DatabaseIdentityProbe)
|
||||
and live_identity.adapter is adapter
|
||||
and live_identity.runner is runner
|
||||
),
|
||||
"Live identity probe must use the backup adapter and runner",
|
||||
)
|
||||
require(
|
||||
self.inventory_command is None or live_identity is not None,
|
||||
"Read-only MySQL backup requires a pinned live identity",
|
||||
)
|
||||
|
||||
require(
|
||||
callable(runner),
|
||||
@@ -177,8 +194,34 @@ class BackupEngine:
|
||||
else "database.dump"
|
||||
)
|
||||
|
||||
def check_inventory(self):
|
||||
if self.inventory_command is None:
|
||||
return
|
||||
output = io.BytesIO()
|
||||
self.runner(self.adapter.backup_grants_command(), output)
|
||||
database, account, _server = self.live_identity.expected
|
||||
user, host = account.split("@", 1)
|
||||
principal = f"`{user}`@`{host}`"
|
||||
expected = {
|
||||
f"GRANT USAGE ON *.* TO {principal}",
|
||||
f"GRANT SELECT, SHOW VIEW ON `{database}`.* TO {principal}",
|
||||
"GRANT EXECUTE ON PROCEDURE `absdev_backup_control`.`check_inventory`"
|
||||
f" TO {principal}",
|
||||
}
|
||||
lines = output.getvalue().decode("utf-8").splitlines()
|
||||
require(lines[:1] == ["1"] and len(lines[1:]) == len(expected) and set(lines[1:]) == expected,
|
||||
"Read-only MySQL backup account has unexpected grants")
|
||||
output = io.BytesIO()
|
||||
self.runner(self.inventory_command, output)
|
||||
require(
|
||||
output.getvalue() in (b"0\t0\t0\t0", b"0\t0\t0\t0\n"),
|
||||
"Read-only MySQL backup cannot certify full object coverage",
|
||||
)
|
||||
|
||||
def identity(self):
|
||||
|
||||
if self.live_identity is not None:
|
||||
return self.live_identity.observe()
|
||||
output = io.BytesIO()
|
||||
|
||||
self.runner(
|
||||
@@ -210,7 +253,7 @@ class BackupEngine:
|
||||
parts = raw.split(separator)
|
||||
|
||||
require(
|
||||
len(parts) == (3 if driver == "mysql" else 2),
|
||||
len(parts) in ((3,) if driver == "mysql" else (2, 3)),
|
||||
"Unexpected database identity format",
|
||||
)
|
||||
|
||||
@@ -240,6 +283,12 @@ class BackupEngine:
|
||||
"Missing database server identity",
|
||||
)
|
||||
|
||||
elif len(parts) == 3:
|
||||
require(
|
||||
re.fullmatch(r"[1-9][0-9]{0,19}", parts[2]) is not None,
|
||||
"Invalid database cluster identifier",
|
||||
)
|
||||
|
||||
return raw
|
||||
|
||||
def create(self):
|
||||
@@ -252,6 +301,7 @@ class BackupEngine:
|
||||
secure_directory(self.root)
|
||||
|
||||
identity_before = self.identity()
|
||||
self.check_inventory()
|
||||
|
||||
stamp = datetime.now(timezone.utc).strftime(
|
||||
"%Y%m%dT%H%M%S"
|
||||
@@ -287,6 +337,7 @@ class BackupEngine:
|
||||
|
||||
require(size > 0, "Database backup is empty")
|
||||
|
||||
self.check_inventory()
|
||||
identity_after = self.identity()
|
||||
|
||||
require(
|
||||
|
||||
Reference in New Issue
Block a user