Skip to content

Commit 9c57d1f

Browse files
committed
fix: allow server-side encryption if the master key is used
Signed-off-by: Richard Steinmetz <richard@steinmetz.cloud>
1 parent b484aff commit 9c57d1f

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\PreviewLimiter\CountLimiter;
@@ -55,6 +56,11 @@ protected function run($argument) {
5556

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

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
@@ -41,7 +41,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4141
try {
4242
$this->preGenerateService->preGenerate();
4343
} catch (EncryptionEnabledException $e) {
44-
$output->writeln('<error>Encryption is enabled. Aborted.</error>');
44+
$output->writeln('<error>' . EncryptionEnabledException::DEFAULT_MESSAGE . '</error>');
4545
return 1;
4646
}
4747

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
@@ -16,7 +16,6 @@
1616
use OCP\AppFramework\Db\TTransactional;
1717
use OCP\AppFramework\Utility\ITimeFactory;
1818
use OCP\DB\Exception;
19-
use OCP\Encryption\IManager;
2019
use OCP\Files\File;
2120
use OCP\Files\GenericFileException;
2221
use OCP\Files\IRootFolder;
@@ -42,7 +41,7 @@ public function __construct(
4241
private IPreview $previewGenerator,
4342
private IConfig $config,
4443
private IDBConnection $connection,
45-
private IManager $encryptionManager,
44+
private EncryptionService $encryptionService,
4645
private ITimeFactory $time,
4746
private SizeHelper $sizeHelper,
4847
private NoMediaService $noMediaService,
@@ -58,11 +57,11 @@ public function setLimiter(PreviewLimiter $limiter): void {
5857
}
5958

6059
/**
61-
* @throws EncryptionEnabledException If encryption is enabled.
60+
* @throws EncryptionEnabledException If encryption is enabled without the master key.
6261
*/
6362
public function preGenerate(): void {
64-
if ($this->encryptionManager->isEnabled()) {
65-
throw new EncryptionEnabledException();
63+
if (!$this->encryptionService->isCompatibleWithCurrentEncryption()) {
64+
throw new EncryptionEnabledException(EncryptionEnabledException::DEFAULT_MESSAGE);
6665
}
6766

6867
if ($this->limiter) {

0 commit comments

Comments
 (0)