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

116 lines
5.7 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudAgentEndpointPolicy;
use App\Services\Cloud\CloudAgentHostResolver;
use App\Services\Cloud\CloudAgentRegistryHealthService;
use App\Services\Cloud\CloudAgentSecretResolver;
use App\Services\Cloud\CloudError;
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$empty = (new CloudAgentRegistryHealthService(static fn (): array => []))->snapshot(1700000000);
$assert($empty['status'] === 'disabled' && $empty['agents'] === [],
'An absent Agent registry must be disabled and must not probe an endpoint.');
$duplicate = new CloudAgentRegistryHealthService(static fn (): array => [
['host_id' => 7, 'agent_id' => 'a-1', 'endpoint' => 'https://agent.example.test'],
['host_id' => 7, 'agent_id' => 'a-2', 'endpoint' => 'https://agent-2.example.test'],
]);
$duplicateResult = $duplicate->snapshot(1700000000);
$assert($duplicateResult['status'] === 'unknown' && $duplicateResult['agents'] === [],
'A host_id may map to exactly one registered Agent.');
foreach (['http://agent.example.test', 'https://127.0.0.1', 'https://localhost',
'https://[::1]', 'https://user:pass@agent.example.test', 'https://agent.example.test/path?q=1'] as $unsafe) {
try {
CloudAgentEndpointPolicy::validate($unsafe);
throw new RuntimeException('Unsafe Agent endpoint accepted: ' . $unsafe);
} catch (CloudError $expected) {
$assert($expected->httpStatus === 422, 'Unsafe endpoint returned an unexpected status.');
}
}
$resolver = new class implements CloudAgentSecretResolver {
public array $requested = [];
public function resolve(string $agentId): ?array
{
$this->requested[] = $agentId;
return null;
}
};
$resolverContract = new class implements CloudAgentHostResolver {
public function resolve(string $hostname): array { return ['8.8.8.8']; }
};
$noSecret = new CloudAgentRegistryHealthService(static fn (): array => [
['host_id' => 8, 'agent_id' => 'a-8', 'endpoint' => 'https://agent.example.test', 'status' => 'active'],
], $resolver, $resolverContract);
$noSecretResult = $noSecret->snapshot(1700000000);
$assert($noSecretResult['status'] === 'unknown'
&& $noSecretResult['agents'][0]['status'] === 'unknown'
&& $resolver->requested === ['a-8'],
'Agent health must remain unknown when host-local secret/TLS resolution is unavailable.');
$unknownStatus = new CloudAgentRegistryHealthService(static fn (): array => [
['host_id' => 9, 'agent_id' => 'a-9', 'endpoint' => 'https://agent.example.test', 'status' => 'mystery'],
]);
$unknownStatusResult = $unknownStatus->snapshot(1700000000);
$assert($unknownStatusResult['agents'][0]['status'] === 'unknown',
'Unknown registry statuses must fail closed.');
$inactive = new CloudAgentRegistryHealthService(static fn (): array => [
['host_id' => 10, 'agent_id' => 'a-10', 'endpoint' => 'https://agent.example.test', 'status' => 'disabled'],
], $resolver, $resolverContract);
$inactiveResult = $inactive->snapshot(1700000000);
$assert($inactiveResult['agents'][0]['status'] === 'disabled' && $resolver->requested === ['a-8'],
'Disabled Agents must not make network requests and must be reported disabled.');
try {
CloudAgentEndpointPolicy::validate('https://agent.example.test');
throw new RuntimeException('DNS hostname was accepted without an explicit resolver.');
} catch (CloudError $expected) {
$assert($expected->httpStatus === 422, 'Missing endpoint resolver returned an unexpected status.');
}
$publicResolver = static fn (string $host): array => ['8.8.8.8'];
$assert(CloudAgentEndpointPolicy::validate('https://8.8.8.8', $publicResolver) === 'https://8.8.8.8',
'A public literal endpoint with an explicit resolver should be accepted.');
try {
CloudAgentEndpointPolicy::validate('https://127.0.0.1', $publicResolver);
throw new RuntimeException('A private literal endpoint was accepted despite a public resolver result.');
} catch (CloudError $expected) {
$assert($expected->httpStatus === 422, 'Private literal endpoint returned an unexpected status.');
}
try {
CloudAgentEndpointPolicy::validate('https://8.8.8.8', static fn (string $host): array => ['10.0.0.8']);
throw new RuntimeException('Resolver returned a private address but endpoint was accepted.');
} catch (CloudError $expected) {
$assert($expected->httpStatus === 422, 'Private resolver result returned an unexpected status.');
}
$source = file_get_contents(dirname(__DIR__) . '/app/Services/Cloud/CloudAgentRegistryHealthService.php');
$assert($source !== false && str_contains($source, 'CloudAgentSecretResolver')
&& str_contains($source, 'CloudAgentEndpointPolicy::validate')
&& str_contains($source, 'CloudAgentHostResolver')
&& str_contains($source, '$resolvedAddresses')
&& str_contains($source, 'CloudAgentClient')
&& str_contains($source, 'validateCredentialPath')
&& str_contains($source, 'MAX_REGISTRY_ROWS')
&& str_contains($source, 'MAX_SNAPSHOT_SECONDS')
&& !str_contains($source, 'CloudEngineClient') && !str_contains($source, 'CloudSecrets')
&& !str_contains($source, 'ssh'), 'Registry shadow crossed the legacy secret or SSH boundary.');
$clientSource = file_get_contents(dirname(__DIR__) . '/app/Services/Cloud/CloudAgentClient.php');
$assert($clientSource !== false
&& str_contains($clientSource, "'resolve'")
&& str_contains($clientSource, 'validateResolvedAddresses')
&& str_contains($clientSource, 'FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE'),
'Agent transport did not retain the explicit public-address pinning contract.');
echo "Cloud Agent registry health shadow contract checks passed.\n";