|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
|
|
|
|
|
|
use App\Models\CloudInstance;
|
|
|
|
|
use App\Services\Cloud\CloudInstanceMetadata;
|
|
|
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
|
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
|
|
|
|
|
|
|
|
$assert = static function ($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_instances', static function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->string('uuid');
|
|
|
|
|
$table->integer('host_id');
|
|
|
|
|
$table->integer('user_id');
|
|
|
|
|
$table->string('name');
|
|
|
|
|
$table->string('status');
|
|
|
|
|
$table->text('spec_json');
|
|
|
|
|
$table->integer('updated_at');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$firstUuid = '11111111-1111-4111-8111-111111111111';
|
|
|
|
|
$secondUuid = '22222222-2222-4222-8222-222222222222';
|
|
|
|
|
foreach ([1 => $firstUuid, 2 => $secondUuid] as $id => $uuid) {
|
|
|
|
|
Capsule::table('cloud_instances')->insert([
|
|
|
|
|
'id' => $id,
|
|
|
|
|
'uuid' => $uuid,
|
|
|
|
|
'host_id' => 7,
|
|
|
|
|
'user_id' => $id,
|
|
|
|
|
'name' => 'vm-' . $id,
|
|
|
|
|
'status' => 'active',
|
|
|
|
|
'spec_json' => json_encode([
|
|
|
|
|
'plan_id' => 91,
|
|
|
|
|
'validity_days' => 30,
|
|
|
|
|
'traffic_quota_bytes' => 1099511627776,
|
|
|
|
|
'template_id' => 'alma10-template',
|
|
|
|
|
'state' => 'running',
|
|
|
|
|
'console_port' => 5999,
|
|
|
|
|
'password_available' => true,
|
|
|
|
|
], JSON_THROW_ON_ERROR),
|
|
|
|
|
'updated_at' => 1,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
$damagedUuid = '33333333-3333-4333-8333-333333333333';
|
|
|
|
|
Capsule::table('cloud_instances')->insert([
|
|
|
|
|
'id' => 3, 'uuid' => $damagedUuid, 'host_id' => 7, 'user_id' => 1,
|
|
|
|
|
'name' => 'damaged-vm', 'status' => 'active',
|
|
|
|
|
'spec_json' => '{damaged-business-snapshot', 'updated_at' => 1,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$live = [
|
|
|
|
|
'uuid' => $firstUuid,
|
|
|
|
|
'name' => 'engine-name',
|
|
|
|
|
'state' => 'running',
|
|
|
|
|
'vcpu' => 4,
|
|
|
|
|
'memory_mib' => 4096,
|
|
|
|
|
'disks' => [
|
|
|
|
|
['capacity_gib' => 40, 'source' => '/home/kvm/private.qcow2',
|
|
|
|
|
'target' => 'vda', 'format' => 'qcow2'],
|
|
|
|
|
['size_gib' => '2.5', 'source' => 'secret-pool-volume'],
|
|
|
|
|
],
|
|
|
|
|
'autostart' => true,
|
|
|
|
|
'managed' => true,
|
|
|
|
|
'public_ip_cidr' => '203.0.113.18/28',
|
|
|
|
|
'os_source' => 'cloud_template',
|
|
|
|
|
'os_name' => 'AlmaLinux 10',
|
|
|
|
|
'install_iso' => 'must-not-win.iso',
|
|
|
|
|
'login_name' => 'root',
|
|
|
|
|
'console_port' => 5908,
|
|
|
|
|
'credential_generation' => 'panel-seed-secret.iso',
|
|
|
|
|
'password_available' => true,
|
|
|
|
|
'unexpected' => ['private_key' => 'must-not-persist'],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$view = CloudInstanceMetadata::publicView([
|
|
|
|
|
'id' => '1', 'api_id' => '1', 'uuid' => $firstUuid,
|
|
|
|
|
'name' => 'database-name', 'disk_gib' => 20,
|
|
|
|
|
], $live);
|
|
|
|
|
$assert($view['id'] === '1' && $view['uuid'] === $firstUuid
|
|
|
|
|
&& $view['name'] === 'database-name'
|
|
|
|
|
&& $view['public_ip_cidr'] === '203.0.113.18/28'
|
|
|
|
|
&& $view['os_source'] === 'cloud_template'
|
|
|
|
|
&& $view['os_name'] === 'AlmaLinux 10'
|
|
|
|
|
&& $view['login_name'] === 'root'
|
|
|
|
|
&& $view['engine_metadata_version'] === 1
|
|
|
|
|
&& $view['disk_gib'] === 42.5,
|
|
|
|
|
'Matching engine metadata was not exposed without replacing the database display name.');
|
|
|
|
|
$assert(array_keys($view['disks']) === [0, 1]
|
|
|
|
|
&& array_keys($view['disks'][0]) === ['capacity_gib']
|
|
|
|
|
&& !array_key_exists('state', $view)
|
|
|
|
|
&& !array_key_exists('console_port', $view)
|
|
|
|
|
&& !array_key_exists('credential_generation', $view)
|
|
|
|
|
&& !array_key_exists('password_available', $view)
|
|
|
|
|
&& !array_key_exists('install_iso', $view),
|
|
|
|
|
'The public VM metadata view leaked dynamic, storage or credential fields.');
|
|
|
|
|
|
|
|
|
|
$mismatch = CloudInstanceMetadata::publicView([
|
|
|
|
|
'id' => '1', 'uuid' => $firstUuid, 'name' => 'database-name',
|
|
|
|
|
'public_ip_cidr' => '198.51.100.2/24',
|
|
|
|
|
], array_replace($live, ['uuid' => $secondUuid]));
|
|
|
|
|
$assert($mismatch['name'] === 'database-name'
|
|
|
|
|
&& $mismatch['public_ip_cidr'] === '198.51.100.2/24'
|
|
|
|
|
&& !isset($mismatch['os_name'], $mismatch['engine_metadata_version']),
|
|
|
|
|
'A mismatched live UUID decorated another database instance.');
|
|
|
|
|
|
|
|
|
|
$operatorUuid = 'aaaaaaaa-aaaa-0aaa-0aaa-aaaaaaaaaaaa';
|
|
|
|
|
$operatorView = CloudInstanceMetadata::publicView([
|
|
|
|
|
'id' => '9', 'uuid' => $operatorUuid, 'name' => 'operator-vm',
|
|
|
|
|
], array_replace($live, ['uuid' => $operatorUuid]));
|
|
|
|
|
$assert(($operatorView['os_name'] ?? null) === 'AlmaLinux 10',
|
|
|
|
|
'A canonical libvirt UUID was rejected solely for non-RFC version nibbles.');
|
|
|
|
|
|
|
|
|
|
$emptyMerge = CloudInstanceMetadata::merge([
|
|
|
|
|
'plan_id' => 91,
|
|
|
|
|
'state' => 'running',
|
|
|
|
|
'engine_metadata_version' => 1,
|
|
|
|
|
], []);
|
|
|
|
|
$assert(($emptyMerge['plan_id'] ?? null) === 91
|
|
|
|
|
&& ($emptyMerge['engine_metadata_version'] ?? null) === 1
|
|
|
|
|
&& !isset($emptyMerge['state']),
|
|
|
|
|
'An empty live result discarded a valid metadata marker or skipped sanitization.');
|
|
|
|
|
|
|
|
|
|
// Only the actor-scoped snapshot is eligible even when the live inventory
|
|
|
|
|
// contains another tenant's UUID.
|
|
|
|
|
CloudInstanceMetadata::persistBatch([
|
|
|
|
|
['id' => '1', 'uuid' => $firstUuid, 'name' => 'vm-1'],
|
|
|
|
|
['id' => '3', 'uuid' => $damagedUuid, 'name' => 'damaged-vm'],
|
|
|
|
|
], [
|
|
|
|
|
$firstUuid => $live,
|
|
|
|
|
$secondUuid => array_replace($live, [
|
|
|
|
|
'uuid' => $secondUuid,
|
|
|
|
|
'public_ip_cidr' => '203.0.113.19/28',
|
|
|
|
|
]),
|
|
|
|
|
$damagedUuid => array_replace($live, ['uuid' => $damagedUuid]),
|
|
|
|
|
]);
|
|
|
|
|
CloudInstanceMetadata::persistBatch([
|
|
|
|
|
['id' => '2', 'uuid' => $secondUuid, 'name' => 'vm-2'],
|
|
|
|
|
], [
|
|
|
|
|
// A numeric/list key must not be accepted merely because the row itself
|
|
|
|
|
// contains a syntactically valid UUID.
|
|
|
|
|
array_replace($live, ['uuid' => $secondUuid]),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$first = json_decode((string) CloudInstance::find(1)->spec_json, true,
|
|
|
|
|
flags: JSON_THROW_ON_ERROR);
|
|
|
|
|
$second = json_decode((string) CloudInstance::find(2)->spec_json, true,
|
|
|
|
|
flags: JSON_THROW_ON_ERROR);
|
|
|
|
|
$assert($first['plan_id'] === 91 && $first['validity_days'] === 30
|
|
|
|
|
&& $first['traffic_quota_bytes'] === 1099511627776
|
|
|
|
|
&& $first['template_id'] === 'alma10-template'
|
|
|
|
|
&& $first['engine_metadata_version'] === 1
|
|
|
|
|
&& $first['public_ip_cidr'] === '203.0.113.18/28'
|
|
|
|
|
&& $first['login_name'] === 'root'
|
|
|
|
|
&& $first['disks'] === [
|
|
|
|
|
['capacity_gib' => 40], ['capacity_gib' => 2.5],
|
|
|
|
|
]
|
|
|
|
|
&& !isset($first['state'], $first['console_port'],
|
|
|
|
|
$first['credential_generation'], $first['unexpected'],
|
|
|
|
|
$first['install_iso']),
|
|
|
|
|
'Persisted metadata lost business fields or escaped the static allowlist.');
|
|
|
|
|
$assert(!isset($second['engine_metadata_version'], $second['public_ip_cidr']),
|
|
|
|
|
'Batch backfill crossed the caller-provided instance scope.');
|
|
|
|
|
$assert(CloudInstance::find(3)->spec_json === '{damaged-business-snapshot',
|
|
|
|
|
'Batch backfill overwrote a damaged business specification.');
|
|
|
|
|
|
|
|
|
|
echo "Cloud instance metadata tests passed.\n";
|