Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(encryption): Migrate from hooks to events #48560

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
9883838
feat(encryption): Migrate from hooks to events
come-nc Oct 3, 2024
b611bcf
fix(tests): Unregister encryption modules in ViewTest to avoid side e…
come-nc Nov 5, 2024
98a23c6
fix(tests): Remove Encryption disabling in ViewTest to avoid side eff…
come-nc Nov 26, 2024
d60355d
fix(encryption): Fix filesize for part files in Encryption wrapper
come-nc Nov 26, 2024
ab6cd32
fix: Fix mtime preservation when moving a directory across storages w…
come-nc Nov 26, 2024
31fd585
fix(tests): Disable encryption wrapper when it makes sense
come-nc Nov 26, 2024
cf60adf
chore(files_versions): Only mock getSystemValue method to avoid probl…
come-nc Nov 26, 2024
e209d65
chore(trashbin): Fix configuration mocking in trashbin tests
come-nc Nov 26, 2024
eb56ecd
chore: Update psalm baseline to remove fixed issue
come-nc Nov 26, 2024
9f8707b
fix(tests): Avoid user login before a private key is setup
come-nc Nov 28, 2024
68bbe63
fix(admin_audit): Survive if file change id after rename (it should not)
come-nc Nov 28, 2024
60ce725
fix(encryption): Fix a PHP error in Encryption Util in specific situa…
come-nc Jan 13, 2025
b1260a4
fix: Preserve file id when moving from object store even if encryptio…
come-nc Jan 13, 2025
07c3c99
chore: Assert rename success in versionning tests
come-nc Jan 16, 2025
d5fcd9b
chore: WIP - debug for CI
come-nc Jan 20, 2025
e95bf85
fix(encryption): Improve Update class and event listenening
come-nc Feb 11, 2025
c60c679
fixup! fix(encryption): Improve Update class and event listenening
come-nc Feb 11, 2025
840489b
WIP debug 2
come-nc Feb 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 2 additions & 21 deletions apps/admin_audit/lib/Actions/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use OC\Files\Node\NonExistingFile;
use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
use OCP\Files\Events\Node\BeforeNodeReadEvent;
use OCP\Files\Events\Node\BeforeNodeRenamedEvent;
use OCP\Files\Events\Node\NodeCopiedEvent;
use OCP\Files\Events\Node\NodeCreatedEvent;
use OCP\Files\Events\Node\NodeRenamedEvent;
Expand All @@ -26,9 +25,6 @@
* @package OCA\AdminAudit\Actions
*/
class Files extends Action {

private array $renamedNodes = [];

/**
* Logs file read actions
*/
Expand All @@ -52,31 +48,16 @@ public function read(BeforeNodeReadEvent $event): void {
);
}

/**
* Logs rename actions of files
*/
public function beforeRename(BeforeNodeRenamedEvent $event): void {
try {
$source = $event->getSource();
$this->renamedNodes[$source->getId()] = $source;
} catch (InvalidPathException|NotFoundException $e) {
Server::get(LoggerInterface::class)->error(
'Exception thrown in file rename: ' . $e->getMessage(), ['app' => 'admin_audit', 'exception' => $e]
);
return;
}
}

