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

50 lines
2.8 KiB
PHTML

<?php
require dirname(__DIR__) . '/app/Services/Config.php';
require dirname(__DIR__) . '/app/Command/CloudSetup.php';
use App\Command\CloudSetup;
define('BASE_PATH', dirname(__DIR__));
$fixture = sys_get_temp_dir() . '/virt-cloud-export-test-' . bin2hex(random_bytes(8));
mkdir($fixture, 0700);
$System_Config = [
'enable_log' => 'true', 'db_driver' => 'mysql', 'db_host' => '127.0.0.1',
'db_port' => '3307', 'db_database' => 'business_fixture', 'db_username' => 'fixture',
'db_password' => 'not-a-real-password', 'db_charset' => 'utf8mb4', 'db_collation' => 'utf8mb4_unicode_ci',
'db_prefix' => '', 'log_db_host' => '127.0.0.2', 'log_db_port' => 3308,
'log_db_database' => 'metrics_fixture', 'log_db_user' => 'fixture_log', 'log_db_password' => '',
'cloud_engine_hmac_file' => $fixture . '/engine.hmac', 'cloud_payload_key_file' => $fixture . '/payload.key',
'cloud_engine_data_dir' => $fixture . '/engine', 'cloud_engine_config_file' => $fixture . '/engine.json',
];
$assert = static function ($condition, $message) {
if (!$condition) throw new RuntimeException($message);
};
try {
ob_start();
CloudSetup::exportEngineConfig();
$output = ob_get_clean();
$config = json_decode(file_get_contents($fixture . '/engine.json'), true, 32, JSON_THROW_ON_ERROR);
$hmac = trim(file_get_contents($fixture . '/engine.hmac'));
$payload = trim(file_get_contents($fixture . '/payload.key'));
$assert($config['main_database']['port'] === 3307 && $config['log_database']['port'] === 3308, 'Export lost database ports');
$assert($config['log_database']['database'] === 'metrics_fixture', 'Metrics must use the log database');
$assert($config['project_dir'] === dirname(BASE_PATH), 'Engine update root must be the repository root');
$assert($config['update_php_fpm_unit'] === 'php-fpm.service', 'Update service must export the default PHP-FPM unit');
$assert(strlen(base64_decode($hmac, true)) === 32
&& base64_encode(base64_decode($hmac, true)) === $hmac
&& $hmac !== $payload, 'Signing and encryption keys must be canonical base64 and separate');
$assert($config['service_secret'] === $hmac && !str_contains($output, $hmac) && !str_contains($output, 'not-a-real-password'), 'Export printed a credential');
ob_start();
CloudSetup::exportEngineConfig();
ob_end_clean();
$assert(trim(file_get_contents($fixture . '/engine.hmac')) === $hmac, 'Repeated initialization rotated a live key');
$assert((fileperms($fixture . '/engine.json') & 0777) === 0600, 'Engine config permissions must be 0600');
echo "Private engine export, separate metric connection and non-rotating keys passed.\n";
} finally {
foreach (['engine.hmac', 'payload.key', 'engine.json'] as $file) {
if (is_file($fixture . '/' . $file)) unlink($fixture . '/' . $file);
}
rmdir($fixture);
}