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.
230 lines
13 KiB
PHP
230 lines
13 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Controllers\CloudAdminRuntimeController;
|
|
use App\Models\User;
|
|
use App\Services\Cloud\CloudError;
|
|
use App\Services\Cloud\CloudRuntimeHealthService;
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Slim\Http\Environment;
|
|
use Slim\Http\Request;
|
|
use Slim\Http\Response;
|
|
|
|
$assert = static function (bool $ok, string $message): void {
|
|
if (!$ok) throw new RuntimeException($message);
|
|
};
|
|
$actor = ['user_id' => 42, 'is_admin' => true, 'is_super_admin' => false,
|
|
'can_manage' => false, 'session_id' => ''];
|
|
$System_Config = ['enable_log' => 'true', 'redis_host' => 'localhost'];
|
|
$calls = [];
|
|
$heartbeats = [
|
|
['key' => 'worker', 'label' => '普通任务处理', 'status' => 'busy',
|
|
'message' => '正在处理任务', 'checked_at' => time(), 'last_seen_at' => time(),
|
|
'last_success_at' => null, 'active_count' => 1, 'private_path' => 'MUST_NOT_EXPOSE'],
|
|
['key' => 'policy', 'label' => '策略任务处理', 'status' => 'unknown',
|
|
'message' => '未收到运行记录', 'checked_at' => time()],
|
|
['key' => 'recovery', 'label' => '任务安全收敛', 'status' => 'unknown',
|
|
'message' => '未收到运行记录', 'checked_at' => time()],
|
|
['key' => 'notification', 'label' => '维护邮件投递', 'status' => 'unknown',
|
|
'message' => '未收到运行记录', 'checked_at' => time()],
|
|
['key' => 'lifecycle', 'label' => '到期与流量调度', 'status' => 'warning',
|
|
'message' => '本轮部分检查未完成', 'checked_at' => time()],
|
|
];
|
|
$service = new CloudRuntimeHealthService(
|
|
static function ($connection) use (&$calls): bool { $calls[] = $connection; return true; },
|
|
static function ($received) use (&$calls, $assert, $actor): bool {
|
|
$assert($received === $actor, 'Health check escalated or altered the actor.');
|
|
$calls[] = 'agents'; return true;
|
|
},
|
|
static function () use (&$calls): bool { $calls[] = 'redis'; return true; },
|
|
static fn (): array => $heartbeats
|
|
);
|
|
foreach ([['user_id' => 42, 'is_admin' => false], ['user_id' => 0, 'is_admin' => true], []] as $denied) {
|
|
try {
|
|
$service->snapshot($denied);
|
|
throw new RuntimeException('Unauthorized runtime check was accepted.');
|
|
} catch (CloudError $exception) {
|
|
$assert($exception->httpStatus === 403, 'Wrong unauthorized status.');
|
|
}
|
|
}
|
|
$assert($calls === [], 'Unauthorized requests performed probes.');
|
|
$payload = $service->snapshot($actor);
|
|
$checks = array_column($payload['checks'], null, 'key');
|
|
$assert(count($checks) === 10 && $calls === ['default', 'log', 'agents', 'redis'], 'Missing or duplicate probes.');
|
|
$assert($checks['agents']['status'] === 'ok' && $checks['worker']['status'] === 'busy'
|
|
&& $checks['policy']['status'] === 'unknown'
|
|
&& $checks['vpc']['status'] === 'unknown'
|
|
&& $checks['notification']['status'] === 'unknown'
|
|
&& $checks['lifecycle']['status'] === 'warning',
|
|
'Health or heartbeat semantics changed.');
|
|
$assert(!str_contains(json_encode($payload), 'MUST_NOT_EXPOSE'), 'Heartbeat internal fields leaked.');
|
|
$assert(str_contains($checks['agents']['message'], '不代表宿主机持续健康'),
|
|
'Agent configuration health masquerades as live host connectivity.');
|
|
$assert(str_contains($checks['main_database']['message'], '不代表所有业务表'), 'SQL ping masquerades as data integrity.');
|
|
|
|
$failure = static function () { throw new RuntimeException('SECRET password=credential db=private/path'); };
|
|
$unavailable = new CloudRuntimeHealthService($failure, $failure, $failure, $failure);
|
|
$failed = $unavailable->snapshot($actor);
|
|
$failedChecks = array_column($failed['checks'], null, 'key');
|
|
foreach (['main_database', 'log_database', 'agents', 'redis'] as $key) {
|
|
$assert($failedChecks[$key]['status'] === 'error', $key . ' failed open.');
|
|
}
|
|
$assert($failedChecks['worker']['status'] === 'unknown', 'Missing heartbeat treated as running.');
|
|
$assert(!str_contains(json_encode($failed), 'SECRET') && !str_contains(json_encode($failed), 'credential'),
|
|
'Probe exception secrets leaked.');
|
|
|
|
$System_Config['enable_log'] = 'false';
|
|
$System_Config['redis_host'] = '';
|
|
$calls = [];
|
|
$optional = $service->snapshot($actor);
|
|
$optionalChecks = array_column($optional['checks'], null, 'key');
|
|
$assert($calls === ['default', 'agents'] && $optionalChecks['redis']['status'] === 'disabled'
|
|
&& $optionalChecks['log_database']['status'] === 'disabled', 'Disabled services were probed.');
|
|
$falseProbe = new CloudRuntimeHealthService(static fn () => false, static fn () => false,
|
|
static fn () => false, static fn () => []);
|
|
$assert(array_column($falseProbe->snapshot($actor)['checks'], 'status', 'key')['agents'] === 'error',
|
|
'Unexpected Agent configuration probe result was treated as healthy.');
|
|
$unsupported = new CloudRuntimeHealthService(static fn () => null, static fn () => true,
|
|
static fn () => true, static fn () => []);
|
|
$unsupportedChecks = array_column($unsupported->snapshot($actor)['checks'], null, 'key');
|
|
$assert($unsupportedChecks['main_database']['status'] === 'unknown'
|
|
&& str_contains($unsupportedChecks['main_database']['message'], '未执行连接检查')
|
|
&& $unsupportedChecks['agents']['status'] === 'ok', 'Unsupported timeout support fabricated a database failure.');
|
|
|
|
// Exercise the bounded connection options without requiring a live MySQL server.
|
|
$argumentsMethod = new ReflectionMethod(CloudRuntimeHealthService::class, 'mysqlProbeArguments');
|
|
$mysqlConfig = ['driver' => 'mysql', 'host' => 'db.example.test', 'port' => '3307',
|
|
'database' => 'panel', 'charset' => 'utf8mb4', 'username' => 'probe-user',
|
|
'password' => 'test-only-password', 'options' => [PDO::ATTR_TIMEOUT => 90, PDO::ATTR_PERSISTENT => true]];
|
|
$originalConfig = $mysqlConfig;
|
|
$arguments = $argumentsMethod->invoke(null, $mysqlConfig);
|
|
$assert($arguments[0] === 'mysql:host=db.example.test;port=3307;dbname=panel;charset=utf8mb4'
|
|
&& $arguments[1] === 'probe-user' && $arguments[2] === 'test-only-password', 'MySQL probe lost connection configuration.');
|
|
$assert($arguments[3][PDO::ATTR_TIMEOUT] === 2 && $arguments[3][PDO::ATTR_PERSISTENT] === false
|
|
&& $arguments[3][PDO::ATTR_ERRMODE] === PDO::ERRMODE_EXCEPTION && $mysqlConfig === $originalConfig,
|
|
'Probe reused unbounded/persistent options or modified application configuration.');
|
|
$socketArguments = $argumentsMethod->invoke(null, $mysqlConfig + ['unix_socket' => '/run/mysqld/mysqld.sock']);
|
|
$assert(str_starts_with($socketArguments[0], 'mysql:unix_socket=/run/mysqld/mysqld.sock;dbname=panel'),
|
|
'Unix socket configuration was not preserved.');
|
|
foreach ([['host' => 'db;port=9999'], ['database' => "panel\n"], ['port' => '3306;host=other']] as $invalid) {
|
|
try {
|
|
$argumentsMethod->invoke(null, array_replace($mysqlConfig, $invalid));
|
|
throw new RuntimeException('Invalid probe DSN was accepted.');
|
|
} catch (InvalidArgumentException $expected) {
|
|
}
|
|
}
|
|
|
|
// Use a real writable INI timeout to verify finally restoration even in runtimes
|
|
// without mysqlnd; the production default remains mysqlnd.net_read_timeout.
|
|
$timeoutMethod = new ReflectionMethod(CloudRuntimeHealthService::class, 'withReadTimeout');
|
|
$setting = 'default_socket_timeout';
|
|
$originalTimeout = ini_get($setting);
|
|
$assert($originalTimeout !== false, 'Timeout test setting unavailable.');
|
|
$boundedProbe = static function () use ($assert, $setting): bool {
|
|
$assert(ini_get($setting) === '2', 'Probe did not install its read timeout before connecting.');
|
|
return true;
|
|
};
|
|
$assert($timeoutMethod->invoke(null, $boundedProbe, $setting) === true
|
|
&& ini_get($setting) === $originalTimeout, 'Successful probe changed the request timeout configuration.');
|
|
$assert($timeoutMethod->invoke(null, static fn (): bool => false, $setting) === false
|
|
&& ini_get($setting) === $originalTimeout, 'Failed probe did not restore timeout configuration.');
|
|
try {
|
|
$timeoutMethod->invoke(null, static function (): void { throw new RuntimeException('Expected probe failure'); }, $setting);
|
|
throw new LogicException('Probe exception disappeared.');
|
|
} catch (RuntimeException $expected) {
|
|
$assert($expected->getMessage() === 'Expected probe failure', 'Probe threw an unexpected exception.');
|
|
}
|
|
$assert(ini_get($setting) === $originalTimeout, 'Throwing probe did not restore timeout configuration.');
|
|
$unboundedCalled = false;
|
|
$assert($timeoutMethod->invoke(null, static function () use (&$unboundedCalled): bool {
|
|
$unboundedCalled = true;
|
|
return true;
|
|
}, 'virt_cloud_nonexistent_timeout') === null && !$unboundedCalled,
|
|
'Unavailable timeout fell back to an unbounded connection.');
|
|
|
|
// Exercise the real read-only database probe and controller without network access.
|
|
$capsule = new Capsule();
|
|
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default');
|
|
$capsule->setAsGlobal();
|
|
$capsule->bootEloquent();
|
|
$connection = User::getConnectionResolver()->connection('default');
|
|
$sharedPdo = $connection->getPdo();
|
|
$sharedConfig = $connection->getConfig();
|
|
$busyTimeout = $sharedPdo->query('PRAGMA busy_timeout')->fetchColumn();
|
|
$errorMode = $sharedPdo->getAttribute(PDO::ATTR_ERRMODE);
|
|
$sharedPdo->beginTransaction();
|
|
$connection->enableQueryLog();
|
|
$real = new CloudRuntimeHealthService(null, static fn (): bool => true,
|
|
static fn (): bool => true, static fn (): array => []);
|
|
$assert($real->snapshot($actor)['checks'][0]['status'] === 'ok', 'Real SQLite read probe failed.');
|
|
$queries = $connection->getQueryLog();
|
|
$assert(count($queries) === 1 && $queries[0]['query'] === 'SELECT 1 AS ready', 'Database probe wrote or enumerated data.');
|
|
$assert($connection->getPdo() === $sharedPdo && $connection->getConfig() === $sharedConfig
|
|
&& $sharedPdo->getAttribute(PDO::ATTR_ERRMODE) === $errorMode
|
|
&& $sharedPdo->query('PRAGMA busy_timeout')->fetchColumn() === $busyTimeout
|
|
&& $sharedPdo->inTransaction(), 'Probe mutated or replaced the shared connection or transaction.');
|
|
$sharedPdo->rollBack();
|
|
if (!extension_loaded('pdo_mysql')) {
|
|
$capsule->addConnection($mysqlConfig, 'unsupported_mysql');
|
|
$unopened = $capsule->getConnection('unsupported_mysql');
|
|
$lazyPdo = $unopened->getRawPdo();
|
|
$probeMethod = new ReflectionMethod(CloudRuntimeHealthService::class, 'probeDatabase');
|
|
$assert($lazyPdo instanceof Closure && $probeMethod->invoke(null, 'unsupported_mysql') === null
|
|
&& $unopened->getRawPdo() === $lazyPdo, 'Unsupported MySQL probe opened or changed the shared PDO.');
|
|
}
|
|
$request = Request::createFromEnvironment(Environment::mock([
|
|
'REQUEST_METHOD' => 'GET', 'REQUEST_URI' => '/api/v1/admin/operations/health',
|
|
]))->withAttribute('cloud_actor', $actor);
|
|
$response = (new CloudAdminRuntimeController())->index($request, new Response());
|
|
$assert($response->getStatusCode() === 200
|
|
&& $response->getHeaderLine('Cache-Control') === 'private, no-store', 'Runtime response cached or unavailable.');
|
|
$decoded = json_decode((string) $response->getBody(), true);
|
|
$assert(count($decoded['checks']) === 10
|
|
&& array_column($decoded['checks'], 'status', 'key')['agents'] === 'error',
|
|
'Partial failure lost other checks or fabricated Agent configuration health.');
|
|
$routes = file_get_contents(dirname(__DIR__) . '/config/routes_cloud.php');
|
|
$middleware = file_get_contents(dirname(__DIR__) . '/app/Middleware/CloudApi.php');
|
|
$assert(str_contains($routes, "get('/admin/operations/health'")
|
|
&& str_contains($middleware, '#^/api/v1/admin/operations(?:/|$)#D'),
|
|
'Health route missing or creates privileged cloud sessions.');
|
|
|
|
// Exercise the default Agent probe against persisted Agent readiness only.
|
|
$schema = $capsule->schema('default');
|
|
$schema->create('cloud_hosts', static function (Blueprint $table): void {
|
|
$table->integer('id')->primary();
|
|
$table->boolean('enabled');
|
|
$table->string('default_network');
|
|
});
|
|
$schema->create('cloud_host_agents', static function (Blueprint $table): void {
|
|
$table->integer('host_id')->primary();
|
|
$table->boolean('enabled');
|
|
$table->integer('revision');
|
|
$table->integer('tested_revision');
|
|
$table->string('last_test_status');
|
|
});
|
|
$agentHealth = new CloudRuntimeHealthService(static fn (): bool => true, null,
|
|
static fn (): bool => true, static fn (): array => []);
|
|
$agentStatus = static fn (): string => array_column(
|
|
$agentHealth->snapshot($actor)['checks'], 'status', 'key'
|
|
)['agents'];
|
|
$assert($agentStatus() === 'ok', 'No enabled hosts were treated as unready.');
|
|
Capsule::table('cloud_hosts')->insert([
|
|
'id' => 1, 'enabled' => 1, 'default_network' => 'bridge:br0',
|
|
]);
|
|
Capsule::table('cloud_host_agents')->insert([
|
|
'host_id' => 1, 'enabled' => 1, 'revision' => 2,
|
|
'tested_revision' => 2, 'last_test_status' => 'healthy',
|
|
]);
|
|
foreach (['bridge:br0', 'public', 'network:default'] as $network) {
|
|
Capsule::table('cloud_hosts')->where('id', 1)->update(['default_network' => $network]);
|
|
$assert($agentStatus() === 'ok',
|
|
'Compatibility default still controls runtime readiness: ' . $network);
|
|
}
|
|
Capsule::table('cloud_host_agents')->where('host_id', 1)->update(['revision' => 3]);
|
|
$assert($agentStatus() === 'error', 'Compatibility metadata bypassed the tested revision gate.');
|
|
Capsule::table('cloud_hosts')->where('id', 1)->update(['enabled' => 0, 'default_network' => 'public']);
|
|
$assert($agentStatus() === 'ok', 'A disabled legacy host failed the enabled-host health check.');
|
|
echo "Runtime health, bounded database options, timeout restoration, shared-connection isolation, auth and privacy passed.\n";
|