diff --git a/web/app/Services/Cloud/CloudInstanceMigrationService.php b/web/app/Services/Cloud/CloudInstanceMigrationService.php index d49af984..7c230595 100644 --- a/web/app/Services/Cloud/CloudInstanceMigrationService.php +++ b/web/app/Services/Cloud/CloudInstanceMigrationService.php @@ -695,19 +695,9 @@ final class CloudInstanceMigrationService || !hash_equals((string) $backup->sha256, (string) ($result['sha256'] ?? '')) || !hash_equals(strtolower((string) $migration->target_uuid), - strtolower((string) ($vm['uuid'] ?? ''))) - || !hash_equals($requestedIp, - (string) ($vm['public_ip_cidr'] ?? '')) - || !hash_equals( - (string) ($payload['body']['public_pool_key'] ?? ''), - (string) ($vm['public_pool_key'] ?? '') - )) { + strtolower((string) ($vm['uuid'] ?? '')))) { throw new CloudError('Agent 迁移恢复结果与目标资源不一致', 502); } - CloudMigrationBundleService::finishRestoreLocked( - $migration, - $result - ); $rootDiskGib = filter_var( $payload['body']['disk_gib'] ?? null, FILTER_VALIDATE_INT, @@ -739,6 +729,16 @@ final class CloudInstanceMigrationService 409 ); } + $targetSpec = self::finalizeTargetSpec( + self::decodeSpec((string) $instance->spec_json), + is_array($payload['body'] ?? null) ? $payload['body'] : [], + $vm, + (int) $reservation->resource_pool_id + ); + CloudMigrationBundleService::finishRestoreLocked( + $migration, + $result + ); // Snapshot rows now account for their target-host volumes. Keep // only the root disk in the ordinary instance reservation; the // source duplicate remains claimed by restored bundle artifacts @@ -765,8 +765,7 @@ final class CloudInstanceMigrationService 409 ); } - $spec = self::decodeSpec((string) $instance->spec_json); - $spec = CloudInstanceMetadata::merge($spec, $vm); + $spec = $targetSpec; $spec['managed'] = true; $spec['migrated_from_host_id'] = (int) $migration->source_host_id; $spec['migrated_from_uuid'] = (string) $migration->source_uuid; @@ -1396,6 +1395,126 @@ final class CloudInstanceMigrationService return $values; } + /** + * Verify the target Agent exactly applied the dispatched resource/network + * snapshot, then converge every authoritative alias in the business spec. + */ + private static function finalizeTargetSpec(array $source, array $body, + array $vm, int $resourcePoolId): array + { + if ($resourcePoolId < 1) { + throw new CloudRecoveryInstanceStateConflict( + '维护迁移目标资源池快照无效', 409 + ); + } + $expected = self::targetRestoreSnapshot($body); + try { + $actual = self::targetRestoreSnapshot($vm); + } catch (CloudError $error) { + throw new CloudError( + 'Agent 迁移恢复结果与目标资源不一致', 502 + ); + } + if ($expected !== $actual) { + throw new CloudError('Agent 迁移恢复结果与目标资源不一致', 502); + } + $publicIp = explode('/', $expected['public_ip_cidr'], 2)[0]; + $spec = CloudInstanceMetadata::merge($source, $vm); + return array_replace($spec, [ + 'vcpu' => $expected['vcpu'], + 'cpu_cores' => $expected['vcpu'], + 'memory_mib' => $expected['memory_mib'], + 'ram_mib' => $expected['memory_mib'], + 'disk_gib' => $expected['disk_gib'], + 'bandwidth_mbps' => $expected['bandwidth_mbps'], + 'network' => $expected['network'], + 'public_pool_key' => $expected['public_pool_key'], + 'public_pool_cidr' => $expected['public_pool_cidr'], + 'public_ip_cidr' => $expected['public_ip_cidr'], + 'public_ip' => $publicIp, + 'gateway' => $expected['gateway'], + 'dns_servers' => $expected['dns_servers'], + 'resource_pool_id' => $resourcePoolId, + ]); + } + + /** @return array */ + private static function targetRestoreSnapshot(array $value): array + { + $integers = []; + foreach ([ + 'vcpu' => [1, 512], + 'memory_mib' => [128, 16 * 1024 * 1024], + 'disk_gib' => [1, 4096], + 'bandwidth_mbps' => [1, 100000], + ] as $field => [$minimum, $maximum]) { + $parsed = filter_var($value[$field] ?? null, + FILTER_VALIDATE_INT, ['options' => [ + 'min_range' => $minimum, 'max_range' => $maximum, + ]]); + if ($parsed === false) { + throw new CloudRecoveryInstanceStateConflict( + '维护迁移目标资源规格快照无效', 409 + ); + } + $integers[$field] = (int) $parsed; + } + $network = $value['network'] ?? null; + $poolKey = $value['public_pool_key'] ?? null; + $poolCidr = $value['public_pool_cidr'] ?? null; + $publicIpCidr = $value['public_ip_cidr'] ?? null; + $gateway = $value['gateway'] ?? null; + $dns = $value['dns_servers'] ?? null; + if ($network !== 'bridge:br0' + || !is_string($poolKey) + || preg_match('/^[a-f0-9]{32}$/D', $poolKey) !== 1 + || !is_string($poolCidr) + || preg_match('/^[0-9.]+\/(?:[1-9]|[12][0-9]|30)$/D', + $poolCidr) !== 1 + || !is_string($publicIpCidr) + || !is_string($gateway) + || filter_var($gateway, FILTER_VALIDATE_IP, + FILTER_FLAG_IPV4) === false + || !is_array($dns) || !array_is_list($dns) + || $dns === [] || count($dns) > 4) { + throw new CloudRecoveryInstanceStateConflict( + '维护迁移目标网络规格快照无效', 409 + ); + } + try { + $publicIpCidr = CloudInput::publicIpCidr($publicIpCidr); + } catch (CloudError $error) { + throw new CloudRecoveryInstanceStateConflict( + '维护迁移目标网络规格快照无效', 409 + ); + } + $poolParts = explode('/', $poolCidr, 2); + if (filter_var($poolParts[0], FILTER_VALIDATE_IP, + FILTER_FLAG_IPV4) === false + || count(array_unique($dns, SORT_STRING)) !== count($dns)) { + throw new CloudRecoveryInstanceStateConflict( + '维护迁移目标网络规格快照无效', 409 + ); + } + foreach ($dns as $server) { + if (!is_string($server) + || filter_var($server, FILTER_VALIDATE_IP, + FILTER_FLAG_IPV4) === false) { + throw new CloudRecoveryInstanceStateConflict( + '维护迁移目标网络规格快照无效', 409 + ); + } + } + return $integers + [ + 'network' => $network, + 'public_pool_key' => $poolKey, + 'public_pool_cidr' => $poolCidr, + 'public_ip_cidr' => $publicIpCidr, + 'gateway' => $gateway, + 'dns_servers' => array_values($dns), + ]; + } + private static function decodeSpec(string $encoded): array { $spec = json_decode($encoded, true); diff --git a/web/app/Services/Cloud/CloudWorkerService.php b/web/app/Services/Cloud/CloudWorkerService.php index fa201302..4e3077ba 100644 --- a/web/app/Services/Cloud/CloudWorkerService.php +++ b/web/app/Services/Cloud/CloudWorkerService.php @@ -1542,8 +1542,17 @@ final class CloudWorkerService 'maintenance_migration_id' => $payload['maintenance_migration_id'] ?? null, 'body' => [ + 'vcpu' => $body['vcpu'] ?? null, + 'memory_mib' => $body['memory_mib'] ?? null, + 'disk_gib' => $body['disk_gib'] ?? null, + 'bandwidth_mbps' => $body['bandwidth_mbps'] ?? null, + 'network' => $body['network'] ?? null, 'public_ip_cidr' => $body['public_ip_cidr'] ?? null, 'public_pool_key' => $body['public_pool_key'] ?? null, + 'public_pool_cidr' => + $body['public_pool_cidr'] ?? null, + 'gateway' => $body['gateway'] ?? null, + 'dns_servers' => $body['dns_servers'] ?? null, ], ]; } diff --git a/web/tests/CloudMaintenanceMigrationTargetSpecTest.php b/web/tests/CloudMaintenanceMigrationTargetSpecTest.php new file mode 100644 index 00000000..fefc87c3 --- /dev/null +++ b/web/tests/CloudMaintenanceMigrationTargetSpecTest.php @@ -0,0 +1,85 @@ + 'preserved', + 'guest_hostname' => 'retained.example', + 'platform_network_policy_snapshot' => ['version' => 1], + 'vcpu' => 1, 'cpu_cores' => 1, + 'memory_mib' => 1024, 'ram_mib' => 1024, + 'disk_gib' => 20, 'bandwidth_mbps' => 100, + 'network' => 'bridge:br0', + 'public_pool_key' => str_repeat('a', 32), + 'public_pool_cidr' => '198.51.100.0/24', + 'public_ip_cidr' => '198.51.100.10/24', + 'public_ip' => '198.51.100.10', + 'gateway' => '198.51.100.1', + 'dns_servers' => ['1.1.1.1'], +]; +$body = [ + 'vcpu' => 4, 'memory_mib' => 8192, 'disk_gib' => 80, + 'bandwidth_mbps' => 500, 'network' => 'bridge:br0', + 'public_pool_key' => str_repeat('b', 32), + 'public_pool_cidr' => '203.0.113.0/24', + 'public_ip_cidr' => '203.0.113.20/24', + 'gateway' => '203.0.113.1', + 'dns_servers' => ['9.9.9.9', '149.112.112.112'], +]; +$vm = $body + [ + 'uuid' => '22222222-2222-4222-8222-222222222222', + 'managed' => true, +]; + +$finalize = new ReflectionMethod( + CloudInstanceMigrationService::class, 'finalizeTargetSpec' +); +$spec = $finalize->invoke(null, $source, $body, $vm, 202); + +$assert(($spec['billing_marker'] ?? null) === 'preserved' + && ($spec['guest_hostname'] ?? null) === 'retained.example' + && ($spec['platform_network_policy_snapshot'] ?? null) === ['version' => 1] + && ($spec['vcpu'] ?? null) === 4 + && ($spec['cpu_cores'] ?? null) === 4 + && ($spec['memory_mib'] ?? null) === 8192 + && ($spec['ram_mib'] ?? null) === 8192 + && ($spec['disk_gib'] ?? null) === 80 + && ($spec['bandwidth_mbps'] ?? null) === 500 + && ($spec['network'] ?? null) === 'bridge:br0' + && ($spec['public_pool_key'] ?? null) === str_repeat('b', 32) + && ($spec['public_pool_cidr'] ?? null) === '203.0.113.0/24' + && ($spec['public_ip_cidr'] ?? null) === '203.0.113.20/24' + && ($spec['public_ip'] ?? null) === '203.0.113.20' + && ($spec['gateway'] ?? null) === '203.0.113.1' + && ($spec['dns_servers'] ?? null) === ['9.9.9.9', '149.112.112.112'] + && ($spec['resource_pool_id'] ?? null) === 202, + 'Maintenance migration did not converge every target resource and network snapshot field.'); + +foreach ([ + 'vcpu' => 8, + 'bandwidth_mbps' => 1000, + 'public_pool_cidr' => '203.0.114.0/24', + 'gateway' => '203.0.113.2', + 'dns_servers' => ['8.8.8.8'], +] as $field => $different) { + $mismatched = array_replace($vm, [$field => $different]); + $rejected = false; + try { + $finalize->invoke(null, $source, $body, $mismatched, 202); + } catch (CloudError $error) { + $rejected = $error->httpStatus === 502; + } + $assert($rejected, + 'Maintenance migration accepted mismatched Agent field: ' . $field); +} + +echo "Maintenance migration target resource/network snapshot tests passed.\n"; diff --git a/web/tests/CloudPortableBackupRecoveryEnvelopeTest.php b/web/tests/CloudPortableBackupRecoveryEnvelopeTest.php index e043053a..b3abf008 100644 --- a/web/tests/CloudPortableBackupRecoveryEnvelopeTest.php +++ b/web/tests/CloudPortableBackupRecoveryEnvelopeTest.php @@ -68,8 +68,16 @@ $restorePayload = [ 'restore_key' => str_repeat('f', 32), 'reservation_id' => 91, 'body' => [ + 'vcpu' => 4, + 'memory_mib' => 8192, + 'disk_gib' => 80, + 'bandwidth_mbps' => 500, + 'network' => 'bridge:br0', 'public_ip_cidr' => '203.0.113.19/24', 'public_pool_key' => str_repeat('1', 32), + 'public_pool_cidr' => '203.0.113.0/24', + 'gateway' => '203.0.113.1', + 'dns_servers' => ['9.9.9.9', '149.112.112.112'], 'source_url' => $largeUrls[0], 'source_sha256' => str_repeat('2', 64), 'root_password' => 'must-not-survive', @@ -78,8 +86,16 @@ $restorePayload = [ $restoreCompact = $compact->invoke(null, 'backup_restore', $restorePayload); $encodedRestore = json_encode($restoreCompact, JSON_THROW_ON_ERROR); $assert(($restoreCompact['body'] ?? null) === [ + 'vcpu' => 4, + 'memory_mib' => 8192, + 'disk_gib' => 80, + 'bandwidth_mbps' => 500, + 'network' => 'bridge:br0', 'public_ip_cidr' => '203.0.113.19/24', 'public_pool_key' => str_repeat('1', 32), + 'public_pool_cidr' => '203.0.113.0/24', + 'gateway' => '203.0.113.1', + 'dns_servers' => ['9.9.9.9', '149.112.112.112'], ] && !str_contains($encodedRestore, 'source_url') && !str_contains($encodedRestore, 'root_password')