Guard ordinary deployment journals and bind migration recovery to the original release
ABSDEV Framework CI / framework-ci (push) Successful in 37s
ABSDEV Framework CI / framework-ci (push) Successful in 37s
This commit is contained in:
@@ -405,6 +405,7 @@ class TransactionArchive:
|
||||
|
||||
with DeploymentLock(self.journal.lock_path):
|
||||
|
||||
self.journal.require_writes_allowed()
|
||||
transaction = self.journal.load()
|
||||
identifier = plan["transaction_id"]
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ class SiteOperations:
|
||||
self.journal = TransactionJournal(state)
|
||||
self.backup_evidence = BackupEvidence(state)
|
||||
self.approvals = ApprovalStore(approvals)
|
||||
self.journal.bind_migration_guard(self.approvals)
|
||||
self.guard = OperationGuard(self.approvals)
|
||||
|
||||
def common(self):
|
||||
|
||||
+48
-21
@@ -14,6 +14,7 @@ from core.rehearsal import approval_context
|
||||
from core.compatibility import canonical_hash
|
||||
from core.locking import DeploymentLock
|
||||
from core.operation_guard import OperationGuard
|
||||
from core.transactions import TransactionJournal
|
||||
|
||||
|
||||
class MigrationError(RuntimeError):
|
||||
@@ -50,6 +51,16 @@ def validate_history(history):
|
||||
return list(history)
|
||||
|
||||
|
||||
def validate_original_release(original):
|
||||
require(isinstance(original, dict) and set(original) == {"release", "revision", "fingerprint"},
|
||||
"Missing original release identity")
|
||||
for key, pattern in (("release", r"[1-9][0-9]{0,19}"),
|
||||
("revision", r"[a-f0-9]{40}"), ("fingerprint", r"[a-f0-9]{64}")):
|
||||
require(isinstance(original[key], str) and re.fullmatch(pattern, original[key]) is not None,
|
||||
"Invalid original release identity")
|
||||
return original
|
||||
|
||||
|
||||
class MigrationCoordinator:
|
||||
|
||||
def __init__(
|
||||
@@ -57,6 +68,8 @@ class MigrationCoordinator:
|
||||
*,
|
||||
store,
|
||||
lock_path,
|
||||
journal,
|
||||
original_release_probe,
|
||||
context_probe,
|
||||
contain,
|
||||
containment_probe,
|
||||
@@ -67,6 +80,7 @@ class MigrationCoordinator:
|
||||
clock,
|
||||
):
|
||||
for callback in (
|
||||
original_release_probe,
|
||||
context_probe,
|
||||
contain,
|
||||
containment_probe,
|
||||
@@ -81,6 +95,12 @@ class MigrationCoordinator:
|
||||
"Missing migration operation adapter",
|
||||
)
|
||||
|
||||
require(isinstance(journal, TransactionJournal)
|
||||
and journal.migration_guard_directory() == store.directory
|
||||
and Path(lock_path) == journal.directory / ".deployment.lock",
|
||||
"Migration requires the bound ordinary journal")
|
||||
self.journal = journal
|
||||
self.original_release_probe = original_release_probe
|
||||
self.store = store
|
||||
self.operation_guard = OperationGuard(store)
|
||||
self.lock_path = Path(lock_path)
|
||||
@@ -169,26 +189,33 @@ class MigrationCoordinator:
|
||||
|
||||
operation = "persist migration intent"
|
||||
|
||||
intent = self.publish(
|
||||
intent_path,
|
||||
{
|
||||
"format_version": 1,
|
||||
"status": "MIGRATION_INTENT",
|
||||
"approval_id": identifier,
|
||||
"approval_checksum": approval["checksum"],
|
||||
"context_fingerprint": canonical_hash(context),
|
||||
"site": context["site"],
|
||||
"release": context["release"],
|
||||
"revision": context["revision"],
|
||||
"database_identity": context["database_identity"],
|
||||
"backup_sha256": context["backup_sha256"],
|
||||
"rehearsal_fingerprint":
|
||||
context["rehearsal_fingerprint"],
|
||||
"history_before": before,
|
||||
"pending_migrations": list(pending),
|
||||
"created_at": current_time.isoformat(),
|
||||
},
|
||||
)
|
||||
with DeploymentLock(self.journal.lock_path):
|
||||
self.journal.require_writes_allowed()
|
||||
original = copy.deepcopy(validate_original_release(self.original_release_probe()))
|
||||
require(original["release"] != context["release"], "Candidate is already active")
|
||||
intent = self.publish(
|
||||
intent_path,
|
||||
{
|
||||
"format_version": 1,
|
||||
"status": "MIGRATION_INTENT",
|
||||
"approval_id": identifier,
|
||||
"approval_checksum": approval["checksum"],
|
||||
"context_fingerprint": canonical_hash(context),
|
||||
"site": context["site"],
|
||||
"release": context["release"],
|
||||
"revision": context["revision"],
|
||||
"database_identity": context["database_identity"],
|
||||
"backup_sha256": context["backup_sha256"],
|
||||
"rehearsal_fingerprint":
|
||||
context["rehearsal_fingerprint"],
|
||||
"original_release": original["release"],
|
||||
"original_revision": original["revision"],
|
||||
"original_provenance": original["fingerprint"],
|
||||
"history_before": before,
|
||||
"pending_migrations": list(pending),
|
||||
"created_at": current_time.isoformat(),
|
||||
},
|
||||
)
|
||||
|
||||
operation = "establish containment"
|
||||
containment_started = True
|
||||
@@ -206,7 +233,7 @@ class MigrationCoordinator:
|
||||
operation = "revalidate migration context"
|
||||
|
||||
require(
|
||||
self.context_probe() == base,
|
||||
self.context_probe() == base and self.original_release_probe() == original,
|
||||
"Migration context changed",
|
||||
)
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ from pathlib import Path
|
||||
from core.approval import validate_context
|
||||
from core.compatibility import canonical_hash
|
||||
from core.locking import DeploymentLock
|
||||
from core.migration import validate_history
|
||||
from core.migration import validate_history, validate_original_release
|
||||
|
||||
|
||||
class MigrationRecoveryError(RuntimeError):
|
||||
@@ -263,7 +263,20 @@ class MigrationRecovery:
|
||||
"Backup integrity could not be verified",
|
||||
)
|
||||
|
||||
original = validate_original_release({
|
||||
"release": intent.get("original_release"),
|
||||
"revision": intent.get("original_revision"),
|
||||
"fingerprint": intent.get("original_provenance"),
|
||||
})
|
||||
current = self.current_probe()
|
||||
original_evidence = self.provenance_probe(original["release"])
|
||||
require(
|
||||
current == original["release"] and current != release
|
||||
and isinstance(original_evidence, dict)
|
||||
and original_evidence.get("revision") == original["revision"]
|
||||
and original_evidence.get("fingerprint") == original["fingerprint"],
|
||||
"Original active release identity changed",
|
||||
)
|
||||
|
||||
require(
|
||||
isinstance(current, str)
|
||||
@@ -290,6 +303,8 @@ class MigrationRecovery:
|
||||
"site": self.site,
|
||||
"approval_id": identifier,
|
||||
"current": current,
|
||||
"original_revision": original["revision"],
|
||||
"original_provenance": original["fingerprint"],
|
||||
"target": release,
|
||||
"revision": context["revision"],
|
||||
"provenance": context["provenance"],
|
||||
|
||||
+44
-1
@@ -10,7 +10,10 @@ import uuid
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
|
||||
from core.approval_store import ApprovalStore
|
||||
from core.backup import sync_directory
|
||||
from core.locking import DeploymentLock
|
||||
from core.operation_guard import OperationGuard
|
||||
|
||||
|
||||
class TransactionError(RuntimeError):
|
||||
@@ -113,6 +116,46 @@ class TransactionJournal:
|
||||
|
||||
self.validate_directory()
|
||||
|
||||
def bind_migration_guard(self, store):
|
||||
require(
|
||||
self.directory.name == "state" and store.directory == self.directory.parent / "approvals",
|
||||
"Invalid journal and migration store binding",
|
||||
)
|
||||
store.validate_directory()
|
||||
marker = self.directory / ".migration-guard"
|
||||
with DeploymentLock(self.lock_path):
|
||||
fd = os.open(marker, os.O_WRONLY | os.O_CREAT | os.O_NOFOLLOW | os.O_NONBLOCK, 0o600)
|
||||
try:
|
||||
info = os.fstat(fd)
|
||||
require(stat.S_ISREG(info.st_mode) and info.st_nlink == 1
|
||||
and info.st_uid == os.getuid() and stat.S_IMODE(info.st_mode) == 0o600,
|
||||
"Invalid migration guard marker")
|
||||
os.fsync(fd)
|
||||
finally:
|
||||
os.close(fd)
|
||||
sync_directory(self.directory)
|
||||
|
||||
def migration_guard_directory(self):
|
||||
marker = self.directory / ".migration-guard"
|
||||
try:
|
||||
info = marker.lstat()
|
||||
except FileNotFoundError:
|
||||
return None
|
||||
require(stat.S_ISREG(info.st_mode) and info.st_nlink == 1
|
||||
and info.st_uid == os.getuid() and stat.S_IMODE(info.st_mode) == 0o600,
|
||||
"Invalid migration guard marker")
|
||||
return self.directory.parent / "approvals"
|
||||
|
||||
def require_writes_allowed(self):
|
||||
"""Call under the ordinary journal lock."""
|
||||
self.validate_directory()
|
||||
approvals = self.migration_guard_directory()
|
||||
if approvals is not None:
|
||||
try:
|
||||
OperationGuard(ApprovalStore(approvals)).require_clear()
|
||||
except (OSError, RuntimeError) as error:
|
||||
raise TransactionError("Ordinary journal writes blocked by migration state") from error
|
||||
|
||||
def validate_directory(self):
|
||||
directory = self.directory
|
||||
|
||||
@@ -336,7 +379,7 @@ class TransactionJournal:
|
||||
os.close(fd)
|
||||
|
||||
def _write(self, data):
|
||||
self.validate_directory()
|
||||
self.require_writes_allowed()
|
||||
|
||||
data["checksum"] = checksum(data)
|
||||
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
# Migration journal protection
|
||||
|
||||
Compose migration and ordinary deployment operations through `SiteOperations`
|
||||
using its journal, approval store and deployment lock. The journal persists
|
||||
its guard binding on disk. Journal creation, transition and retirement check
|
||||
migration state under the journal lock. Migration intent publication uses
|
||||
the same journal lock, so a concurrent ordinary writer cannot slip past the
|
||||
guard while the intent is being recorded.
|
||||
|
||||
The migration coordinator requires a trusted original-release probe. It
|
||||
records the active release number, revision and provenance fingerprint in
|
||||
the checksummed intent and rechecks them after containment. Forward recovery
|
||||
requires that same release to remain active and verifies its current
|
||||
provenance before activation. Unbound older intents require manual review.
|
||||
Interrupted or failed migration evidence keeps ordinary writes blocked.
|
||||
|
||||
Production execution remains disabled.
|
||||
@@ -4,6 +4,7 @@ from pathlib import Path
|
||||
|
||||
from core.dev3.operations import SiteOperations
|
||||
from core.locking import LockError
|
||||
from core.transactions import TransactionError, TransactionJournal
|
||||
|
||||
|
||||
class SiteOperationsTests(unittest.TestCase):
|
||||
@@ -14,3 +15,16 @@ class SiteOperationsTests(unittest.TestCase):
|
||||
with first.lock(), self.assertRaises(LockError):
|
||||
with second.lock():
|
||||
self.fail("Competing operation acquired site lock")
|
||||
|
||||
|
||||
def test_reopened_journal_refuses_writes_with_unfinished_migration(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
operations = SiteOperations(Path(directory).resolve())
|
||||
journal = operations.journal
|
||||
record = journal.create("fixture", "1", "2", "a" * 40, "b" * 64)
|
||||
before = journal.path.read_bytes()
|
||||
(operations.approvals.directory / "unfinished.json").write_text("{}")
|
||||
reopened = TransactionJournal(journal.directory)
|
||||
with self.assertRaisesRegex(TransactionError, "blocked by migration"):
|
||||
reopened.transition(record["transaction_id"], "PREPARED", "FAILED")
|
||||
self.assertEqual(journal.path.read_bytes(), before)
|
||||
|
||||
@@ -6,6 +6,7 @@ from unittest.mock import Mock
|
||||
|
||||
import test_approval_store
|
||||
|
||||
from core.dev3.operations import SiteOperations
|
||||
from core.migration import MigrationCoordinator, MigrationError
|
||||
from core.locking import DeploymentLock, LockError
|
||||
|
||||
@@ -16,6 +17,8 @@ class MigrationTests(unittest.TestCase):
|
||||
self.fixture = test_approval_store.ApprovalStoreTests()
|
||||
self.addCleanup(self.fixture.doCleanups)
|
||||
self.fixture.setUp()
|
||||
self.ops = SiteOperations(self.fixture.root)
|
||||
self.fixture.store = self.ops.approvals
|
||||
self.fixture.save()
|
||||
|
||||
self.root = self.fixture.root
|
||||
@@ -48,7 +51,12 @@ class MigrationTests(unittest.TestCase):
|
||||
|
||||
self.coordinator = MigrationCoordinator(
|
||||
store=self.fixture.store,
|
||||
lock_path=self.root / ".deployment.lock",
|
||||
lock_path=self.ops.lock_path,
|
||||
journal=self.ops.journal,
|
||||
original_release_probe=lambda: {
|
||||
"release":"1", "revision":self.fixture.base["revision"],
|
||||
"fingerprint":self.fixture.base["provenance"],
|
||||
},
|
||||
context_probe=self.context,
|
||||
contain=self.contain,
|
||||
containment_probe=self.contained,
|
||||
@@ -295,7 +303,7 @@ class MigrationTests(unittest.TestCase):
|
||||
self.migrate.assert_not_called()
|
||||
|
||||
def test_concurrent_deployment_rejected(self):
|
||||
with DeploymentLock(self.root / ".deployment.lock"):
|
||||
with DeploymentLock(self.ops.lock_path):
|
||||
with self.assertRaises(LockError):
|
||||
self.execute()
|
||||
|
||||
|
||||
@@ -76,7 +76,7 @@ class MigrationRecoveryTests(unittest.TestCase):
|
||||
self.recovery = MigrationRecovery(
|
||||
site=self.fixture.fixture.base["site"],
|
||||
store=self.store,
|
||||
lock_path=self.fixture.root / ".deployment.lock",
|
||||
lock_path=self.fixture.ops.lock_path,
|
||||
identity_probe=self.identity,
|
||||
history_probe=self.history,
|
||||
provenance_probe=self.provenance,
|
||||
@@ -188,6 +188,31 @@ class MigrationRecoveryTests(unittest.TestCase):
|
||||
with self.assertRaises(MigrationRecoveryError):
|
||||
self.plan()
|
||||
|
||||
def test_changed_original_release_blocks_recovery(self):
|
||||
self.active = "3"
|
||||
with self.assertRaises(MigrationRecoveryError):
|
||||
self.plan()
|
||||
|
||||
def test_changed_original_revision_blocks_recovery(self):
|
||||
candidate = self.provenance.return_value
|
||||
self.provenance.side_effect = lambda release: (
|
||||
{**candidate, "revision": "c" * 40} if release == "1" else candidate
|
||||
)
|
||||
with self.assertRaises(MigrationRecoveryError):
|
||||
self.plan()
|
||||
|
||||
def test_legacy_unbound_intent_blocks_recovery(self):
|
||||
path = self.fixture.coordinator.intent_path(self.identifier)
|
||||
record = self.store.read(path)
|
||||
record.pop("original_revision")
|
||||
self.rewrite(path, record)
|
||||
result_path = self.fixture.coordinator.result_path(self.identifier)
|
||||
result = self.store.read(result_path)
|
||||
result["intent_checksum"] = self.store.read(path)["checksum"]
|
||||
self.rewrite(result_path, result)
|
||||
with self.assertRaises(MigrationRecoveryError):
|
||||
self.plan()
|
||||
|
||||
def test_changed_candidate_revision(self):
|
||||
|
||||
self.provenance.return_value["revision"] = "0" * 40
|
||||
@@ -368,7 +393,7 @@ class MigrationRecoveryTests(unittest.TestCase):
|
||||
def test_concurrent_operation_rejected(self):
|
||||
|
||||
with DeploymentLock(
|
||||
self.fixture.root / ".deployment.lock"
|
||||
self.fixture.ops.lock_path
|
||||
):
|
||||
|
||||
with self.assertRaises(LockError):
|
||||
|
||||
Reference in New Issue
Block a user