refactor: centralize public network address policy
parent
e57ac44fbd
commit
3c1a26edbe
@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Cloud;
|
||||
|
||||
/** Pure address identity and selectability policy for a persisted pool. */
|
||||
final class CloudPublicNetworkAddressPolicy
|
||||
{
|
||||
public static function contract(
|
||||
object $pool,
|
||||
?int $expectedFamily = null
|
||||
): CloudPublicIpPoolContract {
|
||||
$contract = CloudPublicIpPoolContract::fromRow($pool);
|
||||
if ($expectedFamily !== null
|
||||
&& $contract->family() !== $expectedFamily) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Public IP pool family does not match.'
|
||||
);
|
||||
}
|
||||
return $contract;
|
||||
}
|
||||
|
||||
/** Validate immutable row identity without applying mutable exclusions. */
|
||||
public static function assertIdentity(
|
||||
object $row,
|
||||
object $pool,
|
||||
CloudPublicIpPoolContract $contract
|
||||
): CloudIpAddress {
|
||||
$address = CloudIpAddress::parse((string) ($row->address ?? ''));
|
||||
if ((int) ($row->host_id ?? 0) !== (int) ($pool->host_id ?? 0)
|
||||
|| (int) ($row->public_ip_pool_id ?? 0)
|
||||
!== (int) ($pool->id ?? 0)
|
||||
|| (int) ($row->address_family ?? 0)
|
||||
!== (int) ($pool->address_family ?? 0)
|
||||
|| $contract->family() !== (int) ($pool->address_family ?? 0)
|
||||
|| (int) ($row->prefix_length ?? 0)
|
||||
!== $contract->guestPrefixLength()
|
||||
|| !hash_equals($address->text(), (string) $row->address)
|
||||
|| !is_string($row->address_binary ?? null)
|
||||
|| !hash_equals($address->binary(), $row->address_binary)
|
||||
|| !$contract->addressBelongs($address)) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Public IP address identity is invalid.'
|
||||
);
|
||||
}
|
||||
return $address;
|
||||
}
|
||||
|
||||
/** Validate an address row as a current allocation candidate. */
|
||||
public static function assertSelectable(
|
||||
object $row,
|
||||
object $pool,
|
||||
CloudPublicIpPoolContract $contract,
|
||||
?string $managementAddress
|
||||
): CloudIpAddress {
|
||||
$address = self::assertIdentity($row, $pool, $contract);
|
||||
if (!self::addressSelectable(
|
||||
$address,
|
||||
$contract,
|
||||
$managementAddress
|
||||
)) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Public IP address is not selectable.'
|
||||
);
|
||||
}
|
||||
return $address;
|
||||
}
|
||||
|
||||
public static function addressSelectable(
|
||||
CloudIpAddress $address,
|
||||
CloudPublicIpPoolContract $contract,
|
||||
?string $managementAddress
|
||||
): bool {
|
||||
if (!$contract->addressAllowed($address)) {
|
||||
return false;
|
||||
}
|
||||
$managementAddress = trim((string) $managementAddress);
|
||||
if ($managementAddress === '') {
|
||||
return true;
|
||||
}
|
||||
$management = CloudIpAddress::parse($managementAddress);
|
||||
return $management->family() !== $address->family()
|
||||
|| !$management->equals($address);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,177 @@
|
||||
<?php
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
use App\Services\Cloud\CloudIpAddress;
|
||||
use App\Services\Cloud\CloudPublicNetworkAddressPolicy;
|
||||
|
||||
$assert = static function (bool $condition, string $message): void {
|
||||
if (!$condition) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
};
|
||||
$rejects = static function (callable $callback, string $message): void {
|
||||
try {
|
||||
$callback();
|
||||
} catch (InvalidArgumentException) {
|
||||
return;
|
||||
}
|
||||
throw new RuntimeException($message);
|
||||
};
|
||||
$row = static function (object $pool, string $address): object {
|
||||
return (object) [
|
||||
'host_id' => (int) $pool->host_id,
|
||||
'public_ip_pool_id' => (int) $pool->id,
|
||||
'address_family' => (int) $pool->address_family,
|
||||
'address' => $address,
|
||||
'address_binary' => inet_pton($address),
|
||||
'prefix_length' => (int) $pool->guest_prefix_length,
|
||||
];
|
||||
};
|
||||
|
||||
$ipv4 = (object) [
|
||||
'id' => 10,
|
||||
'pool_key' => str_repeat('a', 32),
|
||||
'host_id' => 1,
|
||||
'address_family' => 4,
|
||||
'cidr' => '8.8.8.0/24',
|
||||
'gateway' => '8.8.8.1',
|
||||
'gateway_on_link' => 0,
|
||||
'guest_prefix_length' => 24,
|
||||
'dns_servers_json' => '["1.1.1.1","8.8.8.8"]',
|
||||
'reserved_ips_json' => '["8.8.8.9"]',
|
||||
'allocation_start' => null,
|
||||
'allocation_end' => null,
|
||||
'allocation_cursor' => null,
|
||||
'max_allocations' => 0,
|
||||
];
|
||||
$ipv6 = (object) [
|
||||
'id' => 20,
|
||||
'pool_key' => str_repeat('b', 32),
|
||||
'host_id' => 1,
|
||||
'address_family' => 6,
|
||||
'cidr' => '2404:c140:156:3a::/64',
|
||||
'gateway' => '2404:c140:156:3a::1',
|
||||
'gateway_on_link' => 0,
|
||||
'guest_prefix_length' => 64,
|
||||
'dns_servers_json' => '["2606:4700:4700::1111"]',
|
||||
'reserved_ips_json' => '["2404:c140:156:3a::5"]',
|
||||
'allocation_start' => '2404:c140:156:3a::10',
|
||||
'allocation_end' => '2404:c140:156:3a::ff',
|
||||
'allocation_cursor' => null,
|
||||
'max_allocations' => 8,
|
||||
];
|
||||
|
||||
$v4Contract = CloudPublicNetworkAddressPolicy::contract($ipv4, 4);
|
||||
$v6Contract = CloudPublicNetworkAddressPolicy::contract($ipv6, 6);
|
||||
$v4 = $row($ipv4, '8.8.8.10');
|
||||
$v6 = $row($ipv6, '2404:c140:156:3a::10');
|
||||
$assert(
|
||||
CloudPublicNetworkAddressPolicy::assertSelectable(
|
||||
$v4,
|
||||
$ipv4,
|
||||
$v4Contract,
|
||||
null
|
||||
)->text() === '8.8.8.10'
|
||||
&& CloudPublicNetworkAddressPolicy::assertSelectable(
|
||||
$v6,
|
||||
$ipv6,
|
||||
$v6Contract,
|
||||
'103.138.75.126'
|
||||
)->text() === '2404:c140:156:3a::10',
|
||||
'Canonical IPv4/IPv6 candidates were not accepted.'
|
||||
);
|
||||
|
||||
foreach (['8.8.8.1', '8.8.8.8', '8.8.8.9'] as $excluded) {
|
||||
$excludedRow = $row($ipv4, $excluded);
|
||||
$assert(
|
||||
CloudPublicNetworkAddressPolicy::assertIdentity(
|
||||
$excludedRow,
|
||||
$ipv4,
|
||||
$v4Contract
|
||||
)->text() === $excluded,
|
||||
'Immutable identity rejected a historical excluded address.'
|
||||
);
|
||||
$rejects(
|
||||
static fn () => CloudPublicNetworkAddressPolicy::assertSelectable(
|
||||
$excludedRow,
|
||||
$ipv4,
|
||||
$v4Contract,
|
||||
null
|
||||
),
|
||||
'A gateway or reserved IPv4 was selectable.'
|
||||
);
|
||||
}
|
||||
$rejects(
|
||||
static fn () => CloudPublicNetworkAddressPolicy::assertSelectable(
|
||||
$v4,
|
||||
$ipv4,
|
||||
$v4Contract,
|
||||
'8.8.8.10'
|
||||
),
|
||||
'The host management IPv4 was selectable.'
|
||||
);
|
||||
$historicalV6 = $row($ipv6, '2404:c140:156:3a::6');
|
||||
$assert(
|
||||
CloudPublicNetworkAddressPolicy::assertIdentity(
|
||||
$historicalV6,
|
||||
$ipv6,
|
||||
$v6Contract
|
||||
)->text() === '2404:c140:156:3a::6',
|
||||
'Immutable identity rejected a historical out-of-range IPv6.'
|
||||
);
|
||||
$rejects(
|
||||
static fn () => CloudPublicNetworkAddressPolicy::assertSelectable(
|
||||
$historicalV6,
|
||||
$ipv6,
|
||||
$v6Contract,
|
||||
null
|
||||
),
|
||||
'An IPv6 outside the sparse allocation range was selectable.'
|
||||
);
|
||||
$rejects(
|
||||
static fn () => CloudPublicNetworkAddressPolicy::assertSelectable(
|
||||
$row($ipv4, '1.1.1.1'),
|
||||
$ipv4,
|
||||
$v4Contract,
|
||||
null
|
||||
),
|
||||
'An out-of-pool DNS address passed row identity.'
|
||||
);
|
||||
|
||||
foreach (['host_id', 'public_ip_pool_id', 'address_family', 'prefix_length'] as $field) {
|
||||
$broken = clone $v4;
|
||||
$broken->{$field} = (int) $broken->{$field} + 1;
|
||||
$rejects(
|
||||
static fn () => CloudPublicNetworkAddressPolicy::assertIdentity(
|
||||
$broken,
|
||||
$ipv4,
|
||||
$v4Contract
|
||||
),
|
||||
'A mismatched ' . $field . ' passed immutable identity.'
|
||||
);
|
||||
}
|
||||
$brokenBinary = clone $v4;
|
||||
$brokenBinary->address_binary = inet_pton('8.8.8.11');
|
||||
$rejects(
|
||||
static fn () => CloudPublicNetworkAddressPolicy::assertIdentity(
|
||||
$brokenBinary,
|
||||
$ipv4,
|
||||
$v4Contract
|
||||
),
|
||||
'A mismatched address binary passed immutable identity.'
|
||||
);
|
||||
$rejects(
|
||||
static fn () => CloudPublicNetworkAddressPolicy::contract($ipv6, 4),
|
||||
'A pool contract was accepted for the wrong family.'
|
||||
);
|
||||
$rejects(
|
||||
static fn () => CloudPublicNetworkAddressPolicy::addressSelectable(
|
||||
CloudIpAddress::parse('8.8.8.10'),
|
||||
$v4Contract,
|
||||
'not-an-address'
|
||||
),
|
||||
'An invalid management address was silently ignored.'
|
||||
);
|
||||
|
||||
echo "Public-network address policy tests passed.\n";
|
||||
@ -1,36 +1,66 @@
|
||||
<?php
|
||||
|
||||
$source = file_get_contents(dirname(__DIR__)
|
||||
. '/app/Services/Cloud/CloudPublicNetworkAllocationService.php');
|
||||
if (!is_string($source)) {
|
||||
throw new RuntimeException('Unable to read public-network allocator.');
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
use App\Services\Cloud\CloudPublicNetworkAddressPolicy;
|
||||
|
||||
$root = dirname(__DIR__) . '/app/Services/Cloud/';
|
||||
$policy = file_get_contents($root . 'CloudPublicNetworkAddressPolicy.php');
|
||||
$allocator = file_get_contents(
|
||||
$root . 'CloudPublicNetworkAllocationService.php'
|
||||
);
|
||||
$transition = file_get_contents(
|
||||
$root . 'CloudPublicNetworkTransitionWorkflow.php'
|
||||
);
|
||||
if (!is_string($policy) || !is_string($allocator)
|
||||
|| !is_string($transition)) {
|
||||
throw new RuntimeException('Unable to read public-network policy sources.');
|
||||
}
|
||||
|
||||
$methods = array_values(array_map(
|
||||
static fn (ReflectionMethod $method): string => $method->getName(),
|
||||
array_filter(
|
||||
(new ReflectionClass(CloudPublicNetworkAddressPolicy::class))
|
||||
->getMethods(ReflectionMethod::IS_PUBLIC),
|
||||
static fn (ReflectionMethod $method): bool =>
|
||||
$method->getDeclaringClass()->getName()
|
||||
=== CloudPublicNetworkAddressPolicy::class
|
||||
)
|
||||
));
|
||||
sort($methods, SORT_STRING);
|
||||
if ($methods !== [
|
||||
'addressSelectable',
|
||||
'assertIdentity',
|
||||
'assertSelectable',
|
||||
'contract',
|
||||
]) {
|
||||
throw new RuntimeException('Address policy public seam drifted.');
|
||||
}
|
||||
|
||||
foreach ([
|
||||
'assertPoolLedger(object $pool,' => 'pool-ledger contract parameter',
|
||||
'CloudPublicIpPoolContract $contract, array $rows' =>
|
||||
'shared parsed contract in ledger validation',
|
||||
"lockReservableAddressCandidate(\n object \$pool,\n"
|
||||
. ' CloudPublicIpPoolContract $contract,' =>
|
||||
'shared parsed contract in candidate selection',
|
||||
"'contracts' => \$contracts" => 'per-catalog contract cache',
|
||||
"'contracts' => \$selectedContracts" =>
|
||||
'per-selection contract propagation',
|
||||
] as $needle => $label) {
|
||||
if (!str_contains($source, $needle)) {
|
||||
throw new RuntimeException('Missing ' . $label . '.');
|
||||
'DB::',
|
||||
'lockForUpdate(',
|
||||
'::find(',
|
||||
'->save(',
|
||||
'->update(',
|
||||
'->insert(',
|
||||
'->delete(',
|
||||
] as $forbidden) {
|
||||
if (str_contains($policy, $forbidden)) {
|
||||
throw new RuntimeException(
|
||||
'Pure address policy performs persistence: ' . $forbidden
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$rawAllowed = strstr($source,
|
||||
'private static function rawAddressAllowedByPool');
|
||||
if (!is_string($rawAllowed)
|
||||
|| !str_contains($rawAllowed, 'CloudPublicIpPoolContract $contract')
|
||||
|| str_contains(substr($rawAllowed, 0, 1200),
|
||||
'CloudPublicIpPoolContract::fromRow')) {
|
||||
if (!str_contains($allocator, 'CloudPublicNetworkAddressPolicy::')
|
||||
|| !str_contains($transition, 'CloudPublicNetworkAddressPolicy::')
|
||||
|| str_contains($allocator, 'rawAddressAllowedByPool')
|
||||
|| str_contains($allocator, 'rawAddressBelongsToPool')
|
||||
|| !str_contains($allocator, "'contracts' => \$contracts")
|
||||
|| !str_contains($allocator, "'contracts' => \$selectedContracts")) {
|
||||
throw new RuntimeException(
|
||||
'Hot address validation reconstructs the pool contract per row.'
|
||||
'Allocation and transition do not share the pure address policy.'
|
||||
);
|
||||
}
|
||||
|
||||
echo "Public-network allocator pool-contract reuse checks passed.\n";
|
||||
echo "Public-network shared address-policy contracts passed.\n";
|
||||
|
||||
Loading…
Reference in New Issue