|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Settings\Tests\SetupChecks; |
| 11 | + |
| 12 | +use OCA\Settings\SetupChecks\S3SseCEncryption; |
| 13 | +use OCP\IConfig; |
| 14 | +use OCP\IL10N; |
| 15 | +use OCP\IURLGenerator; |
| 16 | +use OCP\SetupCheck\SetupResult; |
| 17 | +use PHPUnit\Framework\MockObject\MockObject; |
| 18 | +use Test\TestCase; |
| 19 | + |
| 20 | +class S3SseCEncryptionTest extends TestCase { |
| 21 | + private IL10N&MockObject $l10n; |
| 22 | + private IConfig&MockObject $config; |
| 23 | + private IURLGenerator&MockObject $urlGenerator; |
| 24 | + |
| 25 | + #[\Override] |
| 26 | + protected function setUp(): void { |
| 27 | + parent::setUp(); |
| 28 | + |
| 29 | + $this->l10n = $this->createMock(IL10N::class); |
| 30 | + $this->l10n->method('t') |
| 31 | + ->willReturnCallback(function ($message, array $replace = []) { |
| 32 | + return vsprintf($message, $replace); |
| 33 | + }); |
| 34 | + $this->config = $this->createMock(IConfig::class); |
| 35 | + $this->urlGenerator = $this->createMock(IURLGenerator::class); |
| 36 | + } |
| 37 | + |
| 38 | + public static function dataRun(): array { |
| 39 | + $s3 = static fn (array $arguments): array => ['class' => 'OC\\Files\\ObjectStore\\S3', 'arguments' => $arguments]; |
| 40 | + |
| 41 | + return [ |
| 42 | + 'no objectstore configured' => [null, null, SetupResult::SUCCESS], |
| 43 | + 'non-S3 objectstore class' => [['class' => 'OC\\Files\\ObjectStore\\Swift', 'arguments' => ['sse_c_key' => 'x']], null, SetupResult::SUCCESS], |
| 44 | + 'S3 with no sse* arguments' => [$s3([]), null, SetupResult::SUCCESS], |
| 45 | + 'S3 with sse-kms' => [$s3(['sse' => 'sse-kms']), null, SetupResult::SUCCESS], |
| 46 | + 'S3 with sse-kms-dsse' => [$s3(['sse' => 'sse-kms-dsse']), null, SetupResult::SUCCESS], |
| 47 | + 'S3 with legacy sse_c_key only' => [$s3(['sse_c_key' => 'x']), null, SetupResult::WARNING], |
| 48 | + 'S3 with explicit sse => sse-c' => [$s3(['sse' => 'sse-c', 'sse_c_key' => 'x']), null, SetupResult::WARNING], |
| 49 | + 'S3 with both legacy keys set (sse_c_key wins by precedence)' => [$s3(['sse_c_key' => 'x', 'sse_kms_enabled' => true]), null, SetupResult::WARNING], |
| 50 | + 'S3 with explicit sse overriding legacy sse_c_key' => [$s3(['sse' => 'sse-kms', 'sse_c_key' => 'x']), null, SetupResult::SUCCESS], |
| 51 | + 'multibucket S3 with legacy sse_c_key only' => [null, $s3(['sse_c_key' => 'x']), SetupResult::WARNING], |
| 52 | + 'multibucket S3 with sse-kms' => [null, $s3(['sse' => 'sse-kms']), SetupResult::SUCCESS], |
| 53 | + 'S3 with explicit empty sse overriding legacy sse_c_key' => [$s3(['sse' => '', 'sse_c_key' => 'x']), null, SetupResult::SUCCESS], |
| 54 | + 'new-style map with sse-c on the default store' => [['default' => 'server1', 'server1' => $s3(['sse' => 'sse-c', 'sse_c_key' => 'x'])], null, SetupResult::WARNING], |
| 55 | + 'new-style map with sse-c on a non-default store' => [['default' => 'server1', 'server1' => $s3([]), 'server2' => $s3(['sse_c_key' => 'x'])], null, SetupResult::WARNING], |
| 56 | + 'new-style map without sse-c' => [['default' => 'server1', 'server1' => $s3(['sse' => 'sse-kms'])], null, SetupResult::SUCCESS], |
| 57 | + 'new-style map skipping non-S3 and malformed entries' => [['default' => 'server1', 'server1' => ['class' => 'OC\\Files\\ObjectStore\\Swift', 'arguments' => ['sse_c_key' => 'x']], 'server2' => 'invalid', 'server3' => ['class' => 'OC\\Files\\ObjectStore\\S3']], null, SetupResult::SUCCESS], |
| 58 | + 'new-style multibucket map with sse-c' => [null, ['default' => 'server1', 'server1' => $s3(['sse_c_key' => 'x'])], SetupResult::WARNING], |
| 59 | + ]; |
| 60 | + } |
| 61 | + |
| 62 | + #[\PHPUnit\Framework\Attributes\DataProvider('dataRun')] |
| 63 | + public function testRun(?array $objectStore, ?array $objectStoreMultibucket, string $expected): void { |
| 64 | + $this->urlGenerator->method('linkToDocs')->willReturn('admin-s3-sse-c'); |
| 65 | + |
| 66 | + $this->config->method('getSystemValue') |
| 67 | + ->willReturnCallback(function (string $key, $default = null) use ($objectStore, $objectStoreMultibucket) { |
| 68 | + return match ($key) { |
| 69 | + 'objectstore' => $objectStore ?? $default, |
| 70 | + 'objectstore_multibucket' => $objectStoreMultibucket ?? $default, |
| 71 | + default => $default, |
| 72 | + }; |
| 73 | + }); |
| 74 | + |
| 75 | + $check = new S3SseCEncryption($this->l10n, $this->config, $this->urlGenerator); |
| 76 | + |
| 77 | + $result = $check->run(); |
| 78 | + $this->assertEquals($expected, $result->getSeverity()); |
| 79 | + } |
| 80 | +} |
0 commit comments