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.
150 lines
6.5 KiB
PHP
150 lines
6.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
/**
|
|
* Immutable identity for an effective IPv4+IPv6 pair.
|
|
*
|
|
* Unlike CloudActivePublicNetworkClaimSet, a transition target may combine
|
|
* one current active source claim with one fenced inactive target claim.
|
|
*/
|
|
final class CloudPublicNetworkClaimSetIdentity
|
|
{
|
|
private function __construct(private readonly string $digest)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @param array<int,object> $claims keyed by address family
|
|
* @param array<int,object> $addresses keyed by public IP id
|
|
*/
|
|
public static function fromLedger(int $reservationId, array $claims,
|
|
array $addresses, CloudPublicNetworkSnapshot $network): self
|
|
{
|
|
if ($reservationId < 1 || count($claims) !== 2) {
|
|
throw new CloudError('公网网络声明集合不完整', 409);
|
|
}
|
|
$canonical = [];
|
|
$attachmentId = null;
|
|
foreach ([4, 6] as $family) {
|
|
$claim = $claims[$family] ?? null;
|
|
$claimId = is_object($claim) ? (int) ($claim->id ?? 0) : 0;
|
|
$publicIpId = is_object($claim)
|
|
? (int) ($claim->public_ip_id ?? 0) : 0;
|
|
$ip = $addresses[$publicIpId] ?? null;
|
|
$operationId = is_object($claim)
|
|
? (string) ($claim->operation_id ?? '') : '';
|
|
$currentAttachment = is_object($claim)
|
|
? (int) ($claim->network_attachment_id ?? 0) : 0;
|
|
if (!is_object($claim) || !is_object($ip)
|
|
|| $claimId < 1 || $publicIpId < 1
|
|
|| (int) ($claim->reservation_id ?? 0) !== $reservationId
|
|
|| (int) ($claim->address_family ?? 0) !== $family
|
|
|| (int) ($claim->claim_generation ?? 0) < 1
|
|
|| !in_array($claim->active_claim, [null, 1, '1'], true)
|
|
|| !in_array((string) ($claim->state ?? ''),
|
|
['reserved', 'committed', 'uncertain'], true)
|
|
|| preg_match('/^[a-f0-9]{32}$/D', $operationId) !== 1
|
|
|| $currentAttachment < 1
|
|
|| (int) ($claim->public_ip_pool_id ?? 0) < 1
|
|
|| (int) ($ip->id ?? 0) !== $publicIpId
|
|
|| (int) ($ip->reservation_id ?? 0) !== $reservationId
|
|
|| (int) ($ip->reservation_ip_claim_id ?? 0) !== $claimId
|
|
|| (int) ($ip->address_family ?? 0) !== $family
|
|
|| (int) ($ip->public_ip_pool_id ?? 0)
|
|
!== (int) $claim->public_ip_pool_id
|
|
|| !hash_equals($operationId,
|
|
(string) ($ip->operation_id ?? ''))
|
|
|| !hash_equals(
|
|
(string) $ip->address . '/' . (int) $ip->prefix_length,
|
|
$network->address($family)
|
|
)) {
|
|
throw new CloudError('公网网络声明集合关联不一致', 409);
|
|
}
|
|
if ($attachmentId === null) {
|
|
$attachmentId = $currentAttachment;
|
|
} elseif ($attachmentId !== $currentAttachment) {
|
|
throw new CloudError('公网网络声明不属于同一接入', 409);
|
|
}
|
|
$canonical[] = [
|
|
'family' => $family,
|
|
'claim_id' => $claimId,
|
|
'claim_generation' => (int) $claim->claim_generation,
|
|
'claim_operation_id' => $operationId,
|
|
'public_ip_id' => $publicIpId,
|
|
'network_attachment_id' => $currentAttachment,
|
|
'public_ip_pool_id' => (int) $claim->public_ip_pool_id,
|
|
];
|
|
}
|
|
return new self(hash('sha256', json_encode([
|
|
'reservation_id' => $reservationId,
|
|
'claims' => $canonical,
|
|
'public_network_sha256' => $network->fingerprint(),
|
|
], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES)));
|
|
}
|
|
|
|
/**
|
|
* Rebuild immutable identity after addresses have been safely released.
|
|
*
|
|
* Current address rows may already belong to another reservation, so the
|
|
* append-only claim history and the attempt's frozen network are the only
|
|
* valid terminal evidence.
|
|
*
|
|
* @param array<int,object> $claims keyed by address family
|
|
*/
|
|
public static function fromReleasedLedger(int $reservationId,
|
|
array $claims, CloudPublicNetworkSnapshot $network): self
|
|
{
|
|
if ($reservationId < 1 || count($claims) !== 2) {
|
|
throw new CloudError('已释放公网网络声明集合不完整', 409);
|
|
}
|
|
$canonical = [];
|
|
$attachmentId = null;
|
|
foreach ([4, 6] as $family) {
|
|
$claim = $claims[$family] ?? null;
|
|
$currentAttachment = is_object($claim)
|
|
? (int) ($claim->network_attachment_id ?? 0) : 0;
|
|
$operationId = is_object($claim)
|
|
? (string) ($claim->operation_id ?? '') : '';
|
|
if (!is_object($claim)
|
|
|| (int) ($claim->id ?? 0) < 1
|
|
|| (int) ($claim->reservation_id ?? 0) !== $reservationId
|
|
|| (int) ($claim->address_family ?? 0) !== $family
|
|
|| (int) ($claim->claim_generation ?? 0) < 1
|
|
|| $claim->active_claim !== null
|
|
|| (string) ($claim->state ?? '') !== 'released'
|
|
|| ($claim->released_at ?? null) === null
|
|
|| preg_match('/^[a-f0-9]{32}$/D', $operationId) !== 1
|
|
|| $currentAttachment < 1
|
|
|| (int) ($claim->public_ip_pool_id ?? 0) < 1
|
|
|| (int) ($claim->public_ip_id ?? 0) < 1) {
|
|
throw new CloudError('已释放公网网络声明集合不一致', 409);
|
|
}
|
|
if ($attachmentId === null) {
|
|
$attachmentId = $currentAttachment;
|
|
} elseif ($attachmentId !== $currentAttachment) {
|
|
throw new CloudError('已释放公网网络声明不属于同一接入', 409);
|
|
}
|
|
$canonical[] = [
|
|
'family' => $family,
|
|
'claim_id' => (int) $claim->id,
|
|
'claim_generation' => (int) $claim->claim_generation,
|
|
'claim_operation_id' => $operationId,
|
|
'public_ip_id' => (int) $claim->public_ip_id,
|
|
'network_attachment_id' => $currentAttachment,
|
|
'public_ip_pool_id' => (int) $claim->public_ip_pool_id,
|
|
];
|
|
}
|
|
return new self(hash('sha256', json_encode([
|
|
'reservation_id' => $reservationId,
|
|
'claims' => $canonical,
|
|
'public_network_sha256' => $network->fingerprint(),
|
|
], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES)));
|
|
}
|
|
|
|
public function digest(): string
|
|
{
|
|
return $this->digest;
|
|
}
|
|
}
|