From 7d7d1bf5bc3ff33a7ea2468200757eb4b9b73a46 Mon Sep 17 00:00:00 2001 From: codex Date: Sun, 25 Oct 2026 18:00:34 +0800 Subject: [PATCH] fix(cloud): validate DNS for backup restore placement --- .../Services/Cloud/CloudPlacementService.php | 12 +- ...CloudPortableBackupSameHostRestoreTest.php | 113 +++++++++++++++++- 2 files changed, 121 insertions(+), 4 deletions(-) diff --git a/web/app/Services/Cloud/CloudPlacementService.php b/web/app/Services/Cloud/CloudPlacementService.php index 7cb31127..28d8e585 100644 --- a/web/app/Services/Cloud/CloudPlacementService.php +++ b/web/app/Services/Cloud/CloudPlacementService.php @@ -1176,9 +1176,16 @@ final class CloudPlacementService $payload['body']['public_ip_cidr'] = (string) $ip->address . '/' . (int) $ip->prefix_length; $payload['body']['gateway'] = (string) $publicPool->gateway; + $payload['body']['dns_servers'] = self::poolDnsServers($publicPool); + return $payload; + } + + /** Return the validated DNS snapshot frozen on one public address pool. */ + private static function poolDnsServers(CloudPublicIpPool $pool): array + { try { $dnsServers = json_decode( - (string) $publicPool->dns_servers_json, + (string) $pool->dns_servers_json, true, 16, JSON_THROW_ON_ERROR @@ -1198,8 +1205,7 @@ final class CloudPlacementService )) !== count($dnsServers)) { throw new CloudError('公网地址池 DNS 配置损坏', 409); } - $payload['body']['dns_servers'] = array_values($dnsServers); - return $payload; + return array_values($dnsServers); } private static function assertOrderOffer(CloudOrder $order, CloudOffer $offer, diff --git a/web/tests/CloudPortableBackupSameHostRestoreTest.php b/web/tests/CloudPortableBackupSameHostRestoreTest.php index b89ca614..72e256e5 100644 --- a/web/tests/CloudPortableBackupSameHostRestoreTest.php +++ b/web/tests/CloudPortableBackupSameHostRestoreTest.php @@ -2,6 +2,8 @@ require dirname(__DIR__) . '/vendor/autoload.php'; +use App\Models\CloudBackupRestore; +use App\Services\Cloud\CloudError; use App\Services\Cloud\CloudPlacementService; use Illuminate\Database\Capsule\Manager as Capsule; use Illuminate\Database\Schema\Blueprint; @@ -29,6 +31,7 @@ $schema->create('cloud_hosts', static function (Blueprint $table): void { $schema->create('cloud_regions', static function (Blueprint $table): void { $table->increments('id'); $table->string('name'); + $table->string('country_code'); $table->string('status'); }); $schema->create('cloud_network_lines', static function (Blueprint $table): void { @@ -93,6 +96,11 @@ $schema->create('cloud_public_ip_pools', $table->integer('observed_revision'); $table->integer('desired_revision'); $table->integer('guest_prefix_length'); + $table->string('pool_key'); + $table->string('cidr'); + $table->string('gateway'); + $table->text('dns_servers_json'); + $table->text('reserved_ips_json'); }); $schema->create('cloud_public_ips', static function (Blueprint $table): void { $table->increments('id'); @@ -104,16 +112,40 @@ $schema->create('cloud_public_ips', static function (Blueprint $table): void { $table->integer('reservation_id')->nullable(); $table->integer('instance_id')->nullable(); $table->string('operation_id')->nullable(); + $table->integer('created_at'); + $table->integer('updated_at'); }); $schema->create('cloud_resource_reservations', static function (Blueprint $table): void { $table->increments('id'); + $table->string('reservation_key'); + $table->string('operation_id'); + $table->integer('order_id')->nullable(); + $table->integer('backup_restore_id')->nullable(); + $table->integer('offer_id')->nullable(); + $table->integer('resource_pool_id'); $table->integer('host_id'); + $table->integer('user_id'); $table->string('state'); $table->integer('instance_id')->nullable(); + $table->integer('network_attachment_id')->nullable(); + $table->integer('public_ip_pool_id')->nullable(); + $table->integer('public_ip_id')->nullable(); $table->integer('cpu_cores'); $table->integer('memory_mib'); $table->integer('disk_gib'); + $table->integer('expires_at')->nullable(); + $table->integer('released_at')->nullable(); + $table->string('release_reason'); + $table->integer('created_at'); + $table->integer('updated_at'); + }); +$schema->create('cloud_backup_restores', + static function (Blueprint $table): void { + $table->increments('id'); + $table->integer('target_host_id'); + $table->integer('reservation_id')->nullable(); + $table->string('operation_id'); }); $schema->create('cloud_instances', static function (Blueprint $table): void { $table->increments('id'); @@ -123,7 +155,8 @@ $schema->create('cloud_instances', static function (Blueprint $table): void { }); $capsule->table('cloud_regions')->insert([ - 'id' => 1, 'name' => 'Hong Kong', 'status' => 'active', + 'id' => 1, 'name' => 'Hong Kong', 'country_code' => 'HK', + 'status' => 'active', ]); $capsule->table('cloud_network_lines')->insert([ 'id' => 1, 'region_id' => 1, 'status' => 'active', @@ -171,12 +204,18 @@ foreach ([1 => 'source-host', 2 => 'other-host'] as $hostId => $name) { 'network_attachment_id' => $attachmentId, 'state' => 'active', 'sync_state' => 'applied', 'observed_revision' => 1, 'desired_revision' => 1, 'guest_prefix_length' => 24, + 'pool_key' => str_repeat((string) $hostId, 32), + 'cidr' => '203.0.' . $hostId . '.0/24', + 'gateway' => '203.0.' . $hostId . '.1', + 'dns_servers_json' => '["1.1.1.1","8.8.8.8"]', + 'reserved_ips_json' => '[]', ]); $capsule->table('cloud_public_ips')->insert([ 'host_id' => $hostId, 'public_ip_pool_id' => $poolId, 'address' => '203.0.' . $hostId . '.20', 'prefix_length' => 24, 'state' => 'available', 'reservation_id' => null, 'instance_id' => null, 'operation_id' => null, + 'created_at' => 1700000000, 'updated_at' => 1700000000, ]); } @@ -207,4 +246,76 @@ $assert(isset($byId[2]) $assert((int) $byId[1]['available_ip_count'] === 1, 'The original host did not retain its independent IP capacity check.'); +$restore = new CloudBackupRestore(); +$restore->target_host_id = 1; +$restore->reservation_id = null; +$restore->operation_id = str_repeat('a', 32); +$restore->save(); + +$placement = CloudPlacementService::reserveBackupRestore($restore, 77, [ + 'cpu_cores' => 2, 'memory_mib' => 2048, 'disk_gib' => 20, +]); +$reservation = $placement['reservation']; +$publicIp = $placement['public_ip']; +$assert($placement['network']['dns_servers'] === ['1.1.1.1', '8.8.8.8'], + 'A valid pool DNS list could not reach the restore placement payload.'); +$assert((int) $reservation->backup_restore_id === (int) $restore->id + && (int) $reservation->public_ip_id === (int) $publicIp->id + && (string) $reservation->state === 'held' + && (string) $publicIp->state === 'reserved' + && (int) $publicIp->reservation_id === (int) $reservation->id, + 'Restore placement did not atomically reserve capacity and one public IP.'); + +// Maintenance migration consumes the exact pre-reserved network rather than +// scheduling another address after the source VM has already stopped. +$restore->reservation_id = (int) $reservation->id; +$restore->save(); +$reservation->instance_id = 99; +$reservation->save(); +$prepared = CloudPlacementService::preparedBackupRestorePlacement( + $restore->fresh(), + 99 +); +$assert($prepared['network']['dns_servers'] === ['1.1.1.1', '8.8.8.8'] + && (int) $prepared['reservation']->id === (int) $reservation->id, + 'Maintenance migration did not preserve the pre-reserved pool DNS data.'); + +$capsule->table('cloud_public_ip_pools')->where('id', 1)->update([ + 'dns_servers_json' => '["not-an-ip"]', +]); +try { + CloudPlacementService::preparedBackupRestorePlacement( + $restore->fresh(), + 99 + ); + throw new RuntimeException('A malformed prepared DNS list was accepted.'); +} catch (CloudError $error) { + $assert($error->httpStatus === 409 + && $error->getMessage() === '公网地址池 DNS 配置损坏', + 'Malformed prepared DNS did not return the bounded placement error.'); +} + +$capsule->table('cloud_public_ip_pools')->where('id', 2)->update([ + 'dns_servers_json' => '["not-an-ip"]', +]); +$invalidRestore = new CloudBackupRestore(); +$invalidRestore->target_host_id = 2; +$invalidRestore->reservation_id = null; +$invalidRestore->operation_id = str_repeat('b', 32); +$invalidRestore->save(); +try { + CloudPlacementService::reserveBackupRestore($invalidRestore, 77, [ + 'cpu_cores' => 2, 'memory_mib' => 2048, 'disk_gib' => 20, + ]); + throw new RuntimeException('A malformed pool DNS list was accepted.'); +} catch (CloudError $error) { + $assert($error->httpStatus === 409 + && $error->getMessage() === '目标宿主机没有可分配的公网 IP', + 'Malformed pool DNS was not excluded from target allocation.'); +} +$assert($capsule->table('cloud_resource_reservations')->count() === 1 + && $capsule->table('cloud_public_ips')->where('host_id', 2) + ->value('state') === 'available', + 'Malformed pool DNS consumed restore capacity or a public IP.'); + echo "Portable backup same-host restore target behavior passed.\n";