Log and continue when failing to update encryption keys during for individual files

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2021-03-11 11:32:29 +01:00 committed by Vincent Petry
parent 1286848731
commit 76170a58eb
No known key found for this signature in database
GPG Key ID: E055D6A4D513575C
4 changed files with 43 additions and 25 deletions

View File

@ -31,6 +31,7 @@ use OC\Memcache\ArrayCache;
use OCP\Files\Mount\IMountPoint; use OCP\Files\Mount\IMountPoint;
use OCP\Files\Storage; use OCP\Files\Storage;
use OCP\ILogger; use OCP\ILogger;
use Psr\Log\LoggerInterface;
/** /**
* Class EncryptionWrapper * Class EncryptionWrapper
@ -100,6 +101,7 @@ class EncryptionWrapper {
Filesystem::getMountManager(), Filesystem::getMountManager(),
$this->manager, $this->manager,
$fileHelper, $fileHelper,
\OC::$server->get(LoggerInterface::class),
$uid $uid
); );
return new Encryption( return new Encryption(

View File

@ -25,6 +25,7 @@ namespace OC\Encryption;
use OC\Files\Filesystem; use OC\Files\Filesystem;
use OC\Files\View; use OC\Files\View;
use Psr\Log\LoggerInterface;
class HookManager { class HookManager {
/** /**
@ -67,6 +68,7 @@ class HookManager {
Filesystem::getMountManager(), Filesystem::getMountManager(),
\OC::$server->getEncryptionManager(), \OC::$server->getEncryptionManager(),
\OC::$server->getEncryptionFilesHelper(), \OC::$server->getEncryptionFilesHelper(),
\OC::$server->get(LoggerInterface::class),
$uid $uid
); );
} }

View File

@ -26,40 +26,43 @@
namespace OC\Encryption; namespace OC\Encryption;
use Exception;
use InvalidArgumentException;
use OC;
use OC\Files\Filesystem; use OC\Files\Filesystem;
use OC\Files\Mount; use OC\Files\Mount;
use OC\Files\View; use OC\Files\View;
use OCP\Encryption\Exceptions\GenericEncryptionException;
use OCP\ILogger;
use Psr\Log\LoggerInterface;
/** /**
* update encrypted files, e.g. because a file was shared * update encrypted files, e.g. because a file was shared
*/ */
class Update { class Update {
/** @var \OC\Files\View */ /** @var View */
protected $view; protected $view;
/** @var \OC\Encryption\Util */ /** @var Util */
protected $util; protected $util;
/** @var \OC\Files\Mount\Manager */ /** @var \OC\Files\Mount\Manager */
protected $mountManager; protected $mountManager;
/** @var \OC\Encryption\Manager */ /** @var Manager */
protected $encryptionManager; protected $encryptionManager;
/** @var string */ /** @var string */
protected $uid; protected $uid;
/** @var \OC\Encryption\File */ /** @var File */
protected $file; protected $file;
/** @var LoggerInterface */
protected $logger;
/** /**
*
* @param \OC\Files\View $view
* @param \OC\Encryption\Util $util
* @param \OC\Files\Mount\Manager $mountManager
* @param \OC\Encryption\Manager $encryptionManager
* @param \OC\Encryption\File $file
* @param string $uid * @param string $uid
*/ */
public function __construct( public function __construct(
@ -68,6 +71,7 @@ class Update {
Mount\Manager $mountManager, Mount\Manager $mountManager,
Manager $encryptionManager, Manager $encryptionManager,
File $file, File $file,
LoggerInterface $logger,
$uid $uid
) { ) {
$this->view = $view; $this->view = $view;
@ -75,6 +79,7 @@ class Update {
$this->mountManager = $mountManager; $this->mountManager = $mountManager;
$this->encryptionManager = $encryptionManager; $this->encryptionManager = $encryptionManager;
$this->file = $file; $this->file = $file;
$this->logger = $logger;
$this->uid = $uid; $this->uid = $uid;
} }
@ -155,7 +160,7 @@ class Update {
$view = new View('/' . $owner . '/files'); $view = new View('/' . $owner . '/files');
$path = $view->getPath($info->getId()); $path = $view->getPath($info->getId());
if ($path === null) { if ($path === null) {
throw new \InvalidArgumentException('No file found for ' . $info->getId()); throw new InvalidArgumentException('No file found for ' . $info->getId());
} }
return [$owner, $path]; return [$owner, $path];
@ -187,7 +192,12 @@ class Update {
foreach ($allFiles as $file) { foreach ($allFiles as $file) {
$usersSharing = $this->file->getAccessList($file); $usersSharing = $this->file->getAccessList($file);
try {
$encryptionModule->update($file, $this->uid, $usersSharing); $encryptionModule->update($file, $this->uid, $usersSharing);
} catch (GenericEncryptionException $e) {
// If the update of an individual file fails e.g. due to a corrupt key we should continue the operation and just log the failure
$this->logger->error('Failed to update encryption module for ' . $this->uid . ' ' . $file, [ 'exception' => $e ]);
}
} }
} }
} }

View File

@ -22,9 +22,13 @@
namespace Test\Encryption; namespace Test\Encryption;
use OC\Encryption\Update; use OC\Encryption\Update;
use OC\Encryption\Util;
use OC\Files\Mount\Manager; use OC\Files\Mount\Manager;
use OC\Files\View; use OC\Files\View;
use Psr\Log\LoggerInterface;
use Test\TestCase; use Test\TestCase;
use OC\Encryption\File;
use OCP\Encryption\IEncryptionModule;
class UpdateTest extends TestCase { class UpdateTest extends TestCase {
@ -37,7 +41,7 @@ class UpdateTest extends TestCase {
/** @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject */ /** @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject */
private $view; private $view;
/** @var \OC\Encryption\Util | \PHPUnit\Framework\MockObject\MockObject */ /** @var Util | \PHPUnit\Framework\MockObject\MockObject */
private $util; private $util;
/** @var \OC\Files\Mount\Manager | \PHPUnit\Framework\MockObject\MockObject */ /** @var \OC\Files\Mount\Manager | \PHPUnit\Framework\MockObject\MockObject */
@ -52,21 +56,19 @@ class UpdateTest extends TestCase {
/** @var \OC\Encryption\File | \PHPUnit\Framework\MockObject\MockObject */ /** @var \OC\Encryption\File | \PHPUnit\Framework\MockObject\MockObject */
private $fileHelper; private $fileHelper;
/** @var \PHPUnit\Framework\MockObject\MockObject|LoggerInterface */
private $logger;
protected function setUp(): void { protected function setUp(): void {
parent::setUp(); parent::setUp();
$this->view = $this->getMockBuilder(View::class) $this->view = $this->createMock(View::class);
->disableOriginalConstructor()->getMock(); $this->util = $this->createMock(Util::class);
$this->util = $this->getMockBuilder('\OC\Encryption\Util') $this->mountManager = $this->createMock(Manager::class);
->disableOriginalConstructor()->getMock(); $this->encryptionManager = $this->createMock(\OC\Encryption\Manager::class);
$this->mountManager = $this->getMockBuilder(Manager::class) $this->fileHelper = $this->createMock(File::class);
->disableOriginalConstructor()->getMock(); $this->encryptionModule = $this->createMock(IEncryptionModule::class);
$this->encryptionManager = $this->getMockBuilder('\OC\Encryption\Manager') $this->logger = $this->createMock(LoggerInterface::class);
->disableOriginalConstructor()->getMock();
$this->fileHelper = $this->getMockBuilder('\OC\Encryption\File')
->disableOriginalConstructor()->getMock();
$this->encryptionModule = $this->getMockBuilder('\OCP\Encryption\IEncryptionModule')
->disableOriginalConstructor()->getMock();
$this->uid = 'testUser1'; $this->uid = 'testUser1';
@ -76,6 +78,7 @@ class UpdateTest extends TestCase {
$this->mountManager, $this->mountManager,
$this->encryptionManager, $this->encryptionManager,
$this->fileHelper, $this->fileHelper,
$this->logger,
$this->uid); $this->uid);
} }
@ -223,6 +226,7 @@ class UpdateTest extends TestCase {
$this->mountManager, $this->mountManager,
$this->encryptionManager, $this->encryptionManager,
$this->fileHelper, $this->fileHelper,
$this->logger,
$this->uid $this->uid
] ]
)->setMethods($methods)->getMock(); )->setMethods($methods)->getMock();