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/tests/CloudInstancePublicNetworkP...

171 lines
6.5 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudInstancePublicNetworkProjection;
use App\Services\Cloud\CloudInstanceMetadata;
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$snapshot = [
'contract' => 1,
'network_selector' => 'bridge:br0',
'ipv4' => [
'pool_key' => str_repeat('1', 32),
'pool_cidr' => '8.8.8.0/24',
'address_cidr' => '8.8.8.20/24',
'gateway' => '8.8.8.1',
'gateway_on_link' => false,
],
'ipv6' => [
'pool_key' => str_repeat('2', 32),
'pool_cidr' => '2404:c140:156:3a::/64',
'address_cidr' => '2404:c140:156:3a::20/64',
'gateway' => '2404:c140:156::1',
'gateway_on_link' => true,
],
'dns_servers' => ['1.1.1.1', '2606:4700:4700::1111'],
];
$projection = CloudInstancePublicNetworkProjection::fromSpec([
'public_network' => $snapshot,
// Flat values are attacker-controlled legacy input and must never win.
'public_ip' => '9.9.9.9',
'public_ip_cidr' => '9.9.9.9/32',
'gateway' => '9.9.9.1',
]);
$assert(($projection['public_network_valid'] ?? null) === true,
'A canonical dual-stack snapshot was not accepted.');
$assert(($projection['public_network'] ?? null) === [
'contract' => 1,
'network_selector' => 'bridge:br0',
'ipv4' => [
'address_cidr' => '8.8.8.20/24',
'address' => '8.8.8.20',
'pool_cidr' => '8.8.8.0/24',
'gateway' => '8.8.8.1',
'gateway_on_link' => false,
],
'ipv6' => [
'address_cidr' => '2404:c140:156:3a::20/64',
'address' => '2404:c140:156:3a::20',
'pool_cidr' => '2404:c140:156:3a::/64',
'gateway' => '2404:c140:156::1',
'gateway_on_link' => true,
],
'dns_servers' => ['1.1.1.1', '2606:4700:4700::1111'],
], 'The browser projection was not derived exactly from the canonical snapshot.');
$assert(($projection['public_ipv4'] ?? null) === '8.8.8.20'
&& ($projection['public_ipv4_cidr'] ?? null) === '8.8.8.20/24'
&& ($projection['public_ipv6'] ?? null) === '2404:c140:156:3a::20'
&& ($projection['public_ipv6_cidr'] ?? null) === '2404:c140:156:3a::20/64'
&& ($projection['public_ip'] ?? null) === '8.8.8.20'
&& ($projection['public_ip_cidr'] ?? null) === '8.8.8.20/24'
&& ($projection['gateway'] ?? null) === '8.8.8.1'
&& ($projection['dns_servers'] ?? null)
=== ['1.1.1.1', '2606:4700:4700::1111'],
'Presentation aliases were not derived from the snapshot in one place.');
foreach ([
[],
['public_ip' => '8.8.8.20', 'public_ip_cidr' => '8.8.8.20/24'],
['public_network' => array_replace($snapshot, [
'ipv6' => array_replace($snapshot['ipv6'], [
'address_cidr' => 'not-an-address',
]),
])],
] as $invalidSpec) {
$invalid = CloudInstancePublicNetworkProjection::fromSpec($invalidSpec);
$assert($invalid === [
'public_network_valid' => false,
'public_network' => null,
], 'A missing or damaged canonical snapshot did not fail closed.');
}
$publicSpec = CloudInstanceMetadata::publicSpec([
'region' => 'Hong Kong',
'public_network' => $snapshot,
'public_ip' => '9.9.9.9',
'public_ip_cidr' => '9.9.9.9/32',
]);
$assert(($publicSpec['region'] ?? null) === 'Hong Kong'
&& ($publicSpec['public_network_valid'] ?? null) === true
&& ($publicSpec['public_ipv4'] ?? null) === '8.8.8.20'
&& ($publicSpec['public_ipv6'] ?? null) === '2404:c140:156:3a::20'
&& ($publicSpec['public_ip'] ?? null) === '8.8.8.20',
'The public instance specification did not use the canonical projection.');
$closedSpec = CloudInstanceMetadata::publicSpec([
'region' => 'Hong Kong',
'public_ip' => '8.8.8.20',
'public_ip_cidr' => '8.8.8.20/24',
]);
$assert(($closedSpec['public_network_valid'] ?? null) === false
&& array_key_exists('public_network', $closedSpec)
&& $closedSpec['public_network'] === null
&& !isset($closedSpec['public_ip'], $closedSpec['public_ip_cidr']),
'Legacy flat fields were trusted after the canonical snapshot was missing.');
$merged = CloudInstanceMetadata::merge([
'plan_id' => 5,
'public_network' => $snapshot,
'public_ip' => '9.9.9.9',
'public_ip_cidr' => '9.9.9.9/32',
'gateway' => '9.9.9.1',
'dns_servers' => ['9.9.9.9'],
], [
'public_ip_cidr' => '1.1.1.20/24',
'public_network' => array_replace($snapshot, [
'network_selector' => 'bridge:evil0',
]),
'vcpu' => 4,
]);
$assert(($merged['public_network'] ?? null) === $snapshot
&& ($merged['vcpu'] ?? null) === 4
&& !isset($merged['public_ip'], $merged['public_ip_cidr'],
$merged['gateway'], $merged['dns_servers']),
'Agent or legacy flat metadata replaced or duplicated the frozen snapshot.');
$view = CloudInstanceMetadata::publicView(array_merge($publicSpec, [
'id' => '7',
'api_id' => '7',
'uuid' => '77777777-7777-4777-8777-777777777777',
'name' => 'dual-stack-vm',
]), [
'uuid' => '77777777-7777-4777-8777-777777777777',
'public_ip_cidr' => '1.1.1.20/24',
'vcpu' => 8,
]);
$assert(($view['public_network_valid'] ?? null) === true
&& ($view['public_ipv4'] ?? null) === '8.8.8.20'
&& ($view['public_ipv6'] ?? null) === '2404:c140:156:3a::20'
&& ($view['vcpu'] ?? null) === 8,
'The live metadata view discarded or replaced the PHP-owned network projection.');
$tamperedAliases = CloudInstanceMetadata::publicView(array_merge($publicSpec, [
'id' => '7', 'uuid' => '77777777-7777-4777-8777-777777777777',
'name' => 'dual-stack-vm', 'public_ipv4' => '9.9.9.9',
'public_ipv6' => '2606:4700:4700::9999',
]), []);
$assert(($tamperedAliases['public_ipv4'] ?? null) === '8.8.8.20'
&& ($tamperedAliases['public_ipv6'] ?? null) === '2404:c140:156:3a::20',
'Cached aliases were trusted instead of being re-derived by the projection helper.');
$damagedProjection = $publicSpec;
$damagedProjection['public_network']['ipv6']['address'] = '2606:4700:4700::9999';
$closedView = CloudInstanceMetadata::publicView(array_merge($damagedProjection, [
'id' => '7', 'uuid' => '77777777-7777-4777-8777-777777777777',
'name' => 'dual-stack-vm',
]), []);
$assert($closedView['public_network_valid'] === false
&& $closedView['public_network'] === null
&& !isset($closedView['public_ipv4'], $closedView['public_ipv6']),
'A damaged cached network projection was not rejected before display.');
echo "Cloud instance public-network projection tests passed.\n";