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.
117 lines
4.4 KiB
PHP
117 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudInstanceBackup;
|
|
use App\Models\CloudStorageArtifact;
|
|
use App\Models\CloudStoragePublication;
|
|
|
|
/** Secret-free immutable storage authority for a portable backup. */
|
|
final class CloudBackupStorageAuthority
|
|
{
|
|
/**
|
|
* Validate the replicated storage ledger at an admission boundary.
|
|
*
|
|
* @return array{mode:string,identity_sha256:string,
|
|
* source_storage_revision:int}
|
|
*/
|
|
public static function readyEvidence(CloudInstanceBackup $backup): array
|
|
{
|
|
return self::evidence($backup, true);
|
|
}
|
|
|
|
/** Stable business identity shared by order and recovery validation. */
|
|
public static function backingBackupIdentity(
|
|
CloudInstanceBackup $backup
|
|
): string {
|
|
$authority = self::evidence($backup, false);
|
|
return self::hash([
|
|
'id' => (int) $backup->id,
|
|
'backup_key' => (string) $backup->backup_key,
|
|
'source_instance_id' => (int) $backup->source_instance_id,
|
|
'source_host_id' => (int) $backup->source_host_id,
|
|
'user_id' => (int) $backup->user_id,
|
|
'source_uuid' => (string) $backup->source_uuid,
|
|
'purpose' => (string) $backup->purpose,
|
|
'source' => (string) $backup->source,
|
|
'subscription_id' => (int) $backup->subscription_id,
|
|
'object_size_bytes' => (int) $backup->object_size_bytes,
|
|
'virtual_size_bytes' => (int) $backup->virtual_size_bytes,
|
|
'sha256' => (string) $backup->sha256,
|
|
'source_portable_restore_contract_json' =>
|
|
(string) $backup->source_portable_restore_contract_json,
|
|
'source_portable_restore_contract_sha256' =>
|
|
(string) $backup->source_portable_restore_contract_sha256,
|
|
'storage_artifact_id' => (int) $backup->storage_artifact_id,
|
|
'storage_authority_identity_sha256' =>
|
|
$authority['identity_sha256'],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @return array{mode:string,identity_sha256:string,
|
|
* source_storage_revision:int}
|
|
*/
|
|
private static function evidence(
|
|
CloudInstanceBackup $backup,
|
|
bool $requireReady
|
|
): array {
|
|
$size = (int) $backup->object_size_bytes;
|
|
$sha256 = (string) $backup->sha256;
|
|
$artifactId = filter_var(
|
|
$backup->storage_artifact_id,
|
|
FILTER_VALIDATE_INT,
|
|
['options' => ['min_range' => 1]]
|
|
);
|
|
if ($size < 1
|
|
|| preg_match('/^[a-f0-9]{64}$/D', $sha256) !== 1
|
|
|| $artifactId === false) {
|
|
throw new \InvalidArgumentException(
|
|
'Backup replicated storage identity is invalid'
|
|
);
|
|
}
|
|
|
|
$artifactQuery = CloudStorageArtifact::query()
|
|
->whereKey((int) $artifactId);
|
|
$publicationQuery = CloudStoragePublication::query()
|
|
->where('artifact_id', (int) $artifactId);
|
|
if ($backup->getConnection()->transactionLevel() > 0) {
|
|
$artifactQuery->lockForUpdate();
|
|
$publicationQuery->lockForUpdate();
|
|
}
|
|
$artifact = $artifactQuery->first();
|
|
$publication = $publicationQuery->first();
|
|
if (!$artifact instanceof CloudStorageArtifact
|
|
|| !$publication instanceof CloudStoragePublication
|
|
|| (int) $publication->backup_id !== (int) $backup->id
|
|
|| (string) $artifact->kind !== 'backup'
|
|
|| (int) $artifact->size_bytes !== $size
|
|
|| !hash_equals((string) $artifact->sha256, $sha256)
|
|
|| ($requireReady && !in_array((string) $artifact->state,
|
|
['available', 'protected'], true))) {
|
|
throw new \InvalidArgumentException(
|
|
'Backup replicated storage evidence is invalid'
|
|
);
|
|
}
|
|
return [
|
|
'mode' => 'artifact',
|
|
'identity_sha256' => hash('sha256', json_encode([
|
|
'authority' => 'storage_artifact',
|
|
'artifact_id' => (int) $artifactId,
|
|
'object_size_bytes' => $size,
|
|
'object_sha256' => $sha256,
|
|
], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES)),
|
|
'source_storage_revision' => 0,
|
|
];
|
|
}
|
|
|
|
/** @param array<string,mixed> $payload */
|
|
private static function hash(array $payload): string
|
|
{
|
|
return hash('sha256', json_encode(
|
|
$payload,
|
|
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
|
|
));
|
|
}
|
|
}
|