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

99 lines
5.7 KiB
PHP

<?php
$root = dirname(__DIR__);
$read = static fn (string $path): string => file_get_contents($root . '/' . $path);
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$schema = $read('sql/cloud.sql');
$routes = $read('config/routes.php') . "\n" . $read('config/routes_cloud.php');
$middleware = $read('app/Middleware/CloudApi.php');
$controller = $read('app/Controllers/CloudAdminAgentController.php');
$apiController = $read('app/Controllers/CloudApiController.php');
$service = $read('app/Services/Cloud/CloudAgentSettingsService.php');
$cipher = $read('app/Services/Cloud/CloudAgentSecretCipher.php');
$client = $read('app/Services/Cloud/CloudAgentClient.php');
$gatewayEndpoint = $read('app/Services/Cloud/CloudAgentGatewayEndpoint.php');
$page = $read('resources/views/venix/admin/cloud/agents.tpl');
$sidebar = $read('resources/views/venix/admin/static_header.tpl');
$browser = $read('public/cloud/agent-settings.js');
$runtime = $read('public/cloud/runtime.js');
$assert(str_contains($schema, 'CREATE TABLE IF NOT EXISTS cloud_host_agents')
&& str_contains($schema, 'sealed_api_secret LONGTEXT NOT NULL')
&& !str_contains($schema, 'endpoint VARCHAR(255)')
&& str_contains($schema, 'revision BIGINT UNSIGNED NOT NULL DEFAULT 1')
&& str_contains($schema, 'tested_revision BIGINT UNSIGNED NOT NULL DEFAULT 0')
&& str_contains($schema, 'CREATE TABLE IF NOT EXISTS cloud_templates')
&& str_contains($schema, 'startup_script MEDIUMTEXT NOT NULL')
&& str_contains($schema, 'generation_token CHAR(32)')
&& str_contains($schema, 'UNIQUE KEY cloud_template_generation'),
'Fresh schema is missing PHP-owned Agent settings or template CAS data.');
$assert(str_contains($routes,
"get('/agents', 'App\\Controllers\\Admin\\CloudPageController:agents')")
&& str_contains($routes, "get('/admin/agents'")
&& str_contains($routes, "'/admin/agents/{host_id:[1-9][0-9]*}/test'")
&& str_contains($routes, "'/admin/cloud-templates/{id}/startup-script'")
&& str_contains($middleware, '/api/v1/admin/agents')
&& str_contains($middleware, '/startup-script'),
'Agent settings or startup-script routes lost their database-only/CSRF boundary.');
$assert(str_contains($routes, "'/vms/{id}/rescue/force-start'")
&& str_contains($apiController, "elseif (\$action === '/force-start')")
&& str_contains($apiController, "\$suffix = '/rescue/force-start'")
&& str_contains($apiController, "\$body = ['ack_data_loss' => true]")
&& str_contains($runtime,
"if (action === 'load') { body.template_id = byId('rescue-template-id').value; body.ack_data_loss = true; }")
&& str_contains($runtime,
"if (action === 'force-start') body.ack_data_loss = true"),
'Rescue entry or force-start lost its explicit data-loss acknowledgement contract.');
$assert(str_contains($controller, '$this->actor($request, false)')
&& substr_count($controller, '$this->actor($request, true)') >= 1
&& str_contains($controller, 'CloudAccess::manage($actor)'),
'Ordinary-admin reads and super-admin Agent mutations are not separated.');
$assert(str_contains($cipher, "CloudSecrets::key('host-agent-secret')")
&& str_contains($cipher, "private const PREFIX = 'v1.xc20p.agent.'")
&& !str_contains($cipher, 'engine_hmac')
&& str_contains($service, "'secret_configured'")
&& !str_contains($service, "'api_secret_cipher' =>")
&& !str_contains($service, "'api_secret' =>"),
'Agent secrets do not stay behind the PHP payload-key and redaction boundary.');
$assert(str_contains($service, '$setting->tested_revision = 0')
&& str_contains($service, "'tested_revision' => \$status === 'healthy'")
&& str_contains($service, 'if (!$setting->isReady())')
&& str_contains($service, "'agent_not_tested'")
&& str_contains($service, "'ready' => \$ready")
&& str_contains($browser, '未就绪,不参与调度')
&& str_contains($page, '全部通过才标记就绪'),
'Saved Agent revisions can become schedulable before current-revision tests pass.');
$assert(str_contains($client, "'X-Virt-Timestamp'")
&& str_contains($client, "'X-Virt-Content-SHA256'")
&& str_contains($client, "'X-Virt-Signature'")
&& str_contains($client, "'allow_redirects' => false")
&& str_contains($client, "'proxy' => ''")
&& str_contains($client, "'agent_id' => \$this->agentId")
&& str_contains($client, 'CloudAgentGatewayEndpoint::PATH')
&& !str_contains($client, 'CloudEngineClient'),
'CloudAgentClient lost the reviewed signed transport boundary.');
$assert(str_contains($gatewayEndpoint, "public const PATH = '/internal/agent/request'")
&& str_contains($gatewayEndpoint, "public const URL = 'http://127.0.0.1:8001'")
&& str_contains($gatewayEndpoint, "\$host !== '127.0.0.1'")
&& str_contains($gatewayEndpoint, "!== 8001"),
'PHP can dispatch Agent commands outside the fixed loopback gateway.');
$assert(str_contains($page, 'data-cloud-agent-settings')
&& str_contains($page, 'cloud_data_key')
&& str_contains($page, 'id="agent-settings-agent-id"')
&& str_contains($page, 'id="agent-settings-secret"')
&& str_contains($page, '只有超级管理员')
&& str_contains($page, '主动建立 WSS')
&& !str_contains($page, 'agent-settings-endpoint')
&& str_contains($sidebar, 'href="/admin/agents"')
&& str_contains($browser, "'/api/v1/admin/agents'")
&& str_contains($browser, "'X-Admin-CSRF-Token'")
&& str_contains($browser, "byId('agent-settings-secret').value = ''"),
'Agent settings page lost navigation, permission, CSRF or secret clearing behavior.');
echo "Agent API settings schema, routes, permissions and secret boundary passed.\n";