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.
295 lines
12 KiB
PHP
295 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudTemplateImportIntent;
|
|
use Illuminate\Database\QueryException;
|
|
|
|
/** Reserves idempotent local-upload and official-URL import intents. */
|
|
final class CloudTemplateImportReservation extends CloudTemplateImportModule
|
|
{
|
|
public function beginUpload(array $input, array $actor): array
|
|
{
|
|
CloudAccess::manage($actor);
|
|
CloudInput::only($input, [
|
|
'name', 'os_family', 'os_version', 'filename', 'size_bytes',
|
|
'last_modified', 'sha256',
|
|
]);
|
|
$userId = (int) ($actor['user_id'] ?? 0);
|
|
$requestKey = self::requestKey($actor);
|
|
if ($userId < 1) {
|
|
throw new CloudError('管理员身份无效', 403);
|
|
}
|
|
$name = self::label($input['name'] ?? null, '模板名称', 64);
|
|
$family = self::label($input['os_family'] ?? null, '系统系列', 32);
|
|
$version = self::label($input['os_version'] ?? null, '系统版本', 32);
|
|
$filename = self::filename($input['filename'] ?? null);
|
|
$size = self::integer(
|
|
$input['size_bytes'] ?? null,
|
|
self::MIN_UPLOAD_BYTES,
|
|
self::MAX_UPLOAD_BYTES,
|
|
'云镜像文件大小无效'
|
|
);
|
|
$lastModified = self::integer(
|
|
$input['last_modified'] ?? 0,
|
|
0,
|
|
PHP_INT_MAX,
|
|
'云镜像文件时间无效'
|
|
);
|
|
$sha256 = strtolower(trim((string) ($input['sha256'] ?? '')));
|
|
if (preg_match('/^[a-f0-9]{64}$/D', $sha256) !== 1) {
|
|
throw new CloudError(
|
|
'开始上传前必须完成云镜像 SHA-256 校验',
|
|
422
|
|
);
|
|
}
|
|
$canonical = [
|
|
'source_kind' => 'upload',
|
|
'name' => $name,
|
|
'os_family' => $family,
|
|
'os_version' => $version,
|
|
'filename' => $filename,
|
|
'size_bytes' => $size,
|
|
'last_modified' => $lastModified,
|
|
'sha256' => $sha256,
|
|
];
|
|
$fingerprint = self::fingerprint($canonical);
|
|
$now = ($this->clock)();
|
|
if (!is_int($now) || $now < 1) {
|
|
throw new \LogicException('Template import clock is invalid');
|
|
}
|
|
|
|
try {
|
|
$result = $this->reserveUploadIntent(
|
|
$actor,
|
|
$userId,
|
|
$requestKey,
|
|
$fingerprint,
|
|
$canonical,
|
|
$now
|
|
);
|
|
} catch (QueryException $error) {
|
|
$prior = CloudTemplateImportIntent::query()
|
|
->where('user_id', $userId)
|
|
->where('request_idempotency_key', $requestKey)->first();
|
|
if ($prior === null) {
|
|
throw $error;
|
|
}
|
|
self::assertReplay($prior, $fingerprint);
|
|
$result = [
|
|
'created' => false,
|
|
'intent' => CloudTemplateImportReadModel::project($prior),
|
|
];
|
|
}
|
|
$intent = CloudTemplateImportIntent::query()
|
|
->where('id', (string) $result['intent']['id'])->firstOrFail();
|
|
CloudTemplateStoragePublicationService::reserve($intent);
|
|
$intent->refresh();
|
|
$result['intent'] = CloudTemplateImportReadModel::project($intent);
|
|
return $result;
|
|
}
|
|
|
|
public function beginRemote(array $input, array $actor): array
|
|
{
|
|
CloudAccess::manage($actor);
|
|
CloudInput::only($input, ['name', 'url', 'sha256']);
|
|
$userId = (int) ($actor['user_id'] ?? 0);
|
|
$requestKey = self::requestKey($actor);
|
|
if ($userId < 1) {
|
|
throw new CloudError('管理员身份无效', 403);
|
|
}
|
|
$name = self::label($input['name'] ?? null, '模板名称', 64);
|
|
$url = self::remoteUrl($input['url'] ?? null);
|
|
$expectedSha256 = strtolower(trim(
|
|
(string) ($input['sha256'] ?? '')
|
|
));
|
|
if ($expectedSha256 !== ''
|
|
&& preg_match('/^[a-f0-9]{64}$/D', $expectedSha256) !== 1) {
|
|
throw new CloudError(
|
|
'SHA-256 必须留空或填写 64 位小写十六进制值'
|
|
);
|
|
}
|
|
[$family, $version] = self::inferRemoteSystem($url, $name);
|
|
$canonical = [
|
|
'source_kind' => 'remote',
|
|
'name' => $name,
|
|
'url' => $url,
|
|
'sha256' => $expectedSha256,
|
|
];
|
|
$fingerprint = self::fingerprint($canonical);
|
|
$now = ($this->clock)();
|
|
if (!is_int($now) || $now < 1) {
|
|
throw new \LogicException('Template import clock is invalid');
|
|
}
|
|
$connection = (new CloudTemplateImportIntent())->getConnection();
|
|
try {
|
|
return $connection->transaction(static function () use (
|
|
$userId,
|
|
$requestKey,
|
|
$fingerprint,
|
|
$name,
|
|
$url,
|
|
$expectedSha256,
|
|
$family,
|
|
$version,
|
|
$now
|
|
): array {
|
|
$prior = CloudTemplateImportIntent::query()
|
|
->where('user_id', $userId)
|
|
->where('request_idempotency_key', $requestKey)
|
|
->lockForUpdate()->first();
|
|
if ($prior !== null) {
|
|
self::assertReplay($prior, $fingerprint);
|
|
return [
|
|
'created' => false,
|
|
'intent' => CloudTemplateImportReadModel::project($prior),
|
|
];
|
|
}
|
|
$generation = bin2hex(random_bytes(16));
|
|
$templateId = self::newTemplateId($family, $version);
|
|
$intent = new CloudTemplateImportIntent();
|
|
$intent->id = bin2hex(random_bytes(16));
|
|
$intent->user_id = $userId;
|
|
$intent->request_idempotency_key = $requestKey;
|
|
$intent->request_fingerprint = $fingerprint;
|
|
$intent->source_kind = 'remote';
|
|
$intent->name = $name;
|
|
$intent->os_family = $family;
|
|
$intent->os_version = $version;
|
|
$intent->source_filename = self::remoteFilename($url);
|
|
$intent->source_size_bytes = 0;
|
|
$intent->source_last_modified = 0;
|
|
$intent->source_sha256 = $expectedSha256;
|
|
$intent->sealed_source = CloudSecrets::encrypt([
|
|
'kind' => 'remote',
|
|
'url' => $url,
|
|
'expected_sha256' => $expectedSha256,
|
|
], self::SOURCE_PURPOSE);
|
|
$intent->template_id = $templateId;
|
|
$intent->catalog_key = self::catalogKey($family, $version);
|
|
$intent->version_label = gmdate('Ymd-His', $now) . '-'
|
|
. substr($generation, 0, 8);
|
|
$intent->generation_token = $generation;
|
|
$intent->storage_revision = 0;
|
|
$intent->object_key = 'pending-template-import/'
|
|
. (string) $intent->id;
|
|
$intent->multipart_upload_id = '';
|
|
$intent->multipart_part_size_bytes = 0;
|
|
$intent->multipart_part_count = 0;
|
|
$intent->object_version_id = '';
|
|
$intent->object_etag = '';
|
|
$intent->state = 'prepared';
|
|
$intent->active_job_id = null;
|
|
$intent->error_code = '';
|
|
$intent->expires_at = $now + self::INTENT_TTL_SECONDS;
|
|
$intent->completed_at = null;
|
|
$intent->created_at = $intent->updated_at = $now;
|
|
$intent->save();
|
|
return [
|
|
'created' => true,
|
|
'intent' => CloudTemplateImportReadModel::project($intent),
|
|
];
|
|
}, 3);
|
|
} catch (QueryException $error) {
|
|
$prior = CloudTemplateImportIntent::query()
|
|
->where('user_id', $userId)
|
|
->where('request_idempotency_key', $requestKey)->first();
|
|
if ($prior === null) {
|
|
throw $error;
|
|
}
|
|
self::assertReplay($prior, $fingerprint);
|
|
return ['created' => false, 'intent' => CloudTemplateImportReadModel::project($prior)];
|
|
}
|
|
}
|
|
|
|
private function reserveUploadIntent(
|
|
array $actor,
|
|
int $userId,
|
|
string $requestKey,
|
|
string $fingerprint,
|
|
array $canonical,
|
|
int $now
|
|
): array {
|
|
$connection = (new CloudTemplateImportIntent())->getConnection();
|
|
return $connection->transaction(static function () use (
|
|
$actor,
|
|
$userId,
|
|
$requestKey,
|
|
$fingerprint,
|
|
$canonical,
|
|
$now
|
|
): array {
|
|
$prior = CloudTemplateImportIntent::query()
|
|
->where('user_id', $userId)
|
|
->where('request_idempotency_key', $requestKey)
|
|
->lockForUpdate()->first();
|
|
if ($prior !== null) {
|
|
self::assertReplay($prior, $fingerprint);
|
|
return ['created' => false, 'intent' => CloudTemplateImportReadModel::project($prior)];
|
|
}
|
|
|
|
$generation = bin2hex(random_bytes(16));
|
|
$templateId = self::newTemplateId(
|
|
(string) $canonical['os_family'],
|
|
(string) $canonical['os_version']
|
|
);
|
|
$intent = new CloudTemplateImportIntent();
|
|
$intent->id = bin2hex(random_bytes(16));
|
|
$intent->user_id = $userId;
|
|
$intent->request_idempotency_key = $requestKey;
|
|
$intent->request_fingerprint = $fingerprint;
|
|
$intent->source_kind = 'upload';
|
|
$intent->name = (string) $canonical['name'];
|
|
$intent->os_family = (string) $canonical['os_family'];
|
|
$intent->os_version = (string) $canonical['os_version'];
|
|
$intent->source_filename = (string) $canonical['filename'];
|
|
$intent->source_size_bytes = (int) $canonical['size_bytes'];
|
|
$intent->source_last_modified =
|
|
(int) $canonical['last_modified'];
|
|
$intent->source_sha256 = (string) $canonical['sha256'];
|
|
$intent->sealed_source = CloudSecrets::encrypt([
|
|
'kind' => 'upload',
|
|
'filename' => (string) $canonical['filename'],
|
|
'size_bytes' => (int) $canonical['size_bytes'],
|
|
'last_modified' => (int) $canonical['last_modified'],
|
|
], self::SOURCE_PURPOSE);
|
|
$intent->template_id = $templateId;
|
|
$intent->catalog_key = self::catalogKey(
|
|
(string) $canonical['os_family'],
|
|
(string) $canonical['os_version']
|
|
);
|
|
$intent->version_label = gmdate('Ymd-His', $now) . '-'
|
|
. substr($generation, 0, 8);
|
|
$intent->generation_token = $generation;
|
|
$intent->storage_revision = 0;
|
|
$intent->object_key = 'pending-template-import/'
|
|
. (string) $intent->id;
|
|
$intent->multipart_upload_id = '';
|
|
$intent->multipart_part_size_bytes = 0;
|
|
$intent->multipart_part_count = 0;
|
|
$intent->object_version_id = '';
|
|
$intent->object_etag = '';
|
|
$intent->state = 'prepared';
|
|
$intent->active_job_id = null;
|
|
$intent->error_code = '';
|
|
$intent->expires_at = $now + self::INTENT_TTL_SECONDS;
|
|
$intent->completed_at = null;
|
|
$intent->created_at = $intent->updated_at = $now;
|
|
$intent->save();
|
|
return ['created' => true, 'intent' => CloudTemplateImportReadModel::project($intent)];
|
|
}, 3);
|
|
}
|
|
|
|
private static function assertReplay(
|
|
CloudTemplateImportIntent $prior,
|
|
string $fingerprint
|
|
): void {
|
|
if (!hash_equals((string) $prior->request_fingerprint, $fingerprint)) {
|
|
throw new CloudError(
|
|
'同一请求标识不能用于不同的云镜像导入',
|
|
409
|
|
);
|
|
}
|
|
}
|
|
}
|