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.
291 lines
12 KiB
PHP
291 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Services {
|
|
final class Config
|
|
{
|
|
public static string $dataKey = '';
|
|
|
|
public static function get($name, $default = null)
|
|
{
|
|
return $name === 'cloud_data_key' ? self::$dataKey : $default;
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace {
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Models\CloudBackupPeriodEntitlement;
|
|
use App\Models\CloudBackupSubscription;
|
|
use App\Models\CloudInstanceBackup;
|
|
use App\Services\Config;
|
|
use App\Services\Cloud\CloudAutomaticBackupService;
|
|
use App\Services\Cloud\CloudObjectStorageS3Client;
|
|
use App\Services\Cloud\CloudSecrets;
|
|
use GuzzleHttp\Client;
|
|
use GuzzleHttp\Handler\MockHandler;
|
|
use GuzzleHttp\HandlerStack;
|
|
use GuzzleHttp\Middleware;
|
|
use GuzzleHttp\Psr7\Response;
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
|
|
if (!function_exists('sodium_crypto_aead_xchacha20poly1305_ietf_encrypt')) {
|
|
echo "SKIPPED CloudAutomaticBackupRetentionGraceTest: runtime lacks sodium.\n";
|
|
exit(0);
|
|
}
|
|
$assert = static function (bool $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_backup_subscriptions', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->integer('instance_id')->unique();
|
|
$table->integer('user_id');
|
|
$table->integer('offer_id');
|
|
$table->boolean('enabled');
|
|
$table->boolean('renew_next_period');
|
|
$table->string('frequency');
|
|
$table->integer('weekday');
|
|
$table->integer('retention_count');
|
|
$table->string('timezone');
|
|
$table->string('price');
|
|
$table->string('currency');
|
|
$table->integer('price_version');
|
|
$table->integer('state_revision')->default(1);
|
|
$table->integer('next_run_at');
|
|
$table->integer('last_scheduled_at')->nullable();
|
|
$table->integer('last_backup_id')->nullable();
|
|
$table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
});
|
|
$schema->create('cloud_backup_period_entitlements', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->integer('subscription_id');
|
|
$table->integer('service_period_id')->unique();
|
|
$table->integer('instance_id');
|
|
$table->integer('user_id');
|
|
$table->integer('offer_id');
|
|
$table->integer('invoice_id');
|
|
$table->integer('starts_at');
|
|
$table->integer('ends_at');
|
|
$table->string('amount');
|
|
$table->string('full_period_price');
|
|
$table->string('currency');
|
|
$table->integer('price_version');
|
|
$table->string('source');
|
|
$table->integer('paid_at');
|
|
$table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
});
|
|
$schema->create('cloud_instance_backups', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->string('backup_key')->unique();
|
|
$table->integer('source_instance_id');
|
|
$table->integer('source_host_id');
|
|
$table->integer('user_id');
|
|
$table->integer('subscription_id')->nullable();
|
|
$table->string('source_uuid');
|
|
$table->string('name');
|
|
$table->string('purpose');
|
|
$table->string('source');
|
|
$table->string('state');
|
|
$table->string('consistency');
|
|
$table->string('object_key')->unique();
|
|
$table->integer('object_size_bytes');
|
|
$table->integer('virtual_size_bytes');
|
|
$table->string('sha256');
|
|
$table->string('object_etag')->default('');
|
|
$table->string('object_version_id')->default('');
|
|
$table->text('spec_json');
|
|
$table->text('sealed_storage_runtime');
|
|
$table->text('sealed_credential')->nullable();
|
|
$table->integer('storage_revision');
|
|
$table->integer('active_job_id')->nullable();
|
|
$table->string('last_operation_id')->nullable();
|
|
$table->string('delete_operation_id')->nullable();
|
|
$table->integer('delete_generation')->default(0);
|
|
$table->integer('delete_cycle_attempts')->default(0);
|
|
$table->integer('delete_presence_confirmations')->default(0);
|
|
$table->integer('delete_requested_at')->nullable();
|
|
$table->integer('delete_last_checked_at')->nullable();
|
|
$table->integer('delete_next_check_at')->default(0);
|
|
$table->integer('delete_attention_at')->nullable();
|
|
$table->string('error_code');
|
|
$table->integer('scheduled_for')->nullable();
|
|
$table->integer('ready_at')->nullable();
|
|
$table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
});
|
|
$schema->create('cloud_deployment_snapshots', static function (
|
|
Blueprint $table
|
|
): void {
|
|
$table->increments('id');
|
|
$table->integer('backup_id')->unique();
|
|
$table->string('state');
|
|
$table->string('error_code')->default('');
|
|
$table->integer('deleted_at')->nullable();
|
|
$table->integer('updated_at')->default(0);
|
|
});
|
|
foreach (['cloud_backup_restores', 'cloud_backup_recoveries'] as $tableName) {
|
|
$schema->create($tableName, static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->integer('backup_id');
|
|
$table->string('state');
|
|
});
|
|
}
|
|
$schema->create('cloud_jobs', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->integer('backup_id')->nullable();
|
|
$table->string('phase');
|
|
$table->string('effect_resolution');
|
|
$table->boolean('finalization_incident')->default(false);
|
|
});
|
|
|
|
Config::$dataKey = base64_encode(str_repeat('r', 32));
|
|
$sealedRuntime = CloudSecrets::encrypt([
|
|
'config' => [
|
|
'endpoint' => 'https://objects.example.com', 'region' => 'us-east-1',
|
|
'bucket' => 'backup-bucket', 'prefix' => 'virt', 'path_style' => true,
|
|
],
|
|
'credentials' => [
|
|
'access_key_id' => 'RETENTIONACCESS',
|
|
'secret_access_key' => 'retention-secret-value',
|
|
],
|
|
'revision' => 1,
|
|
], 'object-storage-backup-runtime');
|
|
$now = time();
|
|
$newSubscription = static function (int $instanceId, array $endsAt) use ($now): CloudBackupSubscription {
|
|
$subscription = new CloudBackupSubscription();
|
|
foreach ([
|
|
'instance_id' => $instanceId, 'user_id' => 9, 'offer_id' => 3,
|
|
'enabled' => false, 'renew_next_period' => false, 'frequency' => 'daily',
|
|
'weekday' => 0, 'retention_count' => 2, 'timezone' => 'UTC',
|
|
'price' => '2.50', 'currency' => 'USD', 'price_version' => 1,
|
|
'next_run_at' => 0, 'created_at' => $now - 1000,
|
|
'updated_at' => $now - 1000,
|
|
] as $field => $value) {
|
|
$subscription->{$field} = $value;
|
|
}
|
|
$subscription->save();
|
|
foreach ($endsAt as $offset => $end) {
|
|
$entitlement = new CloudBackupPeriodEntitlement();
|
|
foreach ([
|
|
'subscription_id' => (int) $subscription->id,
|
|
'service_period_id' => $instanceId * 10 + $offset,
|
|
'instance_id' => $instanceId, 'user_id' => 9, 'offer_id' => 3,
|
|
'invoice_id' => $instanceId, 'starts_at' => $end - 30 * 86400,
|
|
'ends_at' => $end, 'amount' => '2.50',
|
|
'full_period_price' => '2.50', 'currency' => 'USD',
|
|
'price_version' => 1, 'source' => 'renewal',
|
|
'paid_at' => $now - 1000, 'created_at' => $now - 1000,
|
|
'updated_at' => $now - 1000,
|
|
] as $field => $value) {
|
|
$entitlement->{$field} = $value;
|
|
}
|
|
$entitlement->save();
|
|
}
|
|
return $subscription;
|
|
};
|
|
$newBackup = static function (CloudBackupSubscription $subscription, string $suffix) use ($sealedRuntime, $now): CloudInstanceBackup {
|
|
$backup = new CloudInstanceBackup();
|
|
foreach ([
|
|
'backup_key' => substr(hash('sha256', $suffix), 0, 32),
|
|
'source_instance_id' => (int) $subscription->instance_id,
|
|
'source_host_id' => 2, 'user_id' => 9,
|
|
'subscription_id' => (int) $subscription->id,
|
|
'source_uuid' => '11111111-1111-4111-8111-111111111111',
|
|
'name' => 'autobackup-' . $suffix, 'purpose' => 'portable',
|
|
'source' => 'automatic', 'state' => 'ready', 'consistency' => 'crash',
|
|
'object_key' => 'virt/vm-backups/' . $suffix . '/system.qcow2',
|
|
'object_size_bytes' => 1024, 'virtual_size_bytes' => 2048,
|
|
'sha256' => str_repeat('a', 64),
|
|
'object_etag' => '', 'object_version_id' => '',
|
|
'spec_json' => json_encode([
|
|
'managed' => true, 'os_source' => 'cloud_template',
|
|
'os_name' => 'Debian 13', 'login_name' => 'root',
|
|
'debian_12_network_fix' => false,
|
|
], JSON_THROW_ON_ERROR),
|
|
'sealed_storage_runtime' => $sealedRuntime,
|
|
'sealed_credential' => 'sealed-credential', 'storage_revision' => 1,
|
|
'delete_generation' => 0, 'delete_cycle_attempts' => 0,
|
|
'delete_presence_confirmations' => 0,
|
|
'delete_next_check_at' => 0,
|
|
'error_code' => '', 'scheduled_for' => $now - 1000,
|
|
'ready_at' => $now - 900, 'created_at' => $now - 1000,
|
|
'updated_at' => $now - 1000,
|
|
] as $field => $value) {
|
|
$backup->{$field} = $value;
|
|
}
|
|
$backup->save();
|
|
return $backup;
|
|
};
|
|
|
|
$paid = $newBackup($newSubscription(11, [$now + 3600]), 'paid');
|
|
$insideGrace = $newBackup($newSubscription(12, [$now - 3 * 86400 + 1]), 'inside-grace');
|
|
$expired = $newBackup($newSubscription(13, [$now - 3 * 86400]), 'expired');
|
|
$renewed = $newBackup($newSubscription(14, [$now - 4 * 86400, $now + 20 * 86400]), 'renewed');
|
|
$ordinaryExpired = $newBackup(
|
|
$newSubscription(15, [$now - 3 * 86400]),
|
|
'ordinary-expired'
|
|
);
|
|
$debt = $newBackup(
|
|
$newSubscription(16, [$now - 3 * 86400]),
|
|
'cleanup-debt'
|
|
);
|
|
$debt->state = 'delete_uncertain';
|
|
$debt->object_etag = '"debt-etag"';
|
|
$debt->object_version_id = 'debt-version';
|
|
$debt->delete_generation = 1;
|
|
$debt->delete_cycle_attempts = 1;
|
|
$debt->delete_next_check_at = 0;
|
|
$debt->save();
|
|
$capsule->table('cloud_deployment_snapshots')->insert([
|
|
'backup_id' => (int) $expired->id,
|
|
'state' => 'ready',
|
|
]);
|
|
|
|
$assert(CloudAutomaticBackupService::withinRecoveryGrace($paid, $now)
|
|
&& CloudAutomaticBackupService::withinRecoveryGrace($insideGrace, $now)
|
|
&& !CloudAutomaticBackupService::withinRecoveryGrace($expired, $now)
|
|
&& CloudAutomaticBackupService::withinRecoveryGrace($renewed, $now),
|
|
'Recovery eligibility did not follow the paid period plus three-day grace.');
|
|
$history = [];
|
|
$stack = HandlerStack::create(new MockHandler([
|
|
new Response(404),
|
|
new Response(200, [
|
|
'Content-Length' => '1024',
|
|
'ETag' => '"fresh-etag"',
|
|
'x-amz-version-id' => 'fresh-version',
|
|
]),
|
|
new Response(204),
|
|
]));
|
|
$stack->push(Middleware::history($history));
|
|
$client = new CloudObjectStorageS3Client(new Client(['handler' => $stack]));
|
|
$removed = CloudAutomaticBackupService::pruneRetention(1, $now, $client);
|
|
|
|
foreach ([$paid, $insideGrace, $expired, $renewed, $ordinaryExpired, $debt]
|
|
as $backup) {
|
|
$backup->refresh();
|
|
}
|
|
$assert($removed === 2
|
|
&& (string) $paid->state === 'ready'
|
|
&& (string) $insideGrace->state === 'ready'
|
|
&& (string) $expired->state === 'ready'
|
|
&& (string) $renewed->state === 'ready'
|
|
&& (string) $ordinaryExpired->state === 'deleted'
|
|
&& (string) $debt->state === 'deleted'
|
|
&& count($history) === 3
|
|
&& array_map(static fn ($item): string =>
|
|
$item['request']->getMethod(), $history) === ['HEAD', 'HEAD', 'DELETE'],
|
|
'Cleanup debt starved fresh retention work or entitlement grace was not preserved.');
|
|
|
|
echo "Automatic backup entitlement grace retention tests passed.\n";
|
|
}
|