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.
92 lines
3.5 KiB
PHP
92 lines
3.5 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Services\Cloud\CloudAgentClient;
|
|
use App\Services\Cloud\CloudAgentError;
|
|
use GuzzleHttp\Client;
|
|
use GuzzleHttp\Handler\MockHandler;
|
|
use GuzzleHttp\HandlerStack;
|
|
use GuzzleHttp\Psr7\Response;
|
|
|
|
$assert = static function ($condition, string $message): void {
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
};
|
|
|
|
$cases = [
|
|
[400, 'invalid_request', 422, false, 'Agent 拒绝了请求参数'],
|
|
[422, 'validation_failed', 422, false, 'Agent 拒绝了请求参数'],
|
|
[401, 'unauthorized', 502, false, 'Agent API 身份验证失败,请重新核对密钥'],
|
|
[403, 'forbidden', 403, false, 'Agent 拒绝了未授权的操作请求'],
|
|
[403, 'gateway_forbidden', 502, false, 'Agent 网关身份验证失败,请重新核对网关密钥'],
|
|
[404, 'vm_not_found', 404, false, 'Agent 上的目标资源不存在'],
|
|
[409, 'operation_conflict', 409, false, 'Agent 拒绝冲突或重复的操作请求'],
|
|
[501, 'not_implemented', 501, false, '当前 Agent 不支持此项功能'],
|
|
[503, 'libvirt_unavailable', 503, true, '宿主机 Agent 或虚拟化后端暂不可用'],
|
|
[500, 'internal_error', 502, true, '宿主机 Agent 操作未成功'],
|
|
];
|
|
|
|
$responses = [];
|
|
foreach ($cases as [$status, $code]) {
|
|
$responses[] = new Response($status, ['Content-Type' => 'application/json'],
|
|
json_encode(['error' => [
|
|
'code' => $code,
|
|
'message' => 'Traceback: /root/private/key secret=never-expose',
|
|
]], JSON_THROW_ON_ERROR));
|
|
}
|
|
$client = new CloudAgentClient(
|
|
'1',
|
|
'test-secret-which-is-at-least-32-bytes',
|
|
new Client(['handler' => HandlerStack::create(new MockHandler($responses))]),
|
|
20.0,
|
|
'http://127.0.0.1:8001/internal/agent/request',
|
|
'test-gateway-secret-at-least-32-bytes'
|
|
);
|
|
$actor = [
|
|
'user_id' => 1,
|
|
'is_admin' => true,
|
|
'is_super_admin' => true,
|
|
'can_manage' => true,
|
|
];
|
|
|
|
foreach ($cases as [$status, $code, $panelStatus, $retryable, $message]) {
|
|
try {
|
|
$client->request('POST', '/v1/vms/test-operation', $actor, []);
|
|
throw new RuntimeException('Agent error response was accepted: ' . $status);
|
|
} catch (CloudAgentError $error) {
|
|
$assert($error->agentCode === $code
|
|
&& $error->agentHttpStatus === $status
|
|
&& $error->httpStatus === $panelStatus
|
|
&& $error->retryable === $retryable
|
|
&& $error->getMessage() === $message
|
|
&& !str_contains($error->getMessage(), '/root')
|
|
&& !str_contains($error->getMessage(), 'never-expose'),
|
|
'Agent status mapping or error redaction changed: ' . $status);
|
|
}
|
|
}
|
|
|
|
$fallback = new CloudAgentClient(
|
|
'1',
|
|
'test-secret-which-is-at-least-32-bytes',
|
|
new Client(['handler' => HandlerStack::create(new MockHandler([
|
|
new Response(400, ['Content-Type' => 'application/json'], json_encode([
|
|
'error' => ['code' => '../unsafe', 'message' => 'secret'],
|
|
], JSON_THROW_ON_ERROR)),
|
|
]))]),
|
|
20.0,
|
|
'http://127.0.0.1:8001/internal/agent/request',
|
|
'test-gateway-secret-at-least-32-bytes'
|
|
);
|
|
try {
|
|
$fallback->request('POST', '/v1/vms/test-operation', $actor, []);
|
|
throw new RuntimeException('Invalid Agent error code was accepted.');
|
|
} catch (CloudAgentError $error) {
|
|
$assert($error->agentCode === 'agent_http_error'
|
|
&& $error->httpStatus === 422,
|
|
'Invalid Agent error code did not fall back to a stable panel code.');
|
|
}
|
|
|
|
echo "Cloud Agent error-code and redaction contract passed.\n";
|