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.
196 lines
7.7 KiB
PHP
196 lines
7.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudBackupRestoreAttempt;
|
|
use App\Models\CloudResourceReservation;
|
|
use App\Models\CloudResourceReservationIp;
|
|
use Illuminate\Database\Capsule\Manager as DB;
|
|
|
|
/** Verifies one fully released restore bundle without requiring it to be free. */
|
|
final class CloudReleasedPublicNetworkBundleVerifier
|
|
{
|
|
/** @return array{ipv4:int,ipv6:int} */
|
|
public static function verify(CloudBackupRestoreAttempt $attempt,
|
|
CloudResourceReservation $reservation, int $ownerUserId,
|
|
bool $lock = true): array
|
|
{
|
|
try {
|
|
$plan = CloudPortableRestoreTargetPlan::fromHistoricalStored(
|
|
(string) $attempt->target_plan_json,
|
|
(string) $attempt->target_plan_sha256
|
|
);
|
|
} catch (\InvalidArgumentException) {
|
|
throw new CloudError('恢复尝试的目标计划损坏', 409);
|
|
}
|
|
$target = $plan->toArray()['target'];
|
|
if ((int) $attempt->reservation_id !== (int) $reservation->id
|
|
|| !hash_equals(
|
|
(string) $attempt->attempt_key,
|
|
(string) $reservation->backup_restore_attempt_key
|
|
)
|
|
|| (int) $reservation->host_id !== (int) $attempt->target_host_id
|
|
|| (int) $reservation->host_id !== (int) $target['host_id']
|
|
|| (int) $reservation->resource_pool_id
|
|
!== (int) $target['resource_pool_id']
|
|
|| (int) $reservation->user_id !== $ownerUserId
|
|
|| !hash_equals(
|
|
(string) $reservation->operation_id,
|
|
(string) $attempt->operation_id
|
|
)
|
|
|| (string) $reservation->state !== 'released'
|
|
|| $reservation->released_at === null
|
|
|| $reservation->expires_at !== null
|
|
|| (string) $reservation->release_reason === '') {
|
|
throw new CloudError('恢复尝试仍占用资源或身份不一致', 409);
|
|
}
|
|
|
|
$claimsQuery = CloudResourceReservationIp::where(
|
|
'reservation_id',
|
|
(int) $reservation->id
|
|
)->orderBy('address_family')->orderBy('id');
|
|
if ($lock) {
|
|
$claimsQuery->lockForUpdate();
|
|
}
|
|
$claims = $claimsQuery->get();
|
|
$ids = [];
|
|
$claimsByFamily = [];
|
|
foreach ($claims as $claim) {
|
|
$family = (int) $claim->address_family;
|
|
if (!in_array($family, [4, 6], true)
|
|
|| isset($ids[$family])
|
|
|| (string) $claim->state !== 'released'
|
|
|| !hash_equals(
|
|
(string) $claim->operation_id,
|
|
(string) $attempt->operation_id
|
|
)
|
|
|| (int) $claim->network_attachment_id
|
|
!== (int) $attempt->target_network_attachment_id
|
|
|| $claim->active_claim !== null
|
|
|| $claim->released_at === null
|
|
|| !hash_equals(
|
|
(string) $reservation->release_reason,
|
|
(string) $claim->release_reason
|
|
)) {
|
|
throw new CloudError('恢复尝试的公网声明未完全释放', 409);
|
|
}
|
|
$ids[$family] = (int) $claim->id;
|
|
$claimsByFamily[$family] = $claim;
|
|
}
|
|
if (!isset($ids[4], $ids[6])) {
|
|
throw new CloudError('恢复尝试的公网声明不完整', 409);
|
|
}
|
|
|
|
try {
|
|
$networkValue = json_decode(
|
|
(string) $attempt->target_public_network_json,
|
|
true,
|
|
64,
|
|
JSON_THROW_ON_ERROR
|
|
);
|
|
$network = CloudPublicNetworkSnapshot::fromArray($networkValue);
|
|
$claimIdentity =
|
|
CloudPublicNetworkClaimSetIdentity::fromReleasedLedger(
|
|
(int) $reservation->id,
|
|
$claimsByFamily,
|
|
$network
|
|
);
|
|
} catch (\Throwable) {
|
|
throw new CloudError('恢复尝试的冻结公网身份损坏', 409);
|
|
}
|
|
if (!hash_equals(
|
|
$network->fingerprint(),
|
|
(string) $attempt->target_public_network_sha256
|
|
)
|
|
|| !hash_equals(
|
|
$claimIdentity->digest(),
|
|
(string) $attempt->target_claim_set_sha256
|
|
)) {
|
|
throw new CloudError('恢复尝试的冻结公网身份不一致', 409);
|
|
}
|
|
|
|
$plannedNetwork = $plan->toArray()['public_network'];
|
|
if ((int) $attempt->target_network_attachment_id
|
|
!== (int) $plannedNetwork['network_attachment_id']
|
|
|| (int) $claimsByFamily[4]->public_ip_pool_id
|
|
!== (int) $plannedNetwork['ipv4_pool']['public_ip_pool_id']
|
|
|| (int) $claimsByFamily[6]->public_ip_pool_id
|
|
!== (int) $plannedNetwork['ipv6_pool']['public_ip_pool_id']) {
|
|
throw new CloudError('恢复尝试的公网池身份不一致', 409);
|
|
}
|
|
|
|
$addressQuery = DB::table('cloud_public_ips')->whereIn(
|
|
'id',
|
|
[
|
|
(int) $claimsByFamily[4]->public_ip_id,
|
|
(int) $claimsByFamily[6]->public_ip_id,
|
|
]
|
|
)->orderBy('id');
|
|
if ($lock) {
|
|
$addressQuery->lockForUpdate();
|
|
}
|
|
$addresses = [];
|
|
foreach ($addressQuery->get() as $address) {
|
|
$addresses[(int) $address->id] = $address;
|
|
}
|
|
foreach ([4, 6] as $family) {
|
|
$claim = $claimsByFamily[$family];
|
|
$address = $addresses[(int) $claim->public_ip_id] ?? null;
|
|
if (!is_object($address)
|
|
|| (int) $address->host_id !== (int) $target['host_id']
|
|
|| (int) $address->address_family !== $family
|
|
|| (int) $address->public_ip_pool_id
|
|
!== (int) $claim->public_ip_pool_id
|
|
|| !hash_equals(
|
|
(string) $address->address . '/'
|
|
. (int) $address->prefix_length,
|
|
$network->address($family)
|
|
)
|
|
|| self::retainsOldOwnership(
|
|
$address,
|
|
(int) $reservation->id,
|
|
(int) $claim->id,
|
|
(string) $attempt->operation_id
|
|
)
|
|
|| !self::hasValidCurrentOwnership($address)) {
|
|
throw new CloudError('恢复尝试的公网地址未完整解除归属', 409);
|
|
}
|
|
}
|
|
return ['ipv4' => $ids[4], 'ipv6' => $ids[6]];
|
|
}
|
|
|
|
private static function retainsOldOwnership(object $address,
|
|
int $reservationId, int $claimId, string $operationId): bool
|
|
{
|
|
return (int) ($address->reservation_id ?? 0) === $reservationId
|
|
|| (int) ($address->reservation_ip_claim_id ?? 0) === $claimId
|
|
|| hash_equals(
|
|
$operationId,
|
|
(string) ($address->operation_id ?? '')
|
|
);
|
|
}
|
|
|
|
private static function hasValidCurrentOwnership(object $address): bool
|
|
{
|
|
$state = (string) ($address->state ?? '');
|
|
$reservation = $address->reservation_id;
|
|
$claim = $address->reservation_ip_claim_id;
|
|
$instance = $address->instance_id;
|
|
$operation = $address->operation_id;
|
|
if (in_array($state, ['available', 'disabled'], true)) {
|
|
return $reservation === null && $claim === null
|
|
&& $instance === null && $operation === null;
|
|
}
|
|
if ($reservation === null || $claim === null || $operation === null
|
|
|| preg_match('/^[a-f0-9]{32}$/D', (string) $operation) !== 1) {
|
|
return false;
|
|
}
|
|
return match ($state) {
|
|
'reserved' => $instance === null,
|
|
'assigned' => $instance !== null,
|
|
'changing', 'uncertain' => true,
|
|
default => false,
|
|
};
|
|
}
|
|
}
|