/**
* Logs rename actions of files
*/
public function afterRename(NodeRenamedEvent $event): void {
try {
$target = $event->getTarget();
$originalSource = $this->renamedNodes[$target->getId()];
$source = $event->getSource();
$params = [
'newid' => $target->getId(),
'oldpath' => mb_substr($originalSource->getInternalPath(), 5),
'oldpath' => mb_substr($source->getInternalPath(), 5),
'newpath' => mb_substr($target->getInternalPath(), 5),
];
} catch (InvalidPathException|NotFoundException $e) {
Expand Down
8 changes: 0 additions & 8 deletions apps/admin_audit/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
use OCP\Files\Events\Node\BeforeNodeReadEvent;
use OCP\Files\Events\Node\BeforeNodeRenamedEvent;
use OCP\Files\Events\Node\NodeCopiedEvent;
use OCP\Files\Events\Node\NodeCreatedEvent;
use OCP\Files\Events\Node\NodeRenamedEvent;
Expand Down Expand Up @@ -168,13 +167,6 @@ private function tagHooks(IAuditLogger $logger,
private function fileHooks(IAuditLogger $logger, IEventDispatcher $eventDispatcher): void {
$fileActions = new Files($logger);

$eventDispatcher->addListener(
BeforeNodeRenamedEvent::class,
function (BeforeNodeRenamedEvent $event) use ($fileActions): void {
$fileActions->beforeRename($event);
}
);

$eventDispatcher->addListener(
NodeRenamedEvent::class,
function (NodeRenamedEvent $event) use ($fileActions): void {
Expand Down
7 changes: 6 additions & 1 deletion apps/files_sharing/tests/EncryptedSizePropagationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ class EncryptedSizePropagationTest extends SizePropagationTest {
use EncryptionTrait;

protected function setupUser($name, $password = '') {
$this->config->setAppValue('encryption', 'useMasterKey', '0');
$this->createUser($name, $password);
$tmpFolder = \OC::$server->getTempManager()->getTemporaryFolder();
$this->registerMount($name, '\OC\Files\Storage\Local', '/' . $name, ['datadir' => $tmpFolder]);
$this->config->setAppValue('encryption', 'useMasterKey', '0');
$this->setupForUser($name, $password);
$this->loginWithEncryption($name);
return new View('/' . $name . '/files');
}

protected function loginHelper($user, $create = false, $password = false) {
$this->setupForUser($user, $password);
parent::loginHelper($user, $create, $password);
}
}
4 changes: 2 additions & 2 deletions apps/files_sharing/tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected function setUp(): void {
\OC::$server->get(DisplayNameCache::class)->clear();

//login as user1
self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
$this->loginHelper(self::TEST_FILES_SHARING_API_USER1);

$this->data = 'foobar';
$this->view = new View('/' . self::TEST_FILES_SHARING_API_USER1 . '/files');
Expand Down Expand Up @@ -167,7 +167,7 @@ public static function tearDownAfterClass(): void {
* @param bool $create
* @param bool $password
*/
protected static function loginHelper($user, $create = false, $password = false) {
protected function loginHelper($user, $create = false, $password = false) {
if ($password === false) {
$password = $user;
}
Expand Down
19 changes: 6 additions & 13 deletions apps/files_trashbin/tests/TrashbinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Constants;
use OCP\Files\FileInfo;
use OCP\IConfig;
use OCP\Server;
use OCP\Share\IShare;

/**
Expand Down Expand Up @@ -110,8 +110,11 @@ protected function setUp(): void {

\OC::$server->getAppManager()->enableApp('files_trashbin');
$config = \OC::$server->getConfig();
$mockConfig = $this->createMock(IConfig::class);
$mockConfig
$mockConfig = $this->getMockBuilder(AllConfig::class)
->onlyMethods(['getSystemValue'])
->setConstructorArgs([Server::get(\OC\SystemConfig::class)])
->getMock();
$mockConfig->expects($this->any())
->method('getSystemValue')
->willReturnCallback(static function ($key, $default) use ($config) {
if ($key === 'filesystem_check_changes') {
Expand All @@ -120,16 +123,6 @@ protected function setUp(): void {
return $config->getSystemValue($key, $default);
}
});
$mockConfig
->method('getUserValue')
->willReturnCallback(static function ($userId, $appName, $key, $default = '') use ($config) {
return $config->getUserValue($userId, $appName, $key, $default);
});
$mockConfig
->method('getAppValue')
->willReturnCallback(static function ($appName, $key, $default = '') use ($config) {
return $config->getAppValue($appName, $key, $default);
});
$this->overwriteService(AllConfig::class, $mockConfig);

$this->trashRoot1 = '/' . self::TEST_TRASHBIN_USER1 . '/files_trashbin';
Expand Down
6 changes: 3 additions & 3 deletions apps/files_versions/lib/Listener/FileEventsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public function post_write_hook(Node $node): void {
/**
* Erase versions of deleted file
*
* This function is connected to the delete signal of OC_Filesystem
* This function is connected to the NodeDeletedEvent event
* cleanup the versions directory if the actual file gets deleted
*/
public function remove_hook(Node $node): void {
Expand Down Expand Up @@ -273,7 +273,7 @@ public function pre_remove_hook(Node $node): void {
/**
* rename/move versions of renamed/moved files
*
* This function is connected to the rename signal of OC_Filesystem and adjust the name and location
* This function is connected to the NodeRenamedEvent event and adjust the name and location
* of the stored versions along the actual file
*/
public function rename_hook(Node $source, Node $target): void {
Expand All @@ -292,7 +292,7 @@ public function rename_hook(Node $source, Node $target): void {
/**
* copy versions of copied files
*
* This function is connected to the copy signal of OC_Filesystem and copies the
* This function is connected to the NodeCopiedEvent event and copies the
* the stored versions to the new location
*/
public function copy_hook(Node $source, Node $target): void {
Expand Down
24 changes: 21 additions & 3 deletions apps/files_versions/tests/VersioningTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use OCA\Files_Versions\Versions\IVersionManager;
use OCP\Constants;
use OCP\Files\IMimeTypeLoader;
use OCP\IConfig;
use OCP\IUser;
use OCP\Server;
use OCP\Share\IShare;
Expand Down Expand Up @@ -79,7 +78,10 @@ protected function setUp(): void {
parent::setUp();

$config = \OC::$server->getConfig();
$mockConfig = $this->createMock(IConfig::class);
$mockConfig = $this->getMockBuilder(AllConfig::class)
->onlyMethods(['getSystemValue'])
->setConstructorArgs([Server::get(\OC\SystemConfig::class)])
->getMock();
$mockConfig->expects($this->any())
->method('getSystemValue')
->willReturnCallback(function ($key, $default) use ($config) {
Expand Down Expand Up @@ -423,8 +425,24 @@ public function testMoveFileIntoSharedFolderAsRecipient(): void {
$this->rootView->file_put_contents($v2, 'version2');

// move file into the shared folder as recipient
Filesystem::rename('/test.txt', '/folder1/test.txt');
$success = Filesystem::rename('/test.txt', '/folder1/test.txt');

// TODO: Voir is $v2 existe pour voir si c’est la copie ou la suppression qui foire
// Mettre du debug dans Wrapper/Encrytion::copyBetweenStorage c’est le suspect n-1
$versionsFolder1 = '/' . self::TEST_VERSIONS_USER . '/files_versions';

$v1Renamed = $versionsFolder1 . '/folder1/test.txt.v' . $t1;
$v2Renamed = $versionsFolder1 . '/folder1/test.txt.v' . $t2;
var_dump([
'success' => $success,
'source exists' => Filesystem::file_exists('/test.txt'),
'target exists' => Filesystem::file_exists('/folder1/test.txt'),
'v1 exists' => $this->rootView->file_exists($v1),
'v2 exists' => $this->rootView->file_exists($v2),
'v1Renamed exists' => $this->rootView->file_exists($v1Renamed),
'v2Renamed exists' => $this->rootView->file_exists($v2Renamed),
]);
$this->assertTrue($success); // renvoi true :-O
$this->assertFalse($this->rootView->file_exists($v1));
$this->assertFalse($this->rootView->file_exists($v2));

Expand Down
3 changes: 0 additions & 3 deletions build/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2031,9 +2031,6 @@
</UndefinedInterfaceMethod>
</file>
<file src="lib/private/Files/Storage/Wrapper/Encryption.php">
<InvalidOperand>
<code><![CDATA[$this->copyFromStorage($sourceStorage, $sourceInternalPath . '/' . $file, $targetInternalPath . '/' . $file, false, $isRename)]]></code>
</InvalidOperand>
<InvalidReturnStatement>
<code><![CDATA[$result]]></code>
</InvalidReturnStatement>
Expand Down
14 changes: 7 additions & 7 deletions lib/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@
* SPDX-FileCopyrightText: 2013-2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/
use OC\Encryption\HookManager;
use OC\Share20\Hooks;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Events\BeforeFileSystemSetupEvent;
use OCP\Group\Events\UserRemovedEvent;
use OCP\ILogger;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUserSession;
use OCP\Security\Bruteforce\IThrottler;
use OCP\Server;
use OCP\Share;
use OCP\User\Events\UserChangedEvent;
use Psr\Log\LoggerInterface;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
Expand Down Expand Up @@ -868,15 +867,16 @@ public static function registerCleanupHooks(\OC\SystemConfig $systemConfig): voi
}

private static function registerEncryptionWrapperAndHooks(): void {
/** @var \OC\Encryption\Manager */
$manager = Server::get(\OCP\Encryption\IManager::class);
\OCP\Util::connectHook('OC_Filesystem', 'preSetup', $manager, 'setupStorage');
Server::get(IEventDispatcher::class)->addListener(
BeforeFileSystemSetupEvent::class,
$manager->setupStorage(...),
);

$enabled = $manager->isEnabled();
if ($enabled) {
\OCP\Util::connectHook(Share::class, 'post_shared', HookManager::class, 'postShared');
\OCP\Util::connectHook(Share::class, 'post_unshare', HookManager::class, 'postUnshared');
\OCP\Util::connectHook('OC_Filesystem', 'post_rename', HookManager::class, 'postRename');
\OCP\Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', HookManager::class, 'postRestore');
\OC\Encryption\EncryptionEventListener::register(Server::get(IEventDispatcher::class));
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1515,6 +1515,7 @@
'OC\\DirectEditing\\Token' => $baseDir . '/lib/private/DirectEditing/Token.php',
'OC\\EmojiHelper' => $baseDir . '/lib/private/EmojiHelper.php',
'OC\\Encryption\\DecryptAll' => $baseDir . '/lib/private/Encryption/DecryptAll.php',
'OC\\Encryption\\EncryptionEventListener' => $baseDir . '/lib/private/Encryption/EncryptionEventListener.php',
'OC\\Encryption\\EncryptionWrapper' => $baseDir . '/lib/private/Encryption/EncryptionWrapper.php',
'OC\\Encryption\\Exceptions\\DecryptionFailedException' => $baseDir . '/lib/private/Encryption/Exceptions/DecryptionFailedException.php',
'OC\\Encryption\\Exceptions\\EmptyEncryptionDataException' => $baseDir . '/lib/private/Encryption/Exceptions/EmptyEncryptionDataException.php',
Expand All @@ -1525,7 +1526,6 @@
'OC\\Encryption\\Exceptions\\ModuleDoesNotExistsException' => $baseDir . '/lib/private/Encryption/Exceptions/ModuleDoesNotExistsException.php',
'OC\\Encryption\\Exceptions\\UnknownCipherException' => $baseDir . '/lib/private/Encryption/Exceptions/UnknownCipherException.php',
'OC\\Encryption\\File' => $baseDir . '/lib/private/Encryption/File.php',
'OC\\Encryption\\HookManager' => $baseDir . '/lib/private/Encryption/HookManager.php',
'OC\\Encryption\\Keys\\Storage' => $baseDir . '/lib/private/Encryption/Keys/Storage.php',
'OC\\Encryption\\Manager' => $baseDir . '/lib/private/Encryption/Manager.php',
'OC\\Encryption\\Update' => $baseDir . '/lib/private/Encryption/Update.php',
Expand Down
2 changes: 1 addition & 1 deletion lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1564,6 +1564,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\DirectEditing\\Token' => __DIR__ . '/../../..' . '/lib/private/DirectEditing/Token.php',
'OC\\EmojiHelper' => __DIR__ . '/../../..' . '/lib/private/EmojiHelper.php',
'OC\\Encryption\\DecryptAll' => __DIR__ . '/../../..' . '/lib/private/Encryption/DecryptAll.php',
'OC\\Encryption\\EncryptionEventListener' => __DIR__ . '/../../..' . '/lib/private/Encryption/EncryptionEventListener.php',
'OC\\Encryption\\EncryptionWrapper' => __DIR__ . '/../../..' . '/lib/private/Encryption/EncryptionWrapper.php',
'OC\\Encryption\\Exceptions\\DecryptionFailedException' => __DIR__ . '/../../..' . '/lib/private/Encryption/Exceptions/DecryptionFailedException.php',
'OC\\Encryption\\Exceptions\\EmptyEncryptionDataException' => __DIR__ . '/../../..' . '/lib/private/Encryption/Exceptions/EmptyEncryptionDataException.php',
Expand All @@ -1574,7 +1575,6 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Encryption\\Exceptions\\ModuleDoesNotExistsException' => __DIR__ . '/../../..' . '/lib/private/Encryption/Exceptions/ModuleDoesNotExistsException.php',
'OC\\Encryption\\Exceptions\\UnknownCipherException' => __DIR__ . '/../../..' . '/lib/private/Encryption/Exceptions/UnknownCipherException.php',
'OC\\Encryption\\File' => __DIR__ . '/../../..' . '/lib/private/Encryption/File.php',
'OC\\Encryption\\HookManager' => __DIR__ . '/../../..' . '/lib/private/Encryption/HookManager.php',
'OC\\Encryption\\Keys\\Storage' => __DIR__ . '/../../..' . '/lib/private/Encryption/Keys/Storage.php',
'OC\\Encryption\\Manager' => __DIR__ . '/../../..' . '/lib/private/Encryption/Manager.php',
'OC\\Encryption\\Update' => __DIR__ . '/../../..' . '/lib/private/Encryption/Update.php',
Expand Down
Loading
Loading