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.
374 lines
14 KiB
PHP
374 lines
14 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudBackupRestore;
|
|
use App\Models\CloudBackupRestoreAttempt;
|
|
use App\Models\CloudInstanceBackup;
|
|
use App\Models\CloudJob;
|
|
|
|
/**
|
|
* Stable public admission seam for one independent portable restore.
|
|
*
|
|
* Target discovery, atomic resource admission and job publication deliberately
|
|
* remain separate transactions. A replay first uses the already frozen plan,
|
|
* so later scheduler or capacity changes cannot silently retarget it.
|
|
*/
|
|
final class CloudPortableRestoreRequestService
|
|
{
|
|
/** @return array{restore:CloudBackupRestore,job:CloudJob} */
|
|
public static function request(CloudInstanceBackup $backup, array $actor,
|
|
array $input): array
|
|
{
|
|
CloudAccess::manage($actor);
|
|
CloudInput::only($input, [
|
|
'target_host_id', 'target_resource_pool_id',
|
|
'target_plan_sha256', 'name', 'confirm_new_instance',
|
|
]);
|
|
$requesterId = filter_var(
|
|
$actor['user_id'] ?? null,
|
|
FILTER_VALIDATE_INT,
|
|
['options' => ['min_range' => 1]]
|
|
);
|
|
$requestKey = $actor['request_idempotency_key'] ?? null;
|
|
$hostId = filter_var(
|
|
$input['target_host_id'] ?? null,
|
|
FILTER_VALIDATE_INT,
|
|
['options' => ['min_range' => 1]]
|
|
);
|
|
$resourcePoolId = filter_var(
|
|
$input['target_resource_pool_id'] ?? null,
|
|
FILTER_VALIDATE_INT,
|
|
['options' => ['min_range' => 1]]
|
|
);
|
|
$planSha256 = $input['target_plan_sha256'] ?? null;
|
|
if ($requesterId === false) {
|
|
throw new CloudError('恢复请求用户身份无效', 403);
|
|
}
|
|
if (!is_string($requestKey)
|
|
|| preg_match('/^[A-Za-z0-9_.-]{16,128}$/D', $requestKey) !== 1) {
|
|
throw new CloudError('恢复请求必须提供有效的幂等标识', 400);
|
|
}
|
|
if ($hostId === false) {
|
|
throw new CloudError('请选择目标宿主机');
|
|
}
|
|
if ($resourcePoolId === false) {
|
|
throw new CloudError('请选择目标资源池');
|
|
}
|
|
if (!is_string($planSha256)
|
|
|| preg_match('/^[a-f0-9]{64}$/D', $planSha256) !== 1) {
|
|
throw new CloudError('请选择精确的恢复目标计划', 409);
|
|
}
|
|
if (($input['confirm_new_instance'] ?? false) !== true) {
|
|
throw new CloudError('请确认恢复会创建免计费的独立新实例');
|
|
}
|
|
$targetName = CloudInput::name($input['name'] ?? null);
|
|
$plan = self::plan(
|
|
$backup,
|
|
(int) $requesterId,
|
|
$requestKey,
|
|
(int) $hostId,
|
|
(int) $resourcePoolId,
|
|
$planSha256,
|
|
$targetName
|
|
);
|
|
$reserved = CloudPortableRestoreAttemptCoordinator::reserve(
|
|
$actor,
|
|
$backup,
|
|
$plan,
|
|
$planSha256,
|
|
$targetName,
|
|
$requestKey
|
|
)->toArray();
|
|
$attemptKey = $reserved['attempt_key'] ?? null;
|
|
if (!is_string($attemptKey)
|
|
|| preg_match('/^[a-f0-9]{32}$/D', $attemptKey) !== 1) {
|
|
throw new CloudError('恢复预留回执无效,必须人工核查', 503);
|
|
}
|
|
$attempt = CloudBackupRestoreAttempt::where(
|
|
'attempt_key',
|
|
$attemptKey
|
|
)->first();
|
|
if (!$attempt instanceof CloudBackupRestoreAttempt
|
|
|| (int) $attempt->backup_restore_id
|
|
!== (int) ($reserved['restore_id'] ?? 0)) {
|
|
throw new CloudError('恢复预留账本不完整,必须人工核查', 503);
|
|
}
|
|
if ($attempt->job_id === null
|
|
|| (string) $attempt->state === 'queued') {
|
|
CloudPortableRestoreJobDispatcher::queue(
|
|
$actor,
|
|
$attemptKey
|
|
);
|
|
}
|
|
return self::result(
|
|
(int) $backup->id,
|
|
(int) $requesterId,
|
|
$attemptKey
|
|
);
|
|
}
|
|
|
|
private static function plan(CloudInstanceBackup $backup,
|
|
int $requesterId, string $requestKey, int $hostId,
|
|
int $resourcePoolId, string $planSha256,
|
|
string $targetName): CloudPortableRestoreTargetPlan
|
|
{
|
|
$existing = CloudBackupRestore::where(
|
|
'requested_by_user_id',
|
|
$requesterId
|
|
)->where('request_idempotency_key', $requestKey)->first();
|
|
if ($existing instanceof CloudBackupRestore) {
|
|
try {
|
|
$plan = CloudPortableRestoreTargetPlan::fromStored(
|
|
(string) $existing->selected_plan_json,
|
|
(string) $existing->selected_plan_sha256
|
|
);
|
|
} catch (\InvalidArgumentException) {
|
|
throw new CloudError('恢复请求保存的目标计划损坏', 409);
|
|
}
|
|
$target = $plan->toArray()['target'];
|
|
if ((int) $existing->backup_id !== (int) $backup->id
|
|
|| !hash_equals($plan->sha256(), $planSha256)
|
|
|| (int) $target['host_id'] !== $hostId
|
|
|| (int) $target['resource_pool_id'] !== $resourcePoolId
|
|
|| !hash_equals((string) $existing->new_name, $targetName)) {
|
|
throw new CloudError('恢复请求幂等身份冲突', 409);
|
|
}
|
|
return $plan;
|
|
}
|
|
|
|
$matches = [];
|
|
foreach (CloudPortableRestoreTargetService::candidates($backup)
|
|
as $candidate) {
|
|
$target = $candidate->toArray()['target'];
|
|
if ((int) $target['host_id'] === $hostId
|
|
&& (int) $target['resource_pool_id'] === $resourcePoolId
|
|
&& hash_equals($candidate->sha256(), $planSha256)) {
|
|
$matches[] = $candidate;
|
|
}
|
|
}
|
|
if (count($matches) !== 1) {
|
|
throw new CloudError('恢复目标计划已过期,请刷新后重新选择', 409);
|
|
}
|
|
return $matches[0];
|
|
}
|
|
|
|
/** @return array{restore:CloudBackupRestore,job:CloudJob} */
|
|
private static function result(int $backupId, int $requesterId,
|
|
string $attemptKey): array
|
|
{
|
|
return (new CloudJob())->getConnection()->transaction(
|
|
static function () use (
|
|
$backupId,
|
|
$requesterId,
|
|
$attemptKey
|
|
): array {
|
|
$attempt = CloudBackupRestoreAttempt::where(
|
|
'attempt_key',
|
|
$attemptKey
|
|
)->first();
|
|
$restore = $attempt instanceof CloudBackupRestoreAttempt
|
|
? CloudBackupRestore::find(
|
|
(int) $attempt->backup_restore_id
|
|
) : null;
|
|
$backup = $restore instanceof CloudBackupRestore
|
|
? CloudInstanceBackup::find((int) $restore->backup_id)
|
|
: null;
|
|
$job = $attempt instanceof CloudBackupRestoreAttempt
|
|
&& $attempt->job_id !== null
|
|
? CloudJob::find((int) $attempt->job_id) : null;
|
|
self::assertRows(
|
|
$backup,
|
|
$restore,
|
|
$attempt,
|
|
$job,
|
|
$backupId,
|
|
$requesterId
|
|
);
|
|
self::assertEnvelope($backup, $restore, $attempt, $job);
|
|
return ['restore' => $restore, 'job' => $job];
|
|
},
|
|
1
|
|
);
|
|
}
|
|
|
|
private static function assertRows(?CloudInstanceBackup $backup,
|
|
?CloudBackupRestore $restore,
|
|
?CloudBackupRestoreAttempt $attempt, ?CloudJob $job,
|
|
int $backupId, int $requesterId): void
|
|
{
|
|
$targetInstanceId = $restore?->target_instance_id === null
|
|
? null : (int) $restore->target_instance_id;
|
|
$jobInstanceId = $job?->instance_id === null
|
|
? null : (int) $job->instance_id;
|
|
if (!$backup instanceof CloudInstanceBackup
|
|
|| !$restore instanceof CloudBackupRestore
|
|
|| !$attempt instanceof CloudBackupRestoreAttempt
|
|
|| !$job instanceof CloudJob
|
|
|| (int) $backup->id !== $backupId
|
|
|| (int) $restore->backup_id !== $backupId
|
|
|| (int) $restore->requested_by_user_id !== $requesterId
|
|
|| (int) $attempt->backup_restore_id !== (int) $restore->id
|
|
|| (int) $attempt->job_id !== (int) $job->id
|
|
|| (int) $job->backup_id !== $backupId
|
|
|| (int) $job->backup_restore_id !== (int) $restore->id
|
|
|| (int) $job->user_id !== (int) $backup->user_id
|
|
|| (int) $job->host_id !== (int) $attempt->target_host_id
|
|
|| $targetInstanceId !== $jobInstanceId
|
|
|| (string) $job->kind !== 'backup_restore'
|
|
|| (string) $job->execution_mode !== 'agent'
|
|
|| !hash_equals(
|
|
(string) $job->operation_id,
|
|
(string) $attempt->operation_id
|
|
)
|
|
|| !hash_equals(
|
|
(string) $job->request_idempotency_key,
|
|
(string) $attempt->attempt_key
|
|
)
|
|
|| ((int) $attempt->generation === 1
|
|
&& (int) $restore->job_id !== (int) $job->id)) {
|
|
throw new CloudError('恢复任务账本不完整,必须人工核查', 503);
|
|
}
|
|
}
|
|
|
|
private static function assertEnvelope(CloudInstanceBackup $backup,
|
|
CloudBackupRestore $restore, CloudBackupRestoreAttempt $attempt,
|
|
CloudJob $job): void
|
|
{
|
|
try {
|
|
$payload = CloudSecrets::decrypt(
|
|
(string) $job->sealed_payload,
|
|
'job-payload'
|
|
);
|
|
} catch (\Throwable) {
|
|
throw new CloudError('恢复任务冻结信封无法读取', 409);
|
|
}
|
|
if (!is_array($payload) || array_is_list($payload)) {
|
|
throw new CloudError('恢复任务冻结信封结构无效', 409);
|
|
}
|
|
$state = (string) $attempt->state;
|
|
if ($state === 'queued') {
|
|
CloudPortableRestoreJobEnvelope::fromJobForDispatch(
|
|
$job,
|
|
$payload
|
|
);
|
|
return;
|
|
}
|
|
if (in_array($state, [
|
|
'awaiting_receipt', 'effect_unknown', 'finalization_pending',
|
|
], true)) {
|
|
CloudPortableRestoreJobEnvelope::fromJobForRecovery(
|
|
$job,
|
|
$payload
|
|
);
|
|
return;
|
|
}
|
|
self::assertTerminalEnvelope(
|
|
$backup,
|
|
$restore,
|
|
$attempt,
|
|
$job,
|
|
$payload
|
|
);
|
|
}
|
|
|
|
private static function assertTerminalEnvelope(
|
|
CloudInstanceBackup $backup, CloudBackupRestore $restore,
|
|
CloudBackupRestoreAttempt $attempt, CloudJob $job,
|
|
array $payload): void
|
|
{
|
|
$body = $payload['body'] ?? null;
|
|
$state = (string) $attempt->state;
|
|
$terminalShape = $state === 'succeeded'
|
|
? (string) $attempt->effect_resolution === 'applied'
|
|
&& (string) $job->phase === 'terminal'
|
|
&& (string) $job->outcome === 'succeeded'
|
|
&& (string) $job->effect_resolution === 'applied'
|
|
&& $attempt->terminal_at !== null
|
|
&& $job->terminal_at !== null
|
|
&& $restore->target_instance_id !== null
|
|
: ($state === 'failed'
|
|
&& (string) $attempt->effect_resolution === 'not_applied'
|
|
&& (string) $job->phase === 'terminal'
|
|
&& (string) $job->outcome === 'failed'
|
|
&& (string) $job->effect_resolution === 'not_applied'
|
|
&& $attempt->terminal_at !== null
|
|
&& $job->terminal_at !== null
|
|
&& $restore->target_instance_id === null);
|
|
if (!$terminalShape || !is_array($body) || array_is_list($body)
|
|
|| self::containsCapability($payload)
|
|
|| (int) ($payload['owner_id'] ?? 0) !== (int) $backup->user_id
|
|
|| (int) ($payload['reservation_id'] ?? 0)
|
|
!== (int) $attempt->reservation_id
|
|
|| (int) ($payload['generation'] ?? 0)
|
|
!== (int) $attempt->generation
|
|
|| !hash_equals(
|
|
(string) ($payload['backup_key'] ?? ''),
|
|
(string) $backup->backup_key
|
|
)
|
|
|| !hash_equals(
|
|
(string) ($payload['restore_key'] ?? ''),
|
|
(string) $restore->restore_key
|
|
)
|
|
|| !hash_equals(
|
|
(string) ($payload['attempt_key'] ?? ''),
|
|
(string) $attempt->attempt_key
|
|
)
|
|
|| !hash_equals(
|
|
(string) ($payload['target_plan_sha256'] ?? ''),
|
|
(string) $attempt->target_plan_sha256
|
|
)
|
|
|| !hash_equals(
|
|
(string) ($payload['target_identity_sha256'] ?? ''),
|
|
(string) $attempt->target_identity_sha256
|
|
)
|
|
|| !hash_equals(
|
|
(string) ($payload['target_claim_set_sha256'] ?? ''),
|
|
(string) $attempt->target_claim_set_sha256
|
|
)
|
|
|| !hash_equals(
|
|
(string) ($payload['target_public_network_sha256'] ?? ''),
|
|
(string) $attempt->target_public_network_sha256
|
|
)
|
|
|| !hash_equals(
|
|
(string) ($body['operation_id'] ?? ''),
|
|
(string) $attempt->operation_id
|
|
)
|
|
|| !hash_equals(
|
|
strtolower((string) ($body['uuid'] ?? '')),
|
|
strtolower((string) $attempt->target_uuid)
|
|
)
|
|
|| !hash_equals(
|
|
(string) ($body['name'] ?? ''),
|
|
(string) $attempt->target_name
|
|
)
|
|
|| !hash_equals(
|
|
(string) ($body['backup_key'] ?? ''),
|
|
(string) $backup->backup_key
|
|
)) {
|
|
throw new CloudError('恢复任务终态账本不一致', 409);
|
|
}
|
|
}
|
|
|
|
private static function containsCapability(array $value): bool
|
|
{
|
|
$forbidden = [
|
|
'source_url' => true,
|
|
'object_key' => true,
|
|
'sealed_storage_runtime' => true,
|
|
'access_key_id' => true,
|
|
'secret_access_key' => true,
|
|
'credentials' => true,
|
|
];
|
|
foreach ($value as $key => $item) {
|
|
if (is_string($key) && isset($forbidden[$key])) {
|
|
return true;
|
|
}
|
|
if (is_array($item) && self::containsCapability($item)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|