243 lines
5.9 KiB
Python
243 lines
5.9 KiB
Python
"""Conservative read-only deployment recovery planning."""
|
|
|
|
from pathlib import Path
|
|
|
|
from core.backup_evidence import FIELDS
|
|
from core.compatibility import (
|
|
canonical_hash,
|
|
require_compatible,
|
|
validate_provenance,
|
|
)
|
|
from core.locking import DeploymentLock
|
|
|
|
|
|
class RecoveryError(RuntimeError):
|
|
"""Recovery safety could not be established."""
|
|
|
|
|
|
def require(condition, message):
|
|
|
|
if not condition:
|
|
raise RecoveryError(message)
|
|
|
|
|
|
class RecoveryPlanner:
|
|
|
|
def __init__(
|
|
self,
|
|
*,
|
|
site,
|
|
lock_path,
|
|
releases,
|
|
provenance,
|
|
journal,
|
|
backup_evidence,
|
|
backup_verify,
|
|
runtime_check,
|
|
):
|
|
|
|
self.site = site
|
|
self.lock_path = Path(lock_path)
|
|
self.releases = releases
|
|
self.provenance = provenance
|
|
self.journal = journal
|
|
self.backup_evidence = backup_evidence
|
|
self.backup_verify = backup_verify
|
|
self.runtime_check = runtime_check
|
|
|
|
def inspect(self):
|
|
|
|
with DeploymentLock(self.lock_path):
|
|
|
|
try:
|
|
|
|
return self._inspect()
|
|
|
|
except RecoveryError:
|
|
raise
|
|
|
|
except Exception:
|
|
|
|
raise RecoveryError(
|
|
"Recovery verification failed"
|
|
) from None
|
|
|
|
def _inspect(self):
|
|
|
|
transaction = self.journal.load()
|
|
|
|
result = {
|
|
"format_version": 1,
|
|
"site": self.site,
|
|
"automatic_rollback_allowed": False,
|
|
"database_restore_allowed": False,
|
|
"execution_permitted": False,
|
|
}
|
|
|
|
if transaction is None:
|
|
|
|
result["status"] = "CLEAR"
|
|
|
|
return result
|
|
|
|
require(
|
|
transaction["site"] == self.site,
|
|
"Transaction belongs to another site",
|
|
)
|
|
|
|
current = self.releases.current()
|
|
|
|
previous = transaction["previous"]
|
|
candidate = transaction["candidate"]
|
|
|
|
require(
|
|
current in (previous, candidate),
|
|
"Unexpected active release",
|
|
)
|
|
|
|
result.update({
|
|
"transaction_id": transaction["transaction_id"],
|
|
"transaction_checksum": transaction["checksum"],
|
|
"phase": transaction["phase"],
|
|
"current": current,
|
|
"previous": previous,
|
|
"candidate": candidate,
|
|
"target": None,
|
|
"confirmation": None,
|
|
})
|
|
|
|
if transaction["phase"] == "VERIFIED":
|
|
|
|
require(
|
|
current == candidate,
|
|
"Verified transaction has inconsistent active release",
|
|
)
|
|
|
|
result["status"] = "VERIFIED"
|
|
|
|
return result
|
|
|
|
if current == previous:
|
|
|
|
result["status"] = (
|
|
"PREVIOUS_ACTIVE_REVIEW_REQUIRED"
|
|
)
|
|
|
|
return result
|
|
|
|
history = {
|
|
event["phase"]
|
|
for event in transaction["history"]
|
|
}
|
|
|
|
require(
|
|
{
|
|
"BACKED_UP",
|
|
"QUIESCED",
|
|
"ACTIVATING",
|
|
} <= history,
|
|
"Activation evidence is incomplete",
|
|
)
|
|
|
|
require(
|
|
transaction["phase"] in (
|
|
"ACTIVATING",
|
|
"ACTIVATED",
|
|
"FAILED",
|
|
),
|
|
"Transaction phase does not permit rollback review",
|
|
)
|
|
|
|
old = self.provenance.verify(
|
|
self.releases.validate(previous)
|
|
)
|
|
|
|
new = self.provenance.verify(
|
|
self.releases.validate(candidate)
|
|
)
|
|
|
|
validate_provenance(old)
|
|
validate_provenance(new)
|
|
|
|
require(
|
|
new["revision"] == transaction["revision"]
|
|
and new["fingerprint"] == transaction["provenance"],
|
|
"Candidate provenance differs from transaction",
|
|
)
|
|
|
|
require(
|
|
transaction["previous_revision"] is not None
|
|
and transaction["previous_provenance"] is not None
|
|
and old["revision"]
|
|
== transaction["previous_revision"]
|
|
and old["fingerprint"]
|
|
== transaction["previous_provenance"],
|
|
"Previous release provenance differs from transaction",
|
|
)
|
|
|
|
compatibility = require_compatible(
|
|
new,
|
|
old,
|
|
)
|
|
|
|
evidence = self.backup_evidence.load(
|
|
transaction["transaction_id"]
|
|
)
|
|
|
|
recorded = evidence["backup"]
|
|
|
|
manifest = self.backup_verify(
|
|
recorded["backup_id"]
|
|
)
|
|
|
|
require(
|
|
isinstance(manifest, dict)
|
|
and FIELDS <= set(manifest),
|
|
"Invalid verified backup manifest",
|
|
)
|
|
|
|
verified = {
|
|
field: manifest[field]
|
|
for field in FIELDS
|
|
}
|
|
|
|
require(
|
|
verified == recorded,
|
|
"Verified backup differs from transaction evidence",
|
|
)
|
|
|
|
require(
|
|
self.runtime_check() is True,
|
|
"Database runtime verification failed",
|
|
)
|
|
|
|
require(
|
|
self.releases.current() == current,
|
|
"Active release changed during inspection",
|
|
)
|
|
|
|
require(
|
|
self.journal.load() == transaction,
|
|
"Transaction changed during inspection",
|
|
)
|
|
|
|
result.update({
|
|
"status": "ROLLBACK_REVIEW_READY",
|
|
"target": previous,
|
|
"backup_id": recorded["backup_id"],
|
|
"backup_sha256": recorded["sha256"],
|
|
"backup_evidence_checksum": evidence["checksum"],
|
|
"previous_provenance": old["fingerprint"],
|
|
"candidate_provenance": new["fingerprint"],
|
|
"compatibility_fingerprint": (
|
|
compatibility["fingerprint"]
|
|
),
|
|
"confirmation": (
|
|
"ROLLBACK-" + transaction["transaction_id"]
|
|
),
|
|
})
|
|
|
|
result["plan_fingerprint"] = canonical_hash(result)
|
|
|
|
return result
|