feat: add restore and migration network ledgers
parent
e6bb6723a3
commit
25b386f02b
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
/** One immutable dispatch generation for an independent backup restore. */
|
||||
final class CloudBackupRestoreAttempt extends Model
|
||||
{
|
||||
protected $connection = 'default';
|
||||
protected $table = 'cloud_backup_restore_attempts';
|
||||
|
||||
protected $casts = [
|
||||
'id' => 'int',
|
||||
'backup_restore_id' => 'int',
|
||||
'generation' => 'int',
|
||||
'job_id' => 'int',
|
||||
'reservation_id' => 'int',
|
||||
'target_host_id' => 'int',
|
||||
'target_network_attachment_id' => 'int',
|
||||
'active_attempt' => 'int',
|
||||
'effect_resolved_at' => 'int',
|
||||
'terminal_at' => 'int',
|
||||
'created_at' => 'int',
|
||||
'updated_at' => 'int',
|
||||
];
|
||||
|
||||
public function restore()
|
||||
{
|
||||
return $this->belongsTo(CloudBackupRestore::class, 'backup_restore_id');
|
||||
}
|
||||
|
||||
public function boundReservationQuery()
|
||||
{
|
||||
$reservationId = (int) ($this->reservation_id ?? 0);
|
||||
$attemptKey = (string) ($this->attempt_key ?? '');
|
||||
if ($reservationId < 1
|
||||
|| preg_match('/^[a-f0-9]{32}$/D', $attemptKey) !== 1) {
|
||||
throw new \LogicException(
|
||||
'Restore attempt reservation identity is incomplete.'
|
||||
);
|
||||
}
|
||||
return CloudResourceReservation::query()
|
||||
->where('id', $reservationId)
|
||||
->where('backup_restore_attempt_key', $attemptKey);
|
||||
}
|
||||
|
||||
public function job()
|
||||
{
|
||||
return $this->belongsTo(CloudJob::class, 'job_id');
|
||||
}
|
||||
|
||||
public function matchesReservation(
|
||||
CloudResourceReservation $reservation
|
||||
): bool {
|
||||
$attemptKey = (string) ($this->attempt_key ?? '');
|
||||
return (int) ($this->reservation_id ?? 0) > 0
|
||||
&& (int) $this->reservation_id === (int) ($reservation->id ?? 0)
|
||||
&& preg_match('/^[a-f0-9]{32}$/D', $attemptKey) === 1
|
||||
&& hash_equals(
|
||||
$attemptKey,
|
||||
(string) ($reservation->backup_restore_attempt_key ?? '')
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
/** Durable ownership of a retained maintenance-migration source duplicate. */
|
||||
final class CloudInstanceMigrationSourceDebt extends Model
|
||||
{
|
||||
protected $connection = 'default';
|
||||
protected $table = 'cloud_instance_migration_source_debts';
|
||||
|
||||
protected $casts = [
|
||||
'id' => 'int',
|
||||
'migration_id' => 'int',
|
||||
'source_reservation_id' => 'int',
|
||||
'source_host_id' => 'int',
|
||||
'source_network_attachment_id' => 'int',
|
||||
'cleanup_job_id' => 'int',
|
||||
'active_debt' => 'int',
|
||||
'resolved_at' => 'int',
|
||||
'created_at' => 'int',
|
||||
'updated_at' => 'int',
|
||||
];
|
||||
|
||||
public function migration()
|
||||
{
|
||||
return $this->belongsTo(CloudInstanceMigration::class, 'migration_id');
|
||||
}
|
||||
|
||||
public function boundReservationQuery()
|
||||
{
|
||||
$debtId = (int) ($this->id ?? 0);
|
||||
$reservationId = (int) ($this->source_reservation_id ?? 0);
|
||||
if ($debtId < 1 || $reservationId < 1) {
|
||||
throw new \LogicException(
|
||||
'Migration source-debt reservation identity is incomplete.'
|
||||
);
|
||||
}
|
||||
return CloudResourceReservation::query()
|
||||
->where('id', $reservationId)
|
||||
->where('migration_source_debt_id', $debtId);
|
||||
}
|
||||
|
||||
public function cleanupJob()
|
||||
{
|
||||
return $this->belongsTo(CloudJob::class, 'cleanup_job_id');
|
||||
}
|
||||
|
||||
public function matchesReservation(
|
||||
CloudResourceReservation $reservation
|
||||
): bool {
|
||||
return (int) ($this->id ?? 0) > 0
|
||||
&& (int) ($this->source_reservation_id ?? 0) > 0
|
||||
&& (int) $this->source_reservation_id
|
||||
=== (int) ($reservation->id ?? 0)
|
||||
&& (int) ($reservation->migration_source_debt_id ?? 0)
|
||||
=== (int) $this->id;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,250 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Cloud;
|
||||
|
||||
/**
|
||||
* Immutable stored identity for one exact public dual-stack claim set.
|
||||
*
|
||||
* Canonical network and claim digests come only from the authoritative active
|
||||
* claim-set projection. Callers must obtain that projection through
|
||||
* CloudPublicNetworkAllocationService::currentClaimSet(); this module never
|
||||
* accepts raw claim/address rows and therefore cannot bypass its topology
|
||||
* validation.
|
||||
*/
|
||||
final class CloudFrozenPublicNetworkIdentity
|
||||
{
|
||||
private function __construct(
|
||||
private readonly array $identity,
|
||||
private readonly CloudPublicNetworkSnapshot $network,
|
||||
private readonly string $claimSetSha256
|
||||
) {
|
||||
}
|
||||
|
||||
public static function fromClaimSet(int $hostId, string $uuid,
|
||||
CloudActivePublicNetworkClaimSet $claimSet): self
|
||||
{
|
||||
$reservationId = $claimSet->reservationId();
|
||||
$networkAttachmentId = $claimSet->networkAttachmentId();
|
||||
self::assertResourceIdentity(
|
||||
$reservationId,
|
||||
$hostId,
|
||||
$uuid,
|
||||
$networkAttachmentId
|
||||
);
|
||||
$network = $claimSet->publicNetwork();
|
||||
$claimSetSha256 = $claimSet->digest();
|
||||
if (preg_match('/^[a-f0-9]{64}$/D', $claimSetSha256) !== 1) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Frozen public network claim-set digest is invalid.'
|
||||
);
|
||||
}
|
||||
return new self(self::identity(
|
||||
$reservationId,
|
||||
$hostId,
|
||||
$uuid,
|
||||
$networkAttachmentId,
|
||||
$claimSetSha256,
|
||||
$network->fingerprint()
|
||||
), $network, $claimSetSha256);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rebuild a frozen identity from its row and the current authoritative
|
||||
* claim-set projection. The host id must come from the same locked
|
||||
* reservation used to obtain the claim set. Top-level row columns remain
|
||||
* separate so a self-consistent cross-target JSON replacement is rejected.
|
||||
*/
|
||||
public static function fromStored(int $reservationId, int $hostId,
|
||||
string $uuid, int $networkAttachmentId, string $identityJson,
|
||||
string $identitySha256, string $publicNetworkJson,
|
||||
string $publicNetworkSha256, string $claimSetSha256,
|
||||
CloudActivePublicNetworkClaimSet $claimSet): self
|
||||
{
|
||||
self::assertResourceIdentity(
|
||||
$reservationId,
|
||||
$hostId,
|
||||
$uuid,
|
||||
$networkAttachmentId
|
||||
);
|
||||
if (preg_match('/^[a-f0-9]{64}$/D', $identitySha256) !== 1
|
||||
|| preg_match('/^[a-f0-9]{64}$/D', $publicNetworkSha256) !== 1
|
||||
|| preg_match('/^[a-f0-9]{64}$/D', $claimSetSha256) !== 1) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Frozen public network digest is invalid.'
|
||||
);
|
||||
}
|
||||
try {
|
||||
$identity = json_decode(
|
||||
$identityJson,
|
||||
true,
|
||||
16,
|
||||
JSON_THROW_ON_ERROR
|
||||
);
|
||||
$decoded = json_decode(
|
||||
$publicNetworkJson,
|
||||
true,
|
||||
64,
|
||||
JSON_THROW_ON_ERROR
|
||||
);
|
||||
} catch (\JsonException $exception) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Frozen public network JSON is invalid.',
|
||||
0,
|
||||
$exception
|
||||
);
|
||||
}
|
||||
if (!is_array($identity) || array_is_list($identity)) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Frozen public network identity is invalid.'
|
||||
);
|
||||
}
|
||||
self::exactKeys($identity, [
|
||||
'claim_set_sha256', 'contract', 'host_id',
|
||||
'network_attachment_id', 'public_network_sha256',
|
||||
'reservation_id', 'uuid',
|
||||
]);
|
||||
if (($identity['contract'] ?? null) !== 1
|
||||
|| !is_int($identity['reservation_id'] ?? null)
|
||||
|| !is_int($identity['host_id'] ?? null)
|
||||
|| !is_int($identity['network_attachment_id'] ?? null)
|
||||
|| !is_string($identity['uuid'] ?? null)
|
||||
|| !is_string($identity['claim_set_sha256'] ?? null)
|
||||
|| !is_string($identity['public_network_sha256'] ?? null)) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Frozen public network identity is invalid.'
|
||||
);
|
||||
}
|
||||
$network = CloudPublicNetworkSnapshot::fromArray($decoded);
|
||||
if (!hash_equals($network->fingerprint(), $publicNetworkSha256)) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Frozen public network digest does not match its payload.'
|
||||
);
|
||||
}
|
||||
$canonical = self::identity(
|
||||
$reservationId,
|
||||
$hostId,
|
||||
$uuid,
|
||||
$networkAttachmentId,
|
||||
$claimSetSha256,
|
||||
$publicNetworkSha256
|
||||
);
|
||||
if ($identity !== $canonical
|
||||
|| !hash_equals($identity['claim_set_sha256'], $claimSetSha256)
|
||||
|| !hash_equals(
|
||||
$identity['public_network_sha256'],
|
||||
$publicNetworkSha256
|
||||
)
|
||||
|| !hash_equals(self::digest($canonical), $identitySha256)) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Frozen public network identity digest does not match.'
|
||||
);
|
||||
}
|
||||
$authoritative = self::fromClaimSet($hostId, $uuid, $claimSet);
|
||||
if ($claimSet->reservationId() !== $reservationId
|
||||
|| $claimSet->networkAttachmentId() !== $networkAttachmentId
|
||||
|| !hash_equals(
|
||||
$authoritative->claimSetSha256(),
|
||||
$claimSetSha256
|
||||
)
|
||||
|| !hash_equals(
|
||||
$authoritative->publicNetworkSha256(),
|
||||
$publicNetworkSha256
|
||||
)
|
||||
|| !hash_equals(
|
||||
$authoritative->publicNetwork()->fingerprint(),
|
||||
$network->fingerprint()
|
||||
)) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Frozen public network identity does not match its claim set.'
|
||||
);
|
||||
}
|
||||
return new self($canonical, $network, $claimSetSha256);
|
||||
}
|
||||
|
||||
public function identityJson(): string
|
||||
{
|
||||
return self::encode($this->identity);
|
||||
}
|
||||
|
||||
public function identitySha256(): string
|
||||
{
|
||||
return self::digest($this->identity);
|
||||
}
|
||||
|
||||
public function publicNetwork(): CloudPublicNetworkSnapshot
|
||||
{
|
||||
return $this->network;
|
||||
}
|
||||
|
||||
public function publicNetworkJson(): string
|
||||
{
|
||||
return json_encode(
|
||||
$this->network->toArray(),
|
||||
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
|
||||
);
|
||||
}
|
||||
|
||||
public function publicNetworkSha256(): string
|
||||
{
|
||||
return $this->network->fingerprint();
|
||||
}
|
||||
|
||||
public function claimSetSha256(): string
|
||||
{
|
||||
return $this->claimSetSha256;
|
||||
}
|
||||
|
||||
private static function identity(int $reservationId, int $hostId,
|
||||
string $uuid, int $networkAttachmentId, string $claimSetSha256,
|
||||
string $publicNetworkSha256): array
|
||||
{
|
||||
return [
|
||||
'contract' => 1,
|
||||
'reservation_id' => $reservationId,
|
||||
'host_id' => $hostId,
|
||||
'uuid' => strtolower($uuid),
|
||||
'network_attachment_id' => $networkAttachmentId,
|
||||
'claim_set_sha256' => $claimSetSha256,
|
||||
'public_network_sha256' => $publicNetworkSha256,
|
||||
];
|
||||
}
|
||||
|
||||
private static function assertResourceIdentity(int $reservationId,
|
||||
int $hostId, string $uuid, int $networkAttachmentId): void
|
||||
{
|
||||
if ($reservationId < 1 || $hostId < 1 || $networkAttachmentId < 1
|
||||
|| preg_match(
|
||||
'/^[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/D',
|
||||
strtolower($uuid)
|
||||
) !== 1) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Frozen public network resource identity is invalid.'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private static function exactKeys(array $value, array $expected): void
|
||||
{
|
||||
$actual = array_keys($value);
|
||||
sort($actual, SORT_STRING);
|
||||
sort($expected, SORT_STRING);
|
||||
if ($actual !== $expected) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Frozen public network identity fields are invalid.'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private static function encode(array $value): string
|
||||
{
|
||||
return json_encode(
|
||||
$value,
|
||||
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
|
||||
);
|
||||
}
|
||||
|
||||
private static function digest(array $value): string
|
||||
{
|
||||
return hash('sha256', self::encode($value));
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,278 @@
|
||||
<?php
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
use App\Models\CloudBackupRestoreAttempt;
|
||||
use App\Models\CloudInstanceMigrationSourceDebt;
|
||||
use App\Models\CloudResourceReservation;
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
|
||||
$root = dirname(__DIR__);
|
||||
$schema = file_get_contents($root . '/sql/cloud.sql');
|
||||
$setup = file_get_contents($root . '/app/Command/CloudSetup.php');
|
||||
$reservationModel = file_get_contents(
|
||||
$root . '/app/Models/CloudResourceReservation.php'
|
||||
);
|
||||
$attemptModel = file_get_contents(
|
||||
$root . '/app/Models/CloudBackupRestoreAttempt.php'
|
||||
);
|
||||
$debtModel = file_get_contents(
|
||||
$root . '/app/Models/CloudInstanceMigrationSourceDebt.php'
|
||||
);
|
||||
if (!is_string($schema) || !is_string($setup)
|
||||
|| !is_string($reservationModel) || !is_string($attemptModel)
|
||||
|| !is_string($debtModel)) {
|
||||
throw new RuntimeException('Restore/migration ledger sources are unreadable.');
|
||||
}
|
||||
|
||||
$assert = static function (bool $condition, string $message): void {
|
||||
if (!$condition) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
};
|
||||
$table = static function (string $name) use ($schema): string {
|
||||
$start = strpos($schema, 'CREATE TABLE ' . $name . ' (');
|
||||
if ($start === false) {
|
||||
return '';
|
||||
}
|
||||
$end = strpos(
|
||||
$schema,
|
||||
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;',
|
||||
$start
|
||||
);
|
||||
return $end === false ? '' : substr($schema, $start, $end - $start);
|
||||
};
|
||||
$normalized = static function (string $value): string {
|
||||
return trim((string) preg_replace('/\s+/', ' ', $value));
|
||||
};
|
||||
|
||||
$capsule = new Capsule();
|
||||
$capsule->addConnection([
|
||||
'driver' => 'sqlite',
|
||||
'database' => ':memory:',
|
||||
], 'default');
|
||||
$capsule->setAsGlobal();
|
||||
$capsule->bootEloquent();
|
||||
$schemaBuilder = $capsule->schema('default');
|
||||
$schemaBuilder->create('cloud_backup_restore_attempts', static function (
|
||||
$table
|
||||
): void {
|
||||
$table->integer('id')->primary();
|
||||
$table->string('attempt_key');
|
||||
$table->integer('reservation_id');
|
||||
});
|
||||
$schemaBuilder->create('cloud_instance_migration_source_debts', static function (
|
||||
$table
|
||||
): void {
|
||||
$table->integer('id')->primary();
|
||||
$table->integer('source_reservation_id');
|
||||
});
|
||||
$schemaBuilder->create('cloud_resource_reservations', static function (
|
||||
$table
|
||||
): void {
|
||||
$table->integer('id')->primary();
|
||||
$table->string('backup_restore_attempt_key')->nullable();
|
||||
$table->integer('migration_source_debt_id')->nullable();
|
||||
});
|
||||
|
||||
$attempts = $table('cloud_backup_restore_attempts');
|
||||
$debts = $table('cloud_instance_migration_source_debts');
|
||||
$reservations = $table('cloud_resource_reservations');
|
||||
$assert($attempts !== ''
|
||||
&& str_contains($attempts,
|
||||
'reservation_id BIGINT UNSIGNED NOT NULL')
|
||||
&& str_contains($attempts,
|
||||
'UNIQUE KEY cloud_backup_restore_attempt_generation')
|
||||
&& str_contains($attempts,
|
||||
'UNIQUE KEY cloud_backup_restore_attempt_active')
|
||||
&& str_contains($attempts,
|
||||
'UNIQUE KEY cloud_backup_restore_attempt_operation (operation_id)')
|
||||
&& str_contains($attempts,
|
||||
'target_identity_sha256 CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL')
|
||||
&& str_contains($attempts,
|
||||
'target_claim_set_sha256 CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL')
|
||||
&& str_contains($attempts,
|
||||
'target_public_network_sha256 CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL')
|
||||
&& str_contains($attempts,
|
||||
'CONSTRAINT chk_cloud_backup_restore_attempt_shape CHECK'),
|
||||
'Backup restore attempts do not fence immutable generations and network identity.');
|
||||
$attemptShape = $normalized($attempts);
|
||||
$assert(str_contains($attemptShape,
|
||||
"state = 'reserved' AND job_id IS NULL AND effect_resolution = 'not_applicable' AND active_attempt IS NOT NULL AND active_attempt = 1")
|
||||
&& str_contains($attemptShape,
|
||||
"state IN ('queued','awaiting_receipt') AND job_id IS NOT NULL AND effect_resolution = 'not_applicable' AND active_attempt IS NOT NULL AND active_attempt = 1")
|
||||
&& str_contains($attemptShape,
|
||||
"state = 'effect_unknown' AND effect_resolution = 'unknown' AND job_id IS NOT NULL AND active_attempt IS NOT NULL AND active_attempt = 1")
|
||||
&& str_contains($attemptShape,
|
||||
"state = 'finalization_pending' AND job_id IS NOT NULL AND effect_resolution IN ('applied','not_applied') AND active_attempt IS NOT NULL AND active_attempt = 1")
|
||||
&& str_contains($attemptShape,
|
||||
"state = 'succeeded' AND effect_resolution = 'applied' AND job_id IS NOT NULL AND active_attempt IS NULL")
|
||||
&& str_contains($attemptShape,
|
||||
"state = 'failed' AND effect_resolution = 'not_applied' AND job_id IS NOT NULL AND active_attempt IS NULL"),
|
||||
'Restore-attempt terminal and unknown-effect fencing can diverge.');
|
||||
|
||||
$assert($debts !== ''
|
||||
&& str_contains($debts,
|
||||
'UNIQUE KEY cloud_migration_source_debt_migration (migration_id)')
|
||||
&& str_contains($debts,
|
||||
'UNIQUE KEY cloud_migration_source_debt_active_reservation')
|
||||
&& str_contains($debts,
|
||||
'source_identity_sha256 CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL')
|
||||
&& str_contains($debts,
|
||||
'source_claim_set_sha256 CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL')
|
||||
&& str_contains($debts,
|
||||
'source_public_network_sha256 CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL')
|
||||
&& str_contains($debts,
|
||||
"state IN ('prepared','retained','cleanup_running','released',")
|
||||
&& str_contains($debts,
|
||||
'CONSTRAINT chk_cloud_migration_source_debt_shape CHECK'),
|
||||
'Maintenance migration source cleanup debt is not durable and exclusive.');
|
||||
$debtShape = $normalized($debts);
|
||||
$assert(str_contains($debtShape,
|
||||
"(cleanup_operation_id IS NULL AND cleanup_job_id IS NULL) OR (cleanup_operation_id IS NOT NULL AND cleanup_job_id IS NOT NULL)")
|
||||
&& str_contains($debtShape,
|
||||
"state IN ('prepared','retained') AND active_debt IS NOT NULL AND active_debt = 1 AND cleanup_operation_id IS NULL AND cleanup_job_id IS NULL AND resolved_at IS NULL")
|
||||
&& str_contains($debtShape,
|
||||
"state = 'cleanup_running' AND active_debt IS NOT NULL AND active_debt = 1 AND cleanup_operation_id IS NOT NULL AND cleanup_job_id IS NOT NULL AND resolved_at IS NULL")
|
||||
&& str_contains($debtShape,
|
||||
"state = 'attention' AND active_debt IS NOT NULL AND active_debt = 1 AND resolved_at IS NULL")
|
||||
&& str_contains($debtShape,
|
||||
"state = 'released' AND active_debt IS NULL AND cleanup_operation_id IS NOT NULL AND cleanup_job_id IS NOT NULL AND resolved_at IS NOT NULL")
|
||||
&& str_contains($debtShape,
|
||||
"state = 'rollback_restored' AND active_debt IS NULL AND cleanup_operation_id IS NULL AND cleanup_job_id IS NULL AND resolved_at IS NOT NULL"),
|
||||
'Source debt can become terminal without confirmed resolution.');
|
||||
// MySQL accepts CHECK expressions that evaluate to UNKNOWN. Every live-state
|
||||
// branch must therefore reject NULL explicitly before comparing the nullable
|
||||
// unique-index fence with 1.
|
||||
$assert(!str_contains($attemptShape,
|
||||
"effect_resolution = 'unknown' AND active_attempt = 1")
|
||||
&& !str_contains($debtShape,
|
||||
"state = 'attention' AND active_debt = 1"),
|
||||
'A nullable active fence can satisfy MySQL CHECK through UNKNOWN.');
|
||||
|
||||
$assert($reservations !== ''
|
||||
&& str_contains($reservations,
|
||||
'backup_restore_attempt_key CHAR(32) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL')
|
||||
&& str_contains($reservations,
|
||||
'migration_source_debt_id BIGINT UNSIGNED DEFAULT NULL')
|
||||
&& str_contains($reservations,
|
||||
'UNIQUE KEY cloud_resource_reservation_backup_attempt')
|
||||
&& str_contains($reservations,
|
||||
'UNIQUE KEY cloud_resource_reservation_migration_debt')
|
||||
&& !str_contains($reservations, 'backup_restore_attempt_id')
|
||||
&& substr_count($reservations,
|
||||
'backup_restore_attempt_key IS NOT NULL') === 1
|
||||
&& substr_count($reservations, 'migration_source_debt_id IS NOT NULL') === 1,
|
||||
'Reservation ownership does not enforce one source ledger.');
|
||||
|
||||
foreach ([
|
||||
"'cloud_backup_restore_attempts'",
|
||||
"'cloud_instance_migration_source_debts'",
|
||||
"'backup_restore_attempt_key'",
|
||||
"'migration_source_debt_id'",
|
||||
"'chk_cloud_backup_restore_attempt_shape'",
|
||||
"'chk_cloud_migration_source_debt_shape'",
|
||||
"'chk_cloud_migration_source_debt_cleanup_pair'",
|
||||
"'cloud_backup_restore_attempt_active'",
|
||||
"'cloud_migration_source_debt_active_reservation'",
|
||||
"'columns' => ['backup_restore_attempt_key']",
|
||||
"'columns' => ['migration_source_debt_id']",
|
||||
] as $required) {
|
||||
$assert(str_contains($setup, $required),
|
||||
'CloudSetup omits restore/migration ledger contract: ' . $required);
|
||||
}
|
||||
$assert(str_contains($setup,
|
||||
"SCHEMA_VERSION = 'go-agent-wss-v31-restore-migration-ledgers'")
|
||||
&& str_contains($attemptModel,
|
||||
"protected \$table = 'cloud_backup_restore_attempts';")
|
||||
&& str_contains($attemptModel, "'active_attempt' => 'int'")
|
||||
&& str_contains($debtModel,
|
||||
"protected \$table = 'cloud_instance_migration_source_debts';")
|
||||
&& str_contains($debtModel, "'active_debt' => 'int'")
|
||||
&& str_contains($reservationModel,
|
||||
"function backupRestoreAttemptQuery()")
|
||||
&& str_contains($attemptModel,
|
||||
"function boundReservationQuery()")
|
||||
&& str_contains($debtModel,
|
||||
"function boundReservationQuery()")
|
||||
&& str_contains($reservationModel,
|
||||
"'migration_source_debt_id' => 'int'"),
|
||||
'Models or setup version do not expose the new ownership boundary.');
|
||||
|
||||
$frozenIdentity = file_get_contents(
|
||||
$root . '/app/Services/Cloud/CloudFrozenPublicNetworkIdentity.php'
|
||||
);
|
||||
$assert(is_string($frozenIdentity)
|
||||
&& str_contains($frozenIdentity,
|
||||
'public static function fromClaimSet(int $hostId, string $uuid,')
|
||||
&& str_contains($frozenIdentity,
|
||||
'CloudActivePublicNetworkClaimSet $claimSet')
|
||||
&& str_contains($frozenIdentity,
|
||||
'CloudPublicNetworkAllocationService::currentClaimSet()')
|
||||
&& !str_contains($frozenIdentity,
|
||||
'public static function fromLedger(')
|
||||
&& !str_contains($frozenIdentity, 'array $claims')
|
||||
&& !str_contains($frozenIdentity, 'array $addresses'),
|
||||
'Frozen identity can bypass the authoritative claim-set module.');
|
||||
|
||||
$attemptRecord = new CloudBackupRestoreAttempt();
|
||||
$attemptRecord->id = 91;
|
||||
$attemptRecord->attempt_key = str_repeat('e', 32);
|
||||
$attemptRecord->reservation_id = 92;
|
||||
$reservationRecord = new CloudResourceReservation();
|
||||
$reservationRecord->id = 92;
|
||||
$reservationRecord->backup_restore_attempt_key = str_repeat('e', 32);
|
||||
Capsule::table('cloud_backup_restore_attempts')->insert([
|
||||
'id' => 91,
|
||||
'attempt_key' => str_repeat('e', 32),
|
||||
'reservation_id' => 92,
|
||||
]);
|
||||
Capsule::table('cloud_resource_reservations')->insert([
|
||||
'id' => 92,
|
||||
'backup_restore_attempt_key' => str_repeat('e', 32),
|
||||
'migration_source_debt_id' => null,
|
||||
]);
|
||||
$assert($attemptRecord->matchesReservation($reservationRecord)
|
||||
&& (int) $attemptRecord->boundReservationQuery()->first()?->id === 92
|
||||
&& (int) $reservationRecord->backupRestoreAttemptQuery()->first()?->id
|
||||
=== 91,
|
||||
'Restore attempt and reservation are not joined by id and stable key.');
|
||||
$reservationRecord->backup_restore_attempt_key = str_repeat('f', 32);
|
||||
Capsule::table('cloud_resource_reservations')->where('id', 92)->update([
|
||||
'backup_restore_attempt_key' => str_repeat('f', 32),
|
||||
]);
|
||||
$assert(!$attemptRecord->matchesReservation($reservationRecord)
|
||||
&& $attemptRecord->boundReservationQuery()->first() === null
|
||||
&& $reservationRecord->backupRestoreAttemptQuery()->first() === null,
|
||||
'Restore attempt accepted a cross-bound reservation identity.');
|
||||
|
||||
$debtRecord = new CloudInstanceMigrationSourceDebt();
|
||||
$debtRecord->id = 101;
|
||||
$debtRecord->source_reservation_id = 102;
|
||||
$debtReservation = new CloudResourceReservation();
|
||||
$debtReservation->id = 102;
|
||||
$debtReservation->migration_source_debt_id = 101;
|
||||
Capsule::table('cloud_instance_migration_source_debts')->insert([
|
||||
'id' => 101,
|
||||
'source_reservation_id' => 102,
|
||||
]);
|
||||
Capsule::table('cloud_resource_reservations')->insert([
|
||||
'id' => 102,
|
||||
'backup_restore_attempt_key' => null,
|
||||
'migration_source_debt_id' => 101,
|
||||
]);
|
||||
$assert($debtRecord->matchesReservation($debtReservation)
|
||||
&& (int) $debtRecord->boundReservationQuery()->first()?->id === 102
|
||||
&& (int) $debtReservation->migrationSourceDebtQuery()->first()?->id
|
||||
=== 101,
|
||||
'Migration source debt does not bind the exact source reservation both ways.');
|
||||
$debtReservation->migration_source_debt_id = 103;
|
||||
Capsule::table('cloud_resource_reservations')->where('id', 102)->update([
|
||||
'migration_source_debt_id' => 103,
|
||||
]);
|
||||
$assert(!$debtRecord->matchesReservation($debtReservation)
|
||||
&& $debtRecord->boundReservationQuery()->first() === null
|
||||
&& $debtReservation->migrationSourceDebtQuery()->first() === null,
|
||||
'Migration source debt accepted a cross-bound reservation identity.');
|
||||
|
||||
echo "Restore/migration durable-ledger contract tests passed.\n";
|
||||
Loading…
Reference in New Issue