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.
246 lines
8.9 KiB
PHP
246 lines
8.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services {
|
|
final class Config
|
|
{
|
|
public static function get($name, $default = null)
|
|
{
|
|
return $name === 'cloud_data_key'
|
|
? base64_encode(str_repeat("\x31", 32))
|
|
: ($name === 'super_admin' ? '99' : $default);
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace App\Services\Cloud {
|
|
final class CloudSessionService
|
|
{
|
|
public static bool $active = true;
|
|
public static function active(
|
|
string $id,
|
|
?int $hostId = null,
|
|
?string $uuid = null
|
|
): bool {
|
|
return self::$active
|
|
&& $id === str_repeat('a', 64)
|
|
&& $hostId === 9
|
|
&& $uuid === '11111111-1111-4111-8111-111111111111';
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace {
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Models\CloudConsoleTicket;
|
|
use App\Models\CloudHost;
|
|
use App\Models\CloudHostAgent;
|
|
use App\Models\CloudInstance;
|
|
use App\Models\CloudSession;
|
|
use App\Services\Cloud\CloudConsoleTicketService;
|
|
use App\Services\Cloud\CloudError;
|
|
use App\Services\Cloud\CloudSessionService;
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
|
|
$assert = static function (bool $condition, string $message): void {
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
};
|
|
$capsule = new Capsule();
|
|
$capsule->addConnection([
|
|
'driver' => 'sqlite',
|
|
'database' => ':memory:',
|
|
], 'default');
|
|
$capsule->setAsGlobal();
|
|
$capsule->bootEloquent();
|
|
$schema = $capsule->schema('default');
|
|
$schema->create('cloud_sessions', static function (Blueprint $table): void {
|
|
$table->string('id')->primary();
|
|
$table->integer('user_id');
|
|
$table->integer('expires_at');
|
|
});
|
|
$schema->create('cloud_instances', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->integer('host_id');
|
|
$table->integer('user_id');
|
|
$table->string('uuid');
|
|
});
|
|
$schema->create('cloud_host_agents', static function (Blueprint $table): void {
|
|
$table->integer('host_id')->primary();
|
|
$table->integer('enabled');
|
|
$table->integer('revision');
|
|
$table->integer('tested_revision');
|
|
$table->string('last_test_status');
|
|
$table->text('network_selectors_json');
|
|
});
|
|
$schema->create('cloud_hosts', static function (Blueprint $table): void {
|
|
$table->integer('id')->primary();
|
|
$table->string('default_network');
|
|
});
|
|
$schema->create('cloud_console_tickets', static function (Blueprint $table): void {
|
|
$table->string('ticket_hash')->primary();
|
|
$table->string('session_id');
|
|
$table->integer('user_id');
|
|
$table->integer('instance_id');
|
|
$table->integer('host_id');
|
|
$table->string('vm_uuid');
|
|
$table->text('sealed_agent_ticket');
|
|
$table->integer('agent_expires_at');
|
|
$table->integer('ticket_expires_at');
|
|
$table->integer('connection_expires_at');
|
|
$table->integer('consumed_at')->nullable();
|
|
$table->string('connection_id')->nullable();
|
|
$table->integer('created_at');
|
|
});
|
|
|
|
$session = new CloudSession();
|
|
$session->id = str_repeat('a', 64);
|
|
$session->user_id = 17;
|
|
$session->expires_at = time() + 3600;
|
|
$session->save();
|
|
$instance = new CloudInstance();
|
|
$instance->id = 42;
|
|
$instance->host_id = 9;
|
|
$instance->user_id = 17;
|
|
$instance->uuid = '11111111-1111-4111-8111-111111111111';
|
|
$instance->save();
|
|
$agent = new CloudHostAgent();
|
|
$agent->host_id = 9;
|
|
$agent->enabled = 1;
|
|
$agent->revision = 3;
|
|
$agent->tested_revision = 3;
|
|
$agent->last_test_status = 'healthy';
|
|
$agent->network_selectors_json = '["bridge:br0"]';
|
|
$agent->save();
|
|
$host = new CloudHost();
|
|
$host->id = 9;
|
|
$host->default_network = 'bridge:br0';
|
|
$host->save();
|
|
|
|
$agentSecretTicket = rtrim(strtr(base64_encode(str_repeat('z', 32)), '+/', '-_'), '=');
|
|
$agentExpiry = time() + 60;
|
|
try {
|
|
CloudConsoleTicketService::issue($instance, [
|
|
'user_id' => 17,
|
|
'session_id' => str_repeat('a', 64),
|
|
], [
|
|
'ticket' => $agentSecretTicket,
|
|
'expires_at' => gmdate('Y-m-d\\TH:i:s\\Z', $agentExpiry),
|
|
'url' => '/v1/console',
|
|
]);
|
|
throw new RuntimeException('Retired direct-console URL was accepted.');
|
|
} catch (CloudError $error) {
|
|
$assert($error->httpStatus === 502,
|
|
'Retired direct-console contract returned the wrong error.');
|
|
}
|
|
$issued = CloudConsoleTicketService::issue($instance, [
|
|
'user_id' => 17,
|
|
'session_id' => str_repeat('a', 64),
|
|
], [
|
|
'ticket' => $agentSecretTicket,
|
|
'expires_at' => gmdate('Y-m-d\\TH:i:s\\Z', $agentExpiry),
|
|
]);
|
|
$stored = CloudConsoleTicket::first();
|
|
$assert(($issued['websocket_path'] ?? null) === '/engine-console'
|
|
&& ($issued['ticket'] ?? '') !== $agentSecretTicket
|
|
&& ($issued['expires_in'] ?? 0) > 0
|
|
&& $stored !== null
|
|
&& !str_contains((string) $stored->sealed_agent_ticket,
|
|
$agentSecretTicket),
|
|
'Panel console ticket did not hide the Agent ticket.');
|
|
|
|
$first = CloudConsoleTicketService::introspect([
|
|
'ticket' => $issued['ticket'],
|
|
'consume' => true,
|
|
]);
|
|
$assert($first === [
|
|
'active' => true,
|
|
'connection_id' => $first['connection_id'],
|
|
'agent_id' => '9',
|
|
'agent_ticket' => $agentSecretTicket,
|
|
'agent_expires_at' => gmdate('Y-m-d\\TH:i:s\\Z', $agentExpiry),
|
|
] && preg_match('/^[a-f0-9]{64}$/D', $first['connection_id']) === 1,
|
|
'Initial introspection returned an unsafe relay contract.');
|
|
$again = CloudConsoleTicketService::introspect([
|
|
'ticket' => $issued['ticket'],
|
|
'consume' => true,
|
|
]);
|
|
$assert($again === ['active' => false],
|
|
'One-use browser console ticket was consumed twice.');
|
|
$recheck = CloudConsoleTicketService::introspect([
|
|
'ticket' => $issued['ticket'],
|
|
'connection_id' => $first['connection_id'],
|
|
'consume' => false,
|
|
]);
|
|
$assert($recheck === ['active' => true],
|
|
'Active relay connection did not pass its bounded recheck.');
|
|
|
|
$disabledTicket = CloudConsoleTicketService::issue($instance, [
|
|
'user_id' => 17,
|
|
'session_id' => str_repeat('a', 64),
|
|
], [
|
|
'ticket' => $agentSecretTicket,
|
|
'expires_at' => gmdate('Y-m-d\\TH:i:s\\Z', $agentExpiry),
|
|
]);
|
|
$host->default_network = 'public';
|
|
$host->save();
|
|
$compatTicket = CloudConsoleTicketService::issue($instance, [
|
|
'user_id' => 17, 'session_id' => str_repeat('a', 64),
|
|
], [
|
|
'ticket' => $agentSecretTicket,
|
|
'expires_at' => gmdate('Y-m-d\\TH:i:s\\Z', $agentExpiry),
|
|
]);
|
|
$assert(CloudConsoleTicketService::introspect([
|
|
'ticket' => $disabledTicket['ticket'], 'consume' => true,
|
|
])['active'] === true,
|
|
'Compatibility default blocked first-use console access.');
|
|
$assert(CloudConsoleTicketService::introspect([
|
|
'ticket' => $issued['ticket'], 'connection_id' => $first['connection_id'],
|
|
'consume' => false,
|
|
]) === ['active' => true],
|
|
'Compatibility default blocked an existing console connection.');
|
|
$assert(is_string($compatTicket['ticket'] ?? null),
|
|
'Compatibility default blocked a new console ticket.');
|
|
$host->default_network = 'network:default';
|
|
$host->save();
|
|
$assert(CloudConsoleTicketService::introspect([
|
|
'ticket' => $issued['ticket'], 'connection_id' => $first['connection_id'],
|
|
'consume' => false,
|
|
]) === ['active' => true], 'Canonical libvirt network rejected an existing console connection.');
|
|
$agent->enabled = 0;
|
|
$agent->save();
|
|
$disabledConsume = CloudConsoleTicketService::introspect([
|
|
'ticket' => $compatTicket['ticket'],
|
|
'consume' => true,
|
|
]);
|
|
$assert($disabledConsume === ['active' => false],
|
|
'Disabled Agent retained first-use console access.');
|
|
$agent->enabled = 1;
|
|
$agent->save();
|
|
|
|
$agent->revision = 4;
|
|
$agent->save();
|
|
$revisionRevoked = CloudConsoleTicketService::introspect([
|
|
'ticket' => $issued['ticket'],
|
|
'connection_id' => $first['connection_id'],
|
|
'consume' => false,
|
|
]);
|
|
$assert($revisionRevoked === ['active' => false],
|
|
'Changed Agent revision retained an existing console connection.');
|
|
$agent->revision = 3;
|
|
$agent->save();
|
|
|
|
CloudSessionService::$active = false;
|
|
$revoked = CloudConsoleTicketService::introspect([
|
|
'ticket' => $issued['ticket'],
|
|
'connection_id' => $first['connection_id'],
|
|
'consume' => false,
|
|
]);
|
|
$assert($revoked === ['active' => false],
|
|
'Revoked panel session retained console relay access.');
|
|
|
|
echo "One-use panel-to-Agent console ticket mapping passed.\n";
|
|
}
|