You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
152 lines
5.5 KiB
PHP
152 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudInstanceBackup;
|
|
|
|
/** Secret-free, canonical identity of one ready portable backup object. */
|
|
final class CloudPortableRestoreBackupEvidence
|
|
{
|
|
private const GIB = 1073741824;
|
|
|
|
private function __construct(
|
|
private readonly CloudPortableRestoreCompatibility $compatibility,
|
|
private readonly array $backup,
|
|
private readonly array $resources,
|
|
private readonly CloudPortableRestoreRequestInput $requestInput
|
|
) {
|
|
}
|
|
|
|
public static function fromBackup(CloudInstanceBackup $backup): self
|
|
{
|
|
$contract = $backup->sourcePortableRestoreCompatibility();
|
|
if ((int) $backup->id < 1
|
|
|| preg_match('/^[a-f0-9]{32}$/D',
|
|
(string) $backup->backup_key) !== 1
|
|
|| (int) $backup->source_host_id < 1
|
|
|| (string) $backup->purpose !== 'portable'
|
|
|| (string) $backup->state !== 'ready'
|
|
|| $contract === null
|
|
|| (int) $backup->object_size_bytes < 1
|
|
|| (int) $backup->virtual_size_bytes < 1
|
|
|| preg_match('/^[a-f0-9]{64}$/D',
|
|
(string) $backup->sha256) !== 1) {
|
|
throw new CloudError('备份缺少可信的可移植恢复证据', 409);
|
|
}
|
|
try {
|
|
$storageAuthority =
|
|
CloudBackupStorageAuthority::readyEvidence($backup);
|
|
} catch (\InvalidArgumentException) {
|
|
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);
|
|
}
|
|
$requestInput = CloudPortableRestoreRequestInput::fromSpec($spec);
|
|
$canonicalSpec = CloudCanonicalJson::normalize($spec);
|
|
$sourceSpecSha256 = hash('sha256', json_encode(
|
|
$canonicalSpec,
|
|
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
|
|
| JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_LINE_TERMINATORS
|
|
));
|
|
$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);
|
|
}
|
|
return new self($contract, [
|
|
'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' =>
|
|
$storageAuthority['identity_sha256'],
|
|
'source_contract_sha256' => $contract->sha256(),
|
|
'source_spec_sha256' => $sourceSpecSha256,
|
|
'restore_request_sha256' => $requestInput->sha256(),
|
|
'source_public_network_sha256' =>
|
|
$sourceNetwork->fingerprint(),
|
|
'source_host_id' => (int) $backup->source_host_id,
|
|
'source_line_id' => $lineId,
|
|
'source_storage_revision' =>
|
|
$storageAuthority['source_storage_revision'],
|
|
'virtual_size_bytes' => $virtualSize,
|
|
], [
|
|
'cpu_cores' => $resources['cpu_cores'],
|
|
'memory_mib' => $resources['memory_mib'],
|
|
'disk_gib' => $resources['disk_gib'],
|
|
'bandwidth_mbps' => $resources['bandwidth_mbps'],
|
|
'system_disk_virtual_size_bytes' => $virtualSize,
|
|
], $requestInput);
|
|
}
|
|
|
|
public function compatibility(): CloudPortableRestoreCompatibility
|
|
{
|
|
return $this->compatibility;
|
|
}
|
|
|
|
public function backup(): array
|
|
{
|
|
return $this->backup;
|
|
}
|
|
|
|
public function resources(): array
|
|
{
|
|
return $this->resources;
|
|
}
|
|
|
|
public function capacityResources(): array
|
|
{
|
|
return [
|
|
'cpu_cores' => $this->resources['cpu_cores'],
|
|
'memory_mib' => $this->resources['memory_mib'],
|
|
'disk_gib' => $this->resources['disk_gib'],
|
|
];
|
|
}
|
|
|
|
public function requestInput(): CloudPortableRestoreRequestInput
|
|
{
|
|
return $this->requestInput;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
}
|