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/CloudAgentStaticHostResolve...

120 lines
5.5 KiB
PHP

<?php
// Dependency-free contract for the explicit Agent destination and assembly
// boundary. It intentionally does not require PHP/Composer or a database.
require dirname(__DIR__) . '/app/Services/Cloud/CloudAgentHostResolver.php';
require dirname(__DIR__) . '/app/Services/Cloud/CloudAgentSecretResolver.php';
require dirname(__DIR__) . '/app/Services/Cloud/CloudAgentFileSecretResolver.php';
require dirname(__DIR__) . '/app/Services/Cloud/CloudAgentStaticHostResolver.php';
require dirname(__DIR__) . '/app/Services/Cloud/CloudAgentEndpointPolicy.php';
require dirname(__DIR__) . '/app/Services/Cloud/CloudAgentHostClientResolver.php';
require dirname(__DIR__) . '/app/Services/Cloud/CloudAgentHostClientResolverFactory.php';
require dirname(__DIR__) . '/app/Services/Config.php';
use App\Services\Cloud\CloudAgentFileSecretResolver;
use App\Services\Cloud\CloudAgentHostClientResolver;
use App\Services\Cloud\CloudAgentHostClientResolverFactory;
use App\Services\Cloud\CloudAgentStaticHostResolver;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$resolver = new CloudAgentStaticHostResolver([
'Agent.Example.Test.' => ['8.8.8.103', '2001:4860:4860::8888', '8.8.8.103'],
]);
$assert($resolver->resolve('agent.example.test') === ['2001:4860:4860::8888', '8.8.8.103'],
'The static resolver did not normalize, de-duplicate and sort explicit addresses.');
$assert($resolver->resolve('AGENT.EXAMPLE.TEST.') === $resolver->resolve('agent.example.test'),
'DNS-name case or the root dot changed the allowlist lookup.');
$assert($resolver->resolve('missing.example.test') === []
&& $resolver->resolve('agent/example.test') === [],
'Unknown or malformed hosts were not failed closed.');
foreach ([
['agent.example.test' => []],
['agent.example.test' => ['127.0.0.1']],
['agent.example.test' => ['192.0.2.10']],
['bad host' => ['8.8.8.8']],
['agent.example.test' => ['8.8.8.8'], 'AGENT.EXAMPLE.TEST.' => ['8.8.4.4']],
] as $invalidAllowlist) {
try {
new CloudAgentStaticHostResolver($invalidAllowlist);
throw new RuntimeException('Unsafe Agent address allowlist was accepted.');
} catch (InvalidArgumentException $expected) {
$assert($expected->getMessage() !== '', 'Allowlist rejection did not include a reason.');
}
}
$settings = [
'cloud_agent_secret_dir' => '/run/virt-cloud/agents',
'cloud_agent_address_allowlist' => ['agent.example.test' => ['8.8.8.103']],
'cloud_agent_timeout' => '12.5',
];
$testClientFactory = static function (...$arguments): mixed {
// The factory contract is tested before Composer/Guzzle is installed;
// actual transport construction is covered by CloudAgentClient tests.
return null;
};
$testFingerprint = static fn (string $path): string => str_repeat('a', 64);
$testPathValidator = static fn (string $path, bool $privateKey = false): bool => true;
$assembled = CloudAgentHostClientResolverFactory::create(
$settings,
null,
$testClientFactory,
$testFingerprint,
$testPathValidator,
);
$assert($assembled instanceof CloudAgentHostClientResolver,
'The Agent resolver factory did not return the typed resolver.');
$reflection = new ReflectionClass($assembled);
$hostProperty = $reflection->getProperty('hostResolver');
$hostProperty->setAccessible(true);
$secretProperty = $reflection->getProperty('secrets');
$secretProperty->setAccessible(true);
$configuredHostResolver = $hostProperty->getValue($assembled);
$configuredSecrets = $secretProperty->getValue($assembled);
$assert($configuredHostResolver instanceof CloudAgentStaticHostResolver
&& $configuredHostResolver->resolve('agent.example.test') === ['8.8.8.103'],
'Factory configuration did not install the explicit host resolver.');
$assert($configuredSecrets instanceof CloudAgentFileSecretResolver,
'Factory configuration did not install the file-backed secret resolver.');
$GLOBALS['System_Config'] = [
'cloud_agent_secret_dir' => '/run/virt-cloud/agents',
'cloud_agent_address_allowlist' => ['agent.example.test' => ['8.8.8.103']],
'cloud_agent_timeout' => 11,
];
$fromConfig = CloudAgentHostClientResolverFactory::fromConfig(
null,
null,
$testClientFactory,
$testFingerprint,
$testPathValidator,
);
$configReflection = new ReflectionClass($fromConfig);
$configHostProperty = $configReflection->getProperty('hostResolver');
$configHostProperty->setAccessible(true);
$configHost = $configHostProperty->getValue($fromConfig);
$assert($configHost instanceof CloudAgentStaticHostResolver
&& $configHost->resolve('agent.example.test') === ['8.8.8.103'],
'fromConfig() did not read the explicit address allowlist.');
$source = file_get_contents(dirname(__DIR__) . '/app/Services/Cloud/CloudAgentStaticHostResolver.php');
$factorySource = file_get_contents(dirname(__DIR__) . '/app/Services/Cloud/CloudAgentHostClientResolverFactory.php');
$assert(is_string($source) && is_string($factorySource)
&& !str_contains($source, 'gethostbyname')
&& !str_contains($source, 'dns_get_record')
&& !str_contains($source, 'getaddrinfo')
&& !str_contains($source, 'ssh')
&& !str_contains($source, 'Python')
&& str_contains($factorySource, 'CloudAgentFileSecretResolver')
&& str_contains($factorySource, 'CloudAgentStaticHostResolver')
&& !str_contains($factorySource, 'CloudEngineClient')
&& !str_contains($factorySource, 'ssh'),
'The Agent assembly boundary introduced ambient DNS or a legacy transport fallback.');
fwrite(STDOUT, "Cloud Agent static resolver contract passed.\n");