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

81 lines
3.3 KiB
PHTML

<?php
// Dependency-free source/behavior contract for the host-local Agent key
// resolver. It intentionally does not require PHP/Composer in the audit
// environment.
require dirname(__DIR__) . '/app/Services/Cloud/CloudAgentSecretResolver.php';
require dirname(__DIR__) . '/app/Services/Cloud/CloudAgentFileSecretResolver.php';
use App\Services\Cloud\CloudAgentFileSecretResolver;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$rawKey = str_repeat('k', 32);
$encodedKey = base64_encode($rawKey);
$files = [
'/run/virt-cloud/agents/agent-103.hmac' => $encodedKey . "\n",
'/run/virt-cloud/agents/agent-103.ca.pem' => 'CA',
'/run/virt-cloud/agents/agent-103.crt' => 'CERT',
'/run/virt-cloud/agents/agent-103.key' => 'KEY',
];
$resolver = new CloudAgentFileSecretResolver(
'/run/virt-cloud/agents',
static fn (string $path): ?string => $files[$path] ?? null,
);
$material = $resolver->resolve('agent-103');
$assert(is_array($material) && hash_equals($rawKey, $material['hmac_key'] ?? ''),
'The resolver did not decode the canonical base64 key into raw bytes.');
$assert(($material['cert_file'] ?? '') === '/run/virt-cloud/agents/agent-103.crt'
&& ($material['key_file'] ?? '') === '/run/virt-cloud/agents/agent-103.key',
'The resolver returned unexpected TLS paths.');
foreach (['agent/103', '../agent-103', ' agent-103', '', 'agent-103\n'] as $invalid) {
$assert($resolver->resolve($invalid) === null,
'Invalid Agent ID was accepted: ' . json_encode($invalid));
}
$badFiles = $files;
$badFiles['/run/virt-cloud/agents/agent-103.hmac'] = $encodedKey . " \n";
$bad = new CloudAgentFileSecretResolver(
'/run/virt-cloud/agents',
static fn (string $path): ?string => $badFiles[$path] ?? null,
);
$assert($bad->resolve('agent-103') === null,
'The resolver accepted ambiguous whitespace in the key file.');
$nonCanonical = $files;
$nonCanonical['/run/virt-cloud/agents/agent-103.hmac'] = rtrim($encodedKey, '=') . "\n";
$nonCanonicalResolver = new CloudAgentFileSecretResolver(
'/run/virt-cloud/agents',
static fn (string $path): ?string => $nonCanonical[$path] ?? null,
);
$assert($nonCanonicalResolver->resolve('agent-103') === null,
'The resolver accepted non-canonical Base64 padding.');
foreach (['relative/agents', '/', '/run//virt-cloud/agents', '/run/../agents', '/run/virt cloud/agents'] as $directory) {
try {
new CloudAgentFileSecretResolver($directory);
throw new RuntimeException('Unsafe secret directory was accepted: ' . $directory);
} catch (InvalidArgumentException) {
// expected
}
}
$source = file_get_contents(dirname(__DIR__) . '/app/Services/Cloud/CloudAgentFileSecretResolver.php');
$assert(is_string($source)
&& str_contains($source, 'base64_decode')
&& str_contains($source, 'lstat')
&& str_contains($source, 'validAbsoluteDirectory')
&& str_contains($source, 'hash_equals')
&& str_contains($source, 'CloudAgentSecretResolver')
&& !str_contains($source, 'CloudSecrets')
&& !str_contains($source, 'CloudEngineClient')
&& !str_contains($source, 'ssh'),
'The file resolver crossed the legacy secret or SSH boundary.');
fwrite(STDOUT, "Cloud Agent file secret resolver contract passed.\n");