|
|
|
|
@ -15,8 +15,6 @@ use App\Models\CloudInstanceBackup;
|
|
|
|
|
*/
|
|
|
|
|
final class CloudPortableRestoreTargetService
|
|
|
|
|
{
|
|
|
|
|
private const GIB = 1073741824;
|
|
|
|
|
|
|
|
|
|
/** @return list<CloudPortableRestoreTargetPlan> */
|
|
|
|
|
public static function candidates(
|
|
|
|
|
CloudInstanceBackup $backup
|
|
|
|
|
@ -42,9 +40,14 @@ final class CloudPortableRestoreTargetService
|
|
|
|
|
private static function candidatesFromSnapshot(
|
|
|
|
|
CloudInstanceBackup $backup
|
|
|
|
|
): array {
|
|
|
|
|
[$sourceContract, $sourceLineId, $resources, $virtualSize,
|
|
|
|
|
$sourceNetworkSha256, $objectIdentitySha256] =
|
|
|
|
|
self::backupContext($backup);
|
|
|
|
|
$backupEvidence = CloudPortableRestoreBackupEvidence::fromBackup(
|
|
|
|
|
$backup
|
|
|
|
|
);
|
|
|
|
|
$sourceContract = $backupEvidence->compatibility();
|
|
|
|
|
$backupIdentity = $backupEvidence->backup();
|
|
|
|
|
$resources = $backupEvidence->resources();
|
|
|
|
|
$sourceLineId = $backupIdentity['source_line_id'];
|
|
|
|
|
$virtualSize = $backupIdentity['virtual_size_bytes'];
|
|
|
|
|
$rows = self::candidateRows();
|
|
|
|
|
if ($rows->isEmpty()) {
|
|
|
|
|
return [];
|
|
|
|
|
@ -76,22 +79,7 @@ final class CloudPortableRestoreTargetService
|
|
|
|
|
}
|
|
|
|
|
$evidence = [
|
|
|
|
|
'contract' => 1,
|
|
|
|
|
'backup' => [
|
|
|
|
|
'id' => (int) $backup->id,
|
|
|
|
|
'backup_key' => (string) $backup->backup_key,
|
|
|
|
|
'object_size_bytes' =>
|
|
|
|
|
(int) $backup->object_size_bytes,
|
|
|
|
|
'object_sha256' => (string) $backup->sha256,
|
|
|
|
|
'object_identity_sha256' => $objectIdentitySha256,
|
|
|
|
|
'source_contract_sha256' => $sourceContract->sha256(),
|
|
|
|
|
'source_public_network_sha256' =>
|
|
|
|
|
$sourceNetworkSha256,
|
|
|
|
|
'source_host_id' => (int) $backup->source_host_id,
|
|
|
|
|
'source_line_id' => $sourceLineId,
|
|
|
|
|
'source_storage_revision' =>
|
|
|
|
|
(int) $backup->storage_revision,
|
|
|
|
|
'virtual_size_bytes' => $virtualSize,
|
|
|
|
|
],
|
|
|
|
|
'backup' => $backupIdentity,
|
|
|
|
|
'target' => [
|
|
|
|
|
'host_id' => $hostId,
|
|
|
|
|
'host_name' => (string) $row->name,
|
|
|
|
|
@ -159,98 +147,6 @@ final class CloudPortableRestoreTargetService
|
|
|
|
|
return $plans;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array{0:CloudPortableRestoreCompatibility,1:int,
|
|
|
|
|
* 2:array{cpu_cores:int,memory_mib:int,disk_gib:int,
|
|
|
|
|
* bandwidth_mbps:int},3:int,4:string,5:string}
|
|
|
|
|
*/
|
|
|
|
|
private static function backupContext(CloudInstanceBackup $backup): array
|
|
|
|
|
{
|
|
|
|
|
$contract = $backup->sourcePortableRestoreCompatibility();
|
|
|
|
|
$objectKey = (string) $backup->object_key;
|
|
|
|
|
$sealedRuntime = (string) $backup->sealed_storage_runtime;
|
|
|
|
|
$objectEtag = (string) $backup->object_etag;
|
|
|
|
|
$objectVersionId = (string) $backup->object_version_id;
|
|
|
|
|
if ((int) $backup->id < 1
|
|
|
|
|
|| preg_match('/^[a-f0-9]{32}$/D',
|
|
|
|
|
(string) $backup->backup_key) !== 1
|
|
|
|
|
|| (int) $backup->source_host_id < 1
|
|
|
|
|
|| !self::validObjectKey($objectKey)
|
|
|
|
|
|| trim($sealedRuntime) === ''
|
|
|
|
|
|| strlen($objectEtag) > 256
|
|
|
|
|
|| preg_match('/[\x00-\x1f\x7f]/D', $objectEtag) === 1
|
|
|
|
|
|| strlen($objectVersionId) > 1024
|
|
|
|
|
|| preg_match('/[\x00-\x1f\x7f]/D', $objectVersionId) === 1
|
|
|
|
|
|| ($objectVersionId !== '' && $objectEtag === '')
|
|
|
|
|
|| (string) $backup->purpose !== 'portable'
|
|
|
|
|
|| (string) $backup->state !== 'ready'
|
|
|
|
|
|| $contract === null
|
|
|
|
|
|| (int) $backup->object_size_bytes < 1
|
|
|
|
|
|| (int) $backup->virtual_size_bytes < 1
|
|
|
|
|
|| (int) $backup->storage_revision < 1
|
|
|
|
|
|| preg_match('/^[a-f0-9]{64}$/D',
|
|
|
|
|
(string) $backup->sha256) !== 1) {
|
|
|
|
|
throw new CloudError('备份缺少可信的可移植恢复证据', 409);
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
$spec = json_decode(
|
|
|
|
|
(string) $backup->spec_json,
|
|
|
|
|
true,
|
|
|
|
|
64,
|
|
|
|
|
JSON_THROW_ON_ERROR
|
|
|
|
|
);
|
|
|
|
|
if (!is_array($spec) || array_is_list($spec)) {
|
|
|
|
|
throw new \InvalidArgumentException();
|
|
|
|
|
}
|
|
|
|
|
$sourceNetwork = CloudPublicNetworkSnapshot::fromArray(
|
|
|
|
|
$spec['public_network'] ?? null
|
|
|
|
|
);
|
|
|
|
|
} catch (\Throwable) {
|
|
|
|
|
throw new CloudError('备份缺少可信的冻结双栈网络快照', 409);
|
|
|
|
|
}
|
|
|
|
|
$lineId = self::positiveInt($spec['line_id'] ?? null);
|
|
|
|
|
$resources = [
|
|
|
|
|
'cpu_cores' => self::positiveInt(
|
|
|
|
|
$spec['vcpu'] ?? $spec['cpu_cores'] ?? null
|
|
|
|
|
),
|
|
|
|
|
'memory_mib' => self::positiveInt(
|
|
|
|
|
$spec['memory_mib'] ?? $spec['ram_mib'] ?? null
|
|
|
|
|
),
|
|
|
|
|
'disk_gib' => self::positiveInt($spec['disk_gib'] ?? null),
|
|
|
|
|
'bandwidth_mbps' => self::positiveInt(
|
|
|
|
|
$spec['bandwidth_mbps'] ?? null
|
|
|
|
|
),
|
|
|
|
|
];
|
|
|
|
|
if ($lineId === null || in_array(null, $resources, true)
|
|
|
|
|
|| $resources['disk_gib'] > intdiv(PHP_INT_MAX, self::GIB)) {
|
|
|
|
|
throw new CloudError('备份缺少可信的资源与商业线路快照', 409);
|
|
|
|
|
}
|
|
|
|
|
$virtualSize = (int) $backup->virtual_size_bytes;
|
|
|
|
|
if ($virtualSize > $resources['disk_gib'] * self::GIB) {
|
|
|
|
|
throw new CloudError('备份系统盘容量超过冻结规格', 409);
|
|
|
|
|
}
|
|
|
|
|
$objectIdentitySha256 = hash('sha256', json_encode([
|
|
|
|
|
'object_key' => $objectKey,
|
|
|
|
|
'object_size_bytes' => (int) $backup->object_size_bytes,
|
|
|
|
|
'object_sha256' => (string) $backup->sha256,
|
|
|
|
|
'object_etag' => $objectEtag,
|
|
|
|
|
'object_version_id' => $objectVersionId,
|
|
|
|
|
'storage_revision' => (int) $backup->storage_revision,
|
|
|
|
|
'sealed_storage_runtime_sha256' => hash(
|
|
|
|
|
'sha256',
|
|
|
|
|
$sealedRuntime
|
|
|
|
|
),
|
|
|
|
|
], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES));
|
|
|
|
|
return [
|
|
|
|
|
$contract,
|
|
|
|
|
$lineId,
|
|
|
|
|
$resources,
|
|
|
|
|
$virtualSize,
|
|
|
|
|
$sourceNetwork->fingerprint(),
|
|
|
|
|
$objectIdentitySha256,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function candidateRows()
|
|
|
|
|
{
|
|
|
|
|
return CloudHost::query()->join(
|
|
|
|
|
@ -433,24 +329,4 @@ final class CloudPortableRestoreTargetService
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function positiveInt(mixed $value): ?int
|
|
|
|
|
{
|
|
|
|
|
$normalized = filter_var(
|
|
|
|
|
$value,
|
|
|
|
|
FILTER_VALIDATE_INT,
|
|
|
|
|
['options' => ['min_range' => 1, 'max_range' => PHP_INT_MAX]]
|
|
|
|
|
);
|
|
|
|
|
return $normalized === false ? null : (int) $normalized;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function validObjectKey(string $objectKey): bool
|
|
|
|
|
{
|
|
|
|
|
return $objectKey !== ''
|
|
|
|
|
&& strlen($objectKey) <= 512
|
|
|
|
|
&& !str_starts_with($objectKey, '/')
|
|
|
|
|
&& !str_ends_with($objectKey, '/')
|
|
|
|
|
&& !str_contains($objectKey, '\\')
|
|
|
|
|
&& preg_match('/[\x00-\x1f\x7f]/D', $objectKey) !== 1
|
|
|
|
|
&& !in_array('', explode('/', $objectKey), true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|