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.
163 lines
5.6 KiB
PHTML
163 lines
5.6 KiB
PHTML
|
2 weeks ago
|
<?php
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
||
|
|
|
||
|
|
use App\Models\CloudHostAgent;
|
||
|
|
use App\Services\Cloud\CloudAgentError;
|
||
|
|
use App\Services\Cloud\CloudAgentSettingsService;
|
||
|
|
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);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
$sourceSchema = file_get_contents(dirname(__DIR__) . '/sql/cloud.sql');
|
||
|
|
$setupSource = file_get_contents(dirname(__DIR__) . '/app/Command/CloudSetup.php');
|
||
|
|
$assert(is_string($sourceSchema) && is_string($setupSource)
|
||
|
|
&& str_contains($sourceSchema,
|
||
|
|
'public_network_capabilities_json LONGTEXT NOT NULL')
|
||
|
|
&& str_contains($setupSource, "'public_network_capabilities_json'"),
|
||
|
|
'Fresh schema or CloudSetup omits persisted public-network evidence.');
|
||
|
|
$rejects = static function (callable $callback, string $message) use ($assert): void {
|
||
|
|
try {
|
||
|
|
$callback();
|
||
|
|
} catch (CloudAgentError $error) {
|
||
|
|
$assert($error->agentCode === 'agent_capability_mismatch'
|
||
|
|
|| $error->agentCode === 'agent_invalid_capabilities',
|
||
|
|
'Unexpected public-network capability error: ' . $error->agentCode);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
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_host_agents', static function (Blueprint $table): void {
|
||
|
|
$table->integer('host_id')->primary();
|
||
|
|
$table->text('sealed_api_secret');
|
||
|
|
$table->boolean('enabled');
|
||
|
|
$table->integer('revision');
|
||
|
|
$table->integer('tested_revision');
|
||
|
|
$table->string('last_test_status');
|
||
|
|
$table->string('last_error_code');
|
||
|
|
$table->string('last_agent_version');
|
||
|
|
$table->text('network_selectors_json');
|
||
|
|
$table->text('public_network_capabilities_json');
|
||
|
|
$table->integer('last_tested_at');
|
||
|
|
$table->integer('created_at');
|
||
|
|
$table->integer('updated_at');
|
||
|
|
});
|
||
|
|
|
||
|
|
$features = array_fill_keys([
|
||
|
|
'create_vm', 'delete_vm', 'power', 'resources', 'settings', 'network',
|
||
|
|
'reinstall', 'snapshots', 'backups', 'firewall', 'rescue', 'metrics',
|
||
|
|
'console', 'templates', 'user_iso',
|
||
|
|
], true);
|
||
|
|
$features['uefi'] = true;
|
||
|
|
$features['secure_boot'] = false;
|
||
|
|
$publicNetwork = [
|
||
|
|
'contract' => 1,
|
||
|
|
'dual_stack_required' => true,
|
||
|
|
'project_owned_filter' => true,
|
||
|
|
'ndp_guard' => true,
|
||
|
|
];
|
||
|
|
$capabilities = [
|
||
|
|
'backend' => 'libvirt',
|
||
|
|
'features' => $features,
|
||
|
|
'network_selectors' => ['bridge:br0'],
|
||
|
|
'public_network' => $publicNetwork + ['untrusted_extension' => true],
|
||
|
|
'checks' => ['storage_configuration' => ['available' => true]],
|
||
|
|
];
|
||
|
|
$summaryMethod = new ReflectionMethod(
|
||
|
|
CloudAgentSettingsService::class,
|
||
|
|
'capabilitySummary'
|
||
|
|
);
|
||
|
|
$summary = $summaryMethod->invoke(null, $capabilities);
|
||
|
|
$assert(($summary['public_network'] ?? null) === $publicNetwork,
|
||
|
|
'A valid public-network contract was not normalized into health evidence.');
|
||
|
|
foreach (array_keys($publicNetwork) as $field) {
|
||
|
|
$broken = $capabilities;
|
||
|
|
unset($broken['public_network'][$field]);
|
||
|
|
$rejects(
|
||
|
|
static fn () => $summaryMethod->invoke(null, $broken),
|
||
|
|
'A public-network capability with a missing field was accepted: ' . $field
|
||
|
|
);
|
||
|
|
}
|
||
|
|
foreach (['dual_stack_required', 'project_owned_filter', 'ndp_guard'] as $field) {
|
||
|
|
$broken = $capabilities;
|
||
|
|
$broken['public_network'][$field] = false;
|
||
|
|
$rejects(
|
||
|
|
static fn () => $summaryMethod->invoke(null, $broken),
|
||
|
|
'A false public-network safety capability was accepted: ' . $field
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
$now = time();
|
||
|
|
Capsule::table('cloud_host_agents')->insert([
|
||
|
|
'host_id' => 1, 'sealed_api_secret' => 'sealed', 'enabled' => 1,
|
||
|
|
'revision' => 4, 'tested_revision' => 0,
|
||
|
|
'last_test_status' => 'never', 'last_error_code' => '',
|
||
|
|
'last_agent_version' => '', 'network_selectors_json' => '[]',
|
||
|
|
'public_network_capabilities_json' => '{}', 'last_tested_at' => 0,
|
||
|
|
'created_at' => $now, 'updated_at' => $now,
|
||
|
|
]);
|
||
|
|
$recordTest = new ReflectionMethod(CloudAgentSettingsService::class, 'recordTest');
|
||
|
|
$recordTest->invoke(
|
||
|
|
null,
|
||
|
|
1,
|
||
|
|
4,
|
||
|
|
'healthy',
|
||
|
|
'',
|
||
|
|
'test-agent',
|
||
|
|
['bridge:br0'],
|
||
|
|
$publicNetwork
|
||
|
|
);
|
||
|
|
$agent = CloudHostAgent::find(1);
|
||
|
|
$assert($agent !== null
|
||
|
|
&& json_decode(
|
||
|
|
(string) $agent->public_network_capabilities_json,
|
||
|
|
true,
|
||
|
|
8,
|
||
|
|
JSON_THROW_ON_ERROR
|
||
|
|
) === $publicNetwork
|
||
|
|
&& CloudAgentSettingsService::publicNetworkReady(
|
||
|
|
$agent, 'bridge:br0', 4
|
||
|
|
)
|
||
|
|
&& !CloudAgentSettingsService::publicNetworkReady(
|
||
|
|
$agent, 'bridge:br1', 4
|
||
|
|
)
|
||
|
|
&& !CloudAgentSettingsService::publicNetworkReady(
|
||
|
|
$agent, 'bridge:br0', 3
|
||
|
|
),
|
||
|
|
'Public-network readiness did not bind capability, selector and revision.');
|
||
|
|
|
||
|
|
$agent->public_network_capabilities_json = json_encode([
|
||
|
|
'contract' => 1,
|
||
|
|
'dual_stack_required' => true,
|
||
|
|
'project_owned_filter' => true,
|
||
|
|
], JSON_THROW_ON_ERROR);
|
||
|
|
$agent->save();
|
||
|
|
$agent->refresh();
|
||
|
|
$assert(!CloudAgentSettingsService::publicNetworkReady(
|
||
|
|
$agent, 'bridge:br0', 4
|
||
|
|
),
|
||
|
|
'Legacy capability evidence without ndp_guard remained ready.');
|
||
|
|
|
||
|
|
CloudAgentSettingsService::invalidateReadiness($agent);
|
||
|
|
$agent->refresh();
|
||
|
|
$assert((string) $agent->public_network_capabilities_json === '{}'
|
||
|
|
&& !CloudAgentSettingsService::publicNetworkReady(
|
||
|
|
$agent, 'bridge:br0', 5
|
||
|
|
),
|
||
|
|
'Readiness invalidation retained prior public-network evidence.');
|
||
|
|
|
||
|
|
echo "Agent public-network capability gate passed.\n";
|