Skip to content

Commit 8d02ee9

Browse files
authored
Merge pull request #21693 from nextcloud/fix/noid/import-certificates-only-by-system
Improve CertificateManager to not be user context dependent
2 parents 7a0ac37 + 9435ec2 commit 8d02ee9

26 files changed

Lines changed: 340 additions & 176 deletions

File tree

apps/dav/lib/CardDAV/SyncService.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
use OC\Accounts\AccountManager;
3333
use OCP\AppFramework\Http;
34-
use OCP\ICertificateManager;
3534
use OCP\ILogger;
3635
use OCP\IUser;
3736
use OCP\IUserManager;
@@ -155,8 +154,7 @@ protected function getCertPath() {
155154
return $this->certPath;
156155
}
157156

158-
/** @var ICertificateManager $certManager */
159-
$certManager = \OC::$server->getCertificateManager(null);
157+
$certManager = \OC::$server->getCertificateManager();
160158
$certPath = $certManager->getAbsoluteBundlePath();
161159
if (file_exists($certPath)) {
162160
$this->certPath = $certPath;

apps/files_sharing/lib/External/Manager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ public function getMount($data) {
441441
$data['manager'] = $this;
442442
$mountPoint = '/' . $this->uid . '/files' . $data['mountpoint'];
443443
$data['mountpoint'] = $mountPoint;
444-
$data['certificateManager'] = \OC::$server->getCertificateManager($this->uid);
444+
$data['certificateManager'] = \OC::$server->getCertificateManager();
445445
return new Mount(self::STORAGE, $mountPoint, $data, $this, $this->storageLoader);
446446
}
447447

apps/files_sharing/lib/External/MountProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function getMount(IUser $user, $data, IStorageFactory $storageFactory) {
6666
$mountPoint = '/' . $user->getUID() . '/files/' . ltrim($data['mountpoint'], '/');
6767
$data['mountpoint'] = $mountPoint;
6868
$data['cloudId'] = $this->cloudIdManager->getCloudId($data['owner'], $data['remote']);
69-
$data['certificateManager'] = \OC::$server->getCertificateManager($user->getUID());
69+
$data['certificateManager'] = \OC::$server->getCertificateManager();
7070
$data['HttpClientService'] = \OC::$server->getHTTPClientService();
7171
return new Mount(self::STORAGE, $mountPoint, $data, $manager, $storageFactory);
7272
}

apps/settings/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
'OCA\\Settings\\Settings\\Personal\\Security\\TwoFactor' => $baseDir . '/../lib/Settings/Personal/Security/TwoFactor.php',
5757
'OCA\\Settings\\Settings\\Personal\\Security\\WebAuthn' => $baseDir . '/../lib/Settings/Personal/Security/WebAuthn.php',
5858
'OCA\\Settings\\Settings\\Personal\\ServerDevNotice' => $baseDir . '/../lib/Settings/Personal/ServerDevNotice.php',
59+
'OCA\\Settings\\SetupChecks\\CheckUserCertificates' => $baseDir . '/../lib/SetupChecks/CheckUserCertificates.php',
5960
'OCA\\Settings\\SetupChecks\\LegacySSEKeyFormat' => $baseDir . '/../lib/SetupChecks/LegacySSEKeyFormat.php',
6061
'OCA\\Settings\\SetupChecks\\PhpDefaultCharset' => $baseDir . '/../lib/SetupChecks/PhpDefaultCharset.php',
6162
'OCA\\Settings\\SetupChecks\\PhpOutputBuffering' => $baseDir . '/../lib/SetupChecks/PhpOutputBuffering.php',

apps/settings/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class ComposerStaticInitSettings
7171
'OCA\\Settings\\Settings\\Personal\\Security\\TwoFactor' => __DIR__ . '/..' . '/../lib/Settings/Personal/Security/TwoFactor.php',
7272
'OCA\\Settings\\Settings\\Personal\\Security\\WebAuthn' => __DIR__ . '/..' . '/../lib/Settings/Personal/Security/WebAuthn.php',
7373
'OCA\\Settings\\Settings\\Personal\\ServerDevNotice' => __DIR__ . '/..' . '/../lib/Settings/Personal/ServerDevNotice.php',
74+
'OCA\\Settings\\SetupChecks\\CheckUserCertificates' => __DIR__ . '/..' . '/../lib/SetupChecks/CheckUserCertificates.php',
7475
'OCA\\Settings\\SetupChecks\\LegacySSEKeyFormat' => __DIR__ . '/..' . '/../lib/SetupChecks/LegacySSEKeyFormat.php',
7576
'OCA\\Settings\\SetupChecks\\PhpDefaultCharset' => __DIR__ . '/..' . '/../lib/SetupChecks/PhpDefaultCharset.php',
7677
'OCA\\Settings\\SetupChecks\\PhpOutputBuffering' => __DIR__ . '/..' . '/../lib/SetupChecks/PhpOutputBuffering.php',

apps/settings/lib/AppInfo/Application.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,6 @@ public function register(IRegistrationContext $context): void {
9898
}
9999
return $isSubAdmin;
100100
});
101-
$context->registerService('userCertificateManager', function (IAppContainer $appContainer) {
102-
/** @var IServerContainer $serverContainer */
103-
$serverContainer = $appContainer->get(IServerContainer::class);
104-
return $serverContainer->getCertificateManager();
105-
}, false);
106-
$context->registerService('systemCertificateManager', function (IAppContainer $appContainer) {
107-
/** @var IServerContainer $serverContainer */
108-
$serverContainer = $appContainer->query('ServerContainer');
109-
return $serverContainer->getCertificateManager(null);
110-
}, false);
111101
$context->registerService(IProvider::class, function (IAppContainer $appContainer) {
112102
/** @var IServerContainer $serverContainer */
113103
$serverContainer = $appContainer->query(IServerContainer::class);

apps/settings/lib/Controller/CheckSetupController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
use OC\IntegrityCheck\Checker;
5454
use OC\Lock\NoopLockingProvider;
5555
use OC\MemoryInfo;
56+
use OCA\Settings\SetupChecks\CheckUserCertificates;
5657
use OCA\Settings\SetupChecks\LegacySSEKeyFormat;
5758
use OCA\Settings\SetupChecks\PhpDefaultCharset;
5859
use OCA\Settings\SetupChecks\PhpOutputBuffering;
@@ -692,6 +693,8 @@ public function check() {
692693
$phpDefaultCharset = new PhpDefaultCharset();
693694
$phpOutputBuffering = new PhpOutputBuffering();
694695
$legacySSEKeyFormat = new LegacySSEKeyFormat($this->l10n, $this->config, $this->urlGenerator);
696+
$checkUserCertificates = new CheckUserCertificates($this->l10n, $this->config, $this->urlGenerator);
697+
695698
return new DataResponse(
696699
[
697700
'isGetenvServerWorking' => !empty(getenv('PATH')),
@@ -734,6 +737,7 @@ public function check() {
734737
PhpDefaultCharset::class => ['pass' => $phpDefaultCharset->run(), 'description' => $phpDefaultCharset->description(), 'severity' => $phpDefaultCharset->severity()],
735738
PhpOutputBuffering::class => ['pass' => $phpOutputBuffering->run(), 'description' => $phpOutputBuffering->description(), 'severity' => $phpOutputBuffering->severity()],
736739
LegacySSEKeyFormat::class => ['pass' => $legacySSEKeyFormat->run(), 'description' => $legacySSEKeyFormat->description(), 'severity' => $legacySSEKeyFormat->severity(), 'linkToDocumentation' => $legacySSEKeyFormat->linkToDocumentation()],
740+
CheckUserCertificates::class => ['pass' => $checkUserCertificates->run(), 'description' => $checkUserCertificates->description(), 'severity' => $checkUserCertificates->severity(), 'elements' => $checkUserCertificates->elements()],
737741
]
738742
);
739743
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2020 Morris Jobke <hey@morrisjobke.de>
7+
*
8+
* @author Morris Jobke <hey@morrisjobke.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 OCA\Settings\SetupChecks;
28+
29+
use OCP\IConfig;
30+
use OCP\IL10N;
31+
use OCP\IURLGenerator;
32+
33+
class CheckUserCertificates {
34+
/** @var IL10N */
35+
private $l10n;
36+
/** @var string */
37+
private $configValue;
38+
/** @var IURLGenerator */
39+
private $urlGenerator;
40+
41+
public function __construct(IL10N $l10n, IConfig $config, IURLGenerator $urlGenerator) {
42+
$this->l10n = $l10n;
43+
$configValue = $config->getAppValue('files_external', 'user_certificate_scan', false);
44+
if (!is_string($configValue)) {
45+
$configValue = '';
46+
}
47+
$this->configValue = $configValue;
48+
$this->urlGenerator = $urlGenerator;
49+
}
50+
51+
public function description(): string {
52+
if ($this->configValue === '') {
53+
return '';
54+
}
55+
if ($this->configValue === 'not-run-yet') {
56+
return $this->l10n->t('A background job is pending that checks for user imported SSL certificates. Please check back later.');
57+
}
58+
return $this->l10n->t('There are some user imported SSL certificates present, that are not used anymore with Nextcloud 21. They can be imported on the command line via "occ security:certificates:import" command. Their paths inside the data directory are shown below.');
59+
}
60+
61+
public function severity(): string {
62+
return 'warning';
63+
}
64+
65+
public function run(): bool {
66+
// all fine if neither "not-run-yet" nor a result
67+
return $this->configValue === '';
68+
}
69+
70+
public function elements(): array {
71+
if ($this->configValue === '' || $this->configValue === 'not-run-yet') {
72+
return [];
73+
}
74+
$data = json_decode($this->configValue);
75+
if (!is_array($data)) {
76+
return [];
77+
}
78+
return $data;
79+
}
80+
}

apps/settings/tests/Controller/CheckSetupControllerTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,22 +382,26 @@ public function testForwardedHostPresentButTrustedProxiesEmpty() {
382382

383383
public function testCheck() {
384384
$this->config->expects($this->at(0))
385+
->method('getAppValue')
386+
->with('files_external', 'user_certificate_scan', false)
387+
->willReturn('["a", "b"]');
388+
$this->config->expects($this->at(1))
385389
->method('getAppValue')
386390
->with('core', 'cronErrors')
387391
->willReturn('');
388-
$this->config->expects($this->at(2))
392+
$this->config->expects($this->at(3))
389393
->method('getSystemValue')
390394
->with('connectivity_check_domains', ['www.nextcloud.com', 'www.startpage.com', 'www.eff.org', 'www.edri.org'])
391395
->willReturn(['www.nextcloud.com', 'www.startpage.com', 'www.eff.org', 'www.edri.org']);
392-
$this->config->expects($this->at(3))
396+
$this->config->expects($this->at(4))
393397
->method('getSystemValue')
394398
->with('memcache.local', null)
395399
->willReturn('SomeProvider');
396-
$this->config->expects($this->at(4))
400+
$this->config->expects($this->at(5))
397401
->method('getSystemValue')
398402
->with('has_internet_connection', true)
399403
->willReturn(true);
400-
$this->config->expects($this->at(5))
404+
$this->config->expects($this->at(6))
401405
->method('getSystemValue')
402406
->with('appstoreenabled', true)
403407
->willReturn(false);
@@ -594,6 +598,7 @@ public function testCheck() {
594598
'OCA\Settings\SetupChecks\PhpDefaultCharset' => ['pass' => true, 'description' => 'PHP configuration option default_charset should be UTF-8', 'severity' => 'warning'],
595599
'OCA\Settings\SetupChecks\PhpOutputBuffering' => ['pass' => true, 'description' => 'PHP configuration option output_buffering must be disabled', 'severity' => 'error'],
596600
'OCA\Settings\SetupChecks\LegacySSEKeyFormat' => ['pass' => true, 'description' => 'The old server-side-encryption format is enabled. We recommend disabling this.', 'severity' => 'warning', 'linkToDocumentation' => ''],
601+
'OCA\Settings\SetupChecks\CheckUserCertificates' => ['pass' => false, 'description' => 'There are some user imported SSL certificates present, that are not used anymore with Nextcloud 21. They can be imported on the command line via "occ security:certificates:import" command. Their paths inside the data directory are shown below.', 'severity' => 'warning', 'elements' => ['a', 'b']],
597602
'imageMagickLacksSVGSupport' => false,
598603
]
599604
);
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/**
3+
* @copyright 2020 Morris Jobke <hey@morrisjobke.de>
4+
*
5+
* @author Morris Jobke <hey@morrisjobke.de>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace OC\Core\BackgroundJobs;
25+
26+
use OC\BackgroundJob\QueuedJob;
27+
use OCP\Files\Folder;
28+
use OCP\Files\IRootFolder;
29+
use OCP\Files\NotFoundException;
30+
use OCP\IConfig;
31+
use OCP\IUser;
32+
use OCP\IUserManager;
33+
34+
class CheckForUserCertificates extends QueuedJob {
35+
36+
/** @var IConfig */
37+
protected $config;
38+
/** @var IUserManager */
39+
private $userManager;
40+
/** @var IRootFolder */
41+
private $rootFolder;
42+
43+
public function __construct(IConfig $config, IUserManager $userManager, IRootFolder $rootFolder) {
44+
$this->config = $config;
45+
$this->userManager = $userManager;
46+
$this->rootFolder = $rootFolder;
47+
}
48+
49+
/**
50+
* Checks all user directories for old user uploaded certificates
51+
*/
52+
public function run($arguments) {
53+
$uploadList = [];
54+
$this->userManager->callForSeenUsers(function (IUser $user) use (&$uploadList) {
55+
$userId = $user->getUID();
56+
try {
57+
\OC_Util::setupFS($userId);
58+
$filesExternalUploadsFolder = $this->rootFolder->get($userId . '/files_external/uploads');
59+
} catch (NotFoundException $e) {
60+
\OC_Util::tearDownFS();
61+
return;
62+
}
63+
if ($filesExternalUploadsFolder instanceof Folder) {
64+
$files = $filesExternalUploadsFolder->getDirectoryListing();
65+
foreach ($files as $file) {
66+
$filename = $file->getName();
67+
$uploadList[] = "$userId/files_external/uploads/$filename";
68+
}
69+
}
70+
\OC_Util::tearDownFS();
71+
});
72+
73+
if (empty($uploadList)) {
74+
$this->config->deleteAppValue('files_external', 'user_certificate_scan');
75+
} else {
76+
$this->config->setAppValue('files_external', 'user_certificate_scan', json_encode($uploadList));
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)