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

87 lines
3.2 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudUpdateHelperClient;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Response;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$history = [];
$mock = new MockHandler([
new Response(200, ['Content-Type' => 'application/json'], json_encode([
'repository' => 'https://g.luoli.live/root/virt.git',
'branch' => 'go',
'checkout_ready' => true,
'token_configured' => false,
'maintenance_lock' => false,
], JSON_THROW_ON_ERROR)),
new Response(409, ['Content-Type' => 'application/json'], json_encode([
'error' => ['code' => 'update_precondition_failed'],
], JSON_THROW_ON_ERROR)),
]);
$stack = HandlerStack::create($mock);
$stack->push(Middleware::history($history));
$secret = 'update-helper-test-secret-at-least-32-bytes';
$client = new CloudUpdateHelperClient(
new Client(['handler' => $stack]),
$secret
);
$actor = [
'user_id' => 7,
'is_admin' => true,
'is_super_admin' => true,
'can_manage' => true,
'session_id' => 'must-not-cross-the-local-helper-boundary',
];
$settings = $client->request('GET', '/v1/settings', $actor);
$assert(($settings['branch'] ?? null) === 'go' && count($history) === 1,
'Update helper settings response did not round-trip.');
$request = $history[0]['request'];
$assert((string) $request->getUri() === 'http://localhost/v1/settings'
&& $request->getHeaderLine('X-Virt-Content-SHA256') === hash('sha256', '')
&& preg_match('/^[a-f0-9]{64}$/D',
$request->getHeaderLine('X-Virt-Signature')) === 1
&& $request->getHeaderLine('X-Virt-Actor') !== ''
&& $request->getHeaderLine('X-Cloud-Signature') === '',
'Update helper request did not use the fixed signed X-Virt contract.');
$encoded = $request->getHeaderLine('X-Virt-Actor');
$raw = base64_decode(strtr($encoded, '-_', '+/')
. str_repeat('=', (4 - strlen($encoded) % 4) % 4), true);
$forwardedActor = json_decode((string) $raw, true, 8, JSON_THROW_ON_ERROR);
$assert(!array_key_exists('session_id', $forwardedActor)
&& ($forwardedActor['is_super_admin'] ?? false) === true,
'Panel session data crossed into the privileged update helper.');
try {
$client->request('POST', '/v1/apply', $actor, [
'expected_current_commit' => str_repeat('a', 40),
'expected_remote_commit' => str_repeat('b', 40),
]);
throw new RuntimeException('Unsafe update precondition was accepted.');
} catch (CloudError $error) {
$assert($error->httpStatus === 409
&& !str_contains($error->getMessage(), 'update_precondition_failed'),
'Update helper error was not safely mapped.');
}
foreach (['/internal/v1/update', '/v1/jobs/../settings', '/v1/check?x=1'] as $unsafe) {
try {
$client->request('GET', $unsafe, $actor);
throw new RuntimeException('Unsafe helper target accepted: ' . $unsafe);
} catch (InvalidArgumentException $expected) {
// Expected before transport.
}
}
echo "Authenticated fixed Unix-socket update helper client contract passed.\n";