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.
229 lines
8.8 KiB
PHP
229 lines
8.8 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Models\CloudHost;
|
|
use App\Models\CloudHostAgent;
|
|
use App\Controllers\CloudAdminController;
|
|
use App\Services\Cloud\CloudAgentClient;
|
|
use App\Services\Cloud\CloudAgentError;
|
|
use App\Services\Cloud\CloudAgentSettingsService;
|
|
use App\Services\Cloud\CloudError;
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Slim\Http\Environment;
|
|
use Slim\Http\Request;
|
|
use Slim\Http\Response;
|
|
|
|
if (!function_exists('sodium_crypto_aead_xchacha20poly1305_ietf_encrypt')) {
|
|
echo "SKIPPED CloudAgentActivationGateTest: runtime lacks sodium.\n";
|
|
exit(0);
|
|
}
|
|
|
|
$assert = static function (bool $condition, string $message): void {
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
};
|
|
$System_Config = [
|
|
'cloud_data_key' => base64_encode(str_repeat('g', 32)),
|
|
'cloud_agent_gateway_url' => 'http://127.0.0.1:8001/internal/agent/request',
|
|
'cloud_agent_gateway_secret' => str_repeat('w', 32),
|
|
];
|
|
|
|
$capsule = new Capsule();
|
|
$capsule->addConnection([
|
|
'driver' => 'sqlite',
|
|
'database' => ':memory:',
|
|
], 'default');
|
|
$capsule->setAsGlobal();
|
|
$capsule->bootEloquent();
|
|
$schema = $capsule->schema('default');
|
|
$schema->create('cloud_hosts', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->string('name');
|
|
$table->string('region')->default('');
|
|
$table->string('country_code', 2)->default('');
|
|
$table->integer('enabled')->default(0);
|
|
$table->string('default_network')->default('bridge:br0');
|
|
$table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
});
|
|
$schema->create('cloud_host_agents', static function (Blueprint $table): void {
|
|
$table->integer('host_id')->primary();
|
|
$table->text('sealed_api_secret');
|
|
$table->integer('enabled')->default(0);
|
|
$table->integer('revision')->default(1);
|
|
$table->integer('tested_revision')->default(0);
|
|
$table->string('last_test_status')->default('never');
|
|
$table->string('last_error_code')->default('');
|
|
$table->string('last_agent_version')->default('');
|
|
$table->text('network_selectors_json')->default('[]');
|
|
$table->text('public_network_capabilities_json')->default('{}');
|
|
$table->integer('last_tested_at')->default(0);
|
|
$table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
});
|
|
$schema->create('cloud_host_storage_configurations',
|
|
static function (Blueprint $table): void {
|
|
$table->integer('host_id')->primary();
|
|
$table->integer('revision')->default(0);
|
|
$table->string('state')->default('applying');
|
|
});
|
|
|
|
$now = time();
|
|
$host = new CloudHost();
|
|
$host->name = 'Activation Gate Host';
|
|
$host->region = 'test';
|
|
$host->country_code = 'US';
|
|
$host->enabled = true;
|
|
$host->default_network = 'bridge:br0';
|
|
$host->created_at = $now;
|
|
$host->updated_at = $now;
|
|
$host->save();
|
|
|
|
$saved = CloudAgentSettingsService::save((int) $host->id, [
|
|
'enabled' => true,
|
|
'api_secret' => str_repeat('s', 32),
|
|
'expected_revision' => 0,
|
|
]);
|
|
$assert($saved['enabled'] === true && $saved['revision'] === 1
|
|
&& $saved['tested_revision'] === 0 && $saved['ready'] === false
|
|
&& $saved['storage_configured'] === false
|
|
&& CloudAgentSettingsService::enabledHostIds() === [],
|
|
'A newly saved enabled Agent revision became schedulable before testing.');
|
|
try {
|
|
CloudAgentSettingsService::clientForHost((int) $host->id);
|
|
throw new RuntimeException('Untested Agent client was issued.');
|
|
} catch (CloudAgentError $error) {
|
|
$assert($error->agentCode === 'agent_not_tested',
|
|
'Untested Agent returned the wrong stable error code.');
|
|
}
|
|
|
|
Capsule::table('cloud_host_storage_configurations')->insert([
|
|
'host_id' => (int) $host->id, 'revision' => 1,
|
|
'state' => 'applied',
|
|
]);
|
|
|
|
$recordTest = new ReflectionMethod(CloudAgentSettingsService::class,
|
|
'recordTest');
|
|
$publicNetwork = [
|
|
'contract' => 1,
|
|
'dual_stack_required' => true,
|
|
'project_owned_filter' => true,
|
|
'ndp_guard' => true,
|
|
];
|
|
$recordTest->invoke(null, (int) $host->id, 1, 'healthy', '', 'test-1',
|
|
['bridge:br0'], $publicNetwork);
|
|
$ready = CloudAgentSettingsService::read((int) $host->id);
|
|
$assert($ready['ready'] === true && $ready['tested_revision'] === 1
|
|
&& (string) CloudHostAgent::find((int) $host->id)
|
|
->network_selectors_json === '["bridge:br0"]'
|
|
&& CloudAgentSettingsService::enabledHostIds() === [(int) $host->id]
|
|
&& CloudAgentSettingsService::clientForHost((int) $host->id)
|
|
instanceof CloudAgentClient,
|
|
'The exact successfully tested Agent revision did not become ready.');
|
|
|
|
// The compatibility default is not an Agent or placement readiness authority.
|
|
$host->default_network = 'public';
|
|
$host->save();
|
|
$assert(CloudAgentSettingsService::read((int) $host->id)['ready'] === true
|
|
&& CloudAgentSettingsService::enabledHostIds() === [(int) $host->id]
|
|
&& CloudAgentSettingsService::clientForHost((int) $host->id)
|
|
instanceof CloudAgentClient,
|
|
'A compatibility default still gates current Agent readiness.');
|
|
$assert(CloudAgentSettingsService::clientForConfiguration((int) $host->id)
|
|
instanceof CloudAgentClient,
|
|
'Compatibility metadata prevented management bootstrap.');
|
|
$host->default_network = 'bridge:br0';
|
|
$host->save();
|
|
|
|
$unchanged = CloudAgentSettingsService::save((int) $host->id, [
|
|
'enabled' => true,
|
|
'api_secret' => '',
|
|
'expected_revision' => 1,
|
|
]);
|
|
$assert($unchanged['revision'] === 1
|
|
&& $unchanged['tested_revision'] === 1
|
|
&& $unchanged['last_test_status'] === 'healthy'
|
|
&& $unchanged['ready'] === true
|
|
&& CloudAgentSettingsService::enabledHostIds() === [(int) $host->id],
|
|
'Saving unchanged Agent settings revoked a valid readiness result.');
|
|
|
|
$rotated = CloudAgentSettingsService::save((int) $host->id, [
|
|
'enabled' => true,
|
|
'api_secret' => str_repeat('r', 32),
|
|
'expected_revision' => 1,
|
|
]);
|
|
$assert($rotated['revision'] === 2
|
|
&& $rotated['tested_revision'] === 0
|
|
&& $rotated['last_test_status'] === 'never'
|
|
&& $rotated['ready'] === false
|
|
&& CloudAgentSettingsService::enabledHostIds() === [],
|
|
'Rotating an Agent secret retained the prior readiness result.');
|
|
$recordTest->invoke(null, (int) $host->id, 2, 'healthy', '', 'test-2',
|
|
['bridge:br0'], $publicNetwork);
|
|
|
|
$changed = CloudAgentSettingsService::save((int) $host->id, [
|
|
'enabled' => false,
|
|
'api_secret' => '',
|
|
'expected_revision' => 2,
|
|
]);
|
|
$assert($changed['revision'] === 3 && $changed['enabled'] === false
|
|
&& $changed['tested_revision'] === 0
|
|
&& $changed['last_test_status'] === 'never'
|
|
&& $changed['ready'] === false
|
|
&& CloudAgentSettingsService::enabledHostIds() === [],
|
|
'Changing an Agent setting retained the prior revision readiness.');
|
|
|
|
$recordTest->invoke(null, (int) $host->id, 3, 'healthy', '', 'test-3',
|
|
['bridge:br0'], $publicNetwork);
|
|
$recordTest->invoke(null, (int) $host->id, 3, 'unhealthy',
|
|
'agent_unavailable', '');
|
|
$failed = CloudHostAgent::find((int) $host->id);
|
|
$assert($failed !== null && !$failed->isReady()
|
|
&& (int) $failed->tested_revision === 0
|
|
&& CloudAgentSettingsService::enabledHostIds() === [],
|
|
'A failed retest did not revoke the current Agent revision readiness.');
|
|
|
|
$saveHostNetwork = static function (string $network) use ($host): void {
|
|
$request = Request::createFromEnvironment(Environment::mock([
|
|
'REQUEST_METHOD' => 'PUT', 'REQUEST_URI' => '/api/v1/admin/hosts/1',
|
|
]))->withAttribute('cloud_actor', [
|
|
'user_id' => 1, 'is_admin' => true, 'is_super_admin' => true,
|
|
'can_manage' => true,
|
|
])->withParsedBody([
|
|
'name' => 'Activation Gate Host', 'region' => 'test',
|
|
'country_code' => 'US', 'enabled' => true, 'default_network' => $network,
|
|
]);
|
|
(new CloudAdminController())->hosts($request, new Response(),
|
|
['id' => (int) $host->id]);
|
|
};
|
|
foreach (['public', 'bridge:' . str_repeat('b', 16),
|
|
'network:' . str_repeat('n', 65)] as $invalidNetwork) {
|
|
try {
|
|
$saveHostNetwork($invalidNetwork);
|
|
throw new RuntimeException('Host API accepted an invalid network.');
|
|
} catch (CloudError $error) {
|
|
$assert($error->httpStatus === 422,
|
|
'Host network validation returned the wrong status.');
|
|
}
|
|
}
|
|
foreach (['bridge:br9', 'network:default'] as $network) {
|
|
$setting = CloudHostAgent::find((int) $host->id);
|
|
$revision = (int) $setting->revision;
|
|
$setting->enabled = true;
|
|
$setting->save();
|
|
$recordTest->invoke(null, (int) $host->id, $revision, 'healthy', '',
|
|
'test-current', [$network], $publicNetwork);
|
|
$saveHostNetwork($network);
|
|
$setting->refresh();
|
|
$assert($host->fresh()->default_network === $network
|
|
&& $setting->revision === $revision
|
|
&& $setting->tested_revision === $revision && $setting->isReady(),
|
|
'Compatibility host metadata invalidated current Agent capabilities.');
|
|
}
|
|
|
|
echo "Agent current-revision activation gate passed.\n";
|