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.
virt/web/tests/CloudObjectStorageBackendRe...

286 lines
11 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\CloudStorageBackend;
use App\Services\Config;
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudObjectStorageBackendRegistry;
use App\Services\Cloud\CloudObjectStorageS3Client;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
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 CloudObjectStorageBackendRegistryTest: runtime lacks sodium.\n";
exit(0);
}
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$expectCloudError = static function (
callable $callback,
int $status
) use ($assert): CloudError {
try {
$callback();
} catch (CloudError $error) {
$assert($error->httpStatus === $status,
'Unexpected registry error status: ' . $error->httpStatus);
return $error;
}
throw new RuntimeException('Expected registry error was not raised.');
};
$s3 = static function (int $status): CloudObjectStorageS3Client {
return new CloudObjectStorageS3Client(new Client([
'handler' => HandlerStack::create(new MockHandler([
new Response($status),
])),
]));
};
$capsule = new Capsule();
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:']);
$capsule->setAsGlobal();
$capsule->bootEloquent();
$capsule->schema()->create('cloud_storage_backends', static function (Blueprint $table): void {
$table->increments('id');
$table->string('backend_key')->unique();
$table->string('identity_sha256')->unique();
$table->string('name');
$table->string('driver');
$table->string('endpoint');
$table->string('region');
$table->string('bucket');
$table->string('prefix');
$table->boolean('path_style');
$table->string('failure_domain');
$table->text('sealed_credentials');
$table->unsignedBigInteger('config_revision');
$table->unsignedBigInteger('tested_revision');
$table->string('admin_state');
$table->string('health_state');
$table->unsignedInteger('write_priority');
$table->unsignedInteger('consecutive_failures');
$table->unsignedInteger('consecutive_successes');
$table->string('last_error_code');
$table->unsignedBigInteger('last_tested_at');
$table->unsignedBigInteger('last_healthy_at');
$table->unsignedBigInteger('next_probe_at');
$table->unsignedBigInteger('created_at');
$table->unsignedBigInteger('updated_at');
});
$capsule->schema()->create('cloud_storage_backend_catalog_mutex', static function (Blueprint $table): void {
$table->unsignedTinyInteger('id')->primary();
$table->unsignedBigInteger('updated_at');
});
Capsule::table('cloud_storage_backend_catalog_mutex')->insert([
'id' => 1,
'updated_at' => 0,
]);
$capsule->schema()->create('cloud_storage_replicas', static function (Blueprint $table): void {
$table->increments('id');
$table->unsignedBigInteger('backend_id');
});
Config::$dataKey = base64_encode(str_repeat('r', 32));
$primaryInput = [
'name' => 'Primary',
'endpoint' => 'https://primary.storage.example',
'region' => 'us-east-1',
'bucket' => 'primary-bucket',
'prefix' => 'virt-cloud',
'path_style' => 'true',
'failure_domain' => 'Provider-A',
'access_key_id' => 'PRIMARYKEY',
'secret_access_key' => 'primary-secret-value',
];
$expectCloudError(static fn () => CloudObjectStorageBackendRegistry::create(
array_replace($primaryInput, [
'name' => 'Missing Domain',
'endpoint' => 'https://missing-domain.storage.example',
'bucket' => 'missing-domain-bucket',
'failure_domain' => '',
]),
1
), 422);
$createdPrimary = CloudObjectStorageBackendRegistry::create(
$primaryInput,
1
);
$testedPrimary = CloudObjectStorageBackendRegistry::test(
(int) $createdPrimary['id'],
1,
1,
$s3(200)
);
$imported = CloudObjectStorageBackendRegistry::activate(
(int) $createdPrimary['id'],
1,
1
);
$assert($testedPrimary['health_state'] === 'healthy'
&& $imported['admin_state'] === 'active'
&& $imported['ready'] && $imported['revision'] === 2,
'Primary backend did not require explicit test and activation.');
$database = CloudStorageBackend::firstOrFail();
$databaseJson = json_encode($database->toArray(), JSON_THROW_ON_ERROR);
$publicJson = json_encode($imported, JSON_THROW_ON_ERROR);
$assert(!str_contains((string) $database->sealed_credentials, 'PRIMARYKEY')
&& !str_contains((string) $database->sealed_credentials, 'primary-secret')
&& !str_contains($databaseJson, 'PRIMARYKEY')
&& !str_contains($publicJson, 'PRIMARYKEY')
&& !array_key_exists('sealed_credentials', $imported),
'Backend registry leaked plaintext or sealed credentials.');
$assert((string) $database->failure_domain === 'provider-a',
'Failure-domain identity was not normalized.');
$metadata = CloudObjectStorageBackendRegistry::update(
(int) $imported['id'],
[
'name' => 'Primary Renamed',
'endpoint' => $imported['endpoint'],
'region' => $imported['region'],
'bucket' => $imported['bucket'],
'prefix' => $imported['prefix'],
'path_style' => $imported['path_style'],
'failure_domain' => 'provider-a',
'write_priority' => 5,
],
2,
1
);
$assert($metadata['revision'] === 3 && $metadata['tested_revision'] === 3
&& $metadata['ready'] && $metadata['admin_state'] === 'active',
'Metadata-only update incorrectly invalidated tested runtime evidence.');
$expectCloudError(static fn () => CloudObjectStorageBackendRegistry::update(
(int) $imported['id'], ['name' => 'stale'], 2, 1
), 409);
$secondaryInput = [
'name' => 'Secondary',
'endpoint' => 'https://secondary.storage.example',
'region' => 'us-west-2',
'bucket' => 'secondary-bucket',
'prefix' => 'virt-cloud',
'path_style' => false,
'failure_domain' => 'provider-b',
'write_priority' => 20,
'access_key_id' => 'SECONDARYKEY',
'secret_access_key' => 'secondary-secret-value',
];
$secondary = CloudObjectStorageBackendRegistry::create($secondaryInput, 1);
$assert($secondary['admin_state'] === 'draft'
&& $secondary['health_state'] === 'unknown'
&& !$secondary['ready'],
'New backend bypassed explicit connectivity test and activation.');
$tested = CloudObjectStorageBackendRegistry::test(
(int) $secondary['id'], 1, 1, $s3(200)
);
$assert($tested['health_state'] === 'healthy'
&& $tested['tested_revision'] === 1
&& $tested['admin_state'] === 'draft'
&& $tested['can_activate'],
'Connectivity test silently activated storage or lost health evidence.');
$active = CloudObjectStorageBackendRegistry::activate(
(int) $secondary['id'], 1, 1
);
$assert($active['admin_state'] === 'active' && $active['revision'] === 2
&& CloudObjectStorageBackendRegistry::summary()['writable_backends'] === 2,
'Explicit activation did not expose two healthy writable backends.');
$third = CloudObjectStorageBackendRegistry::create(
array_replace($secondaryInput, [
'name' => 'Third',
'endpoint' => 'https://third.storage.example',
'bucket' => 'third-bucket',
]),
1
);
$assert($third['admin_state'] === 'draft'
&& CloudStorageBackend::count() === 3,
'A replacement backend could not be registered beside two live rows.');
CloudObjectStorageBackendRegistry::test(
(int) $third['id'], 1, 1, $s3(200)
);
$expectCloudError(static fn () => CloudObjectStorageBackendRegistry::activate(
(int) $third['id'], 1, 1
), 409);
$assert(CloudObjectStorageBackendRegistry::summary()['active_backends'] === 2,
'The registry allowed more than two active storage backends.');
Capsule::table('cloud_storage_replicas')->insert([
'backend_id' => (int) $secondary['id'],
]);
$expectCloudError(static fn () => CloudObjectStorageBackendRegistry::update(
(int) $secondary['id'],
array_replace($secondaryInput, [
'endpoint' => 'https://replacement.storage.example',
'access_key_id' => '',
'secret_access_key' => '',
]),
2,
1
), 409);
$failed = $expectCloudError(static fn () =>
CloudObjectStorageBackendRegistry::test(
(int) $secondary['id'], 2, 1, $s3(503)
), 502);
$failedState = CloudObjectStorageBackendRegistry::listView()[1];
$assert($failedState['admin_state'] === 'read_only'
&& $failedState['health_state'] === 'unreachable'
&& !$failedState['ready']
&& !str_contains($failed->getMessage(), 'secondary-secret'),
'Failed health test did not stop new writes or leaked credentials.');
$expectCloudError(static fn () => CloudObjectStorageBackendRegistry::activate(
(int) $secondary['id'], 2, 1
), 409);
$retested = CloudObjectStorageBackendRegistry::test(
(int) $secondary['id'], 2, 1, $s3(200)
);
$reactivated = CloudObjectStorageBackendRegistry::activate(
(int) $secondary['id'], 2, 1
);
$draining = CloudObjectStorageBackendRegistry::drain(
(int) $secondary['id'], 3, 1
);
$resumed = CloudObjectStorageBackendRegistry::resume(
(int) $secondary['id'], 4, 1
);
$disabled = CloudObjectStorageBackendRegistry::disable(
(int) $secondary['id'], 5, 1
);
$runtime = CloudObjectStorageBackendRegistry::runtime(
(int) $secondary['id'], true
);
$assert($retested['ready'] && $reactivated['admin_state'] === 'active'
&& $draining['admin_state'] === 'draining'
&& $resumed['admin_state'] === 'active'
&& $disabled['admin_state'] === 'disabled'
&& $runtime['credentials']['access_key_id'] === 'SECONDARYKEY',
'Backend lifecycle lost exact historical read capability.');
echo "Object-storage backend registry contracts passed.\n";
}