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.
virt/web/app/Services/Cloud/CloudFrozenPublicNetworkIde...

252 lines
8.4 KiB
PHP

<?php
namespace App\Services\Cloud;
/**
* Immutable stored identity for one exact public dual-stack claim set.
*
* Canonical network and claim digests come only from the authoritative active
* claim-set projection. Callers must obtain that projection through
* CloudPublicNetworkAllocationService::currentClaimSet(); this module never
* accepts raw claim/address rows and therefore cannot bypass its topology
* validation.
*/
final class CloudFrozenPublicNetworkIdentity
{
private function __construct(
private readonly array $identity,
private readonly CloudPublicNetworkSnapshot $network,
private readonly string $claimSetSha256
) {
}
public static function fromClaimSet(string $uuid,
CloudActivePublicNetworkClaimSet $claimSet): self
{
$reservationId = $claimSet->reservationId();
$hostId = $claimSet->hostId();
$networkAttachmentId = $claimSet->networkAttachmentId();
self::assertResourceIdentity(
$reservationId,
$hostId,
$uuid,
$networkAttachmentId
);
$network = $claimSet->publicNetwork();
$claimSetSha256 = $claimSet->digest();
if (preg_match('/^[a-f0-9]{64}$/D', $claimSetSha256) !== 1) {
throw new \InvalidArgumentException(
'Frozen public network claim-set digest is invalid.'
);
}
return new self(self::identity(
$reservationId,
$hostId,
$uuid,
$networkAttachmentId,
$claimSetSha256,
$network->fingerprint()
), $network, $claimSetSha256);
}
/**
* Rebuild a frozen identity from its row and the current authoritative
* claim-set projection. Top-level row columns remain separate so a
* self-consistent cross-target JSON replacement is rejected.
*/
public static function fromStored(int $reservationId, int $hostId,
string $uuid, int $networkAttachmentId, string $identityJson,
string $identitySha256, string $publicNetworkJson,
string $publicNetworkSha256, string $claimSetSha256,
CloudActivePublicNetworkClaimSet $claimSet): self
{
self::assertResourceIdentity(
$reservationId,
$hostId,
$uuid,
$networkAttachmentId
);
if (preg_match('/^[a-f0-9]{64}$/D', $identitySha256) !== 1
|| preg_match('/^[a-f0-9]{64}$/D', $publicNetworkSha256) !== 1
|| preg_match('/^[a-f0-9]{64}$/D', $claimSetSha256) !== 1) {
throw new \InvalidArgumentException(
'Frozen public network digest is invalid.'
);
}
try {
$identity = json_decode(
$identityJson,
true,
16,
JSON_THROW_ON_ERROR
);
$decoded = json_decode(
$publicNetworkJson,
true,
64,
JSON_THROW_ON_ERROR
);
} catch (\JsonException $exception) {
throw new \InvalidArgumentException(
'Frozen public network JSON is invalid.',
0,
$exception
);
}
if (!is_array($identity) || array_is_list($identity)) {
throw new \InvalidArgumentException(
'Frozen public network identity is invalid.'
);
}
self::exactKeys($identity, [
'claim_set_sha256', 'contract', 'host_id',
'network_attachment_id', 'public_network_sha256',
'reservation_id', 'uuid',
]);
if (($identity['contract'] ?? null) !== 1
|| !is_int($identity['reservation_id'] ?? null)
|| !is_int($identity['host_id'] ?? null)
|| !is_int($identity['network_attachment_id'] ?? null)
|| !is_string($identity['uuid'] ?? null)
|| !is_string($identity['claim_set_sha256'] ?? null)
|| !is_string($identity['public_network_sha256'] ?? null)) {
throw new \InvalidArgumentException(
'Frozen public network identity is invalid.'
);
}
$network = CloudPublicNetworkSnapshot::fromArray($decoded);
if (!hash_equals($network->fingerprint(), $publicNetworkSha256)) {
throw new \InvalidArgumentException(
'Frozen public network digest does not match its payload.'
);
}
$canonical = self::identity(
$reservationId,
$hostId,
$uuid,
$networkAttachmentId,
$claimSetSha256,
$publicNetworkSha256
);
if ($identity !== $canonical
|| !hash_equals($identity['claim_set_sha256'], $claimSetSha256)
|| !hash_equals(
$identity['public_network_sha256'],
$publicNetworkSha256
)
|| !hash_equals(self::digest($canonical), $identitySha256)) {
throw new \InvalidArgumentException(
'Frozen public network identity digest does not match.'
);
}
$authoritative = self::fromClaimSet($uuid, $claimSet);
if ($claimSet->reservationId() !== $reservationId
|| $claimSet->hostId() !== $hostId
|| $claimSet->networkAttachmentId() !== $networkAttachmentId
|| !hash_equals(
$authoritative->claimSetSha256(),
$claimSetSha256
)
|| !hash_equals(
$authoritative->publicNetworkSha256(),
$publicNetworkSha256
)
|| !hash_equals(
$authoritative->publicNetwork()->fingerprint(),
$network->fingerprint()
)) {
throw new \InvalidArgumentException(
'Frozen public network identity does not match its claim set.'
);
}
return new self($canonical, $network, $claimSetSha256);
}
public function identityJson(): string
{
return self::encode($this->identity);
}
public function identitySha256(): string
{
return self::digest($this->identity);
}
public function publicNetwork(): CloudPublicNetworkSnapshot
{
return $this->network;
}
public function publicNetworkJson(): string
{
return json_encode(
$this->network->toArray(),
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
);
}
public function publicNetworkSha256(): string
{
return $this->network->fingerprint();
}
public function claimSetSha256(): string
{
return $this->claimSetSha256;
}
private static function identity(int $reservationId, int $hostId,
string $uuid, int $networkAttachmentId, string $claimSetSha256,
string $publicNetworkSha256): array
{
return [
'contract' => 1,
'reservation_id' => $reservationId,
'host_id' => $hostId,
'uuid' => strtolower($uuid),
'network_attachment_id' => $networkAttachmentId,
'claim_set_sha256' => $claimSetSha256,
'public_network_sha256' => $publicNetworkSha256,
];
}
private static function assertResourceIdentity(int $reservationId,
int $hostId, string $uuid, int $networkAttachmentId): void
{
if ($reservationId < 1 || $hostId < 1 || $networkAttachmentId < 1
|| preg_match(
'/^[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/D',
strtolower($uuid)
) !== 1) {
throw new \InvalidArgumentException(
'Frozen public network resource identity is invalid.'
);
}
}
private static function exactKeys(array $value, array $expected): void
{
$actual = array_keys($value);
sort($actual, SORT_STRING);
sort($expected, SORT_STRING);
if ($actual !== $expected) {
throw new \InvalidArgumentException(
'Frozen public network identity fields are invalid.'
);
}
}
private static function encode(array $value): string
{
return json_encode(
$value,
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
);
}
private static function digest(array $value): string
{
return hash('sha256', self::encode($value));
}
}