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.
165 lines
5.8 KiB
PHP
165 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
/**
|
|
* Immutable identity of the one active IPv4+IPv6 claim pair.
|
|
*
|
|
* The digest deliberately excludes mutable lifecycle state. It identifies
|
|
* the exact reservation generation, claims, concrete addresses and frozen
|
|
* guest-network contract that an operation is authorised to mutate.
|
|
*/
|
|
final class CloudActivePublicNetworkClaimSet
|
|
{
|
|
private function __construct(
|
|
private readonly int $reservationId,
|
|
private readonly int $hostId,
|
|
private readonly array $claims,
|
|
private readonly CloudPublicNetworkSnapshot $publicNetwork,
|
|
private readonly string $digest
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @internal Topology constructors only. Application business paths must
|
|
* obtain this value through
|
|
* CloudPublicNetworkAllocationService::currentClaimSet().
|
|
*
|
|
* @param array<int, object> $claims
|
|
* @param array<int, object> $addresses keyed by public IP id
|
|
*/
|
|
public static function fromLedger(int $reservationId, int $hostId,
|
|
array $claims, array $addresses,
|
|
CloudPublicNetworkSnapshot $publicNetwork): self
|
|
{
|
|
if ($reservationId < 1 || $hostId < 1 || count($claims) !== 2) {
|
|
throw new CloudError('活动双栈公网地址账本不完整', 409);
|
|
}
|
|
$canonical = [];
|
|
$claimsByFamily = [];
|
|
$attachmentId = null;
|
|
foreach ($claims as $claim) {
|
|
$family = (int) ($claim->address_family ?? 0);
|
|
$claimId = (int) ($claim->id ?? 0);
|
|
$generation = (int) ($claim->claim_generation ?? 0);
|
|
$publicIpId = (int) ($claim->public_ip_id ?? 0);
|
|
$ip = $addresses[$publicIpId] ?? null;
|
|
$operationId = (string) ($claim->operation_id ?? '');
|
|
$currentAttachmentId = (int) (
|
|
$claim->network_attachment_id ?? 0
|
|
);
|
|
$addressCidr = is_object($ip)
|
|
? (string) ($ip->address ?? '') . '/'
|
|
. (int) ($ip->prefix_length ?? 0)
|
|
: '';
|
|
if (!in_array($family, [4, 6], true)
|
|
|| isset($canonical[$family])
|
|
|| $claimId < 1 || $generation < 1 || $publicIpId < 1
|
|
|| (int) ($claim->reservation_id ?? 0) !== $reservationId
|
|
|| (int) ($claim->active_claim ?? 0) !== 1
|
|
|| $currentAttachmentId < 1
|
|
|| (int) ($claim->public_ip_pool_id ?? 0) < 1
|
|
|| preg_match('/^[a-f0-9]{32}$/D', $operationId) !== 1
|
|
|| !is_object($ip)
|
|
|| (int) ($ip->id ?? 0) !== $publicIpId
|
|
|| (int) ($ip->host_id ?? 0) !== $hostId
|
|
|| (int) ($ip->address_family ?? 0) !== $family
|
|
|| (int) ($ip->reservation_id ?? 0) !== $reservationId
|
|
|| (int) ($ip->reservation_ip_claim_id ?? 0) !== $claimId
|
|
|| (int) ($ip->public_ip_pool_id ?? 0)
|
|
!== (int) $claim->public_ip_pool_id
|
|
|| !hash_equals(
|
|
$addressCidr,
|
|
$publicNetwork->address($family)
|
|
)
|
|
|| !hash_equals(
|
|
$operationId,
|
|
(string) ($ip->operation_id ?? '')
|
|
)) {
|
|
throw new CloudError('活动双栈公网地址账本不一致', 409);
|
|
}
|
|
if ($attachmentId === null) {
|
|
$attachmentId = $currentAttachmentId;
|
|
} elseif ($attachmentId !== $currentAttachmentId) {
|
|
throw new CloudError('活动双栈公网地址不属于同一网络接入', 409);
|
|
}
|
|
$canonical[$family] = [
|
|
'family' => $family,
|
|
'claim_id' => $claimId,
|
|
'claim_generation' => $generation,
|
|
'claim_operation_id' => $operationId,
|
|
'public_ip_id' => $publicIpId,
|
|
'network_attachment_id' => $currentAttachmentId,
|
|
'public_ip_pool_id' => (int) $claim->public_ip_pool_id,
|
|
];
|
|
$claimsByFamily[$family] = $claim;
|
|
}
|
|
if (!isset($canonical[4], $canonical[6])) {
|
|
throw new CloudError('活动双栈公网地址账本不完整', 409);
|
|
}
|
|
ksort($canonical, SORT_NUMERIC);
|
|
$digest = CloudPublicNetworkClaimSetIdentity::fromLedger(
|
|
$reservationId,
|
|
$claimsByFamily,
|
|
$addresses,
|
|
$publicNetwork
|
|
)->digest();
|
|
return new self(
|
|
$reservationId,
|
|
$hostId,
|
|
$canonical,
|
|
$publicNetwork,
|
|
$digest
|
|
);
|
|
}
|
|
|
|
public function reservationId(): int
|
|
{
|
|
return $this->reservationId;
|
|
}
|
|
|
|
public function hostId(): int
|
|
{
|
|
return $this->hostId;
|
|
}
|
|
|
|
public function claimId(int $family): int
|
|
{
|
|
if (!isset($this->claims[$family])) {
|
|
throw new \InvalidArgumentException('Address family is invalid.');
|
|
}
|
|
return $this->claims[$family]['claim_id'];
|
|
}
|
|
|
|
public function generation(int $family): int
|
|
{
|
|
if (!isset($this->claims[$family])) {
|
|
throw new \InvalidArgumentException('Address family is invalid.');
|
|
}
|
|
return $this->claims[$family]['claim_generation'];
|
|
}
|
|
|
|
public function publicIpId(int $family): int
|
|
{
|
|
if (!isset($this->claims[$family])) {
|
|
throw new \InvalidArgumentException('Address family is invalid.');
|
|
}
|
|
return $this->claims[$family]['public_ip_id'];
|
|
}
|
|
|
|
public function networkAttachmentId(): int
|
|
{
|
|
return $this->claims[4]['network_attachment_id'];
|
|
}
|
|
|
|
public function publicNetwork(): CloudPublicNetworkSnapshot
|
|
{
|
|
return $this->publicNetwork;
|
|
}
|
|
|
|
public function digest(): string
|
|
{
|
|
return $this->digest;
|
|
}
|
|
}
|