Skip to content

Commit 3c18d34

Browse files
authored
Merge pull request #635 from nextcloud/backport/633/stable5.12
[stable5.12] fix: allow server-side encryption if the master key is used
2 parents 014713d + 031c6ef commit 3c18d34

6 files changed

Lines changed: 54 additions & 19 deletions

File tree

lib/BackgroundJob/PreviewJob.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace OCA\PreviewGenerator\BackgroundJob;
1111

12+
use OCA\PreviewGenerator\Exceptions\EncryptionEnabledException;
1213
use OCA\PreviewGenerator\Service\ConfigService;
1314
use OCA\PreviewGenerator\Service\PreGenerateService;
1415
use OCA\PreviewGenerator\Support\OutputInterfaceLoggerAdapter;
@@ -54,6 +55,11 @@ protected function run($argument) {
5455
}
5556

5657
$this->preGenerateService->setLimiter($this->limiter);
57-
$this->preGenerateService->preGenerate($this->outputInterface);
58+
59+
try {
60+
$this->preGenerateService->preGenerate($this->outputInterface);
61+
} catch (EncryptionEnabledException $e) {
62+
// Just skip the job silently
63+
}
5864
}
5965
}

lib/Command/Generate.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111

1212
use OC\DB\Exceptions\DbalException;
1313
use OCA\Files_External\Service\GlobalStoragesService;
14+
use OCA\PreviewGenerator\Exceptions\EncryptionEnabledException;
1415
use OCA\PreviewGenerator\Model\WorkerConfig;
16+
use OCA\PreviewGenerator\Service\EncryptionService;
1517
use OCA\PreviewGenerator\Service\ModuloService;
1618
use OCA\PreviewGenerator\SizeHelper;
1719
use OCP\DB\Exception;
18-
use OCP\Encryption\IManager;
1920
use OCP\Files\File;
2021
use OCP\Files\Folder;
2122
use OCP\Files\GenericFileException;
@@ -48,7 +49,7 @@ class Generate extends Command {
4849
protected IPreview $previewGenerator;
4950
protected IConfig $config;
5051
protected OutputInterface $output;
51-
protected IManager $encryptionManager;
52+
protected EncryptionService $encryptionService;
5253
protected SizeHelper $sizeHelper;
5354

5455
private ?WorkerConfig $workerConfig = null;
@@ -57,7 +58,7 @@ public function __construct(IRootFolder $rootFolder,
5758
IUserManager $userManager,
5859
IPreview $previewGenerator,
5960
IConfig $config,
60-
IManager $encryptionManager,
61+
EncryptionService $encryptionService,
6162
ContainerInterface $container,
6263
SizeHelper $sizeHelper) {
6364
parent::__construct();
@@ -66,7 +67,7 @@ public function __construct(IRootFolder $rootFolder,
6667
$this->rootFolder = $rootFolder;
6768
$this->previewGenerator = $previewGenerator;
6869
$this->config = $config;
69-
$this->encryptionManager = $encryptionManager;
70+
$this->encryptionService = $encryptionService;
7071
$this->sizeHelper = $sizeHelper;
7172

7273
try {
@@ -98,8 +99,8 @@ protected function configure(): void {
9899
}
99100

100101
protected function execute(InputInterface $input, OutputInterface $output): int {
101-
if ($this->encryptionManager->isEnabled()) {
102-
$output->writeln('<error>Encryption is enabled. Aborted.</error>');
102+
if (!$this->encryptionService->isCompatibleWithCurrentEncryption()) {
103+
$output->writeln('<error>' . EncryptionEnabledException::DEFAULT_MESSAGE . '</error>');
103104
return 1;
104105
}
105106

lib/Command/PreGenerate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3232
try {
3333
$this->preGenerateService->preGenerate($output);
3434
} catch (EncryptionEnabledException $e) {
35-
$output->writeln('<error>Encryption is enabled. Aborted.</error>');
35+
$output->writeln('<error>' . EncryptionEnabledException::DEFAULT_MESSAGE . '</error>');
3636
return 1;
3737
}
3838

lib/Exceptions/EncryptionEnabledException.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,7 @@
1010
namespace OCA\PreviewGenerator\Exceptions;
1111

1212
use Exception;
13-
use Throwable;
1413

1514
class EncryptionEnabledException extends Exception {
16-
public const DEFAULT_MESSAGE = 'Encryption is enabled';
17-
18-
public function __construct(string $message = '', int $code = 0, ?Throwable $previous = null) {
19-
parent::__construct($message ?? self::DEFAULT_MESSAGE, $code, $previous);
20-
}
15+
public const DEFAULT_MESSAGE = 'Encryption is enabled without the master key';
2116
}

lib/Service/EncryptionService.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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\PreviewGenerator\Service;
11+
12+
use OC\Encryption\Exceptions\ModuleDoesNotExistsException;
13+
use OCP\Encryption\IManager as IEncryptionManager;
14+
15+
class EncryptionService {
16+
public function __construct(
17+
private readonly IEncryptionManager $encryptionManager,
18+
) {
19+
}
20+
21+
public function isCompatibleWithCurrentEncryption(): bool {
22+
if (!$this->encryptionManager->isEnabled()) {
23+
return true;
24+
}
25+
26+
try {
27+
$encryptionModule = $this->encryptionManager->getEncryptionModule();
28+
} catch (ModuleDoesNotExistsException $e) {
29+
return false;
30+
}
31+
32+
return !$encryptionModule->needDetailedAccessList();
33+
}
34+
}

lib/Service/PreGenerateService.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use OCP\AppFramework\Db\TTransactional;
1919
use OCP\AppFramework\Utility\ITimeFactory;
2020
use OCP\DB\Exception;
21-
use OCP\Encryption\IManager;
2221
use OCP\Files\File;
2322
use OCP\Files\GenericFileException;
2423
use OCP\Files\IRootFolder;
@@ -44,7 +43,7 @@ public function __construct(
4443
private IPreview $previewGenerator,
4544
private IConfig $config,
4645
private IDBConnection $connection,
47-
private IManager $encryptionManager,
46+
private EncryptionService $encryptionService,
4847
private ITimeFactory $time,
4948
private SizeHelper $sizeHelper,
5049
private NoMediaService $noMediaService,
@@ -56,11 +55,11 @@ public function setLimiter(PreviewLimiter $limiter): void {
5655
}
5756

5857
/**
59-
* @throws EncryptionEnabledException If encryption is enabled.
58+
* @throws EncryptionEnabledException If encryption is enabled without the master key.
6059
*/
6160
public function preGenerate(OutputInterface $output): void {
62-
if ($this->encryptionManager->isEnabled()) {
63-
throw new EncryptionEnabledException();
61+
if (!$this->encryptionService->isCompatibleWithCurrentEncryption()) {
62+
throw new EncryptionEnabledException(EncryptionEnabledException::DEFAULT_MESSAGE);
6463
}
6564

6665
// Set timestamp output

0 commit comments

Comments
 (0)