|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Services {
|
|
|
|
|
final class Config
|
|
|
|
|
{
|
|
|
|
|
public static string $key = '';
|
|
|
|
|
|
|
|
|
|
public static function get($name, $default = null)
|
|
|
|
|
{
|
|
|
|
|
return $name === 'cloud_data_key' ? self::$key : $default;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
|
|
|
|
|
|
use App\Models\CloudTemplateImportIntent;
|
|
|
|
|
use App\Services\Cloud\CloudError;
|
|
|
|
|
use App\Services\Cloud\CloudTemplateImportStateMachine;
|
|
|
|
|
use App\Services\Cloud\CloudTemplateImportReadModel;
|
|
|
|
|
use App\Services\Cloud\CloudTemplateMultipartAcquisition;
|
|
|
|
|
use App\Services\Config;
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Config::$key = base64_encode(str_repeat('i', 32));
|
|
|
|
|
$capsule = new Capsule();
|
|
|
|
|
$capsule->addConnection([
|
|
|
|
|
'driver' => 'sqlite', 'database' => ':memory:',
|
|
|
|
|
], 'default');
|
|
|
|
|
$capsule->setAsGlobal();
|
|
|
|
|
$capsule->bootEloquent();
|
|
|
|
|
$schema = $capsule->schema('default');
|
|
|
|
|
$schema->create('cloud_template_import_intents',
|
|
|
|
|
static function (Blueprint $table): void {
|
|
|
|
|
$table->string('id')->primary();
|
|
|
|
|
$table->integer('user_id');
|
|
|
|
|
$table->string('request_idempotency_key');
|
|
|
|
|
$table->string('request_fingerprint');
|
|
|
|
|
$table->string('source_kind');
|
|
|
|
|
$table->string('name');
|
|
|
|
|
$table->string('os_family');
|
|
|
|
|
$table->string('os_version');
|
|
|
|
|
$table->string('source_filename')->default('');
|
|
|
|
|
$table->integer('source_size_bytes')->default(0);
|
|
|
|
|
$table->integer('source_last_modified')->default(0);
|
|
|
|
|
$table->string('source_sha256')->default('');
|
|
|
|
|
$table->text('sealed_source');
|
|
|
|
|
$table->string('template_id')->unique();
|
|
|
|
|
$table->string('catalog_key');
|
|
|
|
|
$table->string('version_label');
|
|
|
|
|
$table->string('generation_token')->unique();
|
|
|
|
|
$table->integer('storage_revision');
|
|
|
|
|
$table->text('sealed_storage_runtime');
|
|
|
|
|
$table->string('object_key')->unique();
|
|
|
|
|
$table->string('multipart_upload_id')->default('');
|
|
|
|
|
$table->integer('multipart_part_size_bytes')->default(0);
|
|
|
|
|
$table->integer('multipart_part_count')->default(0);
|
|
|
|
|
$table->string('object_version_id')->default('');
|
|
|
|
|
$table->string('object_etag')->default('');
|
|
|
|
|
$table->string('state');
|
|
|
|
|
$table->integer('active_job_id')->nullable();
|
|
|
|
|
$table->string('error_code')->default('');
|
|
|
|
|
$table->integer('expires_at');
|
|
|
|
|
$table->integer('completed_at')->nullable();
|
|
|
|
|
$table->integer('created_at');
|
|
|
|
|
$table->integer('updated_at');
|
|
|
|
|
$table->unique(['user_id', 'request_idempotency_key']);
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_template_import_parts',
|
|
|
|
|
static function (Blueprint $table): void {
|
|
|
|
|
$table->string('intent_id');
|
|
|
|
|
$table->integer('part_number');
|
|
|
|
|
$table->integer('size_bytes');
|
|
|
|
|
$table->string('etag');
|
|
|
|
|
$table->integer('created_at');
|
|
|
|
|
$table->integer('updated_at');
|
|
|
|
|
$table->primary(['intent_id', 'part_number']);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$runtime = [
|
|
|
|
|
'config' => [
|
|
|
|
|
'endpoint' => 'https://objects.example.test',
|
|
|
|
|
'region' => 'ewr',
|
|
|
|
|
'bucket' => 'templates',
|
|
|
|
|
'prefix' => 'virt-cloud',
|
|
|
|
|
'path_style' => true,
|
|
|
|
|
],
|
|
|
|
|
'credentials' => [
|
|
|
|
|
'access_key_id' => 'INTENTACCESS',
|
|
|
|
|
'secret_access_key' => 'intent-secret',
|
|
|
|
|
],
|
|
|
|
|
'revision' => 9,
|
|
|
|
|
];
|
|
|
|
|
$uploads = new CloudTemplateMultipartAcquisition(
|
|
|
|
|
static fn (): array => $runtime,
|
|
|
|
|
static fn (): int => 2000000000
|
|
|
|
|
);
|
|
|
|
|
$readModel = new CloudTemplateImportReadModel();
|
|
|
|
|
$actor = [
|
|
|
|
|
'user_id' => 7,
|
|
|
|
|
'is_admin' => true,
|
|
|
|
|
'is_super_admin' => true,
|
|
|
|
|
'can_manage' => true,
|
|
|
|
|
'request_idempotency_key' => 'template-upload-intent-0001',
|
|
|
|
|
];
|
|
|
|
|
$input = [
|
|
|
|
|
'name' => 'Debian 13 本地版',
|
|
|
|
|
'os_family' => 'Debian',
|
|
|
|
|
'os_version' => '13',
|
|
|
|
|
'filename' => 'debian-13.qcow2',
|
|
|
|
|
'size_bytes' => 64 * 1024 * 1024,
|
|
|
|
|
'last_modified' => 1999999000000,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$first = $uploads->beginUpload($input, $actor);
|
|
|
|
|
$second = $uploads->beginUpload($input, $actor);
|
|
|
|
|
$assert($first['created'] === true && $second['created'] === false
|
|
|
|
|
&& $first['intent']['id'] === $second['intent']['id']
|
|
|
|
|
&& $first['intent']['state'] === 'prepared'
|
|
|
|
|
&& $first['intent']['can_resume'] === true
|
|
|
|
|
&& $first['intent']['source_size_bytes'] === 64 * 1024 * 1024,
|
|
|
|
|
'A same-key replay did not return the original template import intent.');
|
|
|
|
|
$stored = CloudTemplateImportIntent::findOrFail(
|
|
|
|
|
(string) $first['intent']['id']
|
|
|
|
|
);
|
|
|
|
|
$assert((int) $stored->storage_revision === 9
|
|
|
|
|
&& str_starts_with((string) $stored->object_key,
|
|
|
|
|
'virt-cloud/cloud-templates/')
|
|
|
|
|
&& !str_contains(json_encode($first['intent'], JSON_THROW_ON_ERROR),
|
|
|
|
|
'intent-secret')
|
|
|
|
|
&& !array_key_exists('sealed_storage_runtime', $first['intent'])
|
|
|
|
|
&& !array_key_exists('multipart_upload_id', $first['intent']),
|
|
|
|
|
'The intent did not freeze its object identity or leaked secrets.');
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$uploads->beginUpload(
|
|
|
|
|
array_replace($input, ['name' => 'Different']),
|
|
|
|
|
$actor
|
|
|
|
|
);
|
|
|
|
|
throw new RuntimeException('A same-key different request was accepted.');
|
|
|
|
|
} catch (CloudError $error) {
|
|
|
|
|
$assert($error->httpStatus === 409,
|
|
|
|
|
'A same-key fingerprint conflict returned the wrong status.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$listed = $readModel->active($actor);
|
|
|
|
|
$one = $readModel->one((string) $stored->id, $actor);
|
|
|
|
|
$assert(count($listed['intents']) === 1
|
|
|
|
|
&& $one['intent']['id'] === (string) $stored->id,
|
|
|
|
|
'A refreshed administrator page cannot recover the active intent.');
|
|
|
|
|
$originalKey = Config::$key;
|
|
|
|
|
Config::$key = '';
|
|
|
|
|
$assert($readModel->one((string) $stored->id, ['is_admin' => true]) === $one,
|
|
|
|
|
'Reading an intent unexpectedly depends on storage credentials or execution runtime.');
|
|
|
|
|
Config::$key = $originalKey;
|
|
|
|
|
foreach ([['id' => (string) $stored->id, 'actor' => [], 'status' => 403],
|
|
|
|
|
['id' => 'invalid', 'actor' => $actor, 'status' => 422],
|
|
|
|
|
['id' => str_repeat('f', 32), 'actor' => $actor, 'status' => 404]] as $case) {
|
|
|
|
|
try {
|
|
|
|
|
$readModel->one($case['id'], $case['actor']);
|
|
|
|
|
throw new RuntimeException('An invalid import read was accepted.');
|
|
|
|
|
} catch (CloudError $error) {
|
|
|
|
|
$assert($error->httpStatus === $case['status'], 'Import read error semantics changed.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CloudTemplateImportStateMachine::assertIntegrity();
|
|
|
|
|
$definitions = CloudTemplateImportStateMachine::definitions();
|
|
|
|
|
foreach ($definitions as $stateName => $definition) {
|
|
|
|
|
foreach ($definition['sources'] as $source) {
|
|
|
|
|
$probe = clone $stored;
|
|
|
|
|
$probe->state = $stateName;
|
|
|
|
|
$probe->source_kind = $source;
|
|
|
|
|
$probe->multipart_upload_id = '';
|
|
|
|
|
foreach (['start_upload', 'record_part', 'complete_upload',
|
|
|
|
|
'resume_remote', 'cancel'] as $action) {
|
|
|
|
|
$expected = in_array($action, $definition['actions'][$source], true);
|
|
|
|
|
try {
|
|
|
|
|
CloudTemplateImportStateMachine::assertAction($probe, $action);
|
|
|
|
|
$assert($expected, 'An unlisted import action was accepted.');
|
|
|
|
|
} catch (CloudError $error) {
|
|
|
|
|
$assert(!$expected && $error->httpStatus === 409,
|
|
|
|
|
'A listed import action was rejected.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$missingOutlet = $definitions;
|
|
|
|
|
$missingOutlet[$stateName]['actions'][$source] = [];
|
|
|
|
|
try {
|
|
|
|
|
CloudTemplateImportStateMachine::assertDefinitions($missingOutlet);
|
|
|
|
|
throw new RuntimeException('An import state lost all executable outlets.');
|
|
|
|
|
} catch (LogicException) {
|
|
|
|
|
// A future state cannot silently lose its action policy.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$unknownUpload = clone $stored;
|
|
|
|
|
$unknownUpload->state = 'source_unknown';
|
|
|
|
|
$unknownUpload->source_kind = 'upload';
|
|
|
|
|
$unknownUpload->multipart_upload_id = 'pending-provider-upload';
|
|
|
|
|
$unknownUpload->error_code = 'multipart_complete_unknown';
|
|
|
|
|
$assert(!CloudTemplateImportStateMachine::capabilities($unknownUpload)['can_cancel'],
|
|
|
|
|
'An unknown upload completion was exposed as cancellable.');
|
|
|
|
|
try {
|
|
|
|
|
CloudTemplateImportStateMachine::assertAction($unknownUpload, 'cancel');
|
|
|
|
|
throw new RuntimeException('The cancellation endpoint disagrees with its projection.');
|
|
|
|
|
} catch (CloudError $error) {
|
|
|
|
|
$assert($error->httpStatus === 409, 'Unknown cancellation returned the wrong status.');
|
|
|
|
|
}
|
|
|
|
|
$unknownUpload->error_code = 'multipart_abort_not_applied';
|
|
|
|
|
CloudTemplateImportStateMachine::assertAction($unknownUpload, 'cancel');
|
|
|
|
|
$mutated = $definitions;
|
|
|
|
|
$missingSuccessPath = $definitions;
|
|
|
|
|
$missingSuccessPath['source_verifying']['transitions'] = ['upload_failed', 'source_failed', 'source_unknown'];
|
|
|
|
|
$wrongStep = $definitions;
|
|
|
|
|
$wrongStep['source_ready']['resume_step'] = 'cancel';
|
|
|
|
|
$wrongAction = $definitions;
|
|
|
|
|
$wrongAction['source_ready']['actions']['upload'] = ['start_upload'];
|
|
|
|
|
foreach ([$missingSuccessPath, $wrongStep, $wrongAction] as $broken) {
|
|
|
|
|
try {
|
|
|
|
|
CloudTemplateImportStateMachine::assertDefinitions($broken);
|
|
|
|
|
throw new RuntimeException('An invalid import execution path passed completeness checks.');
|
|
|
|
|
} catch (LogicException) {
|
|
|
|
|
// A nonempty but wrong action/step or unreachable success must fail.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
unset($mutated['prepared']['actions']['upload']);
|
|
|
|
|
try {
|
|
|
|
|
CloudTemplateImportStateMachine::assertDefinitions($mutated);
|
|
|
|
|
throw new RuntimeException(
|
|
|
|
|
'A source without an executable import action was accepted.'
|
|
|
|
|
);
|
|
|
|
|
} catch (LogicException) {
|
|
|
|
|
// Expected: every supported source/state pair needs an action policy.
|
|
|
|
|
}
|
|
|
|
|
$wrongSource = clone $stored;
|
|
|
|
|
$wrongSource->state = 'prepared';
|
|
|
|
|
$wrongSource->source_kind = 'upload';
|
|
|
|
|
try {
|
|
|
|
|
CloudTemplateImportStateMachine::move($wrongSource, 'fetching');
|
|
|
|
|
throw new RuntimeException(
|
|
|
|
|
'An upload intent entered the remote-fetch state.'
|
|
|
|
|
);
|
|
|
|
|
} catch (CloudError $error) {
|
|
|
|
|
$assert($error->httpStatus === 409,
|
|
|
|
|
'A source/state mismatch returned the wrong status.');
|
|
|
|
|
}
|
|
|
|
|
$assert(in_array('source_unknown',
|
|
|
|
|
CloudTemplateImportStateMachine::cleanupStates('upload'), true)
|
|
|
|
|
&& !in_array('source_unknown',
|
|
|
|
|
CloudTemplateImportStateMachine::cleanupStates('remote'), true),
|
|
|
|
|
'Cleanup eligibility is not derived from the executable definitions.');
|
|
|
|
|
CloudTemplateImportStateMachine::move($stored, 'uploading');
|
|
|
|
|
CloudTemplateImportStateMachine::move($stored, 'source_verifying');
|
|
|
|
|
CloudTemplateImportStateMachine::move($stored, 'source_ready');
|
|
|
|
|
CloudTemplateImportStateMachine::move($stored, 'distributing');
|
|
|
|
|
CloudTemplateImportStateMachine::move($stored, 'ready');
|
|
|
|
|
$assert($stored->completed_at !== null
|
|
|
|
|
&& CloudTemplateImportStateMachine::terminal('ready'),
|
|
|
|
|
'The import state machine did not mark a terminal intent.');
|
|
|
|
|
try {
|
|
|
|
|
CloudTemplateImportStateMachine::move($stored, 'uploading');
|
|
|
|
|
throw new RuntimeException('A terminal import intent was reopened.');
|
|
|
|
|
} catch (CloudError $error) {
|
|
|
|
|
$assert($error->httpStatus === 409,
|
|
|
|
|
'An invalid terminal transition returned the wrong status.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
echo "Cloud-template import intent and state-machine behavior passed.\n";
|
|
|
|
|
}
|