feat: freeze portable restore compatibility
parent
661eead7c2
commit
a54d96dce6
@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Cloud;
|
||||
|
||||
/**
|
||||
* Canonicalize the only portable restore platform contract supported by this
|
||||
* clean-break release. Callers never compare loosely decoded Agent payloads or
|
||||
* construct hashes themselves.
|
||||
*/
|
||||
final class CloudPortableRestoreCompatibility
|
||||
{
|
||||
private const CONTRACT = [
|
||||
'contract' => 1,
|
||||
'architecture' => 'x86_64',
|
||||
'machine' => 'q35',
|
||||
'firmware' => 'uefi',
|
||||
'secure_boot' => false,
|
||||
'disk_format' => 'qcow2',
|
||||
'system_disk_only' => true,
|
||||
'public_network_contract' => 1,
|
||||
'vpc_inherited' => false,
|
||||
];
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public static function fromPayload(mixed $value): self
|
||||
{
|
||||
if (!is_array($value) || array_is_list($value)
|
||||
|| count($value) !== count(self::CONTRACT)) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Portable restore contract has an invalid shape'
|
||||
);
|
||||
}
|
||||
foreach (self::CONTRACT as $name => $expected) {
|
||||
if (!array_key_exists($name, $value) || $value[$name] !== $expected) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Portable restore contract is unsupported'
|
||||
);
|
||||
}
|
||||
}
|
||||
return new self();
|
||||
}
|
||||
|
||||
public static function fromStored(string $json, string $sha256): self
|
||||
{
|
||||
if (preg_match('/^[a-f0-9]{64}$/D', $sha256) !== 1) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Portable restore contract digest is invalid'
|
||||
);
|
||||
}
|
||||
try {
|
||||
$value = json_decode($json, true, 8, JSON_THROW_ON_ERROR);
|
||||
} catch (\JsonException $error) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Portable restore contract JSON is invalid', 0, $error
|
||||
);
|
||||
}
|
||||
$contract = self::fromPayload($value);
|
||||
if (!hash_equals($contract->json(), $json)
|
||||
|| !hash_equals($contract->sha256(), $sha256)) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Portable restore contract evidence does not match'
|
||||
);
|
||||
}
|
||||
return $contract;
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return self::CONTRACT;
|
||||
}
|
||||
|
||||
public function json(): string
|
||||
{
|
||||
return json_encode(
|
||||
self::CONTRACT,
|
||||
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
|
||||
);
|
||||
}
|
||||
|
||||
public function sha256(): string
|
||||
{
|
||||
return hash('sha256', $this->json());
|
||||
}
|
||||
|
||||
public function matches(self $other): bool
|
||||
{
|
||||
return hash_equals($this->sha256(), $other->sha256());
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
use App\Models\CloudInstanceBackup;
|
||||
use App\Models\CloudJob;
|
||||
use App\Services\Cloud\CloudBackupService;
|
||||
use App\Services\Cloud\CloudError;
|
||||
use App\Services\Cloud\CloudPortableRestoreCompatibility;
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
|
||||
$assert = static function (bool $condition, string $message): void {
|
||||
if (!$condition) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
};
|
||||
$capsule = new Capsule();
|
||||
$capsule->addConnection([
|
||||
'driver' => 'sqlite', 'database' => ':memory:',
|
||||
], 'default');
|
||||
$capsule->setAsGlobal();
|
||||
$capsule->bootEloquent();
|
||||
$schema = $capsule->schema('default');
|
||||
$schema->create('cloud_instance_backups', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->string('backup_key')->unique();
|
||||
$table->integer('source_instance_id');
|
||||
$table->integer('source_host_id');
|
||||
$table->integer('user_id');
|
||||
$table->string('source_uuid');
|
||||
$table->string('name');
|
||||
$table->string('state');
|
||||
$table->string('consistency');
|
||||
$table->string('object_key');
|
||||
$table->integer('object_size_bytes')->default(0);
|
||||
$table->integer('virtual_size_bytes')->default(0);
|
||||
$table->string('sha256')->default('');
|
||||
$table->string('object_etag')->default('');
|
||||
$table->string('object_version_id')->default('');
|
||||
$table->text('source_portable_restore_contract_json')->default('{}');
|
||||
$table->string('source_portable_restore_contract_sha256', 64)->default('');
|
||||
$table->text('spec_json');
|
||||
$table->text('sealed_storage_runtime');
|
||||
$table->text('sealed_credential')->nullable();
|
||||
$table->integer('storage_revision')->default(1);
|
||||
$table->integer('active_job_id')->nullable();
|
||||
$table->string('last_operation_id')->nullable();
|
||||
$table->string('error_code')->default('');
|
||||
$table->integer('ready_at')->nullable();
|
||||
$table->integer('created_at');
|
||||
$table->integer('updated_at');
|
||||
});
|
||||
$schema->create('cloud_jobs', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->string('kind');
|
||||
$table->integer('host_id');
|
||||
$table->integer('instance_id')->nullable();
|
||||
$table->integer('backup_id')->nullable();
|
||||
$table->integer('user_id')->nullable();
|
||||
$table->string('operation_id')->unique();
|
||||
});
|
||||
|
||||
$payload = [
|
||||
'contract' => 1, 'architecture' => 'x86_64', 'machine' => 'q35',
|
||||
'firmware' => 'uefi', 'secure_boot' => false, 'disk_format' => 'qcow2',
|
||||
'system_disk_only' => true, 'public_network_contract' => 1,
|
||||
'vpc_inherited' => false,
|
||||
];
|
||||
$contract = CloudPortableRestoreCompatibility::fromPayload($payload);
|
||||
$now = time();
|
||||
$create = static function (int $id, string $key, string $operation) use (
|
||||
$capsule, $now
|
||||
): CloudJob {
|
||||
$capsule->table('cloud_instance_backups')->insert([
|
||||
'id' => $id, 'backup_key' => $key,
|
||||
'source_instance_id' => 100 + $id, 'source_host_id' => 3,
|
||||
'user_id' => 9,
|
||||
'source_uuid' => sprintf('00000000-0000-4000-8000-%012d', $id),
|
||||
'name' => 'portable-' . $id, 'state' => 'creating',
|
||||
'consistency' => 'crash',
|
||||
'object_key' => 'vm-backups/' . $key . '/system.qcow2',
|
||||
'spec_json' => '{}', 'sealed_storage_runtime' => 'sealed',
|
||||
'active_job_id' => $id, 'last_operation_id' => $operation,
|
||||
'created_at' => $now, 'updated_at' => $now,
|
||||
]);
|
||||
$capsule->table('cloud_jobs')->insert([
|
||||
'id' => $id, 'kind' => 'backup_create', 'host_id' => 3,
|
||||
'instance_id' => 100 + $id, 'backup_id' => $id,
|
||||
'operation_id' => $operation,
|
||||
]);
|
||||
return CloudJob::findOrFail($id);
|
||||
};
|
||||
$result = static function (string $key) use ($payload): array {
|
||||
return [
|
||||
'backup_key' => $key, 'size_bytes' => 1024,
|
||||
'virtual_size_bytes' => 20 * 1073741824,
|
||||
'sha256' => str_repeat('a', 64), 'object_etag' => 'etag',
|
||||
'object_version_id' => '', 'consistency' => 'crash',
|
||||
'cleanup_pending' => false, 'source_contract' => $payload,
|
||||
];
|
||||
};
|
||||
$finalizerPayload = static function (string $key) use ($contract): array {
|
||||
return [
|
||||
'owner_id' => 9, 'backup_key' => $key,
|
||||
'maximum_size_bytes' => 21 * 1073741824,
|
||||
'source_portable_restore_contract_sha256' => $contract->sha256(),
|
||||
];
|
||||
};
|
||||
|
||||
$key = str_repeat('1', 32);
|
||||
CloudBackupService::finishJobLocked(
|
||||
$create(1, $key, str_repeat('2', 32)),
|
||||
'succeeded', $result($key), $finalizerPayload($key)
|
||||
);
|
||||
$ready = CloudInstanceBackup::findOrFail(1);
|
||||
$assert((string) $ready->state === 'ready'
|
||||
&& (string) $ready->source_portable_restore_contract_json
|
||||
=== $contract->json()
|
||||
&& (string) $ready->source_portable_restore_contract_sha256
|
||||
=== $contract->sha256()
|
||||
&& $ready->sourcePortableRestoreCompatibility() !== null,
|
||||
'Valid Agent source contract was not frozen with the ready backup.');
|
||||
|
||||
$missingKey = str_repeat('3', 32);
|
||||
$missing = $result($missingKey);
|
||||
unset($missing['source_contract']);
|
||||
try {
|
||||
CloudBackupService::finishJobLocked(
|
||||
$create(2, $missingKey, str_repeat('4', 32)),
|
||||
'succeeded', $missing, $finalizerPayload($missingKey)
|
||||
);
|
||||
throw new RuntimeException('Missing source contract was accepted.');
|
||||
} catch (CloudError $error) {
|
||||
$assert($error->httpStatus === 502,
|
||||
'Missing source contract returned the wrong status.');
|
||||
}
|
||||
$assert((string) CloudInstanceBackup::findOrFail(2)->state === 'creating',
|
||||
'Missing source contract promoted a backup to ready.');
|
||||
|
||||
echo "Portable backup source contract finalizer passed.\n";
|
||||
@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
use App\Services\Cloud\CloudPortableRestoreCompatibility;
|
||||
|
||||
$assert = static function (bool $condition, string $message): void {
|
||||
if (!$condition) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
};
|
||||
|
||||
$canonical = [
|
||||
'contract' => 1,
|
||||
'architecture' => 'x86_64',
|
||||
'machine' => 'q35',
|
||||
'firmware' => 'uefi',
|
||||
'secure_boot' => false,
|
||||
'disk_format' => 'qcow2',
|
||||
'system_disk_only' => true,
|
||||
'public_network_contract' => 1,
|
||||
'vpc_inherited' => false,
|
||||
];
|
||||
$contract = CloudPortableRestoreCompatibility::fromPayload($canonical);
|
||||
$assert($contract->toArray() === $canonical,
|
||||
'Portable restore contract did not canonicalize exactly.');
|
||||
$assert(strlen($contract->sha256()) === 64,
|
||||
'Portable restore contract digest is invalid.');
|
||||
$assert(CloudPortableRestoreCompatibility::fromStored(
|
||||
$contract->json(), $contract->sha256()
|
||||
)->matches($contract), 'Stored portable restore evidence did not round trip.');
|
||||
|
||||
foreach (array_keys($canonical) as $field) {
|
||||
$broken = $canonical;
|
||||
unset($broken[$field]);
|
||||
try {
|
||||
CloudPortableRestoreCompatibility::fromPayload($broken);
|
||||
throw new RuntimeException("Missing {$field} was accepted.");
|
||||
} catch (InvalidArgumentException) {
|
||||
}
|
||||
}
|
||||
foreach ([
|
||||
'contract' => 2,
|
||||
'architecture' => 'aarch64',
|
||||
'machine' => 'pc-i440fx',
|
||||
'firmware' => 'bios',
|
||||
'secure_boot' => true,
|
||||
'disk_format' => 'raw',
|
||||
'system_disk_only' => false,
|
||||
'public_network_contract' => 2,
|
||||
'vpc_inherited' => true,
|
||||
] as $field => $wrong) {
|
||||
$broken = $canonical;
|
||||
$broken[$field] = $wrong;
|
||||
try {
|
||||
CloudPortableRestoreCompatibility::fromPayload($broken);
|
||||
throw new RuntimeException("Wrong {$field} was accepted.");
|
||||
} catch (InvalidArgumentException) {
|
||||
}
|
||||
}
|
||||
$extra = $canonical + ['path' => '/private/root.qcow2'];
|
||||
try {
|
||||
CloudPortableRestoreCompatibility::fromPayload($extra);
|
||||
throw new RuntimeException('An unbounded contract field was accepted.');
|
||||
} catch (InvalidArgumentException) {
|
||||
}
|
||||
|
||||
$reordered = array_reverse($canonical, true);
|
||||
try {
|
||||
CloudPortableRestoreCompatibility::fromStored(
|
||||
json_encode($reordered, JSON_THROW_ON_ERROR),
|
||||
$contract->sha256()
|
||||
);
|
||||
throw new RuntimeException('Non-canonical stored JSON was accepted.');
|
||||
} catch (InvalidArgumentException) {
|
||||
}
|
||||
try {
|
||||
CloudPortableRestoreCompatibility::fromStored(
|
||||
$contract->json(), str_repeat('f', 64)
|
||||
);
|
||||
throw new RuntimeException('A tampered contract digest was accepted.');
|
||||
} catch (InvalidArgumentException) {
|
||||
}
|
||||
|
||||
echo "Portable restore compatibility contract passed.\n";
|
||||
Loading…
Reference in New Issue