fix: preserve cloud instance template metadata

go
codex 1 month ago
parent b9dcab4ef3
commit a25aa0fe55

@ -0,0 +1,47 @@
package hypervisor
import (
"encoding/json"
"testing"
)
func TestVMMetadataWireContractExcludesPanelTemplateFields(t *testing.T) {
encoded, err := json.Marshal(VMMetadata{
UUID: "11111111-1111-4111-8111-111111111111",
Name: "vm-one",
VCPUs: 2,
MemoryMiB: 2048,
DiskGiB: 20,
PublicIPCIDR: "203.0.113.18/28",
TemplateID: "debian-13-template",
Disks: []Disk{{
Path: "/home/kvm/private-root.qcow2",
Device: "disk",
Format: "qcow2",
}},
})
if err != nil {
t.Fatalf("marshal VMMetadata: %v", err)
}
var wire map[string]any
if err := json.Unmarshal(encoded, &wire); err != nil {
t.Fatalf("decode VMMetadata wire payload: %v", err)
}
for _, key := range []string{"managed", "os_source", "os_name", "login_name"} {
if _, exists := wire[key]; exists {
t.Fatalf("panel-owned field %q escaped into VMMetadata", key)
}
}
disks, ok := wire["disks"].([]any)
if !ok || len(disks) != 1 {
t.Fatalf("unexpected disk wire payload: %#v", wire["disks"])
}
disk, ok := disks[0].(map[string]any)
if !ok {
t.Fatalf("unexpected disk DTO: %#v", disks[0])
}
if _, exists := disk["path"]; exists {
t.Fatalf("host disk path escaped into VMMetadata: %#v", disk)
}
}

@ -63,6 +63,16 @@ php xcat createAdmin
`cloudInit` 创建 PHP 拥有的云业务、Agent 注册、镜像、任务、账期、指标和流量结构;它不生成 `cloud_data_key`、不连接宿主机、不导入或转换任何旧记录。`createAdmin` 后核对 `super_admin` 只包含应有的超级管理员。
若实例是在缺少 PHP 模板展示快照的早期 Go Agent 版本中创建,更新面板代码后先执行只读检查,再显式应用元数据修复:
```bash
cd /home/virt-cloud/web
php xcat cloudRepairInstanceMetadata
php xcat cloudRepairInstanceMetadata --apply
```
命令默认只报告 `would_repair`,不会写数据库。`--apply` 只处理未删除且已成功创建的实例,并要求实例、订单、用户、宿主机和模板 ID 全部相互匹配;已有字段冲突、损坏 JSON、非云镜像或不可信模板元数据都会跳过,不能用该命令覆盖人工数据。修复优先使用付款时冻结在订单中的系统名称;仅对旧订单缺少完整展示快照时,才回退读取当前有效模板。可用 `--limit=N` 限制单次扫描数量;结果为 `has_more: true` 时,把返回的 `next_after_id` 传给下一次 `--after-id=N`,逐批完成检查或修复。
配置、密钥和数据库备份不得提交 Git。备份数据库时必须同步备份 `runtime/php/data.key`;仅有 SQL 无法解密 Agent 密钥。丢失该密钥不能通过导入旧引擎密钥或降级格式恢复。
## 4. 构建并安装宿主机 Agent

@ -0,0 +1,31 @@
<?php
namespace App\Command;
use App\Services\Cloud\CloudInstanceMetadataRepairService;
final class CloudRepairInstanceMetadata
{
public static function run(array $argv): int
{
$apply = in_array('--apply', $argv, true);
$limit = 10000;
$afterId = 0;
foreach ($argv as $argument) {
if (preg_match('/^--limit=([0-9]{1,5})$/D',
(string) $argument, $match) === 1) {
$limit = (int) $match[1];
}
if (preg_match('/^--after-id=([0-9]{1,18})$/D',
(string) $argument, $match) === 1) {
$afterId = (int) $match[1];
}
}
$result = CloudInstanceMetadataRepairService::run(
$apply, $limit, $afterId
);
echo json_encode($result, JSON_THROW_ON_ERROR
| JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . PHP_EOL;
return 0;
}
}

@ -23,6 +23,8 @@ class XCat
'cloudInit' => CloudSetup::initialize(),
'cloudWorker' => CloudWorker::run($this->argv) === 0,
'cloudLifecycle' => CloudLifecycle::run($this->argv) === 0,
'cloudRepairInstanceMetadata' =>
CloudRepairInstanceMetadata::run($this->argv) === 0,
'setTelegram' => $this->setTelegram(),
'backup' => Job::backup(),
'backupJob' => Job::backupJob(),
@ -98,6 +100,7 @@ class XCat
. " php xcat cloudInit\n"
. " php xcat cloudWorker [--once] [--policy-only]\n"
. " php xcat cloudLifecycle [--once]\n"
. " php xcat cloudRepairInstanceMetadata [--apply] [--limit=N] [--after-id=N]\n"
. " php xcat setTelegram\n"
. " php xcat backup\n"
. " php xcat backupJob\n"

@ -0,0 +1,220 @@
<?php
namespace App\Services\Cloud;
use App\Models\CloudInstance;
use App\Models\CloudOrder;
use App\Models\CloudTemplate;
/** Repair only the Go-migration metadata omission on proven panel VMs. */
final class CloudInstanceMetadataRepairService
{
public static function run(bool $apply = false, int $limit = 10000,
int $afterId = 0): array
{
$limit = max(1, min(10000, $limit));
$afterId = max(0, $afterId);
$stats = [
'mode' => $apply ? 'apply' : 'dry-run',
'after_id' => $afterId,
'next_after_id' => null,
'has_more' => false,
'scanned' => 0,
'would_repair' => 0,
'repaired' => 0,
'unchanged' => 0,
'skipped' => 0,
];
$references = CloudInstance::query()
->where('status', '<>', 'deleted')
->whereNotNull('order_id')
->where('id', '>', $afterId)
->orderBy('id')
->limit($limit + 1)
->get(['id', 'order_id']);
if ($references->count() > $limit) {
$stats['has_more'] = true;
$references = $references->take($limit);
}
foreach ($references as $reference) {
$stats['scanned']++;
if ($stats['has_more']) {
$stats['next_after_id'] = (int) $reference->id;
}
$outcome = $apply
? self::applyOne((int) $reference->id,
(int) $reference->order_id)
: self::inspectOne((int) $reference->id,
(int) $reference->order_id);
if ($outcome === 'repair') {
$stats[$apply ? 'repaired' : 'would_repair']++;
} elseif ($outcome === 'unchanged') {
$stats['unchanged']++;
} else {
$stats['skipped']++;
}
}
return $stats;
}
private static function inspectOne(int $instanceId, int $orderId): string
{
$order = CloudOrder::query()->where('id', $orderId)->first(
self::orderColumns()
);
$instance = CloudInstance::query()->where('id', $instanceId)->first(
self::instanceColumns()
);
return self::evaluate($instance, $order)['outcome'];
}
private static function applyOne(int $instanceId, int $orderId): string
{
$connection = (new CloudOrder())->getConnection();
return $connection->transaction(static function () use (
$instanceId, $orderId
): string {
// Match the worker's durable lock order: order before instance.
$order = CloudOrder::query()->where('id', $orderId)
->lockForUpdate()->first(self::orderColumns());
$instance = CloudInstance::query()->where('id', $instanceId)
->lockForUpdate()->first(self::instanceColumns());
$evaluation = self::evaluate($instance, $order);
if ($evaluation['outcome'] !== 'repair') {
return $evaluation['outcome'];
}
$instance->spec_json = json_encode($evaluation['spec'],
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
| JSON_UNESCAPED_UNICODE);
$instance->updated_at = time();
$instance->save();
return 'repair';
}, 3);
}
private static function evaluate(?CloudInstance $instance,
?CloudOrder $order): array
{
if ($instance === null || $order === null
|| (string) $instance->status === 'deleted'
|| (int) $instance->order_id !== (int) $order->id
|| (int) $order->instance_id !== (int) $instance->id
|| (int) $order->user_id !== (int) $instance->user_id
|| (int) $order->host_id !== (int) $instance->host_id
|| (string) $order->status !== 'succeeded') {
return ['outcome' => 'skip'];
}
$instanceSpec = self::spec($instance->spec_json);
$orderSpec = self::spec($order->spec_json);
if ($instanceSpec === null || $orderSpec === null
|| ($instanceSpec['source'] ?? null) !== 'cloud'
|| ($orderSpec['source'] ?? null) !== 'cloud'
|| !is_string($instanceSpec['template_id'] ?? null)
|| !is_string($orderSpec['template_id'] ?? null)
|| !hash_equals($orderSpec['template_id'],
$instanceSpec['template_id'])
|| preg_match('/^[A-Za-z0-9][A-Za-z0-9_.-]{0,79}$/D',
$instanceSpec['template_id']) !== 1) {
return ['outcome' => 'skip'];
}
$expected = self::expectedMetadata($orderSpec,
$instanceSpec['template_id']);
if ($expected === null) {
return ['outcome' => 'skip'];
}
$missing = false;
foreach ($expected as $key => $value) {
if (!array_key_exists($key, $instanceSpec)
|| $instanceSpec[$key] === null
|| $instanceSpec[$key] === '') {
$missing = true;
continue;
}
if ($instanceSpec[$key] !== $value) {
// Never reinterpret ISO/discovered instances or overwrite an
// operator-visible value when historical evidence conflicts.
return ['outcome' => 'skip'];
}
}
if (!$missing) {
return ['outcome' => 'unchanged'];
}
return [
'outcome' => 'repair',
'spec' => array_replace($instanceSpec, $expected),
];
}
/** Prefer the immutable paid-order snapshot; use the catalog only for
* orders created before presentation metadata was frozen. */
private static function expectedMetadata(array $orderSpec,
string $templateId): ?array
{
$snapshotKeys = ['os_source', 'os_name', 'login_name'];
$present = array_filter($snapshotKeys,
static fn (string $key): bool => array_key_exists($key,
$orderSpec));
if ($present !== []) {
if (count($present) !== count($snapshotKeys)
|| ($orderSpec['os_source'] ?? null) !== 'cloud_template'
|| ($orderSpec['login_name'] ?? null) !== 'root'
|| !self::validOsName($orderSpec['os_name'] ?? null)) {
return null;
}
$osName = trim($orderSpec['os_name']);
} else {
$template = CloudTemplate::query()->where('id', $templateId)
->where('state', 'active')->first(['id', 'name']);
$osName = $template === null
? '' : trim((string) $template->name);
if (!self::validOsName($osName)) {
return null;
}
}
return [
'os_source' => 'cloud_template',
'os_name' => $osName,
'login_name' => 'root',
'managed' => true,
];
}
private static function validOsName($value): bool
{
if (!is_string($value)) {
return false;
}
$value = trim($value);
return $value !== '' && mb_strlen($value) <= 64
&& preg_match('//u', $value) === 1
&& preg_match('/[\x00-\x1F\x7F]/u', $value) !== 1;
}
private static function spec($value): ?array
{
try {
$decoded = json_decode((string) $value, true, 32,
JSON_THROW_ON_ERROR);
} catch (\JsonException $error) {
return null;
}
return is_array($decoded) && !array_is_list($decoded)
? $decoded : null;
}
private static function orderColumns(): array
{
return [
'id', 'user_id', 'host_id', 'instance_id', 'status', 'spec_json',
];
}
private static function instanceColumns(): array
{
return [
'id', 'host_id', 'user_id', 'order_id', 'status', 'spec_json',
'updated_at',
];
}
}

@ -17,6 +17,7 @@ final class CloudJobService
{
CloudAccess::vmActor($instance, $actor);
return $instance->getConnection()->transaction(function () use ($instance, $actor, $kind, $method, $suffix, $body, $credential) {
$templateMetadata = null;
if ($kind === 'reinstall') {
CloudTemplateCatalogService::assertProvisionable(
CloudInput::template($body['template_id'] ?? '')
@ -77,6 +78,10 @@ final class CloudJobService
$body,
false
);
$templateMetadata =
CloudTemplateCatalogService::templateMetadataSnapshot(
$body
);
}
$policyService = ['suspending_expired', 'resume_pending', 'resuming',
'suspend_failed', 'resume_failed', 'expired_grace',
@ -248,6 +253,11 @@ final class CloudJobService
'body' => $body,
'credential' => $credential, 'owner_id' => (int) $locked->user_id,
'can_manage' => (bool) ($actor['can_manage'] ?? false)];
if ($templateMetadata !== null) {
// Encrypted panel metadata is deliberately outside body:
// body is the exact strict Go ReinstallRequest DTO.
$jobPayload['template_metadata'] = $templateMetadata;
}
if ($networkLedger !== null) {
$source = $networkLedger['source'];
$target = $networkLedger['target'];

@ -217,6 +217,14 @@ final class CloudOrderService
$payload['body'] = CloudTemplateCatalogService::applyProvisioningSnapshot(
$payload['body']
);
$templateMetadata =
CloudTemplateCatalogService::templateMetadataSnapshot(
$payload['body']
);
// Keep PHP-only presentation metadata beside the strict Go request.
// The encrypted job carries the same immutable snapshot used by the
// durable order record, but only payload.body is sent to the Agent.
$payload['template_metadata'] = $templateMetadata;
// Freeze the complete effective platform deny policy at the payment
// boundary. An unpaid order therefore receives the current catalog,
// while a queued/retried create can never drift after an administrator
@ -227,6 +235,13 @@ final class CloudOrderService
if (!is_array($orderSpec) || array_is_list($orderSpec)) {
throw new CloudError('开通订单的规格快照无效', 409);
}
if (($orderSpec['source'] ?? null) !== 'cloud'
|| !is_string($orderSpec['template_id'] ?? null)
|| !hash_equals((string) $templateMetadata['template_id'],
$orderSpec['template_id'])) {
throw new CloudError('开通订单的云镜像快照不匹配', 409);
}
$orderSpec = array_replace($orderSpec, $templateMetadata);
$orderSpec['platform_network_policy_snapshot'] = $platformPolicy;
$order->spec_json = json_encode($orderSpec, JSON_THROW_ON_ERROR
| JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

@ -626,6 +626,35 @@ final class CloudTemplateCatalogService
return $body;
}
/**
* Freeze the PHP-owned, non-secret guest presentation metadata beside a
* strict Agent request. The caller must keep this snapshot outside the
* Agent body so Go never receives panel-only fields.
*/
public static function templateMetadataSnapshot(array $body): array
{
$id = self::templateId($body['template_id'] ?? '');
$revision = filter_var($body['template_revision'] ?? null,
FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
$generation = $body['template_generation_token'] ?? null;
$template = CloudTemplate::query()->where('id', $id)
->where('state', 'active')->lockForUpdate()->first();
if ($template === null || $revision === false
|| !is_string($generation)
|| preg_match('/^[a-f0-9]{32}$/D', $generation) !== 1
|| (int) $template->revision !== (int) $revision
|| !hash_equals((string) $template->generation_token,
$generation)) {
throw new CloudError('云镜像模板版本已变化,请重新提交', 409);
}
return [
'template_id' => (string) $template->id,
'os_source' => 'cloud_template',
'os_name' => self::name((string) $template->name),
'login_name' => 'root',
];
}
public static function assertProvisionable(string $id): void
{
$template = CloudTemplate::query()->where('id', self::templateId($id))

@ -690,6 +690,10 @@ final class CloudWorkerService
$instance->resource_pool_id = $order->resource_pool_id;
$instance->reservation_id = $order->reservation_id;
$spec = json_decode((string) $order->spec_json, true) ?: [];
$templateMetadata = self::trustedTemplateMetadata($payload);
if ($templateMetadata !== null) {
$spec = array_replace($spec, $templateMetadata);
}
$now = time();
$days = filter_var($spec['validity_days'] ?? null,
FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
@ -722,8 +726,22 @@ final class CloudWorkerService
// and actual capacity without issuing a host read
// on every request. Billing/order fields remain authoritative
// because the allowlist cannot overwrite them.
$instanceSpec = CloudInstanceMetadata::merge($spec, $vm);
if ($templateMetadata !== null) {
// Agent observations cannot replace PHP's catalog
// snapshot, even if an older Agent happens to return
// similarly named fields.
$instanceSpec = array_replace(
$instanceSpec,
$templateMetadata
);
unset($instanceSpec['install_iso']);
}
// A VM is panel-managed only after the Agent's exact create
// operation has returned the validated success above.
$instanceSpec['managed'] = true;
$instance->spec_json = json_encode(
CloudInstanceMetadata::merge($spec, $vm),
$instanceSpec,
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
| JSON_UNESCAPED_UNICODE
);
@ -802,6 +820,8 @@ final class CloudWorkerService
// operator review. The job deliberately remains uncertain.
$decoded = json_decode((string) $instance->spec_json, true);
if (is_array($decoded) && !array_is_list($decoded)) {
$templateMetadata =
self::trustedTemplateMetadata($payload);
$templateId = is_array($payload['body'] ?? null)
? ($payload['body']['template_id'] ?? null) : null;
if (is_string($templateId)
@ -809,9 +829,24 @@ final class CloudWorkerService
$templateId) === 1) {
$decoded['template_id'] = $templateId;
}
unset($decoded['os_source'], $decoded['os_name'],
$decoded['install_iso'], $decoded['login_name'],
$decoded['agent_metadata_version']);
if ($templateMetadata === null) {
// Compatibility for jobs queued before the panel
// began sealing presentation metadata. Never show
// the old guest's system as the replacement's.
unset($decoded['os_source'], $decoded['os_name'],
$decoded['install_iso'],
$decoded['login_name']);
} else {
$decoded = array_replace(
$decoded,
$templateMetadata
);
unset($decoded['install_iso']);
}
$decoded['managed'] = true;
// Force one fresh capacity/IP observation while the
// trusted PHP presentation snapshot remains usable.
unset($decoded['agent_metadata_version']);
$instance->spec_json = json_encode($decoded,
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
| JSON_UNESCAPED_UNICODE);
@ -1050,6 +1085,46 @@ final class CloudWorkerService
return CloudOperationCatalog::isPowerAction($action ?? '') ? $action : null;
}
/**
* Validate a PHP-owned template snapshot without ever forwarding it to
* the Agent. Missing metadata is accepted only for already-queued legacy
* jobs; malformed or cross-template metadata requires reconciliation.
*/
private static function trustedTemplateMetadata(array $payload): ?array
{
if (!array_key_exists('template_metadata', $payload)) {
return null;
}
$metadata = $payload['template_metadata'];
$body = $payload['body'] ?? null;
if (!is_array($metadata) || array_is_list($metadata)
|| !is_array($body) || array_is_list($body)
|| !is_string($metadata['template_id'] ?? null)
|| !is_string($body['template_id'] ?? null)
|| preg_match('/^[A-Za-z0-9][A-Za-z0-9_.-]{0,79}$/D',
$metadata['template_id']) !== 1
|| !hash_equals($body['template_id'],
$metadata['template_id'])) {
throw new CloudError(
'任务中的云镜像展示快照与 Agent 请求不匹配,必须人工核查',
409
);
}
$public = CloudInstanceMetadata::publicSpec($metadata);
if (($public['os_source'] ?? null) !== 'cloud_template'
|| !is_string($public['os_name'] ?? null)
|| ($public['login_name'] ?? null) !== 'root') {
throw new CloudError('任务中的云镜像展示快照无效,必须人工核查',
409);
}
return [
'template_id' => $metadata['template_id'],
'os_source' => 'cloud_template',
'os_name' => $public['os_name'],
'login_name' => 'root',
];
}
/**
* Translate panel vocabulary only at the Agent wire boundary. The PHP job
* keeps "poweroff" so its audit label and user-facing history remain

@ -229,6 +229,23 @@ namespace {
$template->state = 'active';
$template->created_at = $template->updated_at = $now;
$template->save();
$reinstallTemplate = new CloudTemplate();
$reinstallTemplate->id = 'debian13-1234abcd-0123456789abcdef';
$reinstallTemplate->name = 'Debian 13';
$reinstallTemplate->os_family = 'Debian';
$reinstallTemplate->os_version = '13';
$reinstallTemplate->source_type = 'remote';
$reinstallTemplate->sealed_source_url = \App\Services\Cloud\CloudSecrets::encrypt([
'url' => 'https://example.invalid/debian13.qcow2',
], 'template-source');
$reinstallTemplate->image_bytes = 2048;
$reinstallTemplate->sha256 = str_repeat('c', 64);
$reinstallTemplate->startup_script = '';
$reinstallTemplate->revision = 1;
$reinstallTemplate->generation_token = str_repeat('d', 32);
$reinstallTemplate->state = 'active';
$reinstallTemplate->created_at = $reinstallTemplate->updated_at = $now;
$reinstallTemplate->save();
$capsule->connection('default')->table('cloud_template_replicas')->insert([
'host_id' => $host->id,
'template_id' => $template->id,
@ -382,6 +399,10 @@ namespace {
(string) $openingOrder->spec_json, true,
flags: JSON_THROW_ON_ERROR
)['platform_network_policy_snapshot'] ?? null;
$openingSpec = json_decode(
(string) $openingOrder->spec_json, true,
flags: JSON_THROW_ON_ERROR
);
CloudPlatformTestNetworkPolicy::delete(
$testRuleKey, ['expected_revision' => 1], $policyManager
);
@ -420,6 +441,11 @@ namespace {
&& $orderPolicyAfterDelete === $queuedPolicy
&& ($currentPolicy['catalog_revision'] ?? null) === 2
&& count($currentPolicy['rules']) === 9
&& ($openingSpec['template_id'] ?? null) === $template->id
&& ($openingSpec['os_source'] ?? null) === 'cloud_template'
&& ($openingSpec['os_name'] ?? null) === 'AlmaLinux 10'
&& ($openingSpec['login_name'] ?? null) === 'root'
&& !array_key_exists('managed', $openingSpec)
&& CloudPlatformTestNetworkPolicy::assertSnapshot($queuedPolicy)
=== $queuedPolicy
&& $openingOrder->sealed_payload === null,
@ -434,18 +460,14 @@ namespace {
'name' => 'vm-one',
'vcpu' => 2,
'memory_mib' => 2048,
'disk_gib' => 20,
'disks' => [[
'capacity_gib' => 20,
'source' => '/home/kvm/vm-one.qcow2',
'target' => 'vda',
'device' => 'disk',
'format' => 'qcow2',
]],
'autostart' => true,
'managed' => true,
'public_pool_key' => $sourcePoolKey,
'public_ip_cidr' => '203.0.113.18/28',
'os_source' => 'cloud_template',
'os_name' => 'AlmaLinux 10',
'login_name' => 'root',
'console_port' => 5908,
'generated_password' => 'must-not-persist',
];
@ -554,7 +576,8 @@ namespace {
&& ($instanceSpec['login_name'] ?? null) === 'root'
&& ($instanceSpec['managed'] ?? null) === true
&& ($instanceSpec['agent_metadata_version'] ?? null) === 1
&& ($instanceSpec['disks'] ?? null) === [['capacity_gib' => 20]]
&& ($instanceSpec['disk_gib'] ?? null) === 20
&& !isset($instanceSpec['disks'])
&& !isset($instanceSpec['console_port'])
&& !isset($instanceSpec['disks'][0]['source']),
'Successful provisioning did not persist the safe Agent metadata snapshot.');
@ -1390,8 +1413,23 @@ namespace {
&& $expiryDeleteUncertain->sealed_credential === null,
'Expiry deletion did not close after claim-only reconciliation succeeded.');
$reinstall = CloudJobService::enqueue($instance, $actor, 'reinstall', 'POST', '/reinstall', ['template_id' => $input['template_id'], 'confirmation_name' => $instance->name, 'root_password' => 'NewStrongPassword!123'], ['password' => 'NewStrongPassword!123', 'login_name' => 'root']);
$reinstall = CloudJobService::enqueue($instance, $actor, 'reinstall', 'POST', '/reinstall', ['template_id' => $reinstallTemplate->id, 'confirmation_name' => $instance->name, 'root_password' => 'NewStrongPassword!123'], ['password' => 'NewStrongPassword!123', 'login_name' => 'root']);
$oldCredential = $instance->sealed_credential;
$queuedReinstall = \App\Services\Cloud\CloudSecrets::decrypt(
(string) $reinstall->sealed_payload, 'job-payload'
);
$assert(($queuedReinstall['template_metadata']['template_id'] ?? null)
=== $reinstallTemplate->id
&& ($queuedReinstall['template_metadata']['os_source'] ?? null)
=== 'cloud_template'
&& ($queuedReinstall['template_metadata']['os_name'] ?? null)
=== 'Debian 13'
&& ($queuedReinstall['template_metadata']['login_name'] ?? null)
=== 'root'
&& !array_key_exists('os_source', $queuedReinstall['body'] ?? [])
&& !array_key_exists('os_name', $queuedReinstall['body'] ?? [])
&& !array_key_exists('login_name', $queuedReinstall['body'] ?? []),
'Reinstall did not keep PHP template metadata outside the strict Agent DTO.');
$mock->append(new Response(202, [], json_encode([
'operation_id' => $reinstall->operation_id,
'status' => 'reserved',
@ -1430,12 +1468,15 @@ namespace {
$assert(CloudJob::find($reinstall->id)->status === 'succeeded'
&& $reinstalled->sealed_credential !== $oldCredential
&& !str_contains(CloudJob::find($reinstall->id)->result_json, 'never-public')
&& ($reinstalledSpec['template_id'] ?? null) === $input['template_id']
&& ($reinstalledSpec['template_id'] ?? null) === $reinstallTemplate->id
&& ($reinstalledSpec['public_ip_cidr'] ?? null) === '203.0.113.18/28'
&& !isset($reinstalledSpec['os_source'], $reinstalledSpec['os_name'],
$reinstalledSpec['install_iso'], $reinstalledSpec['login_name'],
&& ($reinstalledSpec['os_source'] ?? null) === 'cloud_template'
&& ($reinstalledSpec['os_name'] ?? null) === 'Debian 13'
&& ($reinstalledSpec['login_name'] ?? null) === 'root'
&& ($reinstalledSpec['managed'] ?? null) === true
&& !isset($reinstalledSpec['install_iso'],
$reinstalledSpec['agent_metadata_version']),
'Completed reinstall password, sanitization or metadata invalidation failed.');
'Completed reinstall lost its trusted template metadata.');
// A replacement guest may be running while old-volume cleanup remains
// unresolved. The worker must keep the job uncertain but still persist
@ -1493,6 +1534,12 @@ namespace {
&& $partialInstance->sealed_credential !== $reinstalled->sealed_credential
&& !str_contains((string) $partialJob->result_json, 'private.qcow2')
&& ($partialSpec['template_id'] ?? null) === $input['template_id']
&& ($partialSpec['os_source'] ?? null) === 'cloud_template'
&& ($partialSpec['os_name'] ?? null) === 'AlmaLinux 10'
&& ($partialSpec['login_name'] ?? null) === 'root'
&& ($partialSpec['managed'] ?? null) === true
&& !isset($partialSpec['install_iso'],
$partialSpec['agent_metadata_version'])
&& count($partialRequests) === 2
&& $partialLookupRequest['method'] === 'GET'
&& $partialReconcileRequest['method'] === 'POST'

@ -0,0 +1,140 @@
<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudInstanceMetadataRepairService;
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_templates', static function (Blueprint $table): void {
$table->string('id')->primary();
$table->string('name');
$table->string('state');
});
$schema->create('cloud_orders', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('user_id');
$table->integer('host_id')->nullable();
$table->integer('instance_id')->nullable();
$table->string('status');
$table->text('spec_json');
});
$schema->create('cloud_instances', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('user_id');
$table->integer('order_id')->nullable();
$table->string('status');
$table->text('spec_json');
$table->integer('updated_at');
});
Capsule::table('cloud_templates')->insert([
'id' => 'debian-13-template',
'name' => 'Debian 13',
'state' => 'active',
]);
$baseSpec = [
'source' => 'cloud',
'template_id' => 'debian-13-template',
'public_ip_cidr' => '203.0.113.18/28',
'agent_metadata_version' => 1,
];
foreach ([
1 => $baseSpec,
2 => array_replace($baseSpec, ['os_source' => 'iso']),
3 => $baseSpec,
4 => $baseSpec,
] as $id => $spec) {
$orderSpec = $id === 4 ? array_replace($baseSpec, [
'os_source' => 'cloud_template',
'os_name' => 'Debian 12 Snapshot',
'login_name' => 'root',
]) : $baseSpec;
Capsule::table('cloud_orders')->insert([
'id' => $id,
'user_id' => 10,
'host_id' => 7,
'instance_id' => $id,
'status' => 'succeeded',
'spec_json' => json_encode($orderSpec, JSON_THROW_ON_ERROR),
]);
Capsule::table('cloud_instances')->insert([
'id' => $id,
'host_id' => 7,
'user_id' => $id === 3 ? 99 : 10,
'order_id' => $id,
'status' => 'active',
'spec_json' => json_encode($spec, JSON_THROW_ON_ERROR),
'updated_at' => 1,
]);
}
$firstPage = CloudInstanceMetadataRepairService::run(false, 1);
$assert($firstPage['scanned'] === 1 && $firstPage['would_repair'] === 1
&& $firstPage['has_more'] === true
&& $firstPage['next_after_id'] === 1,
'Repair scan did not expose a stable next-page cursor.');
$lastPage = CloudInstanceMetadataRepairService::run(false, 1, 3);
$assert($lastPage['after_id'] === 3 && $lastPage['scanned'] === 1
&& $lastPage['would_repair'] === 1
&& $lastPage['has_more'] === false
&& $lastPage['next_after_id'] === null,
'Repair scan did not honor the after-id cursor.');
$before = (string) Capsule::table('cloud_instances')->where('id', 1)
->value('spec_json');
$dryRun = CloudInstanceMetadataRepairService::run(false, 100);
$assert($dryRun === [
'mode' => 'dry-run',
'after_id' => 0,
'next_after_id' => null,
'has_more' => false,
'scanned' => 4,
'would_repair' => 2,
'repaired' => 0,
'unchanged' => 0,
'skipped' => 2,
] && Capsule::table('cloud_instances')->where('id', 1)
->value('spec_json') === $before,
'Dry-run changed data or did not report the one proven repair.');
$applied = CloudInstanceMetadataRepairService::run(true, 100);
$repaired = json_decode((string) Capsule::table('cloud_instances')
->where('id', 1)->value('spec_json'), true, flags: JSON_THROW_ON_ERROR);
$conflict = json_decode((string) Capsule::table('cloud_instances')
->where('id', 2)->value('spec_json'), true, flags: JSON_THROW_ON_ERROR);
$snapshot = json_decode((string) Capsule::table('cloud_instances')
->where('id', 4)->value('spec_json'), true, flags: JSON_THROW_ON_ERROR);
$assert($applied['repaired'] === 2 && $applied['skipped'] === 2
&& ($repaired['os_source'] ?? null) === 'cloud_template'
&& ($repaired['os_name'] ?? null) === 'Debian 13'
&& ($repaired['login_name'] ?? null) === 'root'
&& ($repaired['managed'] ?? null) === true
&& ($repaired['public_ip_cidr'] ?? null) === '203.0.113.18/28'
&& ($repaired['agent_metadata_version'] ?? null) === 1
&& ($conflict['os_source'] ?? null) === 'iso',
'Apply did not repair only the proven panel instance.');
$assert(($snapshot['os_name'] ?? null) === 'Debian 12 Snapshot'
&& ($snapshot['managed'] ?? null) === true,
'Repair did not prefer the immutable paid-order metadata snapshot.');
$repeat = CloudInstanceMetadataRepairService::run(true, 100);
$assert($repeat['repaired'] === 0 && $repeat['unchanged'] === 2
&& $repeat['skipped'] === 2,
'Metadata repair was not idempotent.');
echo "Cloud instance metadata repair tests passed.\n";

@ -173,6 +173,28 @@ $operatorView = CloudInstanceMetadata::publicView([
$assert(($operatorView['os_name'] ?? null) === 'AlmaLinux 10',
'A canonical libvirt UUID was rejected solely for non-RFC version nibbles.');
// The Go Agent intentionally reports libvirt/runtime metadata only. Template
// presentation and panel ownership stay authoritative in the PHP snapshot.
$goWireLive = $live;
unset($goWireLive['managed'], $goWireLive['os_source'],
$goWireLive['os_name'], $goWireLive['login_name'], $goWireLive['hostname']);
$phpOwnedMerge = CloudInstanceMetadata::merge([
'source' => 'cloud',
'template_id' => 'alma10-template',
'os_source' => 'cloud_template',
'os_name' => 'AlmaLinux 10',
'login_name' => 'root',
'managed' => true,
], $goWireLive);
$assert(($phpOwnedMerge['os_source'] ?? null) === 'cloud_template'
&& ($phpOwnedMerge['os_name'] ?? null) === 'AlmaLinux 10'
&& ($phpOwnedMerge['login_name'] ?? null) === 'root'
&& ($phpOwnedMerge['managed'] ?? null) === true
&& ($phpOwnedMerge['public_ip_cidr'] ?? null) === '203.0.113.18/28'
&& ($phpOwnedMerge['agent_metadata_version'] ?? null) === 1
&& !isset($phpOwnedMerge['disks'][0]['source']),
'A real Go VMMetadata response discarded PHP-owned template metadata.');
$emptyMerge = CloudInstanceMetadata::merge([
'plan_id' => 91,
'state' => 'running',

@ -87,14 +87,22 @@ namespace {
});
$schema->create('cloud_templates', static function (Blueprint $table): void {
$table->string('id')->primary();
$table->string('name');
$table->text('sealed_source_url');
$table->text('startup_script');
$table->integer('revision');
$table->string('generation_token');
$table->string('state');
});
$capsule->getConnection('default')->table('cloud_templates')->insert([
'id' => 'debian-12',
'name' => 'Debian 12',
'sealed_source_url' => CloudSecrets::encrypt([
'url' => 'https://example.invalid/debian-12.qcow2',
], 'template-source'),
'startup_script' => '',
'revision' => 1,
'generation_token' => str_repeat('a', 32),
'state' => 'active',
]);
$schema->create('cloud_jobs', static function (Blueprint $table): void {

Loading…
Cancel
Save