Skip to content

Commit bc6ecd9

Browse files
committed
Add interface for setup checks
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
1 parent 8dd2499 commit bc6ecd9

File tree

11 files changed

+160
-23
lines changed

11 files changed

+160
-23
lines changed

apps/settings/lib/Controller/CheckSetupController.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
use OCP\IURLGenerator;
7171
use OCP\Lock\ILockingProvider;
7272
use OCP\Security\ISecureRandom;
73+
use OCP\Settings\SetupChecks\ISetupCheck;
74+
use Psr\Container\ContainerExceptionInterface;
7375
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
7476
use Symfony\Component\EventDispatcher\GenericEvent;
7577

@@ -688,9 +690,12 @@ protected function isEnoughTempSpaceAvailableIfS3PrimaryStorageIsUsed(): bool {
688690
* @return DataResponse
689691
*/
690692
public function check() {
691-
$phpDefaultCharset = new PhpDefaultCharset();
692-
$phpOutputBuffering = new PhpOutputBuffering();
693-
$legacySSEKeyFormat = new LegacySSEKeyFormat($this->l10n, $this->config, $this->urlGenerator);
693+
$newChecks = $this->runSetupChecks([
694+
PhpDefaultCharset::class,
695+
PhpOutputBuffering::class,
696+
LegacySSEKeyFormat::class
697+
]);
698+
694699
return new DataResponse(
695700
[
696701
'isGetenvServerWorking' => !empty(getenv('PATH')),
@@ -731,10 +736,30 @@ public function check() {
731736
'isMysqlUsedWithoutUTF8MB4' => $this->isMysqlUsedWithoutUTF8MB4(),
732737
'isEnoughTempSpaceAvailableIfS3PrimaryStorageIsUsed' => $this->isEnoughTempSpaceAvailableIfS3PrimaryStorageIsUsed(),
733738
'reverseProxyGeneratedURL' => $this->urlGenerator->getAbsoluteURL('index.php'),
734-
PhpDefaultCharset::class => ['pass' => $phpDefaultCharset->run(), 'description' => $phpDefaultCharset->description(), 'severity' => $phpDefaultCharset->severity()],
735-
PhpOutputBuffering::class => ['pass' => $phpOutputBuffering->run(), 'description' => $phpOutputBuffering->description(), 'severity' => $phpOutputBuffering->severity()],
736-
LegacySSEKeyFormat::class => ['pass' => $legacySSEKeyFormat->run(), 'description' => $legacySSEKeyFormat->description(), 'severity' => $legacySSEKeyFormat->severity(), 'linkToDocumentation' => $legacySSEKeyFormat->linkToDocumentation()],
737-
]
739+
] + $newChecks
738740
);
739741
}
742+
743+
protected function runSetupChecks(array $checks): array {
744+
$result = [];
745+
746+
foreach ($checks as $check) {
747+
try {
748+
/** @var ISetupCheck $instance */
749+
$instance = \OC::$server->get($check);
750+
} catch (ContainerExceptionInterface $e) {
751+
$this->logger->logException($e);
752+
continue;
753+
}
754+
755+
$result[$check] = [
756+
'pass' => $instance->passes(),
757+
'description' => $instance->description(),
758+
'severity' => $instance->severity(),
759+
'linkToDocumentation' => $instance->linkToDocumentation(),
760+
];
761+
}
762+
763+
return $result;
764+
}
740765
}

apps/settings/lib/SetupChecks/LegacySSEKeyFormat.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
use OCP\IConfig;
3030
use OCP\IL10N;
3131
use OCP\IURLGenerator;
32+
use OCP\Settings\SetupChecks\ISetupCheck;
3233

