fix: restore instance metadata after SSR
parent
ea84d9519e
commit
352ac4bdb8
@ -0,0 +1,333 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Cloud;
|
||||
|
||||
use App\Models\CloudInstance;
|
||||
|
||||
/**
|
||||
* Validate and persist the non-secret, slow-changing part of an engine VM.
|
||||
*
|
||||
* The engine response also contains mutable power/console/rescue fields and
|
||||
* storage source names. Those values must never become an authoritative
|
||||
* business record or be embedded into an SSR bootstrap. This class is the
|
||||
* single allowlist used by provisioning and by an actor-scoped backfill.
|
||||
*/
|
||||
final class CloudInstanceMetadata
|
||||
{
|
||||
private const VERSION = 1;
|
||||
private const STATIC_KEYS = [
|
||||
'vcpu', 'memory_mib', 'disk_gib', 'disks', 'autostart', 'managed',
|
||||
'public_ip_cidr', 'os_source', 'os_name', 'install_iso', 'login_name',
|
||||
'engine_metadata_version',
|
||||
];
|
||||
private const FORBIDDEN_ENGINE_KEYS = [
|
||||
'state', 'console_port', 'credential_generation', 'rescue_mode',
|
||||
'rescue_staged', 'rescue_volume_name', 'auth_mode',
|
||||
'password_available', 'generated_password',
|
||||
];
|
||||
|
||||
/**
|
||||
* Canonicalize cached engine fields without touching order/billing keys.
|
||||
* Safe to call while building an SSR payload from an older database row.
|
||||
*/
|
||||
public static function sanitizeSpec(array $spec): array
|
||||
{
|
||||
$spec = CloudInput::safeResult($spec);
|
||||
$existing = self::metadata($spec);
|
||||
$version = self::integer($spec['engine_metadata_version'] ?? null,
|
||||
1, self::VERSION);
|
||||
foreach (array_merge(self::STATIC_KEYS, self::FORBIDDEN_ENGINE_KEYS) as $key) {
|
||||
unset($spec[$key]);
|
||||
}
|
||||
$spec = array_merge($spec, $existing);
|
||||
if ($version !== null) {
|
||||
$spec['engine_metadata_version'] = $version;
|
||||
}
|
||||
return $spec;
|
||||
}
|
||||
|
||||
/** Merge a trusted engine snapshot into an existing business spec. */
|
||||
public static function merge(array $spec, array $live): array
|
||||
{
|
||||
// Re-validate an older cached copy as well. Early discovery releases
|
||||
// stored raw disk source paths in `disks`; canonicalizing before the
|
||||
// overlay removes them during the first successful backfill.
|
||||
$spec = self::sanitizeSpec($spec);
|
||||
$metadata = self::metadata($live);
|
||||
if ($metadata === []) {
|
||||
return $spec;
|
||||
}
|
||||
unset($spec['engine_metadata_version']);
|
||||
if (($metadata['os_source'] ?? null) === 'cloud_template') {
|
||||
unset($spec['install_iso']);
|
||||
} elseif (($metadata['os_source'] ?? null) === 'iso') {
|
||||
unset($spec['os_name'], $spec['login_name']);
|
||||
} elseif (($metadata['os_source'] ?? null) === 'unknown') {
|
||||
unset($spec['os_name'], $spec['install_iso'], $spec['login_name']);
|
||||
}
|
||||
return array_merge($spec, $metadata, [
|
||||
'engine_metadata_version' => self::VERSION,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return only identity and public static fields after an exact UUID check.
|
||||
*
|
||||
* The snapshot is already actor-scoped by the caller. A mismatched live
|
||||
* record is ignored instead of allowing one libvirt row to decorate a
|
||||
* different tenant's database record.
|
||||
*/
|
||||
public static function publicView(array $snapshot, array $live): array
|
||||
{
|
||||
$view = self::identity($snapshot);
|
||||
$view = array_merge($view, self::metadata($snapshot));
|
||||
if (self::sameUuid($snapshot['uuid'] ?? null, $live['uuid'] ?? null)) {
|
||||
$metadata = self::metadata($live);
|
||||
$view = array_merge($view, $metadata);
|
||||
if ($metadata !== []) {
|
||||
$view['engine_metadata_version'] = self::VERSION;
|
||||
}
|
||||
$name = self::name($live['name'] ?? null);
|
||||
if ($name !== null) {
|
||||
$view['name'] = $name;
|
||||
}
|
||||
}
|
||||
return $view;
|
||||
}
|
||||
|
||||
/**
|
||||
* Persist engine metadata for only the caller-provided, actor-visible rows.
|
||||
*
|
||||
* $liveByUuid is expected to be keyed by canonical UUID, although the UUID
|
||||
* inside every live row remains authoritative and must match as well. The
|
||||
* database rows are locked and re-read before merge so a concurrent worker
|
||||
* cannot lose an updated order/reinstall specification.
|
||||
*/
|
||||
public static function persistBatch(array $snapshots, array $liveByUuid): void
|
||||
{
|
||||
$allowed = [];
|
||||
foreach ($snapshots as $snapshot) {
|
||||
if (!is_array($snapshot)) {
|
||||
continue;
|
||||
}
|
||||
$id = filter_var($snapshot['id'] ?? $snapshot['api_id'] ?? null,
|
||||
FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
|
||||
$uuid = self::uuid($snapshot['uuid'] ?? null);
|
||||
if ($id !== false && $uuid !== null) {
|
||||
$allowed[(int) $id] = $uuid;
|
||||
}
|
||||
}
|
||||
if ($allowed === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
$live = [];
|
||||
foreach ($liveByUuid as $key => $value) {
|
||||
if (!is_array($value)) {
|
||||
continue;
|
||||
}
|
||||
$rowUuid = self::uuid($value['uuid'] ?? null);
|
||||
$keyUuid = self::uuid(is_string($key) ? $key : null);
|
||||
if ($rowUuid !== null && $keyUuid !== null
|
||||
&& hash_equals($keyUuid, $rowUuid)) {
|
||||
$live[$rowUuid] = $value;
|
||||
}
|
||||
}
|
||||
if ($live === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
$connection = (new CloudInstance())->getConnection();
|
||||
$connection->transaction(static function () use ($allowed, $live): void {
|
||||
$instances = CloudInstance::whereIn('id', array_keys($allowed))
|
||||
->where('status', '<>', 'deleted')
|
||||
->lockForUpdate()->get(['id', 'uuid', 'spec_json', 'updated_at']);
|
||||
foreach ($instances as $instance) {
|
||||
$uuid = self::uuid($instance->uuid);
|
||||
if ($uuid === null
|
||||
|| !isset($allowed[(int) $instance->id])
|
||||
|| !hash_equals($allowed[(int) $instance->id], $uuid)
|
||||
|| !isset($live[$uuid])) {
|
||||
continue;
|
||||
}
|
||||
$decoded = json_decode((string) $instance->spec_json, true);
|
||||
if (!is_array($decoded) || array_is_list($decoded)) {
|
||||
// A damaged business snapshot may still contain the only
|
||||
// plan/template evidence. Never replace it with a partial
|
||||
// engine view; publicView can serve the live fields for
|
||||
// this response while an administrator repairs the row.
|
||||
continue;
|
||||
}
|
||||
$spec = $decoded;
|
||||
$merged = self::merge($spec, $live[$uuid]);
|
||||
if ($merged === $spec) {
|
||||
continue;
|
||||
}
|
||||
$instance->spec_json = json_encode($merged,
|
||||
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
||||
$instance->updated_at = time();
|
||||
$instance->save();
|
||||
}
|
||||
}, 3);
|
||||
}
|
||||
|
||||
private static function identity(array $snapshot): array
|
||||
{
|
||||
$id = filter_var($snapshot['id'] ?? $snapshot['api_id'] ?? null,
|
||||
FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
|
||||
$uuid = self::uuid($snapshot['uuid'] ?? null);
|
||||
return [
|
||||
'id' => $id === false ? '' : (string) $id,
|
||||
'api_id' => $id === false ? '' : (string) $id,
|
||||
'uuid' => $uuid ?? '',
|
||||
'name' => self::name($snapshot['name'] ?? null) ?? '',
|
||||
];
|
||||
}
|
||||
|
||||
/** Extract the strict public/static allowlist from one engine row. */
|
||||
private static function metadata(array $value): array
|
||||
{
|
||||
$result = [];
|
||||
$integerBounds = [
|
||||
'vcpu' => [1, 64],
|
||||
'memory_mib' => [1, 262144],
|
||||
'disk_gib' => [1, 1048576],
|
||||
];
|
||||
foreach ($integerBounds as $key => [$minimum, $maximum]) {
|
||||
$integer = self::integer($value[$key] ?? null, $minimum, $maximum);
|
||||
if ($integer !== null) {
|
||||
$result[$key] = $integer;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (['autostart', 'managed'] as $key) {
|
||||
if (is_bool($value[$key] ?? null)) {
|
||||
$result[$key] = $value[$key];
|
||||
}
|
||||
}
|
||||
|
||||
$cidr = self::cidr($value['public_ip_cidr'] ?? null);
|
||||
if ($cidr !== null) {
|
||||
$result['public_ip_cidr'] = $cidr;
|
||||
}
|
||||
$source = $value['os_source'] ?? null;
|
||||
if (is_string($source) && in_array($source,
|
||||
['cloud_template', 'iso', 'unknown'], true)) {
|
||||
$result['os_source'] = $source;
|
||||
}
|
||||
$osName = self::text($value['os_name'] ?? null, 64);
|
||||
if ($osName !== null && $source === 'cloud_template') {
|
||||
$result['os_name'] = $osName;
|
||||
}
|
||||
$installIso = self::text($value['install_iso'] ?? null, 255);
|
||||
if ($installIso !== null && $source === 'iso') {
|
||||
$result['install_iso'] = $installIso;
|
||||
}
|
||||
$login = $value['login_name'] ?? null;
|
||||
if ($source === 'cloud_template' && is_string($login)
|
||||
&& preg_match('/^[A-Za-z_][A-Za-z0-9_.-]{0,31}$/D', $login) === 1) {
|
||||
$result['login_name'] = $login;
|
||||
} elseif (($result['managed'] ?? false) === true
|
||||
&& ($result['os_source'] ?? null) === 'cloud_template') {
|
||||
// Discovery returns raw libvirt data rather than decorate_vm().
|
||||
$result['login_name'] = 'root';
|
||||
}
|
||||
|
||||
$disks = self::disks($value['disks'] ?? null);
|
||||
if ($disks !== []) {
|
||||
$result['disks'] = $disks;
|
||||
$capacity = array_sum(array_column($disks, 'capacity_gib'));
|
||||
if ($capacity > 0) {
|
||||
$result['disk_gib'] = round($capacity, 2);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
private static function disks($value): array
|
||||
{
|
||||
if (!is_array($value) || !array_is_list($value)) {
|
||||
return [];
|
||||
}
|
||||
$result = [];
|
||||
foreach (array_slice($value, 0, 32) as $disk) {
|
||||
if (!is_array($disk)) {
|
||||
continue;
|
||||
}
|
||||
$capacity = $disk['capacity_gib'] ?? $disk['size_gib'] ?? null;
|
||||
if (is_int($capacity) || is_float($capacity)
|
||||
|| (is_string($capacity) && is_numeric($capacity))) {
|
||||
$capacity = (float) $capacity;
|
||||
if (is_finite($capacity) && $capacity > 0 && $capacity <= 1048576) {
|
||||
// Capacity is all the UI needs. Never persist a host path,
|
||||
// pool/volume name, target name or disk format here.
|
||||
$result[] = ['capacity_gib' => round($capacity, 2)];
|
||||
}
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
private static function integer($value, int $minimum, int $maximum): ?int
|
||||
{
|
||||
if (is_bool($value) || (!is_int($value)
|
||||
&& !(is_string($value) && preg_match('/^[0-9]+$/D', $value) === 1))) {
|
||||
return null;
|
||||
}
|
||||
$value = (int) $value;
|
||||
return $value >= $minimum && $value <= $maximum ? $value : null;
|
||||
}
|
||||
|
||||
private static function cidr($value): ?string
|
||||
{
|
||||
if (!is_string($value) || strlen($value) > 64 || trim($value) !== $value) {
|
||||
return null;
|
||||
}
|
||||
$parts = explode('/', $value, 2);
|
||||
if (count($parts) !== 2 || filter_var($parts[0], FILTER_VALIDATE_IP) === false
|
||||
|| preg_match('/^[0-9]{1,3}$/D', $parts[1]) !== 1) {
|
||||
return null;
|
||||
}
|
||||
$maximum = filter_var($parts[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)
|
||||
!== false ? 32 : 128;
|
||||
$prefix = (int) $parts[1];
|
||||
return $prefix <= $maximum ? $parts[0] . '/' . $prefix : null;
|
||||
}
|
||||
|
||||
private static function text($value, int $maximum): ?string
|
||||
{
|
||||
if (!is_string($value) || $value === '' || trim($value) !== $value) {
|
||||
return null;
|
||||
}
|
||||
$controls = preg_match('/[\x00-\x1F\x7F]/u', $value);
|
||||
if ($controls !== 0 || mb_strlen($value) > $maximum) {
|
||||
return null;
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
private static function name($value): ?string
|
||||
{
|
||||
return self::text($value, 63);
|
||||
}
|
||||
|
||||
private static function uuid($value): ?string
|
||||
{
|
||||
if (!is_string($value)) {
|
||||
return null;
|
||||
}
|
||||
$value = strtolower(trim($value));
|
||||
// libvirt also accepts operator-supplied UUIDs whose version/variant
|
||||
// nibbles do not follow RFC 4122. Match the existing ownership and
|
||||
// discovery boundary while still requiring the canonical shape.
|
||||
return preg_match('/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/D',
|
||||
$value) === 1 ? $value : null;
|
||||
}
|
||||
|
||||
private static function sameUuid($left, $right): bool
|
||||
{
|
||||
$left = self::uuid($left);
|
||||
$right = self::uuid($right);
|
||||
return $left !== null && $right !== null && hash_equals($left, $right);
|
||||
}
|
||||
}
|
||||
@ -1 +1 @@
|
||||
{assign var=venix_asset_version value='2026092805' scope='global'}
|
||||
{assign var=venix_asset_version value='2026092806' scope='global'}
|
||||
|
||||
@ -0,0 +1,176 @@
|
||||
<?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'] === 'engine-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 through the public allowlist.');
|
||||
$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";
|
||||
Loading…
Reference in New Issue