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.
154 lines
6.3 KiB
PHP
154 lines
6.3 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\Configurations;
|
|
use App\Services\Config;
|
|
use App\Services\Cloud\CloudError;
|
|
use App\Services\Cloud\CloudObjectStorageConfig;
|
|
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 CloudObjectStorageConfigTest: 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 object-storage error status: ' . $error->httpStatus);
|
|
return $error;
|
|
}
|
|
throw new RuntimeException('Expected object-storage error was not raised.');
|
|
};
|
|
|
|
$capsule = new Capsule();
|
|
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:']);
|
|
$capsule->setAsGlobal();
|
|
$capsule->bootEloquent();
|
|
$capsule->schema()->create('configurations', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->string('name')->unique();
|
|
$table->text('value')->nullable();
|
|
});
|
|
Config::$dataKey = base64_encode(str_repeat('o', 32));
|
|
|
|
$payload = [
|
|
'name' => '测试对象存储',
|
|
'endpoint' => 'https://minio.internal:9000',
|
|
'region' => 'us-east-1',
|
|
'bucket' => 'test-bucket',
|
|
'prefix' => 'virt-cloud/backups',
|
|
'path_style' => 'true',
|
|
'access_key_id' => 'TESTACCESSKEY',
|
|
'secret_access_key' => 'test-secret-key-value',
|
|
'expected_revision' => 0,
|
|
];
|
|
$saved = CloudObjectStorageConfig::save($payload);
|
|
$databaseValues = Configurations::pluck('value')->map(
|
|
static fn ($value): string => (string) $value
|
|
)->implode("\n");
|
|
$publicJson = json_encode($saved, JSON_THROW_ON_ERROR);
|
|
$assert($saved['revision'] === 1 && !$saved['enabled'] && !$saved['ready']
|
|
&& $saved['credentials_configured']
|
|
&& !str_contains($databaseValues, 'TESTACCESSKEY')
|
|
&& !str_contains($databaseValues, 'test-secret-key-value')
|
|
&& !str_contains($publicJson, 'TESTACCESSKEY')
|
|
&& !str_contains($publicJson, 'test-secret-key-value')
|
|
&& !array_key_exists('sealed_credentials', $saved)
|
|
&& !array_key_exists('access_key_id', $saved)
|
|
&& !array_key_exists('secret_access_key', $saved),
|
|
'Object-storage save did not seal credentials or leaked them in its DTO.');
|
|
|
|
$healthyClient = new CloudObjectStorageS3Client(new Client([
|
|
'handler' => HandlerStack::create(new MockHandler([new Response(200)])),
|
|
]));
|
|
$ready = CloudObjectStorageConfig::testAndEnable(1, $healthyClient);
|
|
$assert($ready['enabled'] && $ready['ready']
|
|
&& $ready['tested_revision'] === 1
|
|
&& $ready['last_test_status'] === 'healthy',
|
|
'Successful current-revision connectivity test did not enable storage.');
|
|
|
|
$changed = $payload;
|
|
$changed['bucket'] = 'second-bucket';
|
|
$changed['access_key_id'] = '';
|
|
$changed['secret_access_key'] = '';
|
|
$changed['expected_revision'] = '1';
|
|
$changedState = CloudObjectStorageConfig::save($changed);
|
|
$assert($changedState['revision'] === 2 && !$changedState['enabled']
|
|
&& !$changedState['ready'] && $changedState['last_test_status'] === 'never'
|
|
&& $changedState['credentials_configured'],
|
|
'Configuration change did not preserve sealed credentials and invalidate readiness.');
|
|
|
|
$expectCloudError(static function () use ($changed): void {
|
|
$changed['expected_revision'] = '1';
|
|
CloudObjectStorageConfig::save($changed);
|
|
}, 409);
|
|
$expectCloudError(static function () use ($changed): void {
|
|
$changed['endpoint'] = 'http://127.0.0.1:9000';
|
|
$changed['expected_revision'] = '2';
|
|
CloudObjectStorageConfig::save($changed);
|
|
}, 422);
|
|
|
|
$deniedClient = new CloudObjectStorageS3Client(new Client([
|
|
'handler' => HandlerStack::create(new MockHandler([new Response(403)])),
|
|
]));
|
|
$denied = $expectCloudError(static function () use ($deniedClient): void {
|
|
CloudObjectStorageConfig::testAndEnable(2, $deniedClient);
|
|
}, 502);
|
|
$failed = CloudObjectStorageConfig::view();
|
|
$assert($failed['last_test_status'] === 'failed'
|
|
&& $failed['last_error_code'] === 'auth_or_permission_denied'
|
|
&& !$failed['enabled'] && !$failed['ready']
|
|
&& !str_contains($denied->getMessage(), 'test-secret'),
|
|
'Failed test was not persisted safely or exposed credential material.');
|
|
|
|
$recovered = CloudObjectStorageConfig::testAndEnable(2,
|
|
new CloudObjectStorageS3Client(new Client([
|
|
'handler' => HandlerStack::create(new MockHandler([new Response(200)])),
|
|
])));
|
|
$assert($recovered['ready'], 'A later successful test did not recover readiness.');
|
|
$disabled = CloudObjectStorageConfig::disable('2');
|
|
$assert($disabled['revision'] === 3 && !$disabled['enabled']
|
|
&& !$disabled['ready'] && $disabled['credentials_configured']
|
|
&& $disabled['last_test_status'] === 'disabled'
|
|
&& $disabled['status_label'] === '已停用',
|
|
'Disable did not invalidate an in-flight test generation or preserve credentials.');
|
|
|
|
$ipv6Virtual = $changed;
|
|
$ipv6Virtual['endpoint'] = 'https://[2001:db8::2]:9000';
|
|
$ipv6Virtual['path_style'] = 'false';
|
|
$ipv6Virtual['expected_revision'] = '3';
|
|
$expectCloudError(static function () use ($ipv6Virtual): void {
|
|
CloudObjectStorageConfig::save($ipv6Virtual);
|
|
}, 422);
|
|
|
|
echo "Object-storage encrypted configuration and revision gate passed.\n";
|
|
}
|