133 lines
4.3 KiB
Python
133 lines
4.3 KiB
Python
import json
|
|
import os
|
|
from dataclasses import dataclass
|
|
from uuid import UUID
|
|
from zipfile import BadZipFile
|
|
|
|
from .account_backup import open_account_bundle
|
|
from .account_backup_request import (
|
|
REQUEST_VERSION,
|
|
backup_archive,
|
|
expiration_marker,
|
|
)
|
|
from .node_files import check_root_file, locked_root_directory, write_new_file
|
|
|
|
MAX_RETAINED_BACKUPS = 90
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ArchiveExpirationRequest:
|
|
account_id: int
|
|
backup_id: UUID
|
|
keepers: tuple[UUID, ...]
|
|
|
|
def __post_init__(self):
|
|
if (
|
|
type(self.account_id) is not int
|
|
or not 1 <= self.account_id < 2**63
|
|
):
|
|
raise ValueError("Expected a valid backup account ID.")
|
|
if (
|
|
not 1 <= len(self.keepers) <= MAX_RETAINED_BACKUPS
|
|
or len(set(self.keepers)) != len(self.keepers)
|
|
or self.backup_id in self.keepers
|
|
):
|
|
raise ValueError("Expected distinct backups to retain.")
|
|
|
|
@classmethod
|
|
def from_manifest(cls, data):
|
|
if (
|
|
not isinstance(data, dict)
|
|
or data.keys()
|
|
!= {"version", "operation", "account_id", "backup_id", "keepers"}
|
|
or type(data["version"]) is not int
|
|
or data["version"] != REQUEST_VERSION
|
|
or data["operation"] != "expire"
|
|
or not isinstance(data["keepers"], list)
|
|
or len(data["keepers"]) > MAX_RETAINED_BACKUPS
|
|
):
|
|
raise ValueError("Expected a local archive expiration request.")
|
|
|
|
return cls(
|
|
data["account_id"],
|
|
parse_uuid(data["backup_id"]),
|
|
tuple(parse_uuid(value) for value in data["keepers"]),
|
|
)
|
|
|
|
def manifest(self):
|
|
return {
|
|
"version": REQUEST_VERSION,
|
|
"operation": "expire",
|
|
"account_id": self.account_id,
|
|
"backup_id": str(self.backup_id),
|
|
"keepers": [str(backup) for backup in self.keepers],
|
|
}
|
|
|
|
def identity(self):
|
|
return {
|
|
"version": REQUEST_VERSION,
|
|
"operation": "expire",
|
|
"account_id": self.account_id,
|
|
"backup_id": str(self.backup_id),
|
|
}
|
|
|
|
|
|
def parse_uuid(value):
|
|
if isinstance(value, str):
|
|
try:
|
|
identifier = UUID(value)
|
|
except ValueError:
|
|
pass
|
|
else:
|
|
if str(identifier) == value:
|
|
return identifier
|
|
raise ValueError("Expected a canonical backup ID.")
|
|
|
|
|
|
def check_backup_identity(archive, account_id):
|
|
with open_account_bundle(archive) as (_, backup):
|
|
if backup.website.account_id != account_id:
|
|
raise ValueError("The archive belongs to a different account.")
|
|
|
|
|
|
def expire_requested_archive(request):
|
|
if os.geteuid() != 0:
|
|
raise ValueError("Run archive retention as root on the backup node.")
|
|
archive = backup_archive(request.account_id, request.backup_id)
|
|
marker = expiration_marker(archive)
|
|
with locked_root_directory(archive.parent) as directory:
|
|
marked = os.path.lexists(marker)
|
|
if marked:
|
|
check_root_file(marker, mode=0o600)
|
|
if not os.path.lexists(archive):
|
|
return {"removed": True, "reason": None}
|
|
|
|
if os.environ.get("COULOBRE_LOCAL_BACKUP_RETENTION") != "1":
|
|
return {
|
|
"removed": False,
|
|
"reason": "Enable backup retention on the backup node.",
|
|
}
|
|
|
|
try:
|
|
for backup_id in request.keepers:
|
|
retained = backup_archive(request.account_id, backup_id)
|
|
if os.path.lexists(expiration_marker(retained)):
|
|
raise ValueError("A retained archive is being removed.")
|
|
check_backup_identity(retained, request.account_id)
|
|
if os.path.lexists(archive):
|
|
check_backup_identity(archive, request.account_id)
|
|
except (OSError, BadZipFile, ValueError) as error:
|
|
return {
|
|
"removed": False,
|
|
"reason": f"Could not verify the archives. {error}",
|
|
}
|
|
|
|
# Persist the marker before removing the immutable archive.
|
|
if not marked:
|
|
write_new_file(marker, json.dumps(request.identity()), mode=0o600)
|
|
os.fsync(directory)
|
|
archive.unlink(missing_ok=True)
|
|
os.fsync(directory)
|
|
|
|
return {"removed": True, "reason": None}
|