|
|
<?php
|
|
|
|
|
|
namespace App\Services {
|
|
|
final class Config
|
|
|
{
|
|
|
public static function get($name, $default = null)
|
|
|
{
|
|
|
return $name === 'cloud_engine_url' ? 'http://127.0.0.1:8000' : $default;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
namespace App\Services\Cloud {
|
|
|
final class CloudSecrets
|
|
|
{
|
|
|
public static function key(string $purpose): string
|
|
|
{
|
|
|
return str_repeat('k', 32);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
namespace GuzzleHttp {
|
|
|
final class HostErrorTestResponse
|
|
|
{
|
|
|
public function __construct(private int $status, private string $body)
|
|
|
{
|
|
|
}
|
|
|
|
|
|
public function getStatusCode(): int
|
|
|
{
|
|
|
return $this->status;
|
|
|
}
|
|
|
|
|
|
public function getBody(): string
|
|
|
{
|
|
|
return $this->body;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
class Client
|
|
|
{
|
|
|
public function __construct(private array $responses = [])
|
|
|
{
|
|
|
}
|
|
|
|
|
|
public function request(string $method, string $url, array $options): HostErrorTestResponse
|
|
|
{
|
|
|
$response = array_shift($this->responses);
|
|
|
if (!$response instanceof HostErrorTestResponse) {
|
|
|
throw new \RuntimeException('No fake engine response was queued.');
|
|
|
}
|
|
|
return $response;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
namespace {
|
|
|
use App\Services\Cloud\CloudEngineClient;
|
|
|
use App\Services\Cloud\CloudError;
|
|
|
use GuzzleHttp\Client;
|
|
|
use GuzzleHttp\HostErrorTestResponse;
|
|
|
|
|
|
require dirname(__DIR__) . '/app/Services/Cloud/CloudError.php';
|
|
|
require dirname(__DIR__) . '/app/Services/Cloud/CloudEngineClient.php';
|
|
|
|
|
|
$assert = static function ($condition, string $message): void {
|
|
|
if (!$condition) {
|
|
|
throw new RuntimeException($message);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
$messages = [
|
|
|
'host_configuration_invalid' => '宿主机配置无效,请检查地址、端口、存储目录和默认网络',
|
|
|
'host_private_key_required' => '首次添加宿主机必须提供现有 SSH 私钥',
|
|
|
'host_private_key_format_invalid' => 'SSH 私钥格式无效,请提供 PEM 或 OpenSSH 格式私钥',
|
|
|
'host_private_key_passphrase_required' => 'SSH 私钥已加密,请填写私钥解锁口令',
|
|
|
'host_private_key_invalid' => 'SSH 私钥或解锁口令无效,请检查后重试',
|
|
|
'host_public_pool_invalid' => '公网地址池配置无效,请完整填写可路由的 IPv4 网段、网关和 DNS(网段前缀 /24 至 /30)',
|
|
|
'host_public_gateway_invalid' => '公网网关无效,必须位于所配置的公网地址池内',
|
|
|
'host_public_dns_invalid' => '公网 DNS 无效,请填写最多 4 个可路由的 IPv4 地址',
|
|
|
'host_public_reserved_ip_invalid' => '保留 IP 配置无效,请填写地址池内的 IPv4 地址,最多 254 个',
|
|
|
'host_vm_prefix_invalid' => '客户机网络前缀无效,必须介于 /24 和地址池前缀之间',
|
|
|
];
|
|
|
|
|
|
$allowlisted = new ReflectionMethod(CloudEngineClient::class, 'safeAllowlistedError');
|
|
|
foreach ($messages as $code => $message) {
|
|
|
foreach ([400, 409, 422] as $status) {
|
|
|
foreach (['/internal/v1/hosts/1/host', '/internal/v1/hosts/42/host/test'] as $path) {
|
|
|
$actual = $allowlisted->invoke(null, $path, [
|
|
|
'error_code' => $code,
|
|
|
'error' => 'Traceback: host=/root/private key=secret',
|
|
|
], $status);
|
|
|
$assert($actual === $message,
|
|
|
'A reviewed host error code was not mapped on its fixed route: ' . $code);
|
|
|
$assert(!str_contains($actual, 'private') && !str_contains($actual, '/root'),
|
|
|
'An arbitrary host error body crossed the PHP trust boundary.');
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
foreach ([
|
|
|
['/internal/v1/hosts/1/host', ['error_code' => 'unknown_host_failure', 'error' => 'secret']],
|
|
|
['/internal/v1/hosts/1/host', ['error_code' => ['host_configuration_invalid'], 'error' => 'secret']],
|
|
|
['/internal/v1/hosts/0/host', ['error_code' => 'host_configuration_invalid']],
|
|
|
['/internal/v1/hosts/hk/host', ['error_code' => 'host_configuration_invalid']],
|
|
|
['/internal/v1/hosts/1/host/extra', ['error_code' => 'host_configuration_invalid']],
|
|
|
['/internal/v1/hosts/1/host?unexpected=true', ['error_code' => 'host_configuration_invalid']],
|
|
|
['/internal/v1/hosts/1/vms', ['error_code' => 'host_configuration_invalid']],
|
|
|
] as [$path, $payload]) {
|
|
|
$assert($allowlisted->invoke(null, $path, $payload, 400) === null,
|
|
|
'An unknown code or similar host path crossed the PHP trust boundary.');
|
|
|
}
|
|
|
|
|
|
foreach ([500, 502, 503] as $status) {
|
|
|
$assert($allowlisted->invoke(null, '/internal/v1/hosts/1/host', [
|
|
|
'error_code' => 'host_configuration_invalid',
|
|
|
'error' => 'Traceback: host=/root/private key=secret',
|
|
|
], $status) === null,
|
|
|
'A host 5xx response was incorrectly exposed as a client configuration error.');
|
|
|
}
|
|
|
|
|
|
$actor = ['user_id' => 1, 'is_admin' => true, 'can_manage' => true];
|
|
|
$knownClient = new CloudEngineClient(new Client([
|
|
|
new HostErrorTestResponse(400, json_encode([
|
|
|
'error_code' => 'host_public_gateway_invalid',
|
|
|
'error' => 'Traceback: host=/root/private key=secret',
|
|
|
], JSON_UNESCAPED_UNICODE)),
|
|
|
]));
|
|
|
try {
|
|
|
$knownClient->request('PUT', '/internal/v1/hosts/1/host', $actor, []);
|
|
|
throw new RuntimeException('A reviewed host validation failure did not raise CloudError.');
|
|
|
} catch (CloudError $error) {
|
|
|
$assert($error->getMessage() === $messages['host_public_gateway_invalid']
|
|
|
&& $error->httpStatus === 400
|
|
|
&& !str_contains($error->getMessage(), 'private'),
|
|
|
'A reviewed host error lost its fixed message or original 400 status.');
|
|
|
}
|
|
|
|
|
|
$validationClient = new CloudEngineClient(new Client([
|
|
|
new HostErrorTestResponse(422, json_encode([
|
|
|
'error_code' => 'host_configuration_invalid',
|
|
|
'detail' => [['input' => '-----BEGIN OPENSSH PRIVATE KEY----- secret']],
|
|
|
], JSON_UNESCAPED_UNICODE)),
|
|
|
]));
|
|
|
try {
|
|
|
$validationClient->request('POST', '/internal/v1/hosts/9/host/test', $actor, []);
|
|
|
throw new RuntimeException('A reviewed host payload failure did not raise CloudError.');
|
|
|
} catch (CloudError $error) {
|
|
|
$assert($error->getMessage() === $messages['host_configuration_invalid']
|
|
|
&& $error->httpStatus === 422
|
|
|
&& !str_contains($error->getMessage(), 'PRIVATE KEY'),
|
|
|
'A reviewed host error lost its fixed message or original 422 status.');
|
|
|
}
|
|
|
|
|
|
$unknownClient = new CloudEngineClient(new Client([
|
|
|
new HostErrorTestResponse(400, json_encode([
|
|
|
'error_code' => 'host_traceback',
|
|
|
'error' => 'Traceback: host=/root/private key=secret',
|
|
|
], JSON_UNESCAPED_UNICODE)),
|
|
|
]));
|
|
|
try {
|
|
|
$unknownClient->request('PUT', '/internal/v1/hosts/1/host', $actor, []);
|
|
|
throw new RuntimeException('An unknown host failure did not raise CloudError.');
|
|
|
} catch (CloudError $error) {
|
|
|
$assert($error->getMessage() === '虚拟化操作未成功(引擎状态 400)'
|
|
|
&& !str_contains($error->getMessage(), 'private')
|
|
|
&& !str_contains($error->getMessage(), '/root'),
|
|
|
'An unknown host error escaped instead of the generic failure.');
|
|
|
}
|
|
|
|
|
|
$serverFailureClient = new CloudEngineClient(new Client([
|
|
|
new HostErrorTestResponse(500, json_encode([
|
|
|
'error_code' => 'host_configuration_invalid',
|
|
|
'error' => 'Traceback: host=/root/private key=secret',
|
|
|
], JSON_UNESCAPED_UNICODE)),
|
|
|
]));
|
|
|
try {
|
|
|
$serverFailureClient->request('PUT', '/internal/v1/hosts/1/host', $actor, []);
|
|
|
throw new RuntimeException('A host engine failure did not raise CloudError.');
|
|
|
} catch (CloudError $error) {
|
|
|
$assert($error->getMessage() === '虚拟化操作未成功(引擎状态 500)'
|
|
|
&& $error->httpStatus === 502
|
|
|
&& !str_contains($error->getMessage(), 'private')
|
|
|
&& !str_contains($error->getMessage(), '/root'),
|
|
|
'A host 5xx response crossed the fixed-message and 502 trust boundary.');
|
|
|
}
|
|
|
|
|
|
echo "Cloud host engine error-code contract passed.\n";
|
|
|
}
|