33-
class LegacySSEKeyFormat {
34+
class LegacySSEKeyFormat implements ISetupCheck {
3435
/** @var IL10N */
3536
private $l10n;
3637
/** @var IConfig */
@@ -49,14 +50,14 @@ public function description(): string {
4950
}
5051

5152
public function severity(): string {
52-
return 'warning';
53+
return ISetupCheck::SEVERITY_WARNING;
5354
}
5455

55-
public function run(): bool {
56+
public function passes(): bool {
5657
return $this->config->getSystemValueBool('encryption.legacy_format_support', false) === false;
5758
}
5859

59-
public function linkToDocumentation(): string {
60+
public function linkToDocumentation(): ?string {
6061
return $this->urlGenerator->linkToDocs('admin-sse-legacy-format');
6162
}
6263
}

apps/settings/lib/SetupChecks/PhpDefaultCharset.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,22 @@
2626

2727
namespace OCA\Settings\SetupChecks;
2828

29-
class PhpDefaultCharset {
29+
use OCP\Settings\SetupChecks\ISetupCheck;
30+
31+
class PhpDefaultCharset implements ISetupCheck {
3032
public function description(): string {
3133
return 'PHP configuration option default_charset should be UTF-8';
3234
}
3335

3436
public function severity(): string {
35-
return 'warning';
37+
return ISetupCheck::SEVERITY_WARNING;
3638
}
3739

38-
public function run(): bool {
40+
public function passes(): bool {
3941
return strtoupper(trim(ini_get('default_charset'))) === 'UTF-8';
4042
}
43+
44+
public function linkToDocumentation(): ?string {
45+
return null;
46+
}
4147
}

apps/settings/lib/SetupChecks/PhpOutputBuffering.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,23 @@
2626

2727
namespace OCA\Settings\SetupChecks;
2828

29-
class PhpOutputBuffering {
29+
use OCP\Settings\SetupChecks\ISetupCheck;
30+
31+
class PhpOutputBuffering implements ISetupCheck {
3032
public function description(): string {
3133
return 'PHP configuration option output_buffering must be disabled';
3234
}
3335

3436
public function severity(): string {
35-
return 'error';
37+
return ISetupCheck::SEVERITY_ERROR;
3638
}
3739

38-
public function run(): bool {
40+
public function passes(): bool {
3941
$value = trim(ini_get('output_buffering'));
4042
return $value === '' || $value === '0';
4143
}
44+
45+
public function linkToDocumentation(): ?string {
46+
return null;
47+
}
4248
}

apps/settings/tests/Controller/CheckSetupControllerTest.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,9 @@ public function testCheck() {
536536
if ($key === 'admin-db-conversion') {
537537
return 'http://docs.example.org/server/go.php?to=admin-db-conversion';
538538
}
539+
if ($key === 'admin-sse-legacy-format') {
540+
return 'http://docs.example.org/server/go.php?to=admin-sse-legacy-format';
541+
}
539542
return '';
540543
});
541544

@@ -550,6 +553,11 @@ public function testCheck() {
550553
return '';
551554
});
552555

556+
$this->overwriteService(IURLGenerator::class, $this->urlGenerator);
557+
// OC::$server->registerService(IURLGenerator::class, function () {
558+
// return $this->urlGenerator;
559+
// });
560+
553561
$expected = new DataResponse(
554562
[
555563
'isGetenvServerWorking' => true,
@@ -597,9 +605,9 @@ public function testCheck() {
597605
'isMysqlUsedWithoutUTF8MB4' => false,
598606
'isEnoughTempSpaceAvailableIfS3PrimaryStorageIsUsed' => true,
599607
'reverseProxyGeneratedURL' => 'https://server/index.php',
600-
'OCA\Settings\SetupChecks\PhpDefaultCharset' => ['pass' => true, 'description' => 'PHP configuration option default_charset should be UTF-8', 'severity' => 'warning'],
601-
'OCA\Settings\SetupChecks\PhpOutputBuffering' => ['pass' => true, 'description' => 'PHP configuration option output_buffering must be disabled', 'severity' => 'error'],
602-
'OCA\Settings\SetupChecks\LegacySSEKeyFormat' => ['pass' => true, 'description' => 'The old server-side-encryption format is enabled. We recommend disabling this.', 'severity' => 'warning', 'linkToDocumentation' => ''],
608+
'OCA\Settings\SetupChecks\PhpDefaultCharset' => ['pass' => true, 'description' => 'PHP configuration option default_charset should be UTF-8', 'severity' => 'warning', 'linkToDocumentation' => null],
609+
'OCA\Settings\SetupChecks\PhpOutputBuffering' => ['pass' => true, 'description' => 'PHP configuration option output_buffering must be disabled', 'severity' => 'error', 'linkToDocumentation' => null],
610+
'OCA\Settings\SetupChecks\LegacySSEKeyFormat' => ['pass' => true, 'description' => 'The old server-side-encryption format is enabled. We recommend disabling this.', 'severity' => 'warning', 'linkToDocumentation' => 'http://docs.example.org/server/go.php?to=admin-sse-legacy-format'],
603611
]
604612
);
605613
$this->assertEquals($expected, $this->checkSetupController->check());

apps/settings/tests/SetupChecks/PhpDefaultCharsetTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@
3232
class PhpDefaultCharsetTest extends TestCase {
3333
public function testPass(): void {
3434
$check = new PhpDefaultCharset();
35-
$this->assertTrue($check->run());
35+
$this->assertTrue($check->passes());
3636
}
3737

3838
public function testFail(): void {
3939
ini_set('default_charset', 'ISO-8859-15');
4040

4141
$check = new PhpDefaultCharset();
42-
$this->assertFalse($check->run());
42+
$this->assertFalse($check->passes());
4343

4444
ini_restore('default_charset');
4545
}

apps/settings/tests/SetupChecks/PhpOutputBufferingTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ class PhpOutputBufferingTest extends TestCase {
3737

3838
public function testPass(): void {
3939
$check = new PhpOutputBuffering();
40-
$this->assertTrue($check->run());
40+
$this->assertTrue($check->passes());
4141
}
4242
}

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,7 @@
465465
'OCP\\Settings\\ISection' => $baseDir . '/lib/public/Settings/ISection.php',
466466
'OCP\\Settings\\ISettings' => $baseDir . '/lib/public/Settings/ISettings.php',
467467
'OCP\\Settings\\ISubAdminSettings' => $baseDir . '/lib/public/Settings/ISubAdminSettings.php',
468+
'OCP\\Settings\\SetupChecks\\ISetupCheck' => $baseDir . '/lib/public/Settings/SetupChecks/ISetupCheck.php',
468469
'OCP\\Share' => $baseDir . '/lib/public/Share.php',
469470
'OCP\\Share\\Events\\ShareCreatedEvent' => $baseDir . '/lib/public/Share/Events/ShareCreatedEvent.php',
470471
'OCP\\Share\\Events\\VerifyMountPointEvent' => $baseDir . '/lib/public/Share/Events/VerifyMountPointEvent.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
494494
'OCP\\Settings\\ISection' => __DIR__ . '/../../..' . '/lib/public/Settings/ISection.php',
495495
'OCP\\Settings\\ISettings' => __DIR__ . '/../../..' . '/lib/public/Settings/ISettings.php',
496496
'OCP\\Settings\\ISubAdminSettings' => __DIR__ . '/../../..' . '/lib/public/Settings/ISubAdminSettings.php',
497+
'OCP\\Settings\\SetupChecks\\ISetupCheck' => __DIR__ . '/../../..' . '/lib/public/Settings/SetupChecks/ISetupCheck.php',
497498
'OCP\\Share' => __DIR__ . '/../../..' . '/lib/public/Share.php',
498499
'OCP\\Share\\Events\\ShareCreatedEvent' => __DIR__ . '/../../..' . '/lib/public/Share/Events/ShareCreatedEvent.php',
499500
'OCP\\Share\\Events\\VerifyMountPointEvent' => __DIR__ . '/../../..' . '/lib/public/Share/Events/VerifyMountPointEvent.php',
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright 2020 Daniel Kesselberg <mail@danielkesselberg.de>
7+
*
8+
* @author Daniel Kesselberg <mail@danielkesselberg.de>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCP\Settings\SetupChecks;
28+
29+
/**
30+
* Interface to implement for setup checks.
31+
*
32+
* There is no way (yet) for apps to register setup checks.
33+
*
34+
* @since 21.0.0
35+
*/
36+
interface ISetupCheck {
37+
/**
38+
* @since 21.0.0
39+
*/
40+
public const SEVERITY_INFO = 'info';
41+
42+
/**
43+
* @since 21.0.0
44+
*/
45+
public const SEVERITY_WARNING = 'warning';
46+
47+
/**
48+
* @since 21.0.0
49+
*/
50+
public const SEVERITY_ERROR = 'error';
51+
52+
/**
53+
* A description about the setup check
54+
*
55+
* @return string
56+
* @since 21.0.0
57+
*/
58+
public function description(): string;
59+
60+
/**
61+
* Severity of setup check
62+
*
63+
* @psalm-return ISetupCheck::SEVERITY*
64+
* @return string
65+
* @since 21.0.0
66+
*/
67+
public function severity(): string;
68+
69+
/**
70+
* Execute the setup check. Make sure the setup check is idempotent.
71+
*
72+
* @return bool
73+
* @since 21.0.0
74+
*/
75+
public function passes(): bool;
76+
77+
/**
78+
* A absolute link to the documentation. An empty string if no documentation is available.
79+
*
80+
* @return string
81+
* @since 21.0.0
82+
*/
83+
public function linkToDocumentation(): ?string;
84+
}

0 commit comments

Comments
 (